Bank Payment, Bank Deposit: all parameters now passed to write_bank_transaction(...
[fa-stable.git] / gl / gl_bank.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 $path_to_root = "..";
13 include_once($path_to_root . "/includes/ui/items_cart.inc");
14 include_once($path_to_root . "/includes/session.inc");
15 $page_security = isset($_GET['NewPayment']) || 
16         @($_SESSION['pay_items']->trans_type==ST_BANKPAYMENT)
17  ? 'SA_PAYMENT' : 'SA_DEPOSIT';
18
19 include_once($path_to_root . "/includes/date_functions.inc");
20 include_once($path_to_root . "/includes/data_checks.inc");
21
22 include_once($path_to_root . "/gl/includes/ui/gl_bank_ui.inc");
23 include_once($path_to_root . "/gl/includes/gl_db.inc");
24 include_once($path_to_root . "/gl/includes/gl_ui.inc");
25 include_once($path_to_root . "/admin/db/attachments_db.inc");
26
27 $js = '';
28 if ($SysPrefs->use_popup_windows)
29         $js .= get_js_open_window(800, 500);
30 if (user_use_date_picker())
31         $js .= get_js_date_picker();
32
33 if (isset($_GET['NewPayment'])) {
34         $_SESSION['page_title'] = _($help_context = "Bank Account Payment Entry");
35         create_cart(ST_BANKPAYMENT, 0);
36 } else if(isset($_GET['NewDeposit'])) {
37         $_SESSION['page_title'] = _($help_context = "Bank Account Deposit Entry");
38         create_cart(ST_BANKDEPOSIT, 0);
39 } else if(isset($_GET['ModifyPayment'])) {
40         $_SESSION['page_title'] = _($help_context = "Modify Bank Account Entry")." #".$_GET['trans_no'];
41         create_cart(ST_BANKPAYMENT, $_GET['trans_no']);
42 } else if(isset($_GET['ModifyDeposit'])) {
43         $_SESSION['page_title'] = _($help_context = "Modify Bank Deposit Entry")." #".$_GET['trans_no'];
44         create_cart(ST_BANKDEPOSIT, $_GET['trans_no']);
45 }
46 page($_SESSION['page_title'], false, false, '', $js);
47
48 //-----------------------------------------------------------------------------------------------
49 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
50
51 if (isset($_GET['ModifyDeposit']) || isset($_GET['ModifyPayment']))
52         check_is_editable($_SESSION['pay_items']->trans_type, $_SESSION['pay_items']->order_id);
53
54 //----------------------------------------------------------------------------------------
55 if (list_updated('PersonDetailID')) {
56         $br = get_branch(get_post('PersonDetailID'));
57         $_POST['person_id'] = $br['debtor_no'];
58         $Ajax->activate('person_id');
59 }
60
61 //--------------------------------------------------------------------------------------------------
62 function line_start_focus() {
63         global  $Ajax;
64
65     unset($_POST['amount']);
66     unset($_POST['dimension_id']);
67     unset($_POST['dimension2_id']);
68     unset($_POST['LineMemo']);
69         $Ajax->activate('items_table');
70         $Ajax->activate('footer');
71         set_focus('_code_id_edit');
72 }
73
74 //-----------------------------------------------------------------------------------------------
75
76 if (isset($_GET['AddedID']))
77 {
78         $trans_no = $_GET['AddedID'];
79         $trans_type = ST_BANKPAYMENT;
80
81         display_notification_centered(sprintf(_("Payment %d has been entered"), $trans_no));
82
83         display_note(get_gl_view_str($trans_type, $trans_no, _("&View the GL Postings for this Payment")));
84
85         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another &Payment"), "NewPayment=yes");
86
87         hyperlink_params($_SERVER['PHP_SELF'], _("Enter A &Deposit"), "NewDeposit=yes");
88
89         hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
90
91         display_footer_exit();
92 }
93
94 if (isset($_GET['UpdatedID']))
95 {
96         $trans_no = $_GET['UpdatedID'];
97         $trans_type = ST_BANKPAYMENT;
98
99         display_notification_centered(sprintf(_("Payment %d has been modified"), $trans_no));
100
101         display_note(get_gl_view_str($trans_type, $trans_no, _("&View the GL Postings for this Payment")));
102
103         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another &Payment"), "NewPayment=yes");
104
105         hyperlink_params($_SERVER['PHP_SELF'], _("Enter A &Deposit"), "NewDeposit=yes");
106
107         display_footer_exit();
108 }
109
110 if (isset($_GET['AddedDep']))
111 {
112         $trans_no = $_GET['AddedDep'];
113         $trans_type = ST_BANKDEPOSIT;
114
115         display_notification_centered(sprintf(_("Deposit %d has been entered"), $trans_no));
116
117         display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL Postings for this Deposit")));
118
119         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Deposit"), "NewDeposit=yes");
120
121         hyperlink_params($_SERVER['PHP_SELF'], _("Enter A Payment"), "NewPayment=yes");
122
123         display_footer_exit();
124 }
125 if (isset($_GET['UpdatedDep']))
126 {
127         $trans_no = $_GET['UpdatedDep'];
128         $trans_type = ST_BANKDEPOSIT;
129
130         display_notification_centered(sprintf(_("Deposit %d has been modified"), $trans_no));
131
132         display_note(get_gl_view_str($trans_type, $trans_no, _("&View the GL Postings for this Deposit")));
133
134         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another &Deposit"), "NewDeposit=yes");
135
136         hyperlink_params($_SERVER['PHP_SELF'], _("Enter A &Payment"), "NewPayment=yes");
137
138         display_footer_exit();
139 }
140
141 //--------------------------------------------------------------------------------------------------
142
143 function create_cart($type, $trans_no)
144 {
145         global $Refs;
146
147         $cart = new items_cart($type);
148     $cart->order_id = $trans_no;
149
150         if ($trans_no) {
151                 $bank_trans = db_fetch(get_bank_trans($type, $trans_no));
152                 $cart->bank_act = $bank_trans["bank_act"];
153
154                 $cart->reference = $bank_trans["ref"];
155
156                 if ($bank_trans["person_type_id"] == PT_CUSTOMER)
157                 {
158                         $trans = get_customer_trans($trans_no, $type);  
159                         $cart->person_id = $trans["debtor_no"];
160                         $cart->person_detail_id = $trans["branch_code"];
161                 }
162                 elseif ($bank_trans["person_type_id"] == PT_SUPPLIER)
163                 {
164                         $trans = get_supp_trans($trans_no, $type);
165                         $cart->person_id = $trans["supplier_id"];
166                 }
167                 elseif ($bank_trans["person_type_id"] == PT_MISC)
168                         $cart->person_id = $bank_trans["person_id"];
169                 elseif ($bank_trans["person_type_id"] == PT_QUICKENTRY)
170                         $cart->person_id = $bank_trans["person_id"];
171                 else 
172                         $cart->person_id = $bank_trans["person_id"];
173
174                 $cart->memo_ = get_comments_string($type, $trans_no);
175                 $cart->tran_date = sql2date($bank_trans['trans_date']);
176
177                 $cart->original_amount = $bank_trans['amount'];
178                 $result = get_gl_trans($type, $trans_no);
179                 if ($result) {
180                         while ($row = db_fetch($result)) {
181                                 if (is_bank_account($row['account'])) {
182                                         // date exchange rate is currenly not stored in bank transaction,
183                                         // so we have to restore it from original gl amounts
184                                         $ex_rate = $bank_trans['amount']/$row['amount'];
185                                 } else {
186                                         $cart->add_gl_item( $row['account'], $row['dimension_id'],
187                                                 $row['dimension2_id'], $row['amount'], $row['memo_']);
188                                 }
189                         }
190                 }
191
192                 // apply exchange rate
193                 foreach($cart->gl_items as $line_no => $line)
194                         $cart->gl_items[$line_no]->amount *= $ex_rate;
195
196         } else {
197                 $cart->reference = $Refs->get_next($cart->trans_type, null, $cart->tran_date);
198                 $cart->tran_date = new_doc_date();
199                 if (!is_date_in_fiscalyear($cart->tran_date))
200                         $cart->tran_date = end_fiscalyear();
201         }
202
203         $_POST['PayType'] = $cart->person_type_id;
204         $_POST['person_id'] = $cart->person_id;
205         $_POST['PersonDetailID'] = $cart->person_detail_id;
206
207         $_POST['bank_account'] = $cart->bank_act;
208         $_POST['memo_'] = $cart->memo_;
209         $_POST['ref'] = $cart->reference;
210         $_POST['date_'] = $cart->tran_date;
211
212         $_SESSION['pay_items'] = &$cart;
213 }
214 //-----------------------------------------------------------------------------------------------
215
216 function check_trans()
217 {
218         global $Refs, $systypes_array;
219
220         $input_error = 0;
221
222         if ($_SESSION['pay_items']->count_gl_items() < 1) {
223                 display_error(_("You must enter at least one payment line."));
224                 set_focus('code_id');
225                 $input_error = 1;
226         }
227
228         if ($_SESSION['pay_items']->gl_items_total() == 0.0) {
229                 display_error(_("The total bank amount cannot be 0."));
230                 set_focus('code_id');
231                 $input_error = 1;
232         }
233
234         $limit = get_bank_account_limit($_POST['bank_account'], $_POST['date_']);
235
236         $amnt_chg = -$_SESSION['pay_items']->gl_items_total()-$_SESSION['pay_items']->original_amount;
237
238         if ($limit !== null && floatcmp($limit, -$amnt_chg) < 0)
239         {
240                 display_error(sprintf(_("The total bank amount exceeds allowed limit (%s)."), price_format($limit-$_SESSION['pay_items']->original_amount)));
241                 set_focus('code_id');
242                 $input_error = 1;
243         }
244         if ($trans = check_bank_account_history($amnt_chg, $_POST['bank_account'], $_POST['date_'])) {
245
246                 if (isset($trans['trans_no'])) {
247                         display_error(sprintf(_("The bank transaction would result in exceed of authorized overdraft limit for transaction: %s #%s on %s."),
248                                 $systypes_array[$trans['type']], $trans['trans_no'], sql2date($trans['trans_date'])));
249                         set_focus('amount');
250                         $input_error = 1;
251                 }       
252         }
253         if (!check_reference($_POST['ref'], $_SESSION['pay_items']->trans_type, $_SESSION['pay_items']->order_id))
254         {
255                 set_focus('ref');
256                 $input_error = 1;
257         }
258         if (!is_date($_POST['date_']))
259         {
260                 display_error(_("The entered date for the payment is invalid."));
261                 set_focus('date_');
262                 $input_error = 1;
263         }
264         elseif (!is_date_in_fiscalyear($_POST['date_']))
265         {
266                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
267                 set_focus('date_');
268                 $input_error = 1;
269         } 
270
271         if (get_post('PayType')==PT_CUSTOMER && (!get_post('person_id') || !get_post('PersonDetailID'))) {
272                 display_error(_("You have to select customer and customer branch."));
273                 set_focus('person_id');
274                 $input_error = 1;
275         } elseif (get_post('PayType')==PT_SUPPLIER && (!get_post('person_id'))) {
276                 display_error(_("You have to select supplier."));
277                 set_focus('person_id');
278                 $input_error = 1;
279         }
280         if (!db_has_currency_rates(get_bank_account_currency($_POST['bank_account']), $_POST['date_'], true))
281                 $input_error = 1;
282
283         if (isset($_POST['settled_amount']) && in_array(get_post('PayType'), array(PT_SUPPLIER, PT_CUSTOMER)) && (input_num('settled_amount') <= 0)) {
284                 display_error(_("Settled amount have to be positive number."));
285                 set_focus('person_id');
286                 $input_error = 1;
287         }
288         return $input_error;
289 }
290
291 if (isset($_POST['Process']) && !check_trans())
292 {
293         $cart = &$_SESSION['pay_items'];
294
295         $cart->person_type_id = get_post('PayType');
296         $cart->person_id = get_post('person_id');
297         $cart->person_detail_id = get_post('PersonDetailID');
298         $cart->bank_act = get_post('bank_account');
299         $cart->ex_rate = input_num('_ex_rate');
300         $cart->tran_date = get_post('date_');
301         $cart->memo_ = get_post('memo_');
302         $cart->reference = get_post('ref');
303         $cart->settled_amount = input_num('settled_amount', null);
304
305         $new = $_SESSION['pay_items']->order_id == 0;
306
307         write_bank_transaction($cart);
308
309         $trans_type = $cart->trans_type;
310         $trans_no = $cart->order_id;
311
312         new_doc_date($_POST['date_']);
313         $_SESSION['pay_items']->clear_items();
314         unset($_SESSION['pay_items']);
315
316         if ($new)
317                 meta_forward($_SERVER['PHP_SELF'], $trans_type==ST_BANKPAYMENT ?
318                         "AddedID=$trans_no" : "AddedDep=$trans_no");
319         else
320                 meta_forward($_SERVER['PHP_SELF'], $trans_type==ST_BANKPAYMENT ?
321                         "UpdatedID=$trans_no" : "UpdatedDep=$trans_no");
322
323 }
324
325 //-----------------------------------------------------------------------------------------------
326
327 function check_item_data()
328 {
329         if (!check_num('amount', 0))
330         {
331                 display_error( _("The amount entered is not a valid number or is less than zero."));
332                 set_focus('amount');
333                 return false;
334         }
335         if (isset($_POST['_ex_rate']) && input_num('_ex_rate') <= 0)
336         {
337                 display_error( _("The exchange rate cannot be zero or a negative number."));
338                 set_focus('_ex_rate');
339                 return false;
340         }
341
342         return true;
343 }
344
345 //-----------------------------------------------------------------------------------------------
346
347 function handle_update_item()
348 {
349         $amount = ($_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ? 1:-1) * input_num('amount');
350     if($_POST['UpdateItem'] != "" && check_item_data())
351     {
352         $_SESSION['pay_items']->update_gl_item($_POST['Index'], $_POST['code_id'], 
353             $_POST['dimension_id'], $_POST['dimension2_id'], $amount , $_POST['LineMemo']);
354     }
355         line_start_focus();
356 }
357
358 //-----------------------------------------------------------------------------------------------
359
360 function handle_delete_item($id)
361 {
362         $_SESSION['pay_items']->remove_gl_item($id);
363         line_start_focus();
364 }
365
366 //-----------------------------------------------------------------------------------------------
367
368 function handle_new_item()
369 {
370         if (!check_item_data())
371                 return;
372         $amount = ($_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ? 1:-1) * input_num('amount');
373
374         $_SESSION['pay_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
375                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
376         line_start_focus();
377 }
378 //-----------------------------------------------------------------------------------------------
379 $id = find_submit('Delete');
380 if ($id != -1)
381         handle_delete_item($id);
382
383 if (isset($_POST['AddItem']))
384         handle_new_item();
385
386 if (isset($_POST['UpdateItem']))
387         handle_update_item();
388
389 if (isset($_POST['CancelItemChanges']) || isset($_POST['Index']))
390         line_start_focus();
391
392 if (isset($_POST['go']))
393 {
394         display_quick_entries($_SESSION['pay_items'], $_POST['person_id'], input_num('totamount'), 
395                 $_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ? QE_PAYMENT : QE_DEPOSIT);
396         $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
397         line_start_focus();
398 }
399 //-----------------------------------------------------------------------------------------------
400
401 start_form();
402
403 display_bank_header($_SESSION['pay_items']);
404
405 start_table(TABLESTYLE2, "width='90%'", 10);
406 start_row();
407 echo "<td>";
408 display_gl_items($_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ?
409         _("Payment Items"):_("Deposit Items"), $_SESSION['pay_items']);
410 gl_options_controls($_SESSION['pay_items']);
411 echo "</td>";
412 end_row();
413 end_table(1);
414
415 submit_center_first('Update', _("Update"), '', null);
416 submit_center_last('Process', $_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ?
417         _("Process Payment"):_("Process Deposit"), '', 'default');
418
419 end_form();
420
421 //------------------------------------------------------------------------------------------------
422
423 end_page();
424