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