Budget Entry: ajax page reload on params change, added atomic functions for budget...
[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
33         $amounts = array();
34         for ($i = 0, $da = $_POST['begin']; date1_greater_date2($_POST['end'], $da); $i++) {
35                 $amounts[$da] = input_num('amount'.$i);
36                 $da = add_months($da, 1);
37         }
38         if (isset($_POST['add'])) {
39
40                 add_budget($_POST['account'], $_POST['dim1'], $_POST['dim2'], $amounts);
41                 display_notification_centered(_("The Budget has been saved."));
42
43         } else {
44
45                 delete_budget($_POST['account'], $_POST['dim1'], $_POST['dim2'], array_keys($amounts));
46                 display_notification_centered(_("The Budget has been deleted."));
47
48         }
49
50         $Ajax->activate('budget_tbl');
51 }
52 if (isset($_POST['submit']) || isset($_POST['update']) || list_updated('fyear') || list_updated('account'))
53         $Ajax->activate('budget_tbl');
54
55 //-------------------------------------------------------------------------------------
56
57 start_form();
58
59 if (db_has_gl_accounts())
60 {
61         $dim = get_company_pref('use_dimension');
62         start_table(TABLESTYLE2);
63         fiscalyears_list_row(_("Fiscal Year:"), 'fyear', null, true);
64         gl_all_accounts_list_row(_("Account Code:"), 'account', null, true, false, false, true);
65         if (!isset($_POST['dim1']))
66                 $_POST['dim1'] = 0;
67         if (!isset($_POST['dim2']))
68                 $_POST['dim2'] = 0;
69     if ($dim == 2)
70     {
71                 dimensions_list_row(_("Dimension")." 1", 'dim1', $_POST['dim1'], true, null, false, 1, true);
72                 dimensions_list_row(_("Dimension")." 2", 'dim2', $_POST['dim2'], true, null, false, 2, true);
73         }
74         elseif ($dim == 1)
75         {
76                 dimensions_list_row(_("Dimension"), 'dim1', $_POST['dim1'], true, null, false, 1, true);
77                 hidden('dim2', 0);
78         }
79         else
80         {
81                 hidden('dim1', 0);
82                 hidden('dim2', 0);
83         }
84         submit_row('submit', _("Get"), true, '', '', true);
85         end_table(1);
86         div_start('budget_tbl');
87         start_table(TABLESTYLE2);
88         $showdims = (($dim == 1 && $_POST['dim1'] == 0) ||
89                 ($dim == 2 && $_POST['dim1'] == 0 && $_POST['dim2'] == 0));
90         if ($showdims)
91                 $th = array(_("Period"), _("Amount"), _("Dim. incl."), _("Last Year"));
92         else
93                 $th = array(_("Period"), _("Amount"), _("Last Year"));
94         table_header($th);
95         $year = $_POST['fyear'];
96         if (get_post('update') == '') {
97                 $fyear = get_fiscalyear($year);
98                 $_POST['begin'] = sql2date($fyear['begin']);
99                 $_POST['end'] = sql2date($fyear['end']);
100         }
101         hidden('begin');
102         hidden('end');
103         $total = $btotal = $ltotal = 0;
104         for ($i = 0, $date_ = $_POST['begin']; date1_greater_date2($_POST['end'], $date_); $i++)
105         {
106                 start_row();
107                 if (get_post('update') == '')
108                         $_POST['amount'.$i] = number_format2(get_only_budget_trans_from_to(
109                                 $date_, $date_, $_POST['account'], $_POST['dim1'], $_POST['dim2']), 0);
110
111                 label_cell($date_);
112                 amount_cells(null, 'amount'.$i, null, 15, null, 0);
113                 if ($showdims)
114                 {
115                         $d = get_budget_trans_from_to($date_, $date_, $_POST['account'], $_POST['dim1'], $_POST['dim2']);
116                         label_cell(number_format2($d, 0), "nowrap align=right");
117                         $btotal += $d;
118                 }
119                 $lamount = get_gl_trans_from_to(add_years($date_, -1), add_years(end_month($date_), -1), $_POST['account'], $_POST['dim1'], $_POST['dim2']);
120                 $total += input_num('amount'.$i);
121                 $ltotal += $lamount;
122                 label_cell(number_format2($lamount, 0), "nowrap align=right");
123                 $date_ = add_months($date_, 1);
124                 end_row();
125         }
126         start_row();
127         label_cell("<b>"._("Total")."</b>");
128         label_cell(number_format2($total, 0), 'align=right style="font-weight:bold"', 'Total');
129         if ($showdims)
130                 label_cell("<b>".number_format2($btotal, 0)."</b>", "nowrap align=right");
131         label_cell("<b>".number_format2($ltotal, 0)."</b>", "nowrap align=right");
132         end_row();
133         end_table(1);
134         div_end();
135         submit_center_first('update', _("Update"), '', null);
136         submit('add', _("Save"), true, '', 'default');
137         submit_center_last('delete', _("Delete"), '', true);
138 }
139 end_form();
140
141 end_page();
142