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