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 ***********************************************************************/
14 include($path_to_root . "/includes/session.inc");
16 add_js_file('budget.js');
18 page(_("Budget Entry"));
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");
24 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
26 //-------------------------------------------------------------------------------------
28 function exists_gl_budget($date_, $account, $dimension, $dimension2)
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");
34 return (db_num_rows($result) > 0);
37 function add_update_gl_budget_trans($date_, $account, $dimension, $dimension2, $amount)
39 $date = date2sql($date_);
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'";
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, '')";
49 db_query($sql, "The GL budget transaction could not be saved");
52 function delete_gl_budget_trans($date_, $account, $dimension, $dimension2)
54 $date = date2sql($date_);
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");
61 function get_only_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
64 $from = date2sql($from_date);
65 $to = date2sql($to_date);
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");
72 $row = db_fetch_row($result);
76 //-------------------------------------------------------------------------------------
78 if (isset($_POST['add']) || isset($_POST['delete']))
82 for ($i = 0, $da = $_POST['begin']; date1_greater_date2($_POST['end'], $da); $i++)
84 if (isset($_POST['add']))
85 add_update_gl_budget_trans($da, $_POST['account'], $_POST['dim1'], $_POST['dim2'], input_num('amount'.$i));
87 delete_gl_budget_trans($da, $_POST['account'], $_POST['dim1'], $_POST['dim2']);
88 $da = add_months($da, 1);
92 if (isset($_POST['add']))
93 display_notification_centered(_("The Budget has been saved."));
95 display_notification_centered(_("The Budget has been deleted."));
97 //meta_forward($_SERVER['PHP_SELF']);
98 $Ajax->activate('budget_tbl');
100 if (isset($_POST['submit']) || isset($_POST['update']))
101 $Ajax->activate('budget_tbl');
103 //-------------------------------------------------------------------------------------
107 if (db_has_gl_accounts())
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']))
115 if (!isset($_POST['dim2']))
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);
124 dimensions_list_row(_("Dimension"), 'dim1', $_POST['dim1'], true, null, false, 1);
132 submit_row('submit', _("Get"), true, '', '', true);
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));
139 $th = array(_("Period"), _("Amount"), _("Dim. incl."), _("Last Year"));
141 $th = array(_("Period"), _("Amount"), _("Last Year"));
143 $year = $_POST['fyear'];
144 if (get_post('update') == '') {
145 $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE id=$year";
147 $result = db_query($sql, "could not get current fiscal year");
149 $fyear = db_fetch($result);
150 $_POST['begin'] = sql2date($fyear['begin']);
151 $_POST['end'] = sql2date($fyear['end']);
155 $total = $btotal = $ltotal = 0;
156 for ($i = 0, $date_ = $_POST['begin']; date1_greater_date2($_POST['end'], $date_); $i++)
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);
164 amount_cells(null, 'amount'.$i, null, 15, null, 0);
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");
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);
174 label_cell(number_format2($lamount, 0), "nowrap align=right");
175 $date_ = add_months($date_, 1);
179 label_cell("<b>"._("Total")."</b>");
180 label_cell(number_format2($total, 0), 'align=right style="font-weight:bold"', 'Total');
182 label_cell("<b>".number_format2($btotal, 0)."</b>", "nowrap align=right");
183 label_cell("<b>".number_format2($ltotal, 0)."</b>", "nowrap align=right");
187 submit_center_first('update', _("Update"), '', null);
188 submit('add', _("Save"), true, '', 'default');
189 submit_center_last('delete', _("Delete"), '', true);