dolibarr 21.0.0-beta
combinations.php
1<?php
2/* Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
3 * Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2018-2024 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2022 Open-Dsi <support@open-dsi.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22// Load Dolibarr environment
23require '../main.inc.php';
24require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
25require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
26require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttribute.class.php';
27require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttributeValue.class.php';
28require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php';
29require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php';
30
39$langs->loadLangs(array("products", "other"));
40
41$id = GETPOSTINT('id'); // ID of the parent Product
42$ref = GETPOST('ref', 'alpha');
43
44$combination_id = GETPOSTINT('combination_id'); // ID of the combination
45
46$reference = GETPOST('reference', 'alpha'); // Reference of the variant Product
47
48$weight_impact = GETPOSTFLOAT('weight_impact', 2);
49$price_impact_percent = (bool) GETPOST('price_impact_percent');
50$price_impact = $price_impact_percent ? GETPOSTFLOAT('price_impact', 2) : GETPOSTFLOAT('price_impact', 'MU');
51
52// for PRODUIT_MULTIPRICES
53$level_price_impact = GETPOST('level_price_impact', 'array');
54$level_price_impact = array_map('price2num', $level_price_impact);
55$level_price_impact = array_map('floatval', $level_price_impact);
56$level_price_impact_percent = GETPOST('level_price_impact_percent', 'array');
57$level_price_impact_percent = array_map('boolval', $level_price_impact_percent);
58
59$form = new Form($db);
60
61$action = GETPOST('action', 'aZ09');
62$massaction = GETPOST('massaction', 'alpha');
63$show_files = GETPOSTINT('show_files');
64$confirm = GETPOST('confirm', 'alpha');
65$toselect = GETPOST('toselect', 'array');
66$cancel = GETPOST('cancel', 'alpha');
67$delete_product = GETPOST('delete_product', 'alpha');
68$subaction = GETPOST('subaction', 'aZ09');
69$backtopage = GETPOST('backtopage', 'alpha');
70$sortfield = GETPOST('sortfield', 'aZ09comma');
71$sortorder = GETPOST('sortorder', 'aZ09comma');
72
73// Security check
74$fieldvalue = $id ?: $ref;
75$fieldtype = !empty($ref) ? 'ref' : 'rowid';
76
77$prodstatic = new Product($db);
78$prodattr = new ProductAttribute($db);
79$prodattr_val = new ProductAttributeValue($db);
80
81$object = new Product($db);
82if ($id > 0 || $ref) {
83 $object->fetch($id, $ref);
84}
85
86$selectedvariant = isset($_SESSION['addvariant_'.$object->id]) ? $_SESSION['addvariant_'.$object->id] : array();
87$selected = '';
88// Security check
89if (!isModEnabled('variants')) {
90 accessforbidden('Module not enabled');
91}
92if ($user->socid > 0) { // Protection if external user
94}
95
96if ($object->id > 0) {
97 if ($object->isProduct()) {
98 restrictedArea($user, 'produit', $object->id, 'product&product', '', '');
99 }
100 if ($object->isService()) {
101 restrictedArea($user, 'service', $object->id, 'product&product', '', '');
102 }
103} else {
104 restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
105}
106$usercanread = (($object->isProduct() && $user->hasRight('produit', 'lire')) || ($object->isService() && $user->hasRight('service', 'lire')));
107$usercancreate = (($object->isProduct() && $user->hasRight('produit', 'creer')) || ($object->isService() && $user->hasRight('service', 'creer')));
108$usercandelete = (($object->isProduct() && $user->hasRight('produit', 'supprimer')) || ($object->isService() && $user->hasRight('service', 'supprimer')));
109
110
111/*
112 * Actions
113 */
114
115if ($cancel) {
116 $action = '';
117 $massaction = '';
118 unset($_SESSION['addvariant_'.$object->id]);
119}
120
121if (!$object->isProduct() && !$object->isService()) {
122 header('Location: '.dol_buildpath('/product/card.php?id='.$object->id, 2));
123 exit();
124}
125if ($action == 'add') { // Test on permission not required
126 unset($selectedvariant);
127 unset($_SESSION['addvariant_'.$object->id]);
128}
129if ($action == 'create' && GETPOST('selectvariant', 'alpha') && $usercancreate) { // We click on select combination
130 $action = 'add';
131 $attribute_id = GETPOSTINT('attribute');
132 $attribute_value_id = GETPOSTINT('value');
133 if ($attribute_id > 0 && $attribute_value_id > 0) {
134 $feature = $attribute_id . '-' . $attribute_value_id;
135 $selectedvariant[$feature] = $feature;
136 $_SESSION['addvariant_'.$object->id] = $selectedvariant;
137 }
138}
139if ($action == 'create' && $subaction == 'delete' && $usercancreate) { // We click on select combination
140 $action = 'add';
141 $feature = GETPOST('feature', 'intcomma');
142 if (isset($selectedvariant[$feature])) {
143 unset($selectedvariant[$feature]);
144 $_SESSION['addvariant_'.$object->id] = $selectedvariant;
145 }
146}
147
148
149$prodcomb = new ProductCombination($db);
150$prodcomb2val = new ProductCombination2ValuePair($db);
151
152$productCombination2ValuePairs1 = array();
153
154if (($action == 'add' || $action == 'create') && $usercancreate && empty($massaction) && !GETPOST('selectvariant', 'alpha') && empty($subaction)) { // We click on Create all defined combinations
155 //$features = GETPOST('features', 'array');
156 $features = !empty($_SESSION['addvariant_'.$object->id]) ? $_SESSION['addvariant_'.$object->id] : array();
157
158 if (!$features) {
159 if ($action == 'create') { // Test on permission already done
160 setEventMessages($langs->trans('ErrorFieldsRequired'), null, 'errors');
161 }
162 } else {
163 $reference = trim($reference);
164 if (empty($reference)) {
165 $reference = false;
166 }
167 $weight_impact = price2num($weight_impact);
168 $price_impact = price2num($price_impact);
169
170 if (!getDolGlobalString('PRODUIT_MULTIPRICES')) {
171 $level_price_impact = array(1 => $price_impact);
172 $level_price_impact_percent = array(1 => $price_impact_percent);
173 }
174
175 $sanit_features = array();
176
177 //First, sanitize
178 foreach ($features as $feature) {
179 $explode = explode('-', $feature);
180 if ($prodattr->fetch($explode[0]) <= 0 || $prodattr_val->fetch($explode[1]) <= 0) {
181 continue;
182 }
183
184 // Valuepair
185 $sanit_features[(int) $explode[0]] = (int) $explode[1];
186
187 $tmp = new ProductCombination2ValuePair($db);
188 $tmp->fk_prod_attr = (int) $explode[0];
189 $tmp->fk_prod_attr_val = (int) $explode[1];
190
191 $productCombination2ValuePairs1[] = $tmp;
192 }
193
194 $db->begin();
195
196 // sanit_feature is an array with 1 (and only 1) value per attribute.
197 // For example: Color->blue, Size->Small, Option->2
198 if (!$prodcomb->fetchByProductCombination2ValuePairs($id, $sanit_features)) {
199 $result = $prodcomb->createProductCombination($user, $object, $sanit_features, array(), $level_price_impact_percent, $level_price_impact, $weight_impact, $reference);
200 if ($result > 0) {
201 setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
202 unset($_SESSION['addvariant_'.$object->id]);
203
204 $db->commit();
205 header('Location: '.dol_buildpath('/variants/combinations.php?id='.$id, 2));
206 exit();
207 } else {
208 $langs->load("errors");
209 setEventMessages($prodcomb->error, $prodcomb->errors, 'errors');
210 }
211 } else {
212 setEventMessages($langs->trans('ErrorRecordAlreadyExists'), null, 'errors');
213 }
214
215 $db->rollback();
216 }
217} elseif (!empty($massaction)) {
218 $bulkaction = $massaction;
219 $error = 0;
220
221 $db->begin();
222
223 foreach ($toselect as $prodid) {
224 // need create new of Product to prevent rename dir behavior
225 $prodstatic = new Product($db);
226 if ($prodstatic->fetch($prodid) < 0) {
227 continue;
228 }
229
230 if ($bulkaction == 'on_sell') {
231 $prodstatic->status = 1;
232 $res = $prodstatic->update($prodstatic->id, $user);
233 if ($res <= 0) {
234 setEventMessages($prodstatic->error, $prodstatic->errors, 'errors');
235 $error++;
236 break;
237 }
238 } elseif ($bulkaction == 'on_buy') {
239 $prodstatic->status_buy = 1;
240 $res = $prodstatic->update($prodstatic->id, $user);
241 if ($res <= 0) {
242 setEventMessages($prodstatic->error, $prodstatic->errors, 'errors');
243 $error++;
244 break;
245 }
246 } elseif ($bulkaction == 'not_sell') {
247 $prodstatic->status = 0;
248 $res = $prodstatic->update($prodstatic->id, $user);
249 if ($res <= 0) {
250 setEventMessages($prodstatic->error, $prodstatic->errors, 'errors');
251 $error++;
252 break;
253 }
254 } elseif ($bulkaction == 'not_buy') {
255 $prodstatic->status_buy = 0;
256 $res = $prodstatic->update($prodstatic->id, $user);
257 if ($res <= 0) {
258 setEventMessages($prodstatic->error, $prodstatic->errors, 'errors');
259 $error++;
260 break;
261 }
262 } elseif ($bulkaction == 'delete') {
263 $res = $prodstatic->delete($user, $prodstatic->id);
264 if ($res <= 0) {
265 setEventMessages($prodstatic->error, $prodstatic->errors, 'errors');
266 $error++;
267 break;
268 }
269 } else {
270 break;
271 }
272 }
273
274 if ($error) {
275 $db->rollback();
276 if (empty($prodstatic->error)) {
277 setEventMessages($langs->trans('CoreErrorMessage'), null, 'errors');
278 }
279 } else {
280 $db->commit();
281 setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
282 }
283} elseif ($action === 'update' && $combination_id > 0 && $usercancreate) {
284 if ($prodcomb->fetch($combination_id) < 0) {
285 dol_print_error($db, $langs->trans('ErrorRecordNotFound'));
286 exit();
287 }
288
289 $prodcomb->variation_weight = (float) price2num($weight_impact);
290
291 // for conf PRODUIT_MULTIPRICES
292 if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
293 $prodcomb->variation_price = $level_price_impact[1];
294 $prodcomb->variation_price_percentage = $level_price_impact_percent[1];
295 } else {
296 $level_price_impact = array(1 => $price_impact);
297 $level_price_impact_percent = array(1 => $price_impact_percent);
298
299 $prodcomb->variation_price = $price_impact;
300 $prodcomb->variation_price_percentage = $price_impact_percent;
301 }
302
303 if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
304 $prodcomb->combination_price_levels = array();
305 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
306 $productCombinationLevel = new ProductCombinationLevel($db);
307 $productCombinationLevel->fk_product_attribute_combination = $prodcomb->id;
308 $productCombinationLevel->fk_price_level = $i;
309 $productCombinationLevel->variation_price = (float) $level_price_impact[$i];
310 $productCombinationLevel->variation_price_percentage = (bool) $level_price_impact_percent[$i];
311 $prodcomb->combination_price_levels[$i] = $productCombinationLevel;
312 }
313 }
314
315 $error = 0;
316 $db->begin();
317
318 // Update product variant ref
319 $product_child = new Product($db);
320 $product_child->fetch($prodcomb->fk_product_child);
321 $product_child->oldcopy = clone $product_child; // @phan-ignore-current-line PhanTypeMismatchProperty
322 $product_child->ref = $reference;
323
324 $result = $product_child->update($product_child->id, $user);
325 if ($result < 0) {
326 setEventMessages($product_child->error, $product_child->errors, 'errors');
327 $error++;
328 }
329
330 if (!$error) {
331 // Update product variant infos
332 $result = $prodcomb->update($user);
333 if ($result < 0) {
334 setEventMessages($prodcomb->error, $prodcomb->errors, 'errors');
335 $error++;
336 }
337 }
338
339 if (!$error) {
340 $db->commit();
341 setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
342 header('Location: ' . dol_buildpath('/variants/combinations.php?id=' . $id, 2));
343 exit();
344 } else {
345 $db->rollback();
346 }
347}
348
349
350// Reload variants
351$productCombinations = $prodcomb->fetchAllByFkProductParent($object->id, true);
352
353if ($action === 'confirm_deletecombination' && $usercancreate) {
354 if ($prodcomb->fetch($combination_id) > 0) {
355 $db->begin();
356
357 if ($prodcomb->delete($user) > 0 && (empty($delete_product) || ($delete_product == 'on' && $prodstatic->fetch($prodcomb->fk_product_child) > 0 && $prodstatic->delete($user) > 0))) {
358 $db->commit();
359 setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
360 header('Location: '.dol_buildpath('/variants/combinations.php?id='.$object->id, 2));
361 exit();
362 }
363
364 $db->rollback();
365 setEventMessages($langs->trans('ProductCombinationAlreadyUsed'), null, 'errors');
366 $action = '';
367 }
368} elseif ($action === 'edit' && $usercancreate) {
369 if ($prodcomb->fetch($combination_id) < 0) {
370 dol_print_error($db, $langs->trans('ErrorRecordNotFound'));
371 exit();
372 }
373
374 $product_child = new Product($db);
375 $product_child->fetch($prodcomb->fk_product_child);
376 $reference = $product_child->ref;
377 $weight_impact = $prodcomb->variation_weight;
378 $price_impact = $prodcomb->variation_price;
379 $price_impact_percent = $prodcomb->variation_price_percentage;
380
381 $productCombination2ValuePairs1 = $prodcomb2val->fetchByFkCombination($combination_id);
382} elseif ($action === 'confirm_copycombination' && $usercancreate) {
383 //Check destination product
384 $dest_product = GETPOST('dest_product');
385
386 if ($prodstatic->fetch(0, $dest_product) > 0) {
387 //To prevent from copying to the same product
388 if ($prodstatic->ref != $object->ref) {
389 if ($prodcomb->copyAll($user, $object->id, $prodstatic) > 0) {
390 header('Location: '.dol_buildpath('/variants/combinations.php?id='.$prodstatic->id, 2));
391 exit();
392 } else {
393 setEventMessages($langs->trans('ErrorCopyProductCombinations'), null, 'errors');
394 }
395 }
396 } else {
397 setEventMessages($langs->trans('ErrorDestinationProductNotFound'), null, 'errors');
398 }
399}
400
401
402
403/*
404 * View
405 */
406
407$form = new Form($db);
408
409$title = $langs->trans("Variant");
410
411llxHeader("", $title);
412
413
414if (!empty($id) || !empty($ref)) {
415 $showbarcode = isModEnabled('barcode');
416 if (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && !$user->hasRight('barcode', 'lire_advance')) {
417 $showbarcode = 0;
418 }
419
420 $head = product_prepare_head($object);
421 $titre = $langs->trans("CardProduct".$object->type);
422 $picto = $object->isService() ? 'service' : 'product';
423
424 print dol_get_fiche_head($head, 'combinations', $titre, -1, $picto);
425
426 $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?type='.((int) $object->type).'">'.$langs->trans("BackToList").'</a>';
427 $object->next_prev_filter = "fk_product_type:=:".((int) $object->type); // usf filter
428
429 dol_banner_tab($object, 'ref', $linkback, ($user->socid ? 0 : 1), 'ref', '', '', '', 0, '', '');
430
431 print '<div class="fichecenter">';
432
433 print '<div class="underbanner clearboth"></div>';
434 print '<table class="border centpercent tableforfield">';
435
436 // Type
437 if (isModEnabled("product") && isModEnabled("service")) {
438 $typeformat = 'select;0:'.$langs->trans("Product").',1:'.$langs->trans("Service");
439 print '<tr><td class="titlefieldcreate">';
440 print (!getDolGlobalString('PRODUCT_DENY_CHANGE_PRODUCT_TYPE')) ? $form->editfieldkey("Type", 'fk_product_type', $object->type, $object, $usercancreate, $typeformat) : $langs->trans('Type');
441 print '</td><td>';
442 print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, $usercancreate, $typeformat);
443 print '</td></tr>';
444 }
445
446 // TVA
447 print '<tr><td class="titlefieldcreate">'.$langs->trans("DefaultTaxRate").'</td><td>';
448
449 $positiverates = '';
450 if (price2num($object->tva_tx)) {
451 $positiverates .= ($positiverates ? '/' : '').price2num($object->tva_tx);
452 }
453 if (price2num($object->localtax1_type)) {
454 $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax1_tx);
455 }
456 if (price2num($object->localtax2_type)) {
457 $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax2_tx);
458 }
459 if (empty($positiverates)) {
460 $positiverates = '0';
461 }
462 echo vatrate($positiverates.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), true, $object->tva_npr);
463 /*
464 if ($object->default_vat_code)
465 {
466 print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')';
467 }
468 else print vatrate($object->tva_tx, true, $object->tva_npr, true);*/
469 print '</td></tr>';
470
471 // Price
472 print '<tr><td>'.$langs->trans("SellingPrice").'</td><td>';
473 if ($object->price_base_type == 'TTC') {
474 print price($object->price_ttc).' '.$langs->trans($object->price_base_type);
475 } else {
476 print price($object->price).' '.$langs->trans($object->price_base_type);
477 }
478 print '</td></tr>';
479
480 // Price minimum
481 print '<tr><td>'.$langs->trans("MinPrice").'</td><td>';
482 if ($object->price_base_type == 'TTC') {
483 print price($object->price_min_ttc).' '.$langs->trans($object->price_base_type);
484 } else {
485 print price($object->price_min).' '.$langs->trans($object->price_base_type);
486 }
487 print '</td></tr>';
488
489 // Weight
490 print '<tr><td>'.$langs->trans("Weight").'</td><td>';
491 if ($object->weight != '') {
492 print $object->weight." ".measuringUnitString(0, "weight", $object->weight_units);
493 } else {
494 print '&nbsp;';
495 }
496 print "</td></tr>\n";
497
498 print "</table>\n";
499
500 print '</div>';
501 print '<div class="clearboth"></div>';
502
503 print dol_get_fiche_end();
504
505 $listofvariantselected = '';
506
507 // Create or edit a variant
508 if ($action == 'add' || ($action == 'edit')) {
509 if ($action == 'add') {
510 $title = $langs->trans('NewProductCombination');
511 // print dol_get_fiche_head();
512 $features = !empty($_SESSION['addvariant_'.$object->id]) ? $_SESSION['addvariant_'.$object->id] : array();
513 //First, sanitize
514 $listofvariantselected = '<div id="parttoaddvariant">';
515 if (!empty($features)) {
516 $toprint = array();
517 foreach ($features as $feature) {
518 $explode = explode('-', $feature);
519 if ($prodattr->fetch($explode[0]) <= 0 || $prodattr_val->fetch($explode[1]) <= 0) {
520 continue;
521 }
522 $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #ddd;">' . $prodattr->label.' : '.$prodattr_val->value .
523 ' <a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=create&subaction=delete&feature='.urlencode($feature).'">' . img_delete() . '</a></li>';
524 }
525 $listofvariantselected .= '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
526 }
527 $listofvariantselected .= '</div>';
528 //print dol_get_fiche_end();
529 } else {
530 $title = $langs->trans('EditProductCombination');
531 }
532
533 if ($action == 'add') {
534 $prodattr_all = $prodattr->fetchAll();
535
536 if (!$selected) {
537 $selected = $prodattr_all[key($prodattr_all)]->id;
538 }
539
540 $prodattr_alljson = array();
541
542 foreach ($prodattr_all as $each) {
543 $prodattr_alljson[$each->id] = $each;
544 } ?>
545
546 <script type="text/javascript">
547
548 variants_available = <?php echo json_encode($prodattr_alljson, JSON_PARTIAL_OUTPUT_ON_ERROR); ?>;
549 variants_selected = {
550 index: [],
551 info: []
552 };
553
554 <?php
555 foreach ($productCombination2ValuePairs1 as $pc2v) {
556 $prodattr_val->fetch($pc2v->fk_prod_attr_val); ?>
557 variants_selected.index.push(<?php echo $pc2v->fk_prod_attr ?>);
558 variants_selected.info[<?php echo $pc2v->fk_prod_attr ?>] = {
559 attribute: variants_available[<?php echo $pc2v->fk_prod_attr ?>],
560 value: {
561 id: <?php echo $pc2v->fk_prod_attr_val ?>,
562 label: '<?php echo $prodattr_val->value ?>'
563 }
564 };
565 <?php
566 } ?>
567
568 restoreAttributes = function() {
569 jQuery("select[name=attribute]").empty().append('<option value="-1">&nbsp;</option>');
570
571 jQuery.each(variants_available, function (key, val) {
572 if (jQuery.inArray(val.id, variants_selected.index) == -1) {
573 jQuery("select[name=attribute]").append('<option value="' + val.id + '">' + val.label + '</option>');
574 }
575 });
576 };
577
578
579 jQuery(document).ready(function() {
580 jQuery("select#attribute").change(function () {
581 console.log("Change of field variant attribute");
582 var select = jQuery("select#value");
583
584 if (!jQuery(this).val().length || jQuery(this).val() == '-1') {
585 select.empty();
586 select.append('<option value="-1">&nbsp;</option>');
587 return;
588 }
589
590 select.empty().append('<option value="">Loading...</option>');
591
592 jQuery.getJSON("ajax/get_attribute_values.php", {
593 id: jQuery(this).val()
594 }, function(data) {
595 if (data.error) {
596 select.empty();
597 select.append('<option value="-1">&nbsp;</option>');
598 return alert(data.error);
599 }
600
601 select.empty();
602 select.append('<option value="-1">&nbsp;</option>');
603
604 jQuery(data).each(function (key, val) {
605 keyforoption = val.id
606 valforoption = val.value
607 select.append('<option value="' + keyforoption + '">' + valforoption + '</option>');
608 });
609 });
610 });
611 });
612 </script>
613
614 <?php
615 }
616
617 print '<br>';
618
619 print load_fiche_titre($title);
620
621 print '<form method="post" id="combinationform" action="'.$_SERVER["PHP_SELF"] .'?id='.$object->id.'">'."\n";
622 print '<input type="hidden" name="token" value="'.newToken().'">';
623 print '<input type="hidden" name="action" value="'.(($combination_id > 0) ? "update" : "create").'">'."\n";
624 if ($combination_id > 0) {
625 print '<input type="hidden" name="combination_id" value="'.$combination_id.'">'."\n";
626 }
627
628 print dol_get_fiche_head();
629
630
631 if ($action == 'add') {
632 print '<table class="border" style="width: 100%">';
633 print "<!-- Variant -->\n";
634 print '<tr>';
635 print '<td class="titlefieldcreate fieldrequired"><label for="attribute">'.$langs->trans('ProductAttribute').'</label></td>';
636 print '<td>';
637 if (is_array($prodattr_all)) {
638 print '<select class="flat minwidth100" id="attribute" name="attribute">';
639 print '<option value="-1">&nbsp;</option>';
640 foreach ($prodattr_all as $attr) {
641 //print '<option value="'.$attr->id.'"'.($attr->id == GETPOST('attribute', 'int') ? ' selected="selected"' : '').'>'.$attr->label.'</option>';
642 print '<option value="'.$attr->id.'">'.$attr->label.'</option>';
643 }
644 print '</select>';
645 }
646
647 $htmltext = $langs->trans("GoOnMenuToCreateVairants", $langs->transnoentities("Product"), $langs->transnoentities("VariantAttributes"));
648 print $form->textwithpicto('', $htmltext);
649 /*print ' &nbsp; &nbsp; <a href="'.DOL_URL_ROOT.'/variants/create.php?action=create&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=add&token='.newToken().'&id='.$object->id).'">';
650 print $langs->trans("Create");
651 print '</a>';*/
652
653 print '</td>';
654 print '</tr>'; ?>
655 <!-- Value -->
656 <tr>
657 <td class="fieldrequired"><label for="value"><?php echo $langs->trans('Value') ?></label></td>
658 <td>
659 <select class="flat minwidth100" id="value" name="value">
660 <option value="-1">&nbsp;</option>
661 </select>
662 <?php
663 $htmltext = $langs->trans("GoOnMenuToCreateVairants", $langs->transnoentities("Product"), $langs->transnoentities("VariantAttributes"));
664 print $form->textwithpicto('', $htmltext);
665 /*
666 print ' &nbsp; &nbsp; <a href="'.DOL_URL_ROOT.'/variants/create.php?action=create&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=add&token='.newToken().'&id='.$object->id).'">';
667 print $langs->trans("Create");
668 print '</a>';
669 */ ?>
670 </td>
671 </tr>
672 <tr>
673 <td></td><td>
674 <input type="submit" class="button" name="selectvariant" id="selectvariant" value="<?php echo dol_escape_htmltag($langs->trans("SelectCombination")); ?>">
675 </td>
676 </tr>
677 <?php
678 print '<tr><td></td><td>';
679 print $listofvariantselected;
680 print '</td>';
681 print '</tr>';
682
683 print '</table>';
684 print '<hr>';
685 }
686
687 if (is_array($productCombination2ValuePairs1)) {
688 print '<table class="border" style="width: 100%">';
689
690 // When in edit mode
691 if (is_array($productCombination2ValuePairs1) && count($productCombination2ValuePairs1)) {
692 ?>
693 <tr>
694 <td class="titlefieldcreate tdtop"><label for="features"><?php echo $langs->trans('Attributes') ?></label></td>
695 <td class="tdtop">
696 <div class="inline-block valignmiddle quatrevingtpercent">
697 <?php
698 foreach ($productCombination2ValuePairs1 as $pc2v) {
699 $result1 = $prodattr->fetch($pc2v->fk_prod_attr);
700 $result2 = $prodattr_val->fetch($pc2v->fk_prod_attr_val);
701 if ($result1 > 0 && $result2 > 0) {
702 print $prodattr->label.' : '.$prodattr_val->value.'<br>';
703 // TODO Add delete link
704 }
705 } ?>
706 </div>
707 <!-- <div class="inline-block valignmiddle">
708 <a href="#" class="inline-block valignmiddle button" id="delfeature"><?php echo img_edit_remove() ?></a>
709 </div>-->
710 </td>
711 <td>
712 </td>
713 </tr>
714 <?php
715 } ?>
716 <tr>
717 <td><label for="reference"><?php echo $langs->trans('Reference') ?></label></td>
718 <td><input type="text" id="reference" name="reference" value="<?php echo trim($reference) ?>"></td>
719 </tr>
720 <?php
721 if (!getDolGlobalString('PRODUIT_MULTIPRICES')) {
722 ?>
723 <tr>
724 <td><label for="price_impact"><?php echo $langs->trans('PriceImpact') ?></label></td>
725 <td><input type="text" id="price_impact" name="price_impact" value="<?php echo price($price_impact) ?>">
726 <input type="checkbox" id="price_impact_percent" name="price_impact_percent" <?php echo $price_impact_percent ? ' checked' : '' ?>> <label for="price_impact_percent"><?php echo $langs->trans('PercentageVariation') ?></label>
727 </td>
728 </tr>
729 <?php
730 } else {
731 $prodcomb->fetchCombinationPriceLevels();
732
733 for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
734 $keyforlabel = 'PRODUIT_MULTIPRICES_LABEL'.$i;
735 $text = $langs->trans('ImpactOnPriceLevel', $i).' - '.getDolGlobalString($keyforlabel);
736 print '<tr>';
737 print '<td><label for="level_price_impact_'.$i.'">'.$text.'</label>';
738 if ($i === 1) {
739 print '<br/><a id="apply-price-impact-to-all-level" class="classfortooltip" href="#" title="'.$langs->trans('ApplyToAllPriceImpactLevelHelp').'">('.$langs->trans('ApplyToAllPriceImpactLevel').')</a>';
740 }
741 print '</td>';
742 print '<td><input type="text" class="level_price_impact" id="level_price_impact_'.$i.'" name="level_price_impact['.$i.']" value="'.price($prodcomb->combination_price_levels[$i]->variation_price).'">';
743 print '<input type="checkbox" class="level_price_impact_percent" id="level_price_impact_percent_'.$i.'" name="level_price_impact_percent['.$i.']" '.(!empty($prodcomb->combination_price_levels[$i]->variation_price_percentage) ? ' checked' : '').'> <label for="level_price_impact_percent_'.$i.'">'.$langs->trans('PercentageVariation').'</label>';
744
745 print '</td>';
746 print '</tr>';
747 }
748 }
749
750 if ($object->isProduct()) {
751 print '<tr>';
752 print '<td><label for="weight_impact">'.$langs->trans('WeightImpact').'</label></td>';
753 print '<td><input type="text" id="weight_impact" name="weight_impact" value="'.price($weight_impact).'"></td>';
754 print '</tr>';
755 }
756
757 print '</table>';
758 }
759
760 if (getDolGlobalString('PRODUIT_MULTIPRICES')) {
761 ?>
762 <script>
763 $(document).ready(function() {
764 // Apply level 1 impact to all prices impact levels
765 $('body').on('click', '#apply-price-impact-to-all-level', function(e) {
766 e.preventDefault();
767 let priceImpact = $( "#level_price_impact_1" ).val();
768 let priceImpactPrecent = $( "#level_price_impact_percent_1" ).prop("checked");
769
770 var multipricelimit = <?php print intval($conf->global->PRODUIT_MULTIPRICES_LIMIT); ?>
771
772 for (let i = 2; i <= multipricelimit; i++) {
773 $( "#level_price_impact_" + i ).val(priceImpact);
774 $( "#level_price_impact_percent_" + i ).prop("checked", priceImpactPrecent);
775 }
776 });
777 });
778 </script>
779 <?php
780 }
781
782 print dol_get_fiche_end(); ?>
783
784 <div style="text-align: center">
785 <input type="submit" name="create" <?php if (!is_array($productCombination2ValuePairs1)) {
786 print ' disabled="disabled"';
787 } ?> value="<?php echo $action == 'add' ? $langs->trans('Create') : $langs->trans("Save") ?>" class="button button-save">
788 &nbsp;
789 <input type="submit" name="cancel" value="<?php echo $langs->trans("Cancel"); ?>" class="button button-cancel">
790 </div>
791
792 <?php
793
794 print '</form>';
795 } else {
796 if ($action === 'delete') {
797 if ($prodcomb->fetch($combination_id) > 0) {
798 $prodstatic->fetch($prodcomb->fk_product_child);
799
800 print $form->formconfirm(
801 "combinations.php?id=".urlencode((string) ($id))."&combination_id=".urlencode((string) ($combination_id)),
802 $langs->trans('Delete'),
803 $langs->trans('ProductCombinationDeleteDialog', $prodstatic->ref),
804 "confirm_deletecombination",
805 array(array('label' => $langs->trans('DeleteLinkedProduct'), 'type' => 'checkbox', 'name' => 'delete_product', 'value' => false)),
806 0,
807 1
808 );
809 }
810 } elseif ($action === 'copy') {
811 print $form->formconfirm('combinations.php?id='.$id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneProductCombinations'), 'confirm_copycombination', array(array('type' => 'text', 'label' => $langs->trans('CloneDestinationReference'), 'name' => 'dest_product')), 0, 1);
812 }
813
814 $comb2val = new ProductCombination2ValuePair($db);
815
816 if ($productCombinations) {
817 ?>
818
819 <script type="text/javascript">
820 jQuery(document).ready(function() {
821
822 jQuery('input[name="select_all"]').click(function() {
823
824 if (jQuery(this).prop('checked')) {
825 var checked = true;
826 } else {
827 var checked = false;
828 }
829
830 jQuery('table.liste input[type="checkbox"]').prop('checked', checked);
831 });
832
833 jQuery('input[name^="select["]').click(function() {
834 jQuery('input[name="select_all"]').prop('checked', false);
835 });
836
837 });
838 </script>
839
840 <?php
841 }
842
843 // Buttons
844 print '<div class="tabsAction">';
845
846 print ' <div class="inline-block divButAction">';
847
848 print '<a href="combinations.php?id='.$object->id.'&action=add&token='.newToken().'" class="butAction">'.$langs->trans('NewProductCombination').'</a>'; // NewVariant
849
850 if ($productCombinations) {
851 print '<a href="combinations.php?id='.$object->id.'&action=copy&token='.newToken().'" class="butAction">'.$langs->trans('PropagateVariant').'</a>';
852 }
853
854 print ' </div>';
855
856 print '</div>';
857
858
859
860 $arrayofselected = is_array($toselect) ? $toselect : array();
861
862
863 // List of variants
864 print '<form method="POST" action="'.$_SERVER["PHP_SELF"] .'?id='.$object->id.'">';
865 print '<input type="hidden" name="token" value="'.newToken().'">';
866 print '<input type="hidden" name="action" value="massaction">';
867 print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
868
869 // List of mass actions available
870
871 $aaa = '';
872 if (count($productCombinations)) {
873 $aaa = '<select id="bulk_action" name="massaction" class="flat">';
874 $aaa .= ' <option value="nothing">&nbsp;</option>';
875 $aaa .= ' <option value="not_buy" data-html="'.dol_escape_htmltag(img_picto($langs->trans("SetToStatus"), 'stop-circle', 'class="pictofixedwidth"').$langs->trans('SetToStatus', $langs->transnoentitiesnoconv('ProductStatusNotOnBuy'))).'">'.$langs->trans('ProductStatusNotOnBuy').'</option>';
876 $aaa .= ' <option value="not_sell" data-html="'.dol_escape_htmltag(img_picto($langs->trans("SetToStatus"), 'stop-circle', 'class="pictofixedwidth"').$langs->trans('SetToStatus', $langs->transnoentitiesnoconv('ProductStatusNotOnSell'))).'">'.$langs->trans('ProductStatusNotOnSell').'</option>';
877 $aaa .= ' <option value="on_buy" data-html="'.dol_escape_htmltag(img_picto($langs->trans("SetToStatus"), 'stop-circle', 'class="pictofixedwidth"').$langs->trans('SetToStatus', $langs->transnoentitiesnoconv('ProductStatusOnBuy'))).'">'.$langs->trans('ProductStatusOnBuy').'</option>';
878 $aaa .= ' <option value="on_sell" data-html="'.dol_escape_htmltag(img_picto($langs->trans("SetToStatus"), 'stop-circle', 'class="pictofixedwidth"').$langs->trans('SetToStatus', $langs->transnoentitiesnoconv('ProductStatusOnSell'))).'">'.$langs->trans('ProductStatusOnSell').'</option>';
879 $aaa .= ' <option value="delete" data-html="'.dol_escape_htmltag(img_picto($langs->trans("Delete"), 'delete', 'class="pictofixedwidth"').$langs->trans('Delete')).'">'.$langs->trans('Delete').'</option>';
880 $aaa .= '</select>';
881 $aaa .= ajax_combobox("bulk_action");
882 $aaa .= '<input type="submit" value="'.dol_escape_htmltag($langs->trans("Apply")).'" class="button small">';
883 }
884 $massactionbutton = $aaa;
885
886 $title = $langs->trans("ProductCombinations");
887
888 print_barre_liste($title, 0, $_SERVER["PHP_SELF"], '', $sortfield, $sortorder, $aaa, 0);
889
890 print '<div class="div-table-responsive">'; ?>
891 <table class="liste">
892 <tr class="liste_titre">
893 <?php
894 // Action column
895 if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
896 print '<td class="liste_titre center">';
897 $searchpicto = $form->showCheckAddButtons('checkforselect', 1);
898 print $searchpicto;
899 print '</td>';
900 } ?>
901 <td class="liste_titre"><?php echo $langs->trans('Product') ?></td>
902 <td class="liste_titre"><?php echo $langs->trans('Attributes') ?></td>
903 <td class="liste_titre right"><?php echo $langs->trans('PriceImpact') ?></td>
904 <?php if ($object->isProduct()) {
905 print'<td class="liste_titre right">'.$langs->trans('WeightImpact').'</td>';
906 } ?>
907 <td class="liste_titre center"><?php echo $langs->trans('OnSell') ?></td>
908 <td class="liste_titre center"><?php echo $langs->trans('OnBuy') ?></td>
909 <td class="liste_titre"></td>
910 <?php
911 // Action column
912 if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
913 print '<td class="liste_titre center">';
914 $searchpicto = $form->showCheckAddButtons('checkforselect', 1);
915 print $searchpicto;
916 print '</td>';
917 } ?>
918 </tr>
919 <?php
920
921 if (count($productCombinations)) {
922 foreach ($productCombinations as $currcomb) {
923 $prodstatic->fetch($currcomb->fk_product_child);
924 print '<tr class="oddeven">';
925
926 // Action column
927 if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
928 print '<td class="nowrap center">';
929 if (!empty($productCombinations) || $massactionbutton || $massaction) { // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined
930 $selected = 0;
931 if (in_array($prodstatic->id, $arrayofselected)) {
932 $selected = 1;
933 }
934 print '<input id="cb'.$prodstatic->id.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$prodstatic->id.'"'.($selected ? ' checked="checked"' : '').'>';
935 }
936 print '</td>';
937 }
938
939 print '<td>'.$prodstatic->getNomUrl(1).'</td>';
940 print '<td>';
941 foreach ($comb2val->fetchByFkCombination($currcomb->id) as $pc2v) {
942 print dol_htmlentities($pc2v).'<br>';
943 }
944 print '</td>';
945 print '<td class="right">'.($currcomb->variation_price >= 0 ? '+' : '').price($currcomb->variation_price).($currcomb->variation_price_percentage ? ' %' : '').'</td>';
946 if ($object->isProduct()) {
947 print '<td class="right">'.($currcomb->variation_weight >= 0 ? '+' : '').price($currcomb->variation_weight).' '.measuringUnitString(0, 'weight', $prodstatic->weight_units).'</td>';
948 }
949 print '<td class="center">'.$prodstatic->getLibStatut(2, 0).'</td>';
950 print '<td class="center">'.$prodstatic->getLibStatut(2, 1).'</td>';
951
952 print '<td class="right">';
953 print '<a class="paddingleft paddingright editfielda" href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&action=edit&token='.newToken().'&combination_id='.$currcomb->id.'">'.img_edit().'</a>';
954 print '<a class="paddingleft paddingright" href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&action=delete&token='.newToken().'&combination_id='.$currcomb->id.'">'.img_delete().'</a>';
955 print '</td>';
956
957 // Action column
958 if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
959 print '<td class="nowrap center">';
960 if (!empty($productCombinations) || $massactionbutton || $massaction) { // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined
961 $selected = 0;
962 if (in_array($prodstatic->id, $arrayofselected)) {
963 $selected = 1;
964 }
965 print '<input id="cb'.$prodstatic->id.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$prodstatic->id.'"'.($selected ? ' checked="checked"' : '').'>';
966 }
967 print '</td>';
968 }
969
970 print '</tr>';
971 }
972 } else {
973 print '<tr><td colspan="8"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
974 }
975 print '</table>';
976 print '</div>';
977 print '</form>';
978 }
979}
980
981// End of page
982llxFooter();
983$db->close();
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:459
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:71
Class to manage generation of HTML components Only common components must be here.
Class ProductAttribute Used to represent a Product attribute Examples:
Class ProductAttributeValue Used to represent a product attribute value.
Class ProductCombination2ValuePair Used to represent the relation between a variant and its attribute...
Class ProductCombination Used to represent the relation between a product and one of its variants.
Class ProductCombinationLevel Used to represent a product combination Level.
Class to manage products or services.
llxFooter()
Footer empty.
Definition document.php:107
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
vatrate($rate, $addpercent=false, $info_bits=0, $usestarfornpr=0, $html=0)
Return a string with VAT rate label formatted for view output Used into pdf and HTML pages.
print_barre_liste($title, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $selectlimitsuffix=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dol_htmlentities($string, $flags=ENT_QUOTES|ENT_SUBSTITUTE, $encoding='UTF-8', $double_encode=false)
Replace htmlentities functions.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
GETPOSTFLOAT($paramname, $rounding='')
Return the value of a $_GET or $_POST supervariable, converted into float.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
img_edit($titlealt='default', $float=0, $other='')
Show logo edit/modify fiche.
img_edit_remove($titlealt='default', $other='')
Show logo -.
treeview li table
No Email.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
product_prepare_head($object)
Prepare array with list of tabs.
measuringUnitString($unitid, $measuring_style='', $unitscale='', $use_short_label=0, $outputlangs=null)
Return translation label of a unit key.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:149
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:152
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.