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