Release 1.0.1 established on SourceForge, fixing the bugs and including a Date Picker...
[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 cannot enter both a debit amount and a credit amount."));
142                 return false;
143         }
144
145         if ((!isset($_POST['AmountDebit']) && !isset($_POST['AmountCredit']))
146                         || ($_POST['AmountDebit'] == "" && $_POST['AmountCredit'] == "")) 
147         {
148                 display_error(_("You must enter either a debit amount or a credit amount."));
149                 return false;
150         }
151
152         if (isset($_POST['AmountDebit']) && $_POST['AmountDebit'] != "") 
153         {
154
155         if (!is_numeric($_POST['AmountDebit']))
156         {
157                 display_error(_("The debit amount entered is not a valid number."));
158                 return false;
159         }
160
161         if ($_POST['AmountDebit'] <= 0)
162         {
163                 display_error(_("The debit amount entered cannot be zero or negative."));
164                 return false;
165         }
166         }
167
168         if (isset($_POST['AmountCredit']) && $_POST['AmountCredit'] != "") 
169         {
170
171         if (!is_numeric($_POST['AmountCredit']))
172         {
173                 display_error(_("The credit amount entered is not a valid number."));
174                 return false;
175         }
176
177         if ($_POST['AmountCredit'] <= 0)
178         {
179                 display_error(_("The credit amount entered cannot be zero or negative."));
180                 return false;
181         }
182         }
183
184         if ($_SESSION["wa_current_user"]->access != 2 && is_bank_account($_POST['code_id'])) 
185         {
186                 display_error(_("You cannot make a journal entry for a bank account. Please use one of the banking functions for bank transactions."));
187                 return false;
188         }
189
190         return true;
191 }
192
193 //-----------------------------------------------------------------------------------------------
194
195 function handle_update_item()
196 {
197     if($_POST['UpdateItem'] != "" && check_item_data())
198     {
199         if ($_POST['AmountDebit'] > 0)
200                 $amount = $_POST['AmountDebit'];
201         else
202                 $amount = -$_POST['AmountCredit'];
203
204         $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_POST['dimension_id'],
205                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
206     }
207 }
208
209 //-----------------------------------------------------------------------------------------------
210
211 function handle_delete_item()
212 {
213         $_SESSION['journal_items']->remove_gl_item($_GET['Delete']);
214 }
215
216 //-----------------------------------------------------------------------------------------------
217
218 function handle_new_item()
219 {
220         if (!check_item_data())
221                 return;
222
223         if ($_POST['AmountDebit'] > 0)
224                 $amount = $_POST['AmountDebit'];
225         else
226                 $amount = -$_POST['AmountCredit'];
227         
228         $_SESSION['journal_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
229                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
230 }
231
232 //-----------------------------------------------------------------------------------------------
233
234 if (isset($_GET['Delete']) || isset($_GET['Edit']))
235         copy_from_je();
236
237 if (isset($_GET['Delete']))
238         handle_delete_item();
239
240 if (isset($_POST['AddItem']) || isset($_POST['UpdateItem']))
241         copy_to_je();
242
243 if (isset($_POST['AddItem']))
244         handle_new_item();
245
246 if (isset($_POST['UpdateItem']))
247         handle_update_item();
248
249 //-----------------------------------------------------------------------------------------------
250
251 if (isset($_GET['NewJournal']) || !isset($_SESSION['journal_items']))
252 {
253         handle_new_order();
254 }
255
256 //-----------------------------------------------------------------------------------------------
257
258 start_form();
259
260 display_order_header($_SESSION['journal_items']);
261
262 start_table("$table_style2 width=90%", 10);
263 start_row();
264 echo "<td>";
265 display_gl_items(_("Rows"), $_SESSION['journal_items']);
266 gl_options_controls();
267 echo "</td>";
268 end_row();
269 end_table(1);
270
271 if (!isset($_POST['Process']))
272 {
273         if ($_SESSION['journal_items']->count_gl_items() >= 1 &&
274                 abs($_SESSION['journal_items']->gl_items_total()) < 0.0001)
275         {
276             submit_center('Process', _("Process Journal Entry"));
277         } 
278         else 
279         {
280                 display_note(_("The journal must balance (debits equal to credits) before it can be processed."), 0, 1);
281         }
282 }
283
284 end_form();
285
286 //------------------------------------------------------------------------------------------------
287
288 end_page();
289
290 ?>