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']->memo_ = $_POST['InvoiceText'];
199 $_SESSION['Items']->memo_ = str_replace("'", "\\'", $_SESSION['Items']->memo_);
200 $_SESSION['Items']->order_no = $order_no;
201 meta_forward("$path_to_root/sales/customer_invoice.php", "process_invoice=Yes");
207 unset($_SESSION['Items']->line_items);
208 unset($_SESSION['Items']);
210 meta_forward($_SERVER['PHP_SELF'], "AddedID=$order_no");
214 //-------------------------------------------------------------------------------------------------------------
216 if (isset($_POST['ProcessOrder']) && $_SESSION['Items']->order_no != 0 && can_process())
219 update_sales_order($_SESSION['Items']->order_no, $_SESSION['Items']);
221 $order_no = $_SESSION['Items']->order_no;
223 unset($_SESSION['Items']->line_items);
224 unset($_SESSION['Items']);
226 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$order_no");
230 //--------------------------------------------------------------------------------
232 function check_item_data()
234 if (!is_numeric($_POST['qty']) || $_POST['qty'] < 0 || $_POST['Disc'] > 100 ||
237 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."));
240 elseif($_SESSION['Items']->some_already_delivered($_POST['stock_id']) != 0 &&
241 $_SESSION['Items']->line_items[$_POST['stock_id']]->price != $_POST['price'])
243 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."));
246 elseif($_SESSION['Items']->some_already_delivered($_POST['stock_id']) != 0 &&
247 $_SESSION['Items']->line_items[$_POST['stock_id']]->discount_percent != ($_POST['Disc']/100))
249 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."));
252 elseif (isset($_SESSION['Items']->line_items[$_POST['stock_id']]) && $_SESSION['Items']->line_items[$_POST['stock_id']]->qty_inv > $_POST['qty'])
254 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."));
260 function handle_update_item()
262 if($_POST['UpdateItem'] != "" && check_item_data())
264 $_SESSION['Items']->update_cart_item($_POST['stock_id'], $_POST['qty'],
265 $_POST['price'], ($_POST['Disc'] / 100));
269 //--------------------------------------------------------------------------------
271 function handle_delete_item()
273 if($_GET['Delete'] != "")
275 $stock_id = $_GET['Delete'];
276 if($_SESSION['Items']->some_already_delivered($stock_id) == 0)
278 $_SESSION['Items']->remove_from_cart($stock_id);
282 display_error(_("This item cannot be deleted because some of it has already been invoiced."));
287 //--------------------------------------------------------------------------------
289 function handle_new_item()
291 if (!check_item_data())
294 add_to_order($_SESSION['Items'], $_POST['stock_id'], $_POST['qty'],
295 $_POST['price'], $_POST['Disc']/100);
296 $_POST['StockID2'] = $_POST['stock_id'] = "";
299 //--------------------------------------------------------------------------------
301 function handle_cancel_order()
303 global $path_to_root;
305 if ($_POST['CancelOrder'] != "")
307 $ok_to_delete = 1; //assume this in the first instance
309 if (($_SESSION['Items']->order_no != 0) &&
310 sales_order_has_invoices($_SESSION['Items']->order_no))
313 display_error(_("This order cannot be cancelled because some of it has already been invoiced. However, the line item quantities may be modified."));
316 if ($ok_to_delete == 1)
318 if($_SESSION['Items']->order_no != 0)
320 delete_sales_order($_SESSION['Items']->order_no);
322 $diriv = $_SESSION['Items']->direct_invoice;
323 $_SESSION['Items']->clear_items();
324 $_SESSION['Items'] = new cart;
325 $_SESSION['Items']->direct_invoice = $diriv;
328 display_note(_("This sales invoice has been cancelled as requested."), 1);
329 hyperlink_params($path_to_root . "/sales/sales_order_entry.php", _("Enter a New Sales Invoice"), SID . "&NewInvoice=Yes");
333 display_note(_("This sales order has been cancelled as requested."), 1);
334 hyperlink_params($path_to_root . "/sales/sales_order_entry.php", _("Enter a New Sales Order"), SID . "&NewOrder=Yes");
343 //--------------------------------------------------------------------------------
345 function handle_new_order()
347 /*New order entry - clear any existing order details from the Items object and initiate a newy*/
348 if (isset($_SESSION['Items']))
350 unset ($_SESSION['Items']->line_items);
351 unset ($_SESSION['Items']);
354 session_register("Items");
356 $_SESSION['Items'] = new cart;
357 if (isset($_GET['NewInvoice']))
358 $_SESSION['Items']->direct_invoice = true;
359 $_SESSION['Items']->customer_id = "";
360 $_POST['OrderDate'] = Today();
361 if (!is_date_in_fiscalyear($_POST['OrderDate']))
362 $_POST['OrderDate'] = end_fiscalyear();
363 $_SESSION['Items']->orig_order_date = $_POST['OrderDate'];
366 //--------------------------------------------------------------------------------
368 if (isset($_GET['ModifyOrderNumber']) && $_GET['ModifyOrderNumber'] != "")
371 if (isset($_SESSION['Items']))
373 unset ($_SESSION['Items']->line_items);
374 unset ($_SESSION['Items']);
377 session_register("Items");
379 $_SESSION['Items'] = new cart;
380 $_SESSION['Items']->order_no = $_GET['ModifyOrderNumber'];
382 /*read in all the selected order into the Items cart */
384 read_sales_order($_SESSION['Items']->order_no, $_SESSION['Items']);
387 //--------------------------------------------------------------------------------
389 if (isset($_POST['CancelOrder']))
390 handle_cancel_order();
392 if (isset($_GET['Delete']) || isset($_GET['Edit']))
395 if (isset($_GET['Delete']))
396 handle_delete_item();
398 if (isset($_POST['UpdateItem']) || isset($_POST['AddItem']))
401 if (isset($_POST['UpdateItem']))
402 handle_update_item();
404 if (isset($_POST['AddItem']))
407 //--------------------------------------------------------------------------------
409 if (isset($_GET['NewOrder']) || isset($_GET['NewInvoice']))
415 if (!isset($_POST['customer_id']))
416 $_POST['customer_id'] = $_SESSION['Items']->customer_id;
417 if (!isset($_POST['branch_id']))
418 $_POST['branch_id'] = $_SESSION['Items']->Branch;
419 if (!isset($_POST['OrderDate']))
420 $_POST['OrderDate'] = $_SESSION['Items']->orig_order_date;
423 //--------------------------------------------------------------------------------
425 check_db_has_stock_items(_("There are no inventory items defined in the system."));
427 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
429 if ($_SESSION['Items']->direct_invoice)
431 $idate = _("Invoice Date:");
432 $orderitems = _("Sales Invoice Items");
433 $deliverydetails = _("Enter Delivery Details and Confirm Invoice");
434 $cancelorder = _("Cancel Invoice");
438 $idate = _("Order Date:");
439 $orderitems = _("Sales Order Items");
440 $deliverydetails = _("Enter Delivery Details and Confirm Order");
441 $cancelorder = _("Cancel Order");
443 start_form(false, true);
445 $customer_error = display_order_header($_SESSION['Items'],
446 ($_SESSION['Items']->any_already_delivered() == 0), $idate);
448 if ($customer_error == "")
450 start_table("$table_style width=80%", 10);
452 display_order_summary($orderitems, $_SESSION['Items'], true);
455 display_delivery_details($_SESSION['Items']);
461 display_error($customer_error);
464 if ($_SESSION['Items']->direct_invoice)
466 $porder = _("Place Invoice");
467 $corder = _("Commit Invoice Changes");
468 $eorder = _("Edit Invoice Items");
472 $porder = _("Place Order");
473 $corder = _("Commit Order Changes");
474 $eorder = _("Edit Order Items");
477 if ($_SESSION['Items']->order_no == 0)
479 submit_center_first('ProcessOrder', $porder);
483 submit_center_first('ProcessOrder', $corder);
488 submit('EditOrder', $eorder);
489 if ($_SESSION['Items']->count_items() >= 1)
492 submit('DeliveryDetails', $deliverydetails);
496 submit_center_last('CancelOrder', $cancelorder);
500 //--------------------------------------------------------------------------------
502 if (!isset($_SESSION['Items']))
504 /* It must be a new order being created $_SESSION['Items'] would be set up from the order
505 modification code above if a modification to an existing order. */
507 session_register("Items");
509 $_SESSION['Items'] = new cart;
510 if (isset($_GET['NewInvoice']))
511 $_SESSION['Items']->direct_invoice = true;
512 $_SESSION['Items']->order_no = 0;
515 //--------------------------------------------------------------------------------