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, $thousands)
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 / $thousands ELSE 0 END) AS per01,
50 SUM(CASE WHEN tran_date >= '$date02' AND tran_date < '$date03' THEN amount / $thousands ELSE 0 END) AS per02,
51 SUM(CASE WHEN tran_date >= '$date03' AND tran_date < '$date04' THEN amount / $thousands ELSE 0 END) AS per03,
52 SUM(CASE WHEN tran_date >= '$date04' AND tran_date < '$date05' THEN amount / $thousands ELSE 0 END) AS per04,
53 SUM(CASE WHEN tran_date >= '$date05' AND tran_date < '$date06' THEN amount / $thousands ELSE 0 END) AS per05,
54 SUM(CASE WHEN tran_date >= '$date06' AND tran_date < '$date07' THEN amount / $thousands ELSE 0 END) AS per06,
55 SUM(CASE WHEN tran_date >= '$date07' AND tran_date < '$date08' THEN amount / $thousands ELSE 0 END) AS per07,
56 SUM(CASE WHEN tran_date >= '$date08' AND tran_date < '$date09' THEN amount / $thousands ELSE 0 END) AS per08,
57 SUM(CASE WHEN tran_date >= '$date09' AND tran_date < '$date10' THEN amount / $thousands ELSE 0 END) AS per09,
58 SUM(CASE WHEN tran_date >= '$date10' AND tran_date < '$date11' THEN amount / $thousands ELSE 0 END) AS per10,
59 SUM(CASE WHEN tran_date >= '$date11' AND tran_date < '$date12' THEN amount / $thousands ELSE 0 END) AS per11,
60 SUM(CASE WHEN tran_date >= '$date12' AND tran_date < '$date13' THEN amount / $thousands ELSE 0 END) AS per12,
61 SUM(CASE WHEN tran_date >= '$date01' AND tran_date < '$date13' THEN amount / $thousands ELSE 0 END) AS pertotal
62 FROM ".TB_PREF."gl_trans
63 WHERE account='$account'";
65 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
67 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
69 $result = db_query($sql, "Transactions for account $account could not be calculated");
71 return db_fetch($result);
74 //----------------------------------------------------------------------------------------------------
76 function display_type ($type, $typename, $yr, $mo, $convert, &$dec, &$rep, $dimension, $dimension2,
79 $ctotal = array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
80 $total = array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
81 $totals_arr = array();
83 $printtitle = 0; //Flag for printing type name
85 //Get Accounts directly under this group/type
86 $result = get_gl_accounts(null, null, $type);
87 while ($account=db_fetch($result))
89 if ($tags != -1 && is_array($tags) && $tags[0] != false)
91 if (!is_record_in_tags($tags, TAG_ACCOUNT, $account['account_code']))
94 $bal = getPeriods($yr, $mo, $account["account_code"], $dimension, $dimension2, $thousands);
95 if (!$bal['per01'] && !$bal['per02'] && !$bal['per03'] && !$bal['per04'] && !$bal['per05'] &&
96 !$bal['per06'] && !$bal['per07'] && !$bal['per08'] && !$bal['per09'] && !$bal['per10'] &&
97 !$bal['per11'] && !$bal['per12'])
100 //Print Type Title if it has at least one non-zero account
105 $rep->TextCol(0, 5, $typename);
107 $rep->Line($rep->row);
111 $balance = array(1 => $bal['per01'], $bal['per02'], $bal['per03'], $bal['per04'],
112 $bal['per05'], $bal['per06'], $bal['per07'], $bal['per08'],
113 $bal['per09'], $bal['per10'], $bal['per11'], $bal['per12'], $bal['pertotal']);
114 $rep->TextCol(0, 1, $account['account_code']);
115 $rep->TextCol(1, 2, $account['account_name']);
117 for ($i = 1; $i <= 13; $i++)
119 $rep->AmountCol($i + 1, $i + 2, $balance[$i] * $convert, $dec);
120 $ctotal[$i] += $balance[$i];
126 //Get Account groups/types under this group/type
127 $result = get_account_types(false, false, $type);
128 while ($accounttype=db_fetch($result))
130 //Print Type Title if has sub types and not previously printed
135 $rep->TextCol(0, 5, $typename);
137 $rep->Line($rep->row);
141 $totals_arr = display_type($accounttype["id"], $accounttype["name"], $yr, $mo, $convert, $dec, $rep,
142 $dimension, $dimension2, $tags, $thousands);
143 for ($i = 1; $i <= 13; $i++)
145 $total[$i] += $totals_arr[$i];
149 //Display Type Summary if total is != 0 OR head is printed (Needed in case of unused hierarchical COA)
153 $rep->Line($rep->row);
155 $rep->TextCol(0, 2, _('Total') . " " . $typename);
156 for ($i = 1; $i <= 13; $i++)
157 $rep->AmountCol($i + 1, $i + 2, ($total[$i] + $ctotal[$i]) * $convert, $dec);
160 for ($i = 1; $i <= 13; $i++)
161 $totals_arr[$i] = $total[$i] + $ctotal[$i];
165 //----------------------------------------------------------------------------------------------------
167 function print_annual_expense_breakdown()
169 global $path_to_root, $SysPrefs;
171 $dim = get_company_pref('use_dimension');
172 $dimension = $dimension2 = 0;
177 $year = $_POST['PARAM_0'];
178 $dimension = $_POST['PARAM_1'];
179 $dimension2 = $_POST['PARAM_2'];
180 $tags = (isset($_POST['PARAM_3']) ? $_POST['PARAM_3'] : -1);
181 $comments = $_POST['PARAM_4'];
182 $orientation = $_POST['PARAM_5'];
183 $thousands = $_POST['PARAM_6'];
184 $destination = $_POST['PARAM_7'];
188 $year = $_POST['PARAM_0'];
189 $dimension = $_POST['PARAM_1'];
190 $tags = (isset($_POST['PARAM_2']) ? $_POST['PARAM_2'] : -1);
191 $comments = $_POST['PARAM_3'];
192 $orientation = $_POST['PARAM_4'];
193 $thousands = $_POST['PARAM_5'];
194 $destination = $_POST['PARAM_6'];
198 $year = $_POST['PARAM_0'];
199 $tags = (isset($_POST['PARAM_1']) ? $_POST['PARAM_1'] : -1);
200 $comments = $_POST['PARAM_2'];
201 $orientation = $_POST['PARAM_3'];
202 $thousands = $_POST['PARAM_4'];
203 $destination = $_POST['PARAM_5'];
206 include_once($path_to_root . "/reporting/includes/excel_report.inc");
208 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
210 $orientation = ($orientation ? 'L' : 'P');
215 $amts_thousands = _("Amounts in thousands");
222 $amts_thousands = "";
223 $fontSize = ($orientation == 'P' ? 7 : 8);
226 $cols = array(0, 34, 130, 162, 194, 226, 258, 290, 322, 354, 386, 418, 450, 482, 514, 546);
227 //------------0--1---2----3----4----5----6----7----8----10---11---12---13---14---15---16-
230 $sql = "SELECT begin, end, YEAR(end) AS yr, MONTH(end) AS mo FROM ".TB_PREF."fiscal_year WHERE id=".db_escape($year);
231 $result = db_query($sql, "could not get fiscal year");
232 $row = db_fetch($result);
234 $year = sql2date($row['begin'])." - ".sql2date($row['end']);
238 if ($SysPrefs->date_system == 1)
239 list($yr, $mo, $da) = jalali_to_gregorian($yr, $mo, $da);
240 elseif ($SysPrefs->date_system == 2)
241 list($yr, $mo, $da) = islamic_to_gregorian($yr, $mo, $da);
242 $per12 = substr(date('F',mktime(0,0,0,$mo,$da,$yr)), 0, 3);
243 $per11 = substr(date('F',mktime(0,0,0,$mo-1,$da,$yr)), 0, 3);
244 $per10 = substr(date('F',mktime(0,0,0,$mo-2,$da,$yr)), 0, 3);
245 $per09 = substr(date('F',mktime(0,0,0,$mo-3,$da,$yr)), 0, 3);
246 $per08 = substr(date('F',mktime(0,0,0,$mo-4,$da,$yr)), 0, 3);
247 $per07 = substr(date('F',mktime(0,0,0,$mo-5,$da,$yr)), 0, 3);
248 $per06 = substr(date('F',mktime(0,0,0,$mo-6,$da,$yr)), 0, 3);
249 $per05 = substr(date('F',mktime(0,0,0,$mo-7,$da,$yr)), 0, 3);
250 $per04 = substr(date('F',mktime(0,0,0,$mo-8,$da,$yr)), 0, 3);
251 $per03 = substr(date('F',mktime(0,0,0,$mo-9,$da,$yr)), 0, 3);
252 $per02 = substr(date('F',mktime(0,0,0,$mo-10,$da,$yr)), 0, 3);
253 $per01 = substr(date('F',mktime(0,0,0,$mo-11,$da,$yr)), 0, 3);
255 $headers = array(_('Account'), _('Account Name'), $per01, $per02, $per03, $per04,
256 $per05, $per06, $per07, $per08, $per09, $per10, $per11, $per12, _('Total'));
258 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right',
259 'right', 'right', 'right', 'right', 'right', 'right', 'right');
263 $params = array( 0 => $comments,
264 1 => array('text' => _("Year"),
265 'from' => $year, 'to' => ''),
266 2 => array('text' => _("Dimension")." 1",
267 'from' => get_dimension_string($dimension), 'to' => ''),
268 3 => array('text' => _("Dimension")." 2",
269 'from' => get_dimension_string($dimension2), 'to' => ''),
270 4 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''),
271 5 => array('text' => _('Info'), 'from' => $amts_thousands, 'to' => ''));
275 $params = array( 0 => $comments,
276 1 => array('text' => _("Year"),
277 'from' => $year, 'to' => ''),
278 2 => array('text' => _('Dimension'),
279 'from' => get_dimension_string($dimension), 'to' => ''),
280 3 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''),
281 4 => array('text' => _('Info'), 'from' => $amts_thousands, 'to' => ''));
285 $params = array( 0 => $comments,
286 1 => array('text' => _("Year"),
287 'from' => $year, 'to' => ''),
288 2 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''),
289 3 => array('text' => _('Info'), 'from' => $amts_thousands, 'to' => ''));
292 $rep = new FrontReport(_('Annual Expense Breakdown'), "AnnualBreakDown", user_pagesize(), $fontSize,
294 if ($orientation == 'L')
295 recalculate_cols($cols);
296 elseif (user_pagesize() == 'A4')
298 // Portrait, so adjust columns for A4, 16.7 pts narrower than Letter
299 for ($i = 2; $i < sizeof($cols); $i++)
304 $rep->Info($params, $cols, $headers, $aligns);
307 $sales = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
309 $classresult = get_account_classes(false, 0);
310 while ($class = db_fetch($classresult))
312 $ctotal = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
313 $convert = get_class_type_convert($class["ctype"]);
317 $rep->TextCol(0, 5, $class["class_name"]);
321 //Get Account groups/types under this group/type with no parents
322 $typeresult = get_account_types(false, $class['cid'], -1);
323 while ($accounttype=db_fetch($typeresult))
325 $classtotal = display_type($accounttype["id"], $accounttype["name"], $yr, $mo, $convert, $dec, $rep,
326 $dimension, $dimension2, $tags, $thousands);
327 for ($i = 1; $i <= 13; $i++)
328 $ctotal[$i] += $classtotal[$i];
331 //Print Class Summary
333 $rep->Line($rep->row);
336 $rep->TextCol(0, 2, _('Total') . " " . $class["class_name"]);
337 for ($i = 1; $i <= 13; $i++)
339 $rep->AmountCol($i + 1, $i + 2, $ctotal[$i] * $convert, $dec);
340 $sales[$i] += $ctotal[$i];
346 $rep->TextCol(0, 2, _("Calculated Return"));
347 for ($i = 1; $i <= 13; $i++)
348 $rep->AmountCol($i + 1, $i + 2, $sales[$i] * -1, $dec);
351 $rep->Line($rep->row);