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