Merging version 2.1 RC to main trunk.
[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_'] = Today();
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
184         $_SESSION['pay_items']->clear_items();
185         unset($_SESSION['pay_items']);
186
187         meta_forward($_SERVER['PHP_SELF'], $trans_type==systypes::bank_payment() ?
188                 "AddedID=$trans_no" : "AddedDep=$trans_no");
189
190 } /*end of process credit note */
191
192 //-----------------------------------------------------------------------------------------------
193
194 function check_item_data()
195 {
196         //if (!check_num('amount', 0))
197         //{
198         //      display_error( _("The amount entered is not a valid number or is less than zero."));
199         //      set_focus('amount');
200         //      return false;
201         //}
202
203         if ($_POST['code_id'] == $_POST['bank_account'])
204         {
205                 display_error( _("The source and destination accouts cannot be the same."));
206                 set_focus('code_id');
207                 return false;
208         }
209
210         //if (is_bank_account($_POST['code_id']))
211         //{
212         //      if ($_SESSION['pay_items']->trans_type == systypes::bank_payment())
213         //              display_error( _("You cannot make a payment to a bank account. Please use the transfer funds facility for this."));
214         //      else
215         //              display_error( _("You cannot make a deposit from a bank account. Please use the transfer funds facility for this."));
216         //      set_focus('code_id');
217         //      return false;
218         //}
219
220         return true;
221 }
222
223 //-----------------------------------------------------------------------------------------------
224
225 function handle_update_item()
226 {
227         $amount = ($_SESSION['pay_items']->trans_type==systypes::bank_payment() ? 1:-1) * input_num('amount');
228     if($_POST['UpdateItem'] != "" && check_item_data())
229     {
230         $_SESSION['pay_items']->update_gl_item($_POST['Index'], $_POST['dimension_id'],
231                 $_POST['dimension2_id'], $amount , $_POST['LineMemo']);
232     }
233         line_start_focus();
234 }
235
236 //-----------------------------------------------------------------------------------------------
237
238 function handle_delete_item($id)
239 {
240         $_SESSION['pay_items']->remove_gl_item($id);
241         line_start_focus();
242 }
243
244 //-----------------------------------------------------------------------------------------------
245
246 function handle_new_item()
247 {
248         if (!check_item_data())
249                 return;
250         $amount = ($_SESSION['pay_items']->trans_type==systypes::bank_payment() ? 1:-1) * input_num('amount');
251
252         $_SESSION['pay_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
253                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
254         line_start_focus();
255 }
256 //-----------------------------------------------------------------------------------------------
257 $id = find_submit('Delete');
258 if ($id != -1)
259         handle_delete_item($id);
260
261 if (isset($_POST['AddItem']))
262         handle_new_item();
263
264 if (isset($_POST['UpdateItem']))
265         handle_update_item();
266
267 if (isset($_POST['CancelItemChanges']))
268         line_start_focus();
269
270 if (isset($_POST['go']))
271 {
272         display_quick_entries($_SESSION['pay_items'], $_POST['person_id'], input_num('totamount'), 
273                 $_SESSION['pay_items']->trans_type==systypes::bank_payment() ? QE_PAYMENT : QE_DEPOSIT);
274         $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
275         line_start_focus();
276 }
277 //-----------------------------------------------------------------------------------------------
278
279 start_form(false, true);
280
281 display_bank_header($_SESSION['pay_items']);
282
283 start_table("$table_style2 width=90%", 10);
284 start_row();
285 echo "<td>";
286 display_gl_items($_SESSION['pay_items']->trans_type==systypes::bank_payment() ?
287         _("Payment Items"):_("Deposit Items"), $_SESSION['pay_items']);
288 gl_options_controls();
289 echo "</td>";
290 end_row();
291 end_table(1);
292
293 submit_center_first('Update', _("Update"), '', null);
294 submit_center_last('Process', $_SESSION['pay_items']->trans_type==systypes::bank_payment() ?
295         _("Process Payment"):_("Process Deposit"), '', true);
296
297 end_form();
298
299 //------------------------------------------------------------------------------------------------
300
301 end_page();
302
303 ?>