Changed API for input/lists functions, added empty hints when needed
[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         $_SESSION['Items'] = $ord;
92         copy_from_cart();
93
94 } elseif (isset($_GET['ModifyDelivery']) && $_GET['ModifyDelivery'] > 0) {
95
96         $_SESSION['Items'] = new Cart(13,$_GET['ModifyDelivery']);
97
98         if ($_SESSION['Items']->count_items() == 0) {
99                 hyperlink_params($path_to_root . "/sales/inquiry/sales_orders_view.php",
100                         _("Select a different delivery"), "OutstandingOnly=1");
101                 echo "<br><center><b>" . _("This delivery has all items invoiced. There is nothing to modify.") .
102                         "</center></b>";
103                 display_footer_exit();
104         }
105
106         copy_from_cart();
107
108 } elseif ( !processing_active() ) {
109         /* This page can only be called with an order number for invoicing*/
110
111         display_error(_("This page can only be opened if an order or delivery note has been selected. Please select it first."));
112
113         hyperlink_params("$path_to_root/sales/inquiry/sales_orders_view.php", _("Select a Sales Order to Delivery"), "OutstandingOnly=1");
114
115         end_page();
116         exit;
117
118 } elseif (!check_quantities()) {
119         display_error(_("Selected quantity cannot be less than quantity invoiced nor more than quantity
120                 not dispatched on sales order."));
121
122 } elseif(!check_num('ChargeFreightCost', 0))
123         display_error(_("Freight cost cannot be less than zero"));
124         set_focus('ChargeFreightCost');
125
126
127 //-----------------------------------------------------------------------------
128
129 function check_data()
130 {
131         if (!isset($_POST['DispatchDate']) || !is_date($_POST['DispatchDate'])) {
132                 display_error(_("The entered date of delivery is invalid."));
133                 set_focus('DispatchDate');
134                 return false;
135         }
136
137         if (!is_date_in_fiscalyear($_POST['DispatchDate'])) {
138                 display_error(_("The entered date of delivery is not in fiscal year."));
139                 set_focus('DispatchDate');
140                 return false;
141         }
142
143         if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
144                 display_error(_("The entered dead-line for invoice is invalid."));
145                 set_focus('due_date');
146                 return false;
147         }
148
149         if ($_SESSION['Items']->trans_no==0) {
150                 if (!references::is_valid($_POST['ref'])) {
151                         display_error(_("You must enter a reference."));
152                         set_focus('ref');
153                         return false;
154                 }
155
156                 if ($_SESSION['Items']->trans_no==0 && !is_new_reference($_POST['ref'], 13)) {
157                         display_error(_("The entered reference is already in use."));
158                         set_focus('ref');
159                         return false;
160                 }
161         }
162         if ($_POST['ChargeFreightCost'] == "") {
163                 $_POST['ChargeFreightCost'] = price_format(0);
164         }
165
166         if (!check_num('ChargeFreightCost',0)) {
167                 display_error(_("The entered shipping value is not numeric."));
168                 set_focus('ChargeFreightCost');
169                 return false;
170         }
171
172         if ($_SESSION['Items']->has_items_dispatch() == 0 && input_num('ChargeFreightCost') == 0) {
173                 display_error(_("There are no item quantities on this delivery note."));
174                 return false;
175         }
176
177         if (!check_quantities()) {
178                 display_error(_("Selected quantity cannot be less than quantity invoiced nor more than quantity
179                 not dispatched on sales order."));
180                 return false;
181         }
182
183         return true;
184 }
185 //------------------------------------------------------------------------------
186 function copy_to_cart()
187 {
188         $cart = &$_SESSION['Items'];
189         $cart->ship_via = $_POST['ship_via'];
190         $cart->freight_cost = input_num('ChargeFreightCost');
191         $cart->document_date =  $_POST['DispatchDate'];
192         $cart->due_date =  $_POST['due_date'];
193         $cart->Location = $_POST['Location'];
194         $cart->Comments = $_POST['Comments'];
195         if ($cart->trans_no == 0)
196                 $dn->ref = $_POST['ref'];
197
198 }
199 //------------------------------------------------------------------------------
200
201 function copy_from_cart()
202 {
203         $cart = &$_SESSION['Items'];
204         $_POST['ship_via'] = $cart->ship_via;
205         $_POST['ChargeFreightCost'] = price_format($cart->freight_cost);
206         $_POST['DispatchDate']= $cart->document_date;
207         $_POST['due_date'] = $cart->due_date;
208         $_POST['Location']= $cart->Location;
209         $_POST['Comments']= $cart->Comments;
210 }
211 //------------------------------------------------------------------------------
212
213 function check_quantities()
214 {
215         $ok =1;
216         // Update cart delivery quantities/descriptions
217         foreach ($_SESSION['Items']->line_items as $line=>$itm) {
218                 if (isset($_POST['Line'.$line])) {
219                         if (!check_num('Line'.$line, $itm->qty_done, $itm->quantity) == 0) {
220                                 $_SESSION['Items']->line_items[$line]->qty_dispatched = 
221                                   input_num('Line'.$line);
222                         } else {
223                                 $ok = 0;
224                         }
225
226                 }
227
228                 if (isset($_POST['Line'.$line.'Desc'])) {
229                         $line_desc = $_POST['Line'.$line.'Desc'];
230                         if (strlen($line_desc) > 0) {
231                                 $_SESSION['Items']->line_items[$line]->item_description = $line_desc;
232                         }
233                 }
234         }
235 // ...
236 //      else
237 //        $_SESSION['Items']->freight_cost = input_num('ChargeFreightCost');
238         return $ok;
239 }
240 //------------------------------------------------------------------------------
241
242 function check_qoh()
243 {
244         if (!sys_prefs::allow_negative_stock()) {
245                 foreach ($_SESSION['Items']->line_items as $itm) {
246
247                         if ($itm->qty_dispatched && has_stock_holding($itm->mb_flag)) {
248                                 $qoh = get_qoh_on_date($itm->stock_id, $_POST['Location'], $_POST['DispatchDate']);
249
250                                 if ($itm->qty_dispatched > $qoh) {
251                                         display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:") .
252                                                 " " . $itm->stock_id . " - " .  $itm->item_description);
253                                         return false;
254                                 }
255                         }
256                 }
257         }
258         return true;
259 }
260 //------------------------------------------------------------------------------
261
262 if (isset($_POST['process_delivery']) && check_data() && check_qoh()) {
263
264         $dn = &$_SESSION['Items'];
265
266         if ($_POST['bo_policy']) {
267                 $bo_policy = 0;
268         } else {
269                 $bo_policy = 1;
270         }
271         $newdelivery = ($dn->trans_no == 0);
272
273         copy_to_cart();
274         $delivery_no = $dn->write($bo_policy);
275
276         processing_end();
277         if ($newdelivery) {
278                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$delivery_no");
279         } else {
280                 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$delivery_no");
281         }
282 }
283
284 //------------------------------------------------------------------------------
285 start_form(false, true);
286
287 start_table("$table_style2 width=80%", 5);
288 echo "<tr><td>"; // outer table
289
290 start_table("$table_style width=100%");
291 start_row();
292 label_cells(_("Customer"), $_SESSION['Items']->customer_name, "class='tableheader2'");
293 label_cells(_("Branch"), get_branch_name($_SESSION['Items']->Branch), "class='tableheader2'");
294 label_cells(_("Currency"), $_SESSION['Items']->customer_currency, "class='tableheader2'");
295 end_row();
296 start_row();
297
298 //if (!isset($_POST['ref']))
299 //      $_POST['ref'] = references::get_next(13);
300
301 if ($_SESSION['Items']->trans_no==0) {
302         ref_cells(_("Reference"), 'ref', '', $_SESSION['Items']->reference, "class='tableheader2'");
303 } else {
304         label_cells(_("Reference"), $_SESSION['Items']->reference, "class='tableheader2'");
305 }
306
307 label_cells(_("For Sales Order"), get_customer_trans_view_str(systypes::sales_order(), $_SESSION['Items']->order_no), "class='tableheader2'");
308
309 label_cells(_("Sales Type"), $_SESSION['Items']->sales_type_name, "class='tableheader2'");
310 end_row();
311 start_row();
312
313 if (!isset($_POST['Location'])) {
314         $_POST['Location'] = $_SESSION['Items']->Location;
315 }
316 label_cell(_("Delivery From"), "class='tableheader2'");
317 locations_list_cells(null, 'Location',$_POST['Location'], false, true);
318
319 if (!isset($_POST['ship_via'])) {
320         $_POST['ship_via'] = $_SESSION['Items']->ship_via;
321 }
322 label_cell(_("Shipping Company"), "class='tableheader2'");
323 shippers_list_cells(null, 'ship_via', $_POST['ship_via']);
324
325 // set this up here cuz it's used to calc qoh
326 if (!isset($_POST['DispatchDate']) || !is_date($_POST['DispatchDate'])) {
327         $_POST['DispatchDate'] = Today();
328         if (!is_date_in_fiscalyear($_POST['DispatchDate'])) {
329                 $_POST['DispatchDate'] = end_fiscalyear();
330         }
331 }
332 date_cells(_("Date"), 'DispatchDate', '', $_POST['DispatchDate'], 0, 0, 0, "class='tableheader2'");
333 end_row();
334
335 end_table();
336
337 echo "</td><td>";// outer table
338
339 start_table("$table_style width=90%");
340
341 if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
342         $_POST['due_date'] = get_invoice_duedate($_SESSION['Items']->customer_id, $_POST['DispatchDate']);
343 }
344 date_row(_("Invoice Dead-line"), 'due_date', '', $_POST['due_date'], 0, 0, 0, "class='tableheader2'");
345 end_table();
346
347 echo "</td></tr>";
348 end_table(1); // outer table
349
350 display_heading(_("Delivery Items"));
351
352 start_table("$table_style width=80%");
353 $th = array(_("Item Code"), _("Item Description"), _("Ordered"), _("Units"), _("Delivered"),
354         _("This Delivery"), _("Price"), _("Tax Type"), _("Discount"), _("Total"));
355 table_header($th);
356 $k = 0;
357 $has_marked = false;
358 $show_qoh = true;
359
360 foreach ($_SESSION['Items']->line_items as $line=>$ln_itm) {
361         if ($ln_itm->quantity==$ln_itm->qty_done) {
362                 continue; //this line is fully delivered
363         }
364         // if it's a non-stock item (eg. service) don't show qoh
365         if (sys_prefs::allow_negative_stock() || !has_stock_holding($ln_itm->mb_flag) ||
366                 $ln_itm->qty_dispatched == 0) {
367                 $show_qoh = false;
368         }
369
370         if ($show_qoh) {
371                 $qoh = get_qoh_on_date($ln_itm->stock_id, $_POST['Location'], $_POST['DispatchDate']);
372         }
373
374         if ($show_qoh && ($ln_itm->qty_dispatched > $qoh)) {
375                 // oops, we don't have enough of one of the component items
376                 start_row("class='stockmankobg'");
377                 $has_marked = true;
378         } else {
379                 alt_table_row_color($k);
380         }
381         view_stock_status_cell($ln_itm->stock_id);
382
383         text_cells(null, 'Line'.$line.'Desc', $ln_itm->item_description, 30, 50);
384         qty_cell($ln_itm->quantity);
385         label_cell($ln_itm->units);
386         qty_cell($ln_itm->qty_done);
387
388         small_qty_cells(null, 'Line'.$line, qty_format($ln_itm->qty_dispatched));
389
390         $display_discount_percent = percent_format($ln_itm->discount_percent*100) . "%";
391
392         $line_total = ($ln_itm->qty_dispatched * $ln_itm->price * (1 - $ln_itm->discount_percent));
393
394         amount_cell($ln_itm->price);
395         label_cell($ln_itm->tax_type_name);
396         label_cell($display_discount_percent, "nowrap align=right");
397         amount_cell($line_total);
398
399         end_row();
400 }
401
402 $_POST['ChargeFreightCost'] = price_format($_SESSION['Items']->freight_cost);
403
404 if (!check_num('ChargeFreightCost')) {
405                 $_POST['ChargeFreightCost'] = price_format(0);
406 }
407
408 start_row();
409
410 small_amount_cells(_("Shipping Cost"), 'ChargeFreightCost', $_SESSION['Items']->freight_cost, "colspan=9 align=right");
411
412 $inv_items_total = $_SESSION['Items']->get_items_total_dispatch();
413
414 $display_sub_total = price_format($inv_items_total + input_num('ChargeFreightCost'));
415
416 label_row(_("Sub-total"), $display_sub_total, "colspan=9 align=right","align=right");
417
418 $taxes = $_SESSION['Items']->get_taxes(input_num('ChargeFreightCost'));
419 $tax_total = display_edit_tax_items($taxes, 9, $_SESSION['Items']->tax_included);
420
421 $display_total = price_format(($inv_items_total + input_num('ChargeFreightCost') + $tax_total));
422
423 label_row(_("Amount Total"), $display_total, "colspan=9 align=right","align=right");
424
425 end_table(1);
426
427 if ($has_marked) {
428         display_note(_("Marked items have insufficient quantities in stock as on day of delivery."), 0, 1, "class='red'");
429 }
430 start_table($table_style2);
431
432 policy_list_row(_("Action For Balance"), "bo_policy", null);
433
434 textarea_row(_("Memo"), 'Comments', null, 50, 4);
435
436 end_table(1);
437
438 submit_center_first('Update', _("Update"));
439 submit_center_last('process_delivery', _("Process Dispatch"));
440
441 end_form();
442
443
444 end_page();
445
446 ?>