Annual Expense Breakdown Report: another coding fix.
[fa-stable.git] / reporting / rep705.php
1 <?php
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 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt, Chaitanya for the recursive version 2009-02-05.
16 // date_:       2005-05-19
17 // Title:       Annual expense breakdown
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
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");
26
27 //----------------------------------------------------------------------------------------------------
28
29 print_annual_expense_breakdown();
30
31 //----------------------------------------------------------------------------------------------------
32
33 function getPeriods($yr, $mo, $account, $dimension, $dimension2, $thousands)
34 {
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));
48
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'";
64         if ($dimension != 0)
65                 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
66         if ($dimension2 != 0)
67                 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
68
69         $result = db_query($sql, "Transactions for account $account could not be calculated");
70
71         return db_fetch($result);
72 }
73
74 //----------------------------------------------------------------------------------------------------
75
76 function display_type ($type, $typename, $yr, $mo, $convert, &$dec, &$rep, $dimension, $dimension2, 
77                                                                 $tags, $thousands)
78 {
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();
82
83         $printtitle = 0; //Flag for printing type name  
84
85         //Get Accounts directly under this group/type
86         $result = get_gl_accounts(null, null, $type);   
87         while ($account=db_fetch($result))
88         {
89                 if ($tags != -1 && is_array($tags) && $tags[0] != false)
90                 {
91                         if (!is_record_in_tags($tags, TAG_ACCOUNT, $account['account_code']))
92                                 continue;
93                 }       
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'])
98                         continue;
99         
100                 //Print Type Title if it has at least one non-zero account      
101                 if (!$printtitle)
102                 {
103                         $printtitle = 1;
104                         $rep->row -= 4;
105                         $rep->TextCol(0, 5, $typename);
106                         $rep->row -= 4;
107                         $rep->Line($rep->row);
108                         $rep->NewLine();
109                 }                       
110
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']);
116
117                 for ($i = 1; $i <= 13; $i++)
118                 {
119                         $rep->AmountCol($i + 1, $i + 2, $balance[$i] * $convert, $dec);
120                         $ctotal[$i] += $balance[$i];
121                 }
122
123                 $rep->NewLine();
124         }
125                 
126         //Get Account groups/types under this group/type
127         $result = get_account_types(false, false, $type);
128         while ($accounttype=db_fetch($result))
129         {
130                 //Print Type Title if has sub types and not previously printed
131                 if (!$printtitle)
132                 {
133                         $printtitle = 1;
134                         $rep->row -= 4;
135                         $rep->TextCol(0, 5, $typename);
136                         $rep->row -= 4;
137                         $rep->Line($rep->row);
138                         $rep->NewLine();
139                 }
140
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++)
144                 {
145                         $total[$i] += $totals_arr[$i];
146                 }
147         }
148
149         //Display Type Summary if total is != 0 OR head is printed (Needed in case of unused hierarchical COA) 
150         if ($printtitle)
151         {
152                 $rep->row += 6;
153                 $rep->Line($rep->row);
154                 $rep->NewLine();
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);
158                 $rep->NewLine();
159         }
160         for ($i = 1; $i <= 13; $i++)
161                 $totals_arr[$i] = $total[$i] + $ctotal[$i];     
162         return $totals_arr;
163 }
164
165 //----------------------------------------------------------------------------------------------------
166
167 function print_annual_expense_breakdown()
168 {
169         global $path_to_root, $SysPrefs;
170
171         $dim = get_company_pref('use_dimension');
172         $dimension = $dimension2 = 0;
173         $thousands = 0;
174
175         if ($dim == 2)
176         {
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'];
185         }
186         elseif ($dim == 1)
187         {
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'];
195         }
196         else
197         {
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'];
204         }
205         if ($destination)
206                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
207         else
208                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
209
210         $orientation = ($orientation ? 'L' : 'P');
211         if ($thousands)
212         {
213                 $dec = 1;
214                 $thousands = 1000;
215                 $amts_thousands = _("Amounts in thousands");
216                 $fontSize = 9;
217         }
218         else
219         {
220                 $dec = 2;
221                 $thousands = 1;
222                 $amts_thousands = "";
223                 $fontSize = ($orientation == 'P' ? 7 : 8);
224         }
225
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-
228
229         // from now
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);
233         
234         $year = sql2date($row['begin'])." - ".sql2date($row['end']);
235         $yr = $row['yr'];
236         $mo = $row['mo'];
237         $da = 1;
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 = strftime('%b',mktime(0,0,0,$mo,$da,$yr));
243         $per11 = strftime('%b',mktime(0,0,0,$mo-1,$da,$yr));
244         $per10 = strftime('%b',mktime(0,0,0,$mo-2,$da,$yr));
245         $per09 = strftime('%b',mktime(0,0,0,$mo-3,$da,$yr));
246         $per08 = strftime('%b',mktime(0,0,0,$mo-4,$da,$yr));
247         $per07 = strftime('%b',mktime(0,0,0,$mo-5,$da,$yr));
248         $per06 = strftime('%b',mktime(0,0,0,$mo-6,$da,$yr));
249         $per05 = strftime('%b',mktime(0,0,0,$mo-7,$da,$yr));
250         $per04 = strftime('%b',mktime(0,0,0,$mo-8,$da,$yr));
251         $per03 = strftime('%b',mktime(0,0,0,$mo-9,$da,$yr));
252         $per02 = strftime('%b',mktime(0,0,0,$mo-10,$da,$yr));
253         $per01 = strftime('%b',mktime(0,0,0,$mo-11,$da,$yr));
254
255         $headers = array(_('Account'), _('Account Name'), $per01, $per02, $per03, $per04,
256                 $per05, $per06, $per07, $per08, $per09, $per10, $per11, $per12, _('Total'));
257
258         $aligns = array('left', 'left', 'right', 'right', 'right',      'right', 'right', 'right',
259                 'right', 'right', 'right',      'right', 'right', 'right', 'right');
260
261     if ($dim == 2)
262     {
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' => ''));
272     }
273     elseif ($dim == 1)
274     {
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' => ''));
282     }
283     else
284     {
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' => ''));
290     }
291
292         $rep = new FrontReport(_('Annual Expense Breakdown'), "AnnualBreakDown", user_pagesize(), $fontSize,
293                                                                                 $orientation);
294    if ($orientation == 'L')
295         recalculate_cols($cols);
296         elseif (user_pagesize() == 'A4')
297         {
298                 // Portrait, so adjust columns for A4, 16.7 pts narrower than Letter
299                 for ($i = 2; $i < sizeof($cols); $i++)
300                         $cols[$i] -= 17;
301         }
302
303         $rep->Font();
304         $rep->Info($params, $cols, $headers, $aligns);
305         $rep->NewPage();
306
307         $sales = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
308         
309         $classresult = get_account_classes(false, 0);
310         while ($class = db_fetch($classresult))
311         {
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"]);             
314                 
315                 //Print Class Name      
316                 $rep->Font('bold');
317                 $rep->TextCol(0, 5, $class["class_name"]);
318                 $rep->Font();
319                 $rep->NewLine();
320                 
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))
324                 {
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];
329                 }
330                 
331                 //Print Class Summary   
332                 $rep->row += 6;
333                 $rep->Line($rep->row);
334                 $rep->NewLine();
335                 $rep->Font('bold');
336                 $rep->TextCol(0, 2,     _('Total') . " " . $class["class_name"]);
337                 for ($i = 1; $i <= 13; $i++)
338                 {
339                         $rep->AmountCol($i + 1, $i + 2, $ctotal[$i] * $convert, $dec);
340                         $sales[$i] += $ctotal[$i];
341                 }
342                 $rep->Font();
343                 $rep->NewLine(2);
344         }
345         $rep->Font('bold');     
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);
349         $rep->Font();
350         $rep->NewLine();
351         $rep->Line($rep->row);
352         $rep->NewLine(2);
353         $rep->End();
354 }
355