2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 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/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_STEMPLATE';
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();
27 $trans_type = $_POST['type'];
28 elseif (isset($_GET['type']) && $_GET['type'] == 32)
32 if ($trans_type == 30)
34 if (isset($_GET['OutstandingOnly']) && ($_GET['OutstandingOnly'] == true))
36 $_POST['order_view_mode'] = 'OutstandingOnly';
37 $_SESSION['page_title'] = _("Search Outstanding Sales Orders");
39 elseif (isset($_GET['InvoiceTemplates']) && ($_GET['InvoiceTemplates'] == true))
41 $_POST['order_view_mode'] = 'InvoiceTemplates';
42 $_SESSION['page_title'] = _("Search Template for Invoicing");
44 elseif (isset($_GET['DeliveryTemplates']) && ($_GET['DeliveryTemplates'] == true))
46 $_POST['order_view_mode'] = 'DeliveryTemplates';
47 $_SESSION['page_title'] = _("Select Template for Delivery");
49 elseif (!isset($_POST['order_view_mode']))
51 $_POST['order_view_mode'] = false;
52 $_SESSION['page_title'] = _("Search All Sales Orders");
57 $_POST['order_view_mode'] = "Quotations";
58 $_SESSION['page_title'] = _("Search All Sales Quotations");
60 page($_SESSION['page_title'], false, false, "", $js);
62 if (isset($_GET['selected_customer']))
64 $selected_customer = $_GET['selected_customer'];
66 elseif (isset($_POST['selected_customer']))
68 $selected_customer = $_POST['selected_customer'];
71 $selected_customer = -1;
73 //---------------------------------------------------------------------------------------------
75 if (isset($_POST['SelectStockFromList']) && ($_POST['SelectStockFromList'] != "") &&
76 ($_POST['SelectStockFromList'] != reserved_words::get_all()))
78 $selected_stock_item = $_POST['SelectStockFromList'];
82 unset($selected_stock_item);
84 //---------------------------------------------------------------------------------------------
85 // Query format functions
87 function check_overdue($row)
90 if ($trans_type == 32)
91 return (date1_greater_date2(Today(), sql2date($row['delivery_date'])));
93 return ($row['type'] == 0
94 && date1_greater_date2(Today(), sql2date($row['ord_date']))
95 && ($row['TotDelivered'] < $row['TotQuantity']));
98 function view_link($dummy, $order_no)
101 return get_customer_trans_view_str($trans_type, $order_no);
104 function prt_link($row)
107 return print_document_link($row['order_no'], _("Print"), true, $trans_type, ICON_PRINT);
110 function edit_link($row)
113 $modify = ($trans_type == 30 ? "ModifyOrderNumber" : "ModifyQuotationNumber");
114 return pager_link( _("Edit"),
115 "/sales/sales_order_entry.php?$modify=" . $row['order_no'], ICON_EDIT);
118 function dispatch_link($row)
121 if ($trans_type == 30)
122 return pager_link( _("Dispatch"),
123 "/sales/customer_delivery.php?OrderNumber=" .$row['order_no'], ICON_DOC);
125 return pager_link( _("Sales Order"),
126 "/sales/sales_order_entry.php?OrderNumber=" .$row['order_no'], ICON_DOC);
129 function invoice_link($row)
132 if ($trans_type == 30)
133 return pager_link( _("Invoice"),
134 "/sales/sales_order_entry.php?NewInvoice=" .$row["order_no"], ICON_DOC);
139 function delivery_link($row)
141 return pager_link( _("Delivery"),
142 "/sales/sales_order_entry.php?NewDelivery=" .$row['order_no'], ICON_DOC);
145 function order_link($row)
147 return pager_link( _("Sales Order"),
148 "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
151 function tmpl_checkbox($row)
154 if ($trans_type == 32)
156 $name = "chgtpl" .$row['order_no'];
157 $value = $row['type'] ? 1:0;
159 // save also in hidden field for testing during 'Update'
161 return checkbox(null, $name, $value, true,
162 _('Set this order as a template for direct deliveries/invoices'))
163 . hidden('last['.$row['order_no'].']', $value, false);
165 //---------------------------------------------------------------------------------------------
166 // Update db record if respective checkbox value has changed.
168 function change_tpl_flag($id)
172 $sql = "UPDATE ".TB_PREF."sales_orders SET type = !type WHERE order_no=$id";
174 db_query($sql, "Can't change sales order type");
175 $Ajax->activate('orders_tbl');
178 $id = find_submit('_chgtpl');
180 change_tpl_flag($id);
182 if (isset($_POST['Update']) && isset($_POST['last'])) {
183 foreach($_POST['last'] as $id => $value)
184 if ($value != check_value('chgtpl'.$id))
185 change_tpl_flag($id);
188 //---------------------------------------------------------------------------------------------
191 if (get_post('_OrderNumber_changed')) // enable/disable selection controls
193 $disable = get_post('OrderNumber') !== '';
195 if ($_POST['order_view_mode']!='DeliveryTemplates'
196 && $_POST['order_view_mode']!='InvoiceTemplates') {
197 $Ajax->addDisable(true, 'OrdersAfterDate', $disable);
198 $Ajax->addDisable(true, 'OrdersToDate', $disable);
200 $Ajax->addDisable(true, 'StockLocation', $disable);
201 $Ajax->addDisable(true, '_SelectStockFromList_edit', $disable);
202 $Ajax->addDisable(true, 'SelectStockFromList', $disable);
205 $Ajax->addFocus(true, 'OrderNumber');
207 $Ajax->addFocus(true, 'OrdersAfterDate');
209 $Ajax->activate('orders_tbl');
214 start_table("class='tablestyle_noborder'");
216 ref_cells(_("#:"), 'OrderNumber', '',null, '', true);
217 if ($_POST['order_view_mode'] != 'DeliveryTemplates' && $_POST['order_view_mode'] != 'InvoiceTemplates')
219 date_cells(_("from:"), 'OrdersAfterDate', '', null, -30);
220 date_cells(_("to:"), 'OrdersToDate', '', null, 1);
222 locations_list_cells(_("Location:"), 'StockLocation', null, true);
224 stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true);
226 if ($trans_type == 32)
227 check_cells(_("Show All:"), 'show_all');
228 submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
230 hidden('order_view_mode', $_POST['order_view_mode']);
231 hidden('type', $trans_type);
237 //---------------------------------------------------------------------------------------------
238 // Orders inquiry table
245 .($_POST['order_view_mode']=='InvoiceTemplates'
246 || $_POST['order_view_mode']=='DeliveryTemplates' ?
247 "sorder.comments, " : "sorder.customer_ref, ")
249 sorder.delivery_date,
251 Sum(line.unit_price*line.quantity*(1-line.discount_percent)) AS OrderValue,
254 Sum(line.qty_sent) AS TotDelivered,
255 Sum(line.quantity) AS TotQuantity
256 FROM ".TB_PREF."sales_orders as sorder, "
257 .TB_PREF."sales_order_details as line, "
258 .TB_PREF."debtors_master as debtor, "
259 .TB_PREF."cust_branch as branch
260 WHERE sorder.order_no = line.order_no
261 AND sorder.trans_type = line.trans_type
262 AND sorder.trans_type = $trans_type
263 AND sorder.debtor_no = debtor.debtor_no
264 AND sorder.branch_code = branch.branch_code
265 AND debtor.debtor_no = branch.debtor_no";
267 if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
269 // search orders with number like ...
270 $sql .= " AND sorder.order_no LIKE '%". $_POST['OrderNumber'] ."'"
271 ." GROUP BY sorder.order_no";
273 else // ... or select inquiry constraints
275 if ($_POST['order_view_mode']!='DeliveryTemplates' && $_POST['order_view_mode']!='InvoiceTemplates')
277 $date_after = date2sql($_POST['OrdersAfterDate']);
278 $date_before = date2sql($_POST['OrdersToDate']);
280 $sql .= " AND sorder.ord_date >= '$date_after'"
281 ." AND sorder.ord_date <= '$date_before'";
283 if ($trans_type == 32 && !check_value('show_all'))
284 $sql .= " AND sorder.delivery_date >= '".date2sql(Today())."'";
285 if ($selected_customer != -1)
286 $sql .= " AND sorder.debtor_no='" . $selected_customer . "'";
288 if (isset($selected_stock_item))
289 $sql .= " AND line.stk_code='". $selected_stock_item ."'";
291 if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != reserved_words::get_all())
292 $sql .= " AND sorder.from_stk_loc = '". $_POST['StockLocation'] . "' ";
294 if ($_POST['order_view_mode']=='OutstandingOnly')
295 $sql .= " AND line.qty_sent < line.quantity";
296 elseif ($_POST['order_view_mode']=='InvoiceTemplates' || $_POST['order_view_mode']=='DeliveryTemplates')
297 $sql .= " AND sorder.type=1";
299 $sql .= " GROUP BY sorder.order_no,
307 if ($trans_type == 30)
309 _("Order #") => array('fun'=>'view_link'),
314 _("Order Date") => 'date',
315 _("Required By") =>array('type'=>'date', 'ord'=>''),
317 _("Order Total") => array('type'=>'amount', 'ord'=>''),
319 _("Currency") => array('align'=>'center')
323 _("Quote #") => array('fun'=>'view_link'),
328 _("Quote Date") => 'date',
329 _("Valid until") =>array('type'=>'date', 'ord'=>''),
331 _("Quote Total") => array('type'=>'amount', 'ord'=>''),
333 _("Currency") => array('align'=>'center')
335 if ($_POST['order_view_mode'] == 'OutstandingOnly') {
336 //array_replace($cols, 3, 1, _("Cust Order Ref"));
337 array_append($cols, array(array('insert'=>true, 'fun'=>'dispatch_link')));
339 } elseif ($_POST['order_view_mode'] == 'InvoiceTemplates') {
340 array_replace($cols, 3, 1, _("Description"));
341 array_append($cols, array( array('insert'=>true, 'fun'=>'invoice_link')));
343 } else if ($_POST['order_view_mode'] == 'DeliveryTemplates') {
344 array_replace($cols, 3, 1, _("Description"));
345 array_append($cols, array(
346 array('insert'=>true, 'fun'=>'delivery_link'))
349 } elseif ($trans_type == 32) {
350 array_append($cols,array(
351 array('insert'=>true, 'fun'=>'edit_link'),
352 array('insert'=>true, 'fun'=>'order_link'),
353 array('insert'=>true, 'fun'=>'prt_link')));
354 } elseif ($trans_type == 30) {
355 array_append($cols,array(
356 _("Tmpl") => array('insert'=>true, 'fun'=>'tmpl_checkbox'),
357 array('insert'=>true, 'fun'=>'edit_link'),
358 array('insert'=>true, 'fun'=>'prt_link')));
362 $table =& new_db_pager('orders_tbl', $sql, $cols);
363 $table->set_marker('check_overdue', _("Marked items are overdue."));
365 if (get_post('SearchOrders')) {
366 $table->set_sql($sql);
367 $table->set_columns($cols);
369 $table->width = "80%";
372 display_db_pager($table);
373 submit_center('Update', _("Update"), true, '', null);