5 include($path_to_root . "/includes/session.inc");
7 include($path_to_root . "/sales/includes/sales_ui.inc");
10 if ($use_popup_windows)
11 $js .= get_js_open_window(900, 600);
13 $js .= get_js_date_picker();
15 if (isset($_GET['OutstandingOnly']) && ($_GET['OutstandingOnly'] == true))
17 $_POST['OutstandingOnly'] = true;
18 page(_("Search Not Invoiced Deliveries"), false, false, "", $js);
22 $_POST['OutstandingOnly'] = false;
23 page(_("Search All Deliveries"), false, false, "", $js);
26 if (isset($_GET['selected_customer']))
28 $selected_customer = $_GET['selected_customer'];
30 elseif (isset($_POST['selected_customer']))
32 $selected_customer = $_POST['selected_customer'];
35 $selected_customer = -1;
37 if (isset($_POST['BatchInvoice'])) {
39 // checking batch integrity
41 foreach($_SESSION['Batch'] as $delivery) {
42 $checkbox = 'Sel_'.$delivery['trans'];
43 if (check_value($checkbox)) {
45 $del_customer = $delivery['cust'];
46 $del_branch = $delivery['branch'];
48 if ($del_customer!=$delivery['cust'] || $del_branch != $delivery['branch']) {
52 $selected[] = $delivery['trans'];
58 display_error(_('For batch invoicing you should
59 select at least one delivery. All items must be dispatched to
60 the same customer branch.'));
62 $_SESSION['DeliveryBatch'] = $selected;
63 meta_forward($path_to_root . '/sales/customer_invoice.php','BatchInvoice=Yes');
66 //-----------------------------------------------------------------------------------
68 start_form(false, false, $_SERVER['PHP_SELF'] ."?OutstandingOnly=" . $_POST['OutstandingOnly'] .SID);
70 start_table("class='tablestyle_noborder'");
72 ref_cells(_("#:"), 'DeliveryNumber');
73 date_cells(_("from:"), 'DeliveryAfterDate', null, -30);
74 date_cells(_("to:"), 'DeliveryToDate', null, 1);
76 locations_list_cells(_("Location:"), 'StockLocation', null, true);
78 stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true);
80 submit_cells('SearchOrders', _("Search"));
82 hidden('OutstandingOnly', $_POST['OutstandingOnly']);
88 //---------------------------------------------------------------------------------------------
90 if (isset($_POST['SelectStockFromList']) && ($_POST['SelectStockFromList'] != "") &&
91 ($_POST['SelectStockFromList'] != reserved_words::get_all()))
93 $selected_stock_item = $_POST['SelectStockFromList'];
97 unset($selected_stock_item);
100 //---------------------------------------------------------------------------------------------
101 $sql = "SELECT ".TB_PREF."debtor_trans.trans_no, "
102 .TB_PREF."debtors_master.curr_code, "
103 .TB_PREF."debtors_master.name, "
104 .TB_PREF."cust_branch.br_name, "
105 .TB_PREF."debtor_trans.reference, "
106 .TB_PREF."debtor_trans.tran_date, "
107 .TB_PREF."debtor_trans.due_date, "
108 .TB_PREF."sales_orders.customer_ref, "
109 .TB_PREF."sales_orders.deliver_to, ";
111 $sql .= " Sum(".TB_PREF."debtor_trans_details.quantity-"
112 .TB_PREF."debtor_trans_details.qty_done) AS Outstanding, ";
114 $sql .= " Sum(".TB_PREF."debtor_trans_details.qty_done) AS Done, ";
116 //$sql .= " Sum(".TB_PREF."debtor_trans_details.unit_price*"
117 // .TB_PREF."debtor_trans_details.quantity*(1-"
118 // .TB_PREF."debtor_trans_details.discount_percent)) AS DeliveryValue";
119 $sql .= "(ov_amount+ov_gst+ov_freight+ov_freight_tax) AS DeliveryValue";
121 .TB_PREF."sales_orders, "
122 .TB_PREF."debtor_trans, "
123 .TB_PREF."debtor_trans_details, "
124 .TB_PREF."debtors_master, "
125 .TB_PREF."cust_branch
127 .TB_PREF."sales_orders.order_no = ".TB_PREF."debtor_trans.order_ AND "
128 .TB_PREF."debtor_trans.debtor_no = ".TB_PREF."debtors_master.debtor_no
129 AND ".TB_PREF."debtor_trans.type = 13
130 AND ".TB_PREF."debtor_trans_details.debtor_trans_no = ".TB_PREF."debtor_trans.trans_no
131 AND ".TB_PREF."debtor_trans_details.debtor_trans_type = ".TB_PREF."debtor_trans.type
132 AND ".TB_PREF."debtor_trans.branch_code = ".TB_PREF."cust_branch.branch_code
133 AND ".TB_PREF."debtor_trans.debtor_no = ".TB_PREF."cust_branch.debtor_no ";
135 //figure out the sql required from the inputs available
136 if (isset($_POST['DeliveryNumber']) && $_POST['DeliveryNumber'] != "")
138 $sql .= " AND ".TB_PREF."debtor_trans.trans_no LIKE '%". $_POST['DeliveryNumber'] ."' GROUP BY ".TB_PREF."debtor_trans.trans_no";
143 $date_after = date2sql($_POST['DeliveryAfterDate']);
144 $date_before = date2sql($_POST['DeliveryToDate']);
146 $sql .= " AND ".TB_PREF."debtor_trans.tran_date >= '$date_after'";
147 $sql .= " AND ".TB_PREF."debtor_trans.tran_date <= '$date_before'";
149 if ($selected_customer != -1)
150 $sql .= " AND ".TB_PREF."debtor_trans.debtor_no='" . $selected_customer . "' ";
152 if (isset($selected_stock_item))
153 $sql .= " AND ".TB_PREF."debtor_trans_details.stock_id='". $selected_stock_item ."' ";
155 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != reserved_words::get_all())
156 $sql .= " AND ".TB_PREF."sales_orders.from_stk_loc = '". $_POST['StockLocation'] . "' ";
158 if ($_POST['OutstandingOnly'] == true) {
159 $sql .= " AND ".TB_PREF."debtor_trans_details.qty_done < ".TB_PREF."debtor_trans_details.quantity ";
162 $sql .= " GROUP BY ".TB_PREF."debtor_trans.trans_no ";
163 // .TB_PREF."debtor_trans.debtor_no, "
164 // .TB_PREF."debtor_trans.branch_code, "
165 // ".TB_PREF."sales_orders.customer_ref, "
166 // .TB_PREF."debtor_trans.tran_date";
168 } //end no delivery number selected
170 $result = db_query($sql,"No deliveries were returned");
172 //-----------------------------------------------------------------------------------
173 if (isset($_SESSION['Batch'])) {
174 foreach($_SESSION['Batch'] as $trans=>$del) unset($_SESSION['Batch'][$trans]);
175 unset($_SESSION['Batch']);
180 /*show a table of the deliveries returned by the sql */
182 start_table("$table_style colspan=7 width=95%");
183 $th = array(_("Delivery #"), _("Customer"), _("Branch"), _("Reference"), _("Delivery Date"),
184 _("Due By"), _("Delivery Total"), _("Currency"),
185 submit('BatchInvoice','Batch Inv', false),
190 $k = 0; //row colour counter
191 $overdue_items = false;
192 while ($myrow = db_fetch($result))
194 $_SESSION['Batch'][] = array('trans'=>$myrow["trans_no"],
195 'cust'=>$myrow["name"],'branch'=>$myrow["br_name"] );
197 $view_page = get_customer_trans_view_str(13, $myrow["trans_no"]);
198 $formated_del_date = sql2date($myrow["tran_date"]);
199 $formated_due_date = sql2date($myrow["due_date"]);
200 $not_closed = $myrow["Outstanding"]!=0;
202 // if overdue orders, then highlight as so
204 if (date1_greater_date2(Today(), $formated_due_date) && $not_closed )
206 start_row("class='overduebg'");
207 $overdue_items = true;
211 alt_table_row_color($k);
214 label_cell($view_page);
215 label_cell($myrow["name"]);
216 label_cell($myrow["br_name"]);
217 label_cell($myrow["reference"]);
218 label_cell($formated_del_date);
219 label_cell($formated_due_date);
220 amount_cell($myrow["DeliveryValue"]);
221 label_cell($myrow["curr_code"]);
223 check_cells(null,'Sel_'. $myrow['trans_no'],0,false);
226 if ($_POST['OutstandingOnly'] == true || $not_closed)
228 $modify_page = $path_to_root . "/sales/customer_delivery.php?" . SID . "ModifyDelivery=" . $myrow["trans_no"];
229 $invoice_page = $path_to_root . "/sales/customer_invoice.php?" . SID . "DeliveryNumber=" .$myrow["trans_no"];
230 label_cell("<a href='$modify_page'>" . _("Edit") . "</a>");
232 label_cell($not_closed ? "<a href='$invoice_page'>" . _("Invoice") . "</a>" : '');
248 //end of page full new headings if
255 display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");