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