2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //-----------------------------------------------------------------------------
14 // Entry/Modify Sales Order
15 // Entry Direct Delivery
16 // Entry Direct Invoice
22 include_once($path_to_root . "/sales/includes/cart_class.inc");
23 include_once($path_to_root . "/includes/session.inc");
24 include_once($path_to_root . "/sales/includes/sales_ui.inc");
25 include_once($path_to_root . "/sales/includes/ui/sales_order_ui.inc");
26 include_once($path_to_root . "/sales/includes/sales_db.inc");
27 include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
28 include_once($path_to_root . "/reporting/includes/reporting.inc");
31 if ($use_popup_windows) {
32 $js .= get_js_open_window(900, 500);
35 if ($use_date_picker) {
36 $js .= get_js_date_picker();
39 if (isset($_GET['NewDelivery']) && is_numeric($_GET['NewDelivery'])) {
41 $_SESSION['page_title'] = _("Direct Sales Delivery");
42 create_cart(13, $_GET['NewDelivery']);
44 } elseif (isset($_GET['NewInvoice']) && is_numeric($_GET['NewInvoice'])) {
46 $_SESSION['page_title'] = _("Direct Sales Invoice");
47 create_cart(10, $_GET['NewInvoice']);
49 } elseif (isset($_GET['ModifyOrderNumber']) && is_numeric($_GET['ModifyOrderNumber'])) {
51 $help_page_title = _('Modifying Sales Order');
52 $_SESSION['page_title'] = sprintf( _("Modifying Sales Order # %d"), $_GET['ModifyOrderNumber']);
53 create_cart(30, $_GET['ModifyOrderNumber']);
55 } elseif (isset($_GET['NewOrder'])) {
57 $_SESSION['page_title'] = _("New Sales Order Entry");
61 page($_SESSION['page_title'], false, false, "", $js);
62 //-----------------------------------------------------------------------------
63 set_editor('customer', 'customer_id', 'sales_type');
64 set_editor('branch', 'branch_id', 'sales_type');
66 if (list_updated('branch_id')) {
67 // when branch is selected via external editor also customer can change
68 $br = get_branch(get_post('branch_id'));
69 $_POST['customer_id'] = $br['debtor_no'];
70 $Ajax->activate('customer_id');
73 if (isset($_GET['AddedID'])) {
74 $order_no = $_GET['AddedID'];
75 display_notification_centered(sprintf( _("Order # %d has been entered."),$order_no));
77 submenu_view(_("&View This Order"), 30, $order_no);
79 submenu_print(_("&Print This Order"), 30, $order_no, 'prtopt');
82 submenu_option(_("Make &Delivery Against This Order"),
83 "/sales/customer_delivery.php?OrderNumber=$order_no");
85 submenu_option(_("Enter a &New Order"), "/sales/sales_order_entry.php?NewOrder=0");
87 display_footer_exit();
89 } elseif (isset($_GET['UpdatedID'])) {
90 $order_no = $_GET['UpdatedID'];
92 display_notification_centered(sprintf( _("Order # %d has been updated."),$order_no));
94 submenu_view(_("&View This Order"), 30, $order_no);
96 submenu_print(_("&Print This Order"), 30, $order_no, 'prtopt');
99 submenu_option(_("Confirm Order Quantities and Make &Delivery"),
100 "/sales/customer_delivery.php?OrderNumber=$order_no");
102 submenu_option(_("Select A Different &Order"),
103 "/sales/inquiry/sales_orders_view.php?OutstandingOnly=1");
105 display_footer_exit();
107 } elseif (isset($_GET['AddedDN'])) {
108 $delivery = $_GET['AddedDN'];
110 display_notification_centered(sprintf(_("Delivery # %d has been entered."),$delivery));
112 submenu_view(_("&View This Delivery"), 13, $delivery);
114 submenu_print(_("&Print Delivery Note"), 13, $delivery, 'prtopt');
117 display_note(get_gl_view_str(13, $delivery, _("View the GL Journal Entries for this Dispatch")),0, 1);
119 submenu_option(_("Make &Invoice Against This Delivery"),
120 "/sales/customer_invoice.php?DeliveryNumber=$delivery");
122 if ((isset($_GET['Type']) && $_GET['Type'] == 1))
123 submenu_option(_("Enter a New Template &Delivery"),
124 "/sales/inquiry/sales_orders_view.php?DeliveryTemplates=Yes");
126 submenu_option(_("Enter a &New Delivery"),
127 "/sales/sales_order_entry.php?NewDelivery=0");
129 display_footer_exit();
131 } elseif (isset($_GET['AddedDI'])) {
132 $invoice = $_GET['AddedDI'];
134 display_notification_centered(sprintf(_("Invoice # %d has been entered."), $invoice));
136 submenu_view(_("&View This Invoice"), 10, $invoice);
138 submenu_print(_("&Print Sales Invoice"), 10, $invoice, 'prtopt');
141 display_note(get_gl_view_str(10, $invoice, _("View the GL &Journal Entries for this Invoice")),0, 1);
143 if ((isset($_GET['Type']) && $_GET['Type'] == 1))
144 submenu_option(_("Enter a &New Template Invoice"),
145 "/sales/inquiry/sales_orders_view.php?InvoiceTemplates=Yes");
147 submenu_option(_("Enter a &New Direct Invoice"),
148 "/sales/sales_order_entry.php?NewInvoice=0");
150 display_footer_exit();
152 check_edit_conflicts();
153 //-----------------------------------------------------------------------------
155 function copy_to_cart()
157 $cart = &$_SESSION['Items'];
159 if ($cart->trans_type!=30) {
160 $cart->reference = $_POST['ref'];
162 $cart->Comments = $_POST['Comments'];
164 $cart->document_date = $_POST['OrderDate'];
165 if ($cart->trans_type == 10)
166 $cart->cash = $_POST['cash'];
168 $cart->due_date = $cart->document_date;
169 $cart->phone = $cart->cust_ref = $cart->delivery_address = '';
170 $cart->freight_cost = 0;
172 $cart->deliver_to = '';//$_POST['deliver_to'];
174 $cart->due_date = $_POST['delivery_date'];
175 $cart->cust_ref = $_POST['cust_ref'];
176 $cart->freight_cost = input_num('freight_cost');
177 $cart->deliver_to = $_POST['deliver_to'];
178 $cart->delivery_address = $_POST['delivery_address'];
179 $cart->phone = $_POST['phone'];
180 $cart->Location = $_POST['Location'];
181 $cart->ship_via = $_POST['ship_via'];
183 if (isset($_POST['email']))
184 $cart->email =$_POST['email'];
187 $cart->customer_id = $_POST['customer_id'];
188 $cart->Branch = $_POST['branch_id'];
189 $cart->sales_type = $_POST['sales_type'];
191 if ($cart->trans_type!=30) { // 2008-11-12 Joe Hunt
192 $cart->dimension_id = $_POST['dimension_id'];
193 $cart->dimension2_id = $_POST['dimension2_id'];
197 //-----------------------------------------------------------------------------
199 function copy_from_cart()
201 $cart = &$_SESSION['Items'];
202 if ($cart->trans_type!=30) {
203 $_POST['ref'] = $cart->reference;
205 $_POST['Comments'] = $cart->Comments;
207 $_POST['OrderDate'] = $cart->document_date;
208 $_POST['delivery_date'] = $cart->due_date;
209 $_POST['cust_ref'] = $cart->cust_ref;
210 $_POST['freight_cost'] = price_format($cart->freight_cost);
212 $_POST['deliver_to'] = $cart->deliver_to;
213 $_POST['delivery_address'] = $cart->delivery_address;
214 $_POST['phone'] = $cart->phone;
215 $_POST['Location'] = $cart->Location;
216 $_POST['ship_via'] = $cart->ship_via;
218 $_POST['customer_id'] = $cart->customer_id;
220 $_POST['branch_id'] = $cart->Branch;
221 $_POST['sales_type'] = $cart->sales_type;
223 if ($cart->trans_type == 10)
224 $_POST['cash'] = $cart->cash;
225 if ($cart->trans_type!=30) { // 2008-11-12 Joe Hunt
226 $_POST['dimension_id'] = $cart->dimension_id;
227 $_POST['dimension2_id'] = $cart->dimension2_id;
229 $_POST['cart_id'] = $cart->cart_id;
232 //--------------------------------------------------------------------------------
234 function line_start_focus() {
237 $Ajax->activate('items_table');
238 set_focus('_stock_id_edit');
241 //--------------------------------------------------------------------------------
242 function can_process() {
243 if (!is_date($_POST['OrderDate'])) {
244 display_error(_("The entered date is invalid."));
245 set_focus('OrderDate');
248 if ($_SESSION['Items']->trans_type!=30 && !is_date_in_fiscalyear($_POST['OrderDate'])) {
249 display_error(_("The entered date is not in fiscal year"));
250 set_focus('OrderDate');
253 if (count($_SESSION['Items']->line_items) == 0) {
254 display_error(_("You must enter at least one non empty item line."));
255 set_focus('AddItem');
258 if ($_SESSION['Items']->cash == 0) {
259 if (strlen($_POST['deliver_to']) <= 1) {
260 display_error(_("You must enter the person or company to whom delivery should be made to."));
261 set_focus('deliver_to');
265 if (strlen($_POST['delivery_address']) <= 1) {
266 display_error( _("You should enter the street address in the box provided. Orders cannot be accepted without a valid street address."));
267 set_focus('delivery_address');
271 if ($_POST['freight_cost'] == "")
272 $_POST['freight_cost'] = price_format(0);
274 if (!check_num('freight_cost',0)) {
275 display_error(_("The shipping cost entered is expected to be numeric."));
276 set_focus('freight_cost');
279 if (!is_date($_POST['delivery_date'])) {
280 display_error(_("The delivery date is invalid."));
281 set_focus('delivery_date');
284 //if (date1_greater_date2($_SESSION['Items']->document_date, $_POST['delivery_date'])) {
285 if (date1_greater_date2($_POST['OrderDate'], $_POST['delivery_date'])) {
286 display_error(_("The requested delivery date is before the date of the order."));
287 set_focus('delivery_date');
291 if ($_SESSION['Items']->trans_type != 30 && !references::is_valid($_POST['ref'])) {
292 display_error(_("You must enter a reference."));
299 //-----------------------------------------------------------------------------
301 if (isset($_POST['ProcessOrder']) && can_process()) {
304 $modified = ($_SESSION['Items']->trans_no != 0);
305 $so_type = $_SESSION['Items']->so_type;
306 $_SESSION['Items']->write(1);
307 if (count($messages)) { // abort on failure or error messages are lost
308 $Ajax->activate('_page_body');
309 display_footer_exit();
311 $trans_no = key($_SESSION['Items']->trans_no);
312 $trans_type = $_SESSION['Items']->trans_type;
313 new_doc_date($_SESSION['Items']->document_date);
316 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$trans_no");
317 } elseif ($trans_type == 30) {
318 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
319 } elseif ($trans_type == 10) {
320 meta_forward($_SERVER['PHP_SELF'], "AddedDI=$trans_no&Type=$so_type");
322 meta_forward($_SERVER['PHP_SELF'], "AddedDN=$trans_no&Type=$so_type");
326 //--------------------------------------------------------------------------------
328 function check_item_data()
330 if (!check_num('qty', 0) || !check_num('Disc', 0, 100)) {
331 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."));
334 } elseif (!check_num('price', 0)) {
335 display_error( _("Price for item must be entered and can not be less than 0"));
338 } elseif (isset($_POST['LineNo']) && isset($_SESSION['Items']->line_items[$_POST['LineNo']])
339 && !check_num('qty', $_SESSION['Items']->line_items[$_POST['LineNo']]->qty_done)) {
342 display_error(_("You attempting to make the quantity ordered a quantity less than has already been delivered. The quantity delivered cannot be modified retrospectively."));
344 } // Joe Hunt added 2008-09-22 -------------------------
345 elseif ($_SESSION['Items']->trans_type!=30 && !sys_prefs::allow_negative_stock() &&
346 is_inventory_item($_POST['stock_id']))
348 $qoh = get_qoh_on_date($_POST['stock_id'], $_POST['Location'], $_POST['OrderDate']);
349 if (input_num('qty') > $qoh)
351 $stock = get_item($_POST['stock_id']);
352 display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:") .
353 " " . $stock['stock_id'] . " - " . $stock['description'] . " - " .
354 _("Quantity On Hand") . " = " . number_format2($qoh, get_qty_dec($_POST['stock_id'])));
362 //--------------------------------------------------------------------------------
364 function handle_update_item()
366 if ($_POST['UpdateItem'] != '' && check_item_data()) {
367 $_SESSION['Items']->update_cart_item($_POST['LineNo'],
368 input_num('qty'), input_num('price'),
369 input_num('Disc') / 100, $_POST['item_description'] );
374 //--------------------------------------------------------------------------------
376 function handle_delete_item($line_no)
378 if ($_SESSION['Items']->some_already_delivered($line_no) == 0) {
379 $_SESSION['Items']->remove_from_cart($line_no);
381 display_error(_("This item cannot be deleted because some of it has already been delivered."));
386 //--------------------------------------------------------------------------------
388 function handle_new_item()
391 if (!check_item_data()) {
394 add_to_order($_SESSION['Items'], $_POST['stock_id'], input_num('qty'),
395 input_num('price'), input_num('Disc') / 100);
396 $_POST['_stock_id_edit'] = $_POST['stock_id'] = "";
400 //--------------------------------------------------------------------------------
402 function handle_cancel_order()
404 global $path_to_root, $Ajax;
407 if ($_SESSION['Items']->trans_type == 13) {
408 display_note(_("Direct delivery entry has been cancelled as requested."), 1);
409 submenu_option(_("Enter a New Sales Delivery"), $_SERVER['PHP_SELF']."?NewDelivery=0");
411 } elseif ($_SESSION['Items']->trans_type == 10) {
412 display_note(_("Direct invoice entry has been cancelled as requested."), 1);
413 submenu_option(_("Enter a New Sales Invoice"), $_SERVER['PHP_SELF']."?NewInvoice=0");
415 if ($_SESSION['Items']->trans_no != 0) {
416 if (sales_order_has_deliveries(key($_SESSION['Items']->trans_no)))
417 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."));
419 delete_sales_order(key($_SESSION['Items']->trans_no));
421 display_note(_("This sales order has been cancelled as requested."), 1);
422 submenu_option(_("Enter a New Sales Order"), $_SERVER['PHP_SELF']."?NewOrder=Yes");
426 meta_forward($path_to_root.'/index.php','application=orders');
429 $Ajax->activate('_page_body');
436 //--------------------------------------------------------------------------------
438 function create_cart($type, $trans_no)
443 if($type != 30 && $trans_no != 0) { // this is template
446 $doc = new Cart(30, array($trans_no));
447 $doc->trans_type = $type;
449 $doc->document_date = new_doc_date();
451 $doc->due_date = get_invoice_duedate($doc->customer_id, $doc->document_date);
452 $doc->pos = user_pos();
453 $pos = get_sales_point($doc->pos);
454 $doc->cash = $pos['cash_sale'];
455 if (!$pos['cash_sale'] || !$pos['credit_sale'])
456 $doc->pos = -1; // mark not editable payment type
458 $doc->cash = date_diff($doc->due_date, Today(), 'd')<2;
460 $doc->due_date = $doc->document_date;
461 $doc->reference = references::get_next($doc->trans_type);
463 foreach($doc->line_items as $line_no => $line) {
464 $doc->line_items[$line_no]->qty_done = 0;
466 $_SESSION['Items'] = $doc;
468 $_SESSION['Items'] = new Cart($type,array($trans_no));
472 //--------------------------------------------------------------------------------
474 if (isset($_POST['CancelOrder']))
475 handle_cancel_order();
477 $id = find_submit('Delete');
479 handle_delete_item($id);
481 if (isset($_POST['UpdateItem']))
482 handle_update_item();
484 if (isset($_POST['AddItem']))
487 if (isset($_POST['CancelItemChanges'])) {
491 //--------------------------------------------------------------------------------
492 check_db_has_stock_items(_("There are no inventory items defined in the system."));
494 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
496 if ($_SESSION['Items']->trans_type == 10) {
497 $idate = _("Invoice Date:");
498 $orderitems = _("Sales Invoice Items");
499 $deliverydetails = _("Enter Delivery Details and Confirm Invoice");
500 $cancelorder = _("Cancel Invoice");
501 $porder = _("Place Invoice");
502 } elseif ($_SESSION['Items']->trans_type == 13) {
503 $idate = _("Delivery Date:");
504 $orderitems = _("Delivery Note Items");
505 $deliverydetails = _("Enter Delivery Details and Confirm Dispatch");
506 $cancelorder = _("Cancel Delivery");
507 $porder = _("Place Delivery");
509 $idate = _("Order Date:");
510 $orderitems = _("Sales Order Items");
511 $deliverydetails = _("Enter Delivery Details and Confirm Order");
512 $cancelorder = _("Cancel Order");
513 $porder = _("Place Order");
514 $corder = _("Commit Order Changes");
520 $customer_error = display_order_header($_SESSION['Items'],
521 ($_SESSION['Items']->any_already_delivered() == 0), $idate);
523 if ($customer_error == "") {
524 start_table("$table_style width=80%", 10);
526 display_order_summary($orderitems, $_SESSION['Items'], true);
529 display_delivery_details($_SESSION['Items']);
533 if ($_SESSION['Items']->trans_no == 0) {
535 submit_center_first('ProcessOrder', $porder,
536 _('Check entered data and save document'), 'default');
537 submit_js_confirm('CancelOrder', _('You are about to void this Sales Order.\nDo you want to continue?'));
539 submit_center_first('ProcessOrder', $corder,
540 _('Validate changes and update document'), 'default');
543 submit_center_last('CancelOrder', $cancelorder,
544 _('Cancels document entry or removes sales order when editing an old document'));
546 display_error($customer_error);