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_GLANALYTIC';
13 // ----------------------------------------------------------------
15 // Creator: Joe Hunt, Chaitanya for the recursive version 2009-02-05.
17 // Title: Annual expense breakdown
18 // ----------------------------------------------------------------
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/includes/data_checks.inc");
24 include_once($path_to_root . "/gl/includes/gl_db.inc");
25 include_once($path_to_root . "/admin/db/tags_db.inc");
27 //----------------------------------------------------------------------------------------------------
29 print_annual_expense_breakdown();
31 //----------------------------------------------------------------------------------------------------
33 function getPeriods($yr, $mo, $account, $dimension, $dimension2)
35 $date13 = date('Y-m-d',mktime(0,0,0,$mo+1,1,$yr));
36 $date12 = date('Y-m-d',mktime(0,0,0,$mo,1,$yr));
37 $date11 = date('Y-m-d',mktime(0,0,0,$mo-1,1,$yr));
38 $date10 = date('Y-m-d',mktime(0,0,0,$mo-2,1,$yr));
39 $date09 = date('Y-m-d',mktime(0,0,0,$mo-3,1,$yr));
40 $date08 = date('Y-m-d',mktime(0,0,0,$mo-4,1,$yr));
41 $date07 = date('Y-m-d',mktime(0,0,0,$mo-5,1,$yr));
42 $date06 = date('Y-m-d',mktime(0,0,0,$mo-6,1,$yr));
43 $date05 = date('Y-m-d',mktime(0,0,0,$mo-7,1,$yr));
44 $date04 = date('Y-m-d',mktime(0,0,0,$mo-8,1,$yr));
45 $date03 = date('Y-m-d',mktime(0,0,0,$mo-9,1,$yr));
46 $date02 = date('Y-m-d',mktime(0,0,0,$mo-10,1,$yr));
47 $date01 = date('Y-m-d',mktime(0,0,0,$mo-11,1,$yr));
49 $sql = "SELECT SUM(CASE WHEN tran_date >= '$date01' AND tran_date < '$date02' THEN amount / 1000 ELSE 0 END) AS per01,
50 SUM(CASE WHEN tran_date >= '$date02' AND tran_date < '$date03' THEN amount / 1000 ELSE 0 END) AS per02,
51 SUM(CASE WHEN tran_date >= '$date03' AND tran_date < '$date04' THEN amount / 1000 ELSE 0 END) AS per03,
52 SUM(CASE WHEN tran_date >= '$date04' AND tran_date < '$date05' THEN amount / 1000 ELSE 0 END) AS per04,
53 SUM(CASE WHEN tran_date >= '$date05' AND tran_date < '$date06' THEN amount / 1000 ELSE 0 END) AS per05,
54 SUM(CASE WHEN tran_date >= '$date06' AND tran_date < '$date07' THEN amount / 1000 ELSE 0 END) AS per06,
55 SUM(CASE WHEN tran_date >= '$date07' AND tran_date < '$date08' THEN amount / 1000 ELSE 0 END) AS per07,
56 SUM(CASE WHEN tran_date >= '$date08' AND tran_date < '$date09' THEN amount / 1000 ELSE 0 END) AS per08,
57 SUM(CASE WHEN tran_date >= '$date09' AND tran_date < '$date10' THEN amount / 1000 ELSE 0 END) AS per09,
58 SUM(CASE WHEN tran_date >= '$date10' AND tran_date < '$date11' THEN amount / 1000 ELSE 0 END) AS per10,
59 SUM(CASE WHEN tran_date >= '$date11' AND tran_date < '$date12' THEN amount / 1000 ELSE 0 END) AS per11,
60 SUM(CASE WHEN tran_date >= '$date12' AND tran_date < '$date13' THEN amount / 1000 ELSE 0 END) AS per12
61 FROM ".TB_PREF."gl_trans
62 WHERE account='$account'";
64 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
66 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
68 $result = db_query($sql, "Transactions for account $account could not be calculated");
70 return db_fetch($result);
73 //----------------------------------------------------------------------------------------------------
75 function display_type ($type, $typename, $yr, $mo, $convert, &$dec, &$rep, $dimension, $dimension2, $tags)
77 $ctotal = array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0);
78 $total = array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0);
79 $totals_arr = array();
81 $printtitle = 0; //Flag for printing type name
83 //Get Accounts directly under this group/type
84 $result = get_gl_accounts(null, null, $type);
85 while ($account=db_fetch($result))
87 if ($tags != -1 && is_array($tags) && $tags[0] != false)
89 if (!is_record_in_tags($tags, TAG_ACCOUNT, $account['account_code']))
92 $bal = getPeriods($yr, $mo, $account["account_code"], $dimension, $dimension2);
93 if (!$bal['per01'] && !$bal['per02'] && !$bal['per03'] && !$bal['per04'] && !$bal['per05'] &&
94 !$bal['per06'] && !$bal['per07'] && !$bal['per08'] && !$bal['per09'] && !$bal['per10'] &&
95 !$bal['per11'] && !$bal['per12'])
98 //Print Type Title if it has atleast one non-zero account
103 $rep->TextCol(0, 5, $typename);
105 $rep->Line($rep->row);
109 $balance = array(1 => $bal['per01'], $bal['per02'], $bal['per03'], $bal['per04'],
110 $bal['per05'], $bal['per06'], $bal['per07'], $bal['per08'],
111 $bal['per09'], $bal['per10'], $bal['per11'], $bal['per12']);
112 $rep->TextCol(0, 1, $account['account_code']);
113 $rep->TextCol(1, 2, $account['account_name']);
115 for ($i = 1; $i <= 12; $i++)
117 $rep->AmountCol($i + 1, $i + 2, $balance[$i] * $convert, $dec);
118 $ctotal[$i] += $balance[$i];
124 //Get Account groups/types under this group/type
125 $result = get_account_types(false, false, $type);
126 while ($accounttype=db_fetch($result))
128 //Print Type Title if has sub types and not previously printed
133 $rep->TextCol(0, 5, $typename);
135 $rep->Line($rep->row);
139 $totals_arr = display_type($accounttype["id"], $accounttype["name"], $yr, $mo, $convert, $dec, $rep, $dimension, $dimension2, $tags);
140 for ($i = 1; $i <= 12; $i++)
142 $total[$i] += $totals_arr[$i];
146 //Display Type Summary if total is != 0 OR head is printed (Needed in case of unused hierarchical COA)
150 $rep->Line($rep->row);
152 $rep->TextCol(0, 2, _('Total') . " " . $typename);
153 for ($i = 1; $i <= 12; $i++)
154 $rep->AmountCol($i + 1, $i + 2, ($total[$i] + $ctotal[$i]) * $convert, $dec);
157 for ($i = 1; $i <= 12; $i++)
158 $totals_arr[$i] = $total[$i] + $ctotal[$i];
162 //----------------------------------------------------------------------------------------------------
164 function print_annual_expense_breakdown()
166 global $path_to_root, $SysPrefs;
168 $dim = get_company_pref('use_dimension');
169 $dimension = $dimension2 = 0;
173 $year = $_POST['PARAM_0'];
174 $dimension = $_POST['PARAM_1'];
175 $dimension2 = $_POST['PARAM_2'];
176 $tags = (isset($_POST['PARAM_3']) ? $_POST['PARAM_3'] : -1);
177 $comments = $_POST['PARAM_4'];
178 $orientation = $_POST['PARAM_5'];
179 $destination = $_POST['PARAM_6'];
183 $year = $_POST['PARAM_0'];
184 $dimension = $_POST['PARAM_1'];
185 $tags = (isset($_POST['PARAM_2']) ? $_POST['PARAM_2'] : -1);
186 $comments = $_POST['PARAM_3'];
187 $orientation = $_POST['PARAM_4'];
188 $destination = $_POST['PARAM_5'];
192 $year = $_POST['PARAM_0'];
193 $tags = (isset($_POST['PARAM_1']) ? $_POST['PARAM_1'] : -1);
194 $comments = $_POST['PARAM_2'];
195 $orientation = $_POST['PARAM_3'];
196 $destination = $_POST['PARAM_4'];
199 include_once($path_to_root . "/reporting/includes/excel_report.inc");
201 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
203 $orientation = ($orientation ? 'L' : 'P');
206 $cols = array(0, 40, 150, 180, 210, 240, 270, 300, 330, 360, 390, 420, 450, 480, 510);
207 //------------0--1---2----3----4----5----6----7----8----10---11---12---13---14---15-
210 $sql = "SELECT begin, end, YEAR(end) AS yr, MONTH(end) AS mo FROM ".TB_PREF."fiscal_year WHERE id=".db_escape($year);
211 $result = db_query($sql, "could not get fiscal year");
212 $row = db_fetch($result);
214 $year = sql2date($row['begin'])." - ".sql2date($row['end']);
218 if ($SysPrefs->date_system == 1)
219 list($yr, $mo, $da) = jalali_to_gregorian($yr, $mo, $da);
220 elseif ($SysPrefs->date_system == 2)
221 list($yr, $mo, $da) = islamic_to_gregorian($yr, $mo, $da);
222 $per12 = strftime('%b',mktime(0,0,0,$mo,$da,$yr));
223 $per11 = strftime('%b',mktime(0,0,0,$mo-1,$da,$yr));
224 $per10 = strftime('%b',mktime(0,0,0,$mo-2,$da,$yr));
225 $per09 = strftime('%b',mktime(0,0,0,$mo-3,$da,$yr));
226 $per08 = strftime('%b',mktime(0,0,0,$mo-4,$da,$yr));
227 $per07 = strftime('%b',mktime(0,0,0,$mo-5,$da,$yr));
228 $per06 = strftime('%b',mktime(0,0,0,$mo-6,$da,$yr));
229 $per05 = strftime('%b',mktime(0,0,0,$mo-7,$da,$yr));
230 $per04 = strftime('%b',mktime(0,0,0,$mo-8,$da,$yr));
231 $per03 = strftime('%b',mktime(0,0,0,$mo-9,$da,$yr));
232 $per02 = strftime('%b',mktime(0,0,0,$mo-10,$da,$yr));
233 $per01 = strftime('%b',mktime(0,0,0,$mo-11,$da,$yr));
235 $headers = array(_('Account'), _('Account Name'), $per01, $per02, $per03, $per04,
236 $per05, $per06, $per07, $per08, $per09, $per10, $per11, $per12);
238 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right',
239 'right', 'right', 'right', 'right', 'right', 'right');
243 $params = array( 0 => $comments,
244 1 => array('text' => _("Year"),
245 'from' => $year, 'to' => ''),
246 2 => array('text' => _("Dimension")." 1",
247 'from' => get_dimension_string($dimension), 'to' => ''),
248 3 => array('text' => _("Dimension")." 2",
249 'from' => get_dimension_string($dimension2), 'to' => ''),
250 4 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''),
251 5 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
256 $params = array( 0 => $comments,
257 1 => array('text' => _("Year"),
258 'from' => $year, 'to' => ''),
259 2 => array('text' => _('Dimension'),
260 'from' => get_dimension_string($dimension), 'to' => ''),
261 3 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''),
262 4 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
267 $params = array( 0 => $comments,
268 1 => array('text' => _("Year"),
269 'from' => $year, 'to' => ''),
270 2 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''),
271 3 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
275 $rep = new FrontReport(_('Annual Expense Breakdown'), "AnnualBreakDown", user_pagesize(), 9, $orientation);
276 if ($orientation == 'L')
277 recalculate_cols($cols);
280 $rep->Info($params, $cols, $headers, $aligns);
283 $sales = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0);
285 $classresult = get_account_classes(false, 0);
286 while ($class = db_fetch($classresult))
288 $ctotal = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0);
289 $convert = get_class_type_convert($class["ctype"]);
293 $rep->TextCol(0, 5, $class["class_name"]);
297 //Get Account groups/types under this group/type with no parents
298 $typeresult = get_account_types(false, $class['cid'], -1);
299 while ($accounttype=db_fetch($typeresult))
301 $classtotal = display_type($accounttype["id"], $accounttype["name"], $yr, $mo, $convert, $dec, $rep, $dimension,
303 for ($i = 1; $i <= 12; $i++)
304 $ctotal[$i] += $classtotal[$i];
307 //Print Class Summary
309 $rep->Line($rep->row);
312 $rep->TextCol(0, 2, _('Total') . " " . $class["class_name"]);
313 for ($i = 1; $i <= 12; $i++)
315 $rep->AmountCol($i + 1, $i + 2, $ctotal[$i] * $convert, $dec);
316 $sales[$i] += $ctotal[$i];
322 $rep->TextCol(0, 2, _("Calculated Return"));
323 for ($i = 1; $i <= 12; $i++)
324 $rep->AmountCol($i + 1, $i + 2, $sales[$i] * -1, $dec);
327 $rep->Line($rep->row);