Automatic first field focus on page start, preserved focus sequence
[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         $id = find_submit('Edit');
101         
102         foreach ($order->line_items as $line_no=>$stock_item)
103         {
104
105                 $line_total = round($stock_item->qty_dispatched * $stock_item->price * (1 - $stock_item->discount_percent),
106                    user_price_dec());
107
108
109                 if (!$editable_items || $id != $line_no)
110                 {
111                         alt_table_row_color($k);
112
113                         view_stock_status_cell($stock_item->stock_id);
114
115                         label_cell($stock_item->item_description, "nowrap" );
116                         qty_cell($stock_item->qty_dispatched);
117
118                         if ($order->trans_no!=0)
119                         amount_cell($stock_item->qty_done);
120
121                         label_cell($stock_item->units);
122                         amount_cell($stock_item->price);
123
124                         percent_cell($stock_item->discount_percent * 100);
125                         amount_cell($line_total);
126
127                         if ($editable_items)
128                         {
129                                 edit_button_cell("Edit$line_no", _("Edit"));
130                                 edit_button_cell("Delete$line_no", _("Delete"));
131                         }
132                         end_row();
133                 }
134                 else
135                 {
136                         sales_order_item_controls($order, $k,  $line_no);
137                 }
138
139                 $total += $line_total;
140         }
141
142         if ($id==-1 && $editable_items)
143                 sales_order_item_controls($order, $k);
144
145         $display_total = price_format($total);
146         label_row(_("Total Excluding Shipping"), $display_total, "colspan=6 align=right",
147                 "nowrap align=right", 2);
148
149         end_table();
150 }
151
152 // ------------------------------------------------------------------------------
153
154 function display_order_header(&$order, $editable, $date_text, $display_tax_group=false)
155 {
156         global $table_style;
157         start_table("width=80% $table_style");
158         echo "<tr><td valign=top>"; // outer table
159         echo "<table>";
160
161         $customer_error = "";
162                 $change_prices = 0;
163
164         if (isset($order) && !$editable)
165         {
166                 // can't change the customer/branch if items already received on this order
167                 echo $order->customer_name . " - " . $order->deliver_to;
168                 hidden('customer_id', $order->customer_id);
169                 hidden('branch_id', $order->Branch);
170                 hidden('sales_type', $order->sales_type);
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                         // clear the branch selection
193                         unset($_POST['branch_id']);
194                 }
195                 if (!isset($_POST['branch_id']))
196                         $_POST['branch_id'] = "";
197
198                 customer_branches_list_row(_("Branch:"), $_POST['customer_id'], 'branch_id', null, false, true, true);
199
200                 if (!isset($_POST['branch_id']) || $_POST['branch_id'] == "")
201                 {
202                         $customer_error = _("The selected customer does not have any branches. Please create at least one branch.");
203                         unset($_POST['branch_id']);
204                         $order->Branch = 0;
205                 } else
206                 {
207                         //set_global_customer($_POST['customer_id']);
208                         if (($order->customer_id != $_POST['customer_id']) ||
209                                 ($order->Branch != $_POST['branch_id'])) {
210
211                                 $old_order = (PHP_VERSION<5) ? $order : clone( $order );
212                                 $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
213                                 $_POST['Location'] = $order->Location;
214                                 $_POST['deliver_to'] = $order->deliver_to;
215                                 $_POST['delivery_address'] = $order->delivery_address;
216                                 $_POST['phone'] = $order->phone;
217                                 $_POST['sales_type'] = $order->sales_type;
218
219                                 // change prices if necessary
220                                 // what about discount in template case?
221                                 if ( $old_order->customer_currency!=$order->customer_currency
222                                         || $old_order->sales_type!=$order->sales_type
223                                 //  || $old_order->default_discount!=$order->default_discount
224                                         ) {
225                                         $change_prices = 1;
226                                 }
227
228                         }
229                 } // changed branch
230                 set_global_customer($_POST['customer_id']);
231         }
232
233         if ($_SESSION['Items']->trans_type!=30) {
234                 ref_cells(_("Reference").':', 'ref', null, '');
235         }
236
237         echo "</table>";
238
239         echo "</td><td>"; // outer table
240
241         if (!is_company_currency($order->customer_currency))
242         {
243                 echo "<table height='5'>";
244                 label_row(_("Customer Currency:"), $order->customer_currency);
245                 exchange_rate_display($order->customer_currency, get_company_currency(),
246                         ($editable ? $_POST['OrderDate'] : $order->document_date), $editable);
247                 echo "</table>";
248                 echo "</td><td>"; // outer table
249         }
250
251         if( $order->sales_type != $_POST['sales_type']) {
252                 $myrow = get_sales_type($_POST['sales_type']);
253                 $order->set_sales_type($myrow['id'], $myrow['sales_type'],
254                 $myrow['tax_included']);
255                 $change_prices = 1;
256         }
257
258                 if($change_prices!=0)
259                 {
260                 foreach ($order->line_items as $line_no=>$item) {
261                         $line = &$order->line_items[$line_no];
262                         $line->price = get_price($line->stock_id, $order->customer_currency,
263                                 $order->sales_type);
264                 //              $line->discount_percent = $order->default_discount;
265                 }
266         }
267
268         echo "<table height='5'>";
269         if($editable)
270                 sales_types_list_row(_("Price List"), 'sales_type', $_POST['sales_type'], true);
271         else {
272                 label_row(_("Price List:"), $order->sales_type_name);
273         }
274         label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
275         echo "</table>";
276
277         echo "</td><td>"; // outer table
278
279         echo "<table height='5'>";
280
281         if ($editable)
282         {
283                 if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
284                         $_POST['OrderDate'] = $order->document_date;
285
286                 date_row($date_text, 'OrderDate');
287         }
288         else
289         {
290                 label_row($date_text, $order->document_date);
291                 hidden('OrderDate', $order->document_date);
292         }
293
294         if ($display_tax_group)
295         {
296             label_row(_("Tax Group:"), $order->tax_group_name);
297             hidden('tax_group_id', $_SESSION['Items']->tax_group_id);
298         }
299         echo "</table>";
300
301         echo "</td></tr>";
302
303         end_table(1); // outer table
304
305         return $customer_error;
306 }
307
308 //--------------------------------------------------------------------------------
309
310 function sales_order_item_controls(&$order, &$rowcounter, $line_no=-1)
311 {
312         alt_table_row_color($rowcounter);
313
314         $id = find_submit('Edit');
315         if ($line_no!=-1 && $line_no == $id)
316         {
317                 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
318                 $_POST['qty'] = qty_format($order->line_items[$id]->qty_dispatched);
319                 $_POST['price'] = price_format($order->line_items[$id]->price);
320                 $_POST['Disc'] = percent_format($order->line_items[$id]->discount_percent*100);
321                 $_POST['units'] = $order->line_items[$id]->units;
322                 hidden('stock_id', $_POST['stock_id']);
323                 label_cell($_POST['stock_id']);
324                 label_cell($order->line_items[$line_no]->item_description, "nowrap");
325         }
326         else
327         {
328                 global $no_item_list;
329                 if ($no_item_list)
330                 {
331                         echo "<td colspan=2>\n";
332                         stock_items_list('stock_id', null, false, true);
333                         echo "</td>\n";
334                 }
335                 else
336                 {
337                 text_cells(null, "StockID2", '', 12, 10, "", "", "class='combo' rel='stock_id'"/* onblur='this.form.submit();'"*/);
338                 stock_items_list_cells(null, 'stock_id', null, false, false, "class='combo' rel='StockID2'");
339                 }
340                 $item_info = get_item_edit_info($_POST['stock_id']);
341                 $_POST['units'] = $item_info["units"];
342
343                 $_POST['qty'] = qty_format(1);
344                 $_POST['price'] = price_format(get_price ($_POST['stock_id'],
345                         $order->customer_currency, $order->sales_type));
346                 // default to the customer's discount %
347                 $_POST['Disc'] = percent_format($order->default_discount * 100);
348         }
349
350         qty_cells(null, 'qty', qty_format($_POST['qty']));
351
352         if ($order->trans_no!=0) {
353                 amount_cell($line_no==-1 ? 0 :$order->line_items[$line_no]->qty_done);
354         }
355
356         label_cell($_POST['units']);
357         amount_cells(null, 'price');
358         small_amount_cells(null, 'Disc', percent_format($_POST['Disc']), null, null, user_percent_dec());
359
360         $line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
361         amount_cell($line_total);
362
363         if ($id!=-1)
364         {
365                 edit_button_cell('UpdateItem', _("Update"));
366                 edit_button_cell('CancelItemChanges', _("Cancel"));
367                 hidden('LineNo', $line_no);
368                 set_focus('qty');
369         }
370         else
371         {
372                 submit_cells('AddItem', _("Add Item"), "colspan=2");
373         }
374
375         end_row();
376 }
377
378 //--------------------------------------------------------------------------------
379
380 function display_delivery_details(&$order)
381 {
382         global $table_style2;
383         if ($order->trans_type==10)
384         {
385                 $title = _("Delivery Details");
386                 $delname = _("Due Date").':';
387         }
388         elseif ($order->trans_type==13)
389         {
390                 $title = _("Invoice Delivery Details");
391                 $delname = _("Invoice before").':';
392         }
393         else
394         {
395                 $title = _("Order Delivery Details");
396                 $delname = _("Required Delivery Date").':';
397         }
398         display_heading($title);
399         echo "<br>";
400         start_table("$table_style2 width=90%");
401         echo "<tr valign=top><td>"; // outer table
402
403         echo "<table>";
404
405 //      echo $order->Location, $_POST['Location'];
406
407         locations_list_row(_("Deliver from Location:"), 'Location', $_POST['Location']);
408
409         date_row($delname, 'delivery_date', $order->due_date, 0, 0, 0);
410
411         text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 40, 40);
412
413         textarea_row(_("Address:"), 'delivery_address', $order->delivery_address, 35, 5);
414         text_row(_("Contact Phone Number:"), 'phone', $order->phone, 25, 25);
415
416         echo "</table>";
417
418         echo "</td><td>"; // outer table
419
420         echo "<table>";
421
422         text_row(_("Customer Reference:"), 'cust_ref', $order->cust_ref, 25, 25);
423         textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
424
425         small_amount_row(_("Shipping Charge:"), 'freight_cost', 
426             price_format($_POST['freight_cost']));
427
428         shippers_list_row(_("Shipping Company:"), 'ship_via', $order->ship_via);
429
430         echo "</table>";
431
432         echo "</td></tr>";
433         end_table(1); // outer table
434 }
435
436 ?>