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 //-----------------------------------------------------------------------------
64 if (list_updated('branch_id')) {
65 // when branch is selected via external editor also customer can change
66 $br = get_branch(get_post('branch_id'));
67 $_POST['customer_id'] = $br['debtor_no'];
68 $Ajax->activate('customer_id');
71 if (isset($_GET['AddedID'])) {
72 $order_no = $_GET['AddedID'];
73 display_notification_centered(sprintf( _("Order # %d has been entered."),$order_no));
75 submenu_view(_("&View This Order"), 30, $order_no);
77 submenu_print(_("&Print This Order"), 30, $order_no, 'prtopt');
78 submenu_print(_("&Email This Order"), 30, $order_no, null, 1);
81 submenu_option(_("Make &Delivery Against This Order"),
82 "/sales/customer_delivery.php?OrderNumber=$order_no");
84 submenu_option(_("Enter a &New Order"), "/sales/sales_order_entry.php?NewOrder=0");
86 display_footer_exit();
88 } elseif (isset($_GET['UpdatedID'])) {
89 $order_no = $_GET['UpdatedID'];
91 display_notification_centered(sprintf( _("Order # %d has been updated."),$order_no));
93 submenu_view(_("&View This Order"), 30, $order_no);
95 submenu_print(_("&Print This Order"), 30, $order_no, 'prtopt');
96 submenu_print(_("&Email This Order"), 30, $order_no, null, 1);
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');
115 submenu_print(_("&Email Delivery Note"), 13, $delivery, null, 1);
118 display_note(get_gl_view_str(13, $delivery, _("View the GL Journal Entries for this Dispatch")),0, 1);
120 submenu_option(_("Make &Invoice Against This Delivery"),
121 "/sales/customer_invoice.php?DeliveryNumber=$delivery");
123 if ((isset($_GET['Type']) && $_GET['Type'] == 1))
124 submenu_option(_("Enter a New Template &Delivery"),
125 "/sales/inquiry/sales_orders_view.php?DeliveryTemplates=Yes");
127 submenu_option(_("Enter a &New Delivery"),
128 "/sales/sales_order_entry.php?NewDelivery=0");
130 display_footer_exit();
132 } elseif (isset($_GET['AddedDI'])) {
133 $invoice = $_GET['AddedDI'];
135 display_notification_centered(sprintf(_("Invoice # %d has been entered."), $invoice));
137 submenu_view(_("&View This Invoice"), 10, $invoice);
139 submenu_print(_("&Print Sales Invoice"), 10, $invoice, 'prtopt');
140 submenu_print(_("&Email Sales Invoice"), 10, $invoice, null, 1);
143 display_note(get_gl_view_str(10, $invoice, _("View the GL &Journal Entries for this Invoice")),0, 1);
145 if ((isset($_GET['Type']) && $_GET['Type'] == 1))
146 submenu_option(_("Enter a &New Template Invoice"),
147 "/sales/inquiry/sales_orders_view.php?InvoiceTemplates=Yes");
149 submenu_option(_("Enter a &New Direct Invoice"),
150 "/sales/sales_order_entry.php?NewInvoice=0");
152 display_footer_exit();
154 check_edit_conflicts();
155 //-----------------------------------------------------------------------------
157 function copy_to_cart()
159 $cart = &$_SESSION['Items'];
161 if ($cart->trans_type!=30) {
162 $cart->reference = $_POST['ref'];
164 $cart->Comments = $_POST['Comments'];
166 $cart->document_date = $_POST['OrderDate'];
167 if ($cart->trans_type == 10)
168 $cart->cash = $_POST['cash'];
170 $cart->due_date = $cart->document_date;
171 $cart->phone = $cart->cust_ref = $cart->delivery_address = '';
172 $cart->freight_cost = 0;
174 $cart->deliver_to = '';//$_POST['deliver_to'];
176 $cart->due_date = $_POST['delivery_date'];
177 $cart->cust_ref = $_POST['cust_ref'];
178 $cart->freight_cost = input_num('freight_cost');
179 $cart->deliver_to = $_POST['deliver_to'];
180 $cart->delivery_address = $_POST['delivery_address'];
181 $cart->phone = $_POST['phone'];
182 $cart->Location = $_POST['Location'];
183 $cart->ship_via = $_POST['ship_via'];
185 if (isset($_POST['email']))
186 $cart->email =$_POST['email'];
189 $cart->customer_id = $_POST['customer_id'];
190 $cart->Branch = $_POST['branch_id'];
191 $cart->sales_type = $_POST['sales_type'];
193 if ($cart->trans_type!=30) { // 2008-11-12 Joe Hunt
194 $cart->dimension_id = $_POST['dimension_id'];
195 $cart->dimension2_id = $_POST['dimension2_id'];
199 //-----------------------------------------------------------------------------
201 function copy_from_cart()
203 $cart = &$_SESSION['Items'];
204 if ($cart->trans_type!=30) {
205 $_POST['ref'] = $cart->reference;
207 $_POST['Comments'] = $cart->Comments;
209 $_POST['OrderDate'] = $cart->document_date;
210 $_POST['delivery_date'] = $cart->due_date;
211 $_POST['cust_ref'] = $cart->cust_ref;
212 $_POST['freight_cost'] = price_format($cart->freight_cost);
214 $_POST['deliver_to'] = $cart->deliver_to;
215 $_POST['delivery_address'] = $cart->delivery_address;
216 $_POST['phone'] = $cart->phone;
217 $_POST['Location'] = $cart->Location;
218 $_POST['ship_via'] = $cart->ship_via;
220 $_POST['customer_id'] = $cart->customer_id;
222 $_POST['branch_id'] = $cart->Branch;
223 $_POST['sales_type'] = $cart->sales_type;
225 if ($cart->trans_type == 10)
226 $_POST['cash'] = $cart->cash;
227 if ($cart->trans_type!=30) { // 2008-11-12 Joe Hunt
228 $_POST['dimension_id'] = $cart->dimension_id;
229 $_POST['dimension2_id'] = $cart->dimension2_id;
231 $_POST['cart_id'] = $cart->cart_id;
234 //--------------------------------------------------------------------------------
236 function line_start_focus() {
239 $Ajax->activate('items_table');
240 set_focus('_stock_id_edit');
243 //--------------------------------------------------------------------------------
244 function can_process() {
245 if (!is_date($_POST['OrderDate'])) {
246 display_error(_("The entered date is invalid."));
247 set_focus('OrderDate');
250 if ($_SESSION['Items']->trans_type!=30 && !is_date_in_fiscalyear($_POST['OrderDate'])) {
251 display_error(_("The entered date is not in fiscal year"));
252 set_focus('OrderDate');
255 if (count($_SESSION['Items']->line_items) == 0) {
256 display_error(_("You must enter at least one non empty item line."));
257 set_focus('AddItem');
260 if ($_SESSION['Items']->cash == 0) {
261 if (strlen($_POST['deliver_to']) <= 1) {
262 display_error(_("You must enter the person or company to whom delivery should be made to."));
263 set_focus('deliver_to');
267 if (strlen($_POST['delivery_address']) <= 1) {
268 display_error( _("You should enter the street address in the box provided. Orders cannot be accepted without a valid street address."));
269 set_focus('delivery_address');
273 if ($_POST['freight_cost'] == "")
274 $_POST['freight_cost'] = price_format(0);
276 if (!check_num('freight_cost',0)) {
277 display_error(_("The shipping cost entered is expected to be numeric."));
278 set_focus('freight_cost');
281 if (!is_date($_POST['delivery_date'])) {
282 display_error(_("The delivery date is invalid."));
283 set_focus('delivery_date');
286 //if (date1_greater_date2($_SESSION['Items']->document_date, $_POST['delivery_date'])) {
287 if (date1_greater_date2($_POST['OrderDate'], $_POST['delivery_date'])) {
288 display_error(_("The requested delivery date is before the date of the order."));
289 set_focus('delivery_date');
293 if ($_SESSION['Items']->trans_type != 30 && !references::is_valid($_POST['ref'])) {
294 display_error(_("You must enter a reference."));
301 //-----------------------------------------------------------------------------
303 if (isset($_POST['ProcessOrder']) && can_process()) {
306 $modified = ($_SESSION['Items']->trans_no != 0);
307 $so_type = $_SESSION['Items']->so_type;
308 $_SESSION['Items']->write(1);
309 if (count($messages)) { // abort on failure or error messages are lost
310 $Ajax->activate('_page_body');
311 display_footer_exit();
313 $trans_no = key($_SESSION['Items']->trans_no);
314 $trans_type = $_SESSION['Items']->trans_type;
315 new_doc_date($_SESSION['Items']->document_date);
318 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$trans_no");
319 } elseif ($trans_type == 30) {
320 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
321 } elseif ($trans_type == 10) {
322 meta_forward($_SERVER['PHP_SELF'], "AddedDI=$trans_no&Type=$so_type");
324 meta_forward($_SERVER['PHP_SELF'], "AddedDN=$trans_no&Type=$so_type");
328 //--------------------------------------------------------------------------------
330 function check_item_data()
332 if (!check_num('qty', 0) || !check_num('Disc', 0, 100)) {
333 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."));
336 } elseif (!check_num('price', 0)) {
337 display_error( _("Price for item must be entered and can not be less than 0"));
340 } elseif (isset($_POST['LineNo']) && isset($_SESSION['Items']->line_items[$_POST['LineNo']])
341 && !check_num('qty', $_SESSION['Items']->line_items[$_POST['LineNo']]->qty_done)) {
344 display_error(_("You attempting to make the quantity ordered a quantity less than has already been delivered. The quantity delivered cannot be modified retrospectively."));
346 } // Joe Hunt added 2008-09-22 -------------------------
347 elseif ($_SESSION['Items']->trans_type!=30 && !sys_prefs::allow_negative_stock() &&
348 is_inventory_item($_POST['stock_id']))
350 $qoh = get_qoh_on_date($_POST['stock_id'], $_POST['Location'], $_POST['OrderDate']);
351 if (input_num('qty') > $qoh)
353 $stock = get_item($_POST['stock_id']);
354 display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:") .
355 " " . $stock['stock_id'] . " - " . $stock['description'] . " - " .
356 _("Quantity On Hand") . " = " . number_format2($qoh, get_qty_dec($_POST['stock_id'])));
364 //--------------------------------------------------------------------------------
366 function handle_update_item()
368 if ($_POST['UpdateItem'] != '' && check_item_data()) {
369 $_SESSION['Items']->update_cart_item($_POST['LineNo'],
370 input_num('qty'), input_num('price'),
371 input_num('Disc') / 100, $_POST['item_description'] );
376 //--------------------------------------------------------------------------------
378 function handle_delete_item($line_no)
380 if ($_SESSION['Items']->some_already_delivered($line_no) == 0) {
381 $_SESSION['Items']->remove_from_cart($line_no);
383 display_error(_("This item cannot be deleted because some of it has already been delivered."));
388 //--------------------------------------------------------------------------------
390 function handle_new_item()
393 if (!check_item_data()) {
396 add_to_order($_SESSION['Items'], $_POST['stock_id'], input_num('qty'),
397 input_num('price'), input_num('Disc') / 100);
398 $_POST['_stock_id_edit'] = $_POST['stock_id'] = "";
402 //--------------------------------------------------------------------------------
404 function handle_cancel_order()
406 global $path_to_root, $Ajax;
409 if ($_SESSION['Items']->trans_type == 13) {
410 display_note(_("Direct delivery entry has been cancelled as requested."), 1);
411 submenu_option(_("Enter a New Sales Delivery"), $_SERVER['PHP_SELF']."?NewDelivery=0");
413 } elseif ($_SESSION['Items']->trans_type == 10) {
414 display_note(_("Direct invoice entry has been cancelled as requested."), 1);
415 submenu_option(_("Enter a New Sales Invoice"), $_SERVER['PHP_SELF']."?NewInvoice=0");
417 if ($_SESSION['Items']->trans_no != 0) {
418 if (sales_order_has_deliveries(key($_SESSION['Items']->trans_no)))
419 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."));
421 delete_sales_order(key($_SESSION['Items']->trans_no));
423 display_note(_("This sales order has been cancelled as requested."), 1);
424 submenu_option(_("Enter a New Sales Order"), $_SERVER['PHP_SELF']."?NewOrder=Yes");
428 meta_forward($path_to_root.'/index.php','application=orders');
431 $Ajax->activate('_page_body');
438 //--------------------------------------------------------------------------------
440 function create_cart($type, $trans_no)
445 if($type != 30 && $trans_no != 0) { // this is template
448 $doc = new Cart(30, array($trans_no));
449 $doc->trans_type = $type;
451 $doc->document_date = new_doc_date();
453 $doc->due_date = get_invoice_duedate($doc->customer_id, $doc->document_date);
454 $doc->pos = user_pos();
455 $pos = get_sales_point($doc->pos);
456 $doc->cash = $pos['cash_sale'];
457 if (!$pos['cash_sale'] || !$pos['credit_sale'])
458 $doc->pos = -1; // mark not editable payment type
460 $doc->cash = date_diff($doc->due_date, Today(), 'd')<2;
462 $doc->due_date = $doc->document_date;
463 $doc->reference = references::get_next($doc->trans_type);
465 foreach($doc->line_items as $line_no => $line) {
466 $doc->line_items[$line_no]->qty_done = 0;
468 $_SESSION['Items'] = $doc;
470 $_SESSION['Items'] = new Cart($type,array($trans_no));
474 //--------------------------------------------------------------------------------
476 if (isset($_POST['CancelOrder']))
477 handle_cancel_order();
479 $id = find_submit('Delete');
481 handle_delete_item($id);
483 if (isset($_POST['UpdateItem']))
484 handle_update_item();
486 if (isset($_POST['AddItem']))
489 if (isset($_POST['CancelItemChanges'])) {
493 //--------------------------------------------------------------------------------
494 check_db_has_stock_items(_("There are no inventory items defined in the system."));
496 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
498 if ($_SESSION['Items']->trans_type == 10) {
499 $idate = _("Invoice Date:");
500 $orderitems = _("Sales Invoice Items");
501 $deliverydetails = _("Enter Delivery Details and Confirm Invoice");
502 $cancelorder = _("Cancel Invoice");
503 $porder = _("Place Invoice");
504 } elseif ($_SESSION['Items']->trans_type == 13) {
505 $idate = _("Delivery Date:");
506 $orderitems = _("Delivery Note Items");
507 $deliverydetails = _("Enter Delivery Details and Confirm Dispatch");
508 $cancelorder = _("Cancel Delivery");
509 $porder = _("Place Delivery");
511 $idate = _("Order Date:");
512 $orderitems = _("Sales Order Items");
513 $deliverydetails = _("Enter Delivery Details and Confirm Order");
514 $cancelorder = _("Cancel Order");
515 $porder = _("Place Order");
516 $corder = _("Commit Order Changes");
522 $customer_error = display_order_header($_SESSION['Items'],
523 ($_SESSION['Items']->any_already_delivered() == 0), $idate);
525 if ($customer_error == "") {
526 start_table("$table_style width=80%", 10);
528 display_order_summary($orderitems, $_SESSION['Items'], true);
531 display_delivery_details($_SESSION['Items']);
535 if ($_SESSION['Items']->trans_no == 0) {
537 submit_center_first('ProcessOrder', $porder,
538 _('Check entered data and save document'), 'default');
539 submit_js_confirm('CancelOrder', _('You are about to void this Sales Order.\nDo you want to continue?'));
541 submit_center_first('ProcessOrder', $corder,
542 _('Validate changes and update document'), 'default');
545 submit_center_last('CancelOrder', $cancelorder,
546 _('Cancels document entry or removes sales order when editing an old document'));
548 display_error($customer_error);