5494ae00f83a6b7e27d5af9f41f93487f4505fee
[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 = $stock_item->qty_dispatched * $stock_item->price * (1 - $stock_item->discount_percent);
104  
105                 if (!isset($_GET['Edit']))
106                         $id = -1;
107                 else
108                         $id = $_GET['Edit'];
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);
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                 amount_cell($stock_item->discount_percent * 100);
126                 amount_cell($line_total);
127
128                 if ($editable_items) 
129                 {
130                         edit_link_cell(SID . "Edit=$line_no");
131                         delete_link_cell(SID . "Delete=$line_no");
132                 }
133                 end_row();
134                 } 
135                 else 
136                 {
137                         sales_order_item_controls($order, $line_no);
138                 }
139
140                 $total += $line_total;
141         }
142
143         if (!isset($_GET['Edit']) && $editable_items)
144                 sales_order_item_controls($order);
145
146         $display_total = number_format2($total,user_price_dec());
147         label_row(_("Total Excluding Shipping"), $display_total, "colspan=6 align=right",
148                 "nowrap align=right");
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         } 
172         else 
173         {
174
175             if (!isset($_POST['customer_id']) && (get_global_customer() != reserved_words::get_all()))
176                     $_POST['customer_id'] = get_global_customer();
177
178             customer_list_row(_("Customer:"), 'customer_id', null, false, true);
179
180             if (!isset($_POST['delivery_date']))
181             {
182                         if ($order->trans_type==10)
183                                 $_POST['delivery_date'] = $_SESSION['Items']->due_date = 
184                                         get_invoice_duedate($_POST['customer_id'], $_POST['OrderDate']);
185                         else    
186                                 $_POST['delivery_date'] = $_SESSION['Items']->due_date = 
187                                         add_days($_POST['OrderDate'], 10); // FIX this should be in company prefs
188             }           
189                 if ($order->customer_id != $_POST['customer_id']) 
190             {
191                         // customer has changed
192
193                         // delete all the order items - drastic but necessary because of
194                         // change of currency, sales type, etc
195         //      $order->clear_items();
196                         // clear the branch selection
197                         unset($_POST['branch_id']);
198             }
199             if (!isset($_POST['branch_id']))
200                         $_POST['branch_id'] = "";
201
202                 customer_branches_list_row(_("Branch:"), $_POST['customer_id'], 'branch_id', null, false, true, true);
203
204         if (!isset($_POST['branch_id']) || $_POST['branch_id'] == "") 
205         {
206                 $customer_error = _("The selected customer does not have any branches. Please create at least one branch.");
207                 unset($_POST['branch_id']);
208                 $order->Branch = 0;
209         } else  
210         {
211                 //set_global_customer($_POST['customer_id']);
212             if (($order->customer_id != $_POST['customer_id']) || 
213                 ($order->Branch != $_POST['branch_id'])) {
214
215                   $old_order = (PHP_VERSION<5) ? $order : clone( $order );
216                   $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
217                   $_POST['Location'] = $order->Location;
218                   $_POST['deliver_to'] = $order->deliver_to;
219                   $_POST['delivery_address'] = $order->delivery_address;
220                   $_POST['phone'] = $order->phone;
221                   $_POST['sales_type'] = $order->sales_type;
222
223                 // change prices if necessary
224                 // what about discount in template case?
225                   if ( $old_order->customer_currency!=$order->customer_currency
226                     || $old_order->sales_type!=$order->sales_type
227                 //  || $old_order->default_discount!=$order->default_discount
228                     ) {
229                         $change_prices = 1;
230                     }
231                   
232             }
233          } // changed branch
234             set_global_customer($_POST['customer_id']);
235         }
236
237         if ($_SESSION['Items']->trans_type!=30) {
238                 ref_cells(_("Reference").':', 'ref', null, "");
239         }
240
241         echo "</table>";
242
243         echo "</td><td>"; // outer table
244
245         if (!is_company_currency($order->customer_currency))
246         {
247                 echo "<table height='5'>";
248                 label_row(_("Customer Currency:"), $order->customer_currency);
249                 exchange_rate_display($order->customer_currency, get_company_currency(),        
250                         ($editable?$_POST['OrderDate']:$order->document_date), $editable);
251                 echo "</table>";
252                 echo "</td><td>"; // outer table
253         }
254
255         if( $order->sales_type != $_POST['sales_type']) {
256             $myrow = get_sales_type($_POST['sales_type']);
257             $order->set_sales_type($myrow['id'], $myrow['sales_type'], 
258                 $myrow['tax_included']);
259             $change_prices = 1;
260         }
261
262         if($change_prices!=0)
263         {
264             foreach ($order->line_items as $line_no=>$item) {
265                         $line = &$order->line_items[$line_no];
266                         $line->price = get_price($line->stock_id, $order->customer_currency,
267                             $order->sales_type);
268                 //              $line->discount_percent = $order->default_discount;
269             }
270         }
271
272         echo "<table height='5'>";
273         if($editable)
274             sales_types_list_row(_("Price List"), 'sales_type', $_POST['sales_type'], true);
275         else
276             label_row(_("Price List:"), $order->sales_type_name);
277         label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
278         echo "</table>";
279
280         echo "</td><td>"; // outer table
281
282         echo "<table height='5'>";
283
284         if ($editable) 
285         {
286         if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
287                 $_POST['OrderDate'] = $order->document_date;
288
289                 date_row($date_text, 'OrderDate');
290         }
291         else 
292         {
293                 label_row($date_text, $order->document_date);
294                 hidden('OrderDate', $order->document_date);
295         }
296
297         if ($display_tax_group) 
298         {
299 /*              if ($editable) 
300                 {
301             if (!isset($_POST['tax_group_id']))
302                 $_POST['tax_group_id'] = $_SESSION['Items']->tax_group_id;
303             tax_groups_list_row(_("Tax Group:"), 'tax_group_id', $_POST['tax_group_id'], true);
304                 } 
305                 else 
306 */              {
307                         label_row(_("Tax Group:"), $order->tax_group_name);
308                         hidden('tax_group_id', $_SESSION['Items']->tax_group_id);
309                 }
310         }
311         echo "</table>";
312
313         echo "</td></tr>";
314         
315         end_table(1); // outer table
316
317         return $customer_error;
318 }
319
320 //--------------------------------------------------------------------------------
321
322 function sales_order_item_controls(&$order, $line_no=-1)
323 {
324         start_row();
325         if (isset($_GET['Edit']) && $line_no != -1)
326         {
327                 if (!isset($_POST['stock_id']))
328                         $_POST['stock_id'] = $order->line_items[$_GET['Edit']]->stock_id;
329                 if (!isset($_POST['qty']) || ($_POST['qty'] == ""))
330                         $_POST['qty'] = $order->line_items[$_GET['Edit']]->qty_dispatched;
331                 if (!isset($_POST['price']) || ($_POST['price'] == ""))
332                         $_POST['price'] = $order->line_items[$_GET['Edit']]->price;
333                 if (!isset($_POST['Disc']) || ($_POST['Disc'] == ""))
334                         $_POST['Disc'] = ($order->line_items[$_GET['Edit']]->discount_percent)*100;
335
336                 $_POST['units'] = $order->line_items[$_GET['Edit']]->units;
337                 hidden('stock_id', $_POST['stock_id']);
338                 label_cell($_POST['stock_id']);
339                 label_cell($order->line_items[$line_no]->item_description);
340         } 
341         else 
342         {
343                 global $no_item_list; 
344                 if ($no_item_list)
345                 {
346                         echo "<td colspan=2>\n";
347                         stock_items_list('stock_id', null, false, true);
348                         echo "</td>\n";
349                 }
350                 else
351                 {
352                   text_cells(null, "StockID2", "", 12, 10, "", "", "onkeyup='recalcAccounts();' onKeyDown='if (event.keyCode==13) event.keyCode=9;' onblur='return setAccount(0, true);'");
353                         stock_items_list_cells(null, 'stock_id', null, false, false, "onchange='return setAccount(1, true)'");
354                 }
355                 $item_info = get_item_edit_info($_POST['stock_id']);
356                 $_POST['units'] = $item_info["units"];
357
358                 $_POST['qty'] = 1;
359                 $_POST['price'] = get_price ($_POST['stock_id'], 
360                     $order->customer_currency, $order->sales_type);
361                 // default to the customer's discount %
362                 $_POST['Disc'] = $order->default_discount * 100;
363         }
364
365         text_cells(null, 'qty', $_POST['qty'], 12, 15);
366
367         if ($order->trans_no!=0) {
368                 amount_cell($line_no==-1 ? 0 :$order->line_items[$line_no]->qty_done);
369         }
370
371         label_cell($_POST['units']);
372         amount_cells(null, 'price');
373         text_cells(null, 'Disc', $_POST['Disc'], 7, 5);
374
375         $line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
376         amount_cell($line_total);
377
378         if (isset($_GET['Edit'])) 
379         {
380         submit_cells('UpdateItem', _("Update"));
381         submit_cells('CancelItemChanges', _("Cancel"));
382                 hidden('LineNo', $line_no);
383         } 
384         else 
385         {
386                 submit_cells('AddItem', _("Add Item"), "colspan=2");
387         }
388
389         end_row();
390 }
391
392 //--------------------------------------------------------------------------------
393
394 function display_delivery_details(&$order)
395 {
396         global $table_style2;
397         if ($order->trans_type==10) 
398         {
399                 $title = _("Delivery Details");
400                 $delname = _("Due Date").':';
401         } 
402         elseif ($order->trans_type==13) 
403         {
404                 $title = _("Invoice Delivery Details");
405                 $delname = _("Invoice before").':';
406         } 
407         else 
408         {
409                 $title = _("Order Delivery Details");
410                 $delname = _("Required Delivery Date").':';
411         }
412         display_heading($title);
413     echo "<br>";
414     start_table("$table_style2 width=90%");
415     echo "<tr valign=top><td>"; // outer table
416
417     echo "<table>";
418
419 //      echo $order->Location, $_POST['Location'];
420
421     locations_list_row(_("Deliver from Location:"), 'Location', $_POST['Location']);
422
423         date_row($delname, 'delivery_date', $order->due_date, 0, 0, 0);
424
425     text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 40, 40);
426
427     textarea_row(_("Address:"), 'delivery_address', $order->delivery_address, 35, 5);
428     text_row(_("Contact Phone Number:"), 'phone', $order->phone, 25, 25);
429
430     echo "</table>";
431
432     echo "</td><td>"; // outer table
433
434     echo "<table>";
435
436     text_row(_("Customer Reference:"), 'cust_ref', $order->cust_ref, 25, 25);
437     textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
438
439     text_row(_("Shipping Charge:"), 'freight_cost', $order->freight_cost, 10, 10);
440
441     shippers_list_row(_("Shipping Company:"), 'ship_via', $order->ship_via);
442
443     echo "</table>";
444
445     echo "</td></tr>";
446     end_table(1); // outer table
447 }
448
449 ?>