2 //-----------------------------------------------------------------------------
4 // Entry/Modify Sales Order
5 // Entry Direct Delivery
6 // Entry Direct Invoice
12 include_once($path_to_root . "/sales/includes/cart_class.inc");
13 include_once($path_to_root . "/includes/session.inc");
14 include_once($path_to_root . "/sales/includes/sales_ui.inc");
15 include_once($path_to_root . "/sales/includes/ui/sales_order_ui.inc");
16 include_once($path_to_root . "/sales/includes/sales_db.inc");
17 include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
18 include_once($path_to_root . "/reporting/includes/reporting.inc");
21 if ($ret = context_restore()) {
22 // return from new customer add
24 if(isset($ret['customer_id']))
25 $_POST['customer_id'] = $ret['customer_id'];
26 if(isset($ret['branch_id']))
27 $_POST['branch_id'] = $ret['branch_id'];
29 if (isset($_POST['_customer_id_editor'])) {
30 copy_to_cart(); //store context
31 context_call($path_to_root.'/sales/manage/customers.php?debtor_no='.$_POST['customer_id'], 'Items');
34 if ($use_popup_windows) {
35 $js .= get_js_open_window(900, 500);
38 if ($use_date_picker) {
39 $js .= get_js_date_picker();
42 if (isset($_GET['NewDelivery']) && is_numeric($_GET['NewDelivery'])) {
44 $_SESSION['page_title'] = _("Direct Sales Delivery");
45 create_cart(13, $_GET['NewDelivery']);
47 } elseif (isset($_GET['NewInvoice']) && is_numeric($_GET['NewInvoice'])) {
49 $_SESSION['page_title'] = _("Direct Sales Invoice");
50 create_cart(10, $_GET['NewInvoice']);
52 } elseif (isset($_GET['ModifyOrderNumber']) && is_numeric($_GET['ModifyOrderNumber'])) {
54 $help_page_title = _('Modifying Sales Order');
55 $_SESSION['page_title'] = sprintf( _("Modifying Sales Order # %d"), $_GET['ModifyOrderNumber']);
56 create_cart(30, $_GET['ModifyOrderNumber']);
58 } elseif (isset($_GET['NewOrder'])) {
60 $_SESSION['page_title'] = _("New Sales Order Entry");
64 page($_SESSION['page_title'], false, false, "", $js);
65 //-----------------------------------------------------------------------------
67 if (isset($_GET['AddedID'])) {
68 $order_no = $_GET['AddedID'];
70 display_notification_centered(sprintf( _("Order # %d has been entered."),$order_no));
72 display_note(get_trans_view_str(30, $order_no, _("&View This Order")));
74 display_note(print_document_link($order_no, _("&Print This Order"), true, 30));
76 hyperlink_params($path_to_root . "/sales/customer_delivery.php",
77 _("Make &Delivery Against This Order"), "OrderNumber=$order_no");
79 hyperlink_params($_SERVER['PHP_SELF'], _("Enter a &New Order"), "NewOrder=0");
81 display_footer_exit();
83 } elseif (isset($_GET['UpdatedID'])) {
84 $order_no = $_GET['UpdatedID'];
86 display_notification_centered(sprintf( _("Order # %d has been updated."),$order_no));
88 display_note(get_trans_view_str(30, $order_no, _("&View This Order")));
90 display_note(print_document_link($order_no, _("&Print This Order"), true, 30));
92 hyperlink_params($path_to_root . "/sales/customer_delivery.php",
93 _("Confirm Order Quantities and Make &Delivery"), "OrderNumber=$order_no");
95 hyperlink_params($path_to_root . "/sales/inquiry/sales_orders_view.php",
96 _("Select A Different &Order"), "OutstandingOnly=1");
98 display_footer_exit();
100 } elseif (isset($_GET['AddedDN'])) {
101 $delivery = $_GET['AddedDN'];
103 display_notification_centered(sprintf(_("Delivery # %d has been entered."),$delivery));
105 display_note(get_trans_view_str(13, $delivery, _("&View This Delivery")));
107 display_note(print_document_link($delivery, _("&Print Delivery Note"), true, 13));
109 hyperlink_params($path_to_root . "/sales/customer_invoice.php",
110 _("Make &Invoice Against This Delivery"), "DeliveryNumber=$delivery");
112 if ((isset($_GET['Type']) && $_GET['Type'] == 1))
113 hyperlink_params("inquiry/sales_orders_view.php",
114 _("Enter a New Template &Delivery"), "DeliveryTemplates=Yes");
116 hyperlink_params($_SERVER['PHP_SELF'], _("Enter a &New Delivery"), "NewDelivery=0");
118 display_footer_exit();
120 } elseif (isset($_GET['AddedDI'])) {
121 $invoice = $_GET['AddedDI'];
123 display_notification_centered(sprintf(_("Invoice # %d has been entered."),$invoice));
125 display_note(get_trans_view_str(10, $invoice, _("&View This Invoice")));
127 display_note(print_document_link($invoice, _("&Print Sales Invoice"), true, 10));
129 if ((isset($_GET['Type']) && $_GET['Type'] == 1))
130 hyperlink_params("inquiry/sales_orders_view.php",
131 _("Enter a &New Template Invoice"), "InvoiceTemplates=Yes");
133 hyperlink_params($_SERVER['PHP_SELF'], _("Enter a &New Direct Invoice"), "NewInvoice=0");
135 display_footer_exit();
138 //-----------------------------------------------------------------------------
140 function copy_to_cart()
142 $cart = &$_SESSION['Items'];
144 if ($cart->trans_type!=30) {
145 $cart->reference = $_POST['ref'];
147 $cart->Comments = $_POST['Comments'];
149 $cart->document_date = $_POST['OrderDate'];
150 if ($cart->trans_type == 10)
151 $cart->cash = $_POST['cash'];
153 $cart->due_date = $cart->document_date;
154 $cart->phone = $cart->cust_ref = $cart->delivery_address = '';
155 $cart->freight_cost = 0;
157 $cart->deliver_to = '';//$_POST['deliver_to'];
159 $cart->due_date = $_POST['delivery_date'];
160 $cart->cust_ref = $_POST['cust_ref'];
161 $cart->freight_cost = input_num('freight_cost');
162 $cart->deliver_to = $_POST['deliver_to'];
163 $cart->delivery_address = $_POST['delivery_address'];
164 $cart->phone = $_POST['phone'];
165 $cart->Location = $_POST['Location'];
166 $cart->ship_via = $_POST['ship_via'];
168 if (isset($_POST['email']))
169 $cart->email =$_POST['email'];
172 $cart->customer_id = $_POST['customer_id'];
173 $cart->Branch = $_POST['branch_id'];
174 $cart->sales_type = $_POST['sales_type'];
178 //-----------------------------------------------------------------------------
180 function copy_from_cart()
182 $cart = &$_SESSION['Items'];
183 if ($cart->trans_type!=30) {
184 $_POST['ref'] = $cart->reference;
186 $_POST['Comments'] = $cart->Comments;
188 $_POST['OrderDate'] = $cart->document_date;
189 $_POST['delivery_date'] = $cart->due_date;
190 $_POST['cust_ref'] = $cart->cust_ref;
191 $_POST['freight_cost'] = price_format($cart->freight_cost);
193 $_POST['deliver_to'] = $cart->deliver_to;
194 $_POST['delivery_address'] = $cart->delivery_address;
195 $_POST['phone'] = $cart->phone;
196 $_POST['Location'] = $cart->Location;
197 $_POST['ship_via'] = $cart->ship_via;
199 $_POST['customer_id'] = $cart->customer_id;
201 $_POST['branch_id'] = $cart->Branch;
202 $_POST['sales_type'] = $cart->sales_type;
204 if ($cart->trans_type == 10)
205 $_POST['cash'] = $cart->cash;
207 //--------------------------------------------------------------------------------
209 function line_start_focus() {
212 $Ajax->activate('items_table');
213 set_focus('_stock_id_edit');
215 //--------------------------------------------------------------------------------
217 function can_process() {
218 if (!is_date($_POST['OrderDate'])) {
219 display_error(_("The entered date is invalid."));
220 set_focus('OrderDate');
223 if ($_SESSION['Items']->trans_type!=30 && !is_date_in_fiscalyear($_POST['OrderDate'])) {
224 display_error(_("The entered date is not in fiscal year"));
225 set_focus('OrderDate');
228 if (count($_SESSION['Items']->line_items) == 0) {
229 display_error(_("You must enter at least one non empty item line."));
230 set_focus('AddItem');
233 if ($_SESSION['Items']->cash == 0) {
234 if (strlen($_POST['deliver_to']) <= 1) {
235 display_error(_("You must enter the person or company to whom delivery should be made to."));
236 set_focus('deliver_to');
240 if (strlen($_POST['delivery_address']) <= 1) {
241 display_error( _("You should enter the street address in the box provided. Orders cannot be accepted without a valid street address."));
242 set_focus('delivery_address');
246 if ($_POST['freight_cost'] == "")
247 $_POST['freight_cost'] = price_format(0);
249 if (!check_num('freight_cost',0)) {
250 display_error(_("The shipping cost entered is expected to be numeric."));
251 set_focus('freight_cost');
254 if (!is_date($_POST['delivery_date'])) {
255 display_error(_("The delivery date is invalid."));
256 set_focus('delivery_date');
259 //if (date1_greater_date2($_SESSION['Items']->document_date, $_POST['delivery_date'])) {
260 if (date1_greater_date2($_POST['OrderDate'], $_POST['delivery_date'])) {
261 display_error(_("The requested delivery date is before the date of the order."));
262 set_focus('delivery_date');
266 if ($_SESSION['Items']->trans_type != 30 && !references::is_valid($_POST['ref'])) {
267 display_error(_("You must enter a reference."));
274 //-----------------------------------------------------------------------------
276 if (isset($_POST['ProcessOrder']) && can_process()) {
279 $modified = ($_SESSION['Items']->trans_no != 0);
280 $so_type = $_SESSION['Items']->so_type;
282 $_SESSION['Items']->write(1);
283 if (count($messages)) { // abort on failure or error messages are lost
284 $Ajax->activate('_page_body');
285 display_footer_exit();
287 $trans_no = key($_SESSION['Items']->trans_no);
288 $trans_type = $_SESSION['Items']->trans_type;
292 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$trans_no");
293 } elseif ($trans_type == 30) {
294 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
295 } elseif ($trans_type == 10) {
296 meta_forward($_SERVER['PHP_SELF'], "AddedDI=$trans_no&Type=$so_type");
298 meta_forward($_SERVER['PHP_SELF'], "AddedDN=$trans_no&Type=$so_type");
302 //--------------------------------------------------------------------------------
304 function check_item_data()
306 if (!check_num('qty', 0) || !check_num('Disc', 0, 100)) {
307 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."));
310 } elseif (!check_num('price', 0)) {
311 display_error( _("Price for item must be entered and can not be less than 0"));
314 } elseif (isset($_POST['LineNo']) && isset($_SESSION['Items']->line_items[$_POST['LineNo']])
315 && !check_num('qty', $_SESSION['Items']->line_items[$_POST['LineNo']]->qty_done)) {
318 display_error(_("You attempting to make the quantity ordered a quantity less than has already been delivered. The quantity delivered cannot be modified retrospectively."));
320 } // Joe Hunt added 2008-09-22 -------------------------
321 elseif ($_SESSION['Items']->trans_type!=30 && !sys_prefs::allow_negative_stock() &&
322 is_inventory_item($_POST['stock_id']))
324 $qoh = get_qoh_on_date($_POST['stock_id'], $_POST['Location'], $_POST['OrderDate']);
325 if (input_num('qty') > $qoh)
327 $stock = get_item($_POST['stock_id']);
328 display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:") .
329 " " . $stock['stock_id'] . " - " . $stock['description'] . " - " .
330 _("Quantity On Hand") . " = " . number_format2($qoh, get_qty_dec($_POST['stock_id'])));
338 //--------------------------------------------------------------------------------
340 function handle_update_item()
342 if ($_POST['UpdateItem'] != '' && check_item_data()) {
343 $_SESSION['Items']->update_cart_item($_POST['LineNo'],
344 input_num('qty'), input_num('price'),
345 input_num('Disc') / 100 );
350 //--------------------------------------------------------------------------------
352 function handle_delete_item($line_no)
354 if ($_SESSION['Items']->some_already_delivered($line_no) == 0) {
355 $_SESSION['Items']->remove_from_cart($line_no);
357 display_error(_("This item cannot be deleted because some of it has already been delivered."));
362 //--------------------------------------------------------------------------------
364 function handle_new_item()
367 if (!check_item_data()) {
370 add_to_order($_SESSION['Items'], $_POST['stock_id'], input_num('qty'),
371 input_num('price'), input_num('Disc') / 100);
372 $_POST['_stock_id_edit'] = $_POST['stock_id'] = "";
376 //--------------------------------------------------------------------------------
378 function handle_cancel_order()
380 global $path_to_root, $Ajax;
383 if ($_SESSION['Items']->trans_type == 13) {
384 display_note(_("Direct delivery entry has been cancelled as requested."), 1);
385 hyperlink_params($path_to_root . "/sales/sales_order_entry.php",
386 _("Enter a New Sales Delivery"), SID . "&NewDelivery=0");
387 } elseif ($_SESSION['Items']->trans_type == 10) {
388 display_note(_("Direct invoice entry has been cancelled as requested."), 1);
389 hyperlink_params($path_to_root . "/sales/sales_order_entry.php",
390 _("Enter a New Sales Delivery"), SID . "&NewDelivery=0");
392 if ($_SESSION['Items']->trans_no != 0) {
393 if (sales_order_has_deliveries(key($_SESSION['Items']->trans_no)))
394 display_error(_("This order cannot be cancelled because some of it has already been invoiced or dispatched. However, the line item quantities may be modified."));
396 delete_sales_order(key($_SESSION['Items']->trans_no));
398 display_note(_("This sales order has been cancelled as requested."), 1);
399 hyperlink_params($path_to_root . "/sales/sales_order_entry.php",
400 _("Enter a New Sales Order"), SID . "&NewOrder=Yes");
404 meta_forward($path_to_root.'/index.php?application=orders');
407 $Ajax->activate('_page_body');
414 //--------------------------------------------------------------------------------
416 function create_cart($type, $trans_no)
421 if($type != 30 && $trans_no != 0) { // this is template
424 $doc = new Cart(30, array($trans_no));
425 $doc->trans_type = $type;
427 $doc->document_date = Today(); // 2006-06-15. Added so Invoices and Deliveries get current day
429 $doc->due_date = get_invoice_duedate($doc->customer_id, $doc->document_date);
430 $doc->pos = user_pos();
431 $pos = get_sales_point($doc->pos);
432 $doc->cash = $pos['cash_sale'];
433 if (!$pos['cash_sale'] || !$pos['credit_sale'])
434 $doc->pos = -1; // mark not editable payment type
436 $doc->cash = date_diff($doc->due_date, Today(), 'd')<2;
438 $doc->due_date = $doc->document_date;
439 $doc->reference = references::get_next($doc->trans_type);
441 foreach($doc->line_items as $line_no => $line) {
442 $doc->line_items[$line_no]->qty_done = 0;
444 $_SESSION['Items'] = $doc;
446 $_SESSION['Items'] = new Cart($type,array($trans_no));
450 //--------------------------------------------------------------------------------
452 if (isset($_POST['CancelOrder']))
453 handle_cancel_order();
455 $id = find_submit('Delete');
457 handle_delete_item($id);
459 if (isset($_POST['UpdateItem']))
460 handle_update_item();
462 if (isset($_POST['AddItem']))
465 if (isset($_POST['CancelItemChanges'])) {
469 //--------------------------------------------------------------------------------
470 check_db_has_stock_items(_("There are no inventory items defined in the system."));
472 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
474 if ($_SESSION['Items']->trans_type == 10) {
475 $idate = _("Invoice Date:");
476 $orderitems = _("Sales Invoice Items");
477 $deliverydetails = _("Enter Delivery Details and Confirm Invoice");
478 $cancelorder = _("Cancel Invoice");
479 $porder = _("Place Invoice");
480 } elseif ($_SESSION['Items']->trans_type == 13) {
481 $idate = _("Delivery Date:");
482 $orderitems = _("Delivery Note Items");
483 $deliverydetails = _("Enter Delivery Details and Confirm Dispatch");
484 $cancelorder = _("Cancel Delivery");
485 $porder = _("Place Delivery");
487 $idate = _("Order Date:");
488 $orderitems = _("Sales Order Items");
489 $deliverydetails = _("Enter Delivery Details and Confirm Order");
490 $cancelorder = _("Cancel Order");
491 $porder = _("Place Order");
492 $corder = _("Commit Order Changes");
494 start_form(false, true);
496 $customer_error = display_order_header($_SESSION['Items'],
497 ($_SESSION['Items']->any_already_delivered() == 0), $idate);
499 if ($customer_error == "") {
500 start_table("$table_style width=80%", 10);
502 display_order_summary($orderitems, $_SESSION['Items'], true);
505 display_delivery_details($_SESSION['Items']);
509 if ($_SESSION['Items']->trans_no == 0) {
511 submit_center_first('ProcessOrder', $porder,
512 _('Check entered data and save document'), true, 'confirm.png');
514 submit_center_first('ProcessOrder', $corder,
515 _('Validate changes and update document'), true, 'confirm.png');
518 submit_center_last('CancelOrder', $cancelorder,
519 _('Cancels document entry or removes sales order when editing an old document'),
522 display_error($customer_error);