2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
13 $page_security = 'SA_PURCHASEORDER';
14 include_once($path_to_root . "/purchasing/includes/po_class.inc");
15 include_once($path_to_root . "/includes/session.inc");
16 include_once($path_to_root . "/purchasing/includes/purchasing_ui.inc");
17 include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
18 include_once($path_to_root . "/reporting/includes/reporting.inc");
20 set_page_security( @$_SESSION['PO']->trans_type,
21 array( ST_PURCHORDER => 'SA_PURCHASEORDER',
22 ST_SUPPRECEIVE => 'SA_GRN',
23 ST_SUPPINVOICE => 'SA_SUPPLIERINVOICE'),
24 array( 'NewOrder' => 'SA_PURCHASEORDER',
25 'ModifyOrderNumber' => 'SA_PURCHASEORDER',
26 'AddedID' => 'SA_PURCHASEORDER',
28 'AddedGRN' => 'SA_GRN',
29 'NewInvoice' => 'SA_SUPPLIERINVOICE',
30 'AddedPI' => 'SA_SUPPLIERINVOICE')
34 if ($SysPrefs->use_popup_windows)
35 $js .= get_js_open_window(900, 500);
36 if (user_use_date_picker())
37 $js .= get_js_date_picker();
39 if (isset($_GET['ModifyOrderNumber']) && is_numeric($_GET['ModifyOrderNumber'])) {
41 $_SESSION['page_title'] = _($help_context = "Modify Purchase Order #") . $_GET['ModifyOrderNumber'];
42 create_new_po(ST_PURCHORDER, $_GET['ModifyOrderNumber']);
44 } elseif (isset($_GET['NewOrder'])) {
46 $_SESSION['page_title'] = _($help_context = "Purchase Order Entry");
47 create_new_po(ST_PURCHORDER, 0);
49 } elseif (isset($_GET['NewGRN'])) {
51 $_SESSION['page_title'] = _($help_context = "Direct GRN Entry");
52 create_new_po(ST_SUPPRECEIVE, 0);
54 } elseif (isset($_GET['NewInvoice'])) {
56 create_new_po(ST_SUPPINVOICE, 0);
59 if (isset($_GET['FixedAsset'])) {
60 $_SESSION['page_title'] = _($help_context = "Fixed Asset Purchase Invoice Entry");
61 $_SESSION['PO']->fixed_asset = true;
63 $_SESSION['page_title'] = _($help_context = "Direct Purchase Invoice Entry");
66 page($_SESSION['page_title'], false, false, "", $js);
68 if (isset($_GET['ModifyOrderNumber']))
69 check_is_editable(ST_PURCHORDER, $_GET['ModifyOrderNumber']);
71 //---------------------------------------------------------------------------------------------------
73 check_db_has_suppliers(_("There are no suppliers defined in the system."));
75 //---------------------------------------------------------------------------------------------------------------
77 if (isset($_GET['AddedID']))
79 $order_no = $_GET['AddedID'];
80 $trans_type = ST_PURCHORDER;
82 if (!isset($_GET['Updated']))
83 display_notification_centered(_("Purchase Order has been entered"));
85 display_notification_centered(_("Purchase Order has been updated") . " #$order_no");
86 display_note(get_trans_view_str($trans_type, $order_no, _("&View this order")), 0, 1);
88 display_note(print_document_link($order_no, _("&Print This Order"), true, $trans_type), 0, 1);
90 display_note(print_document_link($order_no, _("&Email This Order"), true, $trans_type, false, "printlink", "", 1));
92 hyperlink_params($path_to_root . "/purchasing/po_receive_items.php", _("&Receive Items on this Purchase Order"), "PONumber=$order_no");
94 // TODO, for fixed asset
95 hyperlink_params($_SERVER['PHP_SELF'], _("Enter &Another Purchase Order"), "NewOrder=yes");
97 hyperlink_no_params($path_to_root."/purchasing/inquiry/po_search.php", _("Select An &Outstanding Purchase Order"));
99 display_footer_exit();
101 } elseif (isset($_GET['AddedGRN'])) {
103 $trans_no = $_GET['AddedGRN'];
104 $trans_type = ST_SUPPRECEIVE;
106 display_notification_centered(_("Direct GRN has been entered"));
108 display_note(get_trans_view_str($trans_type, $trans_no, _("&View this GRN")), 0);
110 $clearing_act = get_company_pref('grn_clearing_act');
112 display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL Journal Entries for this Delivery")), 1);
114 hyperlink_params("$path_to_root/purchasing/supplier_invoice.php",
115 _("Entry purchase &invoice for this receival"), "New=1");
117 hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"),
118 "filterType=$trans_type&trans_no=$trans_no");
120 hyperlink_params($_SERVER['PHP_SELF'], _("Enter &Another GRN"), "NewGRN=Yes");
122 display_footer_exit();
124 } elseif (isset($_GET['AddedPI'])) {
126 $trans_no = $_GET['AddedPI'];
127 $trans_type = ST_SUPPINVOICE;
129 display_notification_centered(_("Direct Purchase Invoice has been entered"));
131 display_note(get_trans_view_str($trans_type, $trans_no, _("&View this Invoice")), 0);
133 display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL Journal Entries for this Invoice")), 1);
135 hyperlink_params("$path_to_root/purchasing/supplier_payment.php", _("Entry supplier &payment for this invoice"),
136 "trans_type=$trans_type&PInvoice=".$trans_no);
138 hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"),
139 "filterType=$trans_type&trans_no=$trans_no");
141 hyperlink_params($_SERVER['PHP_SELF'], _("Enter &Another Direct Invoice"), "NewInvoice=Yes");
143 display_footer_exit();
146 if ($_SESSION['PO']->fixed_asset)
147 check_db_has_purchasable_fixed_assets(_("There are no purchasable fixed assets defined in the system."));
149 check_db_has_purchasable_items(_("There are no purchasable inventory items defined in the system."));
150 //--------------------------------------------------------------------------------------------------
152 function line_start_focus() {
155 $Ajax->activate('items_table');
156 set_focus('_stock_id_edit');
158 //--------------------------------------------------------------------------------------------------
160 function unset_form_variables() {
161 unset($_POST['stock_id']);
162 unset($_POST['qty']);
163 unset($_POST['price']);
164 unset($_POST['req_del_date']);
167 //---------------------------------------------------------------------------------------------------
169 function handle_delete_item($line_no)
171 if($_SESSION['PO']->some_already_received($line_no) == 0)
173 $_SESSION['PO']->remove_from_order($line_no);
174 unset_form_variables();
178 display_error(_("This item cannot be deleted because some of it has already been received."));
183 //---------------------------------------------------------------------------------------------------
185 function handle_cancel_po()
187 global $path_to_root;
189 //need to check that not already dispatched or invoiced by the supplier
190 if(($_SESSION['PO']->order_no != 0) &&
191 $_SESSION['PO']->any_already_received() == 1)
193 display_error(_("This order cannot be cancelled because some of it has already been received.")
194 . "<br>" . _("The line item quantities may be modified to quantities more than already received. prices cannot be altered for lines that have already been received and quantities cannot be reduced below the quantity already received."));
198 $fixed_asset = $_SESSION['PO']->fixed_asset;
200 if($_SESSION['PO']->order_no != 0)
201 delete_po($_SESSION['PO']->order_no);
203 unset($_SESSION['PO']);
206 meta_forward($path_to_root.'/index.php','application=assets');
208 meta_forward($path_to_root.'/index.php','application=AP');
211 $_SESSION['PO']->clear_items();
212 $_SESSION['PO'] = new purch_order;
214 display_notification(_("This purchase order has been cancelled."));
216 hyperlink_params($path_to_root . "/purchasing/po_entry_items.php", _("Enter a new purchase order"), "NewOrder=Yes");
223 //---------------------------------------------------------------------------------------------------
225 function check_data()
227 if(!get_post('stock_id_text', true)) {
228 display_error( _("Item description cannot be empty."));
229 set_focus('stock_id_edit');
233 $dec = get_qty_dec($_POST['stock_id']);
234 $min = 1 / pow(10, $dec);
235 if (!check_num('qty',$min))
237 $min = number_format2($min, $dec);
238 display_error(_("The quantity of the order item must be numeric and not less than ").$min);
243 if (!check_num('price', 0))
245 display_error(_("The price entered must be numeric and not less than zero."));
249 if ($_SESSION['PO']->trans_type == ST_PURCHORDER && !is_date($_POST['req_del_date'])){
250 display_error(_("The date entered is in an invalid format."));
251 set_focus('req_del_date');
258 //---------------------------------------------------------------------------------------------------
260 function handle_update_item()
262 $allow_update = check_data();
266 if ($_SESSION['PO']->line_items[$_POST['line_no']]->qty_inv > input_num('qty') ||
267 $_SESSION['PO']->line_items[$_POST['line_no']]->qty_received > input_num('qty'))
269 display_error(_("You are attempting to make the quantity ordered a quantity less than has already been invoiced or received. This is prohibited.") .
270 "<br>" . _("The quantity received can only be modified by entering a negative receipt and the quantity invoiced can only be reduced by entering a credit note against this item."));
275 $_SESSION['PO']->update_order_item($_POST['line_no'], input_num('qty'), input_num('price'),
276 @$_POST['req_del_date'], $_POST['item_description'] );
277 unset_form_variables();
282 //---------------------------------------------------------------------------------------------------
284 function handle_add_new_item()
286 $allow_update = check_data();
288 if ($allow_update == true)
290 if (count($_SESSION['PO']->line_items) > 0)
292 foreach ($_SESSION['PO']->line_items as $order_item)
294 /* do a loop round the items on the order to see that the item
295 is not already on this order */
296 if (($order_item->stock_id == $_POST['stock_id']))
298 display_warning(_("The selected item is already on this order."));
300 } /* end of the foreach loop to look for pre-existing items of the same code */
303 if ($allow_update == true)
305 $result = get_short_info($_POST['stock_id']);
307 if (db_num_rows($result) == 0)
309 $allow_update = false;
314 $_SESSION['PO']->add_to_order (count($_SESSION['PO']->line_items), $_POST['stock_id'], input_num('qty'),
315 get_post('stock_id_text'), //$myrow["description"],
316 input_num('price'), '', // $myrow["units"], (retrived in cart)
317 $_SESSION['PO']->trans_type == ST_PURCHORDER ? $_POST['req_del_date'] : '', 0, 0);
319 unset_form_variables();
320 $_POST['stock_id'] = "";
324 display_error(_("The selected item does not exist or it is a kit part and therefore cannot be purchased."));
327 } /* end of if not already on the order and allow input was true*/
332 //---------------------------------------------------------------------------------------------------
334 function can_commit()
336 if (!get_post('supplier_id'))
338 display_error(_("There is no supplier selected."));
339 set_focus('supplier_id');
343 if (!is_date($_POST['OrderDate']))
345 display_error(_("The entered order date is invalid."));
346 set_focus('OrderDate');
349 if (($_SESSION['PO']->trans_type == ST_SUPPRECEIVE || $_SESSION['PO']->trans_type == ST_SUPPINVOICE)
350 && !is_date_in_fiscalyear($_POST['OrderDate'])) {
351 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
352 set_focus('OrderDate');
356 if (($_SESSION['PO']->trans_type==ST_SUPPINVOICE) && !is_date($_POST['due_date']))
358 display_error(_("The entered due date is invalid."));
359 set_focus('due_date');
363 if (!$_SESSION['PO']->order_no)
365 if (!check_reference(get_post('ref'), $_SESSION['PO']->trans_type))
372 if ($_SESSION['PO']->trans_type == ST_SUPPINVOICE && trim(get_post('supp_ref')) == false)
374 display_error(_("You must enter a supplier's invoice reference."));
375 set_focus('supp_ref');
378 if ($_SESSION['PO']->trans_type==ST_SUPPINVOICE
379 && is_reference_already_there($_SESSION['PO']->supplier_id, get_post('supp_ref'), $_SESSION['PO']->order_no))
381 display_error(_("This invoice number has already been entered. It cannot be entered again.") . " (" . get_post('supp_ref') . ")");
382 set_focus('supp_ref');
385 if ($_SESSION['PO']->trans_type == ST_PURCHORDER && get_post('delivery_address') == '')
387 display_error(_("There is no delivery address specified."));
388 set_focus('delivery_address');
391 if (get_post('StkLocation') == '')
393 display_error(_("There is no location specified to move any items into."));
394 set_focus('StkLocation');
397 if (!db_has_currency_rates($_SESSION['PO']->curr_code, $_POST['OrderDate'], true))
399 if ($_SESSION['PO']->order_has_items() == false)
401 display_error (_("The order cannot be placed because there are no lines entered on this order."));
404 if (floatcmp(input_num('prep_amount'), $_SESSION['PO']->get_trans_total()) > 0)
406 display_error(_("Required prepayment is greater than total invoice value."));
407 set_focus('prep_amount');
414 function handle_commit_order()
416 $cart = &$_SESSION['PO'];
421 new_doc_date($cart->orig_order_date);
422 if ($cart->order_no == 0) { // new po/grn/invoice
423 $trans_no = add_direct_supp_trans($cart);
425 unset($_SESSION['PO']);
426 if ($cart->trans_type == ST_PURCHORDER)
427 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
428 elseif ($cart->trans_type == ST_SUPPRECEIVE)
429 meta_forward($_SERVER['PHP_SELF'], "AddedGRN=$trans_no");
431 meta_forward($_SERVER['PHP_SELF'], "AddedPI=$trans_no");
433 } else { // order modification
434 $order_no = update_po($cart);
435 unset($_SESSION['PO']);
436 meta_forward($_SERVER['PHP_SELF'], "AddedID=$order_no&Updated=1");
440 //---------------------------------------------------------------------------------------------------
441 if (isset($_POST['update'])) {
443 $Ajax->activate('items_table');
446 $id = find_submit('Delete');
448 handle_delete_item($id);
450 if (isset($_POST['Commit']))
452 handle_commit_order();
454 if (isset($_POST['UpdateLine']))
455 handle_update_item();
457 if (isset($_POST['EnterLine']))
458 handle_add_new_item();
460 if (isset($_POST['CancelOrder']))
463 if (isset($_POST['CancelUpdate']))
464 unset_form_variables();
466 if (isset($_POST['CancelUpdate']) || isset($_POST['UpdateLine'])) {
470 //---------------------------------------------------------------------------------------------------
474 display_po_header($_SESSION['PO']);
477 display_po_items($_SESSION['PO']);
479 start_table(TABLESTYLE2);
482 if ($_SESSION['PO']->trans_type == ST_SUPPINVOICE) {
483 cash_accounts_list_row(_("Payment:"), 'cash_account', null, false, _('Delayed'));
486 textarea_row(_("Memo:"), 'Comments', null, 70, 4);
490 div_start('controls', 'items_table');
491 $process_txt = _("Place Order");
492 $update_txt = _("Update Order");
493 $cancel_txt = _("Cancel Order");
494 if ($_SESSION['PO']->trans_type == ST_SUPPRECEIVE) {
495 $process_txt = _("Process GRN");
496 $update_txt = _("Update GRN");
497 $cancel_txt = _("Cancel GRN");
499 elseif ($_SESSION['PO']->trans_type == ST_SUPPINVOICE) {
500 $process_txt = _("Process Invoice");
501 $update_txt = _("Update Invoice");
502 $cancel_txt = _("Cancel Invoice");
504 if ($_SESSION['PO']->order_has_items())
506 if ($_SESSION['PO']->order_no)
507 submit_center_first('Commit', $update_txt, '', 'default');
509 submit_center_first('Commit', $process_txt, '', 'default');
510 submit_center_last('CancelOrder', $cancel_txt);
513 submit_center('CancelOrder', $cancel_txt, true, false, 'cancel');
515 //---------------------------------------------------------------------------------------------------