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