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