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