-
Notifications
You must be signed in to change notification settings - Fork 0
/
productpackagebuild.php
426 lines (375 loc) · 13.1 KB
/
productpackagebuild.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
<?php
/*
* Copyright (C) 2017 Bruno Généré <bgenere@conseil.encom1.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file productpackagebuild.php
* \brief Displays products package and allow to build selected package
* building is
* decreasing components products from stock to increase package product in stock
* this is done on a single warehouse.
*
* \ingroup dashboard
*/
@include './tpl/maindolibarr.inc.php';
/* get library */
dol_include_once('/vignoble/lib/productpackage.lib.php');
dol_include_once('/product/class/product.class.php');
dol_include_once('/product/stock/class/entrepot.class.php');
/* get language files */
$langs->load("vignoble@vignoble");
$langs->load("products");
$langs->load("stocks");
$langs->load("other");
/* check permissions */
if (! (! empty($conf->product->enabled) && $user->rights->produit->lire)) {
accessforbidden();
}
$action = GETPOST('action', 'alpha');
$error = 0;
/*
* Common Check
*/
if (! empty($action)) {
$selectedproduct = GETPOST('selectedproduct', 'alpha');
$quantity = GETPOST('quantity', 'int');
if (! ($quantity > 0)) {
$error ++;
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Quantity")), null, 'errors');
}
if (! empty($selectedproduct)) {
$productcomponents = fetchProductComponents($selectedproduct, $quantity);
}
}
/*
* Actions
*/
if ($action == 'selectproduct') {
$codemove = dol_print_date(dol_now(), '%y%m%d%H%M%S');
$label = $langs->trans("Winemaking") . ' ' . $selectedproduct . ' ' . $langs->trans('on') . ' ' . dol_print_date(dol_now(), '%Y-%m-%d %H:%M');
}
if ($action == 'createmovements') {
$selectedwarehouse = GETPOST('selectedwarehouse', 'int');
$codemove = GETPOST('codemove', 'alpha');
$label = GETPOST('label', 'alpha');
if (empty($codemove)) {
$error ++;
setEventMessages($langs->trans("ErrorFieldRequired", $langs->trans("InventoryCode")), null, 'errors');
$codemove = dol_print_date(dol_now(), '%y%m%d%H%M%S');
}
if (empty($label)) {
$error ++;
setEventMessages($langs->trans("ErrorFieldRequired", $langs->trans("LabelMovement")), null, 'errors');
$label = $langs->trans("PackageBuild") . ' ' . $selectedproduct . ' ' . $langs->trans('on') . ' ' . dol_print_date(dol_now(), '%Y-%m-%d %H:%M');
}
if ($error == 0) {
$beforestock = fetchWarehouseStock($selectedproduct, $selectedwarehouse);
updateStock($selectedproduct, $selectedwarehouse, $quantity, $label, $codemove, $productcomponents);
$afterstock = fetchWarehouseStock($selectedproduct, $selectedwarehouse);
}
}
/*
* View
*/
displayView($selectedproduct, $quantity, $productcomponents, $selectedwarehouse, $label, $codemove, $beforestock, $afterstock);
/* close database */
$db->close();
/* Function List */
/**
* Display the page view i.e the page dialog
*
* @param string $selectedproduct
* the package reference to build
* @param int $quantity
* the number of packages to build
* @param
* object array $productcomponents the components of the package
* @param int $selectedwarehouse
* the id of the warehouse for the build
* @param string $label
* the label for the stock movements
* @param string $codemove
* the code for the stock movements
* @param
* objet array $beforestock previous stock before build
* @param
* objet array $afterstock new stock after build
*/
function displayView($selectedproduct, $quantity, $productcomponents, $selectedwarehouse, $label, $codemove, $beforestock, $afterstock)
{
global $db, $conf, $langs, $user, $error, $action;
$pagetitle = $langs->trans('Winemaking');
llxHeader('', $pagetitle);
print load_fiche_titre($pagetitle, '', 'object_vignoble@vignoble');
print '<h4>' . $langs->trans('BuildPackageIntro') . '</h4>';
displaySelectionForm($selectedproduct, $quantity);
print '<div class="fichecenter">'; // frame
if (($action == 'selectproduct' && $error == 0) || ($action == 'createmovements' && $error > 0)) {
if (! empty($productcomponents)) {
displayTable($langs->trans('ProductsUsed'), $productcomponents, productcomponentsfields());
$warehouses = fetchWarehouses($selectedproduct, $quantity);
print '<br/><br/>';
if (! empty($warehouses)) {
displayConfirmationForm($warehouses, $selectedproduct, $selectedwarehouse, $quantity, $label, $codemove);
} else {
print '<h4>' . $langs->trans('NoWarehouseWithComponentsStock') . '</h4>';
}
}
}
if ($action == 'createmovements') {
if ($error == 0) {
print '<h4>' . $langs->trans('BuildSuccesfull') . '</h4>';
displayTable($langs->trans('StockBefore'), $beforestock, warehousestockfields());
displayTable($langs->trans('StockAfter'), $afterstock, warehousestockfields());
} else {
print '<h4>' . $langs->trans('BuildFailed') . '</h4>';
}
}
print '</div>'; // frame end
llxFooter();
}
/**
* Display the top form for package selection
*
* @param string $selectedproduct
* the package reference to build
* @param int $quantity
* the number of packages to build
*/
function displaySelectionForm($selectedproduct, $quantity)
{
global $db, $conf, $langs, $user;
$form = new Form($db);
print '<div class="fichecenter">';
print '<form method="post" action="' . DOL_URL_ROOT . '/custom/vignoble/productpackagebuild.php">';
print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
print '<input type="hidden" name="action" value="selectproduct">';
print '<table class="noborder nohover centpercent">';
// Form header
print '<tr class="liste_titre"><td colspan="3"> 1 - ' . $langs->trans("Select") . ' ' . $langs->trans("Package") . ' & ' . $langs->trans("Quantity") . '</td></tr>';
// Product package and Button
print '<tr>';
print '<td class="nowrap"><label for="products">' . $langs->trans("Product") . '</label></td>';
print '<td >' . $form->selectarray('selectedproduct', fetchPackages(), $selectedproduct, 0, 1, 0, 0, '100%') . '</td>';
print '<td rowspan="2"><input type="submit" value="' . $langs->trans("Select") . '" class="button"></td>';
print '</tr>';
// Product Quantity
print '<tr>';
print '<td class="nowrap"><label for="quantity">' . $langs->trans("Quantity") . '</label></td>';
print '<td ><input type="text" class="flat" name="quantity" value="' . $quantity . '">';
print '</tr>';
// Form footer
print '</table>';
print '</form>';
print '<br>';
print '</div>'; // fiche
}
/**
* Display the form to confirm the stock movements
*
* @param array $warehouses
* the list of available warehouses
* @param string $selectedproduct
* the package reference to build
* @param int $selectedwarehouse
* the id of the warehouse for the build
* @param int $quantity
* the number of packages to build
* @param string $label
* the label for the stock movements
* @param string $codemove
* the code for the stock movements
*/
function displayConfirmationForm($warehouses, $selectedproduct, $selectedwarehouse, $quantity, $label, $codemove)
{
global $db, $conf, $langs, $user;
$form = new Form($db);
print '<div class="fichecenter">';
print '<form method="post" action="' . DOL_URL_ROOT . '/custom/vignoble/productpackagebuild.php">';
print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
print '<input type="hidden" name="action" value="createmovements">';
print '<input type="hidden" name="selectedproduct" value="' . $selectedproduct . '">';
print '<input type="hidden" name="quantity" value="' . $quantity . '">';
print '<table class="noborder nohover centpercent">';
// Form header
print '<tr class="liste_titre"><td colspan="3"> 2 - ' . $langs->trans("ConfirmBuild") . '</td></tr>';
// Warehouse and Button
print '<tr>';
print '<td class="nowrap"><label for="warehouse">' . $langs->trans("Warehouse") . '</label></td>';
print '<td >' . $form->selectarray('selectedwarehouse', $warehouses, $selectedwarehouse, 0, 1, 0, 0, '100%') . '</td>';
print '<td rowspan="3"><input type="submit" value="' . $langs->trans("Build") . '" class="button"></td>';
print '</tr>';
// Comment
print '<tr>';
print '<td class="nowrap"><label for="label">' . $langs->trans("LabelMovement") . '</label></td>';
print '<td ><input type="text" name="label" class="quatrevingtpercent" value="' . dol_escape_htmltag($label) . '">';
print '</tr>';
// Comment
print '<tr>';
print '<td class="nowrap"><label for="codemove">' . $langs->trans("InventoryCode") . '</label></td>';
print '<td ><input type="text" name="codemove" size="10" value="' . dol_escape_htmltag($codemove) . '">';
print '</tr>';
// Form footer
print '</table>';
print '</form>';
print '<br>';
print '</div>'; // fiche
}
/**
* Product components fields to display in table
*
* @return string[][]
*/
function productcomponentsfields()
{
$fields = array(
'Ref' => array(
'align' => 'left',
'label' => 'Reference'
),
'Label' => array(
'align' => 'left',
'label' => 'Label'
),
'Unit' => array(
'align' => 'left',
'label' => 'Unit/Package'
),
'Quantity' => array(
'align' => 'left',
'label' => 'Quantity'
)
);
return $fields;
}
/**
* Warehouse stock fields to display in table
*
* @return string[][]
*/
function warehousestockfields()
{
$fields = array(
'warehouse' => array(
'align' => 'left',
'label' => 'Warehouse'
),
'ref' => array(
'align' => 'left',
'label' => 'Reference'
),
'label' => array(
'align' => 'left',
'label' => 'Label'
),
'quantity' => array(
'align' => 'left',
'label' => 'Quantity'
)
);
return $fields;
}
/**
* Display a Table content
*
* @param string $tablename
* name of the object
* @param array[] $table
* result set from SQL query
* @param array[] $fields
* list of fields (columns) and display properties
*
*/
function displayTable($tablename, $table, $fields)
{
global $db, $conf, $langs, $user;
print load_fiche_titre($langs->trans($tablename), '', '');
print '<table class="liste" >';
// Fields title
print '<tr class="liste_titre">';
foreach ($fields as $field => $fieldvalue) {
print print_liste_field_titre($langs->trans($fieldvalue['label']), '', '', '', '', 'align="' . $fieldvalue['align'] . '"');
}
print '</tr>';
// Table lines
foreach ($table as $line) {
print '<tr>';
foreach ($fields as $field => $fieldvalue) {
print '<td align="' . $fieldvalue['align'] . '">';
if (! empty($fieldvalue['display'])) {
print $fieldvalue['display']($line->$field);
} else {
print $line->$field;
}
print '</td>';
}
print '</tr>';
}
print '</table>';
}
/**
* Create all stock movements for the build of a product
*
* @param string $selectedproduct
* the product to build
* @param int $selectedwarehouse
* the warehouse id to use
* @param int $quantity
* of product to build
* @param string $label
* for all movements
* @param string $codemove
* code for all movements
* @param array[] $productcomponents
* all components and quantities to consume
*/
function updateStock($selectedproduct, $selectedwarehouse, $quantity, $label, $codemove, $productcomponents)
{
global $db, $conf, $langs, $user;
$db->begin();
$product = new Product($db);
// process package - add to stock
$result = $product->fetch('', $selectedproduct);
$product->load_stock('novirtual'); // Load array product->stock_warehouse
$result2 = $product->correct_stock($user, $selectedwarehouse, $quantity, 0, $label, $product->price, $codemove);
if ($result2 < 0) {
$error ++;
setEventMessages($product->errors, $product->errorss, 'errors');
}
// process components - remove from stock
foreach ($productcomponents as $component) {
$result = $product->fetch('', $component->Ref);
$product->load_stock('novirtual'); // Load array product->stock_warehouse
// Define value of products moved
// $pricesrc = 0;
// if (! empty($product->pmp))
// $pricesrc = $product->pmp;
// $pricedest = $pricesrc;
// Remove stock
$result1 = $product->correct_stock($user, $selectedwarehouse, $component->Quantity, 1, $label, $product->price, $codemove);
if ($result1 < 0) {
$error ++;
setEventMessages($product->errors, $product->errorss, 'errors');
}
}
if (! $error) {
$db->commit();
setEventMessages($langs->trans("StockMovementRecorded"), null, 'mesgs');
} else {
$db->rollback();
setEventMessages($langs->trans("Error"), null, 'errors');
}
}