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