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