Added POS and cash sale support.
[fa-stable.git] / sales / includes / ui / sales_order_ui.inc
1 <?php
2
3 include_once($path_to_root . "/sales/includes/cart_class.inc");
4 include_once($path_to_root . "/includes/manufacturing.inc");
5
6 //--------------------------------------------------------------------------------
7 function add_to_order(&$order, $new_item, $new_item_qty, $price, $discount)
8 {
9
10         foreach ($order->line_items AS $order_item)
11         {
12                 if (strcasecmp($order_item->stock_id, $new_item) == 0)
13                 {
14                         display_notification(_("For Part :") . $new_item . " " . _("This item is already on this order. You have been warned."));
15                         break;
16                 }
17         }
18
19         $order->add_to_cart (count($order->line_items),$new_item, $new_item_qty, $price, $discount);
20 }
21
22 //---------------------------------------------------------------------------------
23
24 function get_customer_details_to_order(&$order, $customer_id, $branch_id)
25 {
26         $ret_error = "";
27
28         $myrow = get_customer_to_order($customer_id);
29
30         $name = $myrow['name'];
31
32         if ($myrow['dissallow_invoices'] == 1)
33                 $ret_error = _("The selected customer account is currently on hold. Please contact the credit control personnel to discuss.");
34
35         $deliver = $myrow['address']; // in case no delivery address.
36
37         $order->set_customer($customer_id, $name, $myrow['curr_code'], 
38                 $myrow['discount'], $myrow['pymt_discount']);
39
40         // the sales type determines the price list to be used by default
41         $order->set_sales_type($myrow['salestype'], $myrow['sales_type'], $myrow['tax_included'],
42             $myrow['factor']);
43
44         $result = get_branch_to_order($customer_id, $branch_id);
45
46         if (db_num_rows($result) == 0)
47         {
48                 return _("The selected customer and branch are not valid, or the customer does not have any branches.");
49         }
50
51         $myrow = db_fetch($result);
52
53         $order->set_branch($branch_id, $myrow["tax_group_id"],
54         $myrow["tax_group_name"], $myrow["phone"], $myrow["email"]);
55
56         $address = $myrow["br_post_address"];
57
58         if (strlen($address) <= 1)      // if branch has no address
59                 $address = $deliver;    // set sales order address
60         $order->set_delivery($myrow["default_ship_via"], $myrow["br_name"],
61                 $address);
62         if ($order->trans_type == 10) {
63                 $order->due_date = get_invoice_duedate($customer_id, $order->document_date);
64                 if ($order->pos != -1)
65                         $order->cash = date_diff($order->due_date, Today(), 'd')<2;
66         }
67         if($order->cash ) {
68                 if($order->pos != -1) {
69                 $paym = get_sales_point($order->pos);
70                 $order->set_location($paym["pos_location"], $paym["location_name"]);
71                 }
72         } else
73                 $order->set_location($myrow["default_location"], $myrow["location_name"]);
74                 
75         return $ret_error;
76 }
77
78 //---------------------------------------------------------------------------------
79
80 function display_order_summary($title, &$order, $editable_items=false)
81 {
82         global $table_style, $path_to_root;
83
84         display_heading($title);
85
86     div_start('items_table');
87         start_table("$table_style colspan=7 width=90%");
88         $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
89                 _("Delivered"),
90                 _("Unit"), _("Price"), _("Discount %"), _("Total"), "");
91
92         if ($order->trans_no == 0) {
93         unset( $th[3] );
94         }
95
96         if (count($order->line_items))
97              $th[]= '';
98
99         table_header($th);
100
101         $total = 0;
102         $k = 0;  //row colour counter
103
104         $id = find_submit('Edit');
105         foreach ($order->line_items as $line_no=>$stock_item)
106         {
107
108                 $line_total = round($stock_item->qty_dispatched * $stock_item->price * (1 - $stock_item->discount_percent),
109                    user_price_dec());
110
111
112                 if (!$editable_items || $id != $line_no)
113                 {
114                         alt_table_row_color($k);
115
116                         view_stock_status_cell($stock_item->stock_id);
117
118                         label_cell($stock_item->item_description, "nowrap" );
119                         qty_cell($stock_item->qty_dispatched, false, get_qty_dec($stock_item->stock_id));
120
121                         if ($order->trans_no!=0)
122                                 amount_cell($stock_item->qty_done);
123
124                         label_cell($stock_item->units);
125                         amount_cell($stock_item->price);
126
127                         percent_cell($stock_item->discount_percent * 100);
128                         amount_cell($line_total);
129
130                         if ($editable_items)
131                         {
132                                 edit_button_cell("Edit$line_no", _("Edit"),
133                                 _('Edit document line'));
134                                 edit_button_cell("Delete$line_no", _("Delete"),
135                                 _('Remove line from document'));
136                         }
137                         end_row();
138                 }
139                 else
140                 {
141                         sales_order_item_controls($order, $k,  $line_no);
142                 }
143
144                 $total += $line_total;
145         }
146
147         if ($id==-1 && $editable_items)
148                 sales_order_item_controls($order, $k);
149
150         $display_total = price_format($total);
151         label_row(_("Total Excluding Shipping"), $display_total, "colspan=6 align=right",
152                 "nowrap align=right", 2);
153
154         end_table();
155     div_end();
156 }
157
158 // ------------------------------------------------------------------------------
159
160 function display_order_header(&$order, $editable, $date_text, $display_tax_group=false)
161 {
162         global $table_style, $Ajax;
163
164         start_table("width=80% $table_style");
165         echo "<tr><td valign=top>"; // outer table
166         echo "<table>";
167
168         $customer_error = "";
169         $change_prices = 0;
170
171         if (isset($order) && !$editable)
172         {
173                 // can't change the customer/branch if items already received on this order
174                 echo $order->customer_name . " - " . $order->deliver_to;
175                 hidden('customer_id', $order->customer_id);
176                 hidden('branch_id', $order->Branch);
177                 hidden('sales_type', $order->sales_type);
178         }
179         else
180         {
181                 customer_list_row(_("Customer:"), 'customer_id', null, false, true);
182
183                 if ($order->customer_id != get_post('customer_id', -1))
184                 {
185                         // customer has changed
186                         $Ajax->activate('branch_id');
187                 }
188                 customer_branches_list_row(_("Branch:"),
189                   $_POST['customer_id'], 'branch_id', null, false, true, true);
190
191         if( ($order->customer_id != get_post('customer_id', -1)) ||
192                         ($order->Branch != get_post('branch_id', -1)) ||
193                         list_updated('customer_id')) {
194
195                 if (!isset($_POST['branch_id']) || $_POST['branch_id'] == "")
196                 {
197                         // ignore errors on customer search box call
198                         if ($_POST['customer_id'] == '')
199                             $customer_error = _("No customer found for entered text.");
200                         else
201                             $customer_error = _("The selected customer does not have any branches. Please create at least one branch.");
202                     unset($_POST['branch_id']);
203                     $order->Branch = 0;
204                 } else
205                 {
206
207                                 $old_order = (PHP_VERSION<5) ? $order : clone( $order );
208                                 $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
209
210                                 $_POST['Location'] = $order->Location;
211                                 $_POST['deliver_to'] = $order->deliver_to;
212                                 $_POST['delivery_address'] = $order->delivery_address;
213                                 $_POST['phone'] = $order->phone;
214                                 $_POST['cash'] = $order->cash;
215                                 if ($order->trans_type == 10)
216                                 {
217                                         $_POST['delivery_date'] = $order->due_date;
218                                         $Ajax->activate('delivery_date');
219                                 }
220                                 $Ajax->activate('Location');
221                                 $Ajax->activate('deliver_to');
222                                 $Ajax->activate('phone');
223                                 $Ajax->activate('delivery_address');
224                                 $Ajax->activate('cash');
225                                 // change prices if necessary
226                                 // what about discount in template case?
227                                 if ($old_order->customer_currency != $order->customer_currency) {
228                                     $change_prices = 1;
229                                 }
230                                 if ($old_order->sales_type != $order->sales_type) {
231                                 //  || $old_order->default_discount!=$order->default_discount
232                                         $_POST['sales_type'] = $order->sales_type;
233                                     $Ajax->activate('sales_type');
234                                     $change_prices = 1;
235                                 }
236                                 unset($old_order);
237                         }
238                 set_global_customer($_POST['customer_id']);
239                 } // changed branch
240         }
241
242         if ($_SESSION['Items']->trans_type!=30) {
243                 ref_cells(_("Reference").':', 'ref', _('Reference number unique for this document type'), null, '');
244         }
245
246         echo "</table>";
247
248         echo "</td><td>"; // outer table
249
250         if (!is_company_currency($order->customer_currency))
251         {
252             div_start('currency');
253                 echo "<table height='5'>";
254                 label_row(_("Customer Currency:"), $order->customer_currency);
255                 exchange_rate_display($order->customer_currency, get_company_currency(),
256                         ($editable ? $_POST['OrderDate'] : $order->document_date));
257                 echo "</table>";
258             div_end();
259                 echo "</td><td>"; // outer table
260         }
261
262
263         echo "<table height='5'>";
264         if ($order->trans_type == 10 && $order->pos != -1) {
265                 sale_payment_list_cells(_('Payment:'), 'cash', null, true);
266                 $cash_payment = get_post('cash', 0);
267                 // current user can issue both credit and cash invoices
268                 if ($order->cash != $cash_payment) {
269                         $order->cash = $cash_payment;
270                         if ($cash_payment) {
271                                 $paym = get_sales_point(user_pos());
272                                 $order->cash_account = $paym['pos_account'];
273                                 $order->account_name = $paym['bank_account_name'];
274                                 $_POST['Location'] = $order->Location = $paym['pos_location'];
275                                 $order->location_name = $paym['location_name'];
276                         }
277                         check_qoh($order);
278                         $Ajax->activate('delivery');
279                         set_focus($order->pos == -1 ? 'delivery_date' : 'account');
280                 }
281         } else
282                 hidden('cash', $order->cash);
283
284         if($editable) {
285                 $str = sales_types_list_row(_("Price List"), 'sales_type', null, true);
286         } else {
287                 label_row(_("Price List:"), $order->sales_type_name);
288         }
289         if ($order->sales_type != $_POST['sales_type']) {
290                 $myrow = get_sales_type($_POST['sales_type']);
291                 $order->set_sales_type($myrow['id'], $myrow['sales_type'],
292                         $myrow['tax_included'], $myrow['factor']);
293                 $Ajax->activate('sales_type');
294                 $change_prices = 1;
295         }
296
297         label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
298         echo "</table>";
299
300         echo "</td><td>"; // outer table
301
302         echo "<table height='5'>";
303
304         if ($editable)
305         {
306                 if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
307                         $_POST['OrderDate'] = $order->document_date;
308
309                 date_row($date_text, 'OrderDate',
310                   _('Date of order receive'), null, 0, 0, 0, null, true);
311                 if (isset($_POST['_OrderDate_changed'])) {
312                         $change_prices = 1;
313                         $Ajax->activate('currency');
314                         if ($order->trans_type == 10) {
315                                 $_POST['delivery_date'] = get_invoice_duedate(get_post('customer_id'), get_post('OrderDate'));
316                         } else 
317                                 $_POST['delivery_date'] = add_days(get_post('OrderDate'), sys_prefs::default_delivery_required_by());
318                         $Ajax->activate('delivery_date');
319                 }
320         }
321         else
322         {
323                 label_row($date_text, $order->document_date);
324                 hidden('OrderDate', $order->document_date);
325         }
326
327         if ($display_tax_group)
328         {
329             label_row(_("Tax Group:"), $order->tax_group_name);
330             hidden('tax_group_id', $_SESSION['Items']->tax_group_id);
331         }
332         echo "</table>";
333
334         echo "</td></tr>";
335
336         end_table(1); // outer table
337
338         if ($change_prices != 0) {
339                 foreach ($order->line_items as $line_no=>$item) {
340                         $line = &$order->line_items[$line_no];
341                         $line->price = get_price($line->stock_id, $order->customer_currency,
342                                 $order->sales_type, $order->price_factor, get_post('OrderDate'));
343                 //              $line->discount_percent = $order->default_discount;
344                 }
345             $Ajax->activate('items_table');
346         }
347
348         return $customer_error;
349 }
350
351 //--------------------------------------------------------------------------------
352
353 function sales_order_item_controls(&$order, &$rowcounter, $line_no=-1)
354 {
355     global $Ajax;
356
357         alt_table_row_color($rowcounter);
358
359         $id = find_submit('Edit');
360         if ($line_no!=-1 && $line_no == $id)
361         {
362                 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
363                 $dec = get_qty_dec($_POST['stock_id']);
364                 $_POST['qty'] = number_format2($order->line_items[$id]->qty_dispatched, $dec);
365                 $_POST['price'] = price_format($order->line_items[$id]->price);
366                 $_POST['Disc'] = percent_format($order->line_items[$id]->discount_percent*100);
367                 $units = $order->line_items[$id]->units;
368                 hidden('stock_id', $_POST['stock_id']);
369                 label_cell($_POST['stock_id']);
370                 label_cell($order->line_items[$line_no]->item_description, "nowrap");
371             $Ajax->activate('items_table');
372         }
373         else
374         {
375                 stock_items_list_cells(null,'stock_id', null, false, true);
376                 if (list_updated('stock_id')) {
377                             $Ajax->activate('price');
378                             $Ajax->activate('units');
379                             $Ajax->activate('qty');
380                             $Ajax->activate('line_total');
381                 }
382
383                 $item_info = get_item_edit_info($_POST['stock_id']);
384                 $units = $item_info["units"];
385                 $dec = get_qty_dec($_POST['stock_id']);
386                 $_POST['qty'] = number_format2(1, $dec);
387
388                 $_POST['price'] = price_format(get_price ($_POST['stock_id'],
389                         $order->customer_currency, $order->sales_type,
390                         $order->price_factor, get_post('OrderDate')));
391                 // default to the customer's discount %
392                 $_POST['Disc'] = percent_format($order->default_discount * 100);
393
394         }
395
396         qty_cells(null, 'qty', $_POST['qty'], null, null, $dec);
397
398         if ($order->trans_no!=0) {
399                 qty_cell($line_no==-1 ? 0 :$order->line_items[$line_no]->qty_done, false, $dec);
400         }
401
402         label_cell($units, '', 'units');
403
404         $str = amount_cells(null, 'price');
405
406         small_amount_cells(null, 'Disc', percent_format($_POST['Disc']), null, null, user_percent_dec());
407
408         $line_total = input_num('qty') * input_num('price') * (1 - input_num('Disc') / 100);
409
410         amount_cell($line_total, false, '','line_total');
411
412         if ($id!=-1)
413         {
414                 edit_button_cell('UpdateItem', _("Update"),
415                                 _('Confirm changes'));
416                 edit_button_cell('CancelItemChanges', _("Cancel"),
417                                 _('Cancel changes'));
418                 hidden('LineNo', $line_no);
419                 set_focus('qty');
420         }
421         else
422         {
423                 submit_cells('AddItem', _("Add Item"), "colspan=2",
424                     _('Add new item to document'), true);
425         }
426
427         end_row();
428 }
429
430 //--------------------------------------------------------------------------------
431
432 function display_delivery_details(&$order)
433 {
434         global $table_style2, $Ajax;
435
436 div_start('delivery');  
437
438         if (get_post('cash', 0)) {      // Direct payment sale
439                 display_heading(_('Cash payment'));
440                 echo "<table $table_style2 width=60% align=center>";
441                 label_row(_("Deliver from Location:"), $order->location_name);
442
443                 hidden('Location', $order->Location);
444                 label_row(_("Cash account:"), $order->account_name);
445                 textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
446                 echo "</table>";
447         } else {
448
449                 if ($order->trans_type==10)
450                 {
451                         $title = _("Delivery Details");
452                         $delname = _("Due Date").':';
453                 }
454                 elseif ($order->trans_type==13)
455                 {
456                         $title = _("Invoice Delivery Details");
457                         $delname = _("Invoice before").':';
458                 }
459                 else
460                 {
461                         $title = _("Order Delivery Details");
462                         $delname = _("Required Delivery Date").':';
463                 }
464                 display_heading($title);
465                 echo "<br>";
466                 start_table("$table_style2 width=90%");
467                 echo "<tr valign=top><td>"; // outer table
468                 echo "<table>";
469
470                 locations_list_row(_("Deliver from Location:"), 'Location', null, 
471                         false, true);
472                 if (list_updated('Location'))
473                         check_qoh($order);
474
475                 date_row($delname, 'delivery_date',
476                         $order->trans_type==30 ?  _('Enter requested day of delivery') : '', 
477                         $order->due_date, 0, 0, 0);
478                 text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 40, 40,
479                         _('Additional identifier for delivery e.g. name of receiving person'));
480
481                 textarea_row(_("Address:"), 'delivery_address', $order->delivery_address, 35, 5,
482                         _('Delivery address. Default is address of customer branch'));
483                 text_row(_("Contact Phone Number:"), 'phone', $order->phone, 25, 25,
484                     _('Phone number of ordering person. Defaults to branch phone number'));
485
486                 echo "</table>";
487
488                 echo "</td><td>"; // outer table
489
490                 echo "<table>";
491
492                 text_row(_("Customer Reference:"), 'cust_ref', $order->cust_ref, 25, 25,
493                   _('Customer reference number for this order (if any)'));
494                 textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
495
496                 small_amount_row(_("Shipping Charge:"), 'freight_cost',
497                 price_format(get_post('freight_cost',0)));
498
499                 shippers_list_row(_("Shipping Company:"), 'ship_via', $order->ship_via);
500                 echo "</table>";
501                 echo "</td></tr>";
502                 end_table(1); // outer table
503         }
504         div_end();
505 }
506
507 function check_qoh($order) 
508 {
509         $msg = '';
510         foreach($order->line_items as $line_no => $line) {
511                 $qoh = get_qoh_on_date($line->stock_id, $_POST['Location'], $_POST['OrderDate']);
512                 if ($line->qty_dispatched > $qoh)       {
513                         $msg .= $line->stock_id . " - " . $line->item_description . ": " .
514                                 _("Quantity On Hand") . " = " 
515                                 . number_format2($qoh, get_qty_dec($line->stock_id)) . '<br>';
516                 }
517         }
518         if (strlen($msg)) {
519                 display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:")
520                         . '<br>'. $msg);
521                 return false;
522         }
523         return true;
524 }
525 ?>