Removed annoying warnings in several reports.
[fa-stable.git] / reporting / rep707.php
1 <?php
2
3 $page_security = 2;
4 // ----------------------------------------------------------------
5 // $ Revision:  2.0 $
6 // Creator:     Joe Hunt
7 // date_:       2005-05-19
8 // Title:       Profit and Loss Statement
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_profit_and_loss_statement();
21
22 //----------------------------------------------------------------------------------------------------
23
24 function Achieve($d1, $d2)
25 {
26         if ($d1 == 0 && $d2 == 0)
27                 return 0;
28         elseif ($d2 == 0)
29                 return 999;
30         $ret = ($d1 / $d2 * 100.0);
31         if ($ret > 999)
32                 $ret = 999;
33         return $ret;
34 }
35
36 //----------------------------------------------------------------------------------------------------
37
38 function print_profit_and_loss_statement()
39 {
40         global $comp_path, $path_to_root;
41
42         include_once($path_to_root . "reporting/includes/pdf_report.inc");
43         $dim = get_company_pref('use_dimension');
44         $dimension = $dimension2 = 0;
45
46         $from = $_POST['PARAM_0'];
47         $to = $_POST['PARAM_1'];
48         $compare = $_POST['PARAM_2'];
49         if ($dim == 2)
50         {
51                 $dimension = $_POST['PARAM_3'];
52                 $dimension2 = $_POST['PARAM_4'];
53                 $graphics = $_POST['PARAM_5'];
54                 $comments = $_POST['PARAM_6'];
55         }
56         else if ($dim == 1)
57         {
58                 $dimension = $_POST['PARAM_3'];
59                 $graphics = $_POST['PARAM_4'];
60                 $comments = $_POST['PARAM_5'];
61         }
62         else
63         {
64                 $graphics = $_POST['PARAM_3'];
65                 $comments = $_POST['PARAM_4'];
66         }
67         if ($graphics)
68         {
69                 include_once($path_to_root . "reporting/includes/class.graphic.inc");
70                 $pg = new graph();
71         }
72         $dec = 0;
73         $pdec = user_percent_dec();
74
75         $cols = array(0, 50, 200, 350, 425,     500);
76         //------------0--1---2----3----4----5--
77
78         $headers = array(_('Account'), _('Account Name'), _('Period'), _('Accumulated'), _('Achieved %'));
79
80         $aligns = array('left', 'left', 'right', 'right', 'right');
81
82     if ($dim == 2)
83     {
84         $params =   array(      0 => $comments,
85                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
86                         2 => array('text' => _('Dimension')." 1",
87                             'from' => get_dimension_string($dimension), 'to' => ''),
88                         3 => array('text' => _('Dimension')." 2",
89                             'from' => get_dimension_string($dimension2), 'to' => ''));
90     }
91     else if ($dim == 1)
92     {
93         $params =   array(      0 => $comments,
94                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
95                         2 => array('text' => _('Dimension'),
96                             'from' => get_dimension_string($dimension), 'to' => ''));
97     }
98     else
99     {
100         $params =   array(      0 => $comments,
101                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
102     }
103
104
105         if ($compare == 0 || $compare == 2)
106         {
107                 $end = $to;
108                 if ($compare == 2)
109                 {
110                         $begin = $from;
111                         $headers[3] = _('Budget');
112                 }
113                 else
114                         $begin = begin_fiscalyear();
115         }
116         elseif ($compare == 1)
117         {
118                 $begin = add_months($from, -12);
119                 $end = add_months($to, -12);
120                 $headers[3] = _('Period Y-1');
121         }
122
123         $rep = new FrontReport(_('Profit and Loss Statement'), "ProfitAndLoss.pdf", user_pagesize());
124
125         $rep->Font();
126         $rep->Info($params, $cols, $headers, $aligns);
127         $rep->Header();
128
129         $classname = '';
130         $group = '';
131         $totalper = 0.0;
132         $totalacc = 0.0;
133         $classper = 0.0;
134         $classacc = 0.0;
135         $salesper = 0.0;
136         $salesacc = 0.0;
137
138         $accounts = get_gl_accounts_all(0);
139
140         while ($account=db_fetch($accounts))
141         {
142                 $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
143
144                 if ($compare == 2)
145                         $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
146                 else
147                         $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
148                 if (!$per_balance && !$acc_balance)
149                         continue;
150
151                 if ($account['AccountClassName'] != $classname)
152                 {
153                         if ($classname != '')
154                         {
155                                 $closeclass = true;
156                         }
157                 }
158
159                 if ($account['AccountTypeName'] != $group)
160                 {
161                         if ($group != '')
162                         {
163                                 $rep->Line($rep->row + 6);
164                                 $rep->row -= 6;
165                                 $rep->TextCol(0, 2,     _('Total') . " " . $group);
166                                 $rep->TextCol(2, 3,     number_format2($totalper, $dec));
167                                 $rep->TextCol(3, 4,     number_format2($totalacc, $dec));
168                                 $rep->TextCol(4, 5,     number_format2(Achieve($totalper, $totalacc), $pdec));
169                                 if ($graphics)
170                                 {
171                                         $pg->x[] = $group;
172                                         $pg->y[] = abs($totalper);
173                                         $pg->z[] = abs($totalacc);
174                                 }
175                                 $totalper = $totalacc = 0.0;
176                                 $rep->row -= ($rep->lineHeight + 4);
177                                 if ($closeclass)
178                                 {
179                                         $rep->Line($rep->row + 6);
180                                         $rep->row -= 6;
181                                         $rep->Font('bold');
182                                         $rep->TextCol(0, 2,     _('Total') . " " . $classname);
183                                         $rep->TextCol(2, 3,     number_format2($classper, $dec));
184                                         $rep->TextCol(3, 4,     number_format2($classacc, $dec));
185                                         $rep->TextCol(4, 5,     number_format2(Achieve($classper, $classacc), $pdec));
186                                         $rep->Font();
187                                         $salesper += $classper;
188                                         $salesacc += $classacc;
189                                         $classper = $classacc = 0.0;
190                                         $rep->NewLine(3);
191                                         $closeclass = false;
192                                 }
193                         }
194                         if ($account['AccountClassName'] != $classname)
195                         {
196                                 $rep->Font('bold');
197                                 $rep->TextCol(0, 5, $account['AccountClassName']);
198                                 $rep->Font();
199                                 $rep->row -= ($rep->lineHeight + 4);
200                         }
201                         $group = $account['AccountTypeName'];
202                         $rep->TextCol(0, 5, $account['AccountTypeName']);
203                         $rep->Line($rep->row - 4);
204                         $rep->row -= ($rep->lineHeight + 4);
205                 }
206                 $classname = $account['AccountClassName'];
207
208                 $per_balance *= -1;
209                 $acc_balance *= -1;
210                 $totalper += $per_balance;
211                 $totalacc += $acc_balance;
212                 $classper += $per_balance;
213                 $classacc += $acc_balance;
214                 $rep->TextCol(0, 1,     $account['account_code']);
215                 $rep->TextCol(1, 2,     $account['account_name']);
216
217                 $rep->TextCol(2, 3,     number_format2($per_balance, $dec));
218                 $rep->TextCol(3, 4,     number_format2($acc_balance, $dec));
219                 $rep->TextCol(4, 5,     number_format2(Achieve($per_balance, $acc_balance), $pdec));
220
221                 $rep->NewLine();
222
223                 if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
224                 {
225                         $rep->Line($rep->row - 2);
226                         $rep->Header();
227                 }
228         }
229         if ($account['AccountClassName'] != $classname)
230         {
231                 if ($classname != '')
232                 {
233                         $closeclass = true;
234                 }
235         }
236         if ($account['AccountTypeName'] != $group)
237         {
238                 if ($group != '')
239                 {
240                         $rep->Line($rep->row + 6);
241                         $rep->row -= 6;
242                         $rep->TextCol(0, 2,     _('Total') . " " . $group);
243                         $rep->TextCol(2, 3,     number_format2($totalper, $dec));
244                         $rep->TextCol(3, 4,     number_format2($totalacc, $dec));
245                         $rep->TextCol(4, 5,     number_format2(Achieve($totalper, $totalacc), $pdec));
246                         if ($graphics)
247                         {
248                                 $pg->x[] = $group;
249                                 $pg->y[] = abs($totalper);
250                                 $pg->z[] = abs($totalacc);
251                         }
252                         $rep->row -= ($rep->lineHeight + 4);
253                         if ($closeclass)
254                         {
255                                 $rep->Line($rep->row + 6);
256                                 $calculateper = $salesper + $classper;
257                                 $calculateacc = $salesacc + $classacc;
258                                 $rep->row -= 6;
259
260                                 $rep->Font('bold');
261                                 $rep->TextCol(0, 2,     _('Total') . " " . $classname);
262                                 $rep->TextCol(2, 3,     number_format2($classper, $dec));
263                                 $rep->TextCol(3, 4,     number_format2($classacc, $dec));
264                                 $rep->TextCol(4, 5,     number_format2(Achieve($classper, $classacc), $pdec));
265
266                                 $rep->row -= ($rep->lineHeight + 8);
267                                 $rep->TextCol(0, 2,     _('Calculated Return'));
268                                 $rep->TextCol(2, 3,     number_format2($calculateper, $dec));
269                                 $rep->TextCol(3, 4,     number_format2($calculateacc, $dec));
270                                 $rep->TextCol(4, 5,     number_format2(Achieve($calculateper, $calculateacc), $pdec));
271                                 if ($graphics)
272                                 {
273                                         $pg->x[] = _('Calculated Return');
274                                         $pg->y[] = abs($calculateper);
275                                         $pg->z[] = abs($calculateacc);
276                                 }
277
278                                 $rep->Font();
279
280                                 $rep->NewLine();
281                         }
282                 }
283         }
284         $rep->Line($rep->row);
285         if ($graphics)
286         {
287                 global $decseps, $graph_skin;
288                 $pg->title     = $rep->title;
289                 $pg->axis_x    = _("Group");
290                 $pg->axis_y    = _("Amount");
291                 $pg->graphic_1 = $headers[2];
292                 $pg->graphic_2 = $headers[3];
293                 $pg->type      = $graphics;
294                 $pg->skin      = $graph_skin;
295                 $pg->built_in  = false;
296                 $pg->fontfile  = $path_to_root . "reporting/fonts/Vera.ttf";
297                 $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
298                 $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
299                 $pg->display($filename, true);
300                 $w = $pg->width / 1.5;
301                 $h = $pg->height / 1.5;
302                 $x = ($rep->pageWidth - $w) / 2;
303                 $rep->NewLine(2);
304                 if ($rep->row - $h < $rep->bottomMargin)
305                         $rep->Header();
306                 $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
307         }
308         $rep->End();
309 }
310
311 ?>