Merged last changes from stable.
[fa-stable.git] / purchasing / supplier_invoice.php
1 <?php
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 ***********************************************************************/
12 $page_security = 'SA_SUPPLIERINVOICE';
13 $path_to_root = "..";
14
15 include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
16
17 include_once($path_to_root . "/includes/session.inc");
18
19 include_once($path_to_root . "/includes/banking.inc");
20 include_once($path_to_root . "/includes/data_checks.inc");
21
22 include_once($path_to_root . "/purchasing/includes/purchasing_ui.inc");
23 $js = "";
24 if ($use_popup_windows)
25         $js .= get_js_open_window(900, 500);
26 if ($use_date_picker)
27         $js .= get_js_date_picker();
28 //----------------------------------------------------------------------------------------
29
30 check_db_has_suppliers(_("There are no suppliers defined in the system."));
31
32 //--------------------------------------------------------------------------------------------------
33
34 if (isset($_GET['New']))
35 {
36         if (isset( $_SESSION['supp_trans']))
37         {
38                 unset ($_SESSION['supp_trans']->grn_items);
39                 unset ($_SESSION['supp_trans']->gl_codes);
40                 unset ($_SESSION['supp_trans']);
41         }
42         $help_context = "Enter Supplier Invoice";
43         $_SESSION['page_title'] = _("Enter Supplier Invoice");
44
45         $_SESSION['supp_trans'] = new supp_trans(ST_SUPPINVOICE);
46 } else if(isset($_GET['ModifyInvoice'])) {
47         $help_context = 'Modifying Purchase Invoice';
48         $_SESSION['page_title'] = sprintf( _("Modifying Purchase Invoice # %d"), $_GET['ModifyInvoice']);
49         $_SESSION['supp_trans'] = new supp_trans(ST_SUPPINVOICE, $_GET['ModifyInvoice']);
50 }
51
52 page($_SESSION['page_title'], false, false, "", $js);
53
54 if (isset($_GET['ModifyInvoice']))
55         check_is_editable(ST_SUPPINVOICE, $_GET['ModifyInvoice']);
56
57 //---------------------------------------------------------------------------------------------------------------
58
59 if (isset($_GET['AddedID'])) 
60 {
61         $invoice_no = $_GET['AddedID'];
62         $trans_type = ST_SUPPINVOICE;
63
64
65     echo "<center>";
66     display_notification_centered(_("Supplier invoice has been processed."));
67     display_note(get_trans_view_str($trans_type, $invoice_no, _("View this Invoice")));
68
69         display_note(get_gl_view_str($trans_type, $invoice_no, _("View the GL Journal Entries for this Invoice")), 1);
70
71         hyperlink_no_params("$path_to_root/purchasing/supplier_payment.php", _("Entry supplier &payment for this invoice"));
72
73         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Invoice"), "New=1");
74
75         hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$invoice_no");
76         
77         display_footer_exit();
78 }
79
80 //--------------------------------------------------------------------------------------------------
81
82 if (isset($_GET['New']))
83 {
84         if (isset( $_SESSION['supp_trans']))
85         {
86                 unset ($_SESSION['supp_trans']->grn_items);
87                 unset ($_SESSION['supp_trans']->gl_codes);
88                 unset ($_SESSION['supp_trans']);
89         }
90
91         $_SESSION['supp_trans'] = new supp_trans(ST_SUPPINVOICE);
92 } else if(isset($_GET['ModifyInvoice'])) {
93         $_SESSION['supp_trans'] = new supp_trans(ST_SUPPINVOICE, $_GET['ModifyInvoice']);
94 }
95
96 //--------------------------------------------------------------------------------------------------
97 function clear_fields()
98 {
99         global $Ajax;
100         
101         unset($_POST['gl_code']);
102         unset($_POST['dimension_id']);
103         unset($_POST['dimension2_id']);
104         unset($_POST['amount']);
105         unset($_POST['memo_']);
106         unset($_POST['AddGLCodeToTrans']);
107         $Ajax->activate('gl_items');
108         set_focus('gl_code');
109 }
110 //------------------------------------------------------------------------------------------------
111 //      GL postings are often entered in the same form to two accounts
112 //  so fileds are cleared only on user demand.
113 //
114 if (isset($_POST['ClearFields']))
115 {
116         clear_fields();
117 }
118
119 if (isset($_POST['AddGLCodeToTrans'])){
120
121         $Ajax->activate('gl_items');
122         $input_error = false;
123
124         $result = get_gl_account_info($_POST['gl_code']);
125         if (db_num_rows($result) == 0)
126         {
127                 display_error(_("The account code entered is not a valid code, this line cannot be added to the transaction."));
128                 set_focus('gl_code');
129                 $input_error = true;
130         }
131         else
132         {
133                 $myrow = db_fetch_row($result);
134                 $gl_act_name = $myrow[1];
135                 if (!check_num('amount'))
136                 {
137                         display_error(_("The amount entered is not numeric. This line cannot be added to the transaction."));
138                         set_focus('amount');
139                         $input_error = true;
140                 }
141         }
142
143         if (!is_tax_gl_unique(get_post('gl_code'))) {
144                 display_error(_("Cannot post to GL account used by more than one tax type."));
145                 set_focus('gl_code');
146                 $input_error = true;
147         }
148
149         if ($input_error == false)
150         {
151                 $_SESSION['supp_trans']->add_gl_codes_to_trans($_POST['gl_code'], $gl_act_name,
152                         $_POST['dimension_id'], $_POST['dimension2_id'], 
153                         input_num('amount'), $_POST['memo_']);
154                 set_focus('gl_code');
155         }
156 }
157
158 //------------------------------------------------------------------------------------------------
159
160 function check_data()
161 {
162         global $Refs;
163
164         if (!$_SESSION['supp_trans']->is_valid_trans_to_post())
165         {
166                 display_error(_("The invoice cannot be processed because the there are no items or values on the invoice.  Invoices are expected to have a charge."));
167                 return false;
168         }
169
170         if (!$Refs->is_valid($_SESSION['supp_trans']->reference)) 
171         {
172                 display_error(_("You must enter an invoice reference."));
173                 set_focus('reference');
174                 return false;
175         }
176
177         if (!is_new_reference($_SESSION['supp_trans']->reference, ST_SUPPINVOICE, $_SESSION['supp_trans']->trans_no))
178         {
179                 display_error(_("The entered reference is already in use."));
180                 set_focus('reference');
181                 return false;
182         }
183
184         if (!$Refs->is_valid($_SESSION['supp_trans']->supp_reference)) 
185         {
186                 display_error(_("You must enter a supplier's invoice reference."));
187                 set_focus('supp_reference');
188                 return false;
189         }
190
191         if (!is_date( $_SESSION['supp_trans']->tran_date))
192         {
193                 display_error(_("The invoice as entered cannot be processed because the invoice date is in an incorrect format."));
194                 set_focus('trans_date');
195                 return false;
196         } 
197         elseif (!is_date_in_fiscalyear($_SESSION['supp_trans']->tran_date)) 
198         {
199                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
200                 set_focus('trans_date');
201                 return false;
202         }
203         if (!is_date( $_SESSION['supp_trans']->due_date))
204         {
205                 display_error(_("The invoice as entered cannot be processed because the due date is in an incorrect format."));
206                 set_focus('due_date');
207                 return false;
208         }
209
210         if (is_reference_already_there($_SESSION['supp_trans']->supplier_id, $_POST['supp_reference'], $_SESSION['supp_trans']->trans_no))
211         {       /*Transaction reference already entered */
212                 display_error(_("This invoice number has already been entered. It cannot be entered again.") . " (" . $_POST['supp_reference'] . ")");
213                 return false;
214         }
215
216         return true;
217 }
218
219 //--------------------------------------------------------------------------------------------------
220
221 function handle_commit_invoice()
222 {
223         copy_to_trans($_SESSION['supp_trans']);
224
225         if (!check_data())
226                 return;
227         $inv = $_SESSION['supp_trans'];
228         $invoice_no = add_supp_invoice($inv);
229
230         // presume supplier data need correction
231         if ($inv->stored_algorithm != $inv->tax_algorithm)
232                 update_supp_tax_algorithm($inv->supplier_id, $inv->tax_algorithm);
233
234     $_SESSION['supp_trans']->clear_items();
235     unset($_SESSION['supp_trans']);
236
237         meta_forward($_SERVER['PHP_SELF'], "AddedID=$invoice_no");
238 }
239
240 //--------------------------------------------------------------------------------------------------
241
242 if (isset($_POST['PostInvoice']))
243 {
244         handle_commit_invoice();
245 }
246
247 function check_item_data($n)
248 {
249         global $check_price_charged_vs_order_price,
250                 $check_qty_charged_vs_del_qty, $SysPrefs;
251
252         if (!check_num('this_quantity_inv'.$n, 0) || input_num('this_quantity_inv'.$n)==0)
253         {
254                 display_error( _("The quantity to invoice must be numeric and greater than zero."));
255                 set_focus('this_quantity_inv'.$n);
256                 return false;
257         }
258
259         if (!check_num('ChgPrice'.$n))
260         {
261                 display_error( _("The price is not numeric."));
262                 set_focus('ChgPrice'.$n);
263                 return false;
264         }
265
266         $margin = $SysPrefs->over_charge_allowance();
267         if ($check_price_charged_vs_order_price == True)
268         {
269                 if ($_POST['order_price'.$n]!=input_num('ChgPrice'.$n)) {
270                      if ($_POST['order_price'.$n]==0 ||
271                                 input_num('ChgPrice'.$n)/$_POST['order_price'.$n] >
272                             (1 + ($margin/ 100)))
273                     {
274                         display_error(_("The price being invoiced is more than the purchase order price by more than the allowed over-charge percentage. The system is set up to prohibit this. See the system administrator to modify the set up parameters if necessary.") .
275                         _("The over-charge percentage allowance is :") . $margin . "%");
276                         set_focus('ChgPrice'.$n);
277                         return false;
278                     }
279                 }
280         }
281
282         if ($check_qty_charged_vs_del_qty == True)
283         {
284                 if (input_num('this_quantity_inv'.$n) / ($_POST['qty_recd'.$n] - $_POST['prev_quantity_inv'.$n]) >
285                         (1+ ($margin / 100)))
286                 {
287                         display_error( _("The quantity being invoiced is more than the outstanding quantity by more than the allowed over-charge percentage. The system is set up to prohibit this. See the system administrator to modify the set up parameters if necessary.")
288                         . _("The over-charge percentage allowance is :") . $margin . "%");
289                         set_focus('this_quantity_inv'.$n);
290                         return false;
291                 }
292         }
293
294         return true;
295 }
296
297 function commit_item_data($n)
298 {
299         if (check_item_data($n))
300         {
301         if (input_num('this_quantity_inv'.$n) >= ($_POST['qty_recd'.$n] - $_POST['prev_quantity_inv'.$n]))
302         {
303                 $complete = true;
304         }
305         else
306         {
307                 $complete = false;
308         }
309
310                 $_SESSION['supp_trans']->add_grn_to_trans($n, $_POST['po_detail_item'.$n],
311                         $_POST['item_code'.$n], $_POST['item_description'.$n], $_POST['qty_recd'.$n],
312                         $_POST['prev_quantity_inv'.$n], input_num('this_quantity_inv'.$n),
313                         $_POST['order_price'.$n], input_num('ChgPrice'.$n), $complete,
314                         $_POST['std_cost_unit'.$n], "");
315                 unset($_POST['mantax']);
316         }
317 }
318
319 //-----------------------------------------------------------------------------------------
320
321 $id = find_submit('grn_item_id');
322 if ($id != -1)
323 {
324         commit_item_data($id);
325 }
326
327 if (isset($_POST['InvGRNAll']))
328 {
329         foreach($_POST as $postkey=>$postval )
330     {
331                 if (strpos($postkey, "qty_recd") === 0)
332                 {
333                         $id = substr($postkey, strlen("qty_recd"));
334                         $id = (int)$id;
335                         commit_item_data($id);
336                 }
337     }
338 }       
339
340 //--------------------------------------------------------------------------------------------------
341 $id3 = find_submit('Delete');
342 if ($id3 != -1)
343 {
344         $_SESSION['supp_trans']->remove_grn_from_trans($id3);
345         unset($_POST['mantax']);
346         $Ajax->activate('grn_items');
347         $Ajax->activate('inv_tot');
348 }
349
350 $id4 = find_submit('Delete2');
351 if ($id4 != -1)
352 {
353         $_SESSION['supp_trans']->remove_gl_codes_from_trans($id4);
354         clear_fields();
355         $Ajax->activate('gl_items');
356         $Ajax->activate('inv_tot');
357 }
358
359 $id2 = -1;
360 if ($_SESSION["wa_current_user"]->can_access('SA_GRNDELETE'))
361 {
362         $id2 = find_submit('void_item_id');
363         if ($id2 != -1) 
364         {
365                 remove_not_invoice_item($id2);
366                 display_notification(sprintf(_('All yet non-invoiced items on delivery line # %d has been removed.'), $id2));
367
368         }               
369 }
370
371 if (isset($_POST['go']))
372 {
373         $Ajax->activate('gl_items');
374         display_quick_entries($_SESSION['supp_trans'], $_POST['qid'], input_num('totamount'), QE_SUPPINV);
375         $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
376         $Ajax->activate('inv_tot');
377 }
378
379 start_form();
380
381 invoice_header($_SESSION['supp_trans']);
382
383 if ($_POST['supplier_id']=='') 
384                 display_error(_("There is no supplier selected."));
385 else {
386         display_grn_items($_SESSION['supp_trans'], 1);
387
388         display_gl_items($_SESSION['supp_trans'], 1);
389
390         div_start('inv_tot');
391         invoice_totals($_SESSION['supp_trans']);
392         div_end();
393
394 }
395
396 //-----------------------------------------------------------------------------------------
397
398 if ($id != -1 || $id2 != -1)
399 {
400         $Ajax->activate('grn_items');
401         $Ajax->activate('inv_tot');
402 }
403
404 if (get_post('AddGLCodeToTrans'))
405         $Ajax->activate('inv_tot');
406
407 br();
408 submit_center('PostInvoice', _("Enter Invoice"), true, '', 'default');
409 br();
410
411 end_form();
412
413 //--------------------------------------------------------------------------------------------------
414
415 end_page();
416 ?>