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