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