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