5 include_once($path_to_root . "/sales/includes/cart_class.inc");
7 include_once($path_to_root . "/includes/session.inc");
9 include_once($path_to_root . "/includes/data_checks.inc");
11 include_once($path_to_root . "/sales/includes/sales_ui.inc");
12 include_once($path_to_root . "/sales/includes/ui/sales_order_ui.inc");
13 include_once($path_to_root . "/sales/includes/sales_db.inc");
15 $js = get_js_form_entry("StockID2", "stock_id", "qty");
16 if ($use_popup_windows)
17 $js .= get_js_open_window(900, 500);
19 $js .= get_js_date_picker();
21 if (isset($_GET['ModifyOrderNumber']))
23 page(_("Modifying Sales Order") . " #".$_GET['ModifyOrderNumber'], false, false, "", $js);
25 elseif (isset($_GET['NewInvoice']) || (isset($_SESSION['Items']) && $_SESSION['Items']->direct_invoice))
27 page(_("Sales Invoices"), false, false, "", $js);
31 page(_("Sales Order Entry"), false, false, "", $js);
35 //--------------------------------------------------------------------------------
36 if (isset($_GET['AddedID']))
38 $order_no = $_GET['AddedID'];
39 $trans_type = systypes::sales_order();
41 display_notification_centered(_("Order has been entered.") . " #$order_no");
43 display_note(get_trans_view_str($trans_type, $order_no, _("View this order")));
45 hyperlink_params($path_to_root . "/sales/customer_invoice.php", _("Confirm Order Delivery Quantities and Produce Invoice"), "OrderNumber=$order_no");
47 hyperlink_params($_SERVER['PHP_SELF'], _("Enter a New Order"), "NewOrder=Yes");
49 display_footer_exit();
51 //--------------------------------------------------------------------------------
53 if (isset($_GET['UpdatedID']))
55 $order_no = $_GET['UpdatedID'];
56 $trans_type = systypes::sales_order();
58 display_notification_centered(_("Order has been updated.") . " #$order_no");
60 display_note(get_trans_view_str($trans_type, $order_no, _("View this order")));
62 hyperlink_params($path_to_root . "/sales/customer_invoice.php", _("Confirm Order Delivery Quantities and Produce Invoice"), "OrderNumber=$order_no");
64 hyperlink_params($path_to_root . "/sales/inquiry/sales_orders_view.php", _("Select A Different Order"), "OutstandingOnly=1");
66 display_footer_exit();
69 //--------------------------------------------------------------------------------------------------
73 if ($_SESSION['Items']->direct_invoice)
74 $_SESSION['Items']->memo_ = $_POST['InvoiceText'];
76 $_SESSION['Items']->orig_order_date = $_POST['OrderDate'];
77 $_SESSION['Items']->delivery_date = $_POST['delivery_date'];
78 $_SESSION['Items']->cust_ref = $_POST['cust_ref'];
79 $_SESSION['Items']->freight_cost = $_POST['freight_cost'];
80 $_SESSION['Items']->Comments = $_POST['Comments'];
82 $_SESSION['Items']->deliver_to = $_POST['deliver_to'];
83 $_SESSION['Items']->delivery_address = $_POST["delivery_address"];
84 $_SESSION['Items']->phone = $_POST["phone"];
85 $_SESSION['Items']->Location = $_POST["Location"];
86 $_SESSION['Items']->ship_via = $_POST["ship_via"];
89 //--------------------------------------------------------------------------------------------------
91 function copy_from_so()
93 if ($_SESSION['Items']->direct_invoice)
94 $_POST['InvoiceText'] = $_SESSION['Items']->memo_;
96 $_POST['OrderDate'] = $_SESSION['Items']->orig_order_date;
97 $_POST['delivery_date'] = $_SESSION['Items']->delivery_date;
98 $_POST['cust_ref'] = $_SESSION['Items']->cust_ref;
99 $_POST['freight_cost'] = $_SESSION['Items']->freight_cost;
100 $_POST['Comments'] = $_SESSION['Items']->Comments;
102 $_POST['deliver_to'] = $_SESSION['Items']->deliver_to;
103 $_POST["delivery_address"] = $_SESSION['Items']->delivery_address;
104 $_POST["phone"] = $_SESSION['Items']->phone;
105 $_POST["Location"] = $_SESSION['Items']->Location;
106 $_POST["ship_via"] = $_SESSION['Items']->ship_via;
110 function can_process()
112 if ($_SESSION['Items']->direct_invoice)
114 $edate = _("The entered invoice date is invalid.");
118 $edate = _("The entered order date is invalid.");
120 if (!is_date($_POST['OrderDate']))
122 display_error($edate);
125 if ($_SESSION['Items']->direct_invoice && !is_date_in_fiscalyear($_POST['OrderDate']))
127 display_error(_("The entered date is not in fiscal year"));
132 $_SESSION['Items']->orig_order_date = $_POST['OrderDate'];
134 if (count($_SESSION['Items']->line_items) == 0)
136 display_error(_("You must enter at least one line entry."));
139 if (strlen($_POST['deliver_to']) <= 1)
141 display_error(_("You must enter the person or company to whom delivery should be made to."));
145 if (strlen($_POST['delivery_address']) <= 1)
147 display_error( _("You should enter the street address in the box provided. Orders cannot be accepted without a valid street address."));
151 if ($_POST['freight_cost'] == "")
152 $_POST['freight_cost'] = 0;
154 if (!is_numeric($_POST['freight_cost']))
156 display_error(_("The shipping cost entered is expected to be numeric."));
160 if (!is_date($_POST['delivery_date']))
162 display_error(_("The delivery date is invalid."));
166 if (date1_greater_date2($_SESSION['Items']->orig_order_date, $_POST['delivery_date']))
168 display_error(_("The requested delivery date is before the date of the order."));
172 $_SESSION['Items']->deliver_to = $_POST['deliver_to'];
173 $_SESSION['Items']->delivery_date = $_POST['delivery_date'];
174 $_SESSION['Items']->delivery_address = $_POST['delivery_address'];
175 $_SESSION['Items']->phone =$_POST['phone'];
176 if (isset($_POST['email']))
177 $_SESSION['Items']->email =$_POST['email'];
179 $_SESSION['Items']->email = "";
180 $_SESSION['Items']->Location = $_POST['Location'];
181 $_SESSION['Items']->cust_ref = $_POST['cust_ref'];
182 $_SESSION['Items']->Comments = $_POST['Comments'];
183 $_SESSION['Items']->freight_cost = $_POST['freight_cost'];
184 $_SESSION['Items']->ship_via = $_POST['ship_via'];
189 //-----------------------------------------------------------------------------------------------------------
191 if (isset($_POST['ProcessOrder']) && $_SESSION['Items']->order_no == 0 && can_process())
194 $order_no = add_sales_order($_SESSION['Items']);
196 if ($_SESSION['Items']->direct_invoice)
198 $_SESSION['Items']->clear_items();
199 $_SESSION['Items']->memo_ = $_POST['InvoiceText'];
200 $_SESSION['Items']->memo_ = str_replace("'", "\\'", $_SESSION['Items']->memo_);
201 $_SESSION['Items']->order_no = $order_no;
202 meta_forward("$path_to_root/sales/customer_invoice.php", "process_invoice=Yes");
208 unset($_SESSION['Items']->line_items);
209 unset($_SESSION['Items']);
211 meta_forward($_SERVER['PHP_SELF'], "AddedID=$order_no");
215 //-------------------------------------------------------------------------------------------------------------
217 if (isset($_POST['ProcessOrder']) && $_SESSION['Items']->order_no != 0 && can_process())
220 update_sales_order($_SESSION['Items']->order_no, $_SESSION['Items']);
222 $order_no = $_SESSION['Items']->order_no;
224 unset($_SESSION['Items']->line_items);
225 unset($_SESSION['Items']);
227 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$order_no");
231 //--------------------------------------------------------------------------------
233 function check_item_data()
235 if (!is_numeric($_POST['qty']) || $_POST['qty'] < 0 || $_POST['Disc'] > 100 ||
238 display_error( _("The item could not be updated because you are attempting to set the quantity ordered to less than 0, or the discount percent to more than 100."));
241 elseif($_SESSION['Items']->some_already_delivered($_POST['line_no']) != 0 &&
242 $_SESSION['Items']->line_items[$_POST['line_no']]->price != $_POST['price'])
244 display_error(_("The item you attempting to modify the price for has already had some quantity invoiced at the old price. The item unit price cannot be modified retrospectively."));
247 elseif($_SESSION['Items']->some_already_delivered($_POST['line_no']) != 0 &&
248 $_SESSION['Items']->line_items[$_POST['line_no']]->discount_percent != ($_POST['Disc']/100))
250 display_error(_("The item you attempting to modify has had some quantity invoiced at the old discount percent. The items discount cannot be modified retrospectively."));
253 elseif (isset($_SESSION['Items']->line_items[$_POST['line_no']]) && $_SESSION['Items']->line_items[$_POST['line_no']]->qty_inv > $_POST['qty'])
255 display_error(_("You attempting to make the quantity ordered a quantity less than has already been invoiced. The quantity delivered and invoiced cannot be modified retrospectively."));
261 function handle_update_item()
263 if($_POST['UpdateItem'] != "" && check_item_data())
265 $_SESSION['Items']->update_cart_item($_POST['line_no'], $_POST['qty'],
266 $_POST['price'], ($_POST['Disc'] / 100));
270 //--------------------------------------------------------------------------------
272 function handle_delete_item()
274 if($_GET['Delete'] != "")
276 $line_no = $_GET['Delete'];
277 if($_SESSION['Items']->some_already_delivered($line_no) == 0)
279 $_SESSION['Items']->remove_from_cart($line_no);
283 display_error(_("This item cannot be deleted because some of it has already been invoiced."));
288 //--------------------------------------------------------------------------------
290 function handle_new_item()
292 if (!check_item_data())
295 add_to_order($_SESSION['Items'], $_POST['line_no'], $_POST['stock_id'], $_POST['qty'],
296 $_POST['price'], $_POST['Disc']/100);
297 $_POST['StockID2'] = $_POST['stock_id'] = "";
300 //--------------------------------------------------------------------------------
302 function handle_cancel_order()
304 global $path_to_root;
306 if ($_POST['CancelOrder'] != "")
308 $ok_to_delete = 1; //assume this in the first instance
310 if (($_SESSION['Items']->order_no != 0) &&
311 sales_order_has_invoices($_SESSION['Items']->order_no))
314 display_error(_("This order cannot be cancelled because some of it has already been invoiced. However, the line item quantities may be modified."));
317 if ($ok_to_delete == 1)
319 if($_SESSION['Items']->order_no != 0)
321 delete_sales_order($_SESSION['Items']->order_no);
323 $diriv = $_SESSION['Items']->direct_invoice;
324 $_SESSION['Items']->clear_items();
325 $_SESSION['Items'] = new cart;
326 $_SESSION['Items']->direct_invoice = $diriv;
329 display_note(_("This sales invoice has been cancelled as requested."), 1);
330 hyperlink_params($path_to_root . "/sales/sales_order_entry.php", _("Enter a New Sales Invoice"), SID . "&NewInvoice=Yes");
334 display_note(_("This sales order has been cancelled as requested."), 1);
335 hyperlink_params($path_to_root . "/sales/sales_order_entry.php", _("Enter a New Sales Order"), SID . "&NewOrder=Yes");
344 //--------------------------------------------------------------------------------
346 function handle_new_order()
348 /*New order entry - clear any existing order details from the Items object and initiate a newy*/
349 if (isset($_SESSION['Items']))
351 unset ($_SESSION['Items']->line_items);
352 unset ($_SESSION['Items']);
355 session_register("Items");
357 $_SESSION['Items'] = new cart;
358 if (isset($_GET['NewInvoice']))
359 $_SESSION['Items']->direct_invoice = true;
360 $_SESSION['Items']->customer_id = "";
361 $_POST['OrderDate'] = Today();
362 if (!is_date_in_fiscalyear($_POST['OrderDate']))
363 $_POST['OrderDate'] = end_fiscalyear();
364 $_SESSION['Items']->orig_order_date = $_POST['OrderDate'];
367 //--------------------------------------------------------------------------------
369 if (isset($_GET['ModifyOrderNumber']) && $_GET['ModifyOrderNumber'] != "")
372 if (isset($_SESSION['Items']))
374 unset ($_SESSION['Items']->line_items);
375 unset ($_SESSION['Items']);
378 session_register("Items");
380 $_SESSION['Items'] = new cart;
381 $_SESSION['Items']->order_no = $_GET['ModifyOrderNumber'];
383 /*read in all the selected order into the Items cart */
385 read_sales_order($_SESSION['Items']->order_no, $_SESSION['Items']);
388 //--------------------------------------------------------------------------------
390 if (isset($_POST['CancelOrder']))
391 handle_cancel_order();
393 if (isset($_GET['Delete']) || isset($_GET['Edit']))
396 if (isset($_GET['Delete']))
397 handle_delete_item();
399 if (isset($_POST['UpdateItem']) || isset($_POST['AddItem']))
402 if (isset($_POST['UpdateItem']))
403 handle_update_item();
405 if (isset($_POST['AddItem']))
408 //--------------------------------------------------------------------------------
410 if (isset($_GET['NewOrder']) || isset($_GET['NewInvoice']))
416 if (!isset($_POST['customer_id']))
417 $_POST['customer_id'] = $_SESSION['Items']->customer_id;
418 if (!isset($_POST['branch_id']))
419 $_POST['branch_id'] = $_SESSION['Items']->Branch;
420 if (!isset($_POST['OrderDate']))
421 $_POST['OrderDate'] = $_SESSION['Items']->orig_order_date;
424 //--------------------------------------------------------------------------------
426 check_db_has_stock_items(_("There are no inventory items defined in the system."));
428 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
430 if ($_SESSION['Items']->direct_invoice)
432 $idate = _("Invoice Date:");
433 $orderitems = _("Sales Invoice Items");
434 $deliverydetails = _("Enter Delivery Details and Confirm Invoice");
435 $cancelorder = _("Cancel Invoice");
439 $idate = _("Order Date:");
440 $orderitems = _("Sales Order Items");
441 $deliverydetails = _("Enter Delivery Details and Confirm Order");
442 $cancelorder = _("Cancel Order");
444 start_form(false, true);
446 $customer_error = display_order_header($_SESSION['Items'],
447 ($_SESSION['Items']->any_already_delivered() == 0), $idate);
449 if ($customer_error == "")
451 start_table("$table_style width=80%", 10);
453 display_order_summary($orderitems, $_SESSION['Items'], true);
456 display_delivery_details($_SESSION['Items']);
462 display_error($customer_error);
465 if ($_SESSION['Items']->direct_invoice)
467 $porder = _("Place Invoice");
468 $corder = _("Commit Invoice Changes");
469 $eorder = _("Edit Invoice Items");
473 $porder = _("Place Order");
474 $corder = _("Commit Order Changes");
475 $eorder = _("Edit Order Items");
478 if ($_SESSION['Items']->order_no == 0)
480 submit_center_first('ProcessOrder', $porder);
484 submit_center_first('ProcessOrder', $corder);
489 submit('EditOrder', $eorder);
490 if ($_SESSION['Items']->count_items() >= 1)
493 submit('DeliveryDetails', $deliverydetails);
497 submit_center_last('CancelOrder', $cancelorder);
501 //--------------------------------------------------------------------------------
503 if (!isset($_SESSION['Items']))
505 /* It must be a new order being created $_SESSION['Items'] would be set up from the order
506 modification code above if a modification to an existing order. */
508 session_register("Items");
510 $_SESSION['Items'] = new cart;
511 if (isset($_GET['NewInvoice']))
512 $_SESSION['Items']->direct_invoice = true;
513 $_SESSION['Items']->order_no = 0;
516 //--------------------------------------------------------------------------------