- modules/fotolab updated imagej to current version & some cod fixes to make it work
[care2x.git] / Care2007 / modules / weberp / GLProfit_Loss.php
blob62c674156226badc148e8a6f976202ea18a31d53
1 <?php
3 /* $Revision: 1.13 $ */
5 $PageSecurity = 8;
7 include ('includes/session.inc');
8 $title = _('Profit and Loss');
9 include('includes/SQL_CommonFunctions.inc');
10 include('includes/AccountSectionsDef.inc'); // This loads the $Sections variable
13 if ($_POST['FromPeriod'] > $_POST['ToPeriod']){
14 prnMsg(_('The selected period from is actually after the period to') . '! ' . _('Please reselect the reporting period'),'error');
15 $_POST['SelectADifferentPeriod']='Select A Different Period';
18 if ((! isset($_POST['FromPeriod']) AND ! isset($_POST['ToPeriod'])) OR isset($_POST['SelectADifferentPeriod'])){
20 include('includes/header.inc');
21 echo "<FORM METHOD='POST' ACTION=" . $_SERVER['PHP_SELF'] . '?' . SID . '>';
23 if (Date('m') > $_SESSION['YearEnd']){
24 /*Dates in SQL format */
25 $DefaultFromDate = Date ('Y-m-d', Mktime(0,0,0,$_SESSION['YearEnd'] + 2,0,Date('Y')));
26 } else {
27 $DefaultFromDate = Date ('Y-m-d', Mktime(0,0,0,$_SESSION['YearEnd'] + 2,0,Date('Y')-1));
30 /*Show a form to allow input of criteria for profit and loss to show */
31 echo '<CENTER><TABLE><TR><TD>'._('Select Period From').":</TD><TD><SELECT Name='FromPeriod'>";
33 $sql = 'SELECT periodno, lastdate_in_period FROM periods ORDER BY periodno DESC';
34 $Periods = DB_query($sql,$db);
37 while ($myrow=DB_fetch_array($Periods,$db)){
38 if(isset($_POST['FromPeriod']) AND $_POST['FromPeriod']!=''){
39 if( $_POST['FromPeriod']== $myrow['periodno']){
40 echo '<OPTION SELECTED VALUE=' . $myrow['periodno'] . '>' .MonthAndYearFromSQLDate($myrow['lastdate_in_period']);
41 } else {
42 echo '<OPTION VALUE=' . $myrow['periodno'] . '>' . MonthAndYearFromSQLDate($myrow['lastdate_in_period']);
44 } else {
45 if($myrow['lastdate_in_period']==$DefaultFromDate){
46 echo '<OPTION SELECTED VALUE=' . $myrow['periodno'] . '>' . MonthAndYearFromSQLDate($myrow['lastdate_in_period']);
47 } else {
48 echo '<OPTION VALUE=' . $myrow['periodno'] . '>' . MonthAndYearFromSQLDate($myrow['lastdate_in_period']);
53 echo '</SELECT></TD></TR>';
54 if (!isset($_POST['ToPeriod']) OR $_POST['ToPeriod']==''){
55 $sql = 'SELECT MAX(periodno) FROM periods';
56 $MaxPrd = DB_query($sql,$db);
57 $MaxPrdrow = DB_fetch_row($MaxPrd);
59 $DefaultToPeriod = (int) ($MaxPrdrow[0]-1);
60 } else {
61 $DefaultToPeriod = $_POST['ToPeriod'];
64 echo '<TR><TD>' . _('Select Period To') . ":</TD><TD><SELECT Name='ToPeriod'>";
66 $RetResult = DB_data_seek($Periods,0);
68 while ($myrow=DB_fetch_array($Periods,$db)){
70 if($myrow['periodno']==$DefaultToPeriod){
71 echo '<OPTION SELECTED VALUE=' . $myrow['periodno'] . '>' . MonthAndYearFromSQLDate($myrow['lastdate_in_period']);
72 } else {
73 echo '<OPTION VALUE =' . $myrow['periodno'] . '>' . MonthAndYearFromSQLDate($myrow['lastdate_in_period']);
76 echo '</SELECT></TD></TR>';
78 echo '<TR><TD>'._('Detail Or Summary').":</TD><TD><SELECT Name='Detail'>";
79 echo "<OPTION SELECTED VALUE='Summary'>"._('Summary');
80 echo "<OPTION SELECTED VALUE='Detailed'>"._('All Accounts');
81 echo '</SELECT></TD></TR>';
83 echo '</TABLE>';
85 echo "<INPUT TYPE=SUBMIT Name='ShowPL' Value='"._('Show Statement of Profit and Loss')."'></CENTER>";
86 echo "<CENTER><INPUT TYPE=SUBMIT Name='PrintPDF' Value='"._('PrintPDF')."'></CENTER>";
88 /*Now do the posting while the user is thinking about the period to select */
90 include ('includes/GLPostings.inc');
92 } else if (isset($_POST['PrintPDF'])) {
94 include('includes/PDFStarter.php');
95 $PageNumber = 0;
96 $FontSize = 10;
97 $pdf->addinfo('Title', _('Profit and Loss') );
98 $pdf->addinfo('Subject', _('Profit and Loss') );
99 $line_height = 12;
101 $NumberOfMonths = $_POST['ToPeriod'] - $_POST['FromPeriod'] + 1;
103 if ($NumberOfMonths > 12){
104 include('includes/header.inc');
105 echo '<P>';
106 prnMsg(_('A period up to 12 months in duration can be specified') . ' - ' . _('the system automatically shows a comparative for the same period from the previous year') . ' - ' . _('it cannot do this if a period of more than 12 months is specified') . '. ' . _('Please select an alternative period range'),'error');
107 include('includes/footer.inc');
108 exit;
111 $sql = 'SELECT lastdate_in_period FROM periods WHERE periodno=' . $_POST['ToPeriod'];
112 $PrdResult = DB_query($sql, $db);
113 $myrow = DB_fetch_row($PrdResult);
114 $PeriodToDate = MonthAndYearFromSQLDate($myrow[0]);
117 $SQL = 'SELECT accountgroups.sectioninaccounts,
118 accountgroups.groupname,
119 accountgroups.parentgroupname,
120 chartdetails.accountcode ,
121 chartmaster.accountname,
122 Sum(CASE WHEN chartdetails.period=' . $_POST['FromPeriod'] . ' THEN chartdetails.bfwd ELSE 0 END) AS firstprdbfwd,
123 Sum(CASE WHEN chartdetails.period=' . $_POST['FromPeriod'] . ' THEN chartdetails.bfwdbudget ELSE 0 END) AS firstprdbudgetbfwd,
124 Sum(CASE WHEN chartdetails.period=' . $_POST['ToPeriod'] . ' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS lastprdcfwd,
125 Sum(CASE WHEN chartdetails.period=' . ($_POST['FromPeriod'] - 12) . ' THEN chartdetails.bfwd ELSE 0 END) AS lyfirstprdbfwd,
126 Sum(CASE WHEN chartdetails.period=' . ($_POST['ToPeriod']-12) . ' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS lylastprdcfwd,
127 Sum(CASE WHEN chartdetails.period=' . $_POST['ToPeriod'] . ' THEN chartdetails.bfwdbudget + chartdetails.budget ELSE 0 END) AS lastprdbudgetcfwd
128 FROM chartmaster INNER JOIN accountgroups
129 ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
130 ON chartmaster.accountcode= chartdetails.accountcode
131 WHERE accountgroups.pandl=1
132 GROUP BY accountgroups.sectioninaccounts,
133 accountgroups.groupname,
134 accountgroups.parentgroupname,
135 chartdetails.accountcode,
136 chartmaster.accountname,
137 accountgroups.sequenceintb
138 ORDER BY accountgroups.sectioninaccounts,
139 accountgroups.sequenceintb,
140 accountgroups.groupname,
141 chartdetails.accountcode';
143 $AccountsResult = DB_query($SQL,$db);
144 if (DB_error_no($db) != 0) {
145 $title = _('Profit and Loss') . ' - ' . _('Problem Report') . '....';
146 include('includes/header.inc');
147 prnMsg( _('No general ledger accounts were returned by the SQL because') . ' - ' . DB_error_msg($db) );
148 echo '<BR><A HREF="' .$rootpath .'/index.php?' . SID . '">'. _('Back to the menu'). '</A>';
149 if ($debug == 1){
150 echo '<BR>'. $SQL;
152 include('includes/footer.inc');
153 exit;
156 include('includes/PDFProfitAndLossPageHeader.inc');
158 $Section = '';
159 $SectionPrdActual = 0;
160 $SectionPrdLY = 0;
161 $SectionPrdBudget = 0;
163 $ActGrp = '';
164 $ParentGroups = array();
165 $Level = 0;
166 $ParentGroups[$Level]='';
167 $GrpPrdActual = array(0);
168 $GrpPrdLY = array(0);
169 $GrpPrdBudget = array(0);
171 while ($myrow = DB_fetch_array($AccountsResult)){
173 // Print heading if at end of page
174 if ($YPos < ($Bottom_Margin)){
175 include('includes/PDFProfitAndLossPageHeader.inc');
178 if ($myrow['groupname'] != $ActGrp){
179 if ($ActGrp != ''){
180 if ($myrow['parentgroupname']!=$ActGrp){
181 while ($myrow['groupname']!=$ParentGroups[$Level] AND $Level>0) {
182 if ($_POST['Detail'] == 'Detailed'){
183 $ActGrpLabel = $ParentGroups[$Level] . ' ' . _('total');
184 } else {
185 $ActGrpLabel = $ParentGroups[$Level];
187 if ($Section == 1){ /*Income */
188 $LeftOvers = $pdf->addTextWrap($Left_Margin +($Level*10),$YPos,200 -($Level*10),$FontSize,$ActGrpLabel);
189 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format(-$GrpPrdActual[$Level]),'right');
190 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format(-$GrpPrdBudget[$Level]),'right');
191 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format(-$GrpPrdLY[$Level]),'right');
192 $YPos -= (2 * $line_height);
193 } else { /*Costs */
194 $LeftOvers = $pdf->addTextWrap($Left_Margin +($Level*10),$YPos,200 -($Level*10),$FontSize,$ActGrpLabel);
195 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format($GrpPrdActual[$Level]),'right');
196 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format($GrpPrdBudget[$Level]),'right');
197 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format($GrpPrdLY[$Level]),'right');
198 $YPos -= (2 * $line_height);
200 $GrpPrdLY[$Level] = 0;
201 $GrpPrdActual[$Level] = 0;
202 $GrpPrdBudget[$Level] = 0;
203 $ParentGroups[$Level] ='';
204 $Level--;
205 // Print heading if at end of page
206 if ($YPos < ($Bottom_Margin + (2*$line_height))){
207 include('includes/PDFProfitAndLossPageHeader.inc');
209 } //end of loop
210 //still need to print out the group total for the same level
211 if ($_POST['Detail'] == 'Detailed'){
212 $ActGrpLabel = $ParentGroups[$Level] . ' ' . _('total');
213 } else {
214 $ActGrpLabel = $ParentGroups[$Level];
216 if ($Section == 1){ /*Income */
217 $LeftOvers = $pdf->addTextWrap($Left_Margin +($Level*10),$YPos,200 -($Level*10),$FontSize,$ActGrpLabel); $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format(-$GrpPrdActual[$Level]),'right');
218 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format(-$GrpPrdBudget[$Level]),'right');
219 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format(-$GrpPrdLY[$Level]),'right');
220 $YPos -= (2 * $line_height);
221 } else { /*Costs */
222 $LeftOvers = $pdf->addTextWrap($Left_Margin +($Level*10),$YPos,200 -($Level*10),$FontSize,$ActGrpLabel);
223 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format($GrpPrdActual[$Level]),'right');
224 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format($GrpPrdBudget[$Level]),'right');
225 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format($GrpPrdLY[$Level]),'right');
226 $YPos -= (2 * $line_height);
228 $GrpPrdLY[$Level] = 0;
229 $GrpPrdActual[$Level] = 0;
230 $GrpPrdBudget[$Level] = 0;
231 $ParentGroups[$Level] ='';
236 // Print heading if at end of page
237 if ($YPos < ($Bottom_Margin +(2 * $line_height))){
238 include('includes/PDFProfitAndLossPageHeader.inc');
241 if ($myrow['sectioninaccounts'] != $Section){
243 $pdf->selectFont('./fonts/Helvetica-Bold.afm');
244 $FontSize =10;
245 if ($Section != ''){
246 $pdf->line($Left_Margin+310, $YPos+$line_height,$Left_Margin+500, $YPos+$line_height);
247 $pdf->line($Left_Margin+310, $YPos,$Left_Margin+500, $YPos);
248 if ($Section == 1) { /*Income*/
250 $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,200,$FontSize,$Sections[$Section]);
251 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format(-$SectionPrdActual),'right');
252 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format(-$SectionPrdBudget),'right');
253 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format(-$SectionPrdLY),'right');
254 $YPos -= (2 * $line_height);
256 $TotalIncome = -$SectionPrdActual;
257 $TotalBudgetIncome = -$SectionPrdBudget;
258 $TotalLYIncome = -$SectionPrdLY;
259 } else {
260 $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,200,$FontSize,$Sections[$Section]);
261 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format($SectionPrdActual),'right');
262 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format($SectionPrdBudget),'right');
263 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format($SectionPrdLY),'right');
264 $YPos -= (2 * $line_height);
266 if ($Section == 2){ /*Cost of Sales - need sub total for Gross Profit*/
267 $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,200,$FontSize,_('Gross Profit'));
268 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format($TotalIncome - $SectionPrdActual),'right');
269 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format($TotalBudgetIncome - $SectionPrdBudget),'right');
270 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format($TotalLYIncome - $SectionPrdLY),'right');
271 $pdf->line($Left_Margin+310, $YPos+$line_height,$Left_Margin+500, $YPos+$line_height);
272 $pdf->line($Left_Margin+310, $YPos,$Left_Margin+500, $YPos);
273 $YPos -= (2 * $line_height);
275 if ($TotalIncome != 0){
276 $PrdGPPercent = 100 *($TotalIncome - $SectionPrdActual) / $TotalIncome;
277 } else {
278 $PrdGPPercent = 0;
280 if ($TotalBudgetIncome != 0){
281 $BudgetGPPercent = 100 * ($TotalBudgetIncome - $SectionPrdBudget) / $TotalBudgetIncome;
282 } else {
283 $BudgetGPPercent = 0;
285 if ($TotalLYIncome != 0){
286 $LYGPPercent = 100 * ($TotalLYIncome - $SectionPrdLY) / $TotalLYIncome;
287 } else {
288 $LYGPPercent = 0;
290 $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,200,$FontSize,_('Gross Profit Percent'));
291 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format($PrdGPPercent,1) . '%','right');
292 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format($BudgetGPPercent,1) . '%','right');
293 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format($LYGPPercent,1). '%','right');
294 $YPos -= (2 * $line_height);
297 $SectionPrdLY = 0;
298 $SectionPrdActual = 0;
299 $SectionPrdBudget = 0;
301 $Section = $myrow['sectioninaccounts'];
303 if ($_POST['Detail'] == 'Detailed'){
304 $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,200,$FontSize,$Sections[$myrow['sectioninaccounts']]);
305 $YPos -= (2 * $line_height);
307 $FontSize =8;
308 $pdf->selectFont('./fonts/Helvetica.afm');
311 if ($myrow['groupname'] != $ActGrp){
312 if ($myrow['parentgroupname']==$ActGrp AND $ActGrp !=''){ //adding another level of nesting
313 $Level++;
315 $ActGrp = $myrow['groupname'];
316 $ParentGroups[$Level]=$ActGrp;
317 if ($_POST['Detail'] == 'Detailed'){
318 $FontSize =10;
319 $pdf->selectFont('./fonts/Helvetica-Bold.afm');
320 $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,200,$FontSize,$myrow['groupname']);
321 $YPos -= (2 * $line_height);
322 $FontSize =8;
323 $pdf->selectFont('./fonts/Helvetica.afm');
327 $AccountPeriodActual = $myrow['lastprdcfwd'] - $myrow['firstprdbfwd'];
328 $AccountPeriodLY = $myrow['lylastprdcfwd'] - $myrow['lyfirstprdbfwd'];
329 $AccountPeriodBudget = $myrow['lastprdbudgetcfwd'] - $myrow['firstprdbudgetbfwd'];
330 $PeriodProfitLoss += $AccountPeriodActual;
331 $PeriodBudgetProfitLoss += $AccountPeriodBudget;
332 $PeriodLYProfitLoss += $AccountPeriodLY;
334 for ($i=0;$i<=$Level;$i++){
335 $GrpPrdLY[$i] +=$AccountPeriodLY;
336 $GrpPrdActual[$i] +=$AccountPeriodActual;
337 $GrpPrdBudget[$i] +=$AccountPeriodBudget;
341 $SectionPrdLY +=$AccountPeriodLY;
342 $SectionPrdActual +=$AccountPeriodActual;
343 $SectionPrdBudget +=$AccountPeriodBudget;
345 if ($_POST['Detail'] == _('Detailed')) {
346 $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,60,$FontSize,$myrow['accountcode']);
347 $LeftOvers = $pdf->addTextWrap($Left_Margin+60,$YPos,190,$FontSize,$myrow['accountname']);
348 if ($Section == 1) { /*Income*/
349 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format(-$AccountPeriodActual),'right');
350 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format(-$AccountPeriodBudget),'right');
351 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format(-$AccountPeriodLY),'right');
352 } else {
353 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format($AccountPeriodActual),'right');
354 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format($AccountPeriodBudget),'right');
355 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format($AccountPeriodLY),'right');
357 $YPos -= $line_height;
360 //end of loop
362 if ($ActGrp != ''){
364 if ($myrow['parentgroupname']!=$ActGrp){
366 while ($myrow['groupname']!=$ParentGroups[$Level] AND $Level>0) {
367 if ($_POST['Detail'] == 'Detailed'){
368 $ActGrpLabel = $ParentGroups[$Level] . ' ' . _('total');
369 } else {
370 $ActGrpLabel = $ParentGroups[$Level];
372 if ($Section == 1){ /*Income */
373 $LeftOvers = $pdf->addTextWrap($Left_Margin +($Level*10),$YPos,200 -($Level*10),$FontSize,$ActGrpLabel);
374 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format(-$GrpPrdActual[$Level]),'right');
375 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format(-$GrpPrdBudget[$Level]),'right');
376 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format(-$GrpPrdLY[$Level]),'right');
377 $YPos -= (2 * $line_height);
378 } else { /*Costs */
379 $LeftOvers = $pdf->addTextWrap($Left_Margin +($Level*10),$YPos,200 -($Level*10),$FontSize,$ActGrpLabel);
380 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format($GrpPrdActual[$Level]),'right');
381 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format($GrpPrdBudget[$Level]),'right');
382 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format($GrpPrdLY[$Level]),'right');
383 $YPos -= (2 * $line_height);
385 $GrpPrdLY[$Level] = 0;
386 $GrpPrdActual[$Level] = 0;
387 $GrpPrdBudget[$Level] = 0;
388 $ParentGroups[$Level] ='';
389 $Level--;
390 // Print heading if at end of page
391 if ($YPos < ($Bottom_Margin + (2*$line_height))){
392 include('includes/PDFProfitAndLossPageHeader.inc');
395 //still need to print out the group total for the same level
396 if ($_POST['Detail'] == 'Detailed'){
397 $ActGrpLabel = $ParentGroups[$Level] . ' ' . _('total');
398 } else {
399 $ActGrpLabel = $ParentGroups[$Level];
401 if ($Section == 1){ /*Income */
402 $LeftOvers = $pdf->addTextWrap($Left_Margin +($Level*10),$YPos,200 -($Level*10),$FontSize,$ActGrpLabel); $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format(-$GrpPrdActual[$Level]),'right');
403 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format(-$GrpPrdBudget[$Level]),'right');
404 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format(-$GrpPrdLY[$Level]),'right');
405 $YPos -= (2 * $line_height);
406 } else { /*Costs */
407 $LeftOvers = $pdf->addTextWrap($Left_Margin +($Level*10),$YPos,200 -($Level*10),$FontSize,$ActGrpLabel);
408 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format($GrpPrdActual[$Level]),'right');
409 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format($GrpPrdBudget[$Level]),'right');
410 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format($GrpPrdLY[$Level]),'right');
411 $YPos -= (2 * $line_height);
413 $GrpPrdLY[$Level] = 0;
414 $GrpPrdActual[$Level] = 0;
415 $GrpPrdBudget[$Level] = 0;
416 $ParentGroups[$Level] ='';
419 // Print heading if at end of page
420 if ($YPos < ($Bottom_Margin + (2*$line_height))){
421 include('includes/PDFProfitAndLossPageHeader.inc');
423 if ($Section != ''){
425 $pdf->selectFont('./fonts/Helvetica-Bold.afm');
426 $pdf->line($Left_Margin+310, $YPos+10,$Left_Margin+500, $YPos+10);
427 $pdf->line($Left_Margin+310, $YPos,$Left_Margin+500, $YPos);
429 if ($Section == 1) { /*Income*/
430 $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,200,$FontSize,$Sections[$Section]);
431 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format(-$SectionPrdActual),'right');
432 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format(-$SectionPrdBudget),'right');
433 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format(-$SectionPrdLY),'right');
434 $YPos -= (2 * $line_height);
436 $TotalIncome = -$SectionPrdActual;
437 $TotalBudgetIncome = -$SectionPrdBudget;
438 $TotalLYIncome = -$SectionPrdLY;
439 } else {
440 $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,60,$FontSize,$Sections[$Section]);
441 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format($SectionPrdActual),'right');
442 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format($SectionPrdBudget),'right');
443 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format($SectionPrdLY),'right');
444 $YPos -= (2 * $line_height);
446 if ($Section == 2){ /*Cost of Sales - need sub total for Gross Profit*/
447 $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,60,$FontSize,_('Gross Profit'));
448 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format($TotalIncome - $SectionPrdActual),'right');
449 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format($TotalBudgetIncome - $SectionPrdBudget),'right');
450 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format($TotalLYIncome - $SectionPrdLY),'right');
451 $YPos -= (2 * $line_height);
453 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format(100*($TotalIncome - $SectionPrdActual)/$TotalIncome,1) . '%','right');
454 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format(100*($TotalBudgetIncome - $SectionPrdBudget)/$TotalBudgetIncome,1) . '%','right');
455 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format(100*($TotalLYIncome - $SectionPrdLY)/$TotalLYIncome,1). '%','right');
456 $YPos -= (2 * $line_height);
460 $LeftOvers = $pdf->addTextWrap($Left_Margin,$YPos,60,$FontSize,_('Profit').' - '._('Loss'));
461 $LeftOvers = $pdf->addTextWrap($Left_Margin+310,$YPos,70,$FontSize,number_format(-$PeriodProfitLoss),'right');
462 $LeftOvers = $pdf->addTextWrap($Left_Margin+370,$YPos,70,$FontSize,number_format(-$PeriodBudgetProfitLoss),'right');
463 $LeftOvers = $pdf->addTextWrap($Left_Margin+430,$YPos,70,$FontSize,number_format(-$PeriodLYProfitLoss),'right');
465 $pdf->line($Left_Margin+310, $YPos+$line_height,$Left_Margin+500, $YPos+$line_height);
466 $pdf->line($Left_Margin+310, $YPos,$Left_Margin+500, $YPos);
468 $pdfcode = $pdf->output();
469 $len = strlen($pdfcode);
471 if ($len <= 20){
472 $title = _('Print Profit and Loss Error');
473 include('includes/header.inc');
474 echo '<p>';
475 prnMsg( _('There were no entries to print out for the selections specified') );
476 echo '<BR><A HREF="'. $rootpath.'/index.php?' . SID . '">'. _('Back to the menu'). '</A>';
477 include('includes/footer.inc');
478 exit;
479 } else {
480 header('Content-type: application/pdf');
481 header('Content-Length: ' . $len);
482 header('Content-Disposition: inline; filename=Profit_Loss.pdf');
483 header('Expires: 0');
484 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
485 header('Pragma: public');
486 $pdf->Stream();
488 exit;
490 } else {
492 include('includes/header.inc');
493 echo "<FORM METHOD='POST' ACTION=" . $_SERVER['PHP_SELF'] . '?' . SID . '>';
494 echo "<INPUT TYPE=HIDDEN NAME='FromPeriod' VALUE=" . $_POST['FromPeriod'] . "><INPUT TYPE=HIDDEN NAME='ToPeriod' VALUE=" . $_POST['ToPeriod'] . '>';
496 $NumberOfMonths = $_POST['ToPeriod'] - $_POST['FromPeriod'] + 1;
498 if ($NumberOfMonths >12){
499 echo '<P>';
500 prnMsg(_('A period up to 12 months in duration can be specified') . ' - ' . _('the system automatically shows a comparative for the same period from the previous year') . ' - ' . _('it cannot do this if a period of more than 12 months is specified') . '. ' . _('Please select an alternative period range'),'error');
501 include('includes/footer.inc');
502 exit;
505 $sql = 'SELECT lastdate_in_period FROM periods WHERE periodno=' . $_POST['ToPeriod'];
506 $PrdResult = DB_query($sql, $db);
507 $myrow = DB_fetch_row($PrdResult);
508 $PeriodToDate = MonthAndYearFromSQLDate($myrow[0]);
511 $SQL = 'SELECT accountgroups.sectioninaccounts,
512 accountgroups.parentgroupname,
513 accountgroups.groupname,
514 chartdetails.accountcode,
515 chartmaster.accountname,
516 Sum(CASE WHEN chartdetails.period=' . $_POST['FromPeriod'] . ' THEN chartdetails.bfwd ELSE 0 END) AS firstprdbfwd,
517 Sum(CASE WHEN chartdetails.period=' . $_POST['FromPeriod'] . ' THEN chartdetails.bfwdbudget ELSE 0 END) AS firstprdbudgetbfwd,
518 Sum(CASE WHEN chartdetails.period=' . $_POST['ToPeriod'] . ' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS lastprdcfwd,
519 Sum(CASE WHEN chartdetails.period=' . ($_POST['FromPeriod'] - 12) . ' THEN chartdetails.bfwd ELSE 0 END) AS lyfirstprdbfwd,
520 Sum(CASE WHEN chartdetails.period=' . ($_POST['ToPeriod']-12) . ' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS lylastprdcfwd,
521 Sum(CASE WHEN chartdetails.period=' . $_POST['ToPeriod'] . ' THEN chartdetails.bfwdbudget + chartdetails.budget ELSE 0 END) AS lastprdbudgetcfwd
522 FROM chartmaster INNER JOIN accountgroups
523 ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
524 ON chartmaster.accountcode= chartdetails.accountcode
525 WHERE accountgroups.pandl=1
526 GROUP BY accountgroups.sectioninaccounts,
527 accountgroups.parentgroupname,
528 accountgroups.groupname,
529 chartdetails.accountcode,
530 chartmaster.accountname,
531 accountgroups.sequenceintb
532 ORDER BY accountgroups.sectioninaccounts,
533 accountgroups.sequenceintb,
534 accountgroups.groupname,
535 accountgroups.sequenceintb,
536 chartdetails.accountcode';
538 $AccountsResult = DB_query($SQL,$db,_('No general ledger accounts were returned by the SQL because'),_('The SQL that failed was'));
540 echo '<CENTER><FONT SIZE=4 COLOR=BLUE><B>' . _('Statement of Profit and Loss for the'). ' ' . $NumberOfMonths . ' ' . _('months to'). ' ' . $PeriodToDate . '</B></FONT><BR>';
542 /*show a table of the accounts info returned by the SQL
543 Account Code , Account Name , Month Actual, Month Budget, Period Actual, Period Budget */
545 echo '<TABLE CELLPADDING=2>';
547 if ($_POST['Detail']=='Detailed'){
548 $TableHeader = "<TR>
549 <TD class='tableheader'>"._('Account')."</TD>
550 <TD class='tableheader'>"._('Account Name')."</TD>
551 <TD COLSPAN=2 class='tableheader'>"._('Period Actual')."</TD>
552 <TD COLSPAN=2 class='tableheader'>"._('Period Budget')."</TD>
553 <TD COLSPAN=2 class='tableheader'>"._('Last Year').'</TD>
554 </TR>';
555 } else { /*summary */
556 $TableHeader = "<TR>
557 <TD COLSPAN=2 class='tableheader'></TD>
558 <TD COLSPAN=2 class='tableheader'>"._('Period Actual')."</TD>
559 <TD COLSPAN=2 class='tableheader'>"._('Period Budget')."</TD>
560 <TD COLSPAN=2 class='tableheader'>"._('Last Year')."</TD>
561 </TR>";
565 $j = 1;
566 $k=0; //row colour counter
567 $Section='';
568 $SectionPrdActual= 0;
569 $SectionPrdLY = 0;
570 $SectionPrdBudget= 0;
572 $ActGrp ='';
573 $ParentGroups = array();
574 $Level = 0;
575 $ParentGroups[$Level]='';
576 $GrpPrdActual = array(0);
577 $GrpPrdLY = array(0);
578 $GrpPrdBudget = array(0);
581 while ($myrow=DB_fetch_array($AccountsResult)) {
584 if ($myrow['groupname']!= $ActGrp){
585 if ($myrow['parentgroupname']!=$ActGrp AND $ActGrp!=''){
586 while ($myrow['groupname']!=$ParentGroups[$Level] AND $Level>0) {
587 if ($_POST['Detail']=='Detailed'){
588 echo '<TR>
589 <TD COLSPAN=2></TD>
590 <TD COLSPAN=6><HR></TD>
591 </TR>';
592 $ActGrpLabel = str_repeat('___',$Level) . $ParentGroups[$Level] . ' ' . _('total');
593 } else {
594 $ActGrpLabel = str_repeat('___',$Level) . $ParentGroups[$Level];
596 if ($Section ==1){ /*Income */
597 printf('<TR>
598 <TD COLSPAN=2><FONT SIZE=2><I>%s </I></FONT></td>
599 <TD></TD>
600 <TD ALIGN=RIGHT>%s</TD>
601 <TD></TD>
602 <TD ALIGN=RIGHT>%s</TD>
603 <TD></TD>
604 <TD ALIGN=RIGHT>%s</TD>
605 </TR>',
606 $ActGrpLabel,
607 number_format(-$GrpPrdActual[$Level]),
608 number_format(-$GrpPrdBudget[$Level]),
609 number_format(-$GrpPrdLY[$Level]));
610 } else { /*Costs */
611 printf('<TR>
612 <TD COLSPAN=2><FONT SIZE=2><I>%s </I></FONT></td>
613 <TD ALIGN=RIGHT>%s</TD>
614 <TD></TD>
615 <TD ALIGN=RIGHT>%s</TD>
616 <TD></TD>
617 <TD ALIGN=RIGHT>%s</TD>
618 <TD></TD>
619 </TR>',
620 $ActGrpLabel,
621 number_format($GrpPrdActual[$Level]),
622 number_format($GrpPrdBudget[$Level]),
623 number_format($GrpPrdLY[$Level]));
625 $GrpPrdLY[$Level] = 0;
626 $GrpPrdActual[$Level] = 0;
627 $GrpPrdBudget[$Level] = 0;
628 $ParentGroups[$Level] ='';
629 $Level--;
630 }//end while
631 //still need to print out the old group totals
632 if ($_POST['Detail']=='Detailed'){
633 echo '<TR>
634 <TD COLSPAN=2></TD>
635 <TD COLSPAN=6><HR></TD>
636 </TR>';
637 $ActGrpLabel = str_repeat('___',$Level) . $ParentGroups[$Level] . ' ' . _('total');
638 } else {
639 $ActGrpLabel = str_repeat('___',$Level) . $ParentGroups[$Level];
642 if ($Section ==1){ /*Income */
643 printf('<TR>
644 <TD COLSPAN=2><FONT SIZE=2><I>%s </I></FONT></td>
645 <TD></TD>
646 <TD ALIGN=RIGHT>%s</TD>
647 <TD></TD>
648 <TD ALIGN=RIGHT>%s</TD>
649 <TD></TD>
650 <TD ALIGN=RIGHT>%s</TD>
651 </TR>',
652 $ActGrpLabel,
653 number_format(-$GrpPrdActual[$Level]),
654 number_format(-$GrpPrdBudget[$Level]),
655 number_format(-$GrpPrdLY[$Level]));
656 } else { /*Costs */
657 printf('<TR>
658 <TD COLSPAN=2><FONT SIZE=2><I>%s </I></FONT></td>
659 <TD ALIGN=RIGHT>%s</TD>
660 <TD></TD>
661 <TD ALIGN=RIGHT>%s</TD>
662 <TD></TD>
663 <TD ALIGN=RIGHT>%s</TD>
664 <TD></TD>
665 </TR>',
666 $ActGrpLabel,
667 number_format($GrpPrdActual[$Level]),
668 number_format($GrpPrdBudget[$Level]),
669 number_format($GrpPrdLY[$Level]));
671 $GrpPrdLY[$Level] = 0;
672 $GrpPrdActual[$Level] = 0;
673 $GrpPrdBudget[$Level] = 0;
674 $ParentGroups[$Level] ='';
676 $j++;
679 if ($myrow['sectioninaccounts']!= $Section){
681 if ($SectionPrdLY+$SectionPrdActual+$SectionPrdBudget !=0){
682 if ($Section==1) { /*Income*/
684 echo '<TR>
685 <TD COLSPAN=3></TD>
686 <TD><HR></TD>
687 <TD></TD>
688 <TD><HR></TD>
689 <TD></TD>
690 <TD><HR></TD>
691 </TR>';
693 printf('<TR>
694 <TD COLSPAN=2><FONT SIZE=4>%s</FONT></td>
695 <TD></TD>
696 <TD ALIGN=RIGHT>%s</TD>
697 <TD></TD>
698 <TD ALIGN=RIGHT>%s</TD>
699 <TD></TD>
700 <TD ALIGN=RIGHT>%s</TD>
701 </TR>',
702 $Sections[$Section],
703 number_format(-$SectionPrdActual),
704 number_format(-$SectionPrdBudget),
705 number_format(-$SectionPrdLY));
706 $TotalIncome = -$SectionPrdActual;
707 $TotalBudgetIncome = -$SectionPrdBudget;
708 $TotalLYIncome = -$SectionPrdLY;
709 } else {
710 echo '<TR>
711 <TD COLSPAN=2></TD>
712 <TD><HR></TD>
713 <TD></TD>
714 <TD><HR></TD>
715 <TD></TD>
716 <TD><HR></TD>
717 </TR>';
718 printf('<TR>
719 <TD COLSPAN=2><FONT SIZE=4>%s</FONT></td>
720 <TD></TD>
721 <TD ALIGN=RIGHT>%s</TD>
722 <TD></TD>
723 <TD ALIGN=RIGHT>%s</TD>
724 <TD></TD>
725 <TD ALIGN=RIGHT>%s</TD>
726 </TR>',
727 $Sections[$Section],
728 number_format($SectionPrdActual),
729 number_format($SectionPrdBudget),
730 number_format($SectionPrdLY));
732 if ($Section==2){ /*Cost of Sales - need sub total for Gross Profit*/
733 echo '<TR>
734 <TD COLSPAN=2></TD>
735 <TD COLSPAN=6><HR></TD>
736 </TR>';
737 printf('<TR>
738 <TD COLSPAN=2><FONT SIZE=4>'._('Gross Profit').'</FONT></td>
739 <TD></TD>
740 <TD ALIGN=RIGHT>%s</TD>
741 <TD></TD>
742 <TD ALIGN=RIGHT>%s</TD>
743 <TD></TD>
744 <TD ALIGN=RIGHT>%s</TD>
745 </TR>',
746 number_format($TotalIncome - $SectionPrdActual),
747 number_format($TotalBudgetIncome - $SectionPrdBudget),
748 number_format($TotalLYIncome - $SectionPrdLY));
750 if ($TotalIncome !=0){
751 $PrdGPPercent = 100*($TotalIncome - $SectionPrdActual)/$TotalIncome;
752 } else {
753 $PrdGPPercent =0;
755 if ($TotalBudgetIncome !=0){
756 $BudgetGPPercent = 100*($TotalBudgetIncome - $SectionPrdBudget)/$TotalBudgetIncome;
757 } else {
758 $BudgetGPPercent =0;
760 if ($TotalLYIncome !=0){
761 $LYGPPercent = 100*($TotalLYIncome - $SectionPrdLY)/$TotalLYIncome;
762 } else {
763 $LYGPPercent = 0;
765 echo '<TR>
766 <TD COLSPAN=2></TD>
767 <TD COLSPAN=6><HR></TD>
768 </TR>';
769 printf('<TR>
770 <TD COLSPAN=2><FONT SIZE=2><I>'._('Gross Profit Percent').'</I></FONT></td>
771 <TD></TD>
772 <TD ALIGN=RIGHT><I>%s</I></TD>
773 <TD></TD>
774 <TD ALIGN=RIGHT><I>%s</I></TD>
775 <TD></TD>
776 <TD ALIGN=RIGHT><I>%s</I></TD>
777 </TR><TR><TD COLSPAN=6> </TD></TR>',
778 number_format($PrdGPPercent,1) . '%',
779 number_format($BudgetGPPercent,1) . '%',
780 number_format($LYGPPercent,1). '%');
781 $j++;
784 $SectionPrdLY =0;
785 $SectionPrdActual =0;
786 $SectionPrdBudget =0;
788 $Section = $myrow['sectioninaccounts'];
790 if ($_POST['Detail']=='Detailed'){
791 printf('<TR>
792 <td COLSPAN=6><FONT SIZE=4 COLOR=BLUE><B>%s</B></FONT></TD>
793 </TR>',
794 $Sections[$myrow['sectioninaccounts']]);
796 $j++;
802 if ($myrow['groupname']!= $ActGrp){
804 if ($myrow['parentgroupname']==$ActGrp AND $ActGrp !=''){ //adding another level of nesting
805 $Level++;
808 $ParentGroups[$Level] = $myrow['groupname'];
809 $ActGrp = $myrow['groupname'];
810 if ($_POST['Detail']=='Detailed'){
811 printf('<TR>
812 <td COLSPAN=6><FONT SIZE=2 COLOR=BLUE><B>%s</B></FONT></TD>
813 </TR>',
814 $myrow['groupname']);
815 echo $TableHeader;
819 $AccountPeriodActual = $myrow['lastprdcfwd'] - $myrow['firstprdbfwd'];
820 $AccountPeriodLY = $myrow['lylastprdcfwd'] - $myrow['lyfirstprdbfwd'];
821 $AccountPeriodBudget = $myrow['lastprdbudgetcfwd'] - $myrow['firstprdbudgetbfwd'];
822 $PeriodProfitLoss += $AccountPeriodActual;
823 $PeriodBudgetProfitLoss += $AccountPeriodBudget;
824 $PeriodLYProfitLoss += $AccountPeriodLY;
826 for ($i=0;$i<=$Level;$i++){
827 $GrpPrdLY[$i] +=$AccountPeriodLY;
828 $GrpPrdActual[$i] +=$AccountPeriodActual;
829 $GrpPrdBudget[$i] +=$AccountPeriodBudget;
831 $SectionPrdLY +=$AccountPeriodLY;
832 $SectionPrdActual +=$AccountPeriodActual;
833 $SectionPrdBudget +=$AccountPeriodBudget;
835 if ($_POST['Detail']==_('Detailed')){
837 if ($k==1){
838 echo "<tr bgcolor='#CCCCCC'>";
839 $k=0;
840 } else {
841 echo "<tr bgcolor='#EEEEEE'>";
842 $k++;
845 $ActEnquiryURL = "<A HREF='$rootpath/GLAccountInquiry.php?" . SID . '&Period=' . $_POST['ToPeriod'] . '&Account=' . $myrow['accountcode'] . "&Show=Yes'>" . $myrow['accountcode'] . '<A>';
847 if ($Section ==1){
848 printf('<td>%s</td>
849 <td>%s</td>
850 <TD></TD>
851 <td ALIGN=RIGHT>%s</td>
852 <TD></TD>
853 <td ALIGN=RIGHT>%s</td>
854 <TD></TD>
855 <td ALIGN=RIGHT>%s</td>
856 </tr>',
857 $ActEnquiryURL,
858 $myrow['accountname'],
859 number_format(-$AccountPeriodActual),
860 number_format(-$AccountPeriodBudget),
861 number_format(-$AccountPeriodLY));
862 } else {
863 printf('<td>%s</td>
864 <td>%s</td>
865 <td ALIGN=RIGHT>%s</td>
866 <TD></TD>
867 <td ALIGN=RIGHT>%s</td>
868 <TD></TD>
869 <td ALIGN=RIGHT>%s</td>
870 <TD></TD>
871 </tr>',
872 $ActEnquiryURL,
873 $myrow['accountname'],
874 number_format($AccountPeriodActual),
875 number_format($AccountPeriodBudget),
876 number_format($AccountPeriodLY));
879 $j++;
882 //end of loop
885 if ($myrow['groupname']!= $ActGrp){
886 if ($myrow['parentgroupname']!=$ActGrp AND $ActGrp!=''){
887 while ($myrow['groupname']!=$ParentGroups[$Level] AND $Level>0) {
888 if ($_POST['Detail']=='Detailed'){
889 echo '<TR>
890 <TD COLSPAN=2></TD>
891 <TD COLSPAN=6><HR></TD>
892 </TR>';
893 $ActGrpLabel = str_repeat('___',$Level) . $ParentGroups[$Level] . ' ' . _('total');
894 } else {
895 $ActGrpLabel = str_repeat('___',$Level) . $ParentGroups[$Level];
897 if ($Section ==1){ /*Income */
898 printf('<TR>
899 <TD COLSPAN=2><FONT SIZE=2><I>%s </I></FONT></td>
900 <TD></TD>
901 <TD ALIGN=RIGHT>%s</TD>
902 <TD></TD>
903 <TD ALIGN=RIGHT>%s</TD>
904 <TD></TD>
905 <TD ALIGN=RIGHT>%s</TD>
906 </TR>',
907 $ActGrpLabel,
908 number_format(-$GrpPrdActual[$Level]),
909 number_format(-$GrpPrdBudget[$Level]),
910 number_format(-$GrpPrdLY[$Level]));
911 } else { /*Costs */
912 printf('<TR>
913 <TD COLSPAN=2><FONT SIZE=2><I>%s </I></FONT></td>
914 <TD ALIGN=RIGHT>%s</TD>
915 <TD></TD>
916 <TD ALIGN=RIGHT>%s</TD>
917 <TD></TD>
918 <TD ALIGN=RIGHT>%s</TD>
919 <TD></TD>
920 </TR>',
921 $ActGrpLabel,
922 number_format($GrpPrdActual[$Level]),
923 number_format($GrpPrdBudget[$Level]),
924 number_format($GrpPrdLY[$Level]));
926 $GrpPrdLY[$Level] = 0;
927 $GrpPrdActual[$Level] = 0;
928 $GrpPrdBudget[$Level] = 0;
929 $ParentGroups[$Level] ='';
930 $Level--;
931 }//end while
932 //still need to print out the old group totals
933 if ($_POST['Detail']=='Detailed'){
934 echo '<TR>
935 <TD COLSPAN=2></TD>
936 <TD COLSPAN=6><HR></TD>
937 </TR>';
938 $ActGrpLabel = str_repeat('___',$Level) . $ParentGroups[$Level] . ' ' . _('total');
939 } else {
940 $ActGrpLabel = str_repeat('___',$Level) . $ParentGroups[$Level];
943 if ($Section ==1){ /*Income */
944 printf('<TR>
945 <TD COLSPAN=2><FONT SIZE=2><I>%s </I></FONT></td>
946 <TD></TD>
947 <TD ALIGN=RIGHT>%s</TD>
948 <TD></TD>
949 <TD ALIGN=RIGHT>%s</TD>
950 <TD></TD>
951 <TD ALIGN=RIGHT>%s</TD>
952 </TR>',
953 $ActGrpLabel,
954 number_format(-$GrpPrdActual[$Level]),
955 number_format(-$GrpPrdBudget[$Level]),
956 number_format(-$GrpPrdLY[$Level]));
957 } else { /*Costs */
958 printf('<TR>
959 <TD COLSPAN=2><FONT SIZE=2><I>%s </I></FONT></td>
960 <TD ALIGN=RIGHT>%s</TD>
961 <TD></TD>
962 <TD ALIGN=RIGHT>%s</TD>
963 <TD></TD>
964 <TD ALIGN=RIGHT>%s</TD>
965 <TD></TD>
966 </TR>',
967 $ActGrpLabel,
968 number_format($GrpPrdActual[$Level]),
969 number_format($GrpPrdBudget[$Level]),
970 number_format($GrpPrdLY[$Level]));
972 $GrpPrdLY[$Level] = 0;
973 $GrpPrdActual[$Level] = 0;
974 $GrpPrdBudget[$Level] = 0;
975 $ParentGroups[$Level] ='';
977 $j++;
980 if ($myrow['sectioninaccounts']!= $Section){
982 if ($Section==1) { /*Income*/
984 echo '<TR>
985 <TD COLSPAN=3></TD>
986 <TD><HR></TD>
987 <TD></TD>
988 <TD><HR></TD>
989 <TD></TD>
990 <TD><HR></TD>
991 </TR>';
993 printf('<TR>
994 <TD COLSPAN=2><FONT SIZE=4>%s</FONT></td>
995 <TD></TD>
996 <TD ALIGN=RIGHT>%s</TD>
997 <TD></TD>
998 <TD ALIGN=RIGHT>%s</TD>
999 <TD></TD>
1000 <TD ALIGN=RIGHT>%s</TD>
1001 </TR>',
1002 $Sections[$Section],
1003 number_format(-$SectionPrdActual),
1004 number_format(-$SectionPrdBudget),
1005 number_format(-$SectionPrdLY));
1006 $TotalIncome = -$SectionPrdActual;
1007 $TotalBudgetIncome = -$SectionPrdBudget;
1008 $TotalLYIncome = -$SectionPrdLY;
1009 } else {
1010 echo '<TR>
1011 <TD COLSPAN=2></TD>
1012 <TD><HR></TD>
1013 <TD></TD>
1014 <TD><HR></TD>
1015 <TD></TD>
1016 <TD><HR></TD>
1017 </TR>';
1018 printf('<TR>
1019 <TD COLSPAN=2><FONT SIZE=4>%s</FONT></td>
1020 <TD></TD>
1021 <TD ALIGN=RIGHT>%s</TD>
1022 <TD></TD>
1023 <TD ALIGN=RIGHT>%s</TD>
1024 <TD></TD>
1025 <TD ALIGN=RIGHT>%s</TD>
1026 </TR>',
1027 $Sections[$Section],
1028 number_format($SectionPrdActual),
1029 number_format($SectionPrdBudget),
1030 number_format($SectionPrdLY));
1032 if ($Section==2){ /*Cost of Sales - need sub total for Gross Profit*/
1033 echo '<TR>
1034 <TD COLSPAN=2></TD>
1035 <TD COLSPAN=6><HR></TD>
1036 </TR>';
1037 printf('<TR>
1038 <TD COLSPAN=2><FONT SIZE=4>'._('Gross Profit').'</FONT></td>
1039 <TD></TD>
1040 <TD ALIGN=RIGHT>%s</TD>
1041 <TD></TD>
1042 <TD ALIGN=RIGHT>%s</TD>
1043 <TD></TD>
1044 <TD ALIGN=RIGHT>%s</TD>
1045 </TR>',
1046 number_format($TotalIncome - $SectionPrdActual),
1047 number_format($TotalBudgetIncome - $SectionPrdBudget),
1048 number_format($TotalLYIncome - $SectionPrdLY));
1050 if ($TotalIncome !=0){
1051 $PrdGPPercent = 100*($TotalIncome - $SectionPrdActual)/$TotalIncome;
1052 } else {
1053 $PrdGPPercent =0;
1055 if ($TotalBudgetIncome !=0){
1056 $BudgetGPPercent = 100*($TotalBudgetIncome - $SectionPrdBudget)/$TotalBudgetIncome;
1057 } else {
1058 $BudgetGPPercent =0;
1060 if ($TotalLYIncome !=0){
1061 $LYGPPercent = 100*($TotalLYIncome - $SectionPrdLY)/$TotalLYIncome;
1062 } else {
1063 $LYGPPercent = 0;
1065 echo '<TR>
1066 <TD COLSPAN=2></TD>
1067 <TD COLSPAN=6><HR></TD>
1068 </TR>';
1069 printf('<TR>
1070 <TD COLSPAN=2><FONT SIZE=2><I>'._('Gross Profit Percent').'</I></FONT></td>
1071 <TD></TD>
1072 <TD ALIGN=RIGHT><I>%s</I></TD>
1073 <TD></TD>
1074 <TD ALIGN=RIGHT><I>%s</I></TD>
1075 <TD></TD>
1076 <TD ALIGN=RIGHT><I>%s</I></TD>
1077 </TR><TR><TD COLSPAN=6> </TD></TR>',
1078 number_format($PrdGPPercent,1) . '%',
1079 number_format($BudgetGPPercent,1) . '%',
1080 number_format($LYGPPercent,1). '%');
1081 $j++;
1084 $SectionPrdLY =0;
1085 $SectionPrdActual =0;
1086 $SectionPrdBudget =0;
1088 $Section = $myrow['sectioninaccounts'];
1090 if ($_POST['Detail']=='Detailed'){
1091 printf('<TR>
1092 <td COLSPAN=6><FONT SIZE=4 COLOR=BLUE><B>%s</B></FONT></TD>
1093 </TR>',
1094 $Sections[$myrow['sectioninaccounts']]);
1096 $j++;
1100 echo '<TR>
1101 <TD COLSPAN=2></TD>
1102 <TD COLSPAN=6><HR></TD>
1103 </TR>';
1105 printf("<tr bgcolor='#ffffff'>
1106 <td COLSPAN=2><FONT SIZE=4 COLOR=BLUE><B>"._('Profit').' - '._('Loss')."</B></FONT></td>
1107 <TD></TD>
1108 <td ALIGN=RIGHT>%s</td>
1109 <TD></TD>
1110 <td ALIGN=RIGHT>%s</td>
1111 <TD></TD>
1112 <td ALIGN=RIGHT>%s</td>
1113 </tr>",
1114 number_format(-$PeriodProfitLoss),
1115 number_format(-$PeriodBudgetProfitLoss),
1116 number_format(-$PeriodLYProfitLoss)
1119 echo '<TR>
1120 <TD COLSPAN=2></TD>
1121 <TD COLSPAN=6><HR></TD>
1122 </TR>';
1124 echo '</TABLE>';
1125 echo "<INPUT TYPE=SUBMIT Name='SelectADifferentPeriod' Value='"._('Select A Different Period')."'></CENTER>";
1127 echo '</FORM>';
1128 include('includes/footer.inc');