Merged changes from main trunk up to 2.2.5
[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
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
26 //----------------------------------------------------------------------------------------------------
27
28 print_annual_expense_breakdown();
29
30 //----------------------------------------------------------------------------------------------------
31
32 function getPeriods($row, $account, $dimension, $dimension2)
33 {
34         $yr = $row['yr'];
35         $mo = $row['mo'];
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));
49
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'";
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 print_annual_expense_breakdown()
77 {
78         global $path_to_root, $date_system;
79
80         $dim = get_company_pref('use_dimension');
81         $dimension = $dimension2 = 0;
82
83         if ($dim == 2)
84         {
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'];
90         }
91         else if ($dim == 1)
92         {
93                 $year = $_POST['PARAM_0'];
94                 $dimension = $_POST['PARAM_1'];
95                 $comments = $_POST['PARAM_2'];
96                 $destination = $_POST['PARAM_3'];
97         }
98         else
99         {
100                 $year = $_POST['PARAM_0'];
101                 $comments = $_POST['PARAM_1'];
102                 $destination = $_POST['PARAM_2'];
103         }
104         if ($destination)
105                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
106         else
107                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
108
109         $dec = 1;
110         //$pdec = user_percent_dec();
111
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-
114
115         //$yr = date('Y');
116         //$mo = date('m'):
117         // from now
118         $sql = "SELECT begin, end, YEAR(end) AS yr, MONTH(end) AS mo FROM ".TB_PREF."fiscal_year WHERE id=".db_escape($year);
119         $result = db_query($sql, "could not get fiscal year");
120         $row = db_fetch($result);
121         
122         $year = sql2date($row['begin'])." - ".sql2date($row['end']);
123         $yr = $row['yr'];
124         $mo = $row['mo'];
125         $da = 1;
126         if ($date_system == 1)
127                 list($yr, $mo, $da) = jalali_to_gregorian($yr, $mo, $da);
128         elseif ($date_system == 2)
129                 list($yr, $mo, $da) = islamic_to_gregorian($yr, $mo, $da);
130         $per12 = strftime('%b',mktime(0,0,0,$mo,$da,$yr));
131         $per11 = strftime('%b',mktime(0,0,0,$mo-1,$da,$yr));
132         $per10 = strftime('%b',mktime(0,0,0,$mo-2,$da,$yr));
133         $per09 = strftime('%b',mktime(0,0,0,$mo-3,$da,$yr));
134         $per08 = strftime('%b',mktime(0,0,0,$mo-4,$da,$yr));
135         $per07 = strftime('%b',mktime(0,0,0,$mo-5,$da,$yr));
136         $per06 = strftime('%b',mktime(0,0,0,$mo-6,$da,$yr));
137         $per05 = strftime('%b',mktime(0,0,0,$mo-7,$da,$yr));
138         $per04 = strftime('%b',mktime(0,0,0,$mo-8,$da,$yr));
139         $per03 = strftime('%b',mktime(0,0,0,$mo-9,$da,$yr));
140         $per02 = strftime('%b',mktime(0,0,0,$mo-10,$da,$yr));
141         $per01 = strftime('%b',mktime(0,0,0,$mo-11,$da,$yr));
142
143         $headers = array(_('Account'), _('Account Name'), $per01, $per02, $per03, $per04,
144                 $per05, $per06, $per07, $per08, $per09, $per10, $per11, $per12);
145
146         $aligns = array('left', 'left', 'right', 'right', 'right',      'right', 'right', 'right',
147                 'right', 'right', 'right',      'right', 'right', 'right');
148
149     if ($dim == 2)
150     {
151         $params =   array(      0 => $comments,
152                         1 => array('text' => _("Year"),
153                                 'from' => $year, 'to' => ''),
154                         2 => array('text' => _("Dimension")." 1",
155                                 'from' => get_dimension_string($dimension), 'to' => ''),
156                         3 => array('text' => _("Dimension")." 2",
157                                 'from' => get_dimension_string($dimension2), 'to' => ''),
158                         4 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
159                                 'to' => ''));
160     }
161     else if ($dim == 1)
162     {
163         $params =   array(      0 => $comments,
164                         1 => array('text' => _("Year"),
165                                 'from' => $year, 'to' => ''),
166                         2 => array('text' => _('Dimension'),
167                                 'from' => get_dimension_string($dimension), 'to' => ''),
168                         3 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
169                                 'to' => ''));
170     }
171     else
172     {
173         $params =   array(      0 => $comments,
174                         1 => array('text' => _("Year"),
175                                 'from' => $year, 'to' => ''),
176                         2 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
177                                 'to' => ''));
178     }
179
180         $rep = new FrontReport(_('Annual Expense Breakdown'), "AnnualBreakDown", user_pagesize());
181
182         $rep->Font();
183         $rep->Info($params, $cols, $headers, $aligns);
184         $rep->Header();
185
186         $classname = '';
187         $total = Array(
188                 0 => Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
189                         Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
190                         Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
191                         Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
192                         Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
193                         Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
194                         Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
195                         Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
196                         Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
197                         Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0));
198         $total2 = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
199         $sales = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
200         $calc = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
201         $typename = array('','','','','','','','','','');
202         $closing = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
203         $level = 0;
204         $last = -1;
205
206         $closeclass = false;
207         $convert = 1;
208         $ctype = 0;
209
210         $accounts = get_gl_accounts_all(0);
211
212         while ($account=db_fetch($accounts))
213         {
214                 if ($account['account_code'] == null && $account['parent'] > 0)
215                         continue;
216
217                 if ($account['account_code'] != null)
218                 {
219                         $bal = getPeriods($row, $account["account_code"], $dimension, $dimension2);
220                         if (!$bal['per01'] && !$bal['per02'] && !$bal['per03'] && !$bal['per04'] &&
221                                 !$bal['per05'] && !$bal['per06'] && !$bal['per07'] && !$bal['per08'] &&
222                                 !$bal['per09'] && !$bal['per10'] && !$bal['per11'] && !$bal['per12'])
223                                 continue;
224                 }
225                 if ($account['AccountClassName'] != $classname)
226                 {
227                         if ($classname != '')
228                         {
229                                 $closeclass = true;
230                         }
231                 }
232
233                 if ($account['AccountTypeName'] != $typename[$level])
234                 {
235                         if ($typename[$level] != '')
236                         {
237                                 for ( ; $level >= 0, $typename[$level] != ''; $level--) 
238                                 {
239                                         if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0 || $closeclass)
240                                         {
241                                                 $rep->row += 6;
242                                                 $rep->Line($rep->row);
243                                                 $rep->NewLine();
244                                                 $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
245                                                 for ($i = 1; $i <= 12; $i++)
246                                                 {
247                                                         $rep->AmountCol($i + 1, $i + 2, $total[$level][$i] * $convert, $dec);
248                                                         $total[$level][$i] = 0.0;
249                                                 }
250                                         }
251                                         else
252                                                 break;
253                                         $rep->NewLine();
254                                 }       
255                                 if ($closeclass)
256                                 {
257                                         $rep->row += 6;
258                                         $rep->Line($rep->row);
259                                         $rep->NewLine();
260                                         $rep->Font('bold');
261                                         $rep->TextCol(0, 2,     _('Total') . " " . $classname);
262                                         for ($i = 1; $i <= 12; $i++)
263                                         {
264                                                 $rep->AmountCol($i + 1, $i + 2, $total2[$i] * $convert, $dec);
265                                                 $sales[$i] += $total2[$i];
266                                         }
267                                         $rep->Font();
268                                         $total2 = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
269                                         $rep->NewLine(2);
270                                         $closeclass = false;
271                                 }
272                         }
273                         if ($account['AccountClassName'] != $classname)
274                         {
275                                 $rep->Font('bold');
276                                 $rep->TextCol(0, 5, $account['AccountClassName']);
277                                 $rep->Font();
278                                 $rep->NewLine();
279                         }
280                         $level++;
281                         if ($account['parent'] != $last)
282                                 $last = $account['parent'];
283                         $typename[$level] = $account['AccountTypeName'];
284                         $closing[$level] = $account['parent'];
285                         $rep->row -= 4;
286                         $rep->TextCol(0, 5, $account['AccountTypeName']);
287                         $rep->row -= 4;
288                         $rep->Line($rep->row);
289                         $rep->NewLine();
290                 }
291                 $classname = $account['AccountClassName'];
292                 $ctype = $account['ClassType'];
293                 $convert = get_class_type_convert($ctype); 
294
295                 if ($account['account_code'] != null)
296                 {
297                         $balance = array(1 => $bal['per01'], $bal['per02'], $bal['per03'], $bal['per04'],
298                                 $bal['per05'], $bal['per06'], $bal['per07'], $bal['per08'],
299                                 $bal['per09'], $bal['per10'], $bal['per11'], $bal['per12']);
300                         $rep->TextCol(0, 1,     $account['account_code']);
301                         $rep->TextCol(1, 2,     $account['account_name']);
302
303                         for ($i = 1; $i <= 12; $i++)
304                         {
305                                 $rep->AmountCol($i + 1, $i + 2, $balance[$i] * $convert, $dec);
306                                 $total2[$i] += $balance[$i];
307                         }
308                         for ($j = 0; $j <= $level; $j++)
309                         {
310                                 for ($i = 1; $i <= 12; $i++)
311                                         $total[$j][$i] += $balance[$i];
312                         }
313                         $rep->NewLine();
314
315                         if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
316                         {
317                                 $rep->Line($rep->row - 2);
318                                 $rep->Header();
319                         }
320                 }       
321         }
322         if ($account['AccountClassName'] != $classname)
323         {
324                 if ($classname != '')
325                 {
326                         $closeclass = true;
327                 }
328         }
329         if ($account['AccountTypeName'] != $typename[$level])
330         {
331                 if ($typename[$level] != '')
332                 {
333                         for ( ; $level >= 0, $typename[$level] != ''; $level--) 
334                         {
335                                 if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0 || $closeclass)
336                                 {
337                                         $rep->row += 6;
338                                         $rep->Line($rep->row);
339                                         $rep->NewLine();
340                                         $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
341                                         for ($i = 1; $i <= 12; $i++)
342                                         {
343                                                 $rep->AmountCol($i + 1, $i + 2, $total[$level][$i] * $convert, $dec);
344                                                 $total[$level][$i] = 0.0;
345                                         }
346                                 }
347                                 else
348                                         break;
349                                 $rep->NewLine();
350                         }       
351                         if ($closeclass)
352                         {
353                                 $rep->row += 6;
354                                 $rep->Line($rep->row);
355                                 $rep->NewLine();
356
357                                 $rep->Font('bold');
358                                 $rep->TextCol(0, 2,     _('Total') . " " . $classname);
359                                 for ($i = 1; $i <= 12; $i++)
360                                 {
361                                         $rep->AmountCol($i + 1, $i + 2, $total2[$i] * $convert, $dec);
362                                         $calc[$i] = $sales[$i] + $total2[$i];
363                                 }
364
365                                 $rep->NewLine(2);
366                                 $rep->TextCol(0, 2,     _('Calculated Return'));
367                                 for ($i = 1; $i <= 12; $i++)
368                                         $rep->AmountCol($i + 1, $i + 2, $calc[$i] * -1, $dec); // always convert
369                                 $rep->Font();
370
371                                 $rep->NewLine();
372                         }
373                 }
374         }
375         $rep->Line($rep->row);
376         $rep->End();
377 }
378
379 ?>