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