Allowing modifying of Bank Payments/Deposits
[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         handle_new_order(ST_BANKPAYMENT);
35
36 } else if(isset($_GET['NewDeposit'])) {
37         $_SESSION['page_title'] = _($help_context = "Bank Account Deposit Entry");
38         handle_new_order(ST_BANKDEPOSIT);
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 Account 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(_("Payment $trans_no has been entered"));
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         display_footer_exit();
82 }
83
84 if (isset($_GET['UpdatedID']))
85 {
86         $trans_no = $_GET['UpdatedID'];
87         $trans_type = ST_BANKPAYMENT;
88
89         display_notification_centered(_("Payment $trans_no has been modified"));
90
91         display_note(get_gl_view_str($trans_type, $trans_no, _("&View the GL Postings for this Payment")));
92
93         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another &Payment"), "NewPayment=yes");
94
95         hyperlink_params($_SERVER['PHP_SELF'], _("Enter A &Deposit"), "NewDeposit=yes");
96
97         display_footer_exit();
98 }
99
100 if (isset($_GET['AddedDep']))
101 {
102         $trans_no = $_GET['AddedDep'];
103         $trans_type = ST_BANKDEPOSIT;
104
105         display_notification_centered(_("Deposit $trans_no has been entered"));
106
107         display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL Postings for this Deposit")));
108
109         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Deposit"), "NewDeposit=yes");
110
111         hyperlink_params($_SERVER['PHP_SELF'], _("Enter A Payment"), "NewPayment=yes");
112
113         display_footer_exit();
114 }
115 if (isset($_GET['UpdatedDep']))
116 {
117         $trans_no = $_GET['UpdatedDep'];
118         $trans_type = ST_BANKDEPOSIT;
119
120         display_notification_centered(_("Deposit $trans_no has been modified"));
121
122         display_note(get_gl_view_str($trans_type, $trans_no, _("&View the GL Postings for this Deposit")));
123
124         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another &Deposit"), "NewDeposit=yes");
125
126         hyperlink_params($_SERVER['PHP_SELF'], _("Enter A &Payment"), "NewPayment=yes");
127
128         display_footer_exit();
129 }
130
131 if (isset($_POST['_date__changed'])) {
132         $Ajax->activate('_ex_rate');
133 }
134 //--------------------------------------------------------------------------------------------------
135
136 function handle_new_order($type)
137 {
138         if (isset($_SESSION['pay_items']))
139         {
140                 unset ($_SESSION['pay_items']);
141         }
142
143         //session_register("pay_items");
144
145         $_SESSION['pay_items'] = new items_cart($type);
146
147         $_POST['date_'] = new_doc_date();
148         if (!is_date_in_fiscalyear($_POST['date_']))
149                 $_POST['date_'] = end_fiscalyear();
150         $_SESSION['pay_items']->tran_date = $_POST['date_'];
151 }
152
153 function create_cart($type, $trans_no)
154 {
155         global $Refs;
156
157         if (isset($_SESSION['pay_items']))
158         {
159                 unset ($_SESSION['pay_items']);
160         }
161
162         $_SESSION['pay_items'] = new items_cart($type);
163     $_SESSION['pay_items']->order_id = $trans_no;
164
165         if ($trans_no) {
166                 $result = get_gl_trans($type, $trans_no);
167
168                 if ($result) {
169                         while ($row = db_fetch($result)) {
170                                 if ($row['amount'] == 0) continue;
171                                 if (is_bank_account($row['account'])) continue;
172                                 $date = $row['tran_date'];
173                                 $_SESSION['pay_items']->add_gl_item($row['account'], $row['dimension_id'], 
174                                         $row['dimension2_id'], $row['amount'], $row['memo_']);
175                         }
176                 }
177                 $_SESSION['pay_items']->memo_ = get_comments_string($type, $trans_no);
178                 $_SESSION['pay_items']->tran_date = sql2date($date);
179                 $_SESSION['pay_items']->reference = $Refs->get($type, $trans_no);
180                 ////////////////////////////////////////////
181                 // Check Ref Original ?????
182                 $_POST['ref_original'] = $_SESSION['pay_items']->reference; // Store for comparison when updating       
183
184                 $bank_trans = db_fetch(get_bank_trans($type, $trans_no));
185                 $_POST['bank_account'] = $bank_trans["bank_act"];
186                 $_POST['PayType'] = $bank_trans["person_type_id"];
187                 
188                 if ($bank_trans["person_type_id"] == PT_CUSTOMER) //2
189                 {
190                         $trans = get_customer_trans($trans_no, $type);  
191                         $_POST['person_id'] = $trans["debtor_no"];
192                         $_POST['PersonDetailID'] = $trans["branch_code"];
193                 }
194                 elseif ($bank_trans["person_type_id"] == PT_SUPPLIER) //3
195                 {
196                         $trans = get_supp_trans($trans_no, $type);
197                         $_POST['person_id'] = $trans["supplier_id"];
198                 }
199                 elseif ($bank_trans["person_type_id"] == PT_MISC) //0
200                         $_POST['person_id'] = $bank_trans["person_id"];
201                 elseif ($bank_trans["person_type_id"] == PT_QUICKENTRY) //4
202                         $_POST['person_id'] = $bank_trans["person_id"];
203                 else 
204                         $_POST['person_id'] = $bank_trans["person_id"];
205
206         } else {
207                 $_SESSION['pay_items']->reference = $Refs->get_next(0);
208                 $_SESSION['pay_items']->tran_date = new_doc_date();
209                 if (!is_date_in_fiscalyear($_SESSION['pay_items']->tran_date))
210                         $_SESSION['pay_items']->tran_date = end_fiscalyear();
211                 $_POST['ref_original'] = -1;
212         }
213
214         $_POST['memo_'] = $_SESSION['pay_items']->memo_;
215         $_POST['ref'] = $_SESSION['pay_items']->reference;
216         $_POST['date_'] = $_SESSION['pay_items']->tran_date;
217
218         //$_SESSION['pay_items'] = &$_SESSION['pay_items'];
219 }
220 //-----------------------------------------------------------------------------------------------
221
222 if (isset($_POST['Process']))
223 {
224
225         $input_error = 0;
226
227         if ($_SESSION['pay_items']->count_gl_items() < 1) {
228                 display_error(_("You must enter at least one payment line."));
229                 set_focus('code_id');
230                 $input_error = 1;
231         }
232
233         if ($_SESSION['pay_items']->gl_items_total() == 0.0) {
234                 display_error(_("The total bank amount cannot be 0."));
235                 set_focus('code_id');
236                 $input_error = 1;
237         }
238
239         if (!$Refs->is_valid($_POST['ref']))
240         {
241                 display_error( _("You must enter a reference."));
242                 set_focus('ref');
243                 $input_error = 1;
244         }
245         elseif ($_POST['ref'] != $_SESSION['pay_items']->reference && !is_new_reference($_POST['ref'], $_SESSION['pay_items']->trans_type))
246         {
247                 display_error( _("The entered reference is already in use."));
248                 set_focus('ref');
249                 $input_error = 1;
250         }
251         if (!is_date($_POST['date_']))
252         {
253                 display_error(_("The entered date for the payment is invalid."));
254                 set_focus('date_');
255                 $input_error = 1;
256         }
257         elseif (!is_date_in_fiscalyear($_POST['date_']))
258         {
259                 display_error(_("The entered date is not in fiscal year."));
260                 set_focus('date_');
261                 $input_error = 1;
262         }
263
264         if ($input_error == 1)
265                 unset($_POST['Process']);
266 }
267
268 if (isset($_POST['Process']))
269 {
270         begin_transaction();
271         
272         $_SESSION['pay_items'] = &$_SESSION['pay_items'];
273         $new = $_SESSION['pay_items']->order_id == 0;
274         
275         if (!$new)
276         {
277                 clear_bank_trans($_SESSION['pay_items']->trans_type, $_SESSION['pay_items']->order_id, true);
278                 $trans = reinsert_bank_transaction(
279                                 $_SESSION['pay_items']->trans_type, $_SESSION['pay_items']->order_id, $_POST['bank_account'],
280                                 $_SESSION['pay_items'], $_POST['date_'],
281                                 $_POST['PayType'], $_POST['person_id'], get_post('PersonDetailID'),
282                                 $_POST['ref'], $_POST['memo_'], false);         
283         }
284         else 
285         $trans = add_bank_transaction(
286                 $_SESSION['pay_items']->trans_type, $_POST['bank_account'],
287                 $_SESSION['pay_items'], $_POST['date_'],
288                 $_POST['PayType'], $_POST['person_id'], get_post('PersonDetailID'),
289                 $_POST['ref'], $_POST['memo_'], false);
290
291         $trans_type = $trans[0];
292         $trans_no = $trans[1];
293         new_doc_date($_POST['date_']);
294
295         $_SESSION['pay_items']->clear_items();
296         unset($_SESSION['pay_items']);
297         
298         commit_transaction();
299         
300         if ($new)
301         meta_forward($_SERVER['PHP_SELF'], $trans_type==ST_BANKPAYMENT ?
302                 "AddedID=$trans_no" : "AddedDep=$trans_no");
303         else
304         meta_forward($_SERVER['PHP_SELF'], $trans_type==ST_BANKPAYMENT ?
305                 "UpdatedID=$trans_no" : "UpdatedDep=$trans_no");
306
307 } /*end of process credit note */
308
309 //-----------------------------------------------------------------------------------------------
310
311 function check_item_data()
312 {
313         //if (!check_num('amount', 0))
314         //{
315         //      display_error( _("The amount entered is not a valid number or is less than zero."));
316         //      set_focus('amount');
317         //      return false;
318         //}
319
320         if ($_POST['code_id'] == $_POST['bank_account'])
321         {
322                 display_error( _("The source and destination accouts cannot be the same."));
323                 set_focus('code_id');
324                 return false;
325         }
326
327         //if (is_bank_account($_POST['code_id']))
328         //{
329         //      if ($_SESSION['pay_items']->trans_type == ST_BANKPAYMENT)
330         //              display_error( _("You cannot make a payment to a bank account. Please use the transfer funds facility for this."));
331         //      else
332         //              display_error( _("You cannot make a deposit from a bank account. Please use the transfer funds facility for this."));
333         //      set_focus('code_id');
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();
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
420 ?>