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'];
176 if ($cart->trans_type!=30) { // 2008-11-12 Joe Hunt
177 $cart->dimension_id = $_POST['dimension_id'];
178 $cart->dimension2_id = $_POST['dimension2_id'];
182 //-----------------------------------------------------------------------------
184 function copy_from_cart()
186 $cart = &$_SESSION['Items'];
187 if ($cart->trans_type!=30) {
188 $_POST['ref'] = $cart->reference;
190 $_POST['Comments'] = $cart->Comments;
192 $_POST['OrderDate'] = $cart->document_date;
193 $_POST['delivery_date'] = $cart->due_date;
194 $_POST['cust_ref'] = $cart->cust_ref;
195 $_POST['freight_cost'] = price_format($cart->freight_cost);
197 $_POST['deliver_to'] = $cart->deliver_to;
198 $_POST['delivery_address'] = $cart->delivery_address;
199 $_POST['phone'] = $cart->phone;
200 $_POST['Location'] = $cart->Location;
201 $_POST['ship_via'] = $cart->ship_via;
203 $_POST['customer_id'] = $cart->customer_id;
205 $_POST['branch_id'] = $cart->Branch;
206 $_POST['sales_type'] = $cart->sales_type;
208 if ($cart->trans_type == 10)
209 $_POST['cash'] = $cart->cash;
210 if ($cart->trans_type!=30) { // 2008-11-12 Joe Hunt
211 $_POST['dimension_id'] = $cart->dimension_id;
212 $_POST['dimension2_id'] = $cart->dimension2_id;
216 //--------------------------------------------------------------------------------
218 function line_start_focus() {
221 $Ajax->activate('items_table');
222 set_focus('_stock_id_edit');
224 //--------------------------------------------------------------------------------
226 function can_process() {
227 if (!is_date($_POST['OrderDate'])) {
228 display_error(_("The entered date is invalid."));
229 set_focus('OrderDate');
232 if ($_SESSION['Items']->trans_type!=30 && !is_date_in_fiscalyear($_POST['OrderDate'])) {
233 display_error(_("The entered date is not in fiscal year"));
234 set_focus('OrderDate');
237 if (count($_SESSION['Items']->line_items) == 0) {
238 display_error(_("You must enter at least one non empty item line."));
239 set_focus('AddItem');
242 if ($_SESSION['Items']->cash == 0) {
243 if (strlen($_POST['deliver_to']) <= 1) {
244 display_error(_("You must enter the person or company to whom delivery should be made to."));
245 set_focus('deliver_to');
249 if (strlen($_POST['delivery_address']) <= 1) {
250 display_error( _("You should enter the street address in the box provided. Orders cannot be accepted without a valid street address."));
251 set_focus('delivery_address');
255 if ($_POST['freight_cost'] == "")
256 $_POST['freight_cost'] = price_format(0);
258 if (!check_num('freight_cost',0)) {
259 display_error(_("The shipping cost entered is expected to be numeric."));
260 set_focus('freight_cost');
263 if (!is_date($_POST['delivery_date'])) {
264 display_error(_("The delivery date is invalid."));
265 set_focus('delivery_date');
268 //if (date1_greater_date2($_SESSION['Items']->document_date, $_POST['delivery_date'])) {
269 if (date1_greater_date2($_POST['OrderDate'], $_POST['delivery_date'])) {
270 display_error(_("The requested delivery date is before the date of the order."));
271 set_focus('delivery_date');
275 if ($_SESSION['Items']->trans_type != 30 && !references::is_valid($_POST['ref'])) {
276 display_error(_("You must enter a reference."));
283 //-----------------------------------------------------------------------------
285 if (isset($_POST['ProcessOrder']) && can_process()) {
288 $modified = ($_SESSION['Items']->trans_no != 0);
289 $so_type = $_SESSION['Items']->so_type;
290 $_SESSION['Items']->write(1);
291 if (count($messages)) { // abort on failure or error messages are lost
292 $Ajax->activate('_page_body');
293 display_footer_exit();
295 $trans_no = key($_SESSION['Items']->trans_no);
296 $trans_type = $_SESSION['Items']->trans_type;
300 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$trans_no");
301 } elseif ($trans_type == 30) {
302 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
303 } elseif ($trans_type == 10) {
304 meta_forward($_SERVER['PHP_SELF'], "AddedDI=$trans_no&Type=$so_type");
306 meta_forward($_SERVER['PHP_SELF'], "AddedDN=$trans_no&Type=$so_type");
310 //--------------------------------------------------------------------------------
312 function check_item_data()
314 if (!check_num('qty', 0) || !check_num('Disc', 0, 100)) {
315 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."));
318 } elseif (!check_num('price', 0)) {
319 display_error( _("Price for item must be entered and can not be less than 0"));
322 } elseif (isset($_POST['LineNo']) && isset($_SESSION['Items']->line_items[$_POST['LineNo']])
323 && !check_num('qty', $_SESSION['Items']->line_items[$_POST['LineNo']]->qty_done)) {
326 display_error(_("You attempting to make the quantity ordered a quantity less than has already been delivered. The quantity delivered cannot be modified retrospectively."));
328 } // Joe Hunt added 2008-09-22 -------------------------
329 elseif ($_SESSION['Items']->trans_type!=30 && !sys_prefs::allow_negative_stock() &&
330 is_inventory_item($_POST['stock_id']))
332 $qoh = get_qoh_on_date($_POST['stock_id'], $_POST['Location'], $_POST['OrderDate']);
333 if (input_num('qty') > $qoh)
335 $stock = get_item($_POST['stock_id']);
336 display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:") .
337 " " . $stock['stock_id'] . " - " . $stock['description'] . " - " .
338 _("Quantity On Hand") . " = " . number_format2($qoh, get_qty_dec($_POST['stock_id'])));
346 //--------------------------------------------------------------------------------
348 function handle_update_item()
350 if ($_POST['UpdateItem'] != '' && check_item_data()) {
351 $_SESSION['Items']->update_cart_item($_POST['LineNo'],
352 input_num('qty'), input_num('price'),
353 input_num('Disc') / 100 );
358 //--------------------------------------------------------------------------------
360 function handle_delete_item($line_no)
362 if ($_SESSION['Items']->some_already_delivered($line_no) == 0) {
363 $_SESSION['Items']->remove_from_cart($line_no);
365 display_error(_("This item cannot be deleted because some of it has already been delivered."));
370 //--------------------------------------------------------------------------------
372 function handle_new_item()
375 if (!check_item_data()) {
378 add_to_order($_SESSION['Items'], $_POST['stock_id'], input_num('qty'),
379 input_num('price'), input_num('Disc') / 100);
380 $_POST['_stock_id_edit'] = $_POST['stock_id'] = "";
384 //--------------------------------------------------------------------------------
386 function handle_cancel_order()
388 global $path_to_root, $Ajax;
391 if ($_SESSION['Items']->trans_type == 13) {
392 display_note(_("Direct delivery entry has been cancelled as requested."), 1);
393 hyperlink_params($path_to_root . "/sales/sales_order_entry.php",
394 _("Enter a New Sales Delivery"), SID . "&NewDelivery=0");
395 } elseif ($_SESSION['Items']->trans_type == 10) {
396 display_note(_("Direct invoice entry has been cancelled as requested."), 1);
397 hyperlink_params($path_to_root . "/sales/sales_order_entry.php",
398 _("Enter a New Sales Delivery"), SID . "&NewDelivery=0");
400 if ($_SESSION['Items']->trans_no != 0) {
401 if (sales_order_has_deliveries(key($_SESSION['Items']->trans_no)))
402 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."));
404 delete_sales_order(key($_SESSION['Items']->trans_no));
406 display_note(_("This sales order has been cancelled as requested."), 1);
407 hyperlink_params($path_to_root . "/sales/sales_order_entry.php",
408 _("Enter a New Sales Order"), SID . "&NewOrder=Yes");
412 meta_forward($path_to_root.'/index.php?application=orders');
415 $Ajax->activate('_page_body');
422 //--------------------------------------------------------------------------------
424 function create_cart($type, $trans_no)
429 if($type != 30 && $trans_no != 0) { // this is template
432 $doc = new Cart(30, array($trans_no));
433 $doc->trans_type = $type;
435 $doc->document_date = Today(); // 2006-06-15. Added so Invoices and Deliveries get current day
437 $doc->due_date = get_invoice_duedate($doc->customer_id, $doc->document_date);
438 $doc->pos = user_pos();
439 $pos = get_sales_point($doc->pos);
440 $doc->cash = $pos['cash_sale'];
441 if (!$pos['cash_sale'] || !$pos['credit_sale'])
442 $doc->pos = -1; // mark not editable payment type
444 $doc->cash = date_diff($doc->due_date, Today(), 'd')<2;
446 $doc->due_date = $doc->document_date;
447 $doc->reference = references::get_next($doc->trans_type);
449 foreach($doc->line_items as $line_no => $line) {
450 $doc->line_items[$line_no]->qty_done = 0;
452 $_SESSION['Items'] = $doc;
454 $_SESSION['Items'] = new Cart($type,array($trans_no));
458 //--------------------------------------------------------------------------------
460 if (isset($_POST['CancelOrder']))
461 handle_cancel_order();
463 $id = find_submit('Delete');
465 handle_delete_item($id);
467 if (isset($_POST['UpdateItem']))
468 handle_update_item();
470 if (isset($_POST['AddItem']))
473 if (isset($_POST['CancelItemChanges'])) {
477 //--------------------------------------------------------------------------------
478 check_db_has_stock_items(_("There are no inventory items defined in the system."));
480 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
482 if ($_SESSION['Items']->trans_type == 10) {
483 $idate = _("Invoice Date:");
484 $orderitems = _("Sales Invoice Items");
485 $deliverydetails = _("Enter Delivery Details and Confirm Invoice");
486 $cancelorder = _("Cancel Invoice");
487 $porder = _("Place Invoice");
488 } elseif ($_SESSION['Items']->trans_type == 13) {
489 $idate = _("Delivery Date:");
490 $orderitems = _("Delivery Note Items");
491 $deliverydetails = _("Enter Delivery Details and Confirm Dispatch");
492 $cancelorder = _("Cancel Delivery");
493 $porder = _("Place Delivery");
495 $idate = _("Order Date:");
496 $orderitems = _("Sales Order Items");
497 $deliverydetails = _("Enter Delivery Details and Confirm Order");
498 $cancelorder = _("Cancel Order");
499 $porder = _("Place Order");
500 $corder = _("Commit Order Changes");
502 start_form(false, true);
504 $customer_error = display_order_header($_SESSION['Items'],
505 ($_SESSION['Items']->any_already_delivered() == 0), $idate);
507 if ($customer_error == "") {
508 start_table("$table_style width=80%", 10);
510 display_order_summary($orderitems, $_SESSION['Items'], true);
513 display_delivery_details($_SESSION['Items']);
517 if ($_SESSION['Items']->trans_no == 0) {
519 submit_center_first('ProcessOrder', $porder,
520 _('Check entered data and save document'), true, ICON_OK);
522 submit_center_first('ProcessOrder', $corder,
523 _('Validate changes and update document'), true, ICON_OK);
526 submit_center_last('CancelOrder', $cancelorder,
527 _('Cancels document entry or removes sales order when editing an old document'),
530 display_error($customer_error);