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