d9e48f15a91cbd2a3c5d4438c1ffc1cc0aceaa33
[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 include_once($path_to_root . "/includes/session.inc");
7 include_once($path_to_root . "/includes/data_checks.inc");
8 include_once($path_to_root . "/sales/includes/sales_ui.inc");
9 include_once($path_to_root . "/sales/includes/ui/sales_order_ui.inc");
10 include_once($path_to_root . "/sales/includes/sales_db.inc");
11
12 $js = get_js_form_entry("StockID2", "stock_id", "qty");
13 if ($use_popup_windows)
14         $js .= get_js_open_window(900, 500);
15 if ($use_date_picker)
16         $js .= get_js_date_picker();
17
18 if(isset($_GET['NewDelivery'])) {
19         $_SESSION['page_title'] = _("Sales Delivery"); 
20   create_cart('delivery',0);
21 }
22 elseif(isset($_GET['NewOrder'])) {
23         $_SESSION['page_title'] = _("Sales Order Entry"); 
24   create_cart('order',0);
25 }
26 elseif(isset($_GET['ModifyOrderNumber'])) {
27         $_SESSION['page_title'] = _("Modifying Sales Order") . " #".$_GET['ModifyOrderNumber']; 
28   create_cart('order', $_GET['ModifyOrderNumber']);
29 }
30
31 page($_SESSION['page_title'], false, false, "", $js); 
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_delivery.php", _("Make Delivery Against This Order"), "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 # ').$order_no. _( ' has been updated.'));
57
58         display_note(get_trans_view_str($trans_type, $order_no, _("View this order")));
59
60         hyperlink_params($path_to_root . "/sales/customer_delivery.php", _("Confirm Order Quantities and Make Delivery"), "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_cart()
70 {
71         if ($_SESSION['Items']->trans_type=='delivery')
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         if (isset($_POST['email']))
87                 $_SESSION['Items']->email =$_POST['email'];
88         else    
89                 $_SESSION['Items']->email = '';
90
91         $_SESSION['Items']->customer_id = $_POST['customer_id'];
92         $_SESSION['Items']->Branch = $_POST['branch_id'];
93 }
94
95 //--------------------------------------------------------------------------------------------------
96
97 function copy_from_cart()
98 {
99         if ($_SESSION['Items']->trans_type=='delivery')
100                 $_POST['InvoiceText'] = $_SESSION['Items']->memo_;
101
102         $_POST['OrderDate'] = $_SESSION['Items']->orig_order_date;
103         $_POST['delivery_date'] = $_SESSION['Items']->delivery_date;
104         $_POST['cust_ref'] = $_SESSION['Items']->cust_ref;
105         $_POST['freight_cost'] = $_SESSION['Items']->freight_cost;
106         $_POST['Comments'] = $_SESSION['Items']->Comments;
107
108         $_POST['deliver_to'] = $_SESSION['Items']->deliver_to;
109         $_POST['delivery_address'] = $_SESSION['Items']->delivery_address;
110         $_POST['phone'] = $_SESSION['Items']->phone;
111         $_POST['Location'] = $_SESSION['Items']->Location;
112         $_POST['ship_via'] = $_SESSION['Items']->ship_via;
113
114         $_POST['customer_id'] = $_SESSION['Items']->customer_id;
115         $_POST['branch_id'] = $_SESSION['Items']->Branch;
116         
117 }
118
119
120 function can_process()
121 {
122             if ($_SESSION['Items']->trans_type=='delivery') 
123         {
124                 $edate = _("The entered delivery date is invalid.");
125         } 
126         else 
127         {       
128                 $edate = _("The entered order date is invalid.");
129         }       
130         if (!is_date($_POST['OrderDate'])) 
131         {
132                 display_error($edate);
133                 return false;
134         }
135         if (($_SESSION['Items']->trans_type=='delivery') && !is_date_in_fiscalyear($_POST['OrderDate'])) 
136         {
137                 display_error(_("The entered date is not in fiscal year"));
138                 return false;
139         }
140         else
141         {
142                 $_SESSION['Items']->orig_order_date = $_POST['OrderDate'];
143         }
144         if (count($_SESSION['Items']->line_items) == 0)
145         {
146                 display_error(_("You must enter at least one line entry."));
147                 return false;
148         }
149         if (strlen($_POST['deliver_to']) <= 1)
150         {
151                 display_error(_("You must enter the person or company to whom delivery should be made to."));
152                 return false;
153         }
154
155         if (strlen($_POST['delivery_address']) <= 1)
156         {
157                 display_error( _("You should enter the street address in the box provided. Orders cannot be accepted without a valid street address."));
158                 return false;
159         }
160
161         if ($_POST['freight_cost'] == "")
162                 $_POST['freight_cost'] = 0;
163
164         if (!is_numeric($_POST['freight_cost']))
165         {
166                 display_error(_("The shipping cost entered is expected to be numeric."));
167                 return false;
168         }
169
170         if (!is_date($_POST['delivery_date'])) 
171         {
172                 display_error(_("The delivery date is invalid."));
173                 return false;
174         }
175
176         if (date1_greater_date2($_SESSION['Items']->orig_order_date, $_POST['delivery_date'])) 
177         {
178                 display_error(_("The requested delivery date is before the date of the order."));
179                 return false;
180         }
181
182   return true;
183 }
184
185 //-----------------------------------------------------------------------------------------------------------
186
187 if (isset($_POST['ProcessOrder']) && can_process())
188 {
189  copy_to_cart(); 
190  
191  if($_SESSION['Items']->order_no == 0) {
192         $order_no = add_sales_order($_SESSION['Items']);
193         if ($_SESSION['Items']->trans_type=='delivery') 
194         {
195                 $_SESSION['Items']->memo_ = $_POST['InvoiceText'];
196                 $_SESSION['Items']->memo_ = str_replace("'", "\\'", $_SESSION['Items']->memo_);
197                 $_SESSION['Items']->order_no = $order_no;
198
199                 meta_forward("$path_to_root/sales/customer_delivery.php", "process_delivery=Yes");
200         } 
201         else 
202         {
203                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$order_no");
204         }
205  } else { // store modified sales order
206         update_sales_order($_SESSION['Items']->order_no, $_SESSION['Items']);
207         $order_no = $_SESSION['Items']->order_no;
208         meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$order_no");
209  }
210 }
211
212 //--------------------------------------------------------------------------------
213
214 function check_item_data() 
215 {
216         if (!is_numeric($_POST['qty']) || $_POST['qty'] < 0 || $_POST['Disc'] > 100 || 
217                 $_POST['Disc'] < 0)
218         {
219                 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."));
220                 return false;
221         } 
222         else
223         if (!is_numeric($_POST['price']) || $_POST['price']<0)
224         {
225                 display_error( _("Price for item must be entered and can not be less then 0"));
226                 return false;
227         } 
228         else
229 // should this be checked for delivery ?
230 //          if($_SESSION['Items']->some_already_delivered($_POST['stock_id']) != 0 &&
231 //              $_SESSION['Items']->line_items[$_POST['stock_id']]->price != $_POST['price']) 
232 //      {
233 //              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."));
234 //              return false;
235 //      } 
236 //      else
237 //          if($_SESSION['Items']->some_already_delivered($_POST['stock_id']) != 0 && 
238 //              $_SESSION['Items']->line_items[$_POST['stock_id']]->discount_percent != ($_POST['Disc']/100)) 
239 //      {
240 //              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."));
241 //              return false;
242 //      } 
243 //      else
244             if (isset($_POST['LineNo']) && isset($_SESSION['Items']->line_items[$_POST['LineNo']]) && $_SESSION['Items']->line_items[$_POST['LineNo']]->qty_done > $_POST['qty'])
245         {
246                 display_error(_("You attempting to make the quantity ordered a quantity less than has already been delivered. The quantity delivered cannot be modified retrospectively."));
247                 return false;
248         }       
249         return true;
250 }
251
252 function handle_update_item() 
253 {
254     if($_POST['UpdateItem'] != '' && check_item_data())
255     {
256         $_SESSION['Items']->update_cart_item($_POST['LineNo'], $_POST['qty'], 
257                 $_POST['price'], ($_POST['Disc'] / 100));
258     }
259 }
260
261 //--------------------------------------------------------------------------------
262
263 function handle_delete_item() 
264 {   
265     if($_GET['Delete'] != "")
266     {
267         $line_no = $_GET['Delete']; 
268         if($_SESSION['Items']->some_already_delivered($line_no) == 0)
269         {
270                 $_SESSION['Items']->remove_from_cart($line_no);
271         } 
272         else 
273         {
274                 display_error(_("This item cannot be deleted because some of it has already been delivered."));
275         }
276     }
277 }
278
279 //--------------------------------------------------------------------------------
280
281 function handle_new_item()
282 {
283         if (!check_item_data())
284                 return;
285         add_to_order($_SESSION['Items'], $_POST['stock_id'], $_POST['qty'], 
286                 $_POST['price'], $_POST['Disc']/100);
287
288         $_POST['StockID2'] = $_POST['stock_id'] = "";
289 }
290
291 //--------------------------------------------------------------------------------      
292
293 function  handle_cancel_order()
294 {
295         global $path_to_root;
296         
297     if ($_POST['CancelOrder'] != "") 
298     {
299         $ok_to_delete = 1;      //assume this in the first instance
300     
301                 if (($_SESSION['Items']->order_no != 0) && sales_order_has_deliveries($_SESSION['Items']->order_no)) 
302                 {
303                         $ok_to_delete = 0;
304                         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."));
305                 }
306     
307         if ($ok_to_delete == 1)
308         {
309                 if($_SESSION['Items']->order_no != 0)
310                 {
311                         delete_sales_order($_SESSION['Items']->order_no);
312                 }
313
314                         if ($_SESSION['Items']->trans_type=='delivery') 
315                 {
316                  display_note(_("This sales delivery has been cancelled as requested."), 1);
317                                  hyperlink_params($path_to_root . "/sales/sales_order_entry.php", _("Enter a New Sales Delivery"), SID . "&NewDelivery=Yes");
318                 } 
319                 else  
320                 {
321                  display_note(_("This sales order has been cancelled as requested."), 1);
322                                  hyperlink_params($path_to_root . "/sales/sales_order_entry.php", _("Enter a New Sales Order"), SID . "&NewOrder=Yes");
323                 }
324                 br(1);
325                 end_page();                     
326                 exit;
327         }
328     }
329 }
330         
331 //--------------------------------------------------------------------------------
332
333 function create_cart($type, $trans_num) 
334 {
335         /*New order entry - clear any existing order details from the Items object and initiate a newy*/
336         if (isset($_SESSION['Items']))
337         {
338                 unset ($_SESSION['Items']->line_items);
339                 unset ($_SESSION['Items']);
340         }
341         
342         $_SESSION['Items'] = new cart($type);
343
344   if( $trans_num==0 ) { // new transaction
345          $_SESSION['Items']->customer_id = "";
346          $_POST['OrderDate'] = Today();         
347          $_SESSION['Items']->order_no = 0;      
348          if (!is_date_in_fiscalyear($_POST['OrderDate']))
349                 $_POST['OrderDate'] = end_fiscalyear();
350          $_SESSION['Items']->orig_order_date = $_POST['OrderDate'];
351         } else { // read sales order to modify
352           $_SESSION['Items']->order_no = $trans_num;
353          /*read in all the selected order into the Items cart  */
354           read_sales_order($_SESSION['Items']->order_no, $_SESSION['Items']);
355                 copy_from_cart(); // load POST variables
356   }
357 }
358
359 //--------------------------------------------------------------------------------
360
361 if (isset($_POST['CancelOrder']))
362         handle_cancel_order();
363
364 if (isset($_GET['Delete']) || isset($_GET['Edit']))
365         copy_from_cart();
366         
367 if (isset($_GET['Delete']))
368         handle_delete_item();
369
370 if (isset($_POST['UpdateItem']))
371         handle_update_item();
372
373 if (isset($_POST['AddItem']))
374         handle_new_item();
375         
376 //--------------------------------------------------------------------------------      
377
378 check_db_has_stock_items(_("There are no inventory items defined in the system."));
379
380 check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));           
381
382 if ($_SESSION['Items']->trans_type=='delivery') 
383 {
384         $idate = _("Delivery Date:");
385         $orderitems = _("Delivery Note Items");
386         $deliverydetails = _("Enter Delivery Details and Confirm Dispatch");
387         $cancelorder = _("Cancel Delivery");
388
389 else
390 {
391         $idate = _("Order Date:");
392         $orderitems = _("Sales Order Items");
393         $deliverydetails = _("Enter Delivery Details and Confirm Order");
394         $cancelorder = _("Cancel Order");
395 }
396 start_form(false, true);
397
398 $customer_error = display_order_header($_SESSION['Items'], 
399         ($_SESSION['Items']->any_already_delivered() == 0), $idate);
400
401 if ($customer_error == "")
402 {
403         start_table("$table_style width=80%", 10);
404         echo "<tr><td>";                
405         display_order_summary($orderitems, $_SESSION['Items'], true);
406         echo "</td></tr>";
407         echo "<tr><td>";                
408         display_delivery_details($_SESSION['Items']);
409         echo "</td></tr>";
410         end_table(1);           
411
412 else
413 {
414         display_error($customer_error);
415 }
416
417 if ($_SESSION['Items']->trans_type=='delivery') 
418 {
419         $porder = _("Place Delivery");
420         $corder = _("Commit Delivery Changes");
421         $eorder = _("Edit Delivery Items");
422
423 else 
424 {
425         $porder = _("Place Order");
426         $corder = _("Commit Order Changes");
427         $eorder = _("Edit Order Items");
428 }
429
430 if ($_SESSION['Items']->order_no == 0)
431 {
432         submit_center_first('ProcessOrder', $porder);
433
434 else 
435 {
436         submit_center_first('ProcessOrder', $corder);
437 }
438
439   submit_center_last('CancelOrder', $cancelorder);
440         
441 end_form();
442
443 //--------------------------------------------------------------------------------
444 end_page();
445 ?>