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