combinations.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. require '../main.inc.php';
  21. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  22. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  23. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttribute.class.php';
  24. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttributeValue.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php';
  27. $langs->loadLangs(array("products", "other"));
  28. $id = GETPOST('id', 'int');
  29. $valueid = GETPOST('valueid', 'int');
  30. $ref = GETPOST('ref', 'alpha');
  31. $weight_impact = price2num(GETPOST('weight_impact', 'alpha'), 2);
  32. $price_impact_percent = (bool) GETPOST('price_impact_percent');
  33. if ($price_impact_percent) {
  34. $price_impact = price2num(GETPOST('price_impact', 'alpha'), 2);
  35. } else {
  36. $price_impact = price2num(GETPOST('price_impact', 'alpha'), 'MU');
  37. }
  38. $level_price_impact = GETPOST('level_price_impact', 'array');
  39. $level_price_impact_percent = GETPOST('level_price_impact_percent', 'array');
  40. $reference = GETPOST('reference', 'alpha');
  41. $form = new Form($db);
  42. $action = GETPOST('action', 'aZ09');
  43. $massaction = GETPOST('massaction', 'alpha');
  44. $show_files = GETPOST('show_files', 'int');
  45. $confirm = GETPOST('confirm', 'alpha');
  46. $toselect = GETPOST('toselect', 'array');
  47. $cancel = GETPOST('cancel', 'alpha');
  48. $delete_product = GETPOST('delete_product', 'alpha');
  49. $subaction = GETPOST('subaction', 'aZ09');
  50. // Security check
  51. $fieldvalue = (!empty($id) ? $id : $ref);
  52. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  53. $prodstatic = new Product($db);
  54. $prodattr = new ProductAttribute($db);
  55. $prodattr_val = new ProductAttributeValue($db);
  56. $object = new Product($db);
  57. if ($id > 0 || $ref) {
  58. $object->fetch($id, $ref);
  59. }
  60. $selectedvariant = $_SESSION['addvariant_'.$object->id];
  61. // Security check
  62. if (empty($conf->variants->enabled)) {
  63. accessforbidden('Module not enabled');
  64. }
  65. if ($user->socid > 0) { // Protection if external user
  66. accessforbidden();
  67. }
  68. if ($object->id > 0) {
  69. if ($object->type == $object::TYPE_PRODUCT) {
  70. restrictedArea($user, 'produit', $object->id, 'product&product', '', '');
  71. }
  72. if ($object->type == $object::TYPE_SERVICE) {
  73. restrictedArea($user, 'service', $object->id, 'product&product', '', '');
  74. }
  75. } else {
  76. restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
  77. }
  78. /*
  79. * Actions
  80. */
  81. if ($cancel) {
  82. $action = '';
  83. $massaction = '';
  84. unset($_SESSION['addvariant_'.$object->id]);
  85. }
  86. if (!$object->isProduct() && !$object->isService()) {
  87. header('Location: '.dol_buildpath('/product/card.php?id='.$object->id, 2));
  88. exit();
  89. }
  90. if ($action == 'add') {
  91. unset($selectedvariant);
  92. unset($_SESSION['addvariant_'.$object->id]);
  93. }
  94. if ($action == 'create' && GETPOST('selectvariant', 'alpha')) { // We click on select combination
  95. $action = 'add';
  96. $attribute_id = GETPOST('attribute', 'int');
  97. $attribute_value_id = GETPOST('value', 'int');
  98. if ($attribute_id> 0 && $attribute_value_id > 0) {
  99. $feature = $attribute_id . '-' . $attribute_value_id;
  100. $selectedvariant[$feature] = $feature;
  101. $_SESSION['addvariant_'.$object->id] = $selectedvariant;
  102. }
  103. }
  104. if ($action == 'create' && $subaction == 'delete') { // We click on select combination
  105. $action = 'add';
  106. $feature = GETPOST('feature', 'intcomma');
  107. if (isset($selectedvariant[$feature])) {
  108. unset($selectedvariant[$feature]);
  109. $_SESSION['addvariant_'.$object->id] = $selectedvariant;
  110. }
  111. }
  112. $prodcomb = new ProductCombination($db);
  113. $prodcomb2val = new ProductCombination2ValuePair($db);
  114. $productCombination2ValuePairs1 = array();
  115. if (($action == 'add' || $action == 'create') && empty($massaction) && !GETPOST('selectvariant', 'alpha') && empty($subaction)) { // We click on Create all defined combinations
  116. //$features = GETPOST('features', 'array');
  117. $features = $_SESSION['addvariant_'.$object->id];
  118. if (!$features) {
  119. if ($action == 'create') {
  120. setEventMessages($langs->trans('ErrorFieldsRequired'), null, 'errors');
  121. }
  122. } else {
  123. $reference = trim($reference);
  124. if (empty($reference)) {
  125. $reference = false;
  126. }
  127. $weight_impact = price2num($weight_impact);
  128. $price_impact = price2num($price_impact);
  129. // for conf PRODUIT_MULTIPRICES
  130. if ($conf->global->PRODUIT_MULTIPRICES) {
  131. $level_price_impact = array_map('price2num', $level_price_impact);
  132. } else {
  133. $level_price_impact = array(1 => $price_impact);
  134. $level_price_impact_percent = array(1 => $price_impact_percent);
  135. }
  136. $sanit_features = array();
  137. //First, sanitize
  138. foreach ($features as $feature) {
  139. $explode = explode('-', $feature);
  140. if ($prodattr->fetch($explode[0]) <= 0 || $prodattr_val->fetch($explode[1]) <= 0) {
  141. continue;
  142. }
  143. // Valuepair
  144. $sanit_features[$explode[0]] = $explode[1];
  145. $tmp = new ProductCombination2ValuePair($db);
  146. $tmp->fk_prod_attr = $explode[0];
  147. $tmp->fk_prod_attr_val = $explode[1];
  148. $productCombination2ValuePairs1[] = $tmp;
  149. }
  150. $db->begin();
  151. // sanit_feature is an array with 1 (and only 1) value per attribute.
  152. // For example: Color->blue, Size->Small, Option->2
  153. //var_dump($sanit_features);
  154. if (!$prodcomb->fetchByProductCombination2ValuePairs($id, $sanit_features)) {
  155. $result = $prodcomb->createProductCombination($user, $object, $sanit_features, array(), $level_price_impact_percent, $level_price_impact, $weight_impact, $reference);
  156. if ($result > 0) {
  157. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  158. unset($_SESSION['addvariant_'.$object->id]);
  159. $db->commit();
  160. header('Location: '.dol_buildpath('/variants/combinations.php?id='.$id, 2));
  161. exit();
  162. } else {
  163. $langs->load("errors");
  164. setEventMessages($prodcomb->error, $prodcomb->errors, 'errors');
  165. }
  166. } else {
  167. setEventMessages($langs->trans('ErrorRecordAlreadyExists'), null, 'errors');
  168. }
  169. $db->rollback();
  170. }
  171. } elseif (!empty($massaction)) {
  172. $bulkaction = $massaction;
  173. $error = 0;
  174. $db->begin();
  175. foreach ($toselect as $prodid) {
  176. // need create new of Product to prevent rename dir behavior
  177. $prodstatic = new Product($db);
  178. if ($prodstatic->fetch($prodid) < 0) {
  179. continue;
  180. }
  181. if ($bulkaction == 'on_sell') {
  182. $prodstatic->status = 1;
  183. $res = $prodstatic->update($prodstatic->id, $user);
  184. if ($res <= 0) {
  185. setEventMessages($prodstatic->error, $prodstatic->errors, 'errors');
  186. $error++;
  187. break;
  188. }
  189. } elseif ($bulkaction == 'on_buy') {
  190. $prodstatic->status_buy = 1;
  191. $res = $prodstatic->update($prodstatic->id, $user);
  192. if ($res <= 0) {
  193. setEventMessages($prodstatic->error, $prodstatic->errors, 'errors');
  194. $error++;
  195. break;
  196. }
  197. } elseif ($bulkaction == 'not_sell') {
  198. $prodstatic->status = 0;
  199. $res = $prodstatic->update($prodstatic->id, $user);
  200. if ($res <= 0) {
  201. setEventMessages($prodstatic->error, $prodstatic->errors, 'errors');
  202. $error++;
  203. break;
  204. }
  205. } elseif ($bulkaction == 'not_buy') {
  206. $prodstatic->status_buy = 0;
  207. $res = $prodstatic->update($prodstatic->id, $user);
  208. if ($res <= 0) {
  209. setEventMessages($prodstatic->error, $prodstatic->errors, 'errors');
  210. $error++;
  211. break;
  212. }
  213. } elseif ($bulkaction == 'delete') {
  214. $res = $prodstatic->delete($user, $prodstatic->id);
  215. if ($res <= 0) {
  216. setEventMessages($prodstatic->error, $prodstatic->errors, 'errors');
  217. $error++;
  218. break;
  219. }
  220. } else {
  221. break;
  222. }
  223. }
  224. if ($error) {
  225. $db->rollback();
  226. if (empty($prodstatic->error)) {
  227. setEventMessages($langs->trans('CoreErrorMessage'), null, 'errors');
  228. }
  229. } else {
  230. $db->commit();
  231. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  232. }
  233. } elseif ($action === 'update' && $valueid > 0) {
  234. if ($prodcomb->fetch($valueid) < 0) {
  235. dol_print_error($db, $langs->trans('ErrorRecordNotFound'));
  236. exit();
  237. }
  238. $prodcomb->variation_weight = price2num($weight_impact);
  239. // for conf PRODUIT_MULTIPRICES
  240. if ($conf->global->PRODUIT_MULTIPRICES) {
  241. $level_price_impact = array_map('price2num', $level_price_impact);
  242. $prodcomb->variation_price = $level_price_impact[1];
  243. $prodcomb->variation_price_percentage = (bool) $level_price_impact_percent[1];
  244. } else {
  245. $level_price_impact = array(1 => $price_impact);
  246. $level_price_impact_percent = array(1 => $price_impact_percent);
  247. $prodcomb->variation_price = $price_impact;
  248. $prodcomb->variation_price_percentage = $price_impact_percent;
  249. }
  250. if ($conf->global->PRODUIT_MULTIPRICES) {
  251. $prodcomb->combination_price_levels = array();
  252. for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
  253. $productCombinationLevel = new ProductCombinationLevel($db);
  254. $productCombinationLevel->fk_product_attribute_combination = $prodcomb->id;
  255. $productCombinationLevel->fk_price_level = $i;
  256. $productCombinationLevel->variation_price = $level_price_impact[$i];
  257. $productCombinationLevel->variation_price_percentage = (bool) $level_price_impact_percent[$i];
  258. $prodcomb->combination_price_levels[$i] = $productCombinationLevel;
  259. }
  260. }
  261. if ($prodcomb->update($user) > 0) {
  262. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  263. header('Location: '.dol_buildpath('/variants/combinations.php?id='.$id, 2));
  264. exit();
  265. } else {
  266. setEventMessages($prodcomb->error, $prodcomb->errors, 'errors');
  267. }
  268. }
  269. // Reload variants
  270. $productCombinations = $prodcomb->fetchAllByFkProductParent($object->id);
  271. if ($action === 'confirm_deletecombination') {
  272. if ($prodcomb->fetch($valueid) > 0) {
  273. $db->begin();
  274. if ($prodcomb->delete($user) > 0 && (empty($delete_product) || ($delete_product == 'on' && $prodstatic->fetch($prodcomb->fk_product_child) > 0 && $prodstatic->delete($user) > 0))) {
  275. $db->commit();
  276. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  277. header('Location: '.dol_buildpath('/variants/combinations.php?id='.$object->id, 2));
  278. exit();
  279. }
  280. $db->rollback();
  281. setEventMessages($langs->trans('ProductCombinationAlreadyUsed'), null, 'errors');
  282. $action = '';
  283. }
  284. } elseif ($action === 'edit') {
  285. if ($prodcomb->fetch($valueid) < 0) {
  286. dol_print_error($db, $langs->trans('ErrorRecordNotFound'));
  287. exit();
  288. }
  289. $weight_impact = $prodcomb->variation_weight;
  290. $price_impact = $prodcomb->variation_price;
  291. $price_impact_percent = $prodcomb->variation_price_percentage;
  292. $productCombination2ValuePairs1 = $prodcomb2val->fetchByFkCombination($valueid);
  293. } elseif ($action === 'confirm_copycombination') {
  294. //Check destination product
  295. $dest_product = GETPOST('dest_product');
  296. if ($prodstatic->fetch('', $dest_product) > 0) {
  297. //To prevent from copying to the same product
  298. if ($prodstatic->ref != $object->ref) {
  299. if ($prodcomb->copyAll($user, $object->id, $prodstatic) > 0) {
  300. header('Location: '.dol_buildpath('/variants/combinations.php?id='.$prodstatic->id, 2));
  301. exit();
  302. } else {
  303. setEventMessages($langs->trans('ErrorCopyProductCombinations'), null, 'errors');
  304. }
  305. }
  306. } else {
  307. setEventMessages($langs->trans('ErrorDestinationProductNotFound'), null, 'errors');
  308. }
  309. }
  310. /*
  311. * View
  312. */
  313. $form = new Form($db);
  314. if (!empty($id) || !empty($ref)) {
  315. llxHeader("", "", $langs->trans("CardProduct".$object->type));
  316. $showbarcode = empty($conf->barcode->enabled) ? 0 : 1;
  317. if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->barcode->lire_advance)) {
  318. $showbarcode = 0;
  319. }
  320. $head = product_prepare_head($object);
  321. $titre = $langs->trans("CardProduct".$object->type);
  322. $picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product');
  323. print dol_get_fiche_head($head, 'combinations', $titre, -1, $picto);
  324. $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?type='.$object->type.'">'.$langs->trans("BackToList").'</a>';
  325. $object->next_prev_filter = " fk_product_type = ".$object->type;
  326. dol_banner_tab($object, 'ref', $linkback, ($user->socid ? 0 : 1), 'ref', '', '', '', 0, '', '');
  327. print '<div class="fichecenter">';
  328. print '<div class="underbanner clearboth"></div>';
  329. print '<table class="border centpercent tableforfield">';
  330. // Type
  331. if (!empty($conf->product->enabled) && !empty($conf->service->enabled)) {
  332. $typeformat = 'select;0:'.$langs->trans("Product").',1:'.$langs->trans("Service");
  333. print '<tr><td class="titlefieldcreate">';
  334. print (empty($conf->global->PRODUCT_DENY_CHANGE_PRODUCT_TYPE)) ? $form->editfieldkey("Type", 'fk_product_type', $object->type, $object, $usercancreate, $typeformat) : $langs->trans('Type');
  335. print '</td><td>';
  336. print $form->editfieldval("Type", 'fk_product_type', $object->type, $object, $usercancreate, $typeformat);
  337. print '</td></tr>';
  338. }
  339. // TVA
  340. print '<tr><td class="titlefieldcreate">'.$langs->trans("DefaultTaxRate").'</td><td>';
  341. $positiverates = '';
  342. if (price2num($object->tva_tx)) {
  343. $positiverates .= ($positiverates ? '/' : '').price2num($object->tva_tx);
  344. }
  345. if (price2num($object->localtax1_type)) {
  346. $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax1_tx);
  347. }
  348. if (price2num($object->localtax2_type)) {
  349. $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax2_tx);
  350. }
  351. if (empty($positiverates)) {
  352. $positiverates = '0';
  353. }
  354. echo vatrate($positiverates.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), '%', $object->tva_npr);
  355. /*
  356. if ($object->default_vat_code)
  357. {
  358. print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')';
  359. }
  360. else print vatrate($object->tva_tx, true, $object->tva_npr, true);*/
  361. print '</td></tr>';
  362. // Price
  363. print '<tr><td>'.$langs->trans("SellingPrice").'</td><td>';
  364. if ($object->price_base_type == 'TTC') {
  365. print price($object->price_ttc).' '.$langs->trans($object->price_base_type);
  366. } else {
  367. print price($object->price).' '.$langs->trans($object->price_base_type);
  368. }
  369. print '</td></tr>';
  370. // Price minimum
  371. print '<tr><td>'.$langs->trans("MinPrice").'</td><td>';
  372. if ($object->price_base_type == 'TTC') {
  373. print price($object->price_min_ttc).' '.$langs->trans($object->price_base_type);
  374. } else {
  375. print price($object->price_min).' '.$langs->trans($object->price_base_type);
  376. }
  377. print '</td></tr>';
  378. // Weight
  379. print '<tr><td>'.$langs->trans("Weight").'</td><td>';
  380. if ($object->weight != '') {
  381. print $object->weight." ".measuringUnitString(0, "weight", $object->weight_units);
  382. } else {
  383. print '&nbsp;';
  384. }
  385. print "</td></tr>\n";
  386. print "</table>\n";
  387. print '</div>';
  388. print '<div style="clear:both"></div>';
  389. print dol_get_fiche_end();
  390. $listofvariantselected = '';
  391. // Create or edit a varian
  392. if ($action == 'add' || ($action == 'edit')) {
  393. if ($action == 'add') {
  394. $title = $langs->trans('NewProductCombination');
  395. // print dol_get_fiche_head();
  396. $features = $_SESSION['addvariant_'.$object->id];
  397. //First, sanitize
  398. $listofvariantselected = '<div id="parttoaddvariant">';
  399. if (!empty($features)) {
  400. $toprint = array();
  401. foreach ($features as $feature) {
  402. $explode = explode('-', $feature);
  403. if ($prodattr->fetch($explode[0]) <= 0 || $prodattr_val->fetch($explode[1]) <= 0) {
  404. continue;
  405. }
  406. $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #ddd;">' . $prodattr->label.' : '.$prodattr_val->value .
  407. ' <a class="reposition" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=create&subaction=delete&feature='.urlencode($feature).'">' . img_delete() . '</a></li>';
  408. }
  409. $listofvariantselected .= '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
  410. }
  411. $listofvariantselected .= '</div>';
  412. //print dol_get_fiche_end();
  413. } else {
  414. $title = $langs->trans('EditProductCombination');
  415. }
  416. if ($action == 'add') {
  417. $prodattr_all = $prodattr->fetchAll();
  418. if (!$selected) {
  419. $selected = $prodattr_all[key($prodattr_all)]->id;
  420. }
  421. $prodattr_alljson = array();
  422. foreach ($prodattr_all as $each) {
  423. $prodattr_alljson[$each->id] = $each;
  424. }
  425. ?>
  426. <script type="text/javascript">
  427. variants_available = <?php echo json_encode($prodattr_alljson); ?>;
  428. variants_selected = {
  429. index: [],
  430. info: []
  431. };
  432. <?php
  433. foreach ($productCombination2ValuePairs1 as $pc2v) {
  434. $prodattr_val->fetch($pc2v->fk_prod_attr_val);
  435. ?>
  436. variants_selected.index.push(<?php echo $pc2v->fk_prod_attr ?>);
  437. variants_selected.info[<?php echo $pc2v->fk_prod_attr ?>] = {
  438. attribute: variants_available[<?php echo $pc2v->fk_prod_attr ?>],
  439. value: {
  440. id: <?php echo $pc2v->fk_prod_attr_val ?>,
  441. label: '<?php echo $prodattr_val->value ?>'
  442. }
  443. };
  444. <?php
  445. }
  446. ?>
  447. restoreAttributes = function() {
  448. jQuery("select[name=attribute]").empty().append('<option value="-1">&nbsp;</option>');
  449. jQuery.each(variants_available, function (key, val) {
  450. if (jQuery.inArray(val.id, variants_selected.index) == -1) {
  451. jQuery("select[name=attribute]").append('<option value="' + val.id + '">' + val.label + '</option>');
  452. }
  453. });
  454. };
  455. jQuery(document).ready(function() {
  456. jQuery("select#attribute").change(function () {
  457. console.log("Change of field variant attribute");
  458. var select = jQuery("select#value");
  459. if (!jQuery(this).val().length || jQuery(this).val() == '-1') {
  460. select.empty();
  461. select.append('<option value="-1">&nbsp;</option>');
  462. return;
  463. }
  464. select.empty().append('<option value="">Loading...</option>');
  465. jQuery.getJSON("ajax/get_attribute_values.php", {
  466. id: jQuery(this).val()
  467. }, function(data) {
  468. if (data.error) {
  469. select.empty();
  470. select.append('<option value="-1">&nbsp;</option>');
  471. return alert(data.error);
  472. }
  473. select.empty();
  474. select.append('<option value="-1">&nbsp;</option>');
  475. jQuery(data).each(function (key, val) {
  476. keyforoption = val.id
  477. valforoption = val.value
  478. select.append('<option value="' + keyforoption + '">' + valforoption + '</option>');
  479. });
  480. });
  481. });
  482. });
  483. </script>
  484. <?php
  485. }
  486. print '<br>';
  487. print load_fiche_titre($title);
  488. print '<form method="post" id="combinationform" action="'.$_SERVER["PHP_SELF"] .'?id='.$object->id.'">'."\n";
  489. print '<input type="hidden" name="token" value="'.newToken().'">';
  490. print '<input type="hidden" name="action" value="'.(($valueid > 0) ? "update" : "create").'">'."\n";
  491. if ($valueid > 0) {
  492. print '<input type="hidden" name="valueid" value="'.$valueid.'">'."\n";
  493. }
  494. print dol_get_fiche_head();
  495. if ($action == 'add') {
  496. print '<table class="border" style="width: 100%">';
  497. print "<!-- Variant -->\n";
  498. print '<tr>';
  499. print '<td class="titlefieldcreate fieldrequired"><label for="attribute">'.$langs->trans('ProductAttribute').'</label></td>';
  500. print '<td>';
  501. if (is_array($prodattr_all)) {
  502. print '<select class="flat minwidth100" id="attribute" name="attribute">';
  503. print '<option value="-1">&nbsp;</option>';
  504. foreach ($prodattr_all as $attr) {
  505. //print '<option value="'.$attr->id.'"'.($attr->id == GETPOST('attribute', 'int') ? ' selected="selected"' : '').'>'.$attr->label.'</option>';
  506. print '<option value="'.$attr->id.'">'.$attr->label.'</option>';
  507. }
  508. print '</select>';
  509. }
  510. $htmltext = $langs->trans("GoOnMenuToCreateVairants", $langs->transnoentities("Product"), $langs->transnoentities("VariantAttributes"));
  511. print $form->textwithpicto('', $htmltext);
  512. /*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).'">';
  513. print $langs->trans("Create");
  514. print '</a>';*/
  515. print '</td>';
  516. print '</tr>';
  517. ?>
  518. <!-- Value -->
  519. <tr>
  520. <td class="fieldrequired"><label for="value"><?php echo $langs->trans('Value') ?></label></td>
  521. <td>
  522. <select class="flat minwidth100" id="value" name="value">
  523. <option value="-1">&nbsp;</option>
  524. </select>
  525. <?php
  526. $htmltext = $langs->trans("GoOnMenuToCreateVairants", $langs->transnoentities("Product"), $langs->transnoentities("VariantAttributes"));
  527. print $form->textwithpicto('', $htmltext);
  528. /*
  529. 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).'">';
  530. print $langs->trans("Create");
  531. print '</a>';
  532. */
  533. ?>
  534. </td>
  535. </tr>
  536. <tr>
  537. <td></td><td>
  538. <input type="submit" class="button" name="selectvariant" id="selectvariant" value="<?php echo dol_escape_htmltag($langs->trans("SelectCombination")); ?>">
  539. </td>
  540. </tr>
  541. <?php
  542. print '<tr><td></td><td>';
  543. print $listofvariantselected;
  544. print '</td>';
  545. print '</tr>';
  546. print '</table>';
  547. print '<hr>';
  548. }
  549. if (is_array($productCombination2ValuePairs1)) {
  550. print '<table class="border" style="width: 100%">';
  551. // When in edit mode
  552. if (is_array($productCombination2ValuePairs1) && count($productCombination2ValuePairs1)) {
  553. ?>
  554. <tr>
  555. <td class="titlefieldcreate tdtop"><label for="features"><?php echo $langs->trans('Combination') ?></label></td>
  556. <td class="tdtop">
  557. <div class="inline-block valignmiddle quatrevingtpercent">
  558. <?php
  559. foreach ($productCombination2ValuePairs1 as $key => $val) {
  560. $result1 = $prodattr->fetch($val->fk_prod_attr);
  561. $result2 = $prodattr_val->fetch($val->fk_prod_attr_val);
  562. //print 'rr'.$result1.' '.$result2;
  563. if ($result1 > 0 && $result2 > 0) {
  564. print $prodattr->label.' - '.$prodattr_val->value.'<br>';
  565. // TODO Add delete link
  566. }
  567. }
  568. ?>
  569. </div>
  570. <!-- <div class="inline-block valignmiddle">
  571. <a href="#" class="inline-block valignmiddle button" id="delfeature"><?php echo img_edit_remove() ?></a>
  572. </div>-->
  573. </td>
  574. <td>
  575. </td>
  576. </tr>
  577. <?php
  578. }
  579. ?>
  580. <tr>
  581. <td><label for="reference"><?php echo $langs->trans('Reference') ?></label></td>
  582. <td><input type="text" id="reference" name="reference" value="<?php echo trim($reference) ?>"></td>
  583. </tr>
  584. <?php
  585. if (empty($conf->global->PRODUIT_MULTIPRICES)) {
  586. ?>
  587. <tr>
  588. <td><label for="price_impact"><?php echo $langs->trans('PriceImpact') ?></label></td>
  589. <td><input type="text" id="price_impact" name="price_impact" value="<?php echo price($price_impact) ?>">
  590. <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>
  591. </td>
  592. </tr>
  593. <?php
  594. } else {
  595. $prodcomb->fetchCombinationPriceLevels();
  596. for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
  597. print '<tr>';
  598. print '<td><label for="level_price_impact_'.$i.'">'.$langs->trans('ImpactOnPriceLevel', $i).'</label>';
  599. if ($i === 1) {
  600. print ' <a id="apply-price-impact-to-all-level" class="classfortooltip" href="#" title="'.$langs->trans('ApplyToAllPriceImpactLevelHelp').'">('.$langs->trans('ApplyToAllPriceImpactLevel').')</a>';
  601. }
  602. print '</td>';
  603. 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).'">';
  604. 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>';
  605. print '</td>';
  606. print '</tr>';
  607. }
  608. }
  609. if ($object->isProduct()) {
  610. print '<tr>';
  611. print '<td><label for="weight_impact">'.$langs->trans('WeightImpact').'</label></td>';
  612. print '<td><input type="text" id="weight_impact" name="weight_impact" value="'.price($weight_impact).'"></td>';
  613. print '</tr>';
  614. }
  615. print '</table>';
  616. }
  617. if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
  618. ?>
  619. <script>
  620. $(document).ready(function() {
  621. // Apply level 1 impact to all prices impact levels
  622. $('body').on('click', '#apply-price-impact-to-all-level', function(e) {
  623. e.preventDefault();
  624. let priceImpact = $( "#level_price_impact_1" ).val();
  625. let priceImpactPrecent = $( "#level_price_impact_percent_1" ).prop("checked");
  626. var multipricelimit = <?php print intval($conf->global->PRODUIT_MULTIPRICES_LIMIT); ?>
  627. for (let i = 2; i <= multipricelimit; i++) {
  628. $( "#level_price_impact_" + i ).val(priceImpact);
  629. $( "#level_price_impact_percent_" + i ).prop("checked", priceImpactPrecent);
  630. }
  631. });
  632. });
  633. </script>
  634. <?php
  635. }
  636. print dol_get_fiche_end();
  637. ?>
  638. <div style="text-align: center">
  639. <input type="submit" name="create" <?php if (!is_array($productCombination2ValuePairs1)) {
  640. print ' disabled="disabled"';
  641. } ?> value="<?php echo $action == 'add' ? $langs->trans('Create') : $langs->trans("Save") ?>" class="button button-save">
  642. &nbsp;
  643. <input type="submit" name="cancel" value="<?php echo $langs->trans("Cancel"); ?>" class="button button-cancel">
  644. </div>
  645. <?php
  646. print '</form>';
  647. } else {
  648. if ($action === 'delete') {
  649. if ($prodcomb->fetch($valueid) > 0) {
  650. $prodstatic->fetch($prodcomb->fk_product_child);
  651. print $form->formconfirm(
  652. "combinations.php?id=".urlencode($id)."&valueid=".urlencode($valueid),
  653. $langs->trans('Delete'),
  654. $langs->trans('ProductCombinationDeleteDialog', $prodstatic->ref),
  655. "confirm_deletecombination",
  656. array(array('label'=> $langs->trans('DeleteLinkedProduct'), 'type'=> 'checkbox', 'name' => 'delete_product', 'value' => false)),
  657. 0,
  658. 1
  659. );
  660. }
  661. } elseif ($action === 'copy') {
  662. 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);
  663. }
  664. $comb2val = new ProductCombination2ValuePair($db);
  665. if ($productCombinations) {
  666. ?>
  667. <script type="text/javascript">
  668. jQuery(document).ready(function() {
  669. jQuery('input[name="select_all"]').click(function() {
  670. if (jQuery(this).prop('checked')) {
  671. var checked = true;
  672. } else {
  673. var checked = false;
  674. }
  675. jQuery('table.liste input[type="checkbox"]').prop('checked', checked);
  676. });
  677. jQuery('input[name^="select["]').click(function() {
  678. jQuery('input[name="select_all"]').prop('checked', false);
  679. });
  680. });
  681. </script>
  682. <?php
  683. }
  684. // Buttons
  685. print '<div class="tabsAction">';
  686. print ' <div class="inline-block divButAction">';
  687. print '<a href="combinations.php?id='.$object->id.'&action=add&token='.newToken().'" class="butAction">'.$langs->trans('NewProductCombination').'</a>'; // NewVariant
  688. if ($productCombinations) {
  689. print '<a href="combinations.php?id='.$object->id.'&action=copy&token='.newToken().'" class="butAction">'.$langs->trans('PropagateVariant').'</a>';
  690. }
  691. print ' </div>';
  692. print '</div>';
  693. $arrayofselected = is_array($toselect) ? $toselect : array();
  694. // List of variants
  695. print '<form method="POST" action="'.$_SERVER["PHP_SELF"] .'?id='.$object->id.'">';
  696. print '<input type="hidden" name="token" value="'.newToken().'">';
  697. print '<input type="hidden" name="action" value="massaction">';
  698. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  699. // List of mass actions available
  700. /*
  701. $arrayofmassactions = array(
  702. 'presend'=>$langs->trans("SendByMail"),
  703. 'builddoc'=>$langs->trans("PDFMerge"),
  704. );
  705. if ($user->rights->product->supprimer) $arrayofmassactions['predelete']='<span class="fa fa-trash paddingrightonly"></span>'.$langs->trans("Delete");
  706. if (in_array($massaction, array('presend','predelete'))) $arrayofmassactions=array();
  707. $massactionbutton=$form->selectMassAction('', $arrayofmassactions);
  708. */
  709. $aaa = '';
  710. if (count($productCombinations)) {
  711. $aaa = '<label for="massaction">'.$langs->trans('BulkActions').'</label>';
  712. $aaa .= '<select id="bulk_action" name="massaction" class="flat">';
  713. $aaa .= ' <option value="nothing">&nbsp;</option>';
  714. $aaa .= ' <option value="not_buy">'.$langs->trans('ProductStatusNotOnBuy').'</option>';
  715. $aaa .= ' <option value="not_sell">'.$langs->trans('ProductStatusNotOnSell').'</option>';
  716. $aaa .= ' <option value="on_buy">'.$langs->trans('ProductStatusOnBuy').'</option>';
  717. $aaa .= ' <option value="on_sell">'.$langs->trans('ProductStatusOnSell').'</option>';
  718. $aaa .= ' <option value="delete">'.$langs->trans('Delete').'</option>';
  719. $aaa .= '</select>';
  720. $aaa .= '<input type="submit" value="'.dol_escape_htmltag($langs->trans("Apply")).'" class="button small">';
  721. }
  722. $massactionbutton = $aaa;
  723. $title = $langs->trans("ProductCombinations");
  724. print_barre_liste($title, 0, $_SERVER["PHP_SELF"], '', $sortfield, $sortorder, $aaa, 0);
  725. print '<div class="div-table-responsive">';
  726. ?>
  727. <table class="liste">
  728. <tr class="liste_titre">
  729. <td class="liste_titre"><?php echo $langs->trans('Product') ?></td>
  730. <td class="liste_titre"><?php echo $langs->trans('Combination') ?></td>
  731. <td class="liste_titre right"><?php echo $langs->trans('PriceImpact') ?></td>
  732. <?php if ($object->isProduct()) {
  733. print'<td class="liste_titre right">'.$langs->trans('WeightImpact').'</td>';
  734. } ?>
  735. <td class="liste_titre center"><?php echo $langs->trans('OnSell') ?></td>
  736. <td class="liste_titre center"><?php echo $langs->trans('OnBuy') ?></td>
  737. <td class="liste_titre"></td>
  738. <?php
  739. print '<td class="liste_titre center">';
  740. $searchpicto = $form->showCheckAddButtons('checkforselect', 1);
  741. print $searchpicto;
  742. print '</td>';
  743. ?>
  744. </tr>
  745. <?php
  746. if (count($productCombinations)) {
  747. foreach ($productCombinations as $currcomb) {
  748. $prodstatic->fetch($currcomb->fk_product_child);
  749. print '<tr class="oddeven">';
  750. print '<td>'.$prodstatic->getNomUrl(1).'</td>';
  751. print '<td>';
  752. $productCombination2ValuePairs = $comb2val->fetchByFkCombination($currcomb->id);
  753. $iMax = count($productCombination2ValuePairs);
  754. for ($i = 0; $i < $iMax; $i++) {
  755. echo dol_htmlentities($productCombination2ValuePairs[$i]);
  756. if ($i !== ($iMax - 1)) {
  757. echo ', ';
  758. }
  759. }
  760. print '</td>';
  761. print '<td class="right">'.($currcomb->variation_price >= 0 ? '+' : '').price($currcomb->variation_price).($currcomb->variation_price_percentage ? ' %' : '').'</td>';
  762. if ($object->isProduct()) {
  763. print '<td class="right">'.($currcomb->variation_weight >= 0 ? '+' : '').price($currcomb->variation_weight).' '.measuringUnitString(0, 'weight', $prodstatic->weight_units).'</td>';
  764. }
  765. print '<td class="center">'.$prodstatic->getLibStatut(2, 0).'</td>';
  766. print '<td class="center">'.$prodstatic->getLibStatut(2, 1).'</td>';
  767. print '<td class="right">';
  768. print '<a class="paddingleft paddingright editfielda" href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&action=edit&token='.newToken().'&valueid='.$currcomb->id.'">'.img_edit().'</a>';
  769. print '<a class="paddingleft paddingright" href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&action=delete&token='.newToken().'&valueid='.$currcomb->id.'">'.img_delete().'</a>';
  770. print '</td>';
  771. print '<td class="nowrap center">';
  772. if ($productCombinations || $massactionbutton || $massaction) { // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined
  773. $selected = 0;
  774. if (in_array($prodstatic->id, $arrayofselected)) {
  775. $selected = 1;
  776. }
  777. print '<input id="cb'.$prodstatic->id.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$prodstatic->id.'"'.($selected ? ' checked="checked"' : '').'>';
  778. }
  779. print '</td>';
  780. print '</tr>';
  781. }
  782. } else {
  783. print '<tr><td colspan="8"><span class="opacitymedium">'.$langs->trans("None").'</span></td></tr>';
  784. }
  785. print '</table>';
  786. print '</div>';
  787. print '</form>';
  788. }
  789. } else {
  790. llxHeader();
  791. // not found
  792. }
  793. // End of page
  794. llxFooter();
  795. $db->close();