forked from emaijala/MLInvoice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduct_stock_report.php
More file actions
361 lines (319 loc) · 12 KB
/
product_stock_report.php
File metadata and controls
361 lines (319 loc) · 12 KB
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
<?php
/*******************************************************************************
MLInvoice: web-based invoicing application.
Copyright (C) 2010-2016 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-2016 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';
require_once 'abstract_report.php';
class ProductStockReport extends AbstractReport
{
public function createReport()
{
$strReport = getRequest('report', '');
if ($strReport) {
$this->printReport();
return;
}
$intProductId = getRequest('product', false);
?>
<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_stock"> <input name="report"
type="hidden" value="1">
<div class="unlimited_label">
<h1><?php echo $GLOBALS['locProductStockReport']?></h1>
</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="field_sep"></div>
<div class="medium_label"></div>
<div class="field">
<input type="checkbox" id="purchase-price" name="purchase_price" value="1"> <label for="purchase-price"><?php echo $GLOBALS['locOnlyProductsWithPurchasePrice']?></label>
</div>
<div class="medium_label"><?php echo $GLOBALS['locPrintFormat']?></div>
<div class="field">
<input type="radio" id="format-table" name="format" value="table" checked="checked">
<label for="format-table"><?php echo $GLOBALS['locPrintFormatTable']?></label>
</div>
<div class="medium_label"></div>
<div class="field">
<input type="radio" id="format-html" name="format" value="html"><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">
<a class="actionlink" href="#" onclick="document.getElementById('params').submit(); return false;"><?php echo $GLOBALS['locCreateReport']?></a>
</div>
</form>
</div>
<?php
}
protected function printReport()
{
$intProductId = getRequest('product', FALSE);
$format = getRequest('format', 'html');
$purchasePrice = getRequest('purchase_price', false);
$arrParams = [];
$strQuery = 'SELECT * ' . 'FROM {prefix}product ' . 'WHERE deleted=0';
if ($intProductId) {
$strQuery .= ' AND id = ? ';
$arrParams[] = $intProductId;
}
if ($purchasePrice) {
$strQuery .= ' AND NOT (purchase_price IS NULL or purchase_price = 0)';
}
$this->printHeader($format);
$stockValue = 0;
$intRes = mysqli_param_query($strQuery, $arrParams);
while ($row = mysqli_fetch_assoc($intRes)) {
$this->printRow($format, $row['product_code'], $row['product_name'],
$row['purchase_price'], $row['unit_price'], $row['stock_balance']);
$stockValue += $row['stock_balance'] * $row['purchase_price'];
}
$this->printTotals($format, $stockValue);
$this->printFooter($format);
}
protected function printHeader($format)
{
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, 10, $GLOBALS['locProductStockReport'], 0, 1, 'L');
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->Cell(50, 10, date($GLOBALS['locDateFormat']), 0, 1, 'L');
if ($params = $this->getParamsStr(false)) {
$pdf->SetFont('Helvetica', '', 8);
$pdf->MultiCell(180, 5, $params, 0, 'L');
$pdf->setY($pdf->getY() + 5);
}
$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['locUnitPrice'], 0, 0, 'R');
$pdf->Cell(25, 4, $GLOBALS['locPurchasePrice'], 0, 0, 'R');
$pdf->Cell(25, 4, $GLOBALS['locStockBalance'], 0, 0, 'R');
$pdf->Cell(25, 4, $GLOBALS['locStockValue'], 0, 1, 'R');
$this->pdf = $pdf;
return;
}
?>
<div class="report">
<table class="report-table">
<tr>
<td>
<div class="unlimited_label">
<strong><?php echo $GLOBALS['locProductStockReport']?></strong>
</div>
</td>
</tr>
<tr>
<td><?php echo $this->getParamsStr(true) ?></td>
</tr>
</table>
<table class="report-table<?php echo $format == 'table' ? ' datatable' : '' ?>">
<thead>
<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['locUnitPrice']?>
</th>
<th class="label" style="text-align: right">
<?php echo $GLOBALS['locPurchasePrice']?>
</th>
<th class="label" style="text-align: right">
<?php echo $GLOBALS['locStockBalance']?>
</th>
<th class="label" style="text-align: right">
<?php echo $GLOBALS['locStockValue']?>
</th>
</tr>
</thead>
<tbody>
<?php
}
protected function printRow($format, $strCode, $strProduct, $purchasePrice,
$unitPrice, $stockBalance)
{
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($unitPrice), 0, 0, 'R');
$pdf->Cell(25, 3, miscRound2Decim($purchasePrice), 0, 0, 'R');
$pdf->Cell(25, 3, miscRound2Decim($stockBalance), 0, 0, 'R');
$pdf->Cell(25, 3, miscRound2Decim($stockBalance * $purchasePrice), 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($unitPrice)?>
</td>
<td class="input" style="text-align: right">
<?php echo miscRound2Decim($purchasePrice)?>
</td>
<td class="input" style="text-align: right">
<?php echo miscRound2Decim($stockBalance)?>
</td>
<td class="input" style="text-align: right">
<?php echo miscRound2Decim($stockBalance * $purchasePrice)?>
</td>
</tr>
<?php
}
protected function printTotals($format, $stockValue)
{
if ($format == 'pdf') {
$pdf = $this->pdf;
if ($pdf->getY() > $pdf->getPageHeight() - 7 - 15)
$pdf->AddPage();
$pdf->SetFont('Helvetica', '', 8);
$pdf->setLineWidth(0.2);
$sumPos = 130;
$rowWidth = 150;
$pdf = $this->pdf;
$pdf->SetFont('Helvetica', 'B', 8);
$pdf->setY($pdf->getY() + 1);
$pdf->Cell($sumPos, 4, $GLOBALS['locTotal'], 0, 0, 'R');
$pdf->Cell(25, 4, miscRound2Decim($stockValue), 0, 1, 'R');
return;
}
if ($format != 'html') {
return;
}
$colSpan = 5;
?>
<tr>
<?php if ($colSpan > 0) { ?>
<td class="input total_sum" colspan="<?php echo $colSpan?>"
style="text-align: right">
<?php echo $GLOBALS['locTotal']?>
</td>
<?php } ?>
<td class="input total_sum" style="text-align: right">
<?php echo miscRound2Decim($stockValue)?>
</td>
</tr>
<?php
}
protected function printFooter($format)
{
if ($format == 'pdf') {
$pdf = $this->pdf;
$pdf->Output('report.pdf', 'I');
return;
}
?>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>
</div>
<?php
if ($format == 'table') {
?>
<script type="text/javascript">
var table = $('.report-table.datatable').DataTable({
'language': {
<?php echo $GLOBALS['locTableTexts']?>
},
'pageLength': 50,
'jQueryUI': true,
'pagingType': 'full_numbers',
'footerCallback': function (row, data, start, end, display) {
var api = this.api(), data;
$([5]).each(function(i, column) {
// Total over all pages
var total = api
.column(column)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
// Total over this page
var pageTotal = api
.column(column, { page: 'current'})
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
// Update footer
pageTotal = format_currency(pageTotal/100, 2, '<?php echo $GLOBALS['locDecimalSeparator']?>', '<?php echo $GLOBALS['locThousandSeparator']?>');
total = format_currency(total/100, 2, '<?php echo $GLOBALS['locDecimalSeparator']?>', '<?php echo $GLOBALS['locThousandSeparator']?>');
$(api.column(column).footer()).html('<div style="float: right"><?php echo $GLOBALS['locVisiblePage'] ?> ' + pageTotal + '</div><br><div style="float: right"><?php echo $GLOBALS['locTotal'] ?> ' + total + '</div>');
});
}
});
var buttons = new $.fn.dataTable.Buttons(table, {
buttons: [
'copy', 'csv', 'excel', 'pdf'
]
});
table.buttons().container().appendTo($('.fg-toolbar', table.table().container()));
</script>
<?php
}
}
}