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 ***********************************************************************/
13 // ----------------------------------------------------------------
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");
26 //----------------------------------------------------------------------------------------------------
28 print_annual_expense_breakdown();
30 //----------------------------------------------------------------------------------------------------
32 function getPeriods($year, $account, $dimension, $dimension2)
36 $date13 = date('Y-m-d',mktime(0,0,0,$mo+1,1,$yr));
37 $date12 = date('Y-m-d',mktime(0,0,0,$mo,1,$yr));
38 $date11 = date('Y-m-d',mktime(0,0,0,$mo-1,1,$yr));
39 $date10 = date('Y-m-d',mktime(0,0,0,$mo-2,1,$yr));
40 $date09 = date('Y-m-d',mktime(0,0,0,$mo-3,1,$yr));
41 $date08 = date('Y-m-d',mktime(0,0,0,$mo-4,1,$yr));
42 $date07 = date('Y-m-d',mktime(0,0,0,$mo-5,1,$yr));
43 $date06 = date('Y-m-d',mktime(0,0,0,$mo-6,1,$yr));
44 $date05 = date('Y-m-d',mktime(0,0,0,$mo-7,1,$yr));
45 $date04 = date('Y-m-d',mktime(0,0,0,$mo-8,1,$yr));
46 $date03 = date('Y-m-d',mktime(0,0,0,$mo-9,1,$yr));
47 $date02 = date('Y-m-d',mktime(0,0,0,$mo-10,1,$yr));
48 $date01 = date('Y-m-d',mktime(0,0,0,$mo-11,1,$yr));
50 $sql = "SELECT SUM(CASE WHEN tran_date >= '$date01' AND tran_date < '$date02' THEN -amount / 1000 ELSE 0 END) AS per01,
51 SUM(CASE WHEN tran_date >= '$date02' AND tran_date < '$date03' THEN -amount / 1000 ELSE 0 END) AS per02,
52 SUM(CASE WHEN tran_date >= '$date03' AND tran_date < '$date04' THEN -amount / 1000 ELSE 0 END) AS per03,
53 SUM(CASE WHEN tran_date >= '$date04' AND tran_date < '$date05' THEN -amount / 1000 ELSE 0 END) AS per04,
54 SUM(CASE WHEN tran_date >= '$date05' AND tran_date < '$date06' THEN -amount / 1000 ELSE 0 END) AS per05,
55 SUM(CASE WHEN tran_date >= '$date06' AND tran_date < '$date07' THEN -amount / 1000 ELSE 0 END) AS per06,
56 SUM(CASE WHEN tran_date >= '$date07' AND tran_date < '$date08' THEN -amount / 1000 ELSE 0 END) AS per07,
57 SUM(CASE WHEN tran_date >= '$date08' AND tran_date < '$date09' THEN -amount / 1000 ELSE 0 END) AS per08,
58 SUM(CASE WHEN tran_date >= '$date09' AND tran_date < '$date10' THEN -amount / 1000 ELSE 0 END) AS per09,
59 SUM(CASE WHEN tran_date >= '$date10' AND tran_date < '$date11' THEN -amount / 1000 ELSE 0 END) AS per10,
60 SUM(CASE WHEN tran_date >= '$date11' AND tran_date < '$date12' THEN -amount / 1000 ELSE 0 END) AS per11,
61 SUM(CASE WHEN tran_date >= '$date12' AND tran_date < '$date13' THEN -amount / 1000 ELSE 0 END) AS per12
62 FROM ".TB_PREF."gl_trans
63 WHERE account='$account'";
65 $sql .= " AND dimension_id = $dimension";
67 $sql .= " AND dimension2_id = $dimension2";
69 $result = db_query($sql, "Transactions for account $account could not be calculated");
71 return db_fetch($result);
74 //----------------------------------------------------------------------------------------------------
76 function print_annual_expense_breakdown()
78 global $path_to_root, $date_system;
80 $dim = get_company_pref('use_dimension');
81 $dimension = $dimension2 = 0;
85 $year = $_POST['PARAM_0'];
86 $dimension = $_POST['PARAM_1'];
87 $dimension2 = $_POST['PARAM_2'];
88 $comments = $_POST['PARAM_3'];
89 $destination = $_POST['PARAM_4'];
93 $year = $_POST['PARAM_0'];
94 $dimension = $_POST['PARAM_1'];
95 $comments = $_POST['PARAM_2'];
96 $destination = $_POST['PARAM_3'];
100 $year = $_POST['PARAM_0'];
101 $comments = $_POST['PARAM_1'];
102 $destination = $_POST['PARAM_2'];
105 include_once($path_to_root . "/reporting/includes/excel_report.inc");
107 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
110 //$pdec = user_percent_dec();
112 $cols = array(0, 40, 150, 180, 210, 240, 270, 300, 330, 360, 390, 420, 450, 480, 510);
113 //------------0--1---2----3----4----5----6----7----8----10---11---12---13---14---15-
121 if ($date_system == 1)
122 list($yr, $mo, $da) = jalali_to_gregorian($yr, $mo, $da);
123 elseif ($date_system == 2)
124 list($yr, $mo, $da) = islamic_to_gregorian($yr, $mo, $da);
125 $per12 = strftime('%b',mktime(0,0,0,$mo,$da,$yr));
126 $per11 = strftime('%b',mktime(0,0,0,$mo-1,$da,$yr));
127 $per10 = strftime('%b',mktime(0,0,0,$mo-2,$da,$yr));
128 $per09 = strftime('%b',mktime(0,0,0,$mo-3,$da,$yr));
129 $per08 = strftime('%b',mktime(0,0,0,$mo-4,$da,$yr));
130 $per07 = strftime('%b',mktime(0,0,0,$mo-5,$da,$yr));
131 $per06 = strftime('%b',mktime(0,0,0,$mo-6,$da,$yr));
132 $per05 = strftime('%b',mktime(0,0,0,$mo-7,$da,$yr));
133 $per04 = strftime('%b',mktime(0,0,0,$mo-8,$da,$yr));
134 $per03 = strftime('%b',mktime(0,0,0,$mo-9,$da,$yr));
135 $per02 = strftime('%b',mktime(0,0,0,$mo-10,$da,$yr));
136 $per01 = strftime('%b',mktime(0,0,0,$mo-11,$da,$yr));
138 $headers = array(_('Account'), _('Account Name'), $per01, $per02, $per03, $per04,
139 $per05, $per06, $per07, $per08, $per09, $per10, $per11, $per12);
141 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right',
142 'right', 'right', 'right', 'right', 'right', 'right');
146 $params = array( 0 => $comments,
147 1 => array('text' => _("Year"),
148 'from' => $year, 'to' => ''),
149 2 => array('text' => _("Dimension")." 1",
150 'from' => get_dimension_string($dimension), 'to' => ''),
151 3 => array('text' => _("Dimension")." 2",
152 'from' => get_dimension_string($dimension2), 'to' => ''),
153 4 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
158 $params = array( 0 => $comments,
159 1 => array('text' => _("Year"),
160 'from' => $year, 'to' => ''),
161 2 => array('text' => _('Dimension'),
162 'from' => get_dimension_string($dimension), 'to' => ''),
163 3 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
168 $params = array( 0 => $comments,
169 1 => array('text' => _("Year"),
170 'from' => $year, 'to' => ''),
171 2 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
175 $rep = new FrontReport(_('Annual Expense Breakdown'), "AnnualBreakDown", user_pagesize());
178 $rep->Info($params, $cols, $headers, $aligns);
183 $total = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
184 $total2 = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
185 $sales = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
186 $calc = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
187 $accounts = get_gl_accounts_all(0);
189 while ($account = db_fetch($accounts))
191 $bal = getPeriods($year, $account["account_code"], $dimension, $dimension2);
192 if (!$bal['per01'] && !$bal['per02'] && !$bal['per03'] && !$bal['per04'] &&
193 !$bal['per05'] && !$bal['per06'] && !$bal['per07'] && !$bal['per08'] &&
194 !$bal['per09'] && !$bal['per10'] && !$bal['per11'] && !$bal['per12'])
196 $balance = array(1 => $bal['per01'], $bal['per02'], $bal['per03'], $bal['per04'],
197 $bal['per05'], $bal['per06'], $bal['per07'], $bal['per08'],
198 $bal['per09'], $bal['per10'], $bal['per11'], $bal['per12']);
199 if ($account['AccountClassName'] != $classname)
201 if ($classname != '')
207 if ($account['AccountTypeName'] != $group)
212 $rep->Line($rep->row);
214 $rep->TextCol(0, 2, _('Total') . " " . $group);
215 for ($i = 1; $i <= 12; $i++)
216 $rep->AmountCol($i + 1, $i + 2, $total[$i], $dec);
217 $total = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
222 $rep->Line($rep->row);
225 $rep->TextCol(0, 2, _('Total') . " " . $classname);
226 for ($i = 1; $i <= 12; $i++)
228 $rep->AmountCol($i + 1, $i + 2, $total2[$i], $dec);
229 $sales[$i] += $total2[$i];
232 $total2 = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
237 if ($account['AccountClassName'] != $classname)
240 $rep->TextCol(0, 5, $account['AccountClassName']);
244 $group = $account['AccountTypeName'];
246 $rep->TextCol(0, 5, $account['AccountTypeName']);
248 $rep->Line($rep->row);
251 $classname = $account['AccountClassName'];
252 $rep->TextCol(0, 1, $account['account_code']);
253 $rep->TextCol(1, 2, $account['account_name']);
254 for ($i = 1; $i <= 12; $i++)
256 $rep->AmountCol($i + 1, $i + 2, $balance[$i], $dec);
257 $total[$i] += $balance[$i];
258 $total2[$i] += $balance[$i];
263 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
265 $rep->Line($rep->row - 2);
269 if ($account['AccountClassName'] != $classname)
271 if ($classname != '')
276 if ($account['AccountTypeName'] != $group)
281 $rep->Line($rep->row);
283 $rep->TextCol(0, 2, _('Total') . " " . $group);
284 for ($i = 1; $i <= 12; $i++)
285 $rep->AmountCol($i + 1, $i + 2, $total[$i], $dec);
290 $rep->Line($rep->row);
294 $rep->TextCol(0, 2, _('Total') . " " . $classname);
295 for ($i = 1; $i <= 12; $i++)
297 $rep->AmountCol($i + 1, $i + 2, $total2[$i], $dec);
298 $calc[$i] = $sales[$i] + $total2[$i];
302 $rep->TextCol(0, 2, _('Calculated Return'));
303 for ($i = 1; $i <= 12; $i++)
304 $rep->AmountCol($i + 1, $i + 2, $calc[$i], $dec);
311 $rep->Line($rep->row);