*** empty log message ***
[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;
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         $per12 = strftime('%b',mktime(0,0,0,$mo,$da,$yr));
110         $per11 = strftime('%b',mktime(0,0,0,$mo-1,$da,$yr));
111         $per10 = strftime('%b',mktime(0,0,0,$mo-2,$da,$yr));
112         $per09 = strftime('%b',mktime(0,0,0,$mo-3,$da,$yr));
113         $per08 = strftime('%b',mktime(0,0,0,$mo-4,$da,$yr));
114         $per07 = strftime('%b',mktime(0,0,0,$mo-5,$da,$yr));
115         $per06 = strftime('%b',mktime(0,0,0,$mo-6,$da,$yr));
116         $per05 = strftime('%b',mktime(0,0,0,$mo-7,$da,$yr));
117         $per04 = strftime('%b',mktime(0,0,0,$mo-8,$da,$yr));
118         $per03 = strftime('%b',mktime(0,0,0,$mo-9,$da,$yr));
119         $per02 = strftime('%b',mktime(0,0,0,$mo-10,$da,$yr));
120         $per01 = strftime('%b',mktime(0,0,0,$mo-11,$da,$yr));
121         
122         $headers = array(_('Account'), _('Account Name'), $per01, $per02, $per03, $per04,
123                 $per05, $per06, $per07, $per08, $per09, $per10, $per11, $per12);
124         
125         $aligns = array('left', 'left', 'right', 'right', 'right',      'right', 'right', 'right',
126                 'right', 'right', 'right',      'right', 'right', 'right');
127     
128     if ($dim == 2)
129     {
130         $params =   array(      0 => $comments,
131                         1 => array('text' => _("Year"), 
132                                 'from' => $year, 'to' => ''),
133                         2 => array('text' => _("Dimension")." 1", 
134                                 'from' => get_dimension_string($dimension), 'to' => ''),
135                         3 => array('text' => _("Dimension")." 2", 
136                                 'from' => get_dimension_string($dimension2), 'to' => ''),
137                         4 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
138                                 'to' => ''));
139     }
140     else if ($dim == 1)
141     {
142         $params =   array(      0 => $comments,
143                         1 => array('text' => _("Year"), 
144                                 'from' => $year, 'to' => ''),
145                         2 => array('text' => _('Dimension'), 
146                                 'from' => get_dimension_string($dimension), 'to' => ''),
147                         3 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
148                                 'to' => ''));
149     }
150     else
151     {
152         $params =   array(      0 => $comments,
153                         1 => array('text' => _("Year"), 
154                                 'from' => $year, 'to' => ''),
155                         2 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
156                                 'to' => ''));
157     }
158
159         $rep = new FrontReport(_('Annual Expense Breakdown'), "AnnualBreakDown.pdf", user_pagesize());
160
161         $rep->Font();
162         $rep->Info($params, $cols, $headers, $aligns);
163         $rep->Header();
164
165         $classname = '';
166         $group = '';
167         $total = Array();
168         $total2 = Array();
169         $sales = Array();
170         $calc = Array();
171         unset($total);
172         unset($total2);
173         unset($sales);
174         unset($calc);
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                 //if (array_sum($bal) == 0.0)
185                 //$i = 1;
186                 //foreach ($bal as $b)
187                 //      $balance[$i++] = $b;
188                 //$balance = $bal;
189                 $balance = Array(1 => $bal['per01'], $bal['per02'], $bal['per03'], $bal['per04'], 
190                         $bal['per05'], $bal['per06'], $bal['per07'], $bal['per08'], 
191                         $bal['per09'], $bal['per10'], $bal['per11'], $bal['per12']);
192                 if ($account['AccountClassName'] != $classname)
193                 {
194                         if ($classname != '')
195                         {
196                                 $closeclass = true;
197                         }
198                 }
199
200                 if ($account['AccountTypeName'] != $group)
201                 {
202                         if ($group != '')
203                         {
204                                 $rep->Line($rep->row + 6);
205                                 $rep->row -= 6;
206                                 $rep->TextCol(0, 2,     _('Total') . " " . $group);
207                                 for ($i = 1; $i <= 12; $i++)
208                                         $rep->TextCol($i + 1, $i + 2, number_format2($total[$i], $dec));
209                                 unset($total);
210                                 $rep->row -= ($rep->lineHeight + 4);
211                                 if ($closeclass)
212                                 {
213                                         $rep->Line($rep->row + 6);
214                                         $rep->row -= 6;
215                                         $rep->Font('bold');
216                                         $rep->TextCol(0, 2,     _('Total') . " " . $classname);
217                                         for ($i = 1; $i <= 12; $i++)
218                                         {
219                                                 $rep->TextCol($i + 1, $i + 2, number_format2($total2[$i], $dec));
220                                                 $sales[$i] += $total2[$i];
221                                         }       
222                                         $rep->Font();
223                                         unset($total2);
224                                         $rep->NewLine(3);
225                                         $closeclass = false;
226                                 }
227                         }
228                         if ($account['AccountClassName'] != $classname)
229                         {
230                                 $rep->Font('bold');
231                                 $rep->TextCol(0, 5, $account['AccountClassName']);
232                                 $rep->Font();
233                                 $rep->row -= ($rep->lineHeight + 4);
234                         }
235                         $group = $account['AccountTypeName'];
236                         $rep->TextCol(0, 5, $account['AccountTypeName']);
237                         $rep->Line($rep->row - 4);
238                         $rep->row -= ($rep->lineHeight + 4);
239                 }
240                 $classname = $account['AccountClassName'];
241                 $rep->TextCol(0, 1,     $account['account_code']);
242                 $rep->TextCol(1, 2,     $account['account_name']);
243                 for ($i = 1; $i <= 12; $i++)
244                 {
245                         $rep->TextCol($i + 1, $i + 2, number_format2($balance[$i], $dec));
246                         $total[$i] += $balance[$i];
247                         $total2[$i] += $balance[$i];
248                 }                       
249
250                 $rep->NewLine();
251
252                 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
253                 {
254                         $rep->Line($rep->row - 2);
255                         $rep->Header();
256                 }
257         }
258         if ($account['AccountClassName'] != $classname)
259         {
260                 if ($classname != '')
261                 {
262                         $closeclass = true;
263                 }
264         }
265         if ($account['AccountTypeName'] != $group)
266         {
267                 if ($group != '')
268                 {
269                         $rep->Line($rep->row + 6);
270                         $rep->row -= 6;
271                         $rep->TextCol(0, 2,     _('Total') . " " . $group);
272                         for ($i = 1; $i <= 12; $i++)
273                                 $rep->TextCol($i + 1, $i + 2, number_format2($total[$i], $dec));
274                         $rep->row -= ($rep->lineHeight + 4);
275                         if ($closeclass)
276                         {
277                                 $rep->Line($rep->row + 6);
278                                 $rep->row -= 6;
279
280                                 $rep->Font('bold');
281                                 $rep->TextCol(0, 2,     _('Total') . " " . $classname);
282                                 for ($i = 1; $i <= 12; $i++)
283                                 {
284                                         $rep->TextCol($i + 1, $i + 2, number_format2($total2[$i], $dec));
285                                         $calc[$i] = $sales[$i] + $total2[$i];
286                                 }       
287
288                                 $rep->row -= ($rep->lineHeight + 8);
289                                 $rep->TextCol(0, 2,     _('Calculated Return'));
290                                 for ($i = 1; $i <= 12; $i++)
291                                         $rep->TextCol($i + 1, $i + 2, number_format2($calc[$i], $dec));
292                                 $rep->Font();
293
294                                 $rep->NewLine();
295                         }
296                 }
297         }
298         $rep->Line($rep->row);
299         $rep->End();
300 }
301
302 ?>