2cef81c84cfca176a4b1fa377efb30e61ac7a061
[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 function display_quick_entries(&$cart)
218 {
219         if (!get_post('person_id'))
220         {
221                 display_error( _("No Quick Entries are defined."));
222                 set_focus('totamount');
223         }
224         else
225         {
226                 $rate = 0;
227                 $totamount = input_num('totamount');
228                 //$totamount = ($cart->trans_type==systypes::bank_payment() ? 1:-1) * $totamount;
229                 $qe = get_quick_entry($_POST['person_id']);
230                 $qe_lines = get_quick_entry_lines($_POST['person_id']);
231                 while ($qe_line = db_fetch($qe_lines))
232                 {
233                         if ($qe_line['tax_acc'])
234                         {
235                                 $account = get_gl_account($qe_line['account']);
236                                 $tax_group = $account['tax_code'];
237                                 $items = get_tax_group_items($tax_group);
238                                 while ($item = db_fetch($items))
239                                         $rate += $item['rate'];
240                                 if ($rate != 0)
241                                         $totamount = $totamount * 100 / ($rate + 100);
242                                 $cart->clear_items();
243
244                                 $cart->add_gl_item($qe_line['account'], $qe_line['dimension_id'], $qe_line['dimension2_id'], 
245                                         $totamount, $qe['description']);
246                                 $items = get_tax_group_items($tax_group);
247                                 while ($item = db_fetch($items))
248                                 {
249                                         if ($item['rate'] != 0)
250                                         {
251                                                 $amount = $totamount * $item['rate'] / 100;
252                                                 $code = ($amount < 0 ? $item['purchasing_gl_code'] : 
253                                                         $item['sales_gl_code']);
254                                                 $cart->add_gl_item($code, 0, 0, $amount, $qe['description']);
255                                         }
256                                 }
257                         }
258                         else
259                         {
260                                 if ($qe_line['pct'])
261                                         $amount = $totamount * $qe_line['amount'] / 100;
262                                 else
263                                         $amount = $qe_line['amount'];
264                                 $cart->add_gl_item($qe_line['account'], $qe_line['dimension_id'], $qe_line['dimension2_id'], 
265                                         $amount, $qe['description']);
266                         }               
267                 }
268                 line_start_focus();
269         }       
270 }
271
272 //-----------------------------------------------------------------------------------------------
273 $id = find_submit('Delete');
274 if ($id != -1)
275         handle_delete_item($id);
276
277 if (isset($_POST['AddItem'])) 
278         handle_new_item();
279
280 if (isset($_POST['UpdateItem'])) 
281         handle_update_item();
282         
283 if (isset($_POST['CancelItemChanges']))
284         line_start_focus();
285
286 if (isset($_POST['go']))
287         display_quick_entries($_SESSION['journal_items']);
288 //-----------------------------------------------------------------------------------------------
289
290 if (isset($_GET['NewJournal']) || !isset($_SESSION['journal_items']))
291 {
292         handle_new_order();
293 }
294
295 //-----------------------------------------------------------------------------------------------
296
297 start_form();
298
299 display_order_header($_SESSION['journal_items']);
300
301 start_table("$table_style2 width=90%", 10);
302 start_row();
303 echo "<td>";
304 display_gl_items(_("Rows"), $_SESSION['journal_items']);
305 gl_options_controls();
306 echo "</td>";
307 end_row();
308 end_table(1);
309
310 submit_center('Process', _("Process Journal Entry"), true , 
311         _('Process journal entry only if debits equal to credits'), true);
312
313 end_form();
314 //------------------------------------------------------------------------------------------------
315
316 end_page();
317
318 ?>