Opitimized bank transaction edition code, [0000270] invalid next reference update.
[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 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(_("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         $trans = write_bank_transaction(
276                 $_SESSION['pay_items']->trans_type, $_SESSION['pay_items']->order_id, $_POST['bank_account'],
277                 $_SESSION['pay_items'], $_POST['date_'],
278                 $_POST['PayType'], $_POST['person_id'], get_post('PersonDetailID'),
279                 $_POST['ref'], $_POST['memo_'], false);
280
281         $trans_type = $trans[0];
282         $trans_no = $trans[1];
283         new_doc_date($_POST['date_']);
284
285         $_SESSION['pay_items']->clear_items();
286         unset($_SESSION['pay_items']);
287         
288         commit_transaction();
289         
290         if ($new)
291                 meta_forward($_SERVER['PHP_SELF'], $trans_type==ST_BANKPAYMENT ?
292                         "AddedID=$trans_no" : "AddedDep=$trans_no");
293         else
294                 meta_forward($_SERVER['PHP_SELF'], $trans_type==ST_BANKPAYMENT ?
295                         "UpdatedID=$trans_no" : "UpdatedDep=$trans_no");
296
297 } /*end of process credit note */
298
299 //-----------------------------------------------------------------------------------------------
300
301 function check_item_data()
302 {
303         //if (!check_num('amount', 0))
304         //{
305         //      display_error( _("The amount entered is not a valid number or is less than zero."));
306         //      set_focus('amount');
307         //      return false;
308         //}
309
310         if ($_POST['code_id'] == $_POST['bank_account'])
311         {
312                 display_error( _("The source and destination accouts cannot be the same."));
313                 set_focus('code_id');
314                 return false;
315         }
316
317         //if (is_bank_account($_POST['code_id']))
318         //{
319         //      if ($_SESSION['pay_items']->trans_type == ST_BANKPAYMENT)
320         //              display_error( _("You cannot make a payment to a bank account. Please use the transfer funds facility for this."));
321         //      else
322         //              display_error( _("You cannot make a deposit from a bank account. Please use the transfer funds facility for this."));
323         //      set_focus('code_id');
324         //      return false;
325         //}
326
327         return true;
328 }
329
330 //-----------------------------------------------------------------------------------------------
331
332 function handle_update_item()
333 {
334         $amount = ($_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ? 1:-1) * input_num('amount');
335     if($_POST['UpdateItem'] != "" && check_item_data())
336     {
337         $_SESSION['pay_items']->update_gl_item($_POST['Index'], $_POST['code_id'], 
338             $_POST['dimension_id'], $_POST['dimension2_id'], $amount , $_POST['LineMemo']);
339     }
340         line_start_focus();
341 }
342
343 //-----------------------------------------------------------------------------------------------
344
345 function handle_delete_item($id)
346 {
347         $_SESSION['pay_items']->remove_gl_item($id);
348         line_start_focus();
349 }
350
351 //-----------------------------------------------------------------------------------------------
352
353 function handle_new_item()
354 {
355         if (!check_item_data())
356                 return;
357         $amount = ($_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ? 1:-1) * input_num('amount');
358
359         $_SESSION['pay_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
360                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
361         line_start_focus();
362 }
363 //-----------------------------------------------------------------------------------------------
364 $id = find_submit('Delete');
365 if ($id != -1)
366         handle_delete_item($id);
367
368 if (isset($_POST['AddItem']))
369         handle_new_item();
370
371 if (isset($_POST['UpdateItem']))
372         handle_update_item();
373
374 if (isset($_POST['CancelItemChanges']))
375         line_start_focus();
376
377 if (isset($_POST['go']))
378 {
379         display_quick_entries($_SESSION['pay_items'], $_POST['person_id'], input_num('totamount'), 
380                 $_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ? QE_PAYMENT : QE_DEPOSIT);
381         $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
382         line_start_focus();
383 }
384 //-----------------------------------------------------------------------------------------------
385
386 start_form();
387
388 display_bank_header($_SESSION['pay_items']);
389
390 start_table(TABLESTYLE2, "width=90%", 10);
391 start_row();
392 echo "<td>";
393 display_gl_items($_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ?
394         _("Payment Items"):_("Deposit Items"), $_SESSION['pay_items']);
395 gl_options_controls();
396 echo "</td>";
397 end_row();
398 end_table(1);
399
400 submit_center_first('Update', _("Update"), '', null);
401 submit_center_last('Process', $_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ?
402         _("Process Payment"):_("Process Deposit"), '', 'default');
403
404 end_form();
405
406 //------------------------------------------------------------------------------------------------
407
408 end_page();
409
410 ?>