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