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