Fixed many issues in output HTML code according to HTML 4.01 Transitional format.
[fa-stable.git] / sales / customer_delivery.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //-----------------------------------------------------------------------------
13 //
14 //      Entry/Modify Delivery Note against Sales Order
15 //
16 $page_security = 'SA_SALESDELIVERY';
17 $path_to_root = "..";
18
19 include_once($path_to_root . "/sales/includes/cart_class.inc");
20 include_once($path_to_root . "/includes/session.inc");
21 include_once($path_to_root . "/includes/data_checks.inc");
22 include_once($path_to_root . "/includes/manufacturing.inc");
23 include_once($path_to_root . "/sales/includes/sales_db.inc");
24 include_once($path_to_root . "/sales/includes/sales_ui.inc");
25 include_once($path_to_root . "/reporting/includes/reporting.inc");
26 include_once($path_to_root . "/taxes/tax_calc.inc");
27
28 $js = "";
29 if ($use_popup_windows) {
30         $js .= get_js_open_window(900, 500);
31 }
32 if ($use_date_picker) {
33         $js .= get_js_date_picker();
34 }
35
36 if (isset($_GET['ModifyDelivery'])) {
37         $_SESSION['page_title'] = sprintf(_("Modifying Delivery Note # %d."), $_GET['ModifyDelivery']);
38         $help_context = "Modifying Delivery Note";
39         processing_start();
40 } elseif (isset($_GET['OrderNumber'])) {
41         $_SESSION['page_title'] = _($help_context = "Deliver Items for a Sales Order");
42         processing_start();
43 }
44
45 page($_SESSION['page_title'], false, false, "", $js);
46
47 if (isset($_GET['AddedID'])) {
48         $dispatch_no = $_GET['AddedID'];
49
50         display_notification_centered(sprintf(_("Delivery # %d has been entered."),$dispatch_no));
51
52         display_note(get_customer_trans_view_str(ST_CUSTDELIVERY, $dispatch_no, _("&View This Delivery")), 0, 1);
53
54         display_note(print_document_link($dispatch_no, _("&Print Delivery Note"), true, ST_CUSTDELIVERY));
55         display_note(print_document_link($dispatch_no, _("&Email Delivery Note"), true, ST_CUSTDELIVERY, false, "printlink", "", 1), 1, 1);
56         display_note(print_document_link($dispatch_no, _("P&rint as Packing Slip"), true, ST_CUSTDELIVERY, false, "printlink", "", 0, 1));
57         display_note(print_document_link($dispatch_no, _("E&mail as Packing Slip"), true, ST_CUSTDELIVERY, false, "printlink", "", 1, 1), 1);
58
59         display_note(get_gl_view_str(13, $dispatch_no, _("View the GL Journal Entries for this Dispatch")),1);
60
61         hyperlink_params("$path_to_root/sales/customer_invoice.php", _("Invoice This Delivery"), "DeliveryNumber=$dispatch_no");
62
63         hyperlink_params("$path_to_root/sales/inquiry/sales_orders_view.php", _("Select Another Order For Dispatch"), "OutstandingOnly=1");
64
65         display_footer_exit();
66
67 } elseif (isset($_GET['UpdatedID'])) {
68
69         $delivery_no = $_GET['UpdatedID'];
70
71         display_notification_centered(sprintf(_('Delivery Note # %d has been updated.'),$delivery_no));
72
73         display_note(get_trans_view_str(ST_CUSTDELIVERY, $delivery_no, _("View this delivery")), 0, 1);
74
75         display_note(print_document_link($delivery_no, _("&Print Delivery Note"), true, ST_CUSTDELIVERY));
76         display_note(print_document_link($delivery_no, _("&Email Delivery Note"), true, ST_CUSTDELIVERY, false, "printlink", "", 1), 1, 1);
77         display_note(print_document_link($delivery_no, _("P&rint as Packing Slip"), true, ST_CUSTDELIVERY, false, "printlink", "", 0, 1));
78         display_note(print_document_link($delivery_no, _("E&mail as Packing Slip"), true, ST_CUSTDELIVERY, false, "printlink", "", 1, 1), 1);
79
80         hyperlink_params($path_to_root . "/sales/customer_invoice.php", _("Confirm Delivery and Invoice"), "DeliveryNumber=$delivery_no");
81
82         hyperlink_params($path_to_root . "/sales/inquiry/sales_deliveries_view.php", _("Select A Different Delivery"), "OutstandingOnly=1");
83
84         display_footer_exit();
85 }
86 //-----------------------------------------------------------------------------
87
88 if (isset($_GET['OrderNumber']) && $_GET['OrderNumber'] > 0) {
89
90         $ord = new Cart(ST_SALESORDER, $_GET['OrderNumber'], true);
91
92         if ($ord->count_items() == 0) {
93                 hyperlink_params($path_to_root . "/sales/inquiry/sales_orders_view.php",
94                         _("Select a different sales order to delivery"), "OutstandingOnly=1");
95                 die ("<br><b>" . _("This order has no items. There is nothing to delivery.") . "</b>");
96         }
97
98         // Adjust Shipping Charge based upon previous deliveries TAM
99         adjust_shipping_charge($ord, $_GET['OrderNumber']);
100  
101         $_SESSION['Items'] = $ord;
102         copy_from_cart();
103
104 } elseif (isset($_GET['ModifyDelivery']) && $_GET['ModifyDelivery'] > 0) {
105
106         $_SESSION['Items'] = new Cart(ST_CUSTDELIVERY, $_GET['ModifyDelivery']);
107
108         if ($_SESSION['Items']->count_items() == 0) {
109                 hyperlink_params($path_to_root . "/sales/inquiry/customer_inquiry.php",
110                         _("Select a different delivery"), "OutstandingOnly=1");
111                 echo "<br><center><b>" . _("This delivery has all items invoiced. There is nothing to modify.") .
112                         "</center></b>";
113                 display_footer_exit();
114         }
115
116         copy_from_cart();
117         
118 } elseif ( !processing_active() ) {
119         /* This page can only be called with an order number for invoicing*/
120
121         display_error(_("This page can only be opened if an order or delivery note has been selected. Please select it first."));
122
123         hyperlink_params("$path_to_root/sales/inquiry/sales_orders_view.php", _("Select a Sales Order to Delivery"), "OutstandingOnly=1");
124
125         end_page();
126         exit;
127
128 } else {
129         check_edit_conflicts();
130
131         if (!check_quantities()) {
132                 display_error(_("Selected quantity cannot be less than quantity invoiced nor more than quantity not dispatched on sales order."));
133
134         } elseif(!check_num('ChargeFreightCost', 0)) {
135                 display_error(_("Freight cost cannot be less than zero"));
136                 set_focus('ChargeFreightCost');
137         }
138 }
139
140 //-----------------------------------------------------------------------------
141
142 function check_data()
143 {
144         global $Refs, $SysPrefs;
145
146         if (!isset($_POST['DispatchDate']) || !is_date($_POST['DispatchDate'])) {
147                 display_error(_("The entered date of delivery is invalid."));
148                 set_focus('DispatchDate');
149                 return false;
150         }
151
152         if (!is_date_in_fiscalyear($_POST['DispatchDate'])) {
153                 display_error(_("The entered date of delivery is not in fiscal year."));
154                 set_focus('DispatchDate');
155                 return false;
156         }
157
158         if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
159                 display_error(_("The entered dead-line for invoice is invalid."));
160                 set_focus('due_date');
161                 return false;
162         }
163
164         if ($_SESSION['Items']->trans_no==0) {
165                 if (!$Refs->is_valid($_POST['ref'])) {
166                         display_error(_("You must enter a reference."));
167                         set_focus('ref');
168                         return false;
169                 }
170         }
171         if ($_POST['ChargeFreightCost'] == "") {
172                 $_POST['ChargeFreightCost'] = price_format(0);
173         }
174
175         if (!check_num('ChargeFreightCost',0)) {
176                 display_error(_("The entered shipping value is not numeric."));
177                 set_focus('ChargeFreightCost');
178                 return false;
179         }
180
181         if ($_SESSION['Items']->has_items_dispatch() == 0 && input_num('ChargeFreightCost') == 0) {
182                 display_error(_("There are no item quantities on this delivery note."));
183                 return false;
184         }
185
186         if (!check_quantities()) {
187                 return false;
188         }
189
190         copy_to_cart();
191
192         if (!$SysPrefs->allow_negative_stock() && ($low_stock = $_SESSION['Items']->check_qoh()))
193         {
194                 display_error(_("This document cannot be processed because there is insufficient quantity for: ").implode(',', $low_stock));
195                 return false;
196         }
197
198         return true;
199 }
200 //------------------------------------------------------------------------------
201 function copy_to_cart()
202 {
203         $cart = &$_SESSION['Items'];
204         $cart->ship_via = $_POST['ship_via'];
205         $cart->freight_cost = input_num('ChargeFreightCost');
206         $cart->document_date = $_POST['DispatchDate'];
207         $cart->due_date =  $_POST['due_date'];
208         $cart->Location = $_POST['Location'];
209         $cart->Comments = $_POST['Comments'];
210         $cart->dimension_id = $_POST['dimension_id'];
211         $cart->dimension2_id = $_POST['dimension2_id'];
212         if ($cart->trans_no == 0)
213                 $cart->reference = $_POST['ref'];
214
215 }
216 //------------------------------------------------------------------------------
217
218 function copy_from_cart()
219 {
220         $cart = &$_SESSION['Items'];
221         $_POST['ship_via'] = $cart->ship_via;
222         $_POST['ChargeFreightCost'] = price_format($cart->freight_cost);
223         $_POST['DispatchDate'] = $cart->document_date;
224         $_POST['due_date'] = $cart->due_date;
225         $_POST['Location'] = $cart->Location;
226         $_POST['Comments'] = $cart->Comments;
227         $_POST['dimension_id'] = $cart->dimension_id;
228         $_POST['dimension2_id'] = $cart->dimension2_id;
229         $_POST['cart_id'] = $cart->cart_id;
230         $_POST['ref'] = $cart->reference;
231 }
232 //------------------------------------------------------------------------------
233
234 function check_quantities()
235 {
236         $ok =1;
237         // Update cart delivery quantities/descriptions
238         foreach ($_SESSION['Items']->line_items as $line=>$itm) {
239                 if (isset($_POST['Line'.$line])) {
240                 if($_SESSION['Items']->trans_no) {
241                         $min = $itm->qty_done;
242                         $max = $itm->quantity;
243                 } else {
244                         $min = 0;
245                         $max = $itm->quantity - $itm->qty_done;
246                 }
247
248                         if (check_num('Line'.$line, $min, $max)) {
249                                 $_SESSION['Items']->line_items[$line]->qty_dispatched =
250                                   input_num('Line'.$line);
251                         } else {
252                                 set_focus('Line'.$line);
253                                 $ok = 0;
254                         }
255
256                 }
257
258                 if (isset($_POST['Line'.$line.'Desc'])) {
259                         $line_desc = $_POST['Line'.$line.'Desc'];
260                         if (strlen($line_desc) > 0) {
261                                 $_SESSION['Items']->line_items[$line]->item_description = $line_desc;
262                         }
263                 }
264         }
265 // ...
266 //      else
267 //        $_SESSION['Items']->freight_cost = input_num('ChargeFreightCost');
268         return $ok;
269 }
270 //------------------------------------------------------------------------------
271
272 if (isset($_POST['process_delivery']) && check_data())
273 {
274         $dn = &$_SESSION['Items'];
275
276         if ($_POST['bo_policy']) {
277                 $bo_policy = 0;
278         } else {
279                 $bo_policy = 1;
280         }
281         $newdelivery = ($dn->trans_no == 0);
282
283         if ($newdelivery)
284                 new_doc_date($dn->document_date);
285
286         $delivery_no = $dn->write($bo_policy);
287
288         if ($delivery_no == -1)
289         {
290                 display_error(_("The entered reference is already in use."));
291                 set_focus('ref');
292         }
293         else
294         {
295                 processing_end();
296                 if ($newdelivery) {
297                         meta_forward($_SERVER['PHP_SELF'], "AddedID=$delivery_no");
298                 } else {
299                         meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$delivery_no");
300                 }
301         }
302 }
303
304 if (isset($_POST['Update']) || isset($_POST['_Location_update']) || isset($_POST['qty']) || isset($_POST['process_delivery'])) {
305         $Ajax->activate('Items');
306 }
307 //------------------------------------------------------------------------------
308 start_form();
309 hidden('cart_id');
310
311 start_table(TABLESTYLE2, "width='80%'", 5);
312 echo "<tr><td>"; // outer table
313
314 start_table(TABLESTYLE, "width='100%'");
315 start_row();
316 label_cells(_("Customer"), $_SESSION['Items']->customer_name, "class='tableheader2'");
317 label_cells(_("Branch"), get_branch_name($_SESSION['Items']->Branch), "class='tableheader2'");
318 label_cells(_("Currency"), $_SESSION['Items']->customer_currency, "class='tableheader2'");
319 end_row();
320 start_row();
321
322 //if (!isset($_POST['ref']))
323 //      $_POST['ref'] = $Refs->get_next(ST_CUSTDELIVERY);
324
325 if ($_SESSION['Items']->trans_no==0) {
326         ref_cells(_("Reference"), 'ref', '', null, "class='tableheader2'");
327 } else {
328         label_cells(_("Reference"), $_SESSION['Items']->reference, "class='tableheader2'");
329 }
330
331 label_cells(_("For Sales Order"), get_customer_trans_view_str(ST_SALESORDER, $_SESSION['Items']->order_no), "class='tableheader2'");
332
333 label_cells(_("Sales Type"), $_SESSION['Items']->sales_type_name, "class='tableheader2'");
334 end_row();
335 start_row();
336
337 if (!isset($_POST['Location'])) {
338         $_POST['Location'] = $_SESSION['Items']->Location;
339 }
340 label_cell(_("Delivery From"), "class='tableheader2'");
341 locations_list_cells(null, 'Location', null, false, true);
342
343 if (!isset($_POST['ship_via'])) {
344         $_POST['ship_via'] = $_SESSION['Items']->ship_via;
345 }
346 label_cell(_("Shipping Company"), "class='tableheader2'");
347 shippers_list_cells(null, 'ship_via', $_POST['ship_via']);
348
349 // set this up here cuz it's used to calc qoh
350 if (!isset($_POST['DispatchDate']) || !is_date($_POST['DispatchDate'])) {
351         $_POST['DispatchDate'] = new_doc_date();
352         if (!is_date_in_fiscalyear($_POST['DispatchDate'])) {
353                 $_POST['DispatchDate'] = end_fiscalyear();
354         }
355 }
356 date_cells(_("Date"), 'DispatchDate', '', $_SESSION['Items']->trans_no==0, 0, 0, 0, "class='tableheader2'");
357 end_row();
358
359 end_table();
360
361 echo "</td><td>";// outer table
362
363 start_table(TABLESTYLE, "width='90%'");
364
365 if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
366         $_POST['due_date'] = get_invoice_duedate($_SESSION['Items']->payment, $_POST['DispatchDate']);
367 }
368 customer_credit_row($_SESSION['Items']->customer_id, $_SESSION['Items']->credit, "class='tableheader2'");
369 // 2010-09-03 Joe Hunt
370 $dim = get_company_pref('use_dimension');
371 if ($dim > 0) {
372         start_row();
373         label_cell(_("Dimension").":", "class='tableheader2'");
374         dimensions_list_cells(null, 'dimension_id', null, true, ' ', false, 1, false);
375         end_row();
376 }               
377 else
378         hidden('dimension_id', 0);
379 if ($dim > 1) {
380         start_row();
381         label_cell(_("Dimension")." 2:", "class='tableheader2'");
382         dimensions_list_cells(null, 'dimension2_id', null, true, ' ', false, 2, false);
383         end_row();
384 }               
385 else
386         hidden('dimension2_id', 0);
387 //---------
388 start_row();
389 date_cells(_("Invoice Dead-line"), 'due_date', '', null, 0, 0, 0, "class='tableheader2'");
390 end_row();
391 end_table();
392
393 echo "</td></tr>";
394 end_table(1); // outer table
395
396 $row = get_customer_to_order($_SESSION['Items']->customer_id);
397 if ($row['dissallow_invoices'] == 1)
398 {
399         display_error(_("The selected customer account is currently on hold. Please contact the credit control personnel to discuss."));
400         end_form();
401         end_page();
402         exit();
403 }       
404 display_heading(_("Delivery Items"));
405 div_start('Items');
406 start_table(TABLESTYLE, "width='80%'");
407
408 $new = $_SESSION['Items']->trans_no==0;
409 $th = array(_("Item Code"), _("Item Description"), 
410         $new ? _("Ordered") : _("Max. delivery"), _("Units"), $new ? _("Delivered") : _("Invoiced"),
411         _("This Delivery"), _("Price"), _("Tax Type"), _("Discount"), _("Total"));
412
413 table_header($th);
414 $k = 0;
415 $has_marked = false;
416
417 foreach ($_SESSION['Items']->line_items as $line=>$ln_itm) {
418         if ($ln_itm->quantity==$ln_itm->qty_done) {
419                 continue; //this line is fully delivered
420         }
421         if(isset($_POST['_Location_update']) || isset($_POST['clear_quantity']) || isset($_POST['reset_quantity'])) {
422                 // reset quantity
423                 $ln_itm->qty_dispatched = $ln_itm->quantity-$ln_itm->qty_done;
424         }
425         // if it's a non-stock item (eg. service) don't show qoh
426         $row_classes = null;
427         if (has_stock_holding($ln_itm->mb_flag) && $ln_itm->qty_dispatched) {
428                 // It's a stock : call get_dispatchable_quantity hook  to get which quantity to preset in the
429                 // quantity input box. This allows for example a hook to modify the default quantity to what's dispatchable
430                 // (if there is not enough in hand), check at other location or other order people etc ...
431                 // This hook also returns a 'reason' (css classes) which can be used to theme the row.
432                 //
433                 // FIXME: hook_get_dispatchable definition does not allow qoh checks on transaction level
434                 // (but anyway dispatch is checked again later before transaction is saved)
435
436                 $qty = $ln_itm->qty_dispatched;
437                 if ($check = check_negative_stock($ln_itm->stock_id, $ln_itm->qty_done-$ln_itm->qty_dispatched, $_POST['Location'], $_POST['DispatchDate']))
438                         $qty = $check['qty'];
439
440                 $q_class =  hook_get_dispatchable_quantity($ln_itm, $_POST['Location'], $_POST['DispatchDate'], $qty);
441
442                 // Skip line if needed
443                 if($q_class === 'skip')  continue;
444                 if(is_array($q_class)) {
445                   list($ln_itm->qty_dispatched, $row_classes) = $q_class;
446                         $has_marked = true;
447                 }
448         }
449
450         alt_table_row_color($k, $row_classes);
451         view_stock_status_cell($ln_itm->stock_id);
452
453         if ($ln_itm->descr_editable)
454                 text_cells(null, 'Line'.$line.'Desc', $ln_itm->item_description, 30, 50);
455         else
456                 label_cell($ln_itm->item_description);
457
458         $dec = get_qty_dec($ln_itm->stock_id);
459         qty_cell($ln_itm->quantity, false, $dec);
460         label_cell($ln_itm->units);
461         qty_cell($ln_itm->qty_done, false, $dec);
462
463         if(isset($_POST['clear_quantity'])) {
464                 $ln_itm->qty_dispatched = 0;
465         }
466         $_POST['Line'.$line]=$ln_itm->qty_dispatched; /// clear post so value displayed in the fiel is the 'new' quantity
467         small_qty_cells(null, 'Line'.$line, qty_format($ln_itm->qty_dispatched, $ln_itm->stock_id, $dec), null, null, $dec);
468
469         $display_discount_percent = percent_format($ln_itm->discount_percent*100) . "%";
470
471         $line_total = ($ln_itm->qty_dispatched * $ln_itm->price * (1 - $ln_itm->discount_percent));
472
473         amount_cell($ln_itm->price);
474         label_cell($ln_itm->tax_type_name);
475         label_cell($display_discount_percent, "nowrap align=right");
476         amount_cell($line_total);
477
478         end_row();
479 }
480
481 $_POST['ChargeFreightCost'] =  get_post('ChargeFreightCost', 
482         price_format($_SESSION['Items']->freight_cost));
483
484 $colspan = 9;
485
486 start_row();
487 label_cell(_("Shipping Cost"), "colspan=$colspan align=right");
488 small_amount_cells(null, 'ChargeFreightCost', $_SESSION['Items']->freight_cost);
489 end_row();
490
491 $inv_items_total = $_SESSION['Items']->get_items_total_dispatch();
492
493 $display_sub_total = price_format($inv_items_total + input_num('ChargeFreightCost'));
494
495 label_row(_("Sub-total"), $display_sub_total, "colspan=$colspan align=right","align=right");
496
497 $taxes = $_SESSION['Items']->get_taxes(input_num('ChargeFreightCost'));
498 $tax_total = display_edit_tax_items($taxes, $colspan, $_SESSION['Items']->tax_included);
499
500 $display_total = price_format(($inv_items_total + input_num('ChargeFreightCost') + $tax_total));
501
502 label_row(_("Amount Total"), $display_total, "colspan=$colspan align=right","align=right");
503
504 end_table(1);
505
506 if ($has_marked) {
507         display_note(_("Marked items have insufficient quantities in stock as on day of delivery."), 0, 1, "class='stockmankofg'");
508 }
509 start_table(TABLESTYLE2);
510
511 policy_list_row(_("Action For Balance"), "bo_policy", null);
512
513 textarea_row(_("Memo"), 'Comments', null, 50, 4);
514
515 end_table(1);
516 div_end();
517 submit_center_first('Update', _("Update"),
518         _('Refresh document page'), true);
519 if(isset($_POST['clear_quantity'])) {
520         submit('reset_quantity', _('Reset quantity'), true, _('Refresh document page'));
521 }
522 else  {
523         submit('clear_quantity', _('Clear quantity'), true, _('Refresh document page'));
524 }
525 submit_center_last('process_delivery', _("Process Dispatch"),
526         _('Check entered data and save document'), 'default');
527
528 end_form();
529
530
531 end_page();
532
533 ?>