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 //-----------------------------------------------------------------------------
14 // Entry/Modify Delivery Note against Sales Order
16 $page_security = 'SA_SALESDELIVERY';
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");
29 if ($use_popup_windows) {
30 $js .= get_js_open_window(900, 500);
32 if ($use_date_picker) {
33 $js .= get_js_date_picker();
36 if (isset($_GET['ModifyDelivery'])) {
37 $_SESSION['page_title'] = sprintf(_("Modifying Delivery Note # %d."), $_GET['ModifyDelivery']);
38 $help_page_title = _("Modifying Delivery Note");
40 } elseif (isset($_GET['OrderNumber'])) {
41 $_SESSION['page_title'] = _("Deliver Items for a Sales Order");
45 page($_SESSION['page_title'], false, false, "", $js);
47 if (isset($_GET['AddedID'])) {
48 $dispatch_no = $_GET['AddedID'];
50 display_notification_centered(sprintf(_("Delivery # %d has been entered."),$dispatch_no));
52 display_note(get_customer_trans_view_str(13, $dispatch_no, _("&View This Delivery")), 0, 1);
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);
57 display_note(get_gl_view_str(13, $dispatch_no, _("View the GL Journal Entries for this Dispatch")),1);
59 hyperlink_params("$path_to_root/sales/customer_invoice.php", _("Invoice This Delivery"), "DeliveryNumber=$dispatch_no");
61 hyperlink_params("$path_to_root/sales/inquiry/sales_orders_view.php", _("Select Another Order For Dispatch"), "OutstandingOnly=1");
63 display_footer_exit();
65 } elseif (isset($_GET['UpdatedID'])) {
67 $delivery_no = $_GET['UpdatedID'];
69 display_notification_centered(sprintf(_('Delivery Note # %d has been updated.'),$delivery_no));
71 display_note(get_trans_view_str(13, $delivery_no, _("View this delivery")));
73 display_note(print_document_link($delivery_no, _("Print this delivery"), true, 13));
75 hyperlink_params($path_to_root . "/sales/customer_invoice.php", _("Confirm Delivery and Invoice"), "DeliveryNumber=$delivery_no");
77 hyperlink_params($path_to_root . "/sales/inquiry/sales_deliveries_view.php", _("Select A Different Delivery"), "OutstandingOnly=1");
79 display_footer_exit();
81 //-----------------------------------------------------------------------------
83 if (isset($_GET['OrderNumber']) && $_GET['OrderNumber'] > 0) {
85 $ord = new Cart(30, $_GET['OrderNumber'], true);
87 /*read in all the selected order into the Items cart */
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>");
95 $ord->trans_type = 13;
96 $ord->src_docs = $ord->trans_no;
97 $ord->order_no = key($ord->trans_no);
99 $ord->reference = $Refs->get_next(13);
100 $ord->document_date = new_doc_date();
101 $_SESSION['Items'] = $ord;
104 } elseif (isset($_GET['ModifyDelivery']) && $_GET['ModifyDelivery'] > 0) {
106 $_SESSION['Items'] = new Cart(13,$_GET['ModifyDelivery']);
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.") .
113 display_footer_exit();
118 } elseif ( !processing_active() ) {
119 /* This page can only be called with an order number for invoicing*/
121 display_error(_("This page can only be opened if an order or delivery note has been selected. Please select it first."));
123 hyperlink_params("$path_to_root/sales/inquiry/sales_orders_view.php", _("Select a Sales Order to Delivery"), "OutstandingOnly=1");
129 check_edit_conflicts();
131 if (!check_quantities()) {
132 display_error(_("Selected quantity cannot be less than quantity invoiced nor more than quantity not dispatched on sales order."));
134 } elseif(!check_num('ChargeFreightCost', 0))
135 display_error(_("Freight cost cannot be less than zero"));
136 set_focus('ChargeFreightCost');
139 //-----------------------------------------------------------------------------
141 function check_data()
145 if (!isset($_POST['DispatchDate']) || !is_date($_POST['DispatchDate'])) {
146 display_error(_("The entered date of delivery is invalid."));
147 set_focus('DispatchDate');
151 if (!is_date_in_fiscalyear($_POST['DispatchDate'])) {
152 display_error(_("The entered date of delivery is not in fiscal year."));
153 set_focus('DispatchDate');
157 if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
158 display_error(_("The entered dead-line for invoice is invalid."));
159 set_focus('due_date');
163 if ($_SESSION['Items']->trans_no==0) {
164 if (!$Refs->is_valid($_POST['ref'])) {
165 display_error(_("You must enter a reference."));
170 if ($_SESSION['Items']->trans_no==0 && !is_new_reference($_POST['ref'], 13)) {
171 display_error(_("The entered reference is already in use."));
176 if ($_POST['ChargeFreightCost'] == "") {
177 $_POST['ChargeFreightCost'] = price_format(0);
180 if (!check_num('ChargeFreightCost',0)) {
181 display_error(_("The entered shipping value is not numeric."));
182 set_focus('ChargeFreightCost');
186 if ($_SESSION['Items']->has_items_dispatch() == 0 && input_num('ChargeFreightCost') == 0) {
187 display_error(_("There are no item quantities on this delivery note."));
191 if (!check_quantities()) {
192 display_error(_("Selected quantity cannot be less than quantity invoiced nor more than quantity
193 not dispatched on sales order."));
199 //------------------------------------------------------------------------------
200 function copy_to_cart()
202 $cart = &$_SESSION['Items'];
203 $cart->ship_via = $_POST['ship_via'];
204 $cart->freight_cost = input_num('ChargeFreightCost');
205 $cart->document_date = $_POST['DispatchDate'];
206 $cart->due_date = $_POST['due_date'];
207 $cart->Location = $_POST['Location'];
208 $cart->Comments = $_POST['Comments'];
209 if ($cart->trans_no == 0)
210 $dn->ref = $_POST['ref'];
213 //------------------------------------------------------------------------------
215 function copy_from_cart()
217 $cart = &$_SESSION['Items'];
218 $_POST['ship_via'] = $cart->ship_via;
219 $_POST['ChargeFreightCost'] = price_format($cart->freight_cost);
220 $_POST['DispatchDate'] = $cart->document_date;
221 $_POST['due_date'] = $cart->due_date;
222 $_POST['Location'] = $cart->Location;
223 $_POST['Comments'] = $cart->Comments;
224 $_POST['cart_id'] = $cart->cart_id;
225 $_POST['ref'] = $cart->reference;
227 //------------------------------------------------------------------------------
229 function check_quantities()
232 // Update cart delivery quantities/descriptions
233 foreach ($_SESSION['Items']->line_items as $line=>$itm) {
234 if (isset($_POST['Line'.$line])) {
235 if($_SESSION['Items']->trans_no) {
236 $min = $itm->qty_done;
237 $max = $itm->quantity;
240 $max = $itm->quantity - $itm->qty_done;
243 if (check_num('Line'.$line, $min, $max)) {
244 $_SESSION['Items']->line_items[$line]->qty_dispatched =
245 input_num('Line'.$line);
247 set_focus('Line'.$line);
253 if (isset($_POST['Line'.$line.'Desc'])) {
254 $line_desc = $_POST['Line'.$line.'Desc'];
255 if (strlen($line_desc) > 0) {
256 $_SESSION['Items']->line_items[$line]->item_description = $line_desc;
262 // $_SESSION['Items']->freight_cost = input_num('ChargeFreightCost');
265 //------------------------------------------------------------------------------
271 if (!$SysPrefs->allow_negative_stock()) {
272 foreach ($_SESSION['Items']->line_items as $itm) {
274 if ($itm->qty_dispatched && has_stock_holding($itm->mb_flag)) {
275 $qoh = get_qoh_on_date($itm->stock_id, $_POST['Location'], $_POST['DispatchDate']);
277 if ($itm->qty_dispatched > $qoh) {
278 display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:") .
279 " " . $itm->stock_id . " - " . $itm->item_description);
287 //------------------------------------------------------------------------------
289 if (isset($_POST['process_delivery']) && check_data() && check_qoh()) {
291 $dn = &$_SESSION['Items'];
293 if ($_POST['bo_policy']) {
298 $newdelivery = ($dn->trans_no == 0);
301 if ($newdelivery) new_doc_date($dn->document_date);
302 $delivery_no = $dn->write($bo_policy);
306 meta_forward($_SERVER['PHP_SELF'], "AddedID=$delivery_no");
308 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$delivery_no");
312 if (isset($_POST['Update']) || isset($_POST['_Location_update'])) {
313 $Ajax->activate('Items');
315 //------------------------------------------------------------------------------
319 start_table("$table_style2 width=80%", 5);
320 echo "<tr><td>"; // outer table
322 start_table("$table_style width=100%");
324 label_cells(_("Customer"), $_SESSION['Items']->customer_name, "class='tableheader2'");
325 label_cells(_("Branch"), get_branch_name($_SESSION['Items']->Branch), "class='tableheader2'");
326 label_cells(_("Currency"), $_SESSION['Items']->customer_currency, "class='tableheader2'");
330 //if (!isset($_POST['ref']))
331 // $_POST['ref'] = $Refs->get_next(13);
333 if ($_SESSION['Items']->trans_no==0) {
334 ref_cells(_("Reference"), 'ref', '', null, "class='tableheader2'");
336 label_cells(_("Reference"), $_SESSION['Items']->reference, "class='tableheader2'");
339 label_cells(_("For Sales Order"), get_customer_trans_view_str(ST_SALESORDER, $_SESSION['Items']->order_no), "class='tableheader2'");
341 label_cells(_("Sales Type"), $_SESSION['Items']->sales_type_name, "class='tableheader2'");
345 if (!isset($_POST['Location'])) {
346 $_POST['Location'] = $_SESSION['Items']->Location;
348 label_cell(_("Delivery From"), "class='tableheader2'");
349 locations_list_cells(null, 'Location', null, false, true);
351 if (!isset($_POST['ship_via'])) {
352 $_POST['ship_via'] = $_SESSION['Items']->ship_via;
354 label_cell(_("Shipping Company"), "class='tableheader2'");
355 shippers_list_cells(null, 'ship_via', $_POST['ship_via']);
357 // set this up here cuz it's used to calc qoh
358 if (!isset($_POST['DispatchDate']) || !is_date($_POST['DispatchDate'])) {
359 $_POST['DispatchDate'] = new_doc_date();
360 if (!is_date_in_fiscalyear($_POST['DispatchDate'])) {
361 $_POST['DispatchDate'] = end_fiscalyear();
364 date_cells(_("Date"), 'DispatchDate', '', $_SESSION['Items']->trans_no==0, 0, 0, 0, "class='tableheader2'");
369 echo "</td><td>";// outer table
371 start_table("$table_style width=90%");
373 if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
374 $_POST['due_date'] = get_invoice_duedate($_SESSION['Items']->customer_id, $_POST['DispatchDate']);
376 date_row(_("Invoice Dead-line"), 'due_date', '', null, 0, 0, 0, "class='tableheader2'");
380 end_table(1); // outer table
382 $row = get_customer_to_order($_SESSION['Items']->customer_id);
383 if ($row['dissallow_invoices'] == 1)
385 display_error(_("The selected customer account is currently on hold. Please contact the credit control personnel to discuss."));
390 display_heading(_("Delivery Items"));
392 start_table("$table_style width=80%");
393 $th = array(_("Item Code"), _("Item Description"), _("Ordered"), _("Units"), _("Delivered"),
394 _("This Delivery"), _("Price"), _("Tax Type"), _("Discount"), _("Total"));
399 foreach ($_SESSION['Items']->line_items as $line=>$ln_itm) {
400 if ($ln_itm->quantity==$ln_itm->qty_done) {
401 continue; //this line is fully delivered
403 // if it's a non-stock item (eg. service) don't show qoh
405 if ($SysPrefs->allow_negative_stock() || !has_stock_holding($ln_itm->mb_flag) ||
406 $ln_itm->qty_dispatched == 0) {
411 $qoh = get_qoh_on_date($ln_itm->stock_id, $_POST['Location'], $_POST['DispatchDate']);
414 if ($show_qoh && ($ln_itm->qty_dispatched > $qoh)) {
415 // oops, we don't have enough of one of the component items
416 start_row("class='stockmankobg'");
419 alt_table_row_color($k);
421 view_stock_status_cell($ln_itm->stock_id);
423 text_cells(null, 'Line'.$line.'Desc', $ln_itm->item_description, 30, 50);
424 $dec = get_qty_dec($ln_itm->stock_id);
425 qty_cell($ln_itm->quantity, false, $dec);
426 label_cell($ln_itm->units);
427 qty_cell($ln_itm->qty_done, false, $dec);
429 small_qty_cells(null, 'Line'.$line, qty_format($ln_itm->qty_dispatched, $ln_itm->stock_id, $dec), null, null, $dec);
431 $display_discount_percent = percent_format($ln_itm->discount_percent*100) . "%";
433 $line_total = ($ln_itm->qty_dispatched * $ln_itm->price * (1 - $ln_itm->discount_percent));
435 amount_cell($ln_itm->price);
436 label_cell($ln_itm->tax_type_name);
437 label_cell($display_discount_percent, "nowrap align=right");
438 amount_cell($line_total);
443 $_POST['ChargeFreightCost'] = get_post('ChargeFreightCost',
444 price_format($_SESSION['Items']->freight_cost));
448 small_amount_cells(_("Shipping Cost"), 'ChargeFreightCost', $_SESSION['Items']->freight_cost, "colspan=9 align=right");
450 $inv_items_total = $_SESSION['Items']->get_items_total_dispatch();
452 $display_sub_total = price_format($inv_items_total + input_num('ChargeFreightCost'));
454 label_row(_("Sub-total"), $display_sub_total, "colspan=9 align=right","align=right");
456 $taxes = $_SESSION['Items']->get_taxes(input_num('ChargeFreightCost'));
457 $tax_total = display_edit_tax_items($taxes, 9, $_SESSION['Items']->tax_included);
459 $display_total = price_format(($inv_items_total + input_num('ChargeFreightCost') + $tax_total));
461 label_row(_("Amount Total"), $display_total, "colspan=9 align=right","align=right");
466 display_note(_("Marked items have insufficient quantities in stock as on day of delivery."), 0, 1, "class='red'");
468 start_table($table_style2);
470 policy_list_row(_("Action For Balance"), "bo_policy", null);
472 textarea_row(_("Memo"), 'Comments', null, 50, 4);
476 submit_center_first('Update', _("Update"),
477 _('Refresh document page'), true);
478 submit_center_last('process_delivery', _("Process Dispatch"),
479 _('Check entered data and save document'), 'default');