Release 2.4.8
[fa-stable.git] / gl / gl_budget.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_BUDGETENTRY';
13 $path_to_root = "..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 add_js_file('budget.js');
17
18 page(_($help_context = "Budget Entry"));
19
20 include_once($path_to_root . "/includes/ui.inc");
21 include_once($path_to_root . "/gl/includes/gl_db.inc");
22 include_once($path_to_root . "/includes/data_checks.inc");
23 include_once($path_to_root . "/admin/db/fiscalyears_db.inc");
24
25
26 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
27
28 //-------------------------------------------------------------------------------------
29
30 if (isset($_POST['add']) || isset($_POST['delete']))
31 {
32         begin_transaction();
33
34         for ($i = 0, $da = $_POST['begin']; date1_greater_date2($_POST['end'], $da); $i++)
35         {
36                 if (isset($_POST['add']))
37                         add_update_gl_budget_trans($da, $_POST['account'], $_POST['dim1'], $_POST['dim2'], input_num('amount'.$i));
38                 else
39                         delete_gl_budget_trans($da, $_POST['account'], $_POST['dim1'], $_POST['dim2']);
40                 $da = add_months($da, 1);
41         }
42         commit_transaction();
43
44         if (isset($_POST['add']))
45                 display_notification_centered(_("The Budget has been saved."));
46         else
47                 display_notification_centered(_("The Budget has been deleted."));
48
49         $Ajax->activate('budget_tbl');
50 }
51 if (isset($_POST['submit']) || isset($_POST['update']))
52         $Ajax->activate('budget_tbl');
53
54 //-------------------------------------------------------------------------------------
55
56 start_form();
57
58 if (db_has_gl_accounts())
59 {
60         $dim = get_company_pref('use_dimension');
61         start_table(TABLESTYLE2);
62         fiscalyears_list_row(_("Fiscal Year:"), 'fyear', null);
63         gl_all_accounts_list_row(_("Account Code:"), 'account', null);
64         if (!isset($_POST['dim1']))
65                 $_POST['dim1'] = 0;
66         if (!isset($_POST['dim2']))
67                 $_POST['dim2'] = 0;
68     if ($dim == 2)
69     {
70                 dimensions_list_row(_("Dimension")." 1", 'dim1', $_POST['dim1'], true, null, false, 1);
71                 dimensions_list_row(_("Dimension")." 2", 'dim2', $_POST['dim2'], true, null, false, 2);
72         }
73         elseif ($dim == 1)
74         {
75                 dimensions_list_row(_("Dimension"), 'dim1', $_POST['dim1'], true, null, false, 1);
76                 hidden('dim2', 0);
77         }
78         else
79         {
80                 hidden('dim1', 0);
81                 hidden('dim2', 0);
82         }
83         submit_row('submit', _("Get"), true, '', '', true);
84         end_table(1);
85         div_start('budget_tbl');
86         start_table(TABLESTYLE2);
87         $showdims = (($dim == 1 && $_POST['dim1'] == 0) ||
88                 ($dim == 2 && $_POST['dim1'] == 0 && $_POST['dim2'] == 0));
89         if ($showdims)
90                 $th = array(_("Period"), _("Amount"), _("Dim. incl."), _("Last Year"));
91         else
92                 $th = array(_("Period"), _("Amount"), _("Last Year"));
93         table_header($th);
94         $year = $_POST['fyear'];
95         if (get_post('update') == '') {
96                 $fyear = get_fiscalyear($year);
97                 $_POST['begin'] = sql2date($fyear['begin']);
98                 $_POST['end'] = sql2date($fyear['end']);
99         }
100         hidden('begin');
101         hidden('end');
102         $total = $btotal = $ltotal = 0;
103         for ($i = 0, $date_ = $_POST['begin']; date1_greater_date2($_POST['end'], $date_); $i++)
104         {
105                 start_row();
106                 if (get_post('update') == '')
107                         $_POST['amount'.$i] = number_format2(get_only_budget_trans_from_to(
108                                 $date_, $date_, $_POST['account'], $_POST['dim1'], $_POST['dim2']), 0);
109
110                 label_cell($date_);
111                 amount_cells(null, 'amount'.$i, null, 15, null, 0);
112                 if ($showdims)
113                 {
114                         $d = get_budget_trans_from_to($date_, $date_, $_POST['account'], $_POST['dim1'], $_POST['dim2']);
115                         label_cell(number_format2($d, 0), "nowrap align=right");
116                         $btotal += $d;
117                 }
118                 $lamount = get_gl_trans_from_to(add_years($date_, -1), add_years(end_month($date_), -1), $_POST['account'], $_POST['dim1'], $_POST['dim2']);
119                 $total += input_num('amount'.$i);
120                 $ltotal += $lamount;
121                 label_cell(number_format2($lamount, 0), "nowrap align=right");
122                 $date_ = add_months($date_, 1);
123                 end_row();
124         }
125         start_row();
126         label_cell("<b>"._("Total")."</b>");
127         label_cell(number_format2($total, 0), 'align=right style="font-weight:bold"', 'Total');
128         if ($showdims)
129                 label_cell("<b>".number_format2($btotal, 0)."</b>", "nowrap align=right");
130         label_cell("<b>".number_format2($ltotal, 0)."</b>", "nowrap align=right");
131         end_row();
132         end_table(1);
133         div_end();
134         submit_center_first('update', _("Update"), '', null);
135         submit('add', _("Save"), true, '', 'default');
136         submit_center_last('delete', _("Delete"), '', true);
137 }
138 end_form();
139
140 end_page();
141