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