New files from unstable branch
[fa-stable.git] / gl / gl_journal.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_JOURNALENTRY';
13 $path_to_root = "..";
14 include_once($path_to_root . "/includes/ui/items_cart.inc");
15
16 include_once($path_to_root . "/includes/session.inc");
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
20
21 include_once($path_to_root . "/gl/includes/ui/gl_journal_ui.inc");
22 include_once($path_to_root . "/gl/includes/gl_db.inc");
23 include_once($path_to_root . "/gl/includes/gl_ui.inc");
24
25 $js = '';
26 if ($use_popup_windows)
27         $js .= get_js_open_window(800, 500);
28 if ($use_date_picker)
29         $js .= get_js_date_picker();
30
31 if (isset($_GET['ModifyGL'])) {
32         $_SESSION['page_title'] = sprintf(_("Modifying Journal Transaction # %d."), 
33                 $_GET['trans_no']);
34         $help_context = "Modifying Journal Entry";
35 } else
36         $_SESSION['page_title'] = _($help_context = "Journal Entry");
37
38 page($_SESSION['page_title'], false, false,'', $js);
39 //--------------------------------------------------------------------------------------------------
40
41 function line_start_focus() {
42   global        $Ajax;
43
44   $Ajax->activate('items_table');
45   set_focus('_code_id_edit');
46 }
47 //-----------------------------------------------------------------------------------------------
48
49 if (isset($_GET['AddedID'])) 
50 {
51         $trans_no = $_GET['AddedID'];
52         $trans_type = ST_JOURNAL;
53
54         display_notification_centered( _("Journal entry has been entered") . " #$trans_no");
55
56     display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
57
58         reset_focus();
59         hyperlink_params($_SERVER['PHP_SELF'], _("Enter &New Journal Entry"), "NewJournal=Yes");
60
61         display_footer_exit();
62 } elseif (isset($_GET['UpdatedID'])) 
63 {
64         $trans_no = $_GET['UpdatedID'];
65         $trans_type = ST_JOURNAL;
66
67         display_notification_centered( _("Journal entry has been updated") . " #$trans_no");
68
69     display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
70
71         hyperlink_no_params($path_to_root."/gl/inquiry/journal_inquiry.php", _("Return to Journal &Inquiry"));
72
73         display_footer_exit();
74 }
75 //--------------------------------------------------------------------------------------------------
76
77 if (isset($_GET['NewJournal']))
78 {
79         create_cart(0,0);
80
81 elseif (isset($_GET['ModifyGL']))
82 {
83         if (!isset($_GET['trans_type']) || $_GET['trans_type']!= 0) {
84                 display_error(_("You can edit directly only journal entries created via Journal Entry page."));
85                 hyperlink_params("$path_to_root/gl/gl_journal.php", _("Entry &New Journal Entry"), "NewJournal=Yes");
86                 display_footer_exit();
87         }
88         create_cart($_GET['trans_type'], $_GET['trans_no']);
89 }
90
91 function create_cart($type=0, $trans_no=0)
92 {
93         global $Refs;
94
95         if (isset($_SESSION['journal_items']))
96         {
97                 unset ($_SESSION['journal_items']);
98         }
99
100         $cart = new items_cart($type);
101     $cart->order_id = $trans_no;
102
103         if ($trans_no) {
104                 $result = get_gl_trans($type, $trans_no);
105
106                 if ($result) {
107                         while ($row = db_fetch($result)) {
108                                 if ($row['amount'] == 0) continue;
109                                 $date = $row['tran_date'];
110                                 $cart->add_gl_item($row['account'], $row['dimension_id'], 
111                                         $row['dimension2_id'], $row['amount'], $row['memo_']);
112                         }
113                 }
114                 $cart->memo_ = get_comments_string($type, $trans_no);
115                 $cart->tran_date = sql2date($date);
116                 $cart->reference = $Refs->get($type, $trans_no);
117                 $_POST['ref_original'] = $cart->reference; // Store for comparison when updating
118         } else {
119                 $cart->reference = $Refs->get_next(0);
120                 $cart->tran_date = new_doc_date();
121                 if (!is_date_in_fiscalyear($cart->tran_date))
122                         $cart->tran_date = end_fiscalyear();
123                 $_POST['ref_original'] = -1;
124         }
125
126         $_POST['memo_'] = $cart->memo_;
127         $_POST['ref'] = $cart->reference;
128         $_POST['date_'] = $cart->tran_date;
129
130         $_SESSION['journal_items'] = &$cart;
131 }
132
133 //-----------------------------------------------------------------------------------------------
134
135 if (isset($_POST['Process']))
136 {
137
138         $input_error = 0;
139
140         if ($_SESSION['journal_items']->count_gl_items() < 1) {
141                 display_error(_("You must enter at least one journal line."));
142                 set_focus('code_id');
143                 $input_error = 1;
144         }
145         if (abs($_SESSION['journal_items']->gl_items_total()) > 0.0001)
146         {
147                 display_error(_("The journal must balance (debits equal to credits) before it can be processed."));
148                 set_focus('code_id');
149                 $input_error = 1;
150         }
151
152         if (!is_date($_POST['date_'])) 
153         {
154                 display_error(_("The entered date is invalid."));
155                 set_focus('date_');
156                 $input_error = 1;
157         } 
158         elseif (!is_date_in_fiscalyear($_POST['date_'])) 
159         {
160                 display_error(_("The entered date is not in fiscal year."));
161                 set_focus('date_');
162                 $input_error = 1;
163         } 
164         if (!$Refs->is_valid($_POST['ref'])) 
165         {
166                 display_error( _("You must enter a reference."));
167                 set_focus('ref');
168                 $input_error = 1;
169         } 
170         elseif ($Refs->exists(ST_JOURNAL, $_POST['ref'])) 
171         {
172             // The reference can exist already so long as it's the same as the original (when modifying) 
173             if ($_POST['ref'] != $_POST['ref_original']) {
174                 display_error( _("The entered reference is already in use."));
175                 set_focus('ref');
176                 $input_error = 1;
177             }
178         }
179         if ($input_error == 1)
180                 unset($_POST['Process']);
181 }
182
183 if (isset($_POST['Process']))
184 {
185         $cart = &$_SESSION['journal_items'];
186         $new = $cart->order_id == 0;
187
188         $cart->reference = $_POST['ref'];
189         $cart->memo_ = $_POST['memo_'];
190         $cart->tran_date = $_POST['date_'];
191
192         $trans_no = write_journal_entries($cart, check_value('Reverse'));
193
194         $cart->clear_items();
195         new_doc_date($_POST['date_']);
196         unset($_SESSION['journal_items']);
197         if($new)
198                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
199         else
200                 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$trans_no");
201 }
202
203 //-----------------------------------------------------------------------------------------------
204
205 function check_item_data()
206 {
207         if (isset($_POST['dimension_id']) && $_POST['dimension_id'] != 0 && dimension_is_closed($_POST['dimension_id'])) 
208         {
209                 display_error(_("Dimension is closed."));
210                 set_focus('dimension_id');
211                 return false;
212         }
213
214         if (isset($_POST['dimension2_id']) && $_POST['dimension2_id'] != 0 && dimension_is_closed($_POST['dimension2_id'])) 
215         {
216                 display_error(_("Dimension is closed."));
217                 set_focus('dimension2_id');
218                 return false;
219         }
220
221         if (!(input_num('AmountDebit')!=0 ^ input_num('AmountCredit')!=0) )
222         {
223                 display_error(_("You must enter either a debit amount or a credit amount."));
224                 set_focus('AmountDebit');
225                 return false;
226         }
227
228         if (strlen($_POST['AmountDebit']) && !check_num('AmountDebit', 0)) 
229         {
230                 display_error(_("The debit amount entered is not a valid number or is less than zero."));
231                 set_focus('AmountDebit');
232                 return false;
233         } elseif (strlen($_POST['AmountCredit']) && !check_num('AmountCredit', 0))
234         {
235                 display_error(_("The credit amount entered is not a valid number or is less than zero."));
236                 set_focus('AmountCredit');
237                 return false;
238         }
239         
240         if (!is_tax_gl_unique(get_post('code_id'))) {
241                 display_error(_("Cannot post to GL account used by more than one tax type."));
242                 set_focus('code_id');
243                 return false;
244         }
245
246         if (!$_SESSION["wa_current_user"]->can_access('SA_BANKJOURNAL') && is_bank_account($_POST['code_id'])) 
247         {
248                 display_error(_("You cannot make a journal entry for a bank account. Please use one of the banking functions for bank transactions."));
249                 set_focus('code_id');
250                 return false;
251         }
252
253         return true;
254 }
255
256 //-----------------------------------------------------------------------------------------------
257
258 function handle_update_item()
259 {
260     if($_POST['UpdateItem'] != "" && check_item_data())
261     {
262         if (input_num('AmountDebit') > 0)
263                 $amount = input_num('AmountDebit');
264         else
265                 $amount = -input_num('AmountCredit');
266
267         $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_POST['code_id'], 
268             $_POST['dimension_id'], $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
269     }
270         line_start_focus();
271 }
272
273 //-----------------------------------------------------------------------------------------------
274
275 function handle_delete_item($id)
276 {
277         $_SESSION['journal_items']->remove_gl_item($id);
278         line_start_focus();
279 }
280
281 //-----------------------------------------------------------------------------------------------
282
283 function handle_new_item()
284 {
285         if (!check_item_data())
286                 return;
287
288         if (input_num('AmountDebit') > 0)
289                 $amount = input_num('AmountDebit');
290         else
291                 $amount = -input_num('AmountCredit');
292         
293         $_SESSION['journal_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
294                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
295         line_start_focus();
296 }
297
298 //-----------------------------------------------------------------------------------------------
299 $id = find_submit('Delete');
300 if ($id != -1)
301         handle_delete_item($id);
302
303 if (isset($_POST['AddItem'])) 
304         handle_new_item();
305
306 if (isset($_POST['UpdateItem'])) 
307         handle_update_item();
308         
309 if (isset($_POST['CancelItemChanges']))
310         line_start_focus();
311
312 if (isset($_POST['go']))
313 {
314         display_quick_entries($_SESSION['journal_items'], $_POST['person_id'], input_num('totamount'), QE_JOURNAL);
315         $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
316         line_start_focus();
317 }       
318 //-----------------------------------------------------------------------------------------------
319
320 start_form();
321
322 display_order_header($_SESSION['journal_items']);
323
324 start_table(TABLESTYLE2, "width=90%", 10);
325 start_row();
326 echo "<td>";
327 display_gl_items(_("Rows"), $_SESSION['journal_items']);
328 gl_options_controls();
329 echo "</td>";
330 end_row();
331 end_table(1);
332
333 submit_center('Process', _("Process Journal Entry"), true , 
334         _('Process journal entry only if debits equal to credits'), 'default');
335
336 end_form();
337 //------------------------------------------------------------------------------------------------
338
339 end_page();
340
341 ?>