cbc5e08107c4e9909dbcc2abd51b9376b3358caa
[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_page_title = _("Modifying Delivery Note");
39         processing_start();
40 } elseif (isset($_GET['OrderNumber'])) {
41         $_SESSION['page_title'] = _("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(13, $dispatch_no, _("&View This Delivery")), 0, 1);
53
54         display_note(print_document_link($dispatch_no, _("&Print Delivery Note"), true, 13));
55         display_note(print_document_link($dispatch_no, _("&Email Delivery Note"), true, 13, false, "", "", 1), 1);
56
57         display_note(get_gl_view_str(13, $dispatch_no, _("View the GL Journal Entries for this Dispatch")),1);
58
59         hyperlink_params("$path_to_root/sales/customer_invoice.php", _("Invoice This Delivery"), "DeliveryNumber=$dispatch_no");
60
61         hyperlink_params("$path_to_root/sales/inquiry/sales_orders_view.php", _("Select Another Order For Dispatch"), "OutstandingOnly=1");
62
63         display_footer_exit();
64
65 } elseif (isset($_GET['UpdatedID'])) {
66
67         $delivery_no = $_GET['UpdatedID'];
68
69         display_notification_centered(sprintf(_('Delivery Note # %d has been updated.'),$delivery_no));
70
71         display_note(get_trans_view_str(13, $delivery_no, _("View this delivery")));
72         echo '<br>';
73         display_note(print_document_link($delivery_no, _("Print this delivery"), true, 13));
74
75         hyperlink_params($path_to_root . "/sales/customer_invoice.php", _("Confirm Delivery and Invoice"), "DeliveryNumber=$delivery_no");
76
77         hyperlink_params($path_to_root . "/sales/inquiry/sales_deliveries_view.php", _("Select A Different Delivery"), "OutstandingOnly=1");
78
79         display_footer_exit();
80 }
81 //-----------------------------------------------------------------------------
82
83 if (isset($_GET['OrderNumber']) && $_GET['OrderNumber'] > 0) {
84
85         $ord = new Cart(30, $_GET['OrderNumber'], true);
86
87         /*read in all the selected order into the Items cart  */
88
89         if ($ord->count_items() == 0) {
90                 hyperlink_params($path_to_root . "/sales/inquiry/sales_orders_view.php",
91                         _("Select a different sales order to delivery"), "OutstandingOnly=1");
92                 die ("<br><b>" . _("This order has no items. There is nothing to delivery.") . "</b>");
93         }
94
95         $ord->trans_type = 13;
96         $ord->src_docs = $ord->trans_no;
97         $ord->order_no = key($ord->trans_no);
98         $ord->trans_no = 0;
99         $ord->reference = references::get_next(13);
100         $ord->document_date = new_doc_date();
101         $_SESSION['Items'] = $ord;
102         copy_from_cart();
103
104 } elseif (isset($_GET['ModifyDelivery']) && $_GET['ModifyDelivery'] > 0) {
105
106         $_SESSION['Items'] = new Cart(13,$_GET['ModifyDelivery']);
107
108         if ($_SESSION['Items']->count_items() == 0) {
109                 hyperlink_params($path_to_root . "/sales/inquiry/sales_orders_view.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 function check_data()
142 {
143         if (!isset($_POST['DispatchDate']) || !is_date($_POST['DispatchDate'])) {
144                 display_error(_("The entered date of delivery is invalid."));
145                 set_focus('DispatchDate');
146                 return false;
147         }
148
149         if (!is_date_in_fiscalyear($_POST['DispatchDate'])) {
150                 display_error(_("The entered date of delivery is not in fiscal year."));
151                 set_focus('DispatchDate');
152                 return false;
153         }
154
155         if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
156                 display_error(_("The entered dead-line for invoice is invalid."));
157                 set_focus('due_date');
158                 return false;
159         }
160
161         if ($_SESSION['Items']->trans_no==0) {
162                 if (!references::is_valid($_POST['ref'])) {
163                         display_error(_("You must enter a reference."));
164                         set_focus('ref');
165                         return false;
166                 }
167
168                 if ($_SESSION['Items']->trans_no==0 && !is_new_reference($_POST['ref'], 13)) {
169                         display_error(_("The entered reference is already in use."));
170                         set_focus('ref');
171                         return false;
172                 }
173         }
174         if ($_POST['ChargeFreightCost'] == "") {
175                 $_POST['ChargeFreightCost'] = price_format(0);
176         }
177
178         if (!check_num('ChargeFreightCost',0)) {
179                 display_error(_("The entered shipping value is not numeric."));
180                 set_focus('ChargeFreightCost');
181                 return false;
182         }
183
184         if ($_SESSION['Items']->has_items_dispatch() == 0 && input_num('ChargeFreightCost') == 0) {
185                 display_error(_("There are no item quantities on this delivery note."));
186                 return false;
187         }
188
189         if (!check_quantities()) {
190                 display_error(_("Selected quantity cannot be less than quantity invoiced nor more than quantity
191                 not dispatched on sales order."));
192                 return false;
193         }
194
195         return true;
196 }
197 //------------------------------------------------------------------------------
198 function copy_to_cart()
199 {
200         $cart = &$_SESSION['Items'];
201         $cart->ship_via = $_POST['ship_via'];
202         $cart->freight_cost = input_num('ChargeFreightCost');
203         $cart->document_date = $_POST['DispatchDate'];
204         $cart->due_date =  $_POST['due_date'];
205         $cart->Location = $_POST['Location'];
206         $cart->Comments = $_POST['Comments'];
207         if ($cart->trans_no == 0)
208                 $dn->ref = $_POST['ref'];
209
210 }
211 //------------------------------------------------------------------------------
212
213 function copy_from_cart()
214 {
215         $cart = &$_SESSION['Items'];
216         $_POST['ship_via'] = $cart->ship_via;
217         $_POST['ChargeFreightCost'] = price_format($cart->freight_cost);
218         $_POST['DispatchDate'] = $cart->document_date;
219         $_POST['due_date'] = $cart->due_date;
220         $_POST['Location'] = $cart->Location;
221         $_POST['Comments'] = $cart->Comments;
222         $_POST['cart_id'] = $cart->cart_id;
223         $_POST['ref'] = $cart->reference;
224 }
225 //------------------------------------------------------------------------------
226
227 function check_quantities()
228 {
229         $ok =1;
230         // Update cart delivery quantities/descriptions
231         foreach ($_SESSION['Items']->line_items as $line=>$itm) {
232                 if (isset($_POST['Line'.$line])) {
233                 if($_SESSION['Items']->trans_no) {
234                         $min = $itm->qty_done;
235                         $max = $itm->quantity;
236                 } else {
237                         $min = 0;
238                         $max = $itm->quantity - $itm->qty_done;
239                 }
240                 
241                         if (check_num('Line'.$line, $min, $max)) {
242                                 $_SESSION['Items']->line_items[$line]->qty_dispatched =
243                                   input_num('Line'.$line);
244                         } else {
245                                 set_focus('Line'.$line);
246                                 $ok = 0;
247                         }
248
249                 }
250
251                 if (isset($_POST['Line'.$line.'Desc'])) {
252                         $line_desc = $_POST['Line'.$line.'Desc'];
253                         if (strlen($line_desc) > 0) {
254                                 $_SESSION['Items']->line_items[$line]->item_description = $line_desc;
255                         }
256                 }
257         }
258 // ...
259 //      else
260 //        $_SESSION['Items']->freight_cost = input_num('ChargeFreightCost');
261         return $ok;
262 }
263 //------------------------------------------------------------------------------
264
265 function check_qoh()
266 {
267         if (!sys_prefs::allow_negative_stock()) {
268                 foreach ($_SESSION['Items']->line_items as $itm) {
269
270                         if ($itm->qty_dispatched && has_stock_holding($itm->mb_flag)) {
271                                 $qoh = get_qoh_on_date($itm->stock_id, $_POST['Location'], $_POST['DispatchDate']);
272
273                                 if ($itm->qty_dispatched > $qoh) {
274                                         display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:") .
275                                                 " " . $itm->stock_id . " - " .  $itm->item_description);
276                                         return false;
277                                 }
278                         }
279                 }
280         }
281         return true;
282 }
283 //------------------------------------------------------------------------------
284
285 if (isset($_POST['process_delivery']) && check_data() && check_qoh()) {
286
287         $dn = &$_SESSION['Items'];
288
289         if ($_POST['bo_policy']) {
290                 $bo_policy = 0;
291         } else {
292                 $bo_policy = 1;
293         }
294         $newdelivery = ($dn->trans_no == 0);
295
296         copy_to_cart();
297         if ($newdelivery) new_doc_date($dn->document_date);
298         $delivery_no = $dn->write($bo_policy);
299
300         processing_end();
301         if ($newdelivery) {
302                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$delivery_no");
303         } else {
304                 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$delivery_no");
305         }
306 }
307
308 if (isset($_POST['Update']) || isset($_POST['_Location_update'])) {
309         $Ajax->activate('Items');
310 }
311 //------------------------------------------------------------------------------
312 start_form();
313 hidden('cart_id');
314
315 start_table("$table_style2 width=80%", 5);
316 echo "<tr><td>"; // outer table
317
318 start_table("$table_style width=100%");
319 start_row();
320 label_cells(_("Customer"), $_SESSION['Items']->customer_name, "class='tableheader2'");
321 label_cells(_("Branch"), get_branch_name($_SESSION['Items']->Branch), "class='tableheader2'");
322 label_cells(_("Currency"), $_SESSION['Items']->customer_currency, "class='tableheader2'");
323 end_row();
324 start_row();
325
326 //if (!isset($_POST['ref']))
327 //      $_POST['ref'] = references::get_next(13);
328
329 if ($_SESSION['Items']->trans_no==0) {
330         ref_cells(_("Reference"), 'ref', '', null, "class='tableheader2'");
331 } else {
332         label_cells(_("Reference"), $_SESSION['Items']->reference, "class='tableheader2'");
333 }
334
335 label_cells(_("For Sales Order"), get_customer_trans_view_str(systypes::sales_order(), $_SESSION['Items']->order_no), "class='tableheader2'");
336
337 label_cells(_("Sales Type"), $_SESSION['Items']->sales_type_name, "class='tableheader2'");
338 end_row();
339 start_row();
340
341 if (!isset($_POST['Location'])) {
342         $_POST['Location'] = $_SESSION['Items']->Location;
343 }
344 label_cell(_("Delivery From"), "class='tableheader2'");
345 locations_list_cells(null, 'Location', null, false, true);
346
347 if (!isset($_POST['ship_via'])) {
348         $_POST['ship_via'] = $_SESSION['Items']->ship_via;
349 }
350 label_cell(_("Shipping Company"), "class='tableheader2'");
351 shippers_list_cells(null, 'ship_via', $_POST['ship_via']);
352
353 // set this up here cuz it's used to calc qoh
354 if (!isset($_POST['DispatchDate']) || !is_date($_POST['DispatchDate'])) {
355         $_POST['DispatchDate'] = new_doc_date();
356         if (!is_date_in_fiscalyear($_POST['DispatchDate'])) {
357                 $_POST['DispatchDate'] = end_fiscalyear();
358         }
359 }
360 date_cells(_("Date"), 'DispatchDate', '', $_SESSION['Items']->trans_no==0, 0, 0, 0, "class='tableheader2'");
361 end_row();
362
363 end_table();
364
365 echo "</td><td>";// outer table
366
367 start_table("$table_style width=90%");
368
369 if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
370         $_POST['due_date'] = get_invoice_duedate($_SESSION['Items']->customer_id, $_POST['DispatchDate']);
371 }
372 date_row(_("Invoice Dead-line"), 'due_date', '', null, 0, 0, 0, "class='tableheader2'");
373 end_table();
374
375 echo "</td></tr>";
376 end_table(1); // outer table
377
378 $row = get_customer_to_order($_SESSION['Items']->customer_id);
379 if ($row['dissallow_invoices'] == 1)
380 {
381         display_error(_("The selected customer account is currently on hold. Please contact the credit control personnel to discuss."));
382         end_form();
383         end_page();
384         exit();
385 }       
386 display_heading(_("Delivery Items"));
387 div_start('Items');
388 start_table("$table_style width=80%");
389 $th = array(_("Item Code"), _("Item Description"), _("Ordered"), _("Units"), _("Delivered"),
390         _("This Delivery"), _("Price"), _("Tax Type"), _("Discount"), _("Total"));
391 table_header($th);
392 $k = 0;
393 $has_marked = false;
394
395 foreach ($_SESSION['Items']->line_items as $line=>$ln_itm) {
396         if ($ln_itm->quantity==$ln_itm->qty_done) {
397                 continue; //this line is fully delivered
398         }
399         // if it's a non-stock item (eg. service) don't show qoh
400         $show_qoh = true;
401         if (sys_prefs::allow_negative_stock() || !has_stock_holding($ln_itm->mb_flag) ||
402                 $ln_itm->qty_dispatched == 0) {
403                 $show_qoh = false;
404         }
405
406         if ($show_qoh) {
407                 $qoh = get_qoh_on_date($ln_itm->stock_id, $_POST['Location'], $_POST['DispatchDate']);
408         }
409
410         if ($show_qoh && ($ln_itm->qty_dispatched > $qoh)) {
411                 // oops, we don't have enough of one of the component items
412                 start_row("class='stockmankobg'");
413                 $has_marked = true;
414         } else {
415                 alt_table_row_color($k);
416         }
417         view_stock_status_cell($ln_itm->stock_id);
418
419         text_cells(null, 'Line'.$line.'Desc', $ln_itm->item_description, 30, 50);
420         $dec = get_qty_dec($ln_itm->stock_id);
421         qty_cell($ln_itm->quantity, false, $dec);
422         label_cell($ln_itm->units);
423         qty_cell($ln_itm->qty_done, false, $dec);
424
425         small_qty_cells(null, 'Line'.$line, qty_format($ln_itm->qty_dispatched, $ln_itm->stock_id, $dec), null, null, $dec);
426
427         $display_discount_percent = percent_format($ln_itm->discount_percent*100) . "%";
428
429         $line_total = ($ln_itm->qty_dispatched * $ln_itm->price * (1 - $ln_itm->discount_percent));
430
431         amount_cell($ln_itm->price);
432         label_cell($ln_itm->tax_type_name);
433         label_cell($display_discount_percent, "nowrap align=right");
434         amount_cell($line_total);
435
436         end_row();
437 }
438
439 $_POST['ChargeFreightCost'] =  get_post('ChargeFreightCost', 
440         price_format($_SESSION['Items']->freight_cost));
441
442 start_row();
443
444 small_amount_cells(_("Shipping Cost"), 'ChargeFreightCost', $_SESSION['Items']->freight_cost, "colspan=9 align=right");
445
446 $inv_items_total = $_SESSION['Items']->get_items_total_dispatch();
447
448 $display_sub_total = price_format($inv_items_total + input_num('ChargeFreightCost'));
449
450 label_row(_("Sub-total"), $display_sub_total, "colspan=9 align=right","align=right");
451
452 $taxes = $_SESSION['Items']->get_taxes(input_num('ChargeFreightCost'));
453 $tax_total = display_edit_tax_items($taxes, 9, $_SESSION['Items']->tax_included);
454
455 $display_total = price_format(($inv_items_total + input_num('ChargeFreightCost') + $tax_total));
456
457 label_row(_("Amount Total"), $display_total, "colspan=9 align=right","align=right");
458
459 end_table(1);
460
461 if ($has_marked) {
462         display_note(_("Marked items have insufficient quantities in stock as on day of delivery."), 0, 1, "class='red'");
463 }
464 start_table($table_style2);
465
466 policy_list_row(_("Action For Balance"), "bo_policy", null);
467
468 textarea_row(_("Memo"), 'Comments', null, 50, 4);
469
470 end_table(1);
471 div_end();
472 submit_center_first('Update', _("Update"),
473   _('Refresh document page'), true);
474 submit_center_last('process_delivery', _("Process Dispatch"),
475   _('Check entered data and save document'), 'default');
476
477 end_form();
478
479
480 end_page();
481
482 ?>