Fixed page update after customer/branch change.
[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                                 if (get_post('cash') !== $order->cash) {
215                                         $_POST['cash'] = $order->cash;
216                                         $Ajax->activate('delivery');
217                                         $Ajax->activate('cash');
218                                 } else {
219                                         if ($order->trans_type == 10)
220                                         {
221                                                 $_POST['delivery_date'] = $order->due_date;
222                                                 $Ajax->activate('delivery_date');
223                                         }
224                                         $Ajax->activate('Location');
225                                         $Ajax->activate('deliver_to');
226                                         $Ajax->activate('phone');
227                                         $Ajax->activate('delivery_address');
228                                 }
229                                 // change prices if necessary
230                                 // what about discount in template case?
231                                 if ($old_order->customer_currency != $order->customer_currency) {
232                                     $change_prices = 1;
233                                 }
234                                 if ($old_order->sales_type != $order->sales_type) {
235                                 //  || $old_order->default_discount!=$order->default_discount
236                                         $_POST['sales_type'] = $order->sales_type;
237                                     $Ajax->activate('sales_type');
238                                     $change_prices = 1;
239                                 }
240                                 unset($old_order);
241                         }
242                 set_global_customer($_POST['customer_id']);
243                 } // changed branch
244         }
245
246         if ($_SESSION['Items']->trans_type!=30) {
247                 ref_cells(_("Reference").':', 'ref', _('Reference number unique for this document type'), null, '');
248         }
249
250         echo "</table>";
251
252         echo "</td><td>"; // outer table
253
254         if (!is_company_currency($order->customer_currency))
255         {
256             div_start('currency');
257                 echo "<table height='5'>";
258                 label_row(_("Customer Currency:"), $order->customer_currency);
259                 exchange_rate_display($order->customer_currency, get_company_currency(),
260                         ($editable ? $_POST['OrderDate'] : $order->document_date));
261                 echo "</table>";
262             div_end();
263                 echo "</td><td>"; // outer table
264         }
265
266
267         echo "<table height='5'>";
268         if ($order->trans_type == 10 && $order->pos != -1) {
269                 sale_payment_list_cells(_('Payment:'), 'cash', null, true);
270                 $cash_payment = get_post('cash', 0);
271                 // current user can issue both credit and cash invoices
272                 if ($order->cash != $cash_payment) {
273                         $order->cash = $cash_payment;
274                         if ($cash_payment) {
275                                 $paym = get_sales_point(user_pos());
276                                 $order->cash_account = $paym['pos_account'];
277                                 $order->account_name = $paym['bank_account_name'];
278                                 $_POST['Location'] = $order->Location = $paym['pos_location'];
279                                 $order->location_name = $paym['location_name'];
280                         }
281                         check_qoh($order);
282                         $Ajax->activate('delivery');
283                         set_focus($order->pos == -1 ? 'delivery_date' : 'account');
284                 }
285         } else
286                 hidden('cash', $order->cash);
287
288         if($editable) {
289                 $str = sales_types_list_row(_("Price List"), 'sales_type', null, true);
290         } else {
291                 label_row(_("Price List:"), $order->sales_type_name);
292         }
293         if ($order->sales_type != $_POST['sales_type']) {
294                 $myrow = get_sales_type($_POST['sales_type']);
295                 $order->set_sales_type($myrow['id'], $myrow['sales_type'],
296                         $myrow['tax_included'], $myrow['factor']);
297                 $Ajax->activate('sales_type');
298                 $change_prices = 1;
299         }
300
301         label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
302         echo "</table>";
303
304         echo "</td><td>"; // outer table
305
306         echo "<table height='5'>";
307
308         if ($editable)
309         {
310                 if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
311                         $_POST['OrderDate'] = $order->document_date;
312
313                 date_row($date_text, 'OrderDate',
314                   _('Date of order receive'), null, 0, 0, 0, null, true);
315                 if (isset($_POST['_OrderDate_changed'])) {
316                         $change_prices = 1;
317                         $Ajax->activate('currency');
318                         if ($order->trans_type == 10) {
319                                 $_POST['delivery_date'] = get_invoice_duedate(get_post('customer_id'), get_post('OrderDate'));
320                         } else 
321                                 $_POST['delivery_date'] = add_days(get_post('OrderDate'), sys_prefs::default_delivery_required_by());
322                         $Ajax->activate('delivery_date');
323                 }
324         }
325         else
326         {
327                 label_row($date_text, $order->document_date);
328                 hidden('OrderDate', $order->document_date);
329         }
330
331         if ($display_tax_group)
332         {
333             label_row(_("Tax Group:"), $order->tax_group_name);
334             hidden('tax_group_id', $_SESSION['Items']->tax_group_id);
335         }
336         echo "</table>";
337
338         echo "</td></tr>";
339
340         end_table(1); // outer table
341
342         if ($change_prices != 0) {
343                 foreach ($order->line_items as $line_no=>$item) {
344                         $line = &$order->line_items[$line_no];
345                         $line->price = get_price($line->stock_id, $order->customer_currency,
346                                 $order->sales_type, $order->price_factor, get_post('OrderDate'));
347                 //              $line->discount_percent = $order->default_discount;
348                 }
349             $Ajax->activate('items_table');
350         }
351
352         return $customer_error;
353 }
354
355 //--------------------------------------------------------------------------------
356
357 function sales_order_item_controls(&$order, &$rowcounter, $line_no=-1)
358 {
359     global $Ajax;
360
361         alt_table_row_color($rowcounter);
362
363         $id = find_submit('Edit');
364         if ($line_no!=-1 && $line_no == $id)
365         {
366                 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
367                 $dec = get_qty_dec($_POST['stock_id']);
368                 $_POST['qty'] = number_format2($order->line_items[$id]->qty_dispatched, $dec);
369                 $_POST['price'] = price_format($order->line_items[$id]->price);
370                 $_POST['Disc'] = percent_format($order->line_items[$id]->discount_percent*100);
371                 $units = $order->line_items[$id]->units;
372                 hidden('stock_id', $_POST['stock_id']);
373                 label_cell($_POST['stock_id']);
374                 label_cell($order->line_items[$line_no]->item_description, "nowrap");
375             $Ajax->activate('items_table');
376         }
377         else
378         {
379                 stock_items_list_cells(null,'stock_id', null, false, true);
380                 if (list_updated('stock_id')) {
381                             $Ajax->activate('price');
382                             $Ajax->activate('units');
383                             $Ajax->activate('qty');
384                             $Ajax->activate('line_total');
385                 }
386
387                 $item_info = get_item_edit_info($_POST['stock_id']);
388                 $units = $item_info["units"];
389                 $dec = get_qty_dec($_POST['stock_id']);
390                 $_POST['qty'] = number_format2(1, $dec);
391
392                 $_POST['price'] = price_format(get_price ($_POST['stock_id'],
393                         $order->customer_currency, $order->sales_type,
394                         $order->price_factor, get_post('OrderDate')));
395                 // default to the customer's discount %
396                 $_POST['Disc'] = percent_format($order->default_discount * 100);
397
398         }
399
400         qty_cells(null, 'qty', $_POST['qty'], null, null, $dec);
401
402         if ($order->trans_no!=0) {
403                 qty_cell($line_no==-1 ? 0 :$order->line_items[$line_no]->qty_done, false, $dec);
404         }
405
406         label_cell($units, '', 'units');
407
408         $str = amount_cells(null, 'price');
409
410         small_amount_cells(null, 'Disc', percent_format($_POST['Disc']), null, null, user_percent_dec());
411
412         $line_total = input_num('qty') * input_num('price') * (1 - input_num('Disc') / 100);
413
414         amount_cell($line_total, false, '','line_total');
415
416         if ($id!=-1)
417         {
418                 edit_button_cell('UpdateItem', _("Update"),
419                                 _('Confirm changes'));
420                 edit_button_cell('CancelItemChanges', _("Cancel"),
421                                 _('Cancel changes'));
422                 hidden('LineNo', $line_no);
423                 set_focus('qty');
424         }
425         else
426         {
427                 submit_cells('AddItem', _("Add Item"), "colspan=2",
428                     _('Add new item to document'), true);
429         }
430
431         end_row();
432 }
433
434 //--------------------------------------------------------------------------------
435
436 function display_delivery_details(&$order)
437 {
438         global $table_style2, $Ajax;
439
440 div_start('delivery');  
441
442         if (get_post('cash', 0)) {      // Direct payment sale
443                 display_heading(_('Cash payment'));
444                 echo "<table $table_style2 width=60% align=center>";
445                 label_row(_("Deliver from Location:"), $order->location_name);
446
447                 hidden('Location', $order->Location);
448                 label_row(_("Cash account:"), $order->account_name);
449                 textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
450                 echo "</table>";
451         } else {
452
453                 if ($order->trans_type==10)
454                 {
455                         $title = _("Delivery Details");
456                         $delname = _("Due Date").':';
457                 }
458                 elseif ($order->trans_type==13)
459                 {
460                         $title = _("Invoice Delivery Details");
461                         $delname = _("Invoice before").':';
462                 }
463                 else
464                 {
465                         $title = _("Order Delivery Details");
466                         $delname = _("Required Delivery Date").':';
467                 }
468                 display_heading($title);
469                 echo "<br>";
470                 start_table("$table_style2 width=90%");
471                 echo "<tr valign=top><td>"; // outer table
472                 echo "<table>";
473
474                 locations_list_row(_("Deliver from Location:"), 'Location', null, 
475                         false, true);
476                 if (list_updated('Location'))
477                         check_qoh($order);
478
479                 date_row($delname, 'delivery_date',
480                         $order->trans_type==30 ?  _('Enter requested day of delivery') : '', 
481                         $order->due_date, 0, 0, 0);
482                 text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 40, 40,
483                         _('Additional identifier for delivery e.g. name of receiving person'));
484
485                 textarea_row(_("Address:"), 'delivery_address', $order->delivery_address, 35, 5,
486                         _('Delivery address. Default is address of customer branch'));
487                 text_row(_("Contact Phone Number:"), 'phone', $order->phone, 25, 25,
488                     _('Phone number of ordering person. Defaults to branch phone number'));
489
490                 echo "</table>";
491
492                 echo "</td><td>"; // outer table
493
494                 echo "<table>";
495
496                 text_row(_("Customer Reference:"), 'cust_ref', $order->cust_ref, 25, 25,
497                   _('Customer reference number for this order (if any)'));
498                 textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
499
500                 small_amount_row(_("Shipping Charge:"), 'freight_cost',
501                 price_format(get_post('freight_cost',0)));
502
503                 shippers_list_row(_("Shipping Company:"), 'ship_via', $order->ship_via);
504                 echo "</table>";
505                 echo "</td></tr>";
506                 end_table(1); // outer table
507         }
508         div_end();
509 }
510
511 function check_qoh($order) 
512 {
513         $msg = '';
514         foreach($order->line_items as $line_no => $line) {
515                 $qoh = get_qoh_on_date($line->stock_id, $_POST['Location'], $_POST['OrderDate']);
516                 if ($line->qty_dispatched > $qoh)       {
517                         $msg .= $line->stock_id . " - " . $line->item_description . ": " .
518                                 _("Quantity On Hand") . " = " 
519                                 . number_format2($qoh, get_qty_dec($line->stock_id)) . '<br>';
520                 }
521         }
522         if (strlen($msg)) {
523                 display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:")
524                         . '<br>'. $msg);
525                 return false;
526         }
527         return true;
528 }
529 ?>