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 if (isset($_GET['ModifyOrderNumber']))
21 page(_("Modifying Sales Order") . " #".$_GET['ModifyOrderNumber'], false, false, "", $js);
23 elseif (isset($_GET['NewInvoice']) || (isset($_SESSION['Items']) && $_SESSION['Items']->direct_invoice))
25 page(_("Sales Invoices"), false, false, "", $js);
29 page(_("Sales Order Entry"), false, false, "", $js);
33 //--------------------------------------------------------------------------------
34 if (isset($_GET['AddedID']))
36 $order_no = $_GET['AddedID'];
37 $trans_type = systypes::sales_order();
39 display_notification_centered(_("Order has been entered.") . " #$order_no");
41 display_note(get_trans_view_str($trans_type, $order_no, _("View this order")));
43 hyperlink_params($path_to_root . "/sales/customer_invoice.php", _("Confirm Order Delivery Quantities and Produce Invoice"), "OrderNumber=$order_no");
45 hyperlink_params($_SERVER['PHP_SELF'], _("Enter a New Order"), "NewOrder=Yes");
47 display_footer_exit();
49 //--------------------------------------------------------------------------------
51 if (isset($_GET['UpdatedID']))
53 $order_no = $_GET['UpdatedID'];
54 $trans_type = systypes::sales_order();
56 display_notification_centered(_("Order has been updated.") . " #$order_no");
58 display_note(get_trans_view_str($trans_type, $order_no, _("View this order")));
60 hyperlink_params($path_to_root . "/sales/customer_invoice.php", _("Confirm Order Delivery Quantities and Produce Invoice"), "OrderNumber=$order_no");
62 hyperlink_params($path_to_root . "/sales/inquiry/sales_orders_view.php", _("Select A Different Order"), "OutstandingOnly=1");
64 display_footer_exit();
67 //--------------------------------------------------------------------------------------------------
71 if ($_SESSION['Items']->direct_invoice)
72 $_SESSION['Items']->memo_ = $_POST['InvoiceText'];
74 $_SESSION['Items']->orig_order_date = $_POST['OrderDate'];
75 $_SESSION['Items']->delivery_date = $_POST['delivery_date'];
76 $_SESSION['Items']->cust_ref = $_POST['cust_ref'];
77 $_SESSION['Items']->freight_cost = $_POST['freight_cost'];
78 $_SESSION['Items']->Comments = $_POST['Comments'];
80 $_SESSION['Items']->deliver_to = $_POST['deliver_to'];
81 $_SESSION['Items']->delivery_address = $_POST["delivery_address"];
82 $_SESSION['Items']->phone = $_POST["phone"];
83 $_SESSION['Items']->Location = $_POST["Location"];
84 $_SESSION['Items']->ship_via = $_POST["ship_via"];
87 //--------------------------------------------------------------------------------------------------
89 function copy_from_so()
91 if ($_SESSION['Items']->direct_invoice)
92 $_POST['InvoiceText'] = $_SESSION['Items']->memo_;
94 $_POST['OrderDate'] = $_SESSION['Items']->orig_order_date;
95 $_POST['delivery_date'] = $_SESSION['Items']->delivery_date;
96 $_POST['cust_ref'] = $_SESSION['Items']->cust_ref;
97 $_POST['freight_cost'] = $_SESSION['Items']->freight_cost;
98 $_POST['Comments'] = $_SESSION['Items']->Comments;
100 $_POST['deliver_to'] = $_SESSION['Items']->deliver_to;
101 $_POST["delivery_address"] = $_SESSION['Items']->delivery_address;
102 $_POST["phone"] = $_SESSION['Items']->phone;
103 $_POST["Location"] = $_SESSION['Items']->Location;
104 $_POST["ship_via"] = $_SESSION['Items']->ship_via;
108 function can_process()
110 if ($_SESSION['Items']->direct_invoice)
112 $edate = _("The entered invoice date is invalid.");
116 $edate = _("The entered order date is invalid.");
118 if (!is_date($_POST['OrderDate']))
120 display_error($edate);
123 if ($_SESSION['Items']->direct_invoice && !is_date_in_fiscalyear($_POST['OrderDate']))
125 display_error(_("The entered date is not in fiscal year"));
130 $_SESSION['Items']->orig_order_date = $_POST['OrderDate'];
132 if (count($_SESSION['Items']->line_items) == 0)
134 display_error(_("You must enter at least one line entry."));
137 if (strlen($_POST['deliver_to']) <= 1)
139 display_error(_("You must enter the person or company to whom delivery should be made to."));
143 if (strlen($_POST['delivery_address']) <= 1)
145 display_error( _("You should enter the street address in the box provided. Orders cannot be accepted without a valid street address."));
149 if ($_POST['freight_cost'] == "")
150 $_POST['freight_cost'] = 0;
152 if (!is_numeric($_POST['freight_cost']))
154 display_error(_("The shipping cost entered is expected to be numeric."));
158 if (!is_date($_POST['delivery_date']))
160 display_error(_("The delivery date is invalid."));
164 if (date1_greater_date2($_SESSION['Items']->orig_order_date, $_POST['delivery_date']))
166 display_error(_("The requested delivery date is before the date of the order."));
170 $_SESSION['Items']->deliver_to = $_POST['deliver_to'];
171 $_SESSION['Items']->delivery_date = $_POST['delivery_date'];
172 $_SESSION['Items']->delivery_address = $_POST['delivery_address'];
173 $_SESSION['Items']->phone =$_POST['phone'];
174 if (isset($_POST['email']))
175 $_SESSION['Items']->email =$_POST['email'];
177 $_SESSION['Items']->email = "";
178 $_SESSION['Items']->Location = $_POST['Location'];
179 $_SESSION['Items']->cust_ref = $_POST['cust_ref'];
180 $_SESSION['Items']->Comments = $_POST['Comments'];
181 $_SESSION['Items']->freight_cost = $_POST['freight_cost'];
182 $_SESSION['Items']->ship_via = $_POST['ship_via'];
187 //-----------------------------------------------------------------------------------------------------------
189 if (isset($_POST['ProcessOrder']) && $_SESSION['Items']->order_no == 0 && can_process())
192 $order_no = add_sales_order($_SESSION['Items']);
194 if ($_SESSION['Items']->direct_invoice)
196 $_SESSION['Items']->memo_ = $_POST['InvoiceText'];
197 $_SESSION['Items']->memo_ = str_replace("'", "\\'", $_SESSION['Items']->memo_);
198 $_SESSION['Items']->order_no = $order_no;
199 meta_forward("$path_to_root/sales/customer_invoice.php", "process_invoice=Yes");
205 unset($_SESSION['Items']->line_items);
206 unset($_SESSION['Items']);
208 meta_forward($_SERVER['PHP_SELF'], "AddedID=$order_no");
212 //-------------------------------------------------------------------------------------------------------------
214 if (isset($_POST['ProcessOrder']) && $_SESSION['Items']->order_no != 0 && can_process())
217 update_sales_order($_SESSION['Items']->order_no, $_SESSION['Items']);
219 $order_no = $_SESSION['Items']->order_no;
221 unset($_SESSION['Items']->line_items);
222 unset($_SESSION['Items']);
224 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$order_no");
228 //--------------------------------------------------------------------------------
230 function check_item_data()
232 if (!is_numeric($_POST['qty']) || $_POST['qty'] < 0 || $_POST['Disc'] > 100 ||
235 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."));
238 elseif($_SESSION['Items']->some_already_delivered($_POST['stock_id']) != 0 &&
239 $_SESSION['Items']->line_items[$_POST['stock_id']]->price != $_POST['price'])
241 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."));
244 elseif($_SESSION['Items']->some_already_delivered($_POST['stock_id']) != 0 &&
245 $_SESSION['Items']->line_items[$_POST['stock_id']]->discount_percent != ($_POST['Disc']/100))
247 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."));
250 elseif (isset($_SESSION['Items']->line_items[$_POST['stock_id']]) && $_SESSION['Items']->line_items[$_POST['stock_id']]->qty_inv > $_POST['qty'])
252 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."));
258 function handle_update_item()
260 if($_POST['UpdateItem'] != "" && check_item_data())
262 $_SESSION['Items']->update_cart_item($_POST['stock_id'], $_POST['qty'],
263 $_POST['price'], ($_POST['Disc'] / 100));
267 //--------------------------------------------------------------------------------
269 function handle_delete_item()
271 if($_GET['Delete'] != "")
273 $stock_id = $_GET['Delete'];
274 if($_SESSION['Items']->some_already_delivered($stock_id) == 0)
276 $_SESSION['Items']->remove_from_cart($stock_id);
280 display_error(_("This item cannot be deleted because some of it has already been invoiced."));
285 //--------------------------------------------------------------------------------
287 function handle_new_item()
289 if (!check_item_data())
292 add_to_order($_SESSION['Items'], $_POST['stock_id'], $_POST['qty'],
293 $_POST['price'], $_POST['Disc']/100);
294 $_POST['StockID2'] = $_POST['stock_id'] = "";
297 //--------------------------------------------------------------------------------
299 function handle_cancel_order()
301 global $path_to_root;
303 if ($_POST['CancelOrder'] != "")
305 $ok_to_delete = 1; //assume this in the first instance
307 if (($_SESSION['Items']->order_no != 0) &&
308 sales_order_has_invoices($_SESSION['Items']->order_no))
311 display_error(_("This order cannot be cancelled because some of it has already been invoiced. However, the line item quantities may be modified."));
314 if ($ok_to_delete == 1)
316 if($_SESSION['Items']->order_no != 0)
318 delete_sales_order($_SESSION['Items']->order_no);
320 $diriv = $_SESSION['Items']->direct_invoice;
321 $_SESSION['Items']->clear_items();
322 $_SESSION['Items'] = new cart;
323 $_SESSION['Items']->direct_invoice = $diriv;
326 display_note(_("This sales invoice has been cancelled as requested."), 1);
327 hyperlink_params($path_to_root . "/sales/sales_order_entry.php", _("Enter a New Sales Invoice"), SID . "&NewInvoice=Yes");
331 display_note(_("This sales order has been cancelled as requested."), 1);
332 hyperlink_params($path_to_root . "/sales/sales_order_entry.php", _("Enter a New Sales Order"), SID . "&NewOrder=Yes");
341 //--------------------------------------------------------------------------------
343 function handle_new_order()
345 /*New order entry - clear any existing order details from the Items object and initiate a newy*/
346 if (isset($_SESSION['Items']))
348 unset ($_SESSION['Items']->line_items);
349 unset ($_SESSION['Items']);
352 session_register("Items");
354 $_SESSION['Items'] = new cart;
355 if (isset($_GET['NewInvoice']))
356 $_SESSION['Items']->direct_invoice = true;
357 $_SESSION['Items']->customer_id = "";
358 $_POST['OrderDate'] = Today();
359 if (!is_date_in_fiscalyear($_POST['OrderDate']))
360 $_POST['OrderDate'] = end_fiscalyear();
361 $_SESSION['Items']->orig_order_date = $_POST['OrderDate'];
364 //--------------------------------------------------------------------------------
366 if (isset($_GET['ModifyOrderNumber']) && $_GET['ModifyOrderNumber'] != "")
369 if (isset($_SESSION['Items']))
371 unset ($_SESSION['Items']->line_items);
372 unset ($_SESSION['Items']);
375 session_register("Items");
377 $_SESSION['Items'] = new cart;
378 $_SESSION['Items']->order_no = $_GET['ModifyOrderNumber'];
380 /*read in all the selected order into the Items cart */
382 read_sales_order($_SESSION['Items']->order_no, $_SESSION['Items']);
385 //--------------------------------------------------------------------------------
387 if (isset($_POST['CancelOrder']))
388 handle_cancel_order();
390 if (isset($_GET['Delete']) || isset($_GET['Edit']))
393 if (isset($_GET['Delete']))
394 handle_delete_item();
396 if (isset($_POST['UpdateItem']) || isset($_POST['AddItem']))
399 if (isset($_POST['UpdateItem']))
400 handle_update_item();
402 if (isset($_POST['AddItem']))
405 //--------------------------------------------------------------------------------
407 if (isset($_GET['NewOrder']) || isset($_GET['NewInvoice']))
413 if (!isset($_POST['customer_id']))
414 $_POST['customer_id'] = $_SESSION['Items']->customer_id;
415 if (!isset($_POST['branch_id']))
416 $_POST['branch_id'] = $_SESSION['Items']->Branch;
417 if (!isset($_POST['OrderDate']))
418 $_POST['OrderDate'] = $_SESSION['Items']->orig_order_date;
421 //--------------------------------------------------------------------------------
423 check_db_has_stock_items(_("There are no inventory items defined in the system."));
425 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
427 if ($_SESSION['Items']->direct_invoice)
429 $idate = _("Invoice Date:");
430 $orderitems = _("Sales Invoice Items");
431 $deliverydetails = _("Enter Delivery Details and Confirm Invoice");
432 $cancelorder = _("Cancel Invoice");
436 $idate = _("Order Date:");
437 $orderitems = _("Sales Order Items");
438 $deliverydetails = _("Enter Delivery Details and Confirm Order");
439 $cancelorder = _("Cancel Order");
441 start_form(false, true);
443 $customer_error = display_order_header($_SESSION['Items'],
444 ($_SESSION['Items']->any_already_delivered() == 0), $idate);
446 if ($customer_error == "")
448 start_table("$table_style width=80%", 10);
450 display_order_summary($orderitems, $_SESSION['Items'], true);
453 display_delivery_details($_SESSION['Items']);
459 display_error($customer_error);
462 if ($_SESSION['Items']->direct_invoice)
464 $porder = _("Place Invoice");
465 $corder = _("Commit Invoice Changes");
466 $eorder = _("Edit Invoice Items");
470 $porder = _("Place Order");
471 $corder = _("Commit Order Changes");
472 $eorder = _("Edit Order Items");
475 if ($_SESSION['Items']->order_no == 0)
477 submit_center_first('ProcessOrder', $porder);
481 submit_center_first('ProcessOrder', $corder);
486 submit('EditOrder', $eorder);
487 if ($_SESSION['Items']->count_items() >= 1)
490 submit('DeliveryDetails', $deliverydetails);
494 submit_center_last('CancelOrder', $cancelorder);
498 //--------------------------------------------------------------------------------
500 if (!isset($_SESSION['Items']))
502 /* It must be a new order being created $_SESSION['Items'] would be set up from the order
503 modification code above if a modification to an existing order. */
505 session_register("Items");
507 $_SESSION['Items'] = new cart;
508 if (isset($_GET['NewInvoice']))
509 $_SESSION['Items']->direct_invoice = true;
510 $_SESSION['Items']->order_no = 0;
513 //--------------------------------------------------------------------------------