Focus set to invalid field after submit check fail
[fa-stable.git] / gl / gl_journal.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_journal_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 = '';
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
22 set_focus('CodeID2');
23
24 page(_("Journal Entry"), false, false,'', $js);
25
26 //-----------------------------------------------------------------------------------------------
27
28 if (isset($_GET['AddedID'])) 
29 {
30         $trans_no = $_GET['AddedID'];
31         $trans_type = systypes::journal_entry();
32
33         display_notification_centered( _("Journal entry has been entered") . " #$trans_no");
34
35     display_note(get_gl_view_str($trans_type, $trans_no, _("View this Journal Entry")));
36
37         hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter Another Journal Entry"));
38
39         display_footer_exit();
40 }
41
42 //--------------------------------------------------------------------------------------------------
43
44 function copy_to_je()
45 {
46         $_SESSION['journal_items']->tran_date = $_POST['date_'];
47         $_SESSION['journal_items']->transfer_type = check_value('Reverse');
48         $_SESSION['journal_items']->memo_ = $_POST['memo_'];
49 }
50
51 //--------------------------------------------------------------------------------------------------
52
53 function copy_from_je()
54 {
55         $_POST['date_'] = $_SESSION['journal_items']->tran_date;
56         $_POST['Reverse'] = $_SESSION['journal_items']->transfer_type;
57         $_POST['memo_'] = $_SESSION['journal_items']->memo_;
58 }
59
60 //----------------------------------------------------------------------------------------
61
62 function handle_new_order()
63 {
64         if (isset($_SESSION['journal_items']))
65         {
66                 $_SESSION['journal_items']->clear_items();
67                 unset ($_SESSION['journal_items']);
68         }
69
70     session_register("journal_items");
71
72     $_SESSION['journal_items'] = new items_cart;
73
74         $_POST['date_'] = Today();
75         if (!is_date_in_fiscalyear($_POST['date_']))
76                 $_POST['date_'] = end_fiscalyear();
77         $_SESSION['journal_items']->tran_date = $_POST['date_'];        
78 }
79
80 //-----------------------------------------------------------------------------------------------
81
82 if (isset($_POST['Process']))
83 {
84
85         $input_error = 0;
86
87         if (!is_date($_POST['date_'])) 
88         {
89                 display_error(_("The entered date is invalid."));
90                 set_focus('date_');
91                 $input_error = 1;
92         } 
93         elseif (!is_date_in_fiscalyear($_POST['date_'])) 
94         {
95                 display_error(_("The entered date is not in fiscal year."));
96                 set_focus('date_');
97                 $input_error = 1;
98         } 
99         elseif (!references::is_valid($_POST['ref'])) 
100         {
101                 display_error( _("You must enter a reference."));
102                 set_focus('ref');
103                 $input_error = 1;
104         } 
105         elseif (references::exists(systypes::journal_entry(), $_POST['ref'])) 
106         {
107                 display_error( _("The entered reference is already in use."));
108                 set_focus('ref');
109                 $input_error = 1;
110         }
111
112         if ($input_error == 1)
113                 unset($_POST['Process']);
114 }
115
116 if (isset($_POST['Process']))
117 {
118
119         $trans_no = add_journal_entries($_SESSION['journal_items']->gl_items,
120                 $_POST['date_'], $_POST['ref'], check_value('Reverse'), $_POST['memo_']);
121
122         $_SESSION['journal_items']->clear_items();
123         unset($_SESSION['journal_items']);
124
125         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
126 } /*end of process credit note */
127
128 //-----------------------------------------------------------------------------------------------
129
130 function check_item_data()
131 {
132         if (isset($_POST['dimension_id']) && $_POST['dimension_id'] != 0 && dimension_is_closed($_POST['dimension_id'])) 
133         {
134                 display_error(_("Dimension is closed."));
135                 set_focus('dimension_id');
136                 return false;
137         }
138
139         if (isset($_POST['dimension2_id']) && $_POST['dimension2_id'] != 0 && dimension_is_closed($_POST['dimension2_id'])) 
140         {
141                 display_error(_("Dimension is closed."));
142                 set_focus('dimension2_id');
143                 return false;
144         }
145
146         if (!(input_num('AmountDebit')!=0 ^ input_num('AmountCredit')!=0) )
147         {
148                 display_error(_("You must enter either a debit amount or a credit amount."));
149                 set_focus('Amount_Debit');
150                 return false;
151         }
152
153         if (strlen($_POST['AmountDebit']) && !check_num('AmountDebit', 0)) 
154         {
155                 display_error(_("The debit amount entered is not a valid number or is less than zero."));
156                 set_focus('AmountDebit');
157                 return false;
158         } elseif (strlen($_POST['AmountCredit']) && !check_num('AmountCredit', 0))
159         {
160                 display_error(_("The credit amount entered is not a valid number or is less than zero."));
161                 set_focus('AmountCredit');
162                 return false;
163         }
164
165
166         if ($_SESSION["wa_current_user"]->access != 2 && is_bank_account($_POST['code_id'])) 
167         {
168                 display_error(_("You cannot make a journal entry for a bank account. Please use one of the banking functions for bank transactions."));
169                 set_focus('code_id');
170                 return false;
171         }
172
173         return true;
174 }
175
176 //-----------------------------------------------------------------------------------------------
177
178 function handle_update_item()
179 {
180     if($_POST['UpdateItem'] != "" && check_item_data())
181     {
182         if (input_num('AmountDebit') > 0)
183                 $amount = input_num('AmountDebit');
184         else
185                 $amount = -input_num('AmountCredit');
186
187         $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_POST['dimension_id'],
188                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
189     }
190 }
191
192 //-----------------------------------------------------------------------------------------------
193
194 function handle_delete_item()
195 {
196         $_SESSION['journal_items']->remove_gl_item($_GET['Delete']);
197 }
198
199 //-----------------------------------------------------------------------------------------------
200
201 function handle_new_item()
202 {
203         if (!check_item_data())
204                 return;
205
206         if (input_num('AmountDebit') > 0)
207                 $amount = input_num('AmountDebit');
208         else
209                 $amount = -input_num('AmountCredit');
210         
211         $_SESSION['journal_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
212                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
213 }
214
215 //-----------------------------------------------------------------------------------------------
216
217 if (isset($_GET['Delete']) || isset($_GET['Edit']))
218         copy_from_je();
219
220 if (isset($_GET['Delete']))
221         handle_delete_item();
222
223 if (isset($_POST['AddItem']) || isset($_POST['UpdateItem']))
224         copy_to_je();
225
226 if (isset($_POST['AddItem']))
227         handle_new_item();
228
229 if (isset($_POST['UpdateItem']))
230         handle_update_item();
231
232 //-----------------------------------------------------------------------------------------------
233
234 if (isset($_GET['NewJournal']) || !isset($_SESSION['journal_items']))
235 {
236         handle_new_order();
237 }
238
239 //-----------------------------------------------------------------------------------------------
240
241 start_form();
242
243 display_order_header($_SESSION['journal_items']);
244
245 start_table("$table_style2 width=90%", 10);
246 start_row();
247 echo "<td>";
248 display_gl_items(_("Rows"), $_SESSION['journal_items']);
249 gl_options_controls();
250 echo "</td>";
251 end_row();
252 end_table(1);
253
254 if ($_SESSION['journal_items']->count_gl_items() >= 1 &&
255         abs($_SESSION['journal_items']->gl_items_total()) < 0.0001)
256 {
257     submit_center('Process', _("Process Journal Entry"));
258
259 else 
260 {
261         display_note(_("The journal must balance (debits equal to credits) before it can be processed."), 0, 1);
262 }
263
264 end_form();
265
266 //------------------------------------------------------------------------------------------------
267
268 end_page();
269
270 ?>