Added option to handle Jalali and Islamic Calendars
[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         else if ($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();
172         $total2 = Array();
173         $sales = Array();
174         $calc = Array();
175         unset($total);
176         unset($total2);
177         unset($sales);
178         unset($calc);
179         $accounts = get_gl_accounts_all(0);
180
181         while ($account = db_fetch($accounts))
182         {
183                 $bal = getPeriods($year, $account["account_code"], $dimension, $dimension2);
184                 if (!$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                         continue;
188                 //if (array_sum($bal) == 0.0)
189                 //$i = 1;
190                 //foreach ($bal as $b)
191                 //      $balance[$i++] = $b;
192                 //$balance = $bal;
193                 $balance = Array(1 => $bal['per01'], $bal['per02'], $bal['per03'], $bal['per04'], 
194                         $bal['per05'], $bal['per06'], $bal['per07'], $bal['per08'], 
195                         $bal['per09'], $bal['per10'], $bal['per11'], $bal['per12']);
196                 if ($account['AccountClassName'] != $classname)
197                 {
198                         if ($classname != '')
199                         {
200                                 $closeclass = true;
201                         }
202                 }
203
204                 if ($account['AccountTypeName'] != $group)
205                 {
206                         if ($group != '')
207                         {
208                                 $rep->Line($rep->row + 6);
209                                 $rep->row -= 6;
210                                 $rep->TextCol(0, 2,     _('Total') . " " . $group);
211                                 for ($i = 1; $i <= 12; $i++)
212                                         $rep->TextCol($i + 1, $i + 2, number_format2($total[$i], $dec));
213                                 unset($total);
214                                 $rep->row -= ($rep->lineHeight + 4);
215                                 if ($closeclass)
216                                 {
217                                         $rep->Line($rep->row + 6);
218                                         $rep->row -= 6;
219                                         $rep->Font('bold');
220                                         $rep->TextCol(0, 2,     _('Total') . " " . $classname);
221                                         for ($i = 1; $i <= 12; $i++)
222                                         {
223                                                 $rep->TextCol($i + 1, $i + 2, number_format2($total2[$i], $dec));
224                                                 $sales[$i] += $total2[$i];
225                                         }       
226                                         $rep->Font();
227                                         unset($total2);
228                                         $rep->NewLine(3);
229                                         $closeclass = false;
230                                 }
231                         }
232                         if ($account['AccountClassName'] != $classname)
233                         {
234                                 $rep->Font('bold');
235                                 $rep->TextCol(0, 5, $account['AccountClassName']);
236                                 $rep->Font();
237                                 $rep->row -= ($rep->lineHeight + 4);
238                         }
239                         $group = $account['AccountTypeName'];
240                         $rep->TextCol(0, 5, $account['AccountTypeName']);
241                         $rep->Line($rep->row - 4);
242                         $rep->row -= ($rep->lineHeight + 4);
243                 }
244                 $classname = $account['AccountClassName'];
245                 $rep->TextCol(0, 1,     $account['account_code']);
246                 $rep->TextCol(1, 2,     $account['account_name']);
247                 for ($i = 1; $i <= 12; $i++)
248                 {
249                         $rep->TextCol($i + 1, $i + 2, number_format2($balance[$i], $dec));
250                         $total[$i] += $balance[$i];
251                         $total2[$i] += $balance[$i];
252                 }                       
253
254                 $rep->NewLine();
255
256                 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
257                 {
258                         $rep->Line($rep->row - 2);
259                         $rep->Header();
260                 }
261         }
262         if ($account['AccountClassName'] != $classname)
263         {
264                 if ($classname != '')
265                 {
266                         $closeclass = true;
267                 }
268         }
269         if ($account['AccountTypeName'] != $group)
270         {
271                 if ($group != '')
272                 {
273                         $rep->Line($rep->row + 6);
274                         $rep->row -= 6;
275                         $rep->TextCol(0, 2,     _('Total') . " " . $group);
276                         for ($i = 1; $i <= 12; $i++)
277                                 $rep->TextCol($i + 1, $i + 2, number_format2($total[$i], $dec));
278                         $rep->row -= ($rep->lineHeight + 4);
279                         if ($closeclass)
280                         {
281                                 $rep->Line($rep->row + 6);
282                                 $rep->row -= 6;
283
284                                 $rep->Font('bold');
285                                 $rep->TextCol(0, 2,     _('Total') . " " . $classname);
286                                 for ($i = 1; $i <= 12; $i++)
287                                 {
288                                         $rep->TextCol($i + 1, $i + 2, number_format2($total2[$i], $dec));
289                                         $calc[$i] = $sales[$i] + $total2[$i];
290                                 }       
291
292                                 $rep->row -= ($rep->lineHeight + 8);
293                                 $rep->TextCol(0, 2,     _('Calculated Return'));
294                                 for ($i = 1; $i <= 12; $i++)
295                                         $rep->TextCol($i + 1, $i + 2, number_format2($calc[$i], $dec));
296                                 $rep->Font();
297
298                                 $rep->NewLine();
299                         }
300                 }
301         }
302         $rep->Line($rep->row);
303         $rep->End();
304 }
305
306 ?>