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