Automatic calculation of not set item prices from home currency and base price list...
[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
8 function add_to_order(&$order, $new_item, $new_item_qty, $price, $discount)
9 {
10
11         foreach ($order->line_items AS $order_item)
12         {
13                 if (strcasecmp($order_item->stock_id, $new_item) == 0)
14                 {
15                         display_notification(_("For Part :") . $new_item . " " . _("This item is already on this order. You have been warned."));
16                         break;
17                 }
18         }
19
20         $order->add_to_cart (count($order->line_items),$new_item, $new_item_qty, $price, $discount);
21 }
22
23 //---------------------------------------------------------------------------------
24
25 function get_customer_details_to_order(&$order, $customer_id, $branch_id)
26 {
27         $ret_error = "";
28
29         $myrow = get_customer_to_order($customer_id);
30
31         $name = $myrow['name'];
32
33         if ($myrow['dissallow_invoices'] == 1)
34                 $ret_error = _("The selected customer account is currently on hold. Please contact the credit control personnel to discuss.");
35
36 /*      if (!isset($_POST['branch_id']) || $_POST['branch_id'] == "")
37         {
38                 $ret_error = _("The selected customer does not have any branches. Please create at least one branch.");
39                 unset($_POST['branch_id']);
40                 $order->Branch = 0;
41         }
42 */
43         $deliver = $myrow['address']; // in case no delivery address.
44
45         $order->set_customer($customer_id, $name, $myrow['curr_code'], $myrow['discount']);
46
47         // the sales type determines the price list to be used by default
48         $order->set_sales_type($myrow['salestype'], $myrow['sales_type'], $myrow['tax_included'],
49             $myrow['factor']);
50
51 //      if ($order->Branch != 0)
52         {
53                 $result = get_branch_to_order($customer_id, $branch_id);
54
55                 if (db_num_rows($result) == 0)
56                 {
57                 return _("The selected customer and branch are not valid, or the customer does not have any branches.");
58                 }
59
60                 $myrow = db_fetch($result);
61
62                 $order->set_branch($branch_id, $myrow["tax_group_id"],
63                 $myrow["tax_group_name"], $myrow["phone"], $myrow["email"]);
64
65                 $address = $myrow["br_post_address"];
66
67                 if (strlen($address) <= 1)      // if branch has no address
68                         $address = $deliver;    // set sales order address
69
70                 $order->set_location($myrow["default_location"], $myrow["location_name"]);
71                 $order->set_delivery($myrow["default_ship_via"], $myrow["br_name"],
72                 $address);
73         }
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         start_table("$table_style colspan=7 width=90%");
86         $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
87                 _("Delivered"),
88                 _("Unit"), _("Price"), _("Discount %"), _("Total"), "");
89
90         if ($order->trans_no==0) {
91         unset( $th[3] );
92         }
93
94         if (count($order->line_items)) $th[]= '';
95
96         table_header($th);
97
98         $total = 0;
99         $k = 0;  //row colour counter
100
101         $id = find_submit('Edit');
102
103         foreach ($order->line_items as $line_no=>$stock_item)
104         {
105
106                 $line_total = round($stock_item->qty_dispatched * $stock_item->price * (1 - $stock_item->discount_percent),
107                    user_price_dec());
108
109
110                 if (!$editable_items || $id != $line_no)
111                 {
112                         alt_table_row_color($k);
113
114                         view_stock_status_cell($stock_item->stock_id);
115
116                         label_cell($stock_item->item_description, "nowrap" );
117                         qty_cell($stock_item->qty_dispatched);
118
119                         if ($order->trans_no!=0)
120                         amount_cell($stock_item->qty_done);
121
122                         label_cell($stock_item->units);
123                         amount_cell($stock_item->price);
124
125                         percent_cell($stock_item->discount_percent * 100);
126                         amount_cell($line_total);
127
128                         if ($editable_items)
129                         {
130                                 edit_button_cell("Edit$line_no", _("Edit"));
131                                 edit_button_cell("Delete$line_no", _("Delete"));
132                         }
133                         end_row();
134                 }
135                 else
136                 {
137                         sales_order_item_controls($order, $k,  $line_no);
138                 }
139
140                 $total += $line_total;
141         }
142
143         if ($id==-1 && $editable_items)
144                 sales_order_item_controls($order, $k);
145
146         $display_total = price_format($total);
147         label_row(_("Total Excluding Shipping"), $display_total, "colspan=6 align=right",
148                 "nowrap align=right", 2);
149
150         end_table();
151 }
152
153 // ------------------------------------------------------------------------------
154
155 function display_order_header(&$order, $editable, $date_text, $display_tax_group=false)
156 {
157         global $table_style;
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
176                 if (!isset($_POST['customer_id']) && (get_global_customer() != reserved_words::get_all()))
177                                 $_POST['customer_id'] = get_global_customer();
178
179                 customer_list_row(_("Customer:"), 'customer_id', null, false, true);
180
181                 if (!isset($_POST['delivery_date']))
182                 {
183                         if ($order->trans_type==10)
184                                 $_POST['delivery_date'] = $_SESSION['Items']->due_date =
185                                         get_invoice_duedate($_POST['customer_id'], $_POST['OrderDate']);
186                         else
187                                 $_POST['delivery_date'] = $_SESSION['Items']->due_date =
188                                         add_days($_POST['OrderDate'], 10); // FIX this should be in company prefs
189                 }
190                 if ($order->customer_id != $_POST['customer_id'])
191                 {
192                         // customer has changed
193                         // clear the branch selection
194                         unset($_POST['branch_id']);
195                 }
196                 if (!isset($_POST['branch_id']))
197                         $_POST['branch_id'] = "";
198
199                 customer_branches_list_row(_("Branch:"), $_POST['customer_id'], 'branch_id', null, false, true, true);
200
201                 if (!isset($_POST['branch_id']) || $_POST['branch_id'] == "")
202                 {
203                         $customer_error = _("The selected customer does not have any branches. Please create at least one branch.");
204                         unset($_POST['branch_id']);
205                         $order->Branch = 0;
206                 } else
207                 {
208                         //set_global_customer($_POST['customer_id']);
209                         if (($order->customer_id != $_POST['customer_id']) ||
210                                 ($order->Branch != $_POST['branch_id'])) {
211
212                                 $old_order = (PHP_VERSION<5) ? $order : clone( $order );
213                                 $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
214                                 $_POST['Location'] = $order->Location;
215                                 $_POST['deliver_to'] = $order->deliver_to;
216                                 $_POST['delivery_address'] = $order->delivery_address;
217                                 $_POST['phone'] = $order->phone;
218                                 $_POST['sales_type'] = $order->sales_type;
219                                 $_POST['factor'] = $order->price_factor;
220
221                                 // change prices if necessary
222                                 // what about discount in template case?
223                                 if ( $old_order->customer_currency!=$order->customer_currency
224                                         || $old_order->sales_type!=$order->sales_type
225                                 //  || $old_order->default_discount!=$order->default_discount
226                                         ) {
227                                         $change_prices = 1;
228                                 }
229
230                         }
231                 } // changed branch
232                 set_global_customer($_POST['customer_id']);
233         }
234
235         if ($_SESSION['Items']->trans_type!=30) {
236                 ref_cells(_("Reference").':', 'ref', null, '');
237         }
238
239         echo "</table>";
240
241         echo "</td><td>"; // outer table
242
243         if (!is_company_currency($order->customer_currency))
244         {
245                 echo "<table height='5'>";
246                 label_row(_("Customer Currency:"), $order->customer_currency);
247                 exchange_rate_display($order->customer_currency, get_company_currency(),
248                         ($editable ? $_POST['OrderDate'] : $order->document_date), $editable);
249                 echo "</table>";
250                 echo "</td><td>"; // outer table
251         }
252
253         if ($order->sales_type != $_POST['sales_type']) {
254                 $myrow = get_sales_type($_POST['sales_type']);
255                 $order->set_sales_type($myrow['id'], $myrow['sales_type'],
256                 $myrow['tax_included'], $myrow['factor']);
257                 $change_prices = 1;
258         }
259
260         if ($change_prices != 0) {
261                 foreach ($order->line_items as $line_no=>$item) {
262                         $line = &$order->line_items[$line_no];
263                         $line->price = get_price($line->stock_id, $order->customer_currency,
264                                 $order->sales_type, $order->price_factor, $order->document_date);
265                 //              $line->discount_percent = $order->default_discount;
266                 }
267         }
268
269         echo "<table height='5'>";
270         if($editable)
271                 sales_types_list_row(_("Price List"), 'sales_type', $_POST['sales_type'], true);
272         else {
273                 label_row(_("Price List:"), $order->sales_type_name);
274         }
275         label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
276         echo "</table>";
277
278         echo "</td><td>"; // outer table
279
280         echo "<table height='5'>";
281
282         if ($editable)
283         {
284                 if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
285                         $_POST['OrderDate'] = $order->document_date;
286
287                 date_row($date_text, 'OrderDate');
288         }
289         else
290         {
291                 label_row($date_text, $order->document_date);
292                 hidden('OrderDate', $order->document_date);
293         }
294
295         if ($display_tax_group)
296         {
297             label_row(_("Tax Group:"), $order->tax_group_name);
298             hidden('tax_group_id', $_SESSION['Items']->tax_group_id);
299         }
300         echo "</table>";
301
302         echo "</td></tr>";
303
304         end_table(1); // outer table
305
306         return $customer_error;
307 }
308
309 //--------------------------------------------------------------------------------
310
311 function sales_order_item_controls(&$order, &$rowcounter, $line_no=-1)
312 {
313         alt_table_row_color($rowcounter);
314
315         $id = find_submit('Edit');
316         if ($line_no!=-1 && $line_no == $id)
317         {
318                 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
319                 $_POST['qty'] = qty_format($order->line_items[$id]->qty_dispatched);
320                 $_POST['price'] = price_format($order->line_items[$id]->price);
321                 $_POST['Disc'] = percent_format($order->line_items[$id]->discount_percent*100);
322                 $_POST['units'] = $order->line_items[$id]->units;
323                 hidden('stock_id', $_POST['stock_id']);
324                 label_cell($_POST['stock_id']);
325                 label_cell($order->line_items[$line_no]->item_description, "nowrap");
326         }
327         else
328         {
329
330                 if (get_company_pref('no_item_list'))
331                 {
332                         echo "<td colspan=2 nowrap>\n";
333                         stock_items_list('stock_id', null, false, true);
334                         echo "</td>\n";
335                 }
336                 else
337                 {
338                 text_cells(null, "StockID2", '', 12, 10, "", "", "class='combo' rel='stock_id'"/* onblur='this.form.submit();'"*/);
339                 stock_items_list_cells(null, 'stock_id', null, false, true, "class='combo' rel='StockID2'");
340                 }
341                 $item_info = get_item_edit_info($_POST['stock_id']);
342                 $_POST['units'] = $item_info["units"];
343
344                 $_POST['qty'] = qty_format(1);
345                 $_POST['price'] = price_format(get_price ($_POST['stock_id'],
346                         $order->customer_currency, $order->sales_type, 
347                         $order->price_factor, $order->document_date));
348                 // default to the customer's discount %
349                 $_POST['Disc'] = percent_format($order->default_discount * 100);
350         }
351
352         qty_cells(null, 'qty', qty_format($_POST['qty']));
353
354         if ($order->trans_no!=0) {
355                 amount_cell($line_no==-1 ? 0 :$order->line_items[$line_no]->qty_done);
356         }
357
358         label_cell($_POST['units']);
359         amount_cells(null, 'price');
360         small_amount_cells(null, 'Disc', percent_format($_POST['Disc']), null, null, user_percent_dec());
361
362         $line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
363         amount_cell($line_total);
364
365         if ($id!=-1)
366         {
367                 edit_button_cell('UpdateItem', _("Update"));
368                 edit_button_cell('CancelItemChanges', _("Cancel"));
369                 hidden('LineNo', $line_no);
370                 set_focus('qty');
371         }
372         else
373         {
374                 submit_cells('AddItem', _("Add Item"), "colspan=2");
375         }
376
377         end_row();
378 }
379
380 //--------------------------------------------------------------------------------
381
382 function display_delivery_details(&$order)
383 {
384         global $table_style2;
385         if ($order->trans_type==10)
386         {
387                 $title = _("Delivery Details");
388                 $delname = _("Due Date").':';
389         }
390         elseif ($order->trans_type==13)
391         {
392                 $title = _("Invoice Delivery Details");
393                 $delname = _("Invoice before").':';
394         }
395         else
396         {
397                 $title = _("Order Delivery Details");
398                 $delname = _("Required Delivery Date").':';
399         }
400         display_heading($title);
401         echo "<br>";
402         start_table("$table_style2 width=90%");
403         echo "<tr valign=top><td>"; // outer table
404
405         echo "<table>";
406
407 //      echo $order->Location, $_POST['Location'];
408
409         locations_list_row(_("Deliver from Location:"), 'Location', $_POST['Location']);
410
411         date_row($delname, 'delivery_date', $order->due_date, 0, 0, 0);
412
413         text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 40, 40);
414
415         textarea_row(_("Address:"), 'delivery_address', $order->delivery_address, 35, 5);
416         text_row(_("Contact Phone Number:"), 'phone', $order->phone, 25, 25);
417
418         echo "</table>";
419
420         echo "</td><td>"; // outer table
421
422         echo "<table>";
423
424         text_row(_("Customer Reference:"), 'cust_ref', $order->cust_ref, 25, 25);
425         textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
426
427         small_amount_row(_("Shipping Charge:"), 'freight_cost',
428             price_format($_POST['freight_cost']));
429
430         shippers_list_row(_("Shipping Company:"), 'ship_via', $order->ship_via);
431
432         echo "</table>";
433
434         echo "</td></tr>";
435         end_table(1); // outer table
436 }
437
438 ?>