Merging version 2.1 RC to main trunk.
[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 = 10;
13 $path_to_root="..";
14 include($path_to_root . "/includes/session.inc");
15
16 add_js_file('budget.js');
17
18 page(_("Budget Entry"));
19
20 include($path_to_root . "/includes/ui.inc");
21 include($path_to_root . "/gl/includes/gl_db.inc");
22 include_once($path_to_root . "/includes/data_checks.inc");
23
24 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
25
26 //-------------------------------------------------------------------------------------
27
28 function exists_gl_budget($date_, $account, $dimension, $dimension2)
29 {
30         $sql = "SELECT account FROM ".TB_PREF."budget_trans WHERE account='$account' AND tran_date='$date_' AND
31                 dimension_id=$dimension AND dimension2_id=$dimension2";
32         $result = db_query($sql, "Cannot retreive a gl transaction");
33
34     return (db_num_rows($result) > 0);
35 }
36
37 function add_update_gl_budget_trans($date_, $account, $dimension, $dimension2, $amount)
38 {
39         $date = date2sql($date_);
40
41         if (exists_gl_budget($date, $account, $dimension, $dimension2))
42                 $sql = "UPDATE ".TB_PREF."budget_trans SET amount=$amount WHERE account='$account' AND
43                         dimension_id=$dimension AND dimension2_id=$dimension2 AND tran_date='$date'";
44         else
45                 $sql = "INSERT INTO ".TB_PREF."budget_trans (tran_date,
46                         account, dimension_id, dimension2_id, amount, memo_) VALUES ('$date',
47                         '$account', $dimension, $dimension2, $amount, '')";
48
49         db_query($sql, "The GL budget transaction could not be saved");
50 }
51
52 function delete_gl_budget_trans($date_, $account, $dimension, $dimension2)
53 {
54         $date = date2sql($date_);
55
56         $sql = "DELETE FROM ".TB_PREF."budget_trans WHERE account='$account' AND
57                         dimension_id=$dimension AND dimension2_id=$dimension2 AND tran_date='$date'";
58         db_query($sql, "The GL budget transaction could not be deleted");
59 }
60
61 function get_only_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
62 {
63
64         $from = date2sql($from_date);
65         $to = date2sql($to_date);
66
67         $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
68                 WHERE account='$account' AND tran_date >= '$from' AND tran_date <= '$to'
69                  AND dimension_id = $dimension AND dimension2_id = $dimension2";
70         $result = db_query($sql,"No budget accounts were returned");
71
72         $row = db_fetch_row($result);
73         return $row[0];
74 }
75
76 //-------------------------------------------------------------------------------------
77
78 if (isset($_POST['add']) || isset($_POST['delete']))
79 {
80         begin_transaction();
81
82         for ($i = 0, $da = $_POST['begin']; date1_greater_date2($_POST['end'], $da); $i++)
83         {
84                 if (isset($_POST['add']))
85                         add_update_gl_budget_trans($da, $_POST['account'], $_POST['dim1'], $_POST['dim2'], input_num('amount'.$i));
86                 else
87                         delete_gl_budget_trans($da, $_POST['account'], $_POST['dim1'], $_POST['dim2']);
88                 $da = add_months($da, 1);
89         }
90         commit_transaction();
91
92         if (isset($_POST['add']))
93                 display_notification_centered(_("The Budget has been saved."));
94         else
95                 display_notification_centered(_("The Budget has been deleted."));
96
97         //meta_forward($_SERVER['PHP_SELF']);
98         $Ajax->activate('budget_tbl');
99 }
100 if (isset($_POST['submit']) || isset($_POST['update']))
101         $Ajax->activate('budget_tbl');
102
103 //-------------------------------------------------------------------------------------
104
105 start_form();
106
107 if (db_has_gl_accounts())
108 {
109         $dim = get_company_pref('use_dimension');
110         start_table($table_style2);
111         fiscalyears_list_row(_("Fiscal Year:"), 'fyear', null);
112         gl_all_accounts_list_row(_("Account Code:"), 'account', null);
113         if (!isset($_POST['dim1']))
114                 $_POST['dim1'] = 0;
115         if (!isset($_POST['dim2']))
116                 $_POST['dim2'] = 0;
117     if ($dim == 2)
118     {
119                 dimensions_list_row(_("Dimension")." 1", 'dim1', $_POST['dim1'], true, null, false, 1);
120                 dimensions_list_row(_("Dimension")." 2", 'dim2', $_POST['dim2'], true, null, false, 2);
121         }
122         else if ($dim == 1)
123         {
124                 dimensions_list_row(_("Dimension"), 'dim1', $_POST['dim1'], true, null, false, 1);
125                 hidden('dim2', 0);
126         }
127         else
128         {
129                 hidden('dim1', 0);
130                 hidden('dim2', 0);
131         }
132         submit_row('submit', _("Get"), true, '', '', true);
133         end_table(1);
134         div_start('budget_tbl');
135         start_table($table_style2);
136         $showdims = (($dim == 1 && $_POST['dim1'] == 0) ||
137                 ($dim == 2 && $_POST['dim1'] == 0 && $_POST['dim2'] == 0));
138         if ($showdims)
139                 $th = array(_("Period"), _("Amount"), _("Dim. incl."), _("Last Year"));
140         else
141                 $th = array(_("Period"), _("Amount"), _("Last Year"));
142         table_header($th);
143         $year = $_POST['fyear'];
144         if (get_post('update') == '') {
145                 $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE id=$year";
146
147                 $result = db_query($sql, "could not get current fiscal year");
148
149                 $fyear = db_fetch($result);
150                 $_POST['begin'] = sql2date($fyear['begin']);
151                 $_POST['end'] = sql2date($fyear['end']);
152         }
153         hidden('begin');
154         hidden('end');
155         $total = $btotal = $ltotal = 0;
156         for ($i = 0, $date_ = $_POST['begin']; date1_greater_date2($_POST['end'], $date_); $i++)
157         {
158                 start_row();
159                 if (get_post('update') == '')
160                         $_POST['amount'.$i] = number_format2(get_only_budget_trans_from_to(
161                                 $date_, $date_, $_POST['account'], $_POST['dim1'], $_POST['dim2']), 0);
162
163                 label_cell($date_);
164                 amount_cells(null, 'amount'.$i, null, 15, null, 0);
165                 if ($showdims)
166                 {
167                         $d = get_budget_trans_from_to($date_, $date_, $_POST['account'], $_POST['dim1'], $_POST['dim2']);
168                         label_cell(number_format2($d, 0), "nowrap align=right");
169                         $btotal += $d;
170                 }
171                 $lamount = get_gl_trans_from_to(add_years($date_, -1), add_years(end_month($date_), -1), $_POST['account'], $_POST['dim1'], $_POST['dim2']);
172                 $total += input_num('amount'.$i);
173                 $ltotal += $lamount;
174                 label_cell(number_format2($lamount, 0), "nowrap align=right");
175                 $date_ = add_months($date_, 1);
176                 end_row();
177         }
178         start_row();
179         label_cell("<b>"._("Total")."</b>");
180         label_cell(number_format2($total, 0), 'align=right style="font-weight:bold"', 'Total');
181         if ($showdims)
182                 label_cell("<b>".number_format2($btotal, 0)."</b>", "nowrap align=right");
183         label_cell("<b>".number_format2($ltotal, 0)."</b>", "nowrap align=right");
184         end_row();
185         end_table(1);
186         div_end();
187         submit_center_first('update', _("Update"), '', null);
188         submit('add', _("Save"), true, '', true);
189         submit_center_last('delete', _("Delete"), '', true);
190 }
191 end_form();
192
193 end_page();
194
195 ?>