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