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