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