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