2 //-----------------------------------------------------------------------------
4 // Entry/Modify Delivery Note against Sales Order
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");
19 if ($use_popup_windows) {
20 $js .= get_js_open_window(900, 500);
22 if ($use_date_picker) {
23 $js .= get_js_date_picker();
26 if (isset($_GET['ModifyDelivery'])) {
27 $_SESSION['page_title'] = sprintf(_("Modifying Delivery Note # %d."), $_GET['ModifyDelivery']);
28 $help_page_title = _("Modifying Delivery Note");
30 } elseif (isset($_GET['OrderNumber'])) {
31 $_SESSION['page_title'] = _("Deliver Items for a Sales Order");
35 page($_SESSION['page_title'], false, false, "", $js);
37 if (isset($_GET['AddedID'])) {
38 $dispatch_no = $_GET['AddedID'];
39 print_hidden_script(13);
41 display_notification(_("Dispatch processed:") . ' '.$_GET['AddedID'], true);
43 display_note(get_customer_trans_view_str(13, $dispatch_no, _("View this dispatch")), 0, 1);
45 display_note(print_document_link($dispatch_no, _("Print this delivery"), true, 13));
47 display_note(get_gl_view_str(13, $dispatch_no, _("View the GL Journal Entries for this Dispatch")),1);
49 hyperlink_params("$path_to_root/sales/customer_invoice.php", _("Invoice This Delivery"), "DeliveryNumber=$dispatch_no");
51 hyperlink_params("$path_to_root/sales/inquiry/sales_orders_view.php", _("Select Another Order For Dispatch"), "OutstandingOnly=1");
53 display_footer_exit();
55 } elseif (isset($_GET['UpdatedID'])) {
57 $delivery_no = $_GET['UpdatedID'];
58 print_hidden_script(13);
60 display_notification_centered(sprintf(_('Delivery Note # %d has been updated.'),$delivery_no));
62 display_note(get_trans_view_str(13, $delivery_no, _("View this delivery")));
64 display_note(print_document_link($delivery_no, _("Print this delivery"), true, 13));
66 hyperlink_params($path_to_root . "/sales/customer_invoice.php", _("Confirm Delivery and Invoice"), "DeliveryNumber=$delivery_no");
68 hyperlink_params($path_to_root . "/sales/inquiry/sales_deliveries_view.php", _("Select A Different Delivery"), "OutstandingOnly=1");
70 display_footer_exit();
72 //-----------------------------------------------------------------------------
74 if (isset($_GET['OrderNumber']) && $_GET['OrderNumber'] > 0) {
76 $ord = new Cart(30,$_GET['OrderNumber'], true);
78 /*read in all the selected order into the Items cart */
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>");
86 $ord->trans_type = 13;
87 $ord->src_docs = $ord->trans_no;
88 $ord->order_no = key($ord->trans_no);
90 $ord->reference = references::get_next(13);
91 $_SESSION['Items'] = $ord;
94 } elseif (isset($_GET['ModifyDelivery']) && $_GET['ModifyDelivery'] > 0) {
96 $_SESSION['Items'] = new Cart(13,$_GET['ModifyDelivery']);
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.") .
103 display_footer_exit();
108 } elseif ( !processing_active() ) {
109 /* This page can only be called with an order number for invoicing*/
111 display_error(_("This page can only be opened if an order or delivery note has been selected. Please select it first."));
113 hyperlink_params("$path_to_root/sales/inquiry/sales_orders_view.php", _("Select a Sales Order to Delivery"), "OutstandingOnly=1");
118 } elseif (!check_quantities()) {
119 display_error(_("Selected quantity cannot be less then quantity invoiced nor more then quantity
120 not dispatched on sales order."));
122 //-----------------------------------------------------------------------------
124 function check_data()
126 if (!isset($_POST['DispatchDate']) || !is_date($_POST['DispatchDate'])) {
127 display_error(_("The entered date of delivery is invalid."));
131 if (!is_date_in_fiscalyear($_POST['DispatchDate'])) {
132 display_error(_("The entered date of delivery is not in fiscal year."));
136 if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
137 display_error(_("The entered dead-line for invoice is invalid."));
141 if ($_SESSION['Items']->trans_no==0) {
142 if (!references::is_valid($_POST['ref'])) {
143 display_error(_("You must enter a reference."));
147 if ($_SESSION['Items']->trans_no==0 && !is_new_reference($_POST['ref'], 13)) {
148 display_error(_("The entered reference is already in use."));
152 if ($_POST['ChargeFreightCost'] == "") {
153 $_POST['ChargeFreightCost'] = 0;
156 if (!is_numeric($_POST['ChargeFreightCost']) || $_POST['ChargeFreightCost'] < 0) {
157 display_error(_("The entered shipping value is not numeric."));
161 if ($_SESSION['Items']->has_items_dispatch() == 0 && $_POST['ChargeFreightCost'] == 0) {
162 display_error(_("There are no item quantities on this delivery note."));
166 if (!check_quantities()) {
167 display_error(_("Selected quantity cannot be less then quantity invoiced nor more then quantity
168 not dispatched on sales order."));
174 //------------------------------------------------------------------------------
175 function copy_to_cart()
177 $cart = &$_SESSION['Items'];
178 $cart->ship_via = $_POST['ship_via'];
179 $cart->freight_cost = $_POST['ChargeFreightCost'];
180 $cart->document_date = $_POST['DispatchDate'];
181 $cart->due_date = $_POST['due_date'];
182 $cart->Location = $_POST['Location'];
183 $cart->Comments = $_POST['Comments'];
184 $cart->sales_type = $_POST['sales_type_id'];
186 //------------------------------------------------------------------------------
188 function copy_from_cart()
190 $cart = &$_SESSION['Items'];
191 $_POST['ship_via'] = $cart->ship_via;
192 $_POST['ChargeFreightCost'] = $cart->freight_cost;
193 $_POST['DispatchDate']= $cart->document_date;
194 $_POST['due_date'] = $cart->due_date;
195 $_POST['Location']= $cart->Location;
196 $_POST['Comments']= $cart->Comments;
197 $_POST['sales_type_id'] = $cart->sales_type;
199 //------------------------------------------------------------------------------
201 function check_quantities()
204 // Update cart delivery quantities/descriptions
205 foreach ($_SESSION['Items']->line_items as $line=>$itm) {
206 if (isset($_POST['Line'.$line])) {
207 $line_qty = $_POST['Line'.$line];
208 if (is_numeric($line_qty) && ($_POST['Line'.$line] <= $itm->quantity) &&
209 ($_POST['Line'.$line] >= $itm->qty_done)) {
211 $_SESSION['Items']->line_items[$line]->qty_dispatched = $line_qty;
218 if (isset($_POST['Line'.$line.'Desc'])) {
219 $line_desc = $_POST['Line'.$line.'Desc'];
220 if (strlen($line_desc) > 0) {
221 $_SESSION['Items']->line_items[$line]->item_description = $line_desc;
225 $_SESSION['Items']->freight_cost = $_POST['ChargeFreightCost'];
229 //------------------------------------------------------------------------------
233 if (!sys_prefs::allow_negative_stock()) {
234 foreach ($_SESSION['Items']->line_items as $itm) {
236 if ($itm->qty_dispatched && has_stock_holding($itm->mb_flag)) {
237 $qoh = get_qoh_on_date($itm->stock_id, $_POST['Location'], $_POST['DispatchDate']);
239 if ($itm->qty_dispatched > $qoh) {
240 display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:") .
241 " " . $itm->stock_id . " - " . $itm->item_description);
249 //------------------------------------------------------------------------------
251 if (isset($_POST['process_delivery']) && check_data() && check_qoh()) {
253 $dn = &$_SESSION['Items'];
255 if ($_POST['bo_policy']) {
260 $newdelivery = $dn->trans_no==0;
263 $dn->ref = $_POST['ref'];
266 $delivery_no = $dn->write($bo_policy);
270 meta_forward($_SERVER['PHP_SELF'], "AddedID=$delivery_no");
272 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$delivery_no");
276 //------------------------------------------------------------------------------
277 start_form(false, true);
279 start_table("$table_style2 width=80%", 5);
280 echo "<tr><td>"; // outer table
282 start_table("$table_style width=100%");
284 label_cells(_("Customer"), $_SESSION['Items']->customer_name, "class='tableheader2'");
285 label_cells(_("Branch"), get_branch_name($_SESSION['Items']->Branch), "class='tableheader2'");
286 label_cells(_("Currency"), $_SESSION['Items']->customer_currency, "class='tableheader2'");
290 //if (!isset($_POST['ref']))
291 // $_POST['ref'] = references::get_next(13);
293 if ($_SESSION['Items']->trans_no==0) {
294 ref_cells(_("Reference"), 'ref', $_SESSION['Items']->reference, "class='tableheader2'");
296 label_cells(_("Reference"), $_SESSION['Items']->reference, "class='tableheader2'");
299 label_cells(_("For Sales Order"), get_customer_trans_view_str(systypes::sales_order(), $_SESSION['Items']->order_no), "class='tableheader2'");
301 label_cells(_("Sales Type"), $_SESSION['Items']->sales_type_name, "class='tableheader2'");
305 if (!isset($_POST['Location'])) {
306 $_POST['Location'] = $_SESSION['Items']->Location;
308 label_cell(_("Delivery From"), "class='tableheader2'");
309 locations_list_cells(null, 'Location',$_POST['Location'], false, true);
311 if (!isset($_POST['ship_via'])) {
312 $_POST['ship_via'] = $_SESSION['Items']->ship_via;
314 label_cell(_("Shipping Company"), "class='tableheader2'");
315 shippers_list_cells(null, 'ship_via', $_POST['ship_via']);
317 // set this up here cuz it's used to calc qoh
318 if (!isset($_POST['DispatchDate']) || !is_date($_POST['DispatchDate'])) {
319 $_POST['DispatchDate'] = Today();
320 if (!is_date_in_fiscalyear($_POST['DispatchDate'])) {
321 $_POST['DispatchDate'] = end_fiscalyear();
324 date_cells(_("Date"), 'DispatchDate', $_POST['DispatchDate'], 0, 0, 0, "class='tableheader'");
329 echo "</td><td>";// outer table
331 start_table("$table_style width=90%");
333 if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
334 $_POST['due_date'] = get_invoice_duedate($_SESSION['Items']->customer_id, $_POST['DispatchDate']);
336 date_row(_("Invoice Dead-line"), 'due_date', $_POST['due_date'], 0, 0, 0, "class='tableheader'");
340 end_table(1); // outer table
342 display_heading(_("Delivery Items"));
344 start_table("$table_style width=80%");
345 $th = array(_("Item Code"), _("Item Description"), _("Ordered"), _("Units"), _("Delivered"),
346 _("This Delivery"), _("Price"), _("Tax Type"), _("Discount"), _("Total"));
352 foreach ($_SESSION['Items']->line_items as $line=>$ln_itm) {
353 if ($ln_itm->quantity==$ln_itm->qty_done) {
354 continue; //this line is fully delivered
356 // if it's a non-stock item (eg. service) don't show qoh
357 if (sys_prefs::allow_negative_stock() || !has_stock_holding($ln_itm->mb_flag) ||
358 $ln_itm->qty_dispatched == 0) {
363 $qoh = get_qoh_on_date($ln_itm->stock_id, $_POST['Location'], $_POST['DispatchDate']);
366 if ($show_qoh && ($ln_itm->qty_dispatched > $qoh)) {
367 // oops, we don't have enough of one of the component items
368 start_row("class='stockmankobg'");
371 alt_table_row_color($k);
373 view_stock_status_cell($ln_itm->stock_id);
375 text_cells(null, 'Line'.$line.'Desc', $ln_itm->item_description, 30, 50);
376 qty_cell($ln_itm->quantity);
377 label_cell($ln_itm->units);
378 qty_cell($ln_itm->qty_done);
380 text_cells(null, 'Line'.$line, $ln_itm->qty_dispatched, 10, 10);
382 $display_discount_percent = number_format2($ln_itm->discount_percent*100,user_percent_dec()) . "%";
384 $line_total = ($ln_itm->qty_dispatched * $ln_itm->price * (1 - $ln_itm->discount_percent));
386 amount_cell($ln_itm->price);
387 label_cell($ln_itm->tax_type_name);
388 label_cell($display_discount_percent, "nowrap align=right");
389 amount_cell($line_total);
394 $_POST['ChargeFreightCost'] = $_SESSION['Items']->freight_cost;
396 if (!is_numeric($_POST['ChargeFreightCost'])) {
397 $_POST['ChargeFreightCost'] = 0;
402 small_amount_cells(_("Shipping Cost"), 'ChargeFreightCost', $_SESSION['Items']->freight_cost, "colspan=9 align=right");
404 $inv_items_total = $_SESSION['Items']->get_items_total_dispatch();
406 $display_sub_total = number_format2($inv_items_total + $_POST['ChargeFreightCost'],user_price_dec());
408 label_row(_("Sub-total"), $display_sub_total, "colspan=9 align=right","align=right");
410 $taxes = $_SESSION['Items']->get_taxes($_POST['ChargeFreightCost']);
411 $tax_total = display_edit_tax_items($taxes, 9, $_SESSION['Items']->tax_included);
413 $display_total = number_format2(($inv_items_total + $_POST['ChargeFreightCost'] + $tax_total), user_price_dec());
415 label_row(_("Amount Total"), $display_total, "colspan=9 align=right","align=right");
420 display_note(_("Marked items have insufficient quantities in stock as on day of delivery."), 0, 1, "class='red'");
422 start_table($table_style2);
424 policy_list_row(_("Action For Balance"), "bo_policy", null);
426 textarea_row(_("Memo"), 'Comments', null, 50, 4);
430 submit_center_first('Update', _("Update"));
431 submit_center_last('process_delivery', _("Process Dispatch"));