<?php
/**********************************************************************
***********************************************************************/
//-------------------------------------------------------------------------------------------------------------
//
//	FIXME - this should be revised for transaction update case.
//


//----------------------------------------------------------------------------------------

function get_sql_for_grn_inquiry($after_date, $to_date, $supplier_id=ALL_TEXT, $po_no = 0, $grn_no = 0, $project_id = 0)
{
    $date_after = date2sql($after_date);
    $date_to = date2sql($to_date);

    $sql2 = "SELECT ".ST_SUPPRECEIVE." as type, 
            trans.id as trans_no,
            trans.reference, 
            supplier.supp_name, 
            pm.description project_description,
            delivery_date as tran_date, 
            '' as due_date,
            supplier.curr_code, 
            '' AS TotalAmount,
            '' AS Allocated,
            0 as OverDue,
            1 as Settled,
            po.order_no 
    FROM ".TB_PREF."grn_batch as trans, "
        .TB_PREF."suppliers as supplier, "
        .TB_PREF."purch_orders as po, "
        .TB_PREF."projects_master pm 
    WHERE supplier.supplier_id = trans.supplier_id
        AND trans.purch_order_no = po.order_no 
        AND po.project_id = pm.project_id ";
        

    if ($po_no > 0 || $grn_no > 0) {
        if ($po_no > 0) {
   		$sql2 .= " AND po.order_no = ".db_escape($po_no);
   	}
    
        if ($grn_no > 0) {
   		$sql2 .= " AND trans.id = ".db_escape($grn_no);
   	}
    }
    else{
        $sql2 .= " AND trans.delivery_date >= '$date_after' AND trans.delivery_date <= '$date_to'";
        if ($supplier_id != ALL_TEXT) {
                $sql2 .= " AND trans.supplier_id = ".db_escape($supplier_id);
        }
        if ($project_id > 0) {
   		$sql2 .= " AND pm.project_id = ".db_escape($project_id);
   	}
    }
    
   return $sql2;
}

function get_stock_item_previous_grn_lines($item_code)
{
    $sql = "SELECT grni.*, grnb.delivery_date, grnb.purch_order_no 
            FROM ".TB_PREF."grn_items grni 
                JOIN ".TB_PREF."grn_batch grnb 
                    ON grnb.id = grni.grn_batch_id 
            WHERE grni.item_code = ".db_escape($item_code)." 
                AND grni.unit_cost > 0 
            ORDER BY grnb.purch_order_no 
            DESC LIMIT 5";
    
    return db_query($sql,"get_stock_item_previous_grn_lines");
}
?>