2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU Affero General Public License,
5 AGPL, as published by the Free Software Foundation, either version
6 3 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/agpl-3.0.html>.
11 ***********************************************************************/
13 $path_to_root="../..";
15 include($path_to_root . "/includes/db_pager.inc");
16 include($path_to_root . "/includes/session.inc");
17 include($path_to_root . "/sales/includes/sales_ui.inc");
18 include_once($path_to_root . "/reporting/includes/reporting.inc");
21 if ($use_popup_windows)
22 $js .= get_js_open_window(900, 600);
24 $js .= get_js_date_picker();
26 if (isset($_GET['OutstandingOnly']) && ($_GET['OutstandingOnly'] == true))
28 $_POST['order_view_mode'] = 'OutstandingOnly';
29 $_SESSION['page_title'] = _("Search Outstanding Sales Orders");
31 elseif (isset($_GET['InvoiceTemplates']) && ($_GET['InvoiceTemplates'] == true))
33 $_POST['order_view_mode'] = 'InvoiceTemplates';
34 $_SESSION['page_title'] = _("Search Template for Invoicing");
36 elseif (isset($_GET['DeliveryTemplates']) && ($_GET['DeliveryTemplates'] == true))
38 $_POST['order_view_mode'] = 'DeliveryTemplates';
39 $_SESSION['page_title'] = _("Select Template for Delivery");
41 elseif (!isset($_POST['order_view_mode']))
43 $_POST['order_view_mode'] = false;
44 $_SESSION['page_title'] = _("Search All Sales Orders");
47 page($_SESSION['page_title'], false, false, "", $js);
49 if (isset($_GET['selected_customer']))
51 $selected_customer = $_GET['selected_customer'];
53 elseif (isset($_POST['selected_customer']))
55 $selected_customer = $_POST['selected_customer'];
58 $selected_customer = -1;
60 //---------------------------------------------------------------------------------------------
62 if (isset($_POST['SelectStockFromList']) && ($_POST['SelectStockFromList'] != "") &&
63 ($_POST['SelectStockFromList'] != reserved_words::get_all()))
65 $selected_stock_item = $_POST['SelectStockFromList'];
69 unset($selected_stock_item);
71 //---------------------------------------------------------------------------------------------
72 // Query format functions
74 function check_overdue($row)
76 return ($row['type'] == 0
77 && date1_greater_date2(Today(), sql2date($row['ord_date']))
78 && ($row['TotDelivered'] < $row['TotQuantity']));
81 function view_link($dummy, $order_no)
83 return get_customer_trans_view_str(systypes::sales_order(), $order_no);
86 function prt_link($row)
88 return print_document_link($row['order_no'], _("Print"), true, 30, ICON_PRINT);
91 function edit_link($row)
93 return pager_link( _("Edit"),
94 "/sales/sales_order_entry.php?" . SID . "ModifyOrderNumber=" . $row['order_no'], ICON_EDIT);
97 function dispatch_link($row)
99 return pager_link( _("Dispatch"),
100 "/sales/customer_delivery.php?" . SID . "OrderNumber=" .$row['order_no'], ICON_DOC);
103 function invoice_link($row)
105 return pager_link( _("Invoice"),
106 "/sales/sales_order_entry.php?" . SID . "NewInvoice=" .$row["order_no"], ICON_DOC);
109 function delivery_link($row)
111 return pager_link( _("Delivery"),
112 "/sales/sales_order_entry.php?" . SID . "NewDelivery=" .$row['order_no'], ICON_DOC);
115 function tmpl_checkbox($row)
117 $name = "chgtpl" .$row['order_no'];
118 $value = $row['type'] ? ' checked' : '';
120 return "<input $value type='checkbox' name='$name' value='1'"
121 ." onclick='JsHttpRequest.request(\"_{$name}_update\", this.form);'"
122 ." title='"._('Set this order as a template for direct deliveries/invoices')
124 // add also old checkbox name+value for check after 'Update'
125 ."<input name='last[".$row['order_no']."]' type='hidden' value='"
126 .($row['type'] ? 1 : 0)."'>\n";
128 //---------------------------------------------------------------------------------------------
129 // Update db record if respective checkbox value has changed.
131 function change_tpl_flag($id)
135 $sql = "UPDATE ".TB_PREF."sales_orders SET type = !type WHERE order_no=$id";
137 db_query($sql, "Can't change sales order type");
138 $Ajax->activate('orders_tbl');
141 $id = find_submit('_chgtpl');
143 change_tpl_flag($id);
145 if (isset($_POST['Update'])) {
146 foreach($_POST['last'] as $id => $value)
147 if ($value != check_value('chgtpl'.$id))
148 change_tpl_flag($id);
151 //---------------------------------------------------------------------------------------------
154 if (get_post('_OrderNumber_changed')) // enable/disable selection controls
156 $disable = get_post('OrderNumber') !== '';
158 if ($_POST['order_view_mode']!='DeliveryTemplates'
159 && $_POST['order_view_mode']!='InvoiceTemplates') {
160 $Ajax->addDisable(true, 'OrdersAfterDate', $disable);
161 $Ajax->addDisable(true, 'OrdersToDate', $disable);
163 $Ajax->addDisable(true, 'StockLocation', $disable);
164 $Ajax->addDisable(true, '_SelectStockFromList_edit', $disable);
165 $Ajax->addDisable(true, 'SelectStockFromList', $disable);
168 $Ajax->addFocus(true, 'OrderNumber');
170 $Ajax->addFocus(true, 'OrdersAfterDate');
172 $Ajax->activate('orders_tbl');
175 start_form(false, false, $_SERVER['PHP_SELF'] .SID);
177 start_table("class='tablestyle_noborder'");
179 ref_cells(_("#:"), 'OrderNumber', '',null, '', true);
180 if ($_POST['order_view_mode'] != 'DeliveryTemplates' && $_POST['order_view_mode'] != 'InvoiceTemplates')
182 date_cells(_("from:"), 'OrdersAfterDate', '', null, -30);
183 date_cells(_("to:"), 'OrdersToDate', '', null, 1);
185 locations_list_cells(_("Location:"), 'StockLocation', null, true);
187 stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true);
189 submit_cells('SearchOrders', _("Search"),'',_('Select documents'), true);
191 hidden('order_view_mode', $_POST['order_view_mode']);
197 //---------------------------------------------------------------------------------------------
198 // Orders inquiry table
204 .($_POST['order_view_mode']=='InvoiceTemplates'
205 || $_POST['order_view_mode']=='DeliveryTemplates' ?
206 "sorder.comments, " : "sorder.customer_ref, ")
208 sorder.delivery_date,
210 Sum(line.unit_price*line.quantity*(1-line.discount_percent)) AS OrderValue,
213 Sum(line.qty_sent) AS TotDelivered,
214 Sum(line.quantity) AS TotQuantity
215 FROM ".TB_PREF."sales_orders as sorder, "
216 .TB_PREF."sales_order_details as line, "
217 .TB_PREF."debtors_master as debtor, "
218 .TB_PREF."cust_branch as branch
219 WHERE sorder.order_no = line.order_no
220 AND sorder.debtor_no = debtor.debtor_no
221 AND sorder.branch_code = branch.branch_code
222 AND debtor.debtor_no = branch.debtor_no";
224 if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
226 // search orders with number like ...
227 $sql .= " AND sorder.order_no LIKE '%". $_POST['OrderNumber'] ."'"
228 ." GROUP BY sorder.order_no";
230 else // ... or select inquiry constraints
232 if ($_POST['order_view_mode']!='DeliveryTemplates' && $_POST['order_view_mode']!='InvoiceTemplates')
234 $date_after = date2sql($_POST['OrdersAfterDate']);
235 $date_before = date2sql($_POST['OrdersToDate']);
237 $sql .= " AND sorder.ord_date >= '$date_after'"
238 ." AND sorder.ord_date <= '$date_before'";
240 if ($selected_customer != -1)
241 $sql .= " AND sorder.debtor_no='" . $selected_customer . "'";
243 if (isset($selected_stock_item))
244 $sql .= " AND line.stk_code='". $selected_stock_item ."'";
246 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != reserved_words::get_all())
247 $sql .= " AND sorder.from_stk_loc = '". $_POST['StockLocation'] . "' ";
249 if ($_POST['order_view_mode']=='OutstandingOnly')
250 $sql .= " AND line.qty_sent < line.quantity";
251 elseif ($_POST['order_view_mode']=='InvoiceTemplates' || $_POST['order_view_mode']=='DeliveryTemplates')
252 $sql .= " AND sorder.type=1";
254 $sql .= " GROUP BY sorder.order_no,
263 _("Order #") => array('fun'=>'view_link'),
267 _("Order Date") => 'date',
268 _("Required By") =>array('type'=>'date', 'ord'=>''),
270 _("Order Total") => array('type'=>'amount', 'ord'=>''),
272 _("Currency") => array('align'=>'center')
275 if ($_POST['order_view_mode'] == 'OutstandingOnly') {
276 array_replace($cols, 3, 1, _("Cust Order Ref"));
277 array_append($cols, array(array('insert'=>true, 'fun'=>'dispatch_link')));
279 } elseif ($_POST['order_view_mode'] == 'InvoiceTemplates') {
280 array_replace($cols, 3, 1, _("Description"));
281 array_append($cols, array( array('insert'=>true, 'fun'=>'invoice_link')));
283 } else if ($_POST['order_view_mode'] == 'DeliveryTemplates') {
284 array_replace($cols, 3, 1, _("Description"));
285 array_append($cols, array(
286 array('insert'=>true, 'fun'=>'delivery_link'))
290 array_append($cols,array(
291 _("Tmpl") => array('insert'=>true, 'fun'=>'tmpl_checkbox'),
292 array('insert'=>true, 'fun'=>'edit_link'),
293 array('insert'=>true, 'fun'=>'prt_link')));
297 $table =& new_db_pager('orders_tbl', $sql, $cols);
298 $table->set_marker('check_overdue', _("Marked items are overdue."));
300 if (get_post('SearchOrders')) {
301 $table->set_sql($sql);
302 $table->set_columns($cols);
306 display_db_pager($table);
307 submit_center('Update', _("Update"), true, '', null);