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