Added quick entries in supplier invoice/credit note as well. Based on bank payments
[fa-stable.git] / gl / gl_journal.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-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
95         if (!is_date($_POST['date_'])) 
96         {
97                 display_error(_("The entered date is invalid."));
98                 set_focus('date_');
99                 $input_error = 1;
100         } 
101         elseif (!is_date_in_fiscalyear($_POST['date_'])) 
102         {
103                 display_error(_("The entered date is not in fiscal year."));
104                 set_focus('date_');
105                 $input_error = 1;
106         } 
107         if (!references::is_valid($_POST['ref'])) 
108         {
109                 display_error( _("You must enter a reference."));
110                 set_focus('ref');
111                 $input_error = 1;
112         } 
113         elseif (references::exists(systypes::journal_entry(), $_POST['ref'])) 
114         {
115                 display_error( _("The entered reference is already in use."));
116                 set_focus('ref');
117                 $input_error = 1;
118         }
119
120         if ($input_error == 1)
121                 unset($_POST['Process']);
122 }
123
124 if (isset($_POST['Process']))
125 {
126
127         $trans_no = add_journal_entries($_SESSION['journal_items']->gl_items,
128                 $_POST['date_'], $_POST['ref'], check_value('Reverse'), $_POST['memo_']);
129
130         $_SESSION['journal_items']->clear_items();
131         unset($_SESSION['journal_items']);
132
133         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
134 } /*end of process credit note */
135
136 //-----------------------------------------------------------------------------------------------
137
138 function check_item_data()
139 {
140         if (isset($_POST['dimension_id']) && $_POST['dimension_id'] != 0 && dimension_is_closed($_POST['dimension_id'])) 
141         {
142                 display_error(_("Dimension is closed."));
143                 set_focus('dimension_id');
144                 return false;
145         }
146
147         if (isset($_POST['dimension2_id']) && $_POST['dimension2_id'] != 0 && dimension_is_closed($_POST['dimension2_id'])) 
148         {
149                 display_error(_("Dimension is closed."));
150                 set_focus('dimension2_id');
151                 return false;
152         }
153
154         if (!(input_num('AmountDebit')!=0 ^ input_num('AmountCredit')!=0) )
155         {
156                 display_error(_("You must enter either a debit amount or a credit amount."));
157                 set_focus('AmountDebit');
158                 return false;
159         }
160
161         if (strlen($_POST['AmountDebit']) && !check_num('AmountDebit', 0)) 
162         {
163                 display_error(_("The debit amount entered is not a valid number or is less than zero."));
164                 set_focus('AmountDebit');
165                 return false;
166         } elseif (strlen($_POST['AmountCredit']) && !check_num('AmountCredit', 0))
167         {
168                 display_error(_("The credit amount entered is not a valid number or is less than zero."));
169                 set_focus('AmountCredit');
170                 return false;
171         }
172
173
174         if ($_SESSION["wa_current_user"]->access != 2 && is_bank_account($_POST['code_id'])) 
175         {
176                 display_error(_("You cannot make a journal entry for a bank account. Please use one of the banking functions for bank transactions."));
177                 set_focus('code_id');
178                 return false;
179         }
180
181         return true;
182 }
183
184 //-----------------------------------------------------------------------------------------------
185
186 function handle_update_item()
187 {
188     if($_POST['UpdateItem'] != "" && check_item_data())
189     {
190         if (input_num('AmountDebit') > 0)
191                 $amount = input_num('AmountDebit');
192         else
193                 $amount = -input_num('AmountCredit');
194
195         $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_POST['dimension_id'],
196                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
197     }
198         line_start_focus();
199 }
200
201 //-----------------------------------------------------------------------------------------------
202
203 function handle_delete_item($id)
204 {
205         $_SESSION['journal_items']->remove_gl_item($id);
206         line_start_focus();
207 }
208
209 //-----------------------------------------------------------------------------------------------
210
211 function handle_new_item()
212 {
213         if (!check_item_data())
214                 return;
215
216         if (input_num('AmountDebit') > 0)
217                 $amount = input_num('AmountDebit');
218         else
219                 $amount = -input_num('AmountCredit');
220         
221         $_SESSION['journal_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
222                 $_POST['dimension2_id'], $amount, $_POST['LineMemo']);
223         line_start_focus();
224 }
225
226 /*
227 function display_quick_entries(&$cart)
228 {
229         if (!get_post('person_id'))
230         {
231                 display_error( _("No Quick Entries are defined."));
232                 set_focus('totamount');
233         }
234         else
235         {
236                 $rate = 0;
237                 $totamount = input_num('totamount');
238                 //$totamount = ($cart->trans_type==systypes::bank_payment() ? 1:-1) * $totamount;
239                 $qe = get_quick_entry($_POST['person_id']);
240                 $qe_lines = get_quick_entry_lines($_POST['person_id']);
241                 while ($qe_line = db_fetch($qe_lines))
242                 {
243                         if ($qe_line['tax_acc'])
244                         {
245                                 $account = get_gl_account($qe_line['account']);
246                                 $tax_group = $account['tax_code'];
247                                 $items = get_tax_group_items($tax_group);
248                                 while ($item = db_fetch($items))
249                                         $rate += $item['rate'];
250                                 if ($rate != 0)
251                                         $totamount = $totamount * 100 / ($rate + 100);
252                                 $cart->clear_items();
253
254                                 $cart->add_gl_item($qe_line['account'], $qe_line['dimension_id'], $qe_line['dimension2_id'], 
255                                         $totamount, $qe['description']);
256                                 $items = get_tax_group_items($tax_group);
257                                 while ($item = db_fetch($items))
258                                 {
259                                         if ($item['rate'] != 0)
260                                         {
261                                                 $amount = $totamount * $item['rate'] / 100;
262                                                 $code = ($amount < 0 ? $item['purchasing_gl_code'] : 
263                                                         $item['sales_gl_code']);
264                                                 $cart->add_gl_item($code, 0, 0, $amount, $qe['description']);
265                                         }
266                                 }
267                         }
268                         else
269                         {
270                                 if ($qe_line['pct'])
271                                         $amount = $totamount * $qe_line['amount'] / 100;
272                                 else
273                                         $amount = $qe_line['amount'];
274                                 $cart->add_gl_item($qe_line['account'], $qe_line['dimension_id'], $qe_line['dimension2_id'], 
275                                         $amount, $qe['description']);
276                         }               
277                 }
278                 line_start_focus();
279         }       
280 }
281 */
282 //-----------------------------------------------------------------------------------------------
283 $id = find_submit('Delete');
284 if ($id != -1)
285         handle_delete_item($id);
286
287 if (isset($_POST['AddItem'])) 
288         handle_new_item();
289
290 if (isset($_POST['UpdateItem'])) 
291         handle_update_item();
292         
293 if (isset($_POST['CancelItemChanges']))
294         line_start_focus();
295
296 if (isset($_POST['go']))
297         display_quick_entries($_SESSION['journal_items'], $_POST['person_id'], input_num('totamount'));
298 //-----------------------------------------------------------------------------------------------
299
300 if (isset($_GET['NewJournal']) || !isset($_SESSION['journal_items']))
301 {
302         handle_new_order();
303 }
304
305 //-----------------------------------------------------------------------------------------------
306
307 start_form();
308
309 display_order_header($_SESSION['journal_items']);
310
311 start_table("$table_style2 width=90%", 10);
312 start_row();
313 echo "<td>";
314 display_gl_items(_("Rows"), $_SESSION['journal_items']);
315 gl_options_controls();
316 echo "</td>";
317 end_row();
318 end_table(1);
319
320 submit_center('Process', _("Process Journal Entry"), true , 
321         _('Process journal entry only if debits equal to credits'), true);
322
323 end_form();
324 //------------------------------------------------------------------------------------------------
325
326 end_page();
327
328 ?>