Fixed transaction date check messages.
[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         /*read in all the selected order into the Items cart  */
93
94         if ($ord->count_items() == 0) {
95                 hyperlink_params($path_to_root . "/sales/inquiry/sales_orders_view.php",
96                         _("Select a different sales order to delivery"), "OutstandingOnly=1");
97                 die ("<br><b>" . _("This order has no items. There is nothing to delivery.") . "</b>");
98         }
99
100         $ord->trans_type = ST_CUSTDELIVERY;
101         $ord->src_docs = $ord->trans_no;
102         $ord->order_no = key($ord->trans_no);
103         $ord->trans_no = 0;
104         $ord->reference = $Refs->get_next(ST_CUSTDELIVERY);
105         $ord->document_date = new_doc_date();
106         $cust = get_customer($ord->customer_id);
107         // 2010-09-03 Joe Hunt
108         $ord->dimension_id = $cust['dimension_id'];
109         $ord->dimension2_id = $cust['dimension2_id'];
110         $_SESSION['Items'] = $ord;
111         copy_from_cart();
112
113 } elseif (isset($_GET['ModifyDelivery']) && $_GET['ModifyDelivery'] > 0) {
114
115         check_is_closed(ST_CUSTDELIVERY, $_GET['ModifyDelivery']);
116         $_SESSION['Items'] = new Cart(ST_CUSTDELIVERY,$_GET['ModifyDelivery']);
117
118         if ($_SESSION['Items']->count_items() == 0) {
119                 hyperlink_params($path_to_root . "/sales/inquiry/sales_orders_view.php",
120                         _("Select a different delivery"), "OutstandingOnly=1");
121                 echo "<br><center><b>" . _("This delivery has all items invoiced. There is nothing to modify.") .
122                         "</center></b>";
123                 display_footer_exit();
124         }
125
126         copy_from_cart();
127         
128 } elseif ( !processing_active() ) {
129         /* This page can only be called with an order number for invoicing*/
130
131         display_error(_("This page can only be opened if an order or delivery note has been selected. Please select it first."));
132
133         hyperlink_params("$path_to_root/sales/inquiry/sales_orders_view.php", _("Select a Sales Order to Delivery"), "OutstandingOnly=1");
134
135         end_page();
136         exit;
137
138 } else {
139         check_edit_conflicts();
140
141         if (!check_quantities()) {
142                 display_error(_("Selected quantity cannot be less than quantity invoiced nor more than quantity not dispatched on sales order."));
143
144         } elseif(!check_num('ChargeFreightCost', 0)) {
145                 display_error(_("Freight cost cannot be less than zero"));
146                 set_focus('ChargeFreightCost');
147         }
148 }
149
150 //-----------------------------------------------------------------------------
151
152 function check_data()
153 {
154         global $Refs;
155
156         if (!isset($_POST['DispatchDate']) || !is_date($_POST['DispatchDate'])) {
157                 display_error(_("The entered date of delivery is invalid."));
158                 set_focus('DispatchDate');
159                 return false;
160         }
161
162         if (!is_date_in_fiscalyear($_POST['DispatchDate'])) {
163                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
164                 set_focus('DispatchDate');
165                 return false;
166         }
167
168         if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
169                 display_error(_("The entered dead-line for invoice is invalid."));
170                 set_focus('due_date');
171                 return false;
172         }
173
174         if ($_SESSION['Items']->trans_no==0) {
175                 if (!$Refs->is_valid($_POST['ref'])) {
176                         display_error(_("You must enter a reference."));
177                         set_focus('ref');
178                         return false;
179                 }
180
181                 if ($_SESSION['Items']->trans_no==0 && !is_new_reference($_POST['ref'], ST_CUSTDELIVERY)) {
182                         display_error(_("The entered reference is already in use."));
183                         set_focus('ref');
184                         return false;
185                 }
186         }
187         if ($_POST['ChargeFreightCost'] == "") {
188                 $_POST['ChargeFreightCost'] = price_format(0);
189         }
190
191         if (!check_num('ChargeFreightCost',0)) {
192                 display_error(_("The entered shipping value is not numeric."));
193                 set_focus('ChargeFreightCost');
194                 return false;
195         }
196
197         if ($_SESSION['Items']->has_items_dispatch() == 0 && input_num('ChargeFreightCost') == 0) {
198                 display_error(_("There are no item quantities on this delivery note."));
199                 return false;
200         }
201
202         if (!check_quantities()) {
203                 return false;
204         }
205
206         return true;
207 }
208 //------------------------------------------------------------------------------
209 function copy_to_cart()
210 {
211         $cart = &$_SESSION['Items'];
212         $cart->ship_via = $_POST['ship_via'];
213         $cart->freight_cost = input_num('ChargeFreightCost');
214         $cart->document_date = $_POST['DispatchDate'];
215         $cart->due_date =  $_POST['due_date'];
216         $cart->Location = $_POST['Location'];
217         $cart->Comments = $_POST['Comments'];
218         $cart->dimension_id = $_POST['dimension_id'];
219         $cart->dimension2_id = $_POST['dimension2_id'];
220         if ($cart->trans_no == 0)
221                 $cart->reference = $_POST['ref'];
222
223 }
224 //------------------------------------------------------------------------------
225
226 function copy_from_cart()
227 {
228         $cart = &$_SESSION['Items'];
229         $_POST['ship_via'] = $cart->ship_via;
230         $_POST['ChargeFreightCost'] = price_format($cart->freight_cost);
231         $_POST['DispatchDate'] = $cart->document_date;
232         $_POST['due_date'] = $cart->due_date;
233         $_POST['Location'] = $cart->Location;
234         $_POST['Comments'] = $cart->Comments;
235         $_POST['dimension_id'] = $cart->dimension_id;
236         $_POST['dimension2_id'] = $cart->dimension2_id;
237         $_POST['cart_id'] = $cart->cart_id;
238         $_POST['ref'] = $cart->reference;
239 }
240 //------------------------------------------------------------------------------
241
242 function check_quantities()
243 {
244         $ok =1;
245         // Update cart delivery quantities/descriptions
246         foreach ($_SESSION['Items']->line_items as $line=>$itm) {
247                 if (isset($_POST['Line'.$line])) {
248                 if($_SESSION['Items']->trans_no) {
249                         $min = $itm->qty_done;
250                         $max = $itm->quantity;
251                 } else {
252                         $min = 0;
253                         $max = $itm->quantity - $itm->qty_done;
254                 }
255                 
256                         if (check_num('Line'.$line, $min, $max)) {
257                                 $_SESSION['Items']->line_items[$line]->qty_dispatched =
258                                   input_num('Line'.$line);
259                         } else {
260                                 set_focus('Line'.$line);
261                                 $ok = 0;
262                         }
263
264                 }
265
266                 if (isset($_POST['Line'.$line.'Desc'])) {
267                         $line_desc = $_POST['Line'.$line.'Desc'];
268                         if (strlen($line_desc) > 0) {
269                                 $_SESSION['Items']->line_items[$line]->item_description = $line_desc;
270                         }
271                 }
272         }
273 // ...
274 //      else
275 //        $_SESSION['Items']->freight_cost = input_num('ChargeFreightCost');
276         return $ok;
277 }
278 //------------------------------------------------------------------------------
279
280 function check_qoh()
281 {
282         global $SysPrefs;
283
284         if (!$SysPrefs->allow_negative_stock()) {
285                 foreach ($_SESSION['Items']->line_items as $itm) {
286
287                         if ($itm->qty_dispatched && has_stock_holding($itm->mb_flag)) {
288                                 $qoh = get_qoh_on_date($itm->stock_id, $_POST['Location'], $_POST['DispatchDate']);
289
290                                 if ($itm->qty_dispatched > $qoh) {
291                                         display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:") .
292                                                 " " . $itm->stock_id . " - " .  $itm->item_description);
293                                         return false;
294                                 }
295                         }
296                 }
297         }
298         return true;
299 }
300 //------------------------------------------------------------------------------
301
302 if (isset($_POST['process_delivery']) && check_data() && check_qoh()) {
303
304         $dn = &$_SESSION['Items'];
305
306         if ($_POST['bo_policy']) {
307                 $bo_policy = 0;
308         } else {
309                 $bo_policy = 1;
310         }
311         $newdelivery = ($dn->trans_no == 0);
312
313         copy_to_cart();
314         if ($newdelivery) new_doc_date($dn->document_date);
315         $delivery_no = $dn->write($bo_policy);
316
317         processing_end();
318         if ($newdelivery) {
319                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$delivery_no");
320         } else {
321                 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$delivery_no");
322         }
323 }
324
325 if (isset($_POST['Update']) || isset($_POST['_Location_update'])) {
326         $Ajax->activate('Items');
327 }
328 //------------------------------------------------------------------------------
329 start_form();
330 hidden('cart_id');
331
332 start_table(TABLESTYLE2, "width=80%", 5);
333 echo "<tr><td>"; // outer table
334
335 start_table(TABLESTYLE, "width=100%");
336 start_row();
337 label_cells(_("Customer"), $_SESSION['Items']->customer_name, "class='tableheader2'");
338 label_cells(_("Branch"), get_branch_name($_SESSION['Items']->Branch), "class='tableheader2'");
339 label_cells(_("Currency"), $_SESSION['Items']->customer_currency, "class='tableheader2'");
340 end_row();
341 start_row();
342
343 //if (!isset($_POST['ref']))
344 //      $_POST['ref'] = $Refs->get_next(ST_CUSTDELIVERY);
345
346 if ($_SESSION['Items']->trans_no==0) {
347         ref_cells(_("Reference"), 'ref', '', null, "class='tableheader2'");
348 } else {
349         label_cells(_("Reference"), $_SESSION['Items']->reference, "class='tableheader2'");
350 }
351
352 label_cells(_("For Sales Order"), get_customer_trans_view_str(ST_SALESORDER, $_SESSION['Items']->order_no), "class='tableheader2'");
353
354 label_cells(_("Sales Type"), $_SESSION['Items']->sales_type_name, "class='tableheader2'");
355 end_row();
356 start_row();
357
358 if (!isset($_POST['Location'])) {
359         $_POST['Location'] = $_SESSION['Items']->Location;
360 }
361 label_cell(_("Delivery From"), "class='tableheader2'");
362 locations_list_cells(null, 'Location', null, false, true);
363
364 if (!isset($_POST['ship_via'])) {
365         $_POST['ship_via'] = $_SESSION['Items']->ship_via;
366 }
367 label_cell(_("Shipping Company"), "class='tableheader2'");
368 shippers_list_cells(null, 'ship_via', $_POST['ship_via']);
369
370 // set this up here cuz it's used to calc qoh
371 if (!isset($_POST['DispatchDate']) || !is_date($_POST['DispatchDate'])) {
372         $_POST['DispatchDate'] = new_doc_date();
373         if (!is_date_in_fiscalyear($_POST['DispatchDate'])) {
374                 $_POST['DispatchDate'] = end_fiscalyear();
375         }
376 }
377 date_cells(_("Date"), 'DispatchDate', '', $_SESSION['Items']->trans_no==0, 0, 0, 0, "class='tableheader2'");
378 end_row();
379
380 end_table();
381
382 echo "</td><td>";// outer table
383
384 start_table(TABLESTYLE, "width=90%");
385
386 if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
387         $_POST['due_date'] = get_invoice_duedate($_SESSION['Items']->payment, $_POST['DispatchDate']);
388 }
389 customer_credit_row($_SESSION['Items']->customer_id, $_SESSION['Items']->credit, "class='tableheader2'");
390 // 2010-09-03 Joe Hunt
391 $dim = get_company_pref('use_dimension');
392 if ($dim > 0) {
393         start_row();
394         label_cell(_("Dimension").":", "class='tableheader2'");
395         dimensions_list_cells(null, 'dimension_id', null, true, ' ', false, 1, false);
396         end_row();
397 }               
398 else
399         hidden('dimension_id', 0);
400 if ($dim > 1) {
401         start_row();
402         label_cell(_("Dimension")." 2:", "class='tableheader2'");
403         dimensions_list_cells(null, 'dimension2_id', null, true, ' ', false, 2, false);
404         end_row();
405 }               
406 else
407         hidden('dimension2_id', 0);
408 //---------
409 start_row();
410 date_cells(_("Invoice Dead-line"), 'due_date', '', null, 0, 0, 0, "class='tableheader2'");
411 end_row();
412 end_table();
413
414 echo "</td></tr>";
415 end_table(1); // outer table
416
417 $row = get_customer_to_order($_SESSION['Items']->customer_id);
418 if ($row['dissallow_invoices'] == 1)
419 {
420         display_error(_("The selected customer account is currently on hold. Please contact the credit control personnel to discuss."));
421         end_form();
422         end_page();
423         exit();
424 }       
425 display_heading(_("Delivery Items"));
426 div_start('Items');
427 start_table(TABLESTYLE, "width=80%");
428
429 $new = $_SESSION['Items']->trans_no==0;
430 $th = array(_("Item Code"), _("Item Description"), 
431         $new ? _("Ordered") : _("Max. delivery"), _("Units"), $new ? _("Delivered") : _("Invoiced"),
432         _("This Delivery"), _("Price"), _("Tax Type"), _("Discount"), _("Total"));
433
434 table_header($th);
435 $k = 0;
436 $has_marked = false;
437
438 foreach ($_SESSION['Items']->line_items as $line=>$ln_itm) {
439         if ($ln_itm->quantity==$ln_itm->qty_done) {
440                 continue; //this line is fully delivered
441         }
442         // if it's a non-stock item (eg. service) don't show qoh
443         $show_qoh = true;
444         if ($SysPrefs->allow_negative_stock() || !has_stock_holding($ln_itm->mb_flag) ||
445                 $ln_itm->qty_dispatched == 0) {
446                 $show_qoh = false;
447         }
448
449         if ($show_qoh) {
450                 $qoh = get_qoh_on_date($ln_itm->stock_id, $_POST['Location'], $_POST['DispatchDate']);
451         }
452
453         if ($show_qoh && ($ln_itm->qty_dispatched > $qoh)) {
454                 // oops, we don't have enough of one of the component items
455                 start_row("class='stockmankobg'");
456                 $has_marked = true;
457         } else {
458                 alt_table_row_color($k);
459         }
460         view_stock_status_cell($ln_itm->stock_id);
461
462         if ($ln_itm->descr_editable)
463                 text_cells(null, 'Line'.$line.'Desc', $ln_itm->item_description, 30, 50);
464         else
465                 label_cell($ln_itm->item_description);
466
467         $dec = get_qty_dec($ln_itm->stock_id);
468         qty_cell($ln_itm->quantity, false, $dec);
469         label_cell($ln_itm->units);
470         qty_cell($ln_itm->qty_done, false, $dec);
471
472         small_qty_cells(null, 'Line'.$line, qty_format($ln_itm->qty_dispatched, $ln_itm->stock_id, $dec), null, null, $dec);
473
474         $display_discount_percent = percent_format($ln_itm->discount_percent*100) . "%";
475
476         $line_total = ($ln_itm->qty_dispatched * $ln_itm->price * (1 - $ln_itm->discount_percent));
477
478         amount_cell($ln_itm->price);
479         label_cell($ln_itm->tax_type_name);
480         label_cell($display_discount_percent, "nowrap align=right");
481         amount_cell($line_total);
482
483         end_row();
484 }
485
486 $_POST['ChargeFreightCost'] =  get_post('ChargeFreightCost', 
487         price_format($_SESSION['Items']->freight_cost));
488
489 $colspan = 9;
490
491 start_row();
492 label_cell(_("Shipping Cost"), "colspan=$colspan align=right");
493 small_amount_cells(null, 'ChargeFreightCost', $_SESSION['Items']->freight_cost);
494 end_row();
495
496 $inv_items_total = $_SESSION['Items']->get_items_total_dispatch();
497
498 $display_sub_total = price_format($inv_items_total + input_num('ChargeFreightCost'));
499
500 label_row(_("Sub-total"), $display_sub_total, "colspan=$colspan align=right","align=right");
501
502 $taxes = $_SESSION['Items']->get_taxes(input_num('ChargeFreightCost'));
503 $tax_total = display_edit_tax_items($taxes, $colspan, $_SESSION['Items']->tax_included);
504
505 $display_total = price_format(($inv_items_total + input_num('ChargeFreightCost') + $tax_total));
506
507 label_row(_("Amount Total"), $display_total, "colspan=$colspan align=right","align=right");
508
509 end_table(1);
510
511 if ($has_marked) {
512         display_note(_("Marked items have insufficient quantities in stock as on day of delivery."), 0, 1, "class='red'");
513 }
514 start_table(TABLESTYLE2);
515
516 policy_list_row(_("Action For Balance"), "bo_policy", null);
517
518 textarea_row(_("Memo"), 'Comments', null, 50, 4);
519
520 end_table(1);
521 div_end();
522 submit_center_first('Update', _("Update"),
523   _('Refresh document page'), true);
524 submit_center_last('process_delivery', _("Process Dispatch"),
525   _('Check entered data and save document'), 'default');
526
527 end_form();
528
529
530 end_page();
531
532 ?>