Release 1.0.1 established on SourceForge, fixing the bugs and including a Date Picker...
[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']->memo_ = $_POST['InvoiceText'];
199                 $_SESSION['Items']->memo_ = str_replace("'", "\\'", $_SESSION['Items']->memo_);
200                 $_SESSION['Items']->order_no = $order_no;
201                 meta_forward("$path_to_root/sales/customer_invoice.php", "process_invoice=Yes");
202                 
203         } 
204         else 
205         {
206         
207                 unset($_SESSION['Items']->line_items);
208                 unset($_SESSION['Items']);
209
210                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$order_no");
211         }       
212 }
213
214 //-------------------------------------------------------------------------------------------------------------
215
216 if (isset($_POST['ProcessOrder']) && $_SESSION['Items']->order_no != 0 && can_process())
217 {
218
219         update_sales_order($_SESSION['Items']->order_no, $_SESSION['Items']);
220
221         $order_no = $_SESSION['Items']->order_no;
222
223         unset($_SESSION['Items']->line_items);
224         unset($_SESSION['Items']);
225
226         meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$order_no");
227 }
228
229
230 //--------------------------------------------------------------------------------
231
232 function check_item_data() 
233 {
234         if (!is_numeric($_POST['qty']) || $_POST['qty'] < 0 || $_POST['Disc'] > 100 || 
235                 $_POST['Disc'] < 0)
236         {
237                 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."));
238                 return false;
239         } 
240         elseif($_SESSION['Items']->some_already_delivered($_POST['stock_id']) != 0 &&
241                 $_SESSION['Items']->line_items[$_POST['stock_id']]->price != $_POST['price']) 
242         {
243                 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."));
244                 return false;
245         } 
246         elseif($_SESSION['Items']->some_already_delivered($_POST['stock_id']) != 0 && 
247                 $_SESSION['Items']->line_items[$_POST['stock_id']]->discount_percent != ($_POST['Disc']/100)) 
248         {
249                 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."));
250                 return false;
251         } 
252         elseif (isset($_SESSION['Items']->line_items[$_POST['stock_id']]) && $_SESSION['Items']->line_items[$_POST['stock_id']]->qty_inv > $_POST['qty'])
253         {
254                 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."));
255                 return false;
256         }       
257         return true;
258 }
259
260 function handle_update_item() 
261 {
262     if($_POST['UpdateItem'] != "" && check_item_data())
263     {
264         $_SESSION['Items']->update_cart_item($_POST['stock_id'], $_POST['qty'], 
265                 $_POST['price'], ($_POST['Disc'] / 100));
266     }
267 }
268
269 //--------------------------------------------------------------------------------
270
271 function handle_delete_item() 
272 {   
273     if($_GET['Delete'] != "")
274     {
275         $stock_id = $_GET['Delete']; 
276         if($_SESSION['Items']->some_already_delivered($stock_id) == 0)
277         {
278                 $_SESSION['Items']->remove_from_cart($stock_id);
279         } 
280         else 
281         {
282                 display_error(_("This item cannot be deleted because some of it has already been invoiced."));
283         }
284     }
285 }
286
287 //--------------------------------------------------------------------------------
288
289 function handle_new_item()
290 {
291         if (!check_item_data())
292                 return;
293                 
294         add_to_order($_SESSION['Items'], $_POST['stock_id'], $_POST['qty'], 
295                 $_POST['price'], $_POST['Disc']/100);
296         $_POST['StockID2'] = $_POST['stock_id'] = "";
297 }
298
299 //--------------------------------------------------------------------------------      
300
301 function  handle_cancel_order()
302 {
303         global $path_to_root;
304         
305     if ($_POST['CancelOrder'] != "") 
306     {
307         $ok_to_delete = 1;      //assume this in the first instance
308     
309                 if (($_SESSION['Items']->order_no != 0) && 
310                         sales_order_has_invoices($_SESSION['Items']->order_no)) 
311                 {
312                         $ok_to_delete = 0;
313                         display_error(_("This order cannot be cancelled because some of it has already been invoiced. However, the line item quantities may be modified."));
314                 }
315     
316         if ($ok_to_delete == 1)
317         {
318                 if($_SESSION['Items']->order_no != 0)
319                 {
320                         delete_sales_order($_SESSION['Items']->order_no);
321                 }
322                 $diriv = $_SESSION['Items']->direct_invoice;
323                 $_SESSION['Items']->clear_items();
324                 $_SESSION['Items'] = new cart;
325                 $_SESSION['Items']->direct_invoice = $diriv;
326                 if ($diriv) 
327                 {
328                         display_note(_("This sales invoice has been cancelled as requested."), 1);
329                                 hyperlink_params($path_to_root . "/sales/sales_order_entry.php", _("Enter a New Sales Invoice"), SID . "&NewInvoice=Yes");
330                 } 
331                 else 
332                 {
333                         display_note(_("This sales order has been cancelled as requested."), 1);
334                                 hyperlink_params($path_to_root . "/sales/sales_order_entry.php", _("Enter a New Sales Order"), SID . "&NewOrder=Yes");
335                 }
336                 br(1);
337                 end_page();                     
338                 exit;
339         }
340     }
341 }
342         
343 //--------------------------------------------------------------------------------
344
345 function handle_new_order() 
346 {
347         /*New order entry - clear any existing order details from the Items object and initiate a newy*/
348         if (isset($_SESSION['Items']))
349         {
350                 unset ($_SESSION['Items']->line_items);
351                 unset ($_SESSION['Items']);
352         }
353         
354         session_register("Items");
355
356         $_SESSION['Items'] = new cart;
357         if (isset($_GET['NewInvoice']))
358                 $_SESSION['Items']->direct_invoice = true;
359         $_SESSION['Items']->customer_id = "";
360         $_POST['OrderDate'] = Today();          
361         if (!is_date_in_fiscalyear($_POST['OrderDate']))
362                 $_POST['OrderDate'] = end_fiscalyear();
363         $_SESSION['Items']->orig_order_date = $_POST['OrderDate'];
364 }
365
366 //--------------------------------------------------------------------------------      
367
368 if (isset($_GET['ModifyOrderNumber']) && $_GET['ModifyOrderNumber'] != "")
369 {
370
371         if (isset($_SESSION['Items']))
372         {
373                 unset ($_SESSION['Items']->line_items);
374                 unset ($_SESSION['Items']);
375         }
376
377         session_register("Items");
378
379         $_SESSION['Items'] = new cart;
380         $_SESSION['Items']->order_no = $_GET['ModifyOrderNumber']; 
381
382         /*read in all the selected order into the Items cart  */
383         
384         read_sales_order($_SESSION['Items']->order_no, $_SESSION['Items']);
385 }
386
387 //--------------------------------------------------------------------------------
388
389 if (isset($_POST['CancelOrder']))
390         handle_cancel_order();
391
392 if (isset($_GET['Delete']) || isset($_GET['Edit']))
393         copy_from_so();
394         
395 if (isset($_GET['Delete']))
396         handle_delete_item();
397
398 if (isset($_POST['UpdateItem']) || isset($_POST['AddItem']))
399         copy_to_so();
400         
401 if (isset($_POST['UpdateItem']))
402         handle_update_item();
403
404 if (isset($_POST['AddItem']))
405         handle_new_item();
406         
407 //--------------------------------------------------------------------------------
408
409 if (isset($_GET['NewOrder']) || isset($_GET['NewInvoice']))
410 {
411         handle_new_order();
412
413 else 
414 {
415         if (!isset($_POST['customer_id']))
416                 $_POST['customer_id'] = $_SESSION['Items']->customer_id;
417         if (!isset($_POST['branch_id']))                
418                 $_POST['branch_id'] = $_SESSION['Items']->Branch;
419         if (!isset($_POST['OrderDate']))                
420                 $_POST['OrderDate'] = $_SESSION['Items']->orig_order_date;
421 }
422
423 //--------------------------------------------------------------------------------      
424
425 check_db_has_stock_items(_("There are no inventory items defined in the system."));
426
427 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));           
428
429 if ($_SESSION['Items']->direct_invoice) 
430 {
431         $idate = _("Invoice Date:");
432         $orderitems = _("Sales Invoice Items");
433         $deliverydetails = _("Enter Delivery Details and Confirm Invoice");
434         $cancelorder = _("Cancel Invoice");
435
436 else 
437 {
438         $idate = _("Order Date:");
439         $orderitems = _("Sales Order Items");
440         $deliverydetails = _("Enter Delivery Details and Confirm Order");
441         $cancelorder = _("Cancel Order");
442 }
443 start_form(false, true);
444
445 $customer_error = display_order_header($_SESSION['Items'], 
446         ($_SESSION['Items']->any_already_delivered() == 0), $idate);
447
448 if ($customer_error == "")
449 {
450         start_table("$table_style width=80%", 10);
451         echo "<tr><td>";                
452         display_order_summary($orderitems, $_SESSION['Items'], true);
453         echo "</td></tr>";
454         echo "<tr><td>";                
455         display_delivery_details($_SESSION['Items']);
456         echo "</td></tr>";
457         end_table(1);           
458
459 else
460 {
461         display_error($customer_error);
462 }
463
464 if ($_SESSION['Items']->direct_invoice) 
465 {
466         $porder = _("Place Invoice");
467         $corder = _("Commit Invoice Changes");
468         $eorder = _("Edit Invoice Items");
469
470 else 
471 {
472         $porder = _("Place Order");
473         $corder = _("Commit Order Changes");
474         $eorder = _("Edit Order Items");
475 }
476
477 if ($_SESSION['Items']->order_no == 0)
478 {
479         submit_center_first('ProcessOrder', $porder);
480
481 else 
482 {
483         submit_center_first('ProcessOrder', $corder);
484 }
485
486 /*    
487 echo " ";
488 submit('EditOrder', $eorder);
489 if ($_SESSION['Items']->count_items() >= 1)
490 {
491         echo " ";
492         submit('DeliveryDetails', $deliverydetails);
493         echo "   ";
494 }
495 */              
496 submit_center_last('CancelOrder', $cancelorder);
497         
498 end_form();
499
500 //--------------------------------------------------------------------------------
501
502 if (!isset($_SESSION['Items']))
503 {
504         /* It must be a new order being created $_SESSION['Items'] would be set up from the order
505         modification code above if a modification to an existing order.  */
506
507         session_register("Items");
508
509         $_SESSION['Items'] = new cart;
510         if (isset($_GET['NewInvoice']))
511                 $_SESSION['Items']->direct_invoice = true;
512         $_SESSION['Items']->order_no = 0;       
513 }
514
515 //--------------------------------------------------------------------------------
516
517 end_page();
518 ?>