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