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