Unstable release 2.
[fa-stable.git] / sales / sales_order_entry.php
1 <?php
2
3 $page_security = 1;
4 $path_to_root="..";
5 include_once($path_to_root . "/sales/includes/cart_class.inc");
6
7 include_once($path_to_root . "/includes/session.inc");
8
9 include_once($path_to_root . "/includes/data_checks.inc");
10
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");
14
15 $js = get_js_form_entry("StockID2", "stock_id", "qty");
16 if ($use_popup_windows)
17         $js .= get_js_open_window(900, 500);
18 if ($use_date_picker)
19         $js .= get_js_date_picker();
20
21 if (isset($_GET['ModifyOrderNumber']))
22 {
23         page(_("Modifying Sales Order") . " #".$_GET['ModifyOrderNumber'], false, false, "", $js);
24 }
25 elseif (isset($_GET['NewInvoice']) || (isset($_SESSION['Items']) && $_SESSION['Items']->direct_invoice))
26 {
27         page(_("Sales Invoices"), false, false, "", $js);
28 }
29 else
30 {
31         page(_("Sales Order Entry"), false, false, "", $js);
32 }
33
34
35 //--------------------------------------------------------------------------------
36 if (isset($_GET['AddedID']))
37 {
38         $order_no = $_GET['AddedID'];
39         $trans_type = systypes::sales_order();
40
41         display_notification_centered(_("Order has been entered.") . " #$order_no");
42
43         display_note(get_trans_view_str($trans_type, $order_no, _("View this order")));
44
45         hyperlink_params($path_to_root . "/sales/customer_invoice.php", _("Confirm Order Delivery Quantities and Produce Invoice"), "OrderNumber=$order_no");
46
47         hyperlink_params($_SERVER['PHP_SELF'], _("Enter a New Order"), "NewOrder=Yes");
48
49         display_footer_exit();
50 }
51 //--------------------------------------------------------------------------------
52
53 if (isset($_GET['UpdatedID']))
54 {
55         $order_no = $_GET['UpdatedID'];
56         $trans_type = systypes::sales_order();
57
58         display_notification_centered(_("Order has been updated.") . " #$order_no");
59
60         display_note(get_trans_view_str($trans_type, $order_no, _("View this order")));
61
62         hyperlink_params($path_to_root . "/sales/customer_invoice.php", _("Confirm Order Delivery Quantities and Produce Invoice"), "OrderNumber=$order_no");
63
64         hyperlink_params($path_to_root . "/sales/inquiry/sales_orders_view.php", _("Select A Different Order"), "OutstandingOnly=1");
65
66         display_footer_exit();
67 }
68
69 //--------------------------------------------------------------------------------------------------
70
71 function copy_to_so()
72 {
73         if ($_SESSION['Items']->direct_invoice)
74                 $_SESSION['Items']->memo_ = $_POST['InvoiceText'];
75
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'];
81
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"];
87 }
88
89 //--------------------------------------------------------------------------------------------------
90
91 function copy_from_so()
92 {
93         if ($_SESSION['Items']->direct_invoice)
94                 $_POST['InvoiceText'] = $_SESSION['Items']->memo_;
95
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;
101
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;
107 }
108
109
110 function can_process()
111 {
112         if ($_SESSION['Items']->direct_invoice)
113         {
114                 $edate = _("The entered invoice date is invalid.");
115         }
116         else
117         {
118                 $edate = _("The entered order date is invalid.");
119         }
120         if (!is_date($_POST['OrderDate']))
121         {
122                 display_error($edate);
123                 return false;
124         }
125         if ($_SESSION['Items']->direct_invoice && !is_date_in_fiscalyear($_POST['OrderDate']))
126         {
127                 display_error(_("The entered date is not in fiscal year"));
128                 return false;
129         }
130         else
131         {
132                 $_SESSION['Items']->orig_order_date = $_POST['OrderDate'];
133         }
134         if (count($_SESSION['Items']->line_items) == 0)
135         {
136                 display_error(_("You must enter at least one line entry."));
137                 return false;
138         }
139         if (strlen($_POST['deliver_to']) <= 1)
140         {
141                 display_error(_("You must enter the person or company to whom delivery should be made to."));
142                 return false;
143         }
144
145         if (strlen($_POST['delivery_address']) <= 1)
146         {
147                 display_error( _("You should enter the street address in the box provided. Orders cannot be accepted without a valid street address."));
148                 return false;
149         }
150
151         if ($_POST['freight_cost'] == "")
152                 $_POST['freight_cost'] = 0;
153
154         if (!is_numeric($_POST['freight_cost']))
155         {
156                 display_error(_("The shipping cost entered is expected to be numeric."));
157                 return false;
158         }
159
160         if (!is_date($_POST['delivery_date']))
161         {
162                 display_error(_("The delivery date is invalid."));
163                 return false;
164         }
165
166         if (date1_greater_date2($_SESSION['Items']->orig_order_date, $_POST['delivery_date']))
167         {
168                 display_error(_("The requested delivery date is before the date of the order."));
169                 return false;
170         }
171
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'];
178         else
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'];
185
186     return true;
187 }
188
189 //-----------------------------------------------------------------------------------------------------------
190
191 if (isset($_POST['ProcessOrder']) && $_SESSION['Items']->order_no == 0 && can_process())
192 {
193
194         $order_no = add_sales_order($_SESSION['Items']);
195
196         if ($_SESSION['Items']->direct_invoice)
197         {
198                 $_SESSION['Items']->clear_items();
199                 $_SESSION['Items']->memo_ = $_POST['InvoiceText'];
200                 $_SESSION['Items']->memo_ = str_replace("'", "\\'", $_SESSION['Items']->memo_);
201                 $_SESSION['Items']->order_no = $order_no;
202                 meta_forward("$path_to_root/sales/customer_invoice.php", "process_invoice=Yes");
203
204         }
205         else
206         {
207
208                 unset($_SESSION['Items']->line_items);
209                 unset($_SESSION['Items']);
210
211                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$order_no");
212         }
213 }
214
215 //-------------------------------------------------------------------------------------------------------------
216
217 if (isset($_POST['ProcessOrder']) && $_SESSION['Items']->order_no != 0 && can_process())
218 {
219
220         update_sales_order($_SESSION['Items']->order_no, $_SESSION['Items']);
221
222         $order_no = $_SESSION['Items']->order_no;
223
224         unset($_SESSION['Items']->line_items);
225         unset($_SESSION['Items']);
226
227         meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$order_no");
228 }
229
230
231 //--------------------------------------------------------------------------------
232
233 function check_item_data()
234 {
235         if (!is_numeric($_POST['qty']) || $_POST['qty'] < 0 || $_POST['Disc'] > 100 ||
236                 $_POST['Disc'] < 0)
237         {
238                 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."));
239                 return false;
240         }
241         elseif($_SESSION['Items']->some_already_delivered($_POST['line_no']) != 0 &&
242                 $_SESSION['Items']->line_items[$_POST['line_no']]->price != $_POST['price'])
243         {
244                 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."));
245                 return false;
246         }
247         elseif($_SESSION['Items']->some_already_delivered($_POST['line_no']) != 0 &&
248                 $_SESSION['Items']->line_items[$_POST['line_no']]->discount_percent != ($_POST['Disc']/100))
249         {
250                 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."));
251                 return false;
252         }
253         elseif (isset($_SESSION['Items']->line_items[$_POST['line_no']]) && $_SESSION['Items']->line_items[$_POST['line_no']]->qty_inv > $_POST['qty'])
254         {
255                 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."));
256                 return false;
257         }
258         return true;
259 }
260
261 function handle_update_item()
262 {
263     if($_POST['UpdateItem'] != "" && check_item_data())
264     {
265         $_SESSION['Items']->update_cart_item($_POST['line_no'], $_POST['qty'],
266                 $_POST['price'], ($_POST['Disc'] / 100));
267     }
268 }
269
270 //--------------------------------------------------------------------------------
271
272 function handle_delete_item()
273 {
274     if($_GET['Delete'] != "")
275     {
276         $line_no = $_GET['Delete'];
277         if($_SESSION['Items']->some_already_delivered($line_no) == 0)
278         {
279                 $_SESSION['Items']->remove_from_cart($line_no);
280         }
281         else
282         {
283                 display_error(_("This item cannot be deleted because some of it has already been invoiced."));
284         }
285     }
286 }
287
288 //--------------------------------------------------------------------------------
289
290 function handle_new_item()
291 {
292         if (!check_item_data())
293                 return;
294
295         add_to_order($_SESSION['Items'], $_POST['line_no'], $_POST['stock_id'], $_POST['qty'],
296                 $_POST['price'], $_POST['Disc']/100);
297         $_POST['StockID2'] = $_POST['stock_id'] = "";
298 }
299
300 //--------------------------------------------------------------------------------
301
302 function  handle_cancel_order()
303 {
304         global $path_to_root;
305
306     if ($_POST['CancelOrder'] != "")
307     {
308         $ok_to_delete = 1;      //assume this in the first instance
309
310                 if (($_SESSION['Items']->order_no != 0) &&
311                         sales_order_has_invoices($_SESSION['Items']->order_no))
312                 {
313                         $ok_to_delete = 0;
314                         display_error(_("This order cannot be cancelled because some of it has already been invoiced. However, the line item quantities may be modified."));
315                 }
316
317         if ($ok_to_delete == 1)
318         {
319                 if($_SESSION['Items']->order_no != 0)
320                 {
321                         delete_sales_order($_SESSION['Items']->order_no);
322                 }
323                 $diriv = $_SESSION['Items']->direct_invoice;
324                 $_SESSION['Items']->clear_items();
325                 $_SESSION['Items'] = new cart;
326                 $_SESSION['Items']->direct_invoice = $diriv;
327                 if ($diriv)
328                 {
329                         display_note(_("This sales invoice has been cancelled as requested."), 1);
330                                 hyperlink_params($path_to_root . "/sales/sales_order_entry.php", _("Enter a New Sales Invoice"), SID . "&NewInvoice=Yes");
331                 }
332                 else
333                 {
334                         display_note(_("This sales order has been cancelled as requested."), 1);
335                                 hyperlink_params($path_to_root . "/sales/sales_order_entry.php", _("Enter a New Sales Order"), SID . "&NewOrder=Yes");
336                 }
337                 br(1);
338                 end_page();
339                 exit;
340         }
341     }
342 }
343
344 //--------------------------------------------------------------------------------
345
346 function handle_new_order()
347 {
348         /*New order entry - clear any existing order details from the Items object and initiate a newy*/
349         if (isset($_SESSION['Items']))
350         {
351                 unset ($_SESSION['Items']->line_items);
352                 unset ($_SESSION['Items']);
353         }
354
355         session_register("Items");
356
357         $_SESSION['Items'] = new cart;
358         if (isset($_GET['NewInvoice']))
359                 $_SESSION['Items']->direct_invoice = true;
360         $_SESSION['Items']->customer_id = "";
361         $_POST['OrderDate'] = Today();
362         if (!is_date_in_fiscalyear($_POST['OrderDate']))
363                 $_POST['OrderDate'] = end_fiscalyear();
364         $_SESSION['Items']->orig_order_date = $_POST['OrderDate'];
365 }
366
367 //--------------------------------------------------------------------------------
368
369 if (isset($_GET['ModifyOrderNumber']) && $_GET['ModifyOrderNumber'] != "")
370 {
371
372         if (isset($_SESSION['Items']))
373         {
374                 unset ($_SESSION['Items']->line_items);
375                 unset ($_SESSION['Items']);
376         }
377
378         session_register("Items");
379
380         $_SESSION['Items'] = new cart;
381         $_SESSION['Items']->order_no = $_GET['ModifyOrderNumber'];
382
383         /*read in all the selected order into the Items cart  */
384
385         read_sales_order($_SESSION['Items']->order_no, $_SESSION['Items']);
386 }
387
388 //--------------------------------------------------------------------------------
389
390 if (isset($_POST['CancelOrder']))
391         handle_cancel_order();
392
393 if (isset($_GET['Delete']) || isset($_GET['Edit']))
394         copy_from_so();
395
396 if (isset($_GET['Delete']))
397         handle_delete_item();
398
399 if (isset($_POST['UpdateItem']) || isset($_POST['AddItem']))
400         copy_to_so();
401
402 if (isset($_POST['UpdateItem']))
403         handle_update_item();
404
405 if (isset($_POST['AddItem']))
406         handle_new_item();
407
408 //--------------------------------------------------------------------------------
409
410 if (isset($_GET['NewOrder']) || isset($_GET['NewInvoice']))
411 {
412         handle_new_order();
413 }
414 else
415 {
416         if (!isset($_POST['customer_id']))
417                 $_POST['customer_id'] = $_SESSION['Items']->customer_id;
418         if (!isset($_POST['branch_id']))
419                 $_POST['branch_id'] = $_SESSION['Items']->Branch;
420         if (!isset($_POST['OrderDate']))
421                 $_POST['OrderDate'] = $_SESSION['Items']->orig_order_date;
422 }
423
424 //--------------------------------------------------------------------------------
425
426 check_db_has_stock_items(_("There are no inventory items defined in the system."));
427
428 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));
429
430 if ($_SESSION['Items']->direct_invoice)
431 {
432         $idate = _("Invoice Date:");
433         $orderitems = _("Sales Invoice Items");
434         $deliverydetails = _("Enter Delivery Details and Confirm Invoice");
435         $cancelorder = _("Cancel Invoice");
436 }
437 else
438 {
439         $idate = _("Order Date:");
440         $orderitems = _("Sales Order Items");
441         $deliverydetails = _("Enter Delivery Details and Confirm Order");
442         $cancelorder = _("Cancel Order");
443 }
444 start_form(false, true);
445
446 $customer_error = display_order_header($_SESSION['Items'],
447         ($_SESSION['Items']->any_already_delivered() == 0), $idate);
448
449 if ($customer_error == "")
450 {
451         start_table("$table_style width=80%", 10);
452         echo "<tr><td>";
453         display_order_summary($orderitems, $_SESSION['Items'], true);
454         echo "</td></tr>";
455         echo "<tr><td>";
456         display_delivery_details($_SESSION['Items']);
457         echo "</td></tr>";
458         end_table(1);
459 }
460 else
461 {
462         display_error($customer_error);
463 }
464
465 if ($_SESSION['Items']->direct_invoice)
466 {
467         $porder = _("Place Invoice");
468         $corder = _("Commit Invoice Changes");
469         $eorder = _("Edit Invoice Items");
470 }
471 else
472 {
473         $porder = _("Place Order");
474         $corder = _("Commit Order Changes");
475         $eorder = _("Edit Order Items");
476 }
477
478 if ($_SESSION['Items']->order_no == 0)
479 {
480         submit_center_first('ProcessOrder', $porder);
481 }
482 else
483 {
484         submit_center_first('ProcessOrder', $corder);
485 }
486
487 /*
488 echo " ";
489 submit('EditOrder', $eorder);
490 if ($_SESSION['Items']->count_items() >= 1)
491 {
492         echo " ";
493         submit('DeliveryDetails', $deliverydetails);
494         echo "   ";
495 }
496 */
497 submit_center_last('CancelOrder', $cancelorder);
498
499 end_form();
500
501 //--------------------------------------------------------------------------------
502
503 if (!isset($_SESSION['Items']))
504 {
505         /* It must be a new order being created $_SESSION['Items'] would be set up from the order
506         modification code above if a modification to an existing order.  */
507
508         session_register("Items");
509
510         $_SESSION['Items'] = new cart;
511         if (isset($_GET['NewInvoice']))
512                 $_SESSION['Items']->direct_invoice = true;
513         $_SESSION['Items']->order_no = 0;
514 }
515
516 //--------------------------------------------------------------------------------
517
518 end_page();
519 ?>