Merging changes form main trunk 2.2.5-2.2.6
[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 }
40 page($_SESSION['page_title'], false, false, '', $js);
41
42 //-----------------------------------------------------------------------------------------------
43 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
44
45 //----------------------------------------------------------------------------------------
46 if (list_updated('PersonDetailID')) {
47         $br = get_branch(get_post('PersonDetailID'));
48         $_POST['person_id'] = $br['debtor_no'];
49         $Ajax->activate('person_id');
50 }
51
52 //--------------------------------------------------------------------------------------------------
53 function line_start_focus() {
54   global        $Ajax;
55
56   $Ajax->activate('items_table');
57   set_focus('_code_id_edit');
58 }
59
60 //-----------------------------------------------------------------------------------------------
61
62 if (isset($_GET['AddedID']))
63 {
64         $trans_no = $_GET['AddedID'];
65         $trans_type = ST_BANKPAYMENT;
66
67         display_notification_centered(_("Payment $trans_no has been entered"));
68
69         display_note(get_gl_view_str($trans_type, $trans_no, _("&View the GL Postings for this Payment")));
70
71         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another &Payment"), "NewPayment=yes");
72
73         hyperlink_params($_SERVER['PHP_SELF'], _("Enter A &Deposit"), "NewDeposit=yes");
74
75         display_footer_exit();
76 }
77
78 if (isset($_GET['AddedDep']))
79 {
80         $trans_no = $_GET['AddedDep'];
81         $trans_type = ST_BANKDEPOSIT;
82
83         display_notification_centered(_("Deposit $trans_no has been entered"));
84
85         display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL Postings for this Deposit")));
86
87         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Deposit"), "NewDeposit=yes");
88
89         hyperlink_params($_SERVER['PHP_SELF'], _("Enter A Payment"), "NewPayment=yes");
90
91         display_footer_exit();
92 }
93 if (isset($_POST['_date__changed'])) {
94         $Ajax->activate('_ex_rate');
95 }
96 //--------------------------------------------------------------------------------------------------
97
98 function handle_new_order($type)
99 {
100         if (isset($_SESSION['pay_items']))
101         {
102                 unset ($_SESSION['pay_items']);
103         }
104
105         //session_register("pay_items");
106
107         $_SESSION['pay_items'] = new items_cart($type);
108
109         $_POST['date_'] = new_doc_date();
110         if (!is_date_in_fiscalyear($_POST['date_']))
111                 $_POST['date_'] = end_fiscalyear();
112         $_SESSION['pay_items']->tran_date = $_POST['date_'];
113 }
114
115 //-----------------------------------------------------------------------------------------------
116
117 if (isset($_POST['Process']))
118 {
119
120         $input_error = 0;
121
122         if ($_SESSION['pay_items']->count_gl_items() < 1) {
123                 display_error(_("You must enter at least one payment line."));
124                 set_focus('code_id');
125                 $input_error = 1;
126         }
127
128         if ($_SESSION['pay_items']->gl_items_total() == 0.0) {
129                 display_error(_("The total bank amount cannot be 0."));
130                 set_focus('code_id');
131                 $input_error = 1;
132         }
133
134         if (!$Refs->is_valid($_POST['ref']))
135         {
136                 display_error( _("You must enter a reference."));
137                 set_focus('ref');
138                 $input_error = 1;
139         }
140         elseif (!is_new_reference($_POST['ref'], $_SESSION['pay_items']->trans_type))
141         {
142                 display_error( _("The entered reference is already in use."));
143                 set_focus('ref');
144                 $input_error = 1;
145         }
146         if (!is_date($_POST['date_']))
147         {
148                 display_error(_("The entered date for the payment is invalid."));
149                 set_focus('date_');
150                 $input_error = 1;
151         }
152         elseif (!is_date_in_fiscalyear($_POST['date_']))
153         {
154                 display_error(_("The entered date is not in fiscal year."));
155                 set_focus('date_');
156                 $input_error = 1;
157         }
158
159         if ($input_error == 1)
160                 unset($_POST['Process']);
161 }
162
163 if (isset($_POST['Process']))
164 {
165
166         $trans = add_bank_transaction(
167                 $_SESSION['pay_items']->trans_type, $_POST['bank_account'],
168                 $_SESSION['pay_items'], $_POST['date_'],
169                 $_POST['PayType'], $_POST['person_id'], get_post('PersonDetailID'),
170                 $_POST['ref'], $_POST['memo_']);
171
172         $trans_type = $trans[0];
173         $trans_no = $trans[1];
174         new_doc_date($_POST['date_']);
175
176         $_SESSION['pay_items']->clear_items();
177         unset($_SESSION['pay_items']);
178
179         meta_forward($_SERVER['PHP_SELF'], $trans_type==ST_BANKPAYMENT ?
180                 "AddedID=$trans_no" : "AddedDep=$trans_no");
181
182 } /*end of process credit note */
183
184 //-----------------------------------------------------------------------------------------------
185
186 function check_item_data()
187 {
188         //if (!check_num('amount', 0))
189         //{
190         //      display_error( _("The amount entered is not a valid number or is less than zero."));
191         //      set_focus('amount');
192         //      return false;
193         //}
194
195         if ($_POST['code_id'] == $_POST['bank_account'])
196         {
197                 display_error( _("The source and destination accouts cannot be the same."));
198                 set_focus('code_id');
199                 return false;
200         }
201
202         //if (is_bank_account($_POST['code_id']))
203         //{
204         //      if ($_SESSION['pay_items']->trans_type == ST_BANKPAYMENT)
205         //              display_error( _("You cannot make a payment to a bank account. Please use the transfer funds facility for this."));
206         //      else
207         //              display_error( _("You cannot make a deposit from a bank account. Please use the transfer funds facility for this."));
208         //      set_focus('code_id');
209         //      return false;
210         //}
211
212         return true;
213 }
214
215 //-----------------------------------------------------------------------------------------------
216
217 function handle_update_item()
218 {
219         $amount = ($_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ? 1:-1) * input_num('amount');
220     if($_POST['UpdateItem'] != "" && check_item_data())
221     {
222         $_SESSION['pay_items']->update_gl_item($_POST['Index'], $_POST['code_id'], 
223             $_POST['dimension_id'], $_POST['dimension2_id'], $amount , $_POST['LineMemo']);
224     }
225         line_start_focus();
226 }
227
228 //-----------------------------------------------------------------------------------------------
229
230 function handle_delete_item($id)
231 {
232         $_SESSION['pay_items']->remove_gl_item($id);
233         line_start_focus();
234 }
235
236 //-----------------------------------------------------------------------------------------------
237
238 function handle_new_item()
239 {
240         if (!check_item_data())
241                 return;
242         $amount = ($_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ? 1:-1) * input_num('amount');
243
244         $_SESSION['pay_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
245                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
246         line_start_focus();
247 }
248 //-----------------------------------------------------------------------------------------------
249 $id = find_submit('Delete');
250 if ($id != -1)
251         handle_delete_item($id);
252
253 if (isset($_POST['AddItem']))
254         handle_new_item();
255
256 if (isset($_POST['UpdateItem']))
257         handle_update_item();
258
259 if (isset($_POST['CancelItemChanges']))
260         line_start_focus();
261
262 if (isset($_POST['go']))
263 {
264         display_quick_entries($_SESSION['pay_items'], $_POST['person_id'], input_num('totamount'), 
265                 $_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ? QE_PAYMENT : QE_DEPOSIT);
266         $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
267         line_start_focus();
268 }
269 //-----------------------------------------------------------------------------------------------
270
271 start_form();
272
273 display_bank_header($_SESSION['pay_items']);
274
275 start_table("$table_style2 width=90%", 10);
276 start_row();
277 echo "<td>";
278 display_gl_items($_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ?
279         _("Payment Items"):_("Deposit Items"), $_SESSION['pay_items']);
280 gl_options_controls();
281 echo "</td>";
282 end_row();
283 end_table(1);
284
285 submit_center_first('Update', _("Update"), '', null);
286 submit_center_last('Process', $_SESSION['pay_items']->trans_type==ST_BANKPAYMENT ?
287         _("Process Payment"):_("Process Deposit"), '', 'default');
288
289 end_form();
290
291 //------------------------------------------------------------------------------------------------
292
293 end_page();
294
295 ?>