- $sql = "SELECT ".TB_PREF."grn_batch.*, ".TB_PREF."grn_items.*, "
- .TB_PREF."purch_order_details.unit_price,"
- .TB_PREF."purch_order_details.act_price,"
- .TB_PREF."purch_order_details.quantity_ordered,"
- .TB_PREF."purch_order_details.std_cost_unit, units
- FROM ".TB_PREF."grn_batch, ".TB_PREF."grn_items, "
- .TB_PREF."purch_order_details, ".TB_PREF."stock_master";
- if ($invoice_no != 0)
- $sql .= ", ".TB_PREF."supp_invoice_items";
- $sql .= " WHERE ".TB_PREF."grn_items.grn_batch_id=".TB_PREF."grn_batch.id
- AND ".TB_PREF."grn_items.po_detail_item=".TB_PREF."purch_order_details.po_detail_item";
- if ($invoice_no != 0)
- $sql .= " AND ".TB_PREF."supp_invoice_items.supp_trans_type=".ST_SUPPINVOICE." AND
- ".TB_PREF."supp_invoice_items.supp_trans_no=$invoice_no AND
- ".TB_PREF."grn_items.id=".TB_PREF."supp_invoice_items.grn_item_id";
- $sql .= " AND ".TB_PREF."stock_master.stock_id=".TB_PREF."grn_items.item_code ";
+ $sql = "SELECT grn.*, grn_item.*, po_item.delivery_date as req_del_date,
+ grn_item.quantity_inv".($invoice_no ? "-invoice_item.quantity" : '')." quantity_inv,
+ po_item.unit_price,
+ po_item.act_price,
+ po_item.quantity_ordered,
+ po_item.std_cost_unit, units
+ FROM ".TB_PREF."grn_batch grn, "
+ .TB_PREF."purch_order_details po_item, "
+ .TB_PREF."stock_master stock_item,"
+ .TB_PREF."grn_items grn_item ";
+
+ if ($invoice_no != 0) {
+ $sql .= " LEFT JOIN ".TB_PREF."supp_invoice_items invoice_item ON ";
+
+ $inv_cond = "invoice_item.supp_trans_type=".ST_SUPPINVOICE." AND invoice_item.supp_trans_no ";
+ if (is_array($invoice_no))
+ $inv_cond .= " IN (".implode(',' , $invoice_no) . ")";
+ else
+ $inv_cond .= " = $invoice_no";
+ $sql .= "($inv_cond) AND grn_item.id=invoice_item.grn_item_id";
+ }
+ $sql .= " WHERE grn_item.grn_batch_id=grn.id
+ AND grn_item.po_detail_item=po_item.po_detail_item";
+
+ if ($outstanding_only)
+ $sql .= " AND (".($invoice_no ? "$inv_cond OR ":'')."grn_item.qty_recd - grn_item.quantity_inv > 0)";
+
+ $sql .= " AND stock_item.stock_id=grn_item.item_code ";