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