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