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