Huge sales module changes toward delivery and invoicing separation. Includes some...
[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 ($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         // Now check to ensure this account is not on hold */
29         $sql = "SELECT ".TB_PREF."debtors_master.name, ".TB_PREF."debtors_master.address, ".TB_PREF."credit_status.dissallow_invoices,
30                 ".TB_PREF."debtors_master.sales_type AS salestype, ".TB_PREF."sales_types.sales_type, ".TB_PREF."debtors_master.curr_code,
31                 ".TB_PREF."debtors_master.discount
32                 FROM ".TB_PREF."debtors_master, ".TB_PREF."credit_status, ".TB_PREF."sales_types
33                 WHERE ".TB_PREF."debtors_master.sales_type=".TB_PREF."sales_types.id
34                         AND ".TB_PREF."debtors_master.credit_status=".TB_PREF."credit_status.id
35                         AND ".TB_PREF."debtors_master.debtor_no = '" . $customer_id . "'";
36
37         $result =db_query($sql,"Customer Record Retreive");
38
39         $myrow = db_fetch($result);
40
41         $order->customer_id = $customer_id;
42         $order->Branch = $branch_id;
43         $order->customer_name = $myrow['name'];
44
45         if ($myrow['dissallow_invoices'] == 1)
46                 $ret_error = _("The selected customer account is currently on hold. Please contact the credit control personnel to discuss.");
47
48         if (!isset($_POST['branch_id']) || $_POST['branch_id'] == "") 
49         {
50                 $ret_error = _("The selected customer does not have any branches. Please create at least one branch.");
51                 unset($_POST['branch_id']);
52                 $order->Branch = "";
53         }
54
55         # the sales type determines the price list to be used by default
56         $order->default_sales_type = $myrow['salestype'];
57         $order->sales_type_name = $myrow['sales_type'];
58         $order->customer_currency = $myrow['curr_code'];
59         $order->default_discount = $myrow['discount'];
60         $deliver = $myrow['address']; // in case no deliveraddress.
61
62         if ($order->Branch != "")
63         {
64         # the branch was also selected from the customer selection so default the delivery details from the customer branches table cust_branch. The order process will ask for branch details later anyway
65                 $sql = "SELECT ".TB_PREF."cust_branch.br_name, ".TB_PREF."cust_branch.br_post_address, phone, email,
66                         default_location, default_ship_via,
67                         ".TB_PREF."tax_groups.name AS tax_group_name, ".TB_PREF."tax_groups.id AS tax_group_id
68                         FROM ".TB_PREF."cust_branch, ".TB_PREF."tax_groups
69                                 WHERE ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
70                                         AND ".TB_PREF."cust_branch.branch_code='" . $order->Branch . "'
71                                         AND ".TB_PREF."cust_branch.debtor_no = '" . $order->customer_id . "'";
72
73         $result = db_query($sql,"Customer Branch Record Retreive");
74
75         if (db_num_rows($result) == 0)
76         {
77                         return _("The selected customer and branch are not valid, or the customer does not have any branches.");
78         }
79
80         $myrow = db_fetch($result);
81         $order->deliver_to = $myrow["br_name"];
82         $order->delivery_address = $myrow["br_post_address"];
83         if (strlen($order->delivery_address) <= 1)
84                 $order->delivery_address = $deliver;
85         $order->phone = $myrow["phone"];
86         $order->email = $myrow["email"];
87         $order->Location = $myrow["default_location"];
88         $order->ship_via = $myrow["default_ship_via"];
89                 $order->tax_group_name = $myrow["tax_group_name"];
90                 $order->tax_group_id = $myrow["tax_group_id"];
91         }
92
93         return $ret_error;
94 }
95
96 //---------------------------------------------------------------------------------
97
98 function display_order_summary($title, &$order, $editable_items=false)
99 {
100         global $table_style, $path_to_root;
101
102         display_heading($title);
103         start_table("$table_style colspan=7 width=90%");
104         $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
105                 _("Unit"), _("Price"), _("Discount %"), _("Total"), "");
106
107         if(count($order->line_items)) $th[]= '';
108
109         table_header($th);      
110
111         $total = 0;
112         $k = 0;  //row colour counter
113
114         foreach ($order->line_items as $line_no=>$stock_item) 
115         {
116
117                 $line_total = $stock_item->quantity * $stock_item->price * (1 - $stock_item->discount_percent);
118  
119                 if (!isset($_GET['Edit']))
120                         $id = -1;
121                 else
122                         $id = $_GET['Edit'];
123                 if (!$editable_items || $id != $line_no)
124                 {
125                 alt_table_row_color($k);
126
127                         view_stock_status_cell($stock_item->stock_id);
128                 
129                 label_cell($stock_item->item_description);
130                 qty_cell($stock_item->quantity);
131                 label_cell($stock_item->units);
132                 amount_cell($stock_item->price);
133
134                 amount_cell($stock_item->discount_percent * 100);
135                 amount_cell($line_total);
136
137                 if ($editable_items) 
138                 {
139                         edit_link_cell(SID . "Edit=$line_no");
140                         delete_link_cell(SID . "Delete=$line_no");
141                 }
142                 end_row();
143                 } 
144                 else 
145                 {
146                         sales_order_item_controls($order, $line_no);
147                 }
148
149                 $total += $line_total;
150         }
151
152         if (!isset($_GET['Edit']) && $editable_items)
153                 sales_order_item_controls($order);
154
155         $display_total = number_format2($total,user_price_dec());
156         label_row(_("Total Excluding Tax/Shipping"), $display_total, "colspan=6 align=right",
157                 "nowrap align=right");
158
159         end_table();
160 }
161
162 // ------------------------------------------------------------------------------
163
164 function display_order_header(&$order, $editable, $date_text, $display_tax_group=false)
165 {
166         global $table_style;
167         start_table("width=80% $table_style");
168         echo "<tr><td valign=top>"; // outer table
169         echo "<table>";
170
171         $customer_error = "";
172
173         if (isset($order) && !$editable)
174         {
175                 // can't change the customer/branch if items already received on this order
176                 echo $order->customer_name . " - " . $order->deliver_to;
177         } 
178         else 
179         {
180
181         if (!isset($_POST['customer_id']) && (get_global_customer() != reserved_words::get_all()))
182                 $_POST['customer_id'] = get_global_customer();
183
184                 customer_list_row(_("Customer:"), 'customer_id', null, false, true);
185
186                 if (!isset($_POST['delivery_date']))
187                 {
188                         if ($order->trans_type=='invoice')
189                                 $_POST['delivery_date'] = $_SESSION['Items']->delivery_date = 
190                                         get_invoice_duedate($_POST['customer_id'], $_POST['OrderDate']);
191                         else    
192                                 $_POST['delivery_date'] = $_SESSION['Items']->delivery_date = 
193                                         add_days($_POST['OrderDate'], 10);
194                 }               
195                 if ($order->customer_id != $_POST['customer_id']) 
196                 {
197                         // customer has changed
198
199                         // delete all the order items - drastic but necessary because of
200                         // change of currency, sales type, etc
201                 $order->clear_items();
202
203                         // clear the branch selection
204                         unset($_POST['branch_id']);
205                 }
206
207                 customer_branches_list_row(_("Branch:"), $_POST['customer_id'], 'branch_id', null, false, true, true);
208
209                 if (!isset($_POST['branch_id']))
210                         $_POST['branch_id'] = "";
211                 //set_global_customer($_POST['customer_id']);
212                 if (($order->customer_id != $_POST['customer_id']) || 
213                         ($order->Branch != $_POST['branch_id']))
214                         $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
215
216                 set_global_customer($_POST['customer_id']);
217         }
218         echo "</table>";
219
220         echo "</td><td>"; // outer table
221
222         if (!is_company_currency($order->customer_currency))
223         {
224                 echo "<table height='5'>";
225                 label_row(_("Customer Currency:"), $order->customer_currency);
226                 exchange_rate_display($order->customer_currency, get_company_currency(),        
227                         ($editable?$_POST['OrderDate']:$order->orig_order_date), $editable);
228                 echo "</table>";
229                 echo "</td><td>"; // outer table
230         }
231
232         echo "<table height='5'>";
233         label_row(_("Sales Type:"), $order->sales_type_name);
234         label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
235         echo "</table>";
236
237         echo "</td><td>"; // outer table
238
239         echo "<table height='5'>";
240
241         if ($editable) 
242         {
243         if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
244                 $_POST['OrderDate'] = $order->orig_order_date;
245
246                 date_row($date_text, 'OrderDate');
247         }
248         else 
249         {
250                 label_row($date_text, $order->orig_order_date);
251                 hidden('OrderDate', $order->orig_order_date);
252         }
253
254         if ($display_tax_group) 
255         {
256                 if ($editable) 
257                 {
258             if (!isset($_POST['tax_group_id']))
259                 $_POST['tax_group_id'] = $_SESSION['Items']->tax_group_id;
260             tax_groups_list_row(_("Tax Group:"), 'tax_group_id', $_POST['tax_group_id'], true);
261                 } 
262                 else 
263                 {
264                         label_row(_("Tax Group:"), $order->tax_group_name);
265                         hidden('tax_group_id', $_SESSION['Items']->tax_group_id);
266                 }
267         }
268         echo "</table>";
269
270         echo "</td></tr>";
271         
272         end_table(1); // outer table
273
274         return $customer_error;
275 }
276
277 //--------------------------------------------------------------------------------
278
279 function sales_order_item_controls(&$order, $line_no=-1)
280 {
281         start_row();
282         if (isset($_GET['Edit']) && $line_no != -1)
283         {
284                 if (!isset($_POST['stock_id']))
285                         $_POST['stock_id'] = $order->line_items[$_GET['Edit']]->stock_id;
286                 if (!isset($_POST['qty']) || ($_POST['qty'] == ""))
287                         $_POST['qty'] = $order->line_items[$_GET['Edit']]->quantity;
288                 if (!isset($_POST['price']) || ($_POST['price'] == ""))
289                         $_POST['price'] = $order->line_items[$_GET['Edit']]->price;
290                 if (!isset($_POST['Disc']) || ($_POST['Disc'] == ""))
291                         $_POST['Disc'] = ($order->line_items[$_GET['Edit']]->discount_percent)*100;
292
293                 $_POST['units'] = $order->line_items[$_GET['Edit']]->units;
294                 hidden('stock_id', $_POST['stock_id']);
295                 label_cell($_POST['stock_id']);
296                 label_cell($order->line_items[$line_no]->item_description);
297         } 
298         else 
299         {
300                 global $no_item_list; 
301                 if ($no_item_list)
302                 {
303                         echo "<td colspan=2>\n";
304                         stock_items_list('stock_id', null, false, true);
305                         echo "</td>\n";
306                 }
307                 else
308                 {
309                   text_cells(null, "StockID2", "", 12, 10, "", "", "onkeyup='recalcAccounts();' onKeyDown='if(event.keyCode==13) event.keyCode=9;' onblur='return setAccount(0, true);'");
310                         stock_items_list_cells(null, 'stock_id', null, false, false, "onchange='return setAccount(1, true)'");
311                 }
312                 $item_info = get_item_edit_info($_POST['stock_id']);
313                 $_POST['units'] = $item_info["units"];
314
315                 $_POST['qty'] = 1;
316                 $_POST['price'] = get_price ($_POST['stock_id'], $order->customer_id);
317                 // default to the customer's discount %
318                 $_POST['Disc'] = $order->default_discount * 100;
319         }
320
321         text_cells(null, 'qty', $_POST['qty'], 12, 15);
322
323         label_cell($_POST['units']);
324         amount_cells(null, 'price');
325         text_cells(null, 'Disc', $_POST['Disc'], 7, 5);
326
327         $line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
328         amount_cell($line_total);
329
330         if (isset($_GET['Edit'])) 
331         {
332         submit_cells('UpdateItem', _("Update"));
333         submit_cells('CancelItemChanges', _("Cancel"));
334                 hidden('LineNo', $line_no);
335         } 
336         else 
337         {
338                 submit_cells('AddItem', _("Add Item"), "colspan=2");
339         }
340
341         end_row();
342 }
343
344 //--------------------------------------------------------------------------------
345
346 function display_delivery_details(&$order)
347 {
348         global $table_style2;
349
350         if ($order->trans_type=='invoice') 
351         {
352                 $title = _("Invoice Delivery Details");
353                 $delname = _("Due Date");
354         } 
355         else 
356         {
357                 $title = _("Order Delivery Details");
358                 $delname = _("Required Delivery Date:");
359         }
360         display_heading($title);
361     echo "<br>";
362     start_table("$table_style2 width=90%");
363     echo "<tr valign=top><td>"; // outer table
364
365     echo "<table>";
366     locations_list_row(_("Deliver from Location:"), 'Location', $order->Location);
367
368         date_row($delname, 'delivery_date', $order->delivery_date, 0, 0, 0);
369
370     text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 40, 40);
371
372     textarea_row(_("Address:"), 'delivery_address', $order->delivery_address, 35, 5);
373     text_row(_("Contact Phone Number:"), 'phone', $order->phone, 25, 25);
374
375     echo "</table>";
376
377     echo "</td><td>"; // outer table
378
379     echo "<table>";
380
381     text_row(_("Customer Reference:"), 'cust_ref', $order->cust_ref, 25, 25);
382     textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
383
384     text_row(_("Shipping Charge:"), 'freight_cost', $order->freight_cost, 10, 10);
385
386     shippers_list_row(_("Shipping Company:"), 'ship_via', $order->ship_via);
387         if ($_SESSION['Items']->trans_type=='delivery') 
388                 textarea_row(_("Memo"), 'InvoiceText', null, 31, 3);
389     echo "</table>";
390
391     echo "</td></tr>";
392     end_table(1); // outer table
393 }
394
395 ?>