39b26f2a8f8a271ee6c16eef607b7d614cc8fb63
[fa-stable.git] / sales / inquiry / sales_orders_view.php
1 <?php
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 $path_to_root = "../..";
13
14 include_once($path_to_root . "/includes/db_pager.inc");
15 include_once($path_to_root . "/includes/session.inc");
16 include_once($path_to_root . "/sales/includes/sales_ui.inc");
17 include_once($path_to_root . "/reporting/includes/reporting.inc");
18
19 $page_security = 'SA_SALESTRANSVIEW';
20
21 set_page_security( @$_POST['order_view_mode'],
22         array(  'OutstandingOnly' => 'SA_SALESDELIVERY',
23                         'InvoiceTemplates' => 'SA_SALESINVOICE'),
24         array(  'OutstandingOnly' => 'SA_SALESDELIVERY',
25                         'InvoiceTemplates' => 'SA_SALESINVOICE')
26 );
27 if (!@$_GET['popup'])\r
28 {\r
29         $js = "";
30         if ($use_popup_windows)
31                 $js .= get_js_open_window(900, 600);
32         if ($use_date_picker)
33                 $js .= get_js_date_picker();
34         page($_SESSION['page_title'], false, false, "", $js);\r
35 }
36 if (get_post('type'))
37         $trans_type = $_POST['type'];
38 elseif (isset($_GET['type']) && $_GET['type'] == ST_SALESQUOTE)
39         $trans_type = ST_SALESQUOTE;
40 else
41         $trans_type = ST_SALESORDER;
42
43 if ($trans_type == ST_SALESORDER)
44 {
45         if (isset($_GET['OutstandingOnly']) && ($_GET['OutstandingOnly'] == true))
46         {
47                 $_POST['order_view_mode'] = 'OutstandingOnly';
48                 $_SESSION['page_title'] = _($help_context = "Search Outstanding Sales Orders");
49         }
50         elseif (isset($_GET['InvoiceTemplates']) && ($_GET['InvoiceTemplates'] == true))
51         {
52                 $_POST['order_view_mode'] = 'InvoiceTemplates';
53                 $_SESSION['page_title'] = _($help_context = "Search Template for Invoicing");
54         }
55         elseif (isset($_GET['DeliveryTemplates']) && ($_GET['DeliveryTemplates'] == true))
56         {
57                 $_POST['order_view_mode'] = 'DeliveryTemplates';
58                 $_SESSION['page_title'] = _($help_context = "Select Template for Delivery");
59         }
60         elseif (!isset($_POST['order_view_mode']))
61         {
62                 $_POST['order_view_mode'] = false;
63                 $_SESSION['page_title'] = _($help_context = "Search All Sales Orders");
64         }
65 }
66 else
67 {
68         $_POST['order_view_mode'] = "Quotations";
69         $_SESSION['page_title'] = _($help_context = "Search All Sales Quotations");
70 }
71
72 if (isset($_GET['selected_customer']))
73 {
74         $selected_customer = $_GET['selected_customer'];
75 }
76 elseif (isset($_POST['selected_customer']))
77 {
78         $selected_customer = $_POST['selected_customer'];
79 }
80 else
81         $selected_customer = -1;
82
83 //---------------------------------------------------------------------------------------------
84
85 if (isset($_POST['SelectStockFromList']) && ($_POST['SelectStockFromList'] != "") &&
86         ($_POST['SelectStockFromList'] != ALL_TEXT))
87 {
88         $selected_stock_item = $_POST['SelectStockFromList'];
89 }
90 else
91 {
92         unset($selected_stock_item);
93 }
94 //---------------------------------------------------------------------------------------------
95 //      Query format functions
96 //
97 function check_overdue($row)
98 {
99         global $trans_type;
100         if ($trans_type == ST_SALESQUOTE)
101                 return (date1_greater_date2(Today(), sql2date($row['delivery_date'])));
102         else
103                 return ($row['type'] == 0
104                         && date1_greater_date2(Today(), sql2date($row['ord_date']))
105                         && ($row['TotDelivered'] < $row['TotQuantity']));
106 }
107
108 function view_link($dummy, $order_no)
109 {
110         global $trans_type;
111         return  get_customer_trans_view_str($trans_type, $order_no);
112 }
113
114 function prt_link($row)
115 {
116         global $trans_type;
117         return print_document_link($row['order_no'], _("Print"), true, $trans_type, ICON_PRINT);
118 }
119
120 function edit_link($row) 
121 {
122         if (@$_GET['popup'])\r
123                 return '';\r
124         global $trans_type;
125         $modify = ($trans_type == ST_SALESORDER ? "ModifyOrderNumber" : "ModifyQuotationNumber");
126   return pager_link( _("Edit"),
127     "/sales/sales_order_entry.php?$modify=" . $row['order_no'], ICON_EDIT);
128 }
129
130 function dispatch_link($row)
131 {
132         global $trans_type;
133         if ($trans_type == ST_SALESORDER)
134                 return pager_link( _("Dispatch"),
135                         "/sales/customer_delivery.php?OrderNumber=" .$row['order_no'], ICON_DOC);
136         else            
137                 return pager_link( _("Sales Order"),
138                         "/sales/sales_order_entry.php?OrderNumber=" .$row['order_no'], ICON_DOC);
139 }
140
141 function invoice_link($row)
142 {
143         global $trans_type;
144         if ($trans_type == ST_SALESORDER)
145                 return pager_link( _("Invoice"),
146                         "/sales/sales_order_entry.php?NewInvoice=" .$row["order_no"], ICON_DOC);
147         else
148                 return '';
149 }
150
151 function delivery_link($row)
152 {
153   return pager_link( _("Delivery"),
154         "/sales/sales_order_entry.php?NewDelivery=" .$row['order_no'], ICON_DOC);
155 }
156
157 function order_link($row)
158 {
159   return pager_link( _("Sales Order"),
160         "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
161 }
162
163 function tmpl_checkbox($row)
164 {
165         global $trans_type;
166         if ($trans_type == ST_SALESQUOTE)
167                 return '';
168         if (@$_GET['popup'])\r
169                 return '';\r
170         $name = "chgtpl" .$row['order_no'];
171         $value = $row['type'] ? 1:0;
172
173 // save also in hidden field for testing during 'Update'
174
175  return checkbox(null, $name, $value, true,
176         _('Set this order as a template for direct deliveries/invoices'))
177         . hidden('last['.$row['order_no'].']', $value, false);
178 }
179 //---------------------------------------------------------------------------------------------
180 // Update db record if respective checkbox value has changed.
181 //
182 function change_tpl_flag($id)
183 {
184         global  $Ajax;
185         
186         $sql = "UPDATE ".TB_PREF."sales_orders SET type = !type WHERE order_no=$id";
187
188         db_query($sql, "Can't change sales order type");
189         $Ajax->activate('orders_tbl');
190 }
191
192 $id = find_submit('_chgtpl');
193 if ($id != -1)
194         change_tpl_flag($id);
195
196 if (isset($_POST['Update']) && isset($_POST['last'])) {
197         foreach($_POST['last'] as $id => $value)
198                 if ($value != check_value('chgtpl'.$id))
199                         change_tpl_flag($id);
200 }
201
202 $show_dates = !in_array($_POST['order_view_mode'], array('OutstandingOnly', 'InvoiceTemplates', 'DeliveryTemplates'));
203 //---------------------------------------------------------------------------------------------
204 //      Order range form
205 //
206 if (get_post('_OrderNumber_changed') || get_post('_OrderReference_changed')) // enable/disable selection controls
207 {
208         $disable = get_post('OrderNumber') !== '' || get_post('OrderReference') !== '';
209
210         if ($show_dates) {
211                         $Ajax->addDisable(true, 'OrdersAfterDate', $disable);
212                         $Ajax->addDisable(true, 'OrdersToDate', $disable);
213         }
214
215         $Ajax->activate('orders_tbl');
216 }
217
218 if (!@$_GET['popup'])\r
219         start_form();
220
221 start_table(TABLESTYLE_NOBORDER);
222 start_row();
223 ref_cells(_("#:"), 'OrderNumber', '',null, '', true);
224 ref_cells(_("Ref"), 'OrderReference', '',null, '', true);
225 if ($show_dates)
226 {
227         date_cells(_("from:"), 'OrdersAfterDate', '', null, -30);
228         date_cells(_("to:"), 'OrdersToDate', '', null, 1);
229 }
230 locations_list_cells(_("Location:"), 'StockLocation', null, true);
231
232 if($show_dates) {
233         end_row();
234         end_table();
235
236         start_table(TABLESTYLE_NOBORDER);
237         start_row();
238 }
239 stock_items_list_cells(_("Item:"), 'SelectStockFromList', null, true);
240 if (!@$_GET['popup'])\r
241         customer_list_cells(_("Select a customer: "), 'customer_id', null, true);\r
242 if ($trans_type == ST_SALESQUOTE)
243         check_cells(_("Show All:"), 'show_all');
244
245 submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
246 hidden('order_view_mode', $_POST['order_view_mode']);
247 hidden('type', $trans_type);
248
249 end_row();
250
251 end_table(1);
252 //---------------------------------------------------------------------------------------------
253 //      Orders inquiry table
254 //
255 $sql = get_sql_for_sales_orders_view($selected_customer, $trans_type, $_POST['OrderNumber'], $_POST['order_view_mode'],
256         @$selected_stock_item, @$_POST['OrdersAfterDate'], @$_POST['OrdersToDate'], @$_POST['OrderReference'], $_POST['StockLocation'], $_POST['customer_id']);\r
257
258 if ($trans_type == ST_SALESORDER)
259         $cols = array(
260                 _("Order #") => array('fun'=>'view_link'),
261                 _("Ref"),
262                 _("Customer"),
263                 _("Branch"), 
264                 _("Cust Order Ref"),
265                 _("Order Date") => 'date',
266                 _("Required By") =>array('type'=>'date', 'ord'=>''),
267                 _("Delivery To"), 
268                 _("Order Total") => array('type'=>'amount', 'ord'=>''),
269                 'Type' => 'skip',
270                 _("Currency") => array('align'=>'center')
271         );
272 else
273         $cols = array(
274                 _("Quote #") => array('fun'=>'view_link'),
275                 _("Ref"),
276                 _("Customer"),
277                 _("Branch"), 
278                 _("Cust Order Ref"),
279                 _("Quote Date") => 'date',
280                 _("Valid until") =>array('type'=>'date', 'ord'=>''),
281                 _("Delivery To"), 
282                 _("Quote Total") => array('type'=>'amount', 'ord'=>''),
283                 'Type' => 'skip',
284                 _("Currency") => array('align'=>'center')
285         );
286 if ($_POST['order_view_mode'] == 'OutstandingOnly') {\r
287         //array_substitute($cols, 3, 1, _("Cust Order Ref"));\r
288         array_append($cols, array(\r
289                 array('insert'=>true, 'fun'=>'dispatch_link'),\r
290                 array('insert'=>true, 'fun'=>'edit_link')));\r
291
292 } elseif ($_POST['order_view_mode'] == 'InvoiceTemplates') {
293         array_substitute($cols, 3, 1, _("Description"));
294         array_append($cols, array( array('insert'=>true, 'fun'=>'invoice_link')));
295
296 } else if ($_POST['order_view_mode'] == 'DeliveryTemplates') {
297         array_substitute($cols, 3, 1, _("Description"));
298         array_append($cols, array(
299                         array('insert'=>true, 'fun'=>'delivery_link'))
300         );
301
302 } elseif ($trans_type == ST_SALESQUOTE) {
303          array_append($cols,array(
304                                         array('insert'=>true, 'fun'=>'edit_link'),
305                                         array('insert'=>true, 'fun'=>'order_link'),
306                                         array('insert'=>true, 'fun'=>'prt_link')));
307 } elseif ($trans_type == ST_SALESORDER) {
308          array_append($cols,array(
309                         _("Tmpl") => array('insert'=>true, 'fun'=>'tmpl_checkbox'),
310                                         array('insert'=>true, 'fun'=>'edit_link'),
311                                         array('insert'=>true, 'fun'=>'prt_link')));
312 };
313
314
315 $table =& new_db_pager('orders_tbl', $sql, $cols);
316 $table->set_marker('check_overdue', _("Marked items are overdue."));
317
318 $table->width = "80%";
319
320 display_db_pager($table);
321 submit_center('Update', _("Update"), true, '', null);
322
323 if (!@$_GET['popup'])\r
324 {
325         end_form();
326         end_page();
327 }\r
328 ?>