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