5 include($path_to_root . "/includes/session.inc");
7 page(_("Budget Entry"));
9 include($path_to_root . "/includes/ui.inc");
10 include($path_to_root . "/gl/includes/gl_db.inc");
11 include_once($path_to_root . "/includes/data_checks.inc");
13 check_db_has_gl_account_groups(_("There are no account groups defined. Please define at least one account group before entering accounts."));
15 //-------------------------------------------------------------------------------------
17 function exists_gl_budget($date_, $account, $dimension, $dimension2)
19 $sql = "SELECT account FROM ".TB_PREF."budget_trans WHERE account='$account' AND tran_date='$date_' AND
20 dimension_id=$dimension AND dimension2_id=$dimension2";
21 $result = db_query($sql, "Cannot retreive a gl transaction");
23 return (db_num_rows($result) > 0);
26 function add_update_gl_budget_trans($date_, $account, $dimension, $dimension2, $amount)
28 $date = date2sql($date_);
30 if (exists_gl_budget($date, $account, $dimension, $dimension2))
31 $sql = "UPDATE ".TB_PREF."budget_trans SET amount=$amount WHERE account='$account' AND
32 dimension_id=$dimension AND dimension2_id=$dimension2 AND tran_date='$date'";
34 $sql = "INSERT INTO ".TB_PREF."budget_trans (tran_date,
35 account, dimension_id, dimension2_id, amount) VALUES ('$date',
36 '$account', $dimension, $dimension2, $amount)";
38 db_query($sql, "The GL budget transaction could not be saved");
41 function delete_gl_budget_trans($date_, $account, $dimension, $dimension2)
43 $date = date2sql($date_);
45 $sql = "DELETE FROM ".TB_PREF."budget_trans WHERE account='$account' AND
46 dimension_id=$dimension AND dimension2_id=$dimension2 AND tran_date='$date'";
47 db_query($sql, "The GL budget transaction could not be deleted");
50 function get_only_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
53 $from = date2sql($from_date);
54 $to = date2sql($to_date);
56 $sql = "SELECT SUM(amount) FROM ".TB_PREF."budget_trans
57 WHERE account='$account' AND tran_date >= '$from' AND tran_date <= '$to'
58 AND dimension_id = $dimension AND dimension2_id = $dimension2";
59 $result = db_query($sql,"No budget accounts were returned");
61 $row = db_fetch_row($result);
65 //-------------------------------------------------------------------------------------
67 if (isset($_POST['add']) || isset($_POST['delete']))
71 for ($i = 0, $da = $_POST['begin']; date1_greater_date2($_POST['end'], $da); $i++)
73 if (isset($_POST['add']))
74 add_update_gl_budget_trans($da, $_POST['account'], $_POST['dim1'], $_POST['dim2'], $_POST['amount'.$i]);
76 delete_gl_budget_trans($da, $_POST['account'], $_POST['dim1'], $_POST['dim2']);
77 $da = add_months($da, 1);
81 if (isset($_POST['add']))
82 display_notification_centered(_("The Budget has been saved."));
84 display_notification_centered(_("The Budget has been deleted."));
86 //meta_forward($_SERVER['PHP_SELF']);
89 //-------------------------------------------------------------------------------------
93 if (db_has_gl_accounts())
95 $dim = get_company_pref('use_dimension');
96 start_table($table_style2);
97 fiscalyears_list_row(_("Fiscal Year:"), 'fyear', null);
98 gl_all_accounts_list_row(_("Account Code:"), 'account', null);
99 if (!isset($_POST['dim1']))
101 if (!isset($_POST['dim2']))
105 dimensions_list_row(_("Dimension")." 1", 'dim1', $_POST['dim1'], true, null, false, 1);
106 dimensions_list_row(_("Dimension")." 2", 'dim2', $_POST['dim2'], true, null, false, 2);
110 dimensions_list_row(_("Dimension"), 'dim1', $_POST['dim1'], true, null, false, 1);
118 submit_row('submit', _("Get"));
120 start_table($table_style2);
121 $showdims = (($dim == 1 && $_POST['dim1'] == 0) ||
122 ($dim == 2 && $_POST['dim1'] == 0 && $_POST['dim2'] == 0));
124 $th = array(_("Period"), _("Amount"), _("Dim. incl."), _("Last Year"));
126 $th = array(_("Period"), _("Amount"), _("Last Year"));
128 $year = $_POST['fyear'];
129 $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE id=$year";
131 $result = db_query($sql, "could not get current fiscal year");
133 $fyear = db_fetch($result);
134 $begin = sql2date($fyear['begin']);
135 $end = sql2date($fyear['end']);
136 hidden('begin', $begin);
138 $total = $btotal = $ltotal = 0;
139 for ($i = 0, $date_ = $begin; date1_greater_date2($end, $date_); $i++)
142 $_POST['amount'.$i] = get_only_budget_trans_from_to($date_, $date_, $_POST['account'], $_POST['dim1'], $_POST['dim2']);
145 if (!isset($_POST['amount'.$i]))
146 $_POST['amount'.$i] = "0";
147 text_cells(null, 'amount'.$i, null, 15);
150 $d = get_budget_trans_from_to($date_, $date_, $_POST['account'], $_POST['dim1'], $_POST['dim2']);
151 label_cell(number_format2($d, 0), "nowrap align=right");
154 $lamount = get_gl_trans_from_to(add_years($date_, -1), add_years(end_month($date_), -1), $_POST['account'], $_POST['dim1'], $_POST['dim2']);
155 $total += $_POST['amount'.$i];
157 label_cell(number_format2($lamount, 0), "nowrap align=right");
158 $date_ = add_months($date_, 1);
162 label_cell("<b>"._("Total")."</b>");
163 label_cell("<b>".number_format2($total, 0)."</b>");
165 label_cell("<b>".number_format2($btotal, 0)."</b>", "nowrap align=right");
166 label_cell("<b>".number_format2($ltotal, 0)."</b>", "nowrap align=right");
169 submit_center_first('add', _("Save"));
170 submit_center_last('delete', _("Delete"));