Release 1.0.1 established on SourceForge, fixing the bugs and including a Date Picker...
[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
7 include_once($path_to_root . "/includes/session.inc");
8
9 include_once($path_to_root . "/includes/date_functions.inc");
10 include_once($path_to_root . "/includes/data_checks.inc");
11
12 include_once($path_to_root . "/gl/includes/ui/gl_payment_ui.inc");
13 include_once($path_to_root . "/gl/includes/gl_db.inc");
14 include_once($path_to_root . "/gl/includes/gl_ui.inc");
15
16 $js = get_js_form_entry("CodeID2", "code_id", "amount");
17 if ($use_popup_windows)
18         $js .= get_js_open_window(800, 500);
19 if ($use_date_picker)
20         $js .= get_js_date_picker();
21 $js .= get_js_set_focus('CodeID2');
22
23 page(_("Bank Account Payment Entry"), false, false, "setFocus()", $js);
24
25 //-----------------------------------------------------------------------------------------------
26
27 check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));
28
29 check_db_has_bank_trans_types(_("There are no bank payment types defined in the system."));
30
31 //-----------------------------------------------------------------------------------------------
32
33 if (isset($_GET['AddedID'])) 
34 {
35         $trans_no = $_GET['AddedID'];
36         $trans_type = systypes::bank_payment();
37
38         display_notification_centered(_("Payment has been entered"));
39
40         display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL Postings for this Payment")));
41
42         hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter Another Payment"));
43
44         display_footer_exit();
45 }
46
47 //--------------------------------------------------------------------------------------------------
48
49 function copy_to_py()
50 {
51         $_SESSION['pay_items']->from_loc = $_POST['bank_account'];
52         $_SESSION['pay_items']->tran_date = $_POST['date_'];
53         $_SESSION['pay_items']->transfer_type = $_POST['type'];
54         $_SESSION['pay_items']->increase = $_POST['PayType'];
55         if (isset($_POST['person_id']))
56                 $_POST['person_id'] = 0;        
57         $_SESSION['pay_items']->person_id = $_POST['person_id'];
58         if (!isset($_POST['PersonDetailID']))
59                 $_POST['PersonDetailID'] = 0;
60         $_SESSION['pay_items']->branch_id = $_POST['PersonDetailID'];
61         $_SESSION['pay_items']->memo_ = $_POST['memo_'];
62 }
63
64 //--------------------------------------------------------------------------------------------------
65
66 function copy_from_py()
67 {
68         $_POST['bank_account'] = $_SESSION['pay_items']->from_loc;
69         $_POST['date_'] = $_SESSION['pay_items']->tran_date;
70         $_POST['type'] = $_SESSION['pay_items']->transfer_type;
71         $_POST['PayType'] = $_SESSION['pay_items']->increase;
72         $_POST['person_id'] = $_SESSION['pay_items']->person_id;
73         $_POST['PersonDetailID'] = $_SESSION['pay_items']->branch_id;
74         $_POST['memo_'] = $_SESSION['pay_items']->memo_;
75 }
76
77 //-----------------------------------------------------------------------------------------------
78
79 function handle_new_order()
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;
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 (!references::is_valid($_POST['ref'])) 
105         {
106                 display_error( _("You must enter a reference."));
107                 $input_error = 1;
108         } 
109         elseif (!is_new_reference($_POST['ref'], systypes::bank_payment())) 
110         {
111                 display_error( _("The entered reference is already in use."));
112                 $input_error = 1;
113         } 
114         elseif (!is_date($_POST['date_'])) 
115         {
116                 display_error(_("The entered date for the payment is invalid."));
117                 $input_error = 1;
118         } 
119         elseif (!is_date_in_fiscalyear($_POST['date_'])) 
120         {
121                 display_error(_("The entered date is not in fiscal year."));
122                 $input_error = 1;
123         }
124
125         if ($input_error == 1)
126                 unset($_POST['Process']);
127 }
128
129 if (isset($_POST['Process']))
130 {
131
132         $trans = add_bank_payment($_POST['bank_account'],
133                 $_SESSION['pay_items'], $_POST['date_'],
134                 $_POST['PayType'], $_POST['person_id'], $_POST['PersonDetailID'],
135                 $_POST['type'], $_POST['ref'], $_POST['memo_']);
136
137         $trans_type = $trans[0];
138         $trans_no = $trans[1];
139
140         $_SESSION['pay_items']->clear_items();
141         unset($_SESSION['pay_items']);
142
143         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
144
145 } /*end of process credit note */
146
147 //-----------------------------------------------------------------------------------------------
148
149 function check_item_data()
150 {
151         if (!is_numeric($_POST['amount']))
152         {
153                 display_error( _("The amount entered is not a valid number."));
154                 return false;
155         }
156
157         if ($_POST['amount'] <= 0)
158         {
159                 display_error( _("The amount entered must be a postitive number."));
160                 return false;
161         }
162
163         if ($_POST['code_id'] == $_POST['bank_account']) 
164         {
165                 display_error( _("The source and destination accouts cannot be the same."));
166                 return false;
167         }
168
169         if (is_bank_account($_POST['code_id'])) 
170         {
171                 display_error( _("You cannot make a payment to a bank account. Please use the transfer funds facility for this."));
172                 return false;
173         }
174
175         return true;
176 }
177
178 //-----------------------------------------------------------------------------------------------
179
180 function handle_update_item()
181 {
182     if($_POST['UpdateItem'] != "" && check_item_data())
183     {
184         $_SESSION['pay_items']->update_gl_item($_POST['Index'], $_POST['dimension_id'], 
185                 $_POST['dimension2_id'], $_POST['amount'], $_POST['LineMemo']);
186     }
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'], $_POST['amount'], $_POST['LineMemo']);
205 }
206
207 //-----------------------------------------------------------------------------------------------
208
209 if (isset($_GET['Delete']) || isset($_GET['Edit']))
210         copy_from_py();
211
212 if (isset($_GET['Delete']))
213         handle_delete_item();
214
215 if (isset($_POST['AddItem']) || isset($_POST['UpdateItem']))
216         copy_to_py();
217         
218 if (isset($_POST['AddItem']))
219         handle_new_item();
220
221 if (isset($_POST['UpdateItem']))
222         handle_update_item();
223
224 //-----------------------------------------------------------------------------------------------
225
226 if (isset($_GET['NewPayment']) || !isset($_SESSION['pay_items']))
227 {
228         handle_new_order();
229 }
230
231 //-----------------------------------------------------------------------------------------------
232
233 start_form(false, true);
234
235 display_order_header($_SESSION['pay_items']);
236
237 start_table("$table_style2 width=90%", 10);
238 start_row();
239 echo "<td>";
240 display_gl_items(_("Payment Items"), $_SESSION['pay_items']);
241 gl_options_controls();
242 echo "</td>";
243 end_row();
244 end_table(1);
245
246 if (!isset($_POST['Process']))
247 {
248     submit_center_first('Update', _("Update"));
249         if ($_SESSION['pay_items']->count_gl_items() >= 1)
250             submit_center_last('Process', _("Process Payment"));
251 }
252
253 end_form();
254
255 //------------------------------------------------------------------------------------------------
256
257 end_page();
258
259 ?>