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