This repository has been archived by the owner on Feb 21, 2019. It is now read-only.
forked from emaijala/MLInvoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product_report.php
433 lines (387 loc) · 15.9 KB
/
product_report.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
<?php
/*******************************************************************************
MLInvoice: web-based invoicing application.
Copyright (C) 2010-2015 Ere Maijala
Portions based on:
PkLasku : web-based invoicing software.
Copyright (C) 2004-2008 Samu Reinikainen
This program is free software. See attached LICENSE.
*******************************************************************************/
/*******************************************************************************
MLInvoice: web-pohjainen laskutusohjelma.
Copyright (C) 2010-2015 Ere Maijala
Perustuu osittain sovellukseen:
PkLasku : web-pohjainen laskutusohjelmisto.
Copyright (C) 2004-2008 Samu Reinikainen
Tämä ohjelma on vapaa. Lue oheinen LICENSE.
*******************************************************************************/
require_once 'htmlfuncs.php';
require_once 'sqlfuncs.php';
require_once 'miscfuncs.php';
require_once 'datefuncs.php';
require_once 'localize.php';
require_once 'pdf.php';
class ProductReport
{
private $pdf = null;
public function createReport()
{
$strReport = getRequest('report', '');
if ($strReport) {
$this->printReport();
return;
}
$intBaseId = getRequest('base', FALSE);
$intCompanyId = getRequest('company', FALSE);
$intProductId = getRequest('product', FALSE);
$dateRange = getRequest('date', '');
?>
<script type="text/javascript">
$(document).ready(function() {
$('input[class~="hasDateRangePicker"]').daterangepicker(<?php echo $GLOBALS['locDateRangePickerOptions']?>);
});
</script>
<div class="form_container ui-widget-content ui-helper-clearfix">
<form method="get" id="params" name="params">
<input name="func" type="hidden" value="reports"> <input name="form"
type="hidden" value="product"> <input name="report" type="hidden"
value="1">
<div class="unlimited_label">
<h1><?php echo $GLOBALS['locProductReport']?></h1>
</div>
<div class="medium_label"><?php echo $GLOBALS['locInvoiceDateInterval']?></div>
<div class="field"><?php echo htmlFormElement('date', 'TEXT', "$dateRange" , 'medium hasDateRangePicker', '', 'MODIFY', FALSE)?></div>
<div class="medium_label"><?php echo $GLOBALS['locBiller']?></div>
<div class="field"><?php echo htmlFormElement('base', 'LIST', $intBaseId, 'medium', 'SELECT id, name FROM {prefix}base WHERE deleted=0 ORDER BY name', 'MODIFY', FALSE)?></div>
<div class="medium_label"><?php echo $GLOBALS['locClient']?></div>
<div class="field"><?php echo htmlFormElement('company', 'LIST', $intCompanyId, 'medium', 'SELECT id, company_name FROM {prefix}company WHERE deleted=0 ORDER BY company_name', 'MODIFY', FALSE)?></div>
<div class="medium_label"><?php echo $GLOBALS['locProduct']?></div>
<div class="field"><?php echo htmlFormElement('product', 'LIST', $intProductId, 'medium', 'SELECT id, product_name FROM {prefix}product WHERE deleted=0 ORDER BY product_name', 'MODIFY', FALSE)?></div>
<div class="medium_label"><?php echo $GLOBALS['locPrintFormat']?></div>
<div class="field">
<input type="radio" id="format-html" name="format" value="html" checked="checked"><label for="format-html"><?php echo $GLOBALS['locPrintFormatHTML']?></label>
</div>
<div class="medium_label"></div>
<div class="field">
<input type="radio" id="format-pdf" name="format" value="pdf"><label for="format-pdf"><?php echo $GLOBALS['locPrintFormatPDF']?></label>
</div>
<div class="field_sep"></div>
<div class="medium_label"><?php echo $GLOBALS['locPrintReportStates']?></div>
<?php
$strQuery = 'SELECT id, name ' . 'FROM {prefix}invoice_state WHERE deleted=0 ' .
'ORDER BY order_no';
$intRes = mysqli_query_check($strQuery);
$first = true;
while ($row = mysqli_fetch_assoc($intRes)) {
$intStateId = $row['id'];
$strStateName = isset($GLOBALS['loc' . $row['name']]) ? $GLOBALS['loc' .
$row['name']] : $row['name'];
$tmpSelected = getRequest("stateid_$intStateId", TRUE) ? TRUE : false;
$strChecked = $tmpSelected ? ' checked' : '';
if (!$first) {
echo " <div class=\"medium_label\"></div>\n";
}
$first = false;
?>
<div class="field">
<input type="checkbox" id="state-<?php echo $intStateId?>" name="stateid_<?php echo $intStateId?>"
value="1" <?php echo $strChecked?>> <label for="state-<?php echo $intStateId?>"><?php echo htmlspecialchars($strStateName)?></label></div>
<?php
}
?>
<div class="medium_label">
<a class="actionlink" href="#"
onclick="document.getElementById('params').submit(); return false;"><?php echo $GLOBALS['locCreateReport']?></a>
</div>
</form>
</div>
<?php
}
private function printReport()
{
$intStateID = getRequest('stateid', FALSE);
$intBaseId = getRequest('base', FALSE);
$intCompanyId = getRequest('company', FALSE);
$intProductId = getRequest('product', FALSE);
$format = getRequest('format', 'html');
$dateRange = explode(' - ', getRequest('date', ''));
$startDate = $dateRange[0];
$endDate = isset($dateRange[1]) ? $dateRange[1] : $startDate;
if ($startDate) {
$startDate = dateConvDate2DBDate($startDate);
}
if ($endDate) {
$endDate = dateConvDate2DBDate($endDate);
}
$arrParams = [];
$strQuery = 'SELECT i.id ' . 'FROM {prefix}invoice i ' . 'WHERE i.deleted=0';
if ($startDate) {
$strQuery .= ' AND i.invoice_date >= ?';
$arrParams[] = $startDate;
}
if ($endDate) {
$strQuery .= ' AND i.invoice_date <= ?';
$arrParams[] = $endDate;
}
if ($intBaseId) {
$strQuery .= ' AND i.base_id = ?';
$arrParams[] = $intBaseId;
}
if ($intCompanyId) {
$strQuery .= ' AND i.company_id = ?';
$arrParams[] = $intCompanyId;
}
$strQuery2 = '';
$strQuery3 = 'SELECT id, name ' .
'FROM {prefix}invoice_state WHERE deleted=0 ' . 'ORDER BY order_no';
$intRes = mysqli_query_check($strQuery3);
while ($row = mysqli_fetch_assoc($intRes)) {
$intStateId = $row['id'];
$strStateName = $row['name'];
$strTemp = "stateid_$intStateId";
$tmpSelected = getRequest($strTemp, FALSE) ? TRUE : FALSE;
if ($tmpSelected) {
$strQuery2 .= ' i.state_id = ? OR ';
$arrParams[] = $intStateId;
}
}
if ($strQuery2) {
$strQuery2 = ' AND (' . substr($strQuery2, 0, -3) . ')';
}
$strQuery .= "$strQuery2 ORDER BY invoice_no";
if ($intProductId) {
$strProductWhere = 'AND ir.product_id = ? ';
$arrParams[] = $intProductId;
} else
$strProductWhere = '';
$strProductQuery = 'SELECT p.id, p.product_code, p.product_name, ir.description, ' .
'ir.vat, ir.pcs, t.name as unit, ir.price, ir.vat_included, ir.discount ' .
'FROM {prefix}invoice_row ir ' .
'LEFT OUTER JOIN {prefix}product p ON p.id = ir.product_id ' .
'LEFT OUTER JOIN {prefix}row_type t ON t.id = ir.type_id ' .
"WHERE ir.deleted = 0 AND ir.partial_payment = 0 AND ir.invoice_id IN ($strQuery) $strProductWhere" .
'ORDER BY p.id, ir.description, t.name, ir.vat';
$this->printHeader($format, $startDate, $endDate);
$totalSum = 0;
$totalVAT = 0;
$totalSumVAT = 0;
$prevRow = false;
$productCount = 0;
$productSum = 0;
$productVAT = 0;
$productSumVAT = 0;
$intRes = mysqli_param_query($strProductQuery, $arrParams);
while ($row = mysqli_fetch_assoc($intRes)) {
if ($prevRow !== false && ($prevRow['id'] != $row['id'] ||
$prevRow['description'] != $row['description'] ||
$prevRow['unit'] != $row['unit'] || $prevRow['vat'] != $row['vat'])) {
$this->printRow($format, $prevRow['product_code'],
$prevRow['product_name'], $prevRow['description'], $productCount,
$prevRow['unit'], $productSum, $prevRow['vat'], $productVAT,
$productSumVAT);
$productCount = 0;
$productSum = 0;
$productVAT = 0;
$productSumVAT = 0;
}
$prevRow = $row;
$productCount += $row['pcs'];
list ($rowSum, $rowVAT, $rowSumVAT) = calculateRowSum($row['price'],
$row['pcs'], $row['vat'], $row['vat_included'], $row['discount']);
$productSum += $rowSum;
$productVAT += $rowVAT;
$productSumVAT += $rowSumVAT;
$totalSum += $rowSum;
$totalVAT += $rowVAT;
$totalSumVAT += $rowSumVAT;
}
if ($prevRow !== false) {
$this->printRow($format, $prevRow['product_code'],
$prevRow['product_name'], $prevRow['description'], $productCount,
$prevRow['unit'], $productSum, $prevRow['vat'], $productVAT,
$productSumVAT);
}
$this->printTotals($format, $totalSum, $totalVAT, $totalSumVAT);
$this->printFooter($format);
}
private function printHeader($format, $startDate, $endDate)
{
if ($format == 'pdf') {
ob_end_clean();
$pdf = new PDF('P', 'mm', 'A4', _CHARSET_ == 'UTF-8', _CHARSET_, false);
$pdf->setTopMargin(20);
$pdf->headerRight = $GLOBALS['locReportPage'];
$pdf->printHeaderOnFirstPage = true;
$pdf->AddPage();
$pdf->SetAutoPageBreak(TRUE, 15);
$pdf->setY(10);
$pdf->SetFont('Helvetica', 'B', 12);
$pdf->Cell(100, 15, $GLOBALS['locProductReport'], 0, 1, 'L');
if ($startDate || $endDate) {
$pdf->SetFont('Helvetica', '', 8);
$pdf->Cell(25, 15, $GLOBALS['locDateInterval'], 0, 0, 'L');
$pdf->Cell(50, 15,
dateConvDBDate2Date($startDate) . ' - ' .
dateConvDBDate2Date($endDate), 0, 1, 'L');
}
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->Cell(15, 4, $GLOBALS['locCode'], 0, 0, 'L');
$pdf->Cell(40, 4, $GLOBALS['locProduct'], 0, 0, 'L');
$pdf->Cell(25, 4, $GLOBALS['locPCS'], 0, 0, 'R');
$pdf->Cell(25, 4, $GLOBALS['locUnit'], 0, 0, 'R');
$pdf->Cell(25, 4, $GLOBALS['locVATLess'], 0, 0, 'R');
$pdf->Cell(15, 4, $GLOBALS['locVATPercent'], 0, 0, 'R');
$pdf->Cell(25, 4, $GLOBALS['locVATPart'], 0, 0, 'R');
$pdf->Cell(25, 4, $GLOBALS['locWithVAT'], 0, 1, 'R');
$this->pdf = $pdf;
return;
}
?>
<div class="report">
<table>
<tr>
<th class="label">
<?php echo $GLOBALS['locCode']?>
</th>
<th class="label">
<?php echo $GLOBALS['locProduct']?>
</th>
<th class="label" style="text-align: right">
<?php echo $GLOBALS['locPCS']?>
</th>
<th class="label" style="text-align: right">
<?php echo $GLOBALS['locUnit']?>
</th>
<th class="label" style="text-align: right">
<?php echo $GLOBALS['locVATLess']?>
</th>
<th class="label" style="text-align: right">
<?php echo $GLOBALS['locVATPercent']?>
</th>
<th class="label" style="text-align: right">
<?php echo $GLOBALS['locVATPart']?>
</th>
<th class="label" style="text-align: right">
<?php echo $GLOBALS['locWithVAT']?>
</th>
</tr>
<?php
}
private function printRow($format, $strCode, $strProduct, $strDescription,
$intCount, $strUnit, $intSum, $intVATPercent, $intVAT, $intSumVAT)
{
if ($strDescription) {
if ($format == 'html' && mb_strlen($strDescription, 'UTF-8') > 20)
$strDescription = mb_substr($strDescription, 0, 17, 'UTF-8') . '...';
if ($strProduct)
$strProduct .= " ($strDescription)";
else
$strProduct = $strDescription;
}
if ($strUnit && isset($GLOBALS["loc$strUnit"])) {
$strUnit = $GLOBALS["loc$strUnit"];
}
if ($format == 'pdf') {
if (!$strProduct)
$strProduct = '-';
$pdf = $this->pdf;
$pdf->SetFont('Helvetica', '', 8);
$pdf->setY($pdf->getY() + 1);
$cells = $pdf->MultiCell(16, 3, $strCode, 0, 'L', false, 0);
$nameX = 25;
$pdf->setX($nameX + 40);
$pdf->Cell(25, 3, miscRound2Decim($intCount), 0, 0, 'R');
$pdf->Cell(25, 3, $strUnit, 0, 0, 'R');
$pdf->Cell(25, 3, miscRound2Decim($intSum), 0, 0, 'R');
$pdf->Cell(15, 3, miscRound2Decim($intVATPercent, 1), 0, 0, 'R');
$pdf->Cell(25, 3, miscRound2Decim($intVAT), 0, 0, 'R');
$pdf->Cell(25, 3, miscRound2Decim($intSumVAT), 0, 0, 'R');
$pdf->setX($nameX);
$cells2 = $pdf->MultiCell(40, 3, $strProduct, 0, 'L');
if ($cells > $cells2) {
$pdf->setY($pdf->getY() + ($cells - $cells2) * 3);
}
return;
}
if (!$strProduct)
$strProduct = '–';
else
$strProduct = htmlspecialchars($strProduct);
?>
<tr>
<td class="input">
<?php echo $strCode?>
</td>
<td class="input">
<?php echo $strProduct?>
</td>
<td class="input" style="text-align: right">
<?php echo miscRound2Decim($intCount)?>
</td>
<td class="input" style="text-align: left">
<?php echo htmlspecialchars($strUnit)?>
</td>
<td class="input" style="text-align: right">
<?php echo miscRound2Decim($intSum)?>
</td>
<td class="input" style="text-align: right">
<?php echo miscRound2Decim($intVATPercent, 1)?>
</td>
<td class="input" style="text-align: right">
<?php echo miscRound2Decim($intVAT)?>
</td>
<td class="input" style="text-align: right">
<?php echo miscRound2Decim($intSumVAT)?>
</td>
</tr>
<?php
}
private function printTotals($format, $intTotSum, $intTotVAT, $intTotSumVAT)
{
if ($format == 'pdf') {
$pdf = $this->pdf;
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->setY($pdf->getY() + 3);
$pdf->Cell(55, 3, $GLOBALS['locTotal'], 0, 0, 'L');
$pdf->Cell(25, 3, '', 0, 0, 'L');
$pdf->Cell(25, 3, '', 0, 0, 'L');
$pdf->Cell(25, 3, miscRound2Decim($intTotSum), 0, 0, 'R');
$pdf->Cell(15, 3, '', 0, 0, 'L');
$pdf->Cell(25, 3, miscRound2Decim($intTotVAT), 0, 0, 'R');
$pdf->Cell(25, 3, miscRound2Decim($intTotSumVAT), 0, 1, 'R');
return;
}
?>
<tr>
<td class="input total_sum">
<?php echo $GLOBALS['locTotal']?>
</td>
<td class="input total_sum" style="text-align: right"> </td>
<td class="input total_sum" style="text-align: right"> </td>
<td class="input total_sum" style="text-align: right"> </td>
<td class="input total_sum" style="text-align: right">
<?php echo miscRound2Decim($intTotSum)?>
</td>
<td class="input total_sum" style="text-align: right"> </td>
<td class="input total_sum" style="text-align: right">
<?php echo miscRound2Decim($intTotVAT)?>
</td>
<td class="input total_sum" style="text-align: right">
<?php echo miscRound2Decim($intTotSumVAT)?>
</td>
</tr>
<?php
}
private function printFooter($format)
{
if ($format == 'pdf') {
$pdf = $this->pdf;
$pdf->Output('report.pdf', 'I');
return;
}
?>
</table>
</div>
<?php
}
}