forked from timschofield/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLProfit_Loss.php
702 lines (645 loc) · 27.5 KB
/
GLProfit_Loss.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
<?php
// GLProfit_Loss.php
// Shows the profit and loss of the company for the range of periods entered.
/*
Info about financial statements: IAS 1 - Presentation of Financial Statements.
Parameters:
PeriodFrom: Select the beginning of the reporting period.
PeriodTo: Select the end of the reporting period.
Period: Select a period instead of using the beginning and end of the reporting period.
{ ShowBudget: Check this box to show the budget for the period. Not used in this script.}
ShowDetail: Check this box to show all accounts instead a summary.
ShowZeroBalance: Check this box to show all accounts including those with zero balance.
NewReport: Click this button to start a new report.
IsIncluded: Parameter to indicate that a script is included within another.
*/
// BEGIN: Functions division ===================================================
// END: Functions division =====================================================
// BEGIN: Procedure division ===================================================
include('includes/session.php');
use Dompdf\Dompdf;
$Title = _('Profit and Loss');
$Title2 = _('Statement of Comprehensive Income');// Name as IAS.
$ViewTopic= 'GeneralLedger';
$BookMark = 'ProfitAndLoss';
include_once('includes/SQL_CommonFunctions.inc');
include_once('includes/AccountSectionsDef.php'); // This loads the $Sections variable
include_once('includes/CurrenciesArray.php');// Array to retrieve currency name.
if (isset($_POST['PrintPDF']) or isset($_POST['View'])) {
// Initialize SelectedBudget if not set
if (!isset($_POST['SelectedBudget'])) {
$_POST['SelectedBudget'] = 0; // Default value when not selected
}
// Sets PeriodFrom and PeriodTo from Period:
if(isset($_POST['Period']) and $_POST['Period'] != '') {
$_POST['PeriodFrom'] = ReportPeriod($_POST['Period'] , 'From');
$_POST['PeriodTo'] = ReportPeriod($_POST['Period'] , 'To');
}
$NumberOfMonths = $_POST['PeriodTo'] - $_POST['PeriodFrom'] + 1;
$SQL = "SELECT lastdate_in_period FROM periods WHERE periodno='" . $_POST['PeriodTo'] . "'";
$PrdResult = DB_query($SQL);
$MyRow = DB_fetch_row($PrdResult);
$PeriodToDate = MonthAndYearFromSQLDate($MyRow[0]);
$HTML = '';
if (isset($_POST['PrintPDF'])) {
$HTML .= '<html>
<head>';
$HTML .= '<link href="css/reports.css" rel="stylesheet" type="text/css" />';
}
$HTML .= '<meta name="author" content="WebERP ' . $Version . '>
<meta name="Creator" content="webERP //www.weberp.org">
</head>
<body>';
// _('From') . ' ' . $PeriodFromDate? . ' ' . _('to') . ' ' . $PeriodToDate . '<br />'; // Page title . reporting period.
$HTML .= '<table class="selection">'.
// Content of the header and footer of the output table:
'<thead>
<tr>';
if ($_POST['ShowDetail']=='Detailed') {
$HTML .= '<th>' . _('Account') . '</th><th>' . _('Account Name') . '</th>';
} else { /*summary */
$HTML .= '<th colspan="2"> </th>';
}
$HTML .= '<table summary="' . _('General Ledger Profit Loss Inquiry') . '">
<thead>
<tr>
<th colspan="10">
<b>' . _('General Ledger Profit Loss Inquiry') . '</b>
</th>
</tr>';
if ($_POST['ShowDetail'] == 'Detailed') {
$HTML .= '<tr>
<th>' . _('Account') . '</th>
<th>' . _('Account Name') . '</th>
<th colspan="2">' . _('Period Actual') . '</th>
<th colspan="2">' . _('Period Budget') . '</th>
<th colspan="2">' . _('Last Year') . '</th>
</tr>';
} else {
/*summary */
$HTML .= '<tr>
<th colspan="2"></th>
<th colspan="2">' . _('Period Actual') . '</th>
<th colspan="2">' . _('Period Budget') . '</th>
<th colspan="2">' . _('Last Year') . '</th>
</tr>';
}
$HTML .= '</thead>';
$j = 1;
$Section = '';
$SectionPrdActual = 0;
$SectionPrdLY = 0;
$SectionPrdBudget = 0;
$PeriodProfitLoss = 0;
$PeriodProfitLoss = 0;
$PeriodLYProfitLoss = 0;
$PeriodBudgetProfitLoss = 0;
$ActGrp = '';
$ParentGroups = array();
$Level = 0;
$ParentGroups[$Level] = '';
$GrpPrdActual = array(0);
$GrpPrdLY = array(0);
$GrpPrdBudget = array(0);
$TotalIncome = 0;
$TotalBudgetIncome = 0;
$TotalLYIncome = 0;
$PeriodProfitLossActual = 0;
$PeriodProfitLossBudget = 0;
$PeriodProfitLossLY = 0;
// Get all account codes
$SQL = "SELECT sectionid,
sectionname,
parentgroupname,
chartmaster.group_,
chartmaster.accountcode,
group_,
accountname,
pandl
FROM chartmaster
INNER JOIN glaccountusers
ON glaccountusers.accountcode=chartmaster.accountcode
AND glaccountusers.userid='" . $_SESSION['UserID'] . "'
AND glaccountusers.canview=1
INNER JOIN accountgroups
ON accountgroups.groupname=chartmaster.group_
INNER JOIN accountsection
ON accountsection.sectionid=accountgroups.sectioninaccounts
WHERE pandl=1
ORDER BY sequenceintb,
group_,
accountcode";
$AccountListResult = DB_query($SQL);
$SQL = "SELECT account,
SUM(amount) AS accounttotal
FROM gltotals
WHERE period>='" . $_POST['PeriodFrom'] . "'
AND period<='" . $_POST['PeriodTo'] . "'
GROUP BY account
ORDER BY account";
$Result = DB_query($SQL);
$ThisYearActuals = array();
while ($MyRow = DB_fetch_array($Result)) {
$ThisYearActuals[$MyRow['account']] = $MyRow['accounttotal'];
}
$SQL = "SELECT account,
SUM(amount) AS accounttotal
FROM gltotals
WHERE period>='" . ($_POST['PeriodFrom'] - 12) . "'
AND period<='" . ($_POST['PeriodTo'] - 12) . "'
GROUP BY account
ORDER BY account";
$Result = DB_query($SQL);
$LastYearActuals = array();
while ($MyRow = DB_fetch_array($Result)) {
$LastYearActuals[$MyRow['account']] = $MyRow['accounttotal'];
}
while ($MyRow = DB_fetch_array($AccountListResult)) {
$SQL = "SELECT SUM(amount) AS periodbudget
FROM glbudgetdetails
WHERE account='" . $MyRow['accountcode'] . "'
AND period>='" . $_POST['PeriodFrom'] . "'
AND period<='" . $_POST['PeriodTo'] . "'
AND headerid='" . $_POST['SelectedBudget'] . "'";
$PeriodBudgetResult = DB_query($SQL);
$PeriodBudgetRow = DB_fetch_array($PeriodBudgetResult);
if (!isset($PeriodBudgetRow['periodbudget'])) {
$PeriodBudgetRow['periodbudget'] = 0;
}
if ($MyRow['group_'] != $ActGrp) {
if ($MyRow['parentgroupname'] != $ActGrp and $ActGrp != '') {
while ($MyRow['group_'] != $ParentGroups[$Level] and $Level > 0) {
if ($_POST['ShowDetail'] == 'Detailed') {
$HTML .= '<tr>
<td colspan="2"></td>
<td colspan="6"><hr /></td>
</tr>';
$ActGrpLabel = str_repeat('___', $Level) . $ParentGroups[$Level] . ' ' . _('total');
} else {
$ActGrpLabel = str_repeat('___', $Level) . $ParentGroups[$Level];
}
if ($Section == 1) { /*Income */
$HTML .= '<tr>
<td colspan="2"><h4><i>' . $ActGrpLabel . '</i></h4></td>
<td> </td>
<td class="number">' . locale_number_format(-$GrpPrdActual[$Level], $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$GrpPrdBudget[$Level], $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$GrpPrdLY[$Level], $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
</tr>';
} else { /*Costs */
$HTML .= '<tr>
<td colspan="2"><h4><i>' . $ActGrpLabel . '</i></h4></td>
<td class="number">' . locale_number_format($GrpPrdActual[$Level], $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format($GrpPrdBudget[$Level], $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format($GrpPrdLY[$Level], $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
</tr>';
}
$GrpPrdActual[$Level] = 0;
$GrpPrdBudget[$Level] = 0;
$GrpPrdLY[$Level] = 0;
$ParentGroups[$Level] = '';
$Level--;
} //end while
//still need to print out the old group totals
if ($_POST['ShowDetail'] == 'Detailed') {
$HTML .= '<tr>
<td colspan="2"></td>
<td colspan="6"><hr /></td>
</tr>';
$ActGrpLabel = str_repeat('___', $Level) . $ParentGroups[$Level] . ' ' . _('total');
} else {
$ActGrpLabel = str_repeat('___', $Level) . $ParentGroups[$Level];
}
if ($Section == 1) { /*Income */
$HTML .= '<tr class="total_row">
<td colspan="2"><h4><i>' . $ActGrpLabel . '</i></h4></td>
<td> </td>
<td class="number">' . locale_number_format(-$GrpPrdActual[$Level], $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$GrpPrdBudget[$Level], $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$GrpPrdLY[$Level], $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
</tr>';
} else { /*Costs */
$HTML .= '<tr class="total_row">
<td colspan="2"><h4><i>' . $ActGrpLabel . '</i></h4></td>
<td class="number">' . locale_number_format($GrpPrdActual[$Level], $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format($GrpPrdBudget[$Level], $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format($GrpPrdLY[$Level], $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
</tr>';
}
$GrpPrdLY[$Level] = 0;
$GrpPrdActual[$Level] = 0;
$GrpPrdBudget[$Level] = 0;
$ParentGroups[$Level] = '';
}
}
if ($MyRow['sectionid'] != $Section) {
if ($SectionPrdLY + $SectionPrdActual + $SectionPrdBudget != 0) {
if ($Section == 1) { /*Income*/
$HTML .= '<tr>
<td colspan="3"></td>
<td><hr /></td>
<td> </td>
<td><hr /></td>
<td> </td>
<td><hr /></td>
</tr>' . '<tr class="total_row">
<td colspan="2"><h2>' . $Sections[$Section] . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$SectionPrdActual, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$SectionPrdBudget, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$SectionPrdLY, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
</tr>';
$TotalIncomeActual = - $SectionPrdActual;
$TotalIncomeBudget = - $SectionPrdBudget;
$TotalIncomeLY = - $SectionPrdLY;
} else {
$HTML .= '<tr>
<td colspan="2"></td>
<td><hr /></td>
<td> </td>
<td><hr /></td>
<td> </td>
<td><hr /></td>
</tr><tr class="total_row">
<td colspan="2"><h2>' . $Sections[$Section] . '</h2></td>
<td> </td>
<td class="number">' . locale_number_format($SectionPrdActual, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format($SectionPrdBudget, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format($SectionPrdLY, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
</tr>';
}
if ($Section == 2) { /*Cost of Sales - need sub total for Gross Profit*/
$HTML .= '<tr>
<td colspan="2"></td>
<td colspan="6"><hr /></td>
</tr><tr class="total_row">
<td colspan="2"><h2>' . _('Gross Profit') . '</h2></td>
<td> </td>
<td class="number">' . locale_number_format($TotalIncomeActual - $SectionPrdActual, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format($TotalIncomeBudget - $SectionPrdBudget, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format($TotalIncomeLY - $SectionPrdLY, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
</tr>';
if ($TotalIncomeActual != 0) {
$GPPercentActual = ($TotalIncomeActual - $SectionPrdActual) / $TotalIncomeActual * 100;
} else {
$GPPercentActual = 0;
}
if ($TotalIncomeBudget != 0) {
$GPPercentBudget = ($TotalIncomeBudget - $SectionPrdBudget) / $TotalIncomeBudget * 100;
} else {
$GPPercentBudget = 0;
}
if ($TotalIncomeLY != 0) {
$GPPercentLY = ($TotalIncomeLY - $SectionPrdLY) / $TotalIncomeLY * 100;
} else {
$GPPercentLY = 0;
}
$HTML .= '<tr>
<td colspan="2"></td>
<td colspan="6"><hr /></td>
</tr><tr class="total_row">
<td colspan="2"><h4><i>' . _('Gross Profit Percent') . '</i></h4></td>
<td> </td>
<td class="number"><i>' . locale_number_format($GPPercentActual, 1) . '%</i></td>
<td> </td>
<td class="number"><i>' . locale_number_format($GPPercentBudget, 1) . '%</i></td>
<td> </td>
<td class="number"><i>' . locale_number_format($GPPercentLY, 1) . '%</i></td>
</tr>
<tr><td colspan="6"> </td></tr>';
}
if (($Section != 1) and ($Section != 2)) {
$HTML .= '<tr>
<td colspan="2"></td>
<td colspan="6"><hr /></td>
</tr><tr class="total_row">
<td colspan="2"><h4><b>' . _('Profit') . ' - ' . _('Loss') . ' ' . _('after') . ' ' . $Sections[$Section] . '</b></h2></td>
<td> </td>
<td class="number">' . locale_number_format(-$PeriodProfitLossActual, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$PeriodProfitLossBudget, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$PeriodProfitLossLY, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
</tr>';
if ($TotalIncomeActual != 0) {
$NPPercentActual = (-$PeriodProfitLossActual) / $TotalIncomeActual * 100;
} else {
$NPPercentActual = 0;
}
if ($TotalIncomeBudget != 0) {
$NPPercentBudget = (-$PeriodProfitLossBudget) / $TotalIncomeBudget * 100;
} else {
$NPPercentBudget = 0;
}
if ($TotalIncomeLY != 0) {
$NPPercentLY = (-$PeriodProfitLossLY) / $TotalIncomeLY * 100;
} else {
$NPPercentLY = 0;
}
$HTML .= '<tr class="total_row">
<td colspan="2"><h4><i>' . _('P/L Percent after') . ' ' . $Sections[$Section] . '</i></h4></td>
<td> </td>
<td class="number"><i>' . locale_number_format($NPPercentActual, 1) . '%</i></td>
<td> </td>
<td class="number"><i>' . locale_number_format($NPPercentBudget, 1). '%</i></td>
<td> </td>
<td class="number"><i>' . locale_number_format($NPPercentLY, 1) . '%</i></td>
</tr>
<tr><td colspan="6"> </td></tr>' . '<tr>
<td colspan="2"></td>
<td colspan="6"><hr /></td>
</tr>';
}
}
$SectionPrdActual = 0;
$SectionPrdBudget = 0;
$SectionPrdLY = 0;
$Section = $MyRow['sectionid'];
if ($_POST['ShowDetail'] == 'Detailed') {
$HTML .= '<tr>
<td colspan="6"><h2><b>' . $Sections[$MyRow['sectionid']] . '</b></h2></td>
</tr>';
}
}
if ($MyRow['group_'] != $ActGrp) {
if ($MyRow['parentgroupname'] == $ActGrp and $ActGrp != '') { //adding another level of nesting
$Level++;
}
$ParentGroups[$Level] = $MyRow['group_'];
$ActGrp = $MyRow['group_'];
if ($_POST['ShowDetail'] == 'Detailed') {
$HTML .= '<tr>
<td colspan="8"><b>' . $MyRow['group_'] . '</b></td>
</tr>';
}
}
$AccountPeriodActual = $ThisYearActuals[$MyRow['accountcode']];
$AccountPeriodBudget = $PeriodBudgetRow['periodbudget'];
$AccountPeriodLY = $LastYearActuals[$MyRow['accountcode']];
$PeriodProfitLossActual+= $AccountPeriodActual;
$PeriodProfitLossBudget+= $AccountPeriodBudget;
$PeriodProfitLossLY+= $AccountPeriodLY;
for ($i = 0;$i <= $Level;$i++) {
if (!isset($GrpPrdActual[$i])) {
$GrpPrdActual[$i] = 0;
}
$GrpPrdActual[$i]+= $AccountPeriodActual;
if (!isset($GrpPrdBudget[$i])) {
$GrpPrdBudget[$i] = 0;
}
$GrpPrdBudget[$i]+= $AccountPeriodBudget;
if (!isset($GrpPrdLY[$i])) {
$GrpPrdLY[$i] = 0;
}
$GrpPrdLY[$i]+= $AccountPeriodLY;
}
$SectionPrdActual+= $AccountPeriodActual;
$SectionPrdBudget+= $AccountPeriodBudget;
$SectionPrdLY+= $AccountPeriodLY;
if ($_POST['ShowDetail'] == 'Detailed') {
if (isset($_POST['ShowZeroBalance']) or (!isset($_POST['ShowZeroBalance']) and ($AccountPeriodActual <> 0 or $AccountPeriodBudget <> 0 or $AccountPeriodLY <> 0))) {
$ActEnquiryURL = '<a href="' . $RootPath . '/GLAccountInquiry.php?PeriodFrom=' . urlencode($_POST['PeriodFrom']) . '&PeriodTo=' . urlencode($_POST['PeriodTo']) . '&Account=' . urlencode($MyRow['accountcode']) . '&Show=Yes">' . $MyRow['accountcode'] . '</a>';
if ($Section == 1) {
$HTML .= '<tr class="striped_row">
<td>' . $ActEnquiryURL . '</td>
<td>' . htmlspecialchars($MyRow['accountname'], ENT_QUOTES, 'UTF-8', false) . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$AccountPeriodActual, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$AccountPeriodBudget, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$AccountPeriodLY, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
</tr>';
} else {
$HTML .= '<tr class="striped_row">
<td>' . $ActEnquiryURL . '</td>
<td>' . htmlspecialchars($MyRow['accountname'], ENT_QUOTES, 'UTF-8', false) . '</td>
<td class="number">' . locale_number_format($AccountPeriodActual, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format($AccountPeriodBudget, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format($AccountPeriodLY, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
</tr>';
}
}
}
}
//end of loop
$HTML .= '<tr>
<td colspan="2"></td>
<td colspan="6"><hr /></td>
</tr>';
$HTML .= '<tr class="total_row">
<td colspan="2"><h2><b>' . _('Profit') . ' - ' . _('Loss') . '</b></h2></td>
<td> </td>
<td class="number">' . locale_number_format(-$PeriodProfitLossActual, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$PeriodProfitLossBudget, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td> </td>
<td class="number">' . locale_number_format(-$PeriodProfitLossLY, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
</tr>';
if ($TotalIncomeActual != 0) {
$NPPercentActual = (-$PeriodProfitLossActual) / $TotalIncomeActual * 100;
} else {
$NPPercentActual = 0;
}
if ($TotalIncomeBudget != 0) {
$NPPercentBudget = (-$PeriodProfitLossBudget) / $TotalIncomeBudget * 100;
} else {
$NPPercentBudget = 0;
}
if ($TotalIncomeLY != 0) {
$NPPercentLY = (-$PeriodProfitLossLY) / $TotalIncomeLY * 100;
} else {
$NPPercentLY = 0;
}
$HTML .= '<tr>
<td colspan="2"></td>
<td colspan="6"><hr /></td>
</tr><tr class="total_row">
<td colspan="2"><h4><i>' . _('Net Profit Percent') . '</i></h4></td>
<td> </td>
<td class="number"><i>' . locale_number_format($NPPercentActual, 1) . '%</i></td>
<td> </td>
<td class="number"><i>' . locale_number_format($NPPercentBudget, 1) . '%</i></td>
<td> </td>
<td class="number"><i>' . locale_number_format($NPPercentLY, 1) . '%</i></td>
</tr>
<tr><td colspan="6"> </td>
</tr><tr>
<td colspan="2"></td>
<td colspan="6"><hr /></td>
</tr>
</tbody></table></div>'; // div id="Report".
$HTML .= '</table>';
if (isset($_POST['PrintPDF'])) {
$HTML .= '</body></html>';
$dompdf = new Dompdf(['chroot' => __DIR__]);
$dompdf->loadHtml($HTML);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper($_SESSION['PageSize'], 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream($_SESSION['DatabaseName'] . '_Profit_Loss_' . date('Y-m-d') . '.pdf', array(
"Attachment" => false
));
} else {
$Title = _('General Ledger Profit and Loss');
include('includes/header.php');
echo '<p class="page_title_text">
<img src="' . $RootPath . '/css/' . $Theme . '/images/gl.png" title="' . _('Profit and Loss Report') . '" alt="" />
' . _('Profit and Loss Report') . '
</p>';
echo $HTML;
echo // Shows a form to select an action after the report was shown:
'<form action="' . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '" method="post">',
'<input name="FormID" type="hidden" value="' . $_SESSION['FormID'] . '" />',
// Resend report parameters:
'<input name="PeriodFrom" type="hidden" value="' . $_POST['PeriodFrom'] . '" />',
'<input name="PeriodTo" type="hidden" value="' . $_POST['PeriodTo'] . '" />',
'<div class="centre">
<input type="submit" name="close" value="' . _('Close') . '" onclick="window.close()" />
</div>' .
'</form>';
}
} else {
include('includes/header.php');
echo '<p class="page_title_text"><img alt="" src="' . $RootPath . '/css/' . $Theme . '/images/printer.png" title="' . // Icon image.
$Title2 . '" /> ' . // Icon title.
$Title . '</p>';// Page title.
fShowPageHelp(// Shows the page help text if $_SESSION['ShowFieldHelp'] is TRUE or is not set
_('Profit and loss statement (P&L) . also called an Income Statement . or Statement of Operations . this is the statement that indicates how the revenue (money received from the sale of products and services before expenses are taken out . also known as the top line) is transformed into the net income (the result after all revenues and expenses have been accounted for . also known as the bottom line).') . '<br />' .
_('The purpose of the income statement is to show whether the company made or lost money during the period being reported.') . '<br />' .
_('The P&L represents a period of time. This contrasts with the Balance Sheet . which represents a single moment in time.') . '<br />' .
_('webERP is an accrual based system (not a cash based system). Accrual systems include items when they are invoiced to the customer . and when expenses are owed based on the supplier invoice date.'));// Function fShowPageHelp() in ~/includes/MiscFunctions.php
echo // Shows a form to input the report parameters:
'<form action="' . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '" method="post" target="_blank">',
'<input name="FormID" type="hidden" value="' . $_SESSION['FormID'] . '" />',
// Input table:
'<fieldset>
<legend>' . _('Report Criteria') . '</legend>',
// Content of the body of the input table:
// Select period from:
'<field>
<label for="PeriodFrom">' . _('Select period from') . '</label>
<select id="PeriodFrom" name="PeriodFrom" required="required">';
$Periods = DB_query('SELECT periodno, lastdate_in_period FROM periods ORDER BY periodno DESC');
if (Date('m') > $_SESSION['YearEnd']) {
/*Dates in SQL format */
$DefaultFromDate = Date ('Y-m-d', Mktime(0,0,0, $_SESSION['YearEnd'] + 2,0,Date('Y')));
$FromDate = Date($_SESSION['DefaultDateFormat'], Mktime(0,0,0, $_SESSION['YearEnd'] + 2,0,Date('Y')));
} else {
$DefaultFromDate = Date ('Y-m-d', Mktime(0,0,0, $_SESSION['YearEnd'] + 2,0,Date('Y')-1));
$FromDate = Date($_SESSION['DefaultDateFormat'], Mktime(0,0,0, $_SESSION['YearEnd'] + 2,0,Date('Y')-1));
}
$Period = GetPeriod($FromDate);
while ($MyRow=DB_fetch_array($Periods)) {
if(isset($_POST['PeriodFrom']) AND $_POST['PeriodFrom']!='') {
if( $_POST['PeriodFrom']== $MyRow['periodno']) {
echo '<option selected="selected" value="' . $MyRow['periodno'] . '">' .MonthAndYearFromSQLDate($MyRow['lastdate_in_period']) . '</option>';
} else {
echo '<option value="' . $MyRow['periodno'] . '">' . MonthAndYearFromSQLDate($MyRow['lastdate_in_period']) . '</option>';
}
} else {
if($MyRow['lastdate_in_period']== $DefaultFromDate) {
echo '<option selected="selected" value="' . $MyRow['periodno'] . '">' . MonthAndYearFromSQLDate($MyRow['lastdate_in_period']) . '</option>';
} else {
echo '<option value="' . $MyRow['periodno'] . '">' . MonthAndYearFromSQLDate($MyRow['lastdate_in_period']) . '</option>';
}
}
}
echo '</select>
<fieldhelp>' . _('Select the beginning of the reporting period') . '</fieldhelp>
</field>';
// Select period to:
if(!isset($_POST['PeriodTo'])) {
$PeriodSQL = "SELECT periodno
FROM periods
WHERE MONTH(lastdate_in_period) = MONTH(CURRENT_DATE())
AND YEAR(lastdate_in_period ) = YEAR(CURRENT_DATE())";
$PeriodResult = DB_query($PeriodSQL);
$PeriodRow = DB_fetch_array($PeriodResult);
$_POST['PeriodTo'] = $PeriodRow['periodno'];;
}
echo '<field>
<label for="PeriodTo">' . _('Select period to') . '</label>
<select id="PeriodTo" name="PeriodTo" required="required">';
DB_data_seek($Periods, 0);
while($MyRow = DB_fetch_array($Periods)) {
echo '<option',($MyRow['periodno'] == $_POST['PeriodTo'] ? ' selected="selected"' : '' ) . ' value="' . $MyRow['periodno'] . '">' . MonthAndYearFromSQLDate($MyRow['lastdate_in_period']) . '</option>';
}
echo '</select>
<fieldhelp>' . _('Select the end of the reporting period') . '</fieldhelp>
</field>';
// OR Select period:
if(!isset($_POST['Period'])) {
$_POST['Period'] = '';
}
$SQL = "SELECT `id`,
`name`,
`current`
FROM glbudgetheaders";
$Result = DB_query($SQL);
echo '<field>
<label for="SelectedBudget">', _('Budget To Show Comparisons With'), '</label>
<select name="SelectedBudget">';
while ($MyRow = DB_fetch_array($Result)) {
if (!isset($_POST['SelectedBudget']) and $MyRow['current'] == 1) {
$_POST['SelectedBudget'] = $MyRow['id'];
}
if ($MyRow['id'] == $_POST['SelectedBudget']) {
echo '<option selected="selected" value="', $MyRow['id'], '">', $MyRow['name'], '</option>';
} else {
echo '<option value="', $MyRow['id'], '">', $MyRow['name'], '</option>';
}
}
echo '<fieldhelp>', _('Select the budget to make comparisons with.'), '</fieldhelp>
</select>
</field>';
echo '<field>
<label for="Period">' . '<b>' . _('OR') . ' </b>' . _('Select Period') . '</label>
' . ReportPeriodList($_POST['Period'], array('l', 't')),
'<fieldhelp>' . _('Select a period instead of using the beginning and end of the reporting period.') . '</fieldhelp>
</field>';
echo '<field>
<label for="ShowDetail">' . _('Detail or summary') . '</label>
<select name="ShowDetail">
<option value="Summary">' . _('Summary') . '</option>
<option selected="selected" value="Detailed">' . _('All Accounts') . '</option>
</select>
</field>',
// Show accounts with zero balance:
'<field>',
'<label for="ShowZeroBalance">' . _('Show accounts with zero balance') . '</label>
<input',(isset($_POST['ShowZeroBalance']) && $_POST['ShowZeroBalance'] ? ' checked="checked"' : '') . ' id="ShowZeroBalance" name="ShowZeroBalance" type="checkbox">
<fieldhelp>' . _('Check this box to show all accounts including those with zero balance') . '</fieldhelp>
</field>',
'</fieldset>';
/*Now do the posting while the user is thinking about the period to select */
echo '<div class="centre">
<input type="submit" name="PrintPDF" title="PDF" value="'._('PDF P & L Account').'" />
<input type="submit" name="View" title="View" value="' . _('Show P & L Account') .'" />
</div>',
'</form>';
include('includes/GLPostings.inc');
include('includes/footer.php');
}
?>