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