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