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