0000696: Additional permission level for other user's transactions edition.
[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
42 function line_start_focus() {
43   global        $Ajax;
44
45   unset($_POST['Index']);
46   $Ajax->activate('items_table');
47   set_focus('_code_id_edit');
48 }
49 //-----------------------------------------------------------------------------------------------
50
51 if (isset($_GET['AddedID'])) 
52 {
53         $trans_no = $_GET['AddedID'];
54         $trans_type = ST_JOURNAL;
55
56         display_notification_centered( _("Journal entry has been entered") . " #$trans_no");
57
58     display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
59
60         reset_focus();
61         hyperlink_params($_SERVER['PHP_SELF'], _("Enter &New Journal Entry"), "NewJournal=Yes");
62
63         hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
64
65         display_footer_exit();
66 } elseif (isset($_GET['UpdatedID'])) 
67 {
68         $trans_no = $_GET['UpdatedID'];
69         $trans_type = ST_JOURNAL;
70
71         display_notification_centered( _("Journal entry has been updated") . " #$trans_no");
72
73     display_note(get_gl_view_str($trans_type, $trans_no, _("&View this Journal Entry")));
74
75         hyperlink_no_params($path_to_root."/gl/inquiry/journal_inquiry.php", _("Return to Journal &Inquiry"));
76
77         display_footer_exit();
78 }
79 //--------------------------------------------------------------------------------------------------
80
81 if (isset($_GET['NewJournal']))
82 {
83         create_cart(0,0);
84 }
85 elseif (isset($_GET['ModifyGL']))
86 {
87         check_is_editable($_GET['trans_type'], $_GET['trans_no']);
88
89         if (!isset($_GET['trans_type']) || $_GET['trans_type']!= 0) {
90                 display_error(_("You can edit directly only journal entries created via Journal Entry page."));
91                 hyperlink_params("$path_to_root/gl/gl_journal.php", _("Entry &New Journal Entry"), "NewJournal=Yes");
92                 display_footer_exit();
93         }
94
95         create_cart($_GET['trans_type'], $_GET['trans_no']);
96 }
97
98 function create_cart($type=0, $trans_no=0)
99 {
100         global $Refs;
101
102         if (isset($_SESSION['journal_items']))
103         {
104                 unset ($_SESSION['journal_items']);
105         }
106
107         $cart = new items_cart($type);
108     $cart->order_id = $trans_no;
109
110         if ($trans_no) {
111                 $result = get_gl_trans($type, $trans_no);
112
113                 if ($result) {
114                         while ($row = db_fetch($result)) {
115                                 if ($row['amount'] == 0) continue;
116                                 $date = $row['tran_date'];
117                                 $cart->add_gl_item($row['account'], $row['dimension_id'], 
118                                         $row['dimension2_id'], $row['amount'], $row['memo_'], '', $row['person_id']);
119                         }
120                 }
121                 $cart->memo_ = get_comments_string($type, $trans_no);
122                 $cart->tran_date = sql2date($date);
123                 $cart->reference = $Refs->get($type, $trans_no);
124                 $_POST['ref_original'] = $cart->reference; // Store for comparison when updating
125         } else {
126                 $cart->reference = $Refs->get_next(0);
127                 $cart->tran_date = new_doc_date();
128                 if (!is_date_in_fiscalyear($cart->tran_date))
129                         $cart->tran_date = end_fiscalyear();
130                 $_POST['ref_original'] = -1;
131         }
132
133         $_POST['memo_'] = $cart->memo_;
134         $_POST['ref'] = $cart->reference;
135         $_POST['date_'] = $cart->tran_date;
136
137         $_SESSION['journal_items'] = &$cart;
138 }
139
140 //-----------------------------------------------------------------------------------------------
141
142 if (isset($_POST['Process']))
143 {
144
145         $input_error = 0;
146
147         if ($_SESSION['journal_items']->count_gl_items() < 1) {
148                 display_error(_("You must enter at least one journal line."));
149                 set_focus('code_id');
150                 $input_error = 1;
151         }
152         if (abs($_SESSION['journal_items']->gl_items_total()) > 0.0001)
153         {
154                 display_error(_("The journal must balance (debits equal to credits) before it can be processed."));
155                 set_focus('code_id');
156                 $input_error = 1;
157         }
158
159         if (!is_date($_POST['date_'])) 
160         {
161                 display_error(_("The entered date is invalid."));
162                 set_focus('date_');
163                 $input_error = 1;
164         } 
165         elseif (!is_date_in_fiscalyear($_POST['date_'])) 
166         {
167                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
168                 set_focus('date_');
169                 $input_error = 1;
170         } 
171         if (!$Refs->is_valid($_POST['ref'])) 
172         {
173                 display_error( _("You must enter a reference."));
174                 set_focus('ref');
175                 $input_error = 1;
176         } 
177         elseif ($Refs->exists(ST_JOURNAL, $_POST['ref'])) 
178         {
179             // The reference can exist already so long as it's the same as the original (when modifying) 
180             if ($_POST['ref'] != $_POST['ref_original']) {
181                 display_error( _("The entered reference is already in use."));
182                 set_focus('ref');
183                 $input_error = 1;
184             }
185         }
186         if ($input_error == 1)
187                 unset($_POST['Process']);
188 }
189
190 if (isset($_POST['Process']))
191 {
192         $cart = &$_SESSION['journal_items'];
193         $new = $cart->order_id == 0;
194
195         $cart->reference = $_POST['ref'];
196         $cart->memo_ = $_POST['memo_'];
197         $cart->tran_date = $_POST['date_'];
198
199         $trans_no = write_journal_entries($cart, check_value('Reverse'));
200
201         $cart->clear_items();
202         new_doc_date($_POST['date_']);
203         unset($_SESSION['journal_items']);
204         if($new)
205                 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
206         else
207                 meta_forward($_SERVER['PHP_SELF'], "UpdatedID=$trans_no");
208 }
209
210 //-----------------------------------------------------------------------------------------------
211
212 function check_item_data()
213 {
214         if (isset($_POST['dimension_id']) && $_POST['dimension_id'] != 0 && dimension_is_closed($_POST['dimension_id'])) 
215         {
216                 display_error(_("Dimension is closed."));
217                 set_focus('dimension_id');
218                 return false;
219         }
220
221         if (isset($_POST['dimension2_id']) && $_POST['dimension2_id'] != 0 && dimension_is_closed($_POST['dimension2_id'])) 
222         {
223                 display_error(_("Dimension is closed."));
224                 set_focus('dimension2_id');
225                 return false;
226         }
227
228         if (!(input_num('AmountDebit')!=0 ^ input_num('AmountCredit')!=0) )
229         {
230                 display_error(_("You must enter either a debit amount or a credit amount."));
231                 set_focus('AmountDebit');
232                 return false;
233         }
234
235         if (strlen($_POST['AmountDebit']) && !check_num('AmountDebit', 0)) 
236         {
237                 display_error(_("The debit amount entered is not a valid number or is less than zero."));
238                 set_focus('AmountDebit');
239                 return false;
240         } elseif (strlen($_POST['AmountCredit']) && !check_num('AmountCredit', 0))
241         {
242                 display_error(_("The credit amount entered is not a valid number or is less than zero."));
243                 set_focus('AmountCredit');
244                 return false;
245         }
246         
247         if (!is_tax_gl_unique(get_post('code_id'))) {
248                 display_error(_("Cannot post to GL account used by more than one tax type."));
249                 set_focus('code_id');
250                 return false;
251         }
252
253         if (!$_SESSION["wa_current_user"]->can_access('SA_BANKJOURNAL') && is_bank_account($_POST['code_id'])) 
254         {
255                 display_error(_("You cannot make a journal entry for a bank account. Please use one of the banking functions for bank transactions."));
256                 set_focus('code_id');
257                 return false;
258         }
259
260         return true;
261 }
262
263 //-----------------------------------------------------------------------------------------------
264
265 function handle_update_item()
266 {
267     if($_POST['UpdateItem'] != "" && check_item_data())
268     {
269         if (input_num('AmountDebit') > 0)
270                 $amount = input_num('AmountDebit');
271         else
272                 $amount = -input_num('AmountCredit');
273
274         $_SESSION['journal_items']->update_gl_item($_POST['Index'], $_POST['code_id'], 
275             $_POST['dimension_id'], $_POST['dimension2_id'], $amount, $_POST['LineMemo'], '', get_post('person_id'));
276     }
277         line_start_focus();
278 }
279
280 //-----------------------------------------------------------------------------------------------
281
282 function handle_delete_item($id)
283 {
284         $_SESSION['journal_items']->remove_gl_item($id);
285         line_start_focus();
286 }
287
288 //-----------------------------------------------------------------------------------------------
289
290 function handle_new_item()
291 {
292         if (!check_item_data())
293                 return;
294
295         if (input_num('AmountDebit') > 0)
296                 $amount = input_num('AmountDebit');
297         else
298                 $amount = -input_num('AmountCredit');
299         
300         $_SESSION['journal_items']->add_gl_item($_POST['code_id'], $_POST['dimension_id'],
301                 $_POST['dimension2_id'], $amount, $_POST['LineMemo'], '', get_post('person_id'));
302         line_start_focus();
303 }
304
305 //-----------------------------------------------------------------------------------------------
306 $id = find_submit('Delete');
307 if ($id != -1)
308         handle_delete_item($id);
309
310 if (isset($_POST['AddItem'])) 
311         handle_new_item();
312
313 if (isset($_POST['UpdateItem'])) 
314         handle_update_item();
315         
316 if (isset($_POST['CancelItemChanges']))
317         line_start_focus();
318
319 if (isset($_POST['go']))
320 {
321         display_quick_entries($_SESSION['journal_items'], $_POST['person_id'], input_num('totamount'), QE_JOURNAL);
322         $_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
323         line_start_focus();
324 }       
325 //-----------------------------------------------------------------------------------------------
326
327 start_form();
328
329 display_order_header($_SESSION['journal_items']);
330
331 start_table(TABLESTYLE2, "width=90%", 10);
332 start_row();
333 echo "<td>";
334 display_gl_items(_("Rows"), $_SESSION['journal_items']);
335 gl_options_controls();
336 echo "</td>";
337 end_row();
338 end_table(1);
339
340 submit_center('Process', _("Process Journal Entry"), true , 
341         _('Process journal entry only if debits equal to credits'), 'default');
342
343 end_form();
344 //------------------------------------------------------------------------------------------------
345
346 end_page();
347
348 ?>