0df087b2a2baf5e102fe1983d1cfcab1be55aac5
[fa-stable.git] / gl / gl_bank.php
1 <?php
2
3 $page_security = 3;
4 $path_to_root="..";
5 include_once($path_to_root . "/includes/ui/items_cart.inc");
6 include_once($path_to_root . "/includes/session.inc");
7
8 include_once($path_to_root . "/includes/date_functions.inc");
9 include_once($path_to_root . "/includes/data_checks.inc");
10
11 include_once($path_to_root . "/gl/includes/ui/gl_bank_ui.inc");
12 include_once($path_to_root . "/gl/includes/gl_db.inc");
13 include_once($path_to_root . "/gl/includes/gl_ui.inc");
14
15 $js = '';
16 if ($use_popup_windows)
17         $js .= get_js_open_window(800, 500);
18 if ($use_date_picker)
19         $js .= get_js_date_picker();
20
21 if (isset($_GET['NewPayment'])) {
22         $_SESSION['page_title'] = _("Bank Account Payment Entry");
23         handle_new_order(systypes::bank_payment());
24
25 } else if(isset($_GET['NewDeposit'])) {
26         $_SESSION['page_title'] = _("Bank Account Deposit Entry");
27         handle_new_order(systypes::bank_deposit());
28 }
29 page($_SESSION['page_title'], false, false, '', $js);
30
31 //-----------------------------------------------------------------------------------------------
32 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
33
34 //----------------------------------------------------------------------------------------
35 if ($ret = context_restore()) {
36         if(isset($ret['supplier_id']))
37                 $_POST['person_id'] = $ret['supplier_id'];
38         if(isset($ret['customer_id']))
39                 $_POST['person_id'] = $ret['customer_id'];
40         set_focus('person_id');
41         if(isset($ret['branch_id'])) {
42                 $_POST['PersonDetailID'] = $ret['branch_id'];
43                 set_focus('PersonDetailID');
44         }
45 }
46 if (isset($_POST['_person_id_editor'])) {
47         if ($_POST['PayType']==payment_person_types::supplier())
48                 $editor = '/purchasing/manage/suppliers.php?supplier_id=';
49         else
50                 $editor = '/sales/manage/customers.php?debtor_no=';
51                 
52 //      $_SESSION['pay_items'] should stay unchanged during call
53 //
54 context_call($path_to_root.$editor.$_POST['person_id'], 
55         array('bank_account', 'date_', 'PayType', 'person_id',
56                 'PersonDetailID', 'ref', 'memo_') );
57 }
58 //--------------------------------------------------------------------------------------------------
59 function line_start_focus() {
60   global        $Ajax;
61
62   $Ajax->activate('items_table');
63   set_focus('_code_id_edit');
64 }
65
66 //-----------------------------------------------------------------------------------------------
67
68 if (isset($_GET['AddedID']))
69 {
70         $trans_no = $_GET['AddedID'];
71         $trans_type = systypes::bank_payment();
72
73         display_notification_centered(_("Payment has been entered"));
74
75         display_note(get_gl_view_str($trans_type, $trans_no, _("&View the GL Postings for this Payment")));
76
77         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another &Payment"), "NewPayment=yes");
78
79         hyperlink_params($_SERVER['PHP_SELF'], _("Enter A &Deposit"), "NewDeposit=yes");
80
81         display_footer_exit();
82 }
83
84 if (isset($_GET['AddedDep']))
85 {
86         $trans_no = $_GET['AddedDep'];
87         $trans_type = systypes::bank_deposit();
88
89         display_notification_centered(_("Deposit has been entered"));
90
91         display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL Postings for this Deposit")));
92
93         hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another Deposit"), "NewDeposit=yes");
94
95         hyperlink_params($_SERVER['PHP_SELF'], _("Enter A Payment"), "NewPayment=yes");
96
97         display_footer_exit();
98 }
99 if (isset($_POST['_date__changed'])) {
100         $Ajax->activate('_ex_rate');
101 }
102 //--------------------------------------------------------------------------------------------------
103
104 function handle_new_order($type)
105 {
106         if (isset($_SESSION['pay_items']))
107         {
108                 $_SESSION['pay_items']->clear_items();
109                 unset ($_SESSION['pay_items']);
110         }
111
112         session_register("pay_items");
113
114         $_SESSION['pay_items'] = new items_cart($type);
115
116         $_POST['date_'] = Today();
117         if (!is_date_in_fiscalyear($_POST['date_']))
118                 $_POST['date_'] = end_fiscalyear();
119         $_SESSION['pay_items']->tran_date = $_POST['date_'];
120 }
121
122 //-----------------------------------------------------------------------------------------------
123
124 if (isset($_POST['Process']))
125 {
126
127         $input_error = 0;
128
129         if ($_SESSION['pay_items']->count_gl_items() < 1) {
130                 display_error(_("You must enter at least one payment line."));
131                 set_focus('code_id');
132                 $input_error = 1;
133         }
134
135         if (!references::is_valid($_POST['ref']))
136         {
137                 display_error( _("You must enter a reference."));
138                 set_focus('ref');
139                 $input_error = 1;
140         }
141         elseif (!is_new_reference($_POST['ref'], $_SESSION['pay_items']->trans_type))
142         {
143                 display_error( _("The entered reference is already in use."));
144                 set_focus('ref');
145                 $input_error = 1;
146         }
147         if (!is_date($_POST['date_']))
148         {
149                 display_error(_("The entered date for the payment is invalid."));
150                 set_focus('date_');
151                 $input_error = 1;
152         }
153         elseif (!is_date_in_fiscalyear($_POST['date_']))
154         {
155                 display_error(_("The entered date is not in fiscal year."));
156                 set_focus('date_');
157                 $input_error = 1;
158         }
159
160         if ($input_error == 1)
161                 unset($_POST['Process']);
162 }
163
164 if (isset($_POST['Process']))
165 {
166
167         $trans = add_bank_transaction(
168                 $_SESSION['pay_items']->trans_type, $_POST['bank_account'],
169                 $_SESSION['pay_items'], $_POST['date_'],
170                 $_POST['PayType'], $_POST['person_id'], get_post('PersonDetailID'),
171                 $_POST['ref'], $_POST['memo_']);
172
173         $trans_type = $trans[0];
174         $trans_no = $trans[1];
175
176         $_SESSION['pay_items']->clear_items();
177         unset($_SESSION['pay_items']);
178
179         meta_forward($_SERVER['PHP_SELF'], $trans_type==systypes::bank_payment() ?
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 == systypes::bank_payment())
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==systypes::bank_payment() ? 1:-1) * input_num('amount');
220     if($_POST['UpdateItem'] != "" && check_item_data())
221     {
222         $_SESSION['pay_items']->update_gl_item($_POST['Index'], $_POST['dimension_id'],
223                 $_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==systypes::bank_payment() ? 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 function display_quick_entries(&$cart)
250 {
251         if (!check_num('totamount', 0))
252         {
253                 display_error( _("The amount entered is not a valid number or is less than zero."));
254                 set_focus('totamount');
255         }
256         elseif (!get_post('person_id'))
257         {
258                 if ($cart->trans_type==systypes::bank_payment())        
259                         display_error( _("No Quick Entries are defined for Payment."));
260                 else    
261                         display_error( _("No Quick Entries are defined for Deposit."));
262                 set_focus('totamount');
263         }
264         else
265         {
266                 $rate = 0;
267                 $totamount = input_num('totamount');
268                 $totamount = ($cart->trans_type==systypes::bank_payment() ? 1:-1) * $totamount;
269                 $qe = get_quick_entry($_POST['person_id']);
270                 $qe_lines = get_quick_entry_lines($_POST['person_id']);
271                 while ($qe_line = db_fetch($qe_lines))
272                 {
273                         if ($qe_line['tax_acc'])
274                         {
275                                 $account = get_gl_account($qe_line['account']);
276                                 $tax_group = $account['tax_code'];
277                                 $items = get_tax_group_items($tax_group);
278                                 while ($item = db_fetch($items))
279                                         $rate += $item['rate'];
280                                 if ($rate != 0)
281                                         $totamount = $totamount * 100 / ($rate + 100);
282                                 $cart->clear_items();
283
284                                 $cart->add_gl_item($qe_line['account'], $qe_line['dimension_id'], $qe_line['dimension2_id'], 
285                                         $totamount, $qe['description']);
286                                 $items = get_tax_group_items($tax_group);
287                                 while ($item = db_fetch($items))
288                                 {
289                                         if ($item['rate'] != 0)
290                                         {
291                                                 $amount = $totamount * $item['rate'] / 100;
292                                                 $code = ($cart->trans_type==systypes::bank_payment() ? $item['purchasing_gl_code'] : 
293                                                         $item['sales_gl_code']);
294                                                 $cart->add_gl_item($code, 0, 0, $amount, $qe['description']);
295                                         }
296                                 }
297                         }
298                         else
299                         {
300                                 if ($qe_line['pct'])
301                                         $amount = $totamount * $qe_line['amount'] / 100;
302                                 else
303                                         $amount = $qe_line['amount'];
304                                 $cart->add_gl_item($qe_line['account'], $qe_line['dimension_id'], $qe_line['dimension2_id'], 
305                                         $amount, $qe['description']);
306                         }               
307                 }
308                 line_start_focus();
309         }       
310 }
311 //-----------------------------------------------------------------------------------------------
312 $id = find_submit('Delete');
313 if ($id != -1)
314         handle_delete_item($id);
315
316 if (isset($_POST['AddItem']))
317         handle_new_item();
318
319 if (isset($_POST['UpdateItem']))
320         handle_update_item();
321
322 if (isset($_POST['CancelItemChanges']))
323         line_start_focus();
324
325 if (isset($_POST['go']))
326         display_quick_entries($_SESSION['pay_items']);
327
328 //-----------------------------------------------------------------------------------------------
329
330 start_form(false, true);
331
332 display_bank_header($_SESSION['pay_items']);
333
334 start_table("$table_style2 width=90%", 10);
335 start_row();
336 echo "<td>";
337 display_gl_items($_SESSION['pay_items']->trans_type==systypes::bank_payment() ?
338         _("Payment Items"):_("Deposit Items"), $_SESSION['pay_items']);
339 gl_options_controls();
340 echo "</td>";
341 end_row();
342 end_table(1);
343
344 submit_center_first('Update', _("Update"), '', null);
345 submit_center_last('Process', $_SESSION['pay_items']->trans_type==systypes::bank_payment() ?
346         _("Process Payment"):_("Process Deposit"), '', true);
347
348 end_form();
349
350 //------------------------------------------------------------------------------------------------
351
352 end_page();
353
354 ?>