Merging changes form main trunk 2.2.5-2.2.6
[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
26 //----------------------------------------------------------------------------------------------------
27
28 print_annual_expense_breakdown();
29
30 //----------------------------------------------------------------------------------------------------
31
32 function getPeriods($yr, $mo, $account, $dimension, $dimension2)
33 {
34         $date13 = date('Y-m-d',mktime(0,0,0,$mo+1,1,$yr));
35         $date12 = date('Y-m-d',mktime(0,0,0,$mo,1,$yr));
36         $date11 = date('Y-m-d',mktime(0,0,0,$mo-1,1,$yr));
37         $date10 = date('Y-m-d',mktime(0,0,0,$mo-2,1,$yr));
38         $date09 = date('Y-m-d',mktime(0,0,0,$mo-3,1,$yr));
39         $date08 = date('Y-m-d',mktime(0,0,0,$mo-4,1,$yr));
40         $date07 = date('Y-m-d',mktime(0,0,0,$mo-5,1,$yr));
41         $date06 = date('Y-m-d',mktime(0,0,0,$mo-6,1,$yr));
42         $date05 = date('Y-m-d',mktime(0,0,0,$mo-7,1,$yr));
43         $date04 = date('Y-m-d',mktime(0,0,0,$mo-8,1,$yr));
44         $date03 = date('Y-m-d',mktime(0,0,0,$mo-9,1,$yr));
45         $date02 = date('Y-m-d',mktime(0,0,0,$mo-10,1,$yr));
46         $date01 = date('Y-m-d',mktime(0,0,0,$mo-11,1,$yr));
47
48     $sql = "SELECT SUM(CASE WHEN tran_date >= '$date01' AND tran_date < '$date02' THEN amount / 1000 ELSE 0 END) AS per01,
49                                 SUM(CASE WHEN tran_date >= '$date02' AND tran_date < '$date03' THEN amount / 1000 ELSE 0 END) AS per02,
50                                 SUM(CASE WHEN tran_date >= '$date03' AND tran_date < '$date04' THEN amount / 1000 ELSE 0 END) AS per03,
51                                 SUM(CASE WHEN tran_date >= '$date04' AND tran_date < '$date05' THEN amount / 1000 ELSE 0 END) AS per04,
52                                 SUM(CASE WHEN tran_date >= '$date05' AND tran_date < '$date06' THEN amount / 1000 ELSE 0 END) AS per05,
53                                 SUM(CASE WHEN tran_date >= '$date06' AND tran_date < '$date07' THEN amount / 1000 ELSE 0 END) AS per06,
54                                 SUM(CASE WHEN tran_date >= '$date07' AND tran_date < '$date08' THEN amount / 1000 ELSE 0 END) AS per07,
55                                 SUM(CASE WHEN tran_date >= '$date08' AND tran_date < '$date09' THEN amount / 1000 ELSE 0 END) AS per08,
56                                 SUM(CASE WHEN tran_date >= '$date09' AND tran_date < '$date10' THEN amount / 1000 ELSE 0 END) AS per09,
57                                 SUM(CASE WHEN tran_date >= '$date10' AND tran_date < '$date11' THEN amount / 1000 ELSE 0 END) AS per10,
58                                 SUM(CASE WHEN tran_date >= '$date11' AND tran_date < '$date12' THEN amount / 1000 ELSE 0 END) AS per11,
59                                 SUM(CASE WHEN tran_date >= '$date12' AND tran_date < '$date13' THEN amount / 1000 ELSE 0 END) AS per12
60                         FROM ".TB_PREF."gl_trans
61                                 WHERE account='$account'";
62         if ($dimension != 0)
63                 $sql .= " AND dimension_id = ".($dimension<0?0:db_escape($dimension));
64         if ($dimension2 != 0)
65                 $sql .= " AND dimension2_id = ".($dimension2<0?0:db_escape($dimension2));
66
67         $result = db_query($sql, "Transactions for account $account could not be calculated");
68
69         return db_fetch($result);
70 }
71
72 //----------------------------------------------------------------------------------------------------
73
74 function display_type ($type, $typename, $yr, $mo, $convert, &$dec, &$rep, $dimension, $dimension2)
75 {
76         $ctotal = array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0);
77         $total = array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0);
78         $totals_arr = array();
79
80         $printtitle = 0; //Flag for printing type name  
81
82         //Get Accounts directly under this group/type
83         $result = get_gl_accounts(null, null, $type);   
84         while ($account=db_fetch($result))
85         {
86                 $bal = getPeriods($yr, $mo, $account["account_code"], $dimension, $dimension2);
87                 if (!$bal['per01'] && !$bal['per02'] && !$bal['per03'] && !$bal['per04'] &&     !$bal['per05'] && 
88                         !$bal['per06'] && !$bal['per07'] && !$bal['per08'] && !$bal['per09'] && !$bal['per10'] && 
89                         !$bal['per11'] && !$bal['per12'])
90                         continue;
91         
92                 //Print Type Title if it has atleast one non-zero account       
93                 if (!$printtitle)
94                 {
95                         $printtitle = 1;
96                         $rep->row -= 4;
97                         $rep->TextCol(0, 5, $typename);
98                         $rep->row -= 4;
99                         $rep->Line($rep->row);
100                         $rep->NewLine();
101                 }                       
102
103                 $balance = array(1 => $bal['per01'], $bal['per02'], $bal['per03'], $bal['per04'],
104                         $bal['per05'], $bal['per06'], $bal['per07'], $bal['per08'],
105                         $bal['per09'], $bal['per10'], $bal['per11'], $bal['per12']);
106                 $rep->TextCol(0, 1,     $account['account_code']);
107                 $rep->TextCol(1, 2,     $account['account_name']);
108
109                 for ($i = 1; $i <= 12; $i++)
110                 {
111                         $rep->AmountCol($i + 1, $i + 2, $balance[$i] * $convert, $dec);
112                         $ctotal[$i] += $balance[$i];
113                 }
114
115                 $rep->NewLine();
116         }
117                 
118         //Get Account groups/types under this group/type
119         $result = get_account_types(false, false, $type);
120         while ($accounttype=db_fetch($result))
121         {
122                 //Print Type Title if has sub types and not previously printed
123                 if (!$printtitle)
124                 {
125                         $printtitle = 1;
126                         $rep->row -= 4;
127                         $rep->TextCol(0, 5, $typename);
128                         $rep->row -= 4;
129                         $rep->Line($rep->row);
130                         $rep->NewLine();
131                 }
132
133                 $totals_arr = display_type($accounttype["id"], $accounttype["name"], $yr, $mo, $convert, $dec, $rep, $dimension, $dimension2);
134                 for ($i = 1; $i <= 12; $i++)
135                 {
136                         $total[$i] += $totals_arr[$i];
137                 }
138         }
139
140         //Display Type Summary if total is != 0 OR head is printed (Needed in case of unused hierarchical COA) 
141         if ($printtitle)
142         {
143                 $rep->row += 6;
144                 $rep->Line($rep->row);
145                 $rep->NewLine();
146                 $rep->TextCol(0, 2,     _('Total') . " " . $typename);
147                 for ($i = 1; $i <= 12; $i++)
148                         $rep->AmountCol($i + 1, $i + 2, ($total[$i] + $ctotal[$i]) * $convert, $dec);
149                 $rep->NewLine();
150         }
151         for ($i = 1; $i <= 12; $i++)
152                 $totals_arr[$i] = $total[$i] + $ctotal[$i];     
153         return $totals_arr;
154 }
155
156 //----------------------------------------------------------------------------------------------------
157
158 function print_annual_expense_breakdown()
159 {
160         global $path_to_root, $date_system;
161
162         $dim = get_company_pref('use_dimension');
163         $dimension = $dimension2 = 0;
164
165         if ($dim == 2)
166         {
167                 $year = $_POST['PARAM_0'];
168                 $dimension = $_POST['PARAM_1'];
169                 $dimension2 = $_POST['PARAM_2'];
170                 $comments = $_POST['PARAM_3'];
171                 $destination = $_POST['PARAM_4'];
172         }
173         else if ($dim == 1)
174         {
175                 $year = $_POST['PARAM_0'];
176                 $dimension = $_POST['PARAM_1'];
177                 $comments = $_POST['PARAM_2'];
178                 $destination = $_POST['PARAM_3'];
179         }
180         else
181         {
182                 $year = $_POST['PARAM_0'];
183                 $comments = $_POST['PARAM_1'];
184                 $destination = $_POST['PARAM_2'];
185         }
186         if ($destination)
187                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
188         else
189                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
190
191         $dec = 1;
192         //$pdec = user_percent_dec();
193
194         $cols = array(0, 40, 150, 180, 210, 240, 270, 300, 330, 360, 390, 420, 450, 480, 510);
195         //------------0--1---2----3----4----5----6----7----8----10---11---12---13---14---15-
196
197         //$yr = date('Y');
198         //$mo = date('m'):
199         // from now
200         $sql = "SELECT begin, end, YEAR(end) AS yr, MONTH(end) AS mo FROM ".TB_PREF."fiscal_year WHERE id=".db_escape($year);
201         $result = db_query($sql, "could not get fiscal year");
202         $row = db_fetch($result);
203         
204         $year = sql2date($row['begin'])." - ".sql2date($row['end']);
205         $yr = $row['yr'];
206         $mo = $row['mo'];
207         $da = 1;
208         if ($date_system == 1)
209                 list($yr, $mo, $da) = jalali_to_gregorian($yr, $mo, $da);
210         elseif ($date_system == 2)
211                 list($yr, $mo, $da) = islamic_to_gregorian($yr, $mo, $da);
212         $per12 = strftime('%b',mktime(0,0,0,$mo,$da,$yr));
213         $per11 = strftime('%b',mktime(0,0,0,$mo-1,$da,$yr));
214         $per10 = strftime('%b',mktime(0,0,0,$mo-2,$da,$yr));
215         $per09 = strftime('%b',mktime(0,0,0,$mo-3,$da,$yr));
216         $per08 = strftime('%b',mktime(0,0,0,$mo-4,$da,$yr));
217         $per07 = strftime('%b',mktime(0,0,0,$mo-5,$da,$yr));
218         $per06 = strftime('%b',mktime(0,0,0,$mo-6,$da,$yr));
219         $per05 = strftime('%b',mktime(0,0,0,$mo-7,$da,$yr));
220         $per04 = strftime('%b',mktime(0,0,0,$mo-8,$da,$yr));
221         $per03 = strftime('%b',mktime(0,0,0,$mo-9,$da,$yr));
222         $per02 = strftime('%b',mktime(0,0,0,$mo-10,$da,$yr));
223         $per01 = strftime('%b',mktime(0,0,0,$mo-11,$da,$yr));
224
225         $headers = array(_('Account'), _('Account Name'), $per01, $per02, $per03, $per04,
226                 $per05, $per06, $per07, $per08, $per09, $per10, $per11, $per12);
227
228         $aligns = array('left', 'left', 'right', 'right', 'right',      'right', 'right', 'right',
229                 'right', 'right', 'right',      'right', 'right', 'right');
230
231     if ($dim == 2)
232     {
233         $params =   array(      0 => $comments,
234                         1 => array('text' => _("Year"),
235                                 'from' => $year, 'to' => ''),
236                         2 => array('text' => _("Dimension")." 1",
237                                 'from' => get_dimension_string($dimension), 'to' => ''),
238                         3 => array('text' => _("Dimension")." 2",
239                                 'from' => get_dimension_string($dimension2), 'to' => ''),
240                         4 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
241                                 'to' => ''));
242     }
243     else if ($dim == 1)
244     {
245         $params =   array(      0 => $comments,
246                         1 => array('text' => _("Year"),
247                                 'from' => $year, 'to' => ''),
248                         2 => array('text' => _('Dimension'),
249                                 'from' => get_dimension_string($dimension), 'to' => ''),
250                         3 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
251                                 'to' => ''));
252     }
253     else
254     {
255         $params =   array(      0 => $comments,
256                         1 => array('text' => _("Year"),
257                                 'from' => $year, 'to' => ''),
258                         2 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
259                                 'to' => ''));
260     }
261
262         $rep = new FrontReport(_('Annual Expense Breakdown'), "AnnualBreakDown", user_pagesize());
263
264         $rep->Font();
265         $rep->Info($params, $cols, $headers, $aligns);
266         $rep->Header();
267
268         $sales = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0);
269         
270         $classresult = get_account_classes(false, 0);
271         while ($class = db_fetch($classresult))
272         {
273                 $ctotal = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0);
274                 $convert = get_class_type_convert($class["ctype"]);             
275                 
276                 //Print Class Name      
277                 $rep->Font('bold');
278                 $rep->TextCol(0, 5, $class["class_name"]);
279                 $rep->Font();
280                 $rep->NewLine();
281                 
282                 //Get Account groups/types under this group/type with no parents
283                 $typeresult = get_account_types(false, $class['cid'], -1);
284                 while ($accounttype=db_fetch($typeresult))
285                 {
286                         $classtotal = display_type($accounttype["id"], $accounttype["name"], $yr, $mo, $convert, $dec, $rep, $dimension, $dimension2);
287                         for ($i = 1; $i <= 12; $i++)
288                                 $ctotal[$i] += $classtotal[$i];
289                 }
290                 
291                 //Print Class Summary   
292                 $rep->row += 6;
293                 $rep->Line($rep->row);
294                 $rep->NewLine();
295                 $rep->Font('bold');
296                 $rep->TextCol(0, 2,     _('Total') . " " . $class["class_name"]);
297                 for ($i = 1; $i <= 12; $i++)
298                 {
299                         $rep->AmountCol($i + 1, $i + 2, $ctotal[$i] * $convert, $dec);
300                         $sales[$i] += $ctotal[$i];
301                 }
302                 $rep->Font();
303                 $rep->NewLine(2);
304         }
305         $rep->Font('bold');     
306         $rep->TextCol(0, 2,     _("Calculated Return"));
307         for ($i = 1; $i <= 12; $i++)
308                 $rep->AmountCol($i + 1, $i + 2, $sales[$i] * -1, $dec);
309         $rep->Font();
310         $rep->NewLine();
311         $rep->Line($rep->row);
312         $rep->NewLine(2);
313         $rep->End();
314 }
315
316 ?>