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