5 include($path_to_root . "/includes/session.inc");
7 add_js_file('budget.js');
9 page(_("Budget Entry"));
11 include($path_to_root . "/includes/ui.inc");
12 include($path_to_root . "/gl/includes/gl_db.inc");
13 include_once($path_to_root . "/includes/data_checks.inc");
15 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
17 //-------------------------------------------------------------------------------------
19 function exists_gl_budget($date_, $account, $dimension, $dimension2)
21 $sql = "SELECT account FROM ".TB_PREF."budget_trans WHERE account='$account' AND tran_date='$date_' AND
22 dimension_id=$dimension AND dimension2_id=$dimension2";
23 $result = db_query($sql, "Cannot retreive a gl transaction");
25 return (db_num_rows($result) > 0);
28 function add_update_gl_budget_trans($date_, $account, $dimension, $dimension2, $amount)
30 $date = date2sql($date_);
32 if (exists_gl_budget($date, $account, $dimension, $dimension2))
33 $sql = "UPDATE ".TB_PREF."budget_trans SET amount=$amount WHERE account='$account' AND
34 dimension_id=$dimension AND dimension2_id=$dimension2 AND tran_date='$date'";
36 $sql = "INSERT INTO ".TB_PREF."budget_trans (tran_date,
37 account, dimension_id, dimension2_id, amount, memo_) VALUES ('$date',
38 '$account', $dimension, $dimension2, $amount, '')";
40 db_query($sql, "The GL budget transaction could not be saved");
43 function delete_gl_budget_trans($date_, $account, $dimension, $dimension2)
45 $date = date2sql($date_);
47 $sql = "DELETE FROM ".TB_PREF."budget_trans WHERE account='$account' AND
48 dimension_id=$dimension AND dimension2_id=$dimension2 AND tran_date='$date'";
49 db_query($sql, "The GL budget transaction could not be deleted");
52 function get_only_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
55 $from = date2sql($from_date);
56 $to = date2sql($to_date);
58 $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
59 WHERE account='$account' AND tran_date >= '$from' AND tran_date <= '$to'
60 AND dimension_id = $dimension AND dimension2_id = $dimension2";
61 $result = db_query($sql,"No budget accounts were returned");
63 $row = db_fetch_row($result);
67 //-------------------------------------------------------------------------------------
69 if (isset($_POST['add']) || isset($_POST['delete']))
73 for ($i = 0, $da = $_POST['begin']; date1_greater_date2($_POST['end'], $da); $i++)
75 if (isset($_POST['add']))
76 add_update_gl_budget_trans($da, $_POST['account'], $_POST['dim1'], $_POST['dim2'], input_num('amount'.$i));
78 delete_gl_budget_trans($da, $_POST['account'], $_POST['dim1'], $_POST['dim2']);
79 $da = add_months($da, 1);
83 if (isset($_POST['add']))
84 display_notification_centered(_("The Budget has been saved."));
86 display_notification_centered(_("The Budget has been deleted."));
88 //meta_forward($_SERVER['PHP_SELF']);
89 $Ajax->activate('budget_tbl');
91 if (isset($_POST['submit']) || isset($_POST['update']))
92 $Ajax->activate('budget_tbl');
94 //-------------------------------------------------------------------------------------
98 if (db_has_gl_accounts())
100 $dim = get_company_pref('use_dimension');
101 start_table($table_style2);
102 fiscalyears_list_row(_("Fiscal Year:"), 'fyear', null);
103 gl_all_accounts_list_row(_("Account Code:"), 'account', null);
104 if (!isset($_POST['dim1']))
106 if (!isset($_POST['dim2']))
110 dimensions_list_row(_("Dimension")." 1", 'dim1', $_POST['dim1'], true, null, false, 1);
111 dimensions_list_row(_("Dimension")." 2", 'dim2', $_POST['dim2'], true, null, false, 2);
115 dimensions_list_row(_("Dimension"), 'dim1', $_POST['dim1'], true, null, false, 1);
123 submit_row('submit', _("Get"), true, '', '', true);
125 div_start('budget_tbl');
126 start_table($table_style2);
127 $showdims = (($dim == 1 && $_POST['dim1'] == 0) ||
128 ($dim == 2 && $_POST['dim1'] == 0 && $_POST['dim2'] == 0));
130 $th = array(_("Period"), _("Amount"), _("Dim. incl."), _("Last Year"));
132 $th = array(_("Period"), _("Amount"), _("Last Year"));
134 $year = $_POST['fyear'];
135 if (get_post('update') == '') {
136 $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE id=$year";
138 $result = db_query($sql, "could not get current fiscal year");
140 $fyear = db_fetch($result);
141 $_POST['begin'] = sql2date($fyear['begin']);
142 $_POST['end'] = sql2date($fyear['end']);
146 $total = $btotal = $ltotal = 0;
147 for ($i = 0, $date_ = $_POST['begin']; date1_greater_date2($_POST['end'], $date_); $i++)
150 if (get_post('update') == '')
151 $_POST['amount'.$i] = number_format2(get_only_budget_trans_from_to(
152 $date_, $date_, $_POST['account'], $_POST['dim1'], $_POST['dim2']), 0);
155 amount_cells(null, 'amount'.$i, null, 15, null, 0);
158 $d = get_budget_trans_from_to($date_, $date_, $_POST['account'], $_POST['dim1'], $_POST['dim2']);
159 label_cell(number_format2($d, 0), "nowrap align=right");
162 $lamount = get_gl_trans_from_to(add_years($date_, -1), add_years(end_month($date_), -1), $_POST['account'], $_POST['dim1'], $_POST['dim2']);
163 $total += input_num('amount'.$i);
165 label_cell(number_format2($lamount, 0), "nowrap align=right");
166 $date_ = add_months($date_, 1);
170 label_cell("<b>"._("Total")."</b>");
171 label_cell(number_format2($total, 0), 'align=right style="font-weight:bold"', 'Total');
173 label_cell("<b>".number_format2($btotal, 0)."</b>", "nowrap align=right");
174 label_cell("<b>".number_format2($ltotal, 0)."</b>", "nowrap align=right");
178 submit_center_first('update', _("Update"), '', null);
179 submit('add', _("Save"), true, '', true);
180 submit_center_last('delete', _("Delete"), '', true);