* Copyright (C) 2017 Laurent Destailleur * Copyright (C) 2018-2019 Frédéric France * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ require '../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttribute.class.php'; require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttributeValue.class.php'; require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php'; require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php'; $langs->loadLangs(array("products", "other")); $id = GETPOST('id', 'int'); $valueid = GETPOST('valueid', 'int'); $ref = GETPOST('ref', 'alpha'); $weight_impact = GETPOST('weight_impact', 'alpha'); $price_impact = GETPOST('price_impact', 'alpha'); $price_impact_percent = (bool) GETPOST('price_impact_percent'); $reference = GETPOST('reference', 'alpha'); $form = new Form($db); $action = GETPOST('action', 'alpha'); $massaction = GETPOST('massaction', 'alpha'); $show_files = GETPOST('show_files', 'int'); $confirm = GETPOST('confirm', 'alpha'); $toselect = GETPOST('toselect', 'array'); $cancel = GETPOST('cancel', 'alpha'); $delete_product = GETPOST('delete_product', 'alpha'); // Security check $fieldvalue = (!empty($id) ? $id : $ref); $fieldtype = (!empty($ref) ? 'ref' : 'rowid'); $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype); $prodstatic = new Product($db); $prodattr = new ProductAttribute($db); $prodattr_val = new ProductAttributeValue($db); $object = new Product($db); if ($id > 0 || $ref) { $object->fetch($id, $ref); } $selectedvariant = $_SESSION['addvariant_'.$object->id]; /* * Actions */ if ($cancel) { $action = ''; $massactions = ''; unset($_SESSION['addvariant_'.$object->id]); } if (!$object->isProduct() && !$object->isService()) { header('Location: '.dol_buildpath('/product/card.php?id='.$object->id, 2)); exit(); } if ($action == 'add') { unset($selectedvariant); unset($_SESSION['addvariant_'.$object->id]); } if ($action == 'create' && GETPOST('selectvariant', 'alpha')) // We click on select combination { $action = 'add'; if (GETPOST('attribute') != '-1' && GETPOST('value') != '-1') { $selectedvariant[GETPOST('attribute').':'.GETPOST('value')] = GETPOST('attribute').':'.GETPOST('value'); $_SESSION['addvariant_'.$object->id] = $selectedvariant; } } $prodcomb = new ProductCombination($db); $prodcomb2val = new ProductCombination2ValuePair($db); $productCombination2ValuePairs1 = array(); if ($_POST) { if (($action == 'add' || $action == 'create') && empty($massaction) && !GETPOST('selectvariant', 'alpha')) // We click on Create all defined combinations { //$features = GETPOST('features', 'array'); $features = $_SESSION['addvariant_'.$object->id]; if (!$features) { setEventMessages($langs->trans('ErrorFieldsRequired'), null, 'errors'); } else { $reference = trim($reference); if (empty($reference)) { $reference = false; } $weight_impact = price2num($weight_impact); $price_impact = price2num($price_impact); $sanit_features = array(); //First, sanitize foreach ($features as $feature) { $explode = explode(':', $feature); if ($prodattr->fetch($explode[0]) < 0) { continue; } if ($prodattr_val->fetch($explode[1]) < 0) { continue; } // Valuepair $sanit_features[$explode[0]] = $explode[1]; $tmp = new ProductCombination2ValuePair($db); $tmp->fk_prod_attr = $explode[0]; $tmp->fk_prod_attr_val = $explode[1]; $productCombination2ValuePairs1[] = $tmp; } $db->begin(); // sanit_feature is an array with 1 (and only 1) value per attribute. // For example: Color->blue, Size->Small, Option->2 //var_dump($sanit_features); //var_dump($productCombination2ValuePairs1); exit; if (!$prodcomb->fetchByProductCombination2ValuePairs($id, $sanit_features)) { $result = $prodcomb->createProductCombination($user, $object, $sanit_features, array(), $price_impact_percent, $price_impact, $weight_impact, $reference); if ($result > 0) { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); unset($_SESSION['addvariant_'.$object->id]); $db->commit(); header('Location: '.dol_buildpath('/variants/combinations.php?id='.$id, 2)); exit(); } else { $langs->load("errors"); setEventMessages('', $prodcomb->errors, 'errors'); } } else { setEventMessages($langs->trans('ErrorRecordAlreadyExists'), null, 'errors'); } $db->rollback(); } } elseif (!empty($massaction)) { $bulkaction = $massaction; $error = 0; $db->begin(); foreach ($toselect as $prodid) { // need create new of Product to prevent rename dir behavior $prodstatic = new Product($db); if ($prodstatic->fetch($prodid) < 0) { continue; } if ($bulkaction == 'on_sell') { $prodstatic->status = 1; $res = $prodstatic->update($prodstatic->id, $user); } elseif ($bulkaction == 'on_buy') { $prodstatic->status_buy = 1; $res = $prodstatic->update($prodstatic->id, $user); } elseif ($bulkaction == 'not_sell') { $prodstatic->status = 0; $res = $prodstatic->update($prodstatic->id, $user); } elseif ($bulkaction == 'not_buy') { $prodstatic->status_buy = 0; $res = $prodstatic->update($prodstatic->id, $user); } elseif ($bulkaction == 'delete') { $res = $prodstatic->delete($user, $prodstatic->id); } else { break; } if ($res <= 0) { $error++; break; } } if ($error) { $db->rollback(); if ($prodstatic->error) { setEventMessages($prodstatic->error, $prodstatic->errors, 'errors'); } else { setEventMessages($langs->trans('CoreErrorMessage'), null, 'errors'); } } else { $db->commit(); setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); } } elseif ($valueid > 0) { if ($prodcomb->fetch($valueid) < 0) { dol_print_error($db, $langs->trans('ErrorRecordNotFound')); exit(); } $prodcomb->variation_price_percentage = $price_impact_percent; $prodcomb->variation_price = $price_impact; $prodcomb->variation_weight = $weight_impact; if ($prodcomb->update($user) > 0) { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); header('Location: '.dol_buildpath('/variants/combinations.php?id='.$id, 2)); exit(); } else { setEventMessages($prodcomb->error, $prodcomb->errors, 'errors'); } } } // Reload variants $productCombinations = $prodcomb->fetchAllByFkProductParent($object->id); if ($action === 'confirm_deletecombination') { if ($prodcomb->fetch($valueid) > 0) { $db->begin(); if ($prodcomb->delete($user) > 0 && (empty($delete_product) || ($delete_product == 'on' && $prodstatic->fetch($prodcomb->fk_product_child) > 0 && $prodstatic->delete($user) > 0))) { $db->commit(); setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); header('Location: '.dol_buildpath('/variants/combinations.php?id='.$object->id, 2)); exit(); } $db->rollback(); setEventMessages($langs->trans('ProductCombinationAlreadyUsed'), null, 'errors'); $action = ''; } } elseif ($action === 'edit') { if ($prodcomb->fetch($valueid) < 0) { dol_print_error($db, $langs->trans('ErrorRecordNotFound')); exit(); } $weight_impact = $prodcomb->variation_weight; $price_impact = $prodcomb->variation_price; $price_impact_percent = $prodcomb->variation_price_percentage; $productCombination2ValuePairs1 = $prodcomb2val->fetchByFkCombination($valueid); } elseif ($action === 'confirm_copycombination') { //Check destination product $dest_product = GETPOST('dest_product'); if ($prodstatic->fetch('', $dest_product) > 0) { //To prevent from copying to the same product if ($prodstatic->ref != $object->ref) { if ($prodcomb->copyAll($user, $object->id, $prodstatic) > 0) { header('Location: '.dol_buildpath('/variants/combinations.php?id='.$prodstatic->id, 2)); exit(); } else { setEventMessages($langs->trans('ErrorCopyProductCombinations'), null, 'errors'); } } } else { setEventMessages($langs->trans('ErrorDestinationProductNotFound'), null, 'errors'); } } /* * View */ $form = new Form($db); if (!empty($id) || !empty($ref)) { llxHeader("", "", $langs->trans("CardProduct".$object->type)); $showbarcode = empty($conf->barcode->enabled) ? 0 : 1; if (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->barcode->lire_advance)) $showbarcode = 0; $head = product_prepare_head($object); $titre = $langs->trans("CardProduct".$object->type); $picto = ($object->type == Product::TYPE_SERVICE ? 'service' : 'product'); dol_fiche_head($head, 'combinations', $titre, -1, $picto); $linkback = ''.$langs->trans("BackToList").''; $object->next_prev_filter = " fk_product_type = ".$object->type; dol_banner_tab($object, 'ref', $linkback, ($user->socid ? 0 : 1), 'ref', '', '', '', 0, '', '', 1); print '
'; print '
'; print ''; // TVA print ''; // Price print ''; // Price minimum print ''; // Weight print '\n"; print "
'.$langs->trans("DefaultTaxRate").''; $positiverates = ''; if (price2num($object->tva_tx)) $positiverates .= ($positiverates ? '/' : '').price2num($object->tva_tx); if (price2num($object->localtax1_type)) $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax1_tx); if (price2num($object->localtax2_type)) $positiverates .= ($positiverates ? '/' : '').price2num($object->localtax2_tx); if (empty($positiverates)) $positiverates = '0'; echo vatrate($positiverates.($object->default_vat_code ? ' ('.$object->default_vat_code.')' : ''), '%', $object->tva_npr); /* if ($object->default_vat_code) { print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')'; } else print vatrate($object->tva_tx, true, $object->tva_npr, true);*/ print '
'.$langs->trans("SellingPrice").''; if ($object->price_base_type == 'TTC') { print price($object->price_ttc).' '.$langs->trans($object->price_base_type); } else { print price($object->price).' '.$langs->trans($object->price_base_type); } print '
'.$langs->trans("MinPrice").''; if ($object->price_base_type == 'TTC') { print price($object->price_min_ttc).' '.$langs->trans($object->price_base_type); } else { print price($object->price_min).' '.$langs->trans($object->price_base_type); } print '
'.$langs->trans("Weight").''; if ($object->weight != '') { print $object->weight." ".measuringUnitString(0, "weight", $object->weight_units); } else { print ' '; } print "
\n"; print '
'; print '
'; dol_fiche_end(); $listofvariantselected = ''; // Create or edit a varian if ($action == 'add' || ($action == 'edit')) { if ($action == 'add') { $title = $langs->trans('NewProductCombination'); // dol_fiche_head(); $features = $_SESSION['addvariant_'.$object->id]; //First, sanitize $listofvariantselected = '
'; if (!empty($features)) { foreach ($features as $feature) { $explode = explode(':', $feature); if ($prodattr->fetch($explode[0]) < 0) { continue; } if ($prodattr_val->fetch($explode[1]) < 0) { continue; } $listofvariantselected .= ''.$prodattr->label.':'.$prodattr_val->value.' '; } } $listofvariantselected .= '
'; //dol_fiche_end(); } else { $title = $langs->trans('EditProductCombination'); } if ($action == 'add') { $prodattr_all = $prodattr->fetchAll(); if (!$selected) { $selected = $prodattr_all[key($prodattr_all)]->id; } $prodattr_alljson = array(); foreach ($prodattr_all as $each) { $prodattr_alljson[$each->id] = $each; } ?> '; print load_fiche_titre($title); print '
'."\n"; print ''; print ''."\n"; print ''."\n"; if ($valueid > 0) { print ''."\n"; } dol_fiche_head(); print ''; if ($action == 'add') { print "\n"; print ''; print ''; print ''; print '
'; if (is_array($prodattr_all)) { print ''; } $htmltext = $langs->trans("GoOnMenuToCreateVairants", $langs->transnoentities("Product"), $langs->transnoentities("VariantAttributes")); print $form->textwithpicto('', $htmltext); /*print '     id).'">'; print $langs->trans("Create"); print '';*/ ?>
trans("GoOnMenuToCreateVairants", $langs->transnoentities("Product"), $langs->transnoentities("VariantAttributes")); print $form->textwithpicto('', $htmltext); /* print '     id).'">'; print $langs->trans("Create"); print ''; */ ?>
">
'; print '
'; } if (is_array($productCombination2ValuePairs1)) { ?>
isProduct()) { print ''; print ''; print ''; print ''; } print '
$val) { $result1 = $prodattr->fetch($val->fk_prod_attr); $result2 = $prodattr_val->fetch($val->fk_prod_attr_val); if ($result1 > 0 && $result2 > 0) { print $prodattr->label.' - '.$prodattr_val->value.'
'; // TODO Add delete link } } } ?>
>
'; } dol_fiche_end(); ?>
value="trans('Create') : $langs->trans('Save') ?>" class="button">  
'; } else { if ($action === 'delete') { if ($prodcomb->fetch($valueid) > 0) { $prodstatic->fetch($prodcomb->fk_product_child); print $form->formconfirm( "combinations.php?id=".$id."&valueid=".$valueid, $langs->trans('Delete'), $langs->trans('ProductCombinationDeleteDialog', $prodstatic->ref), "confirm_deletecombination", array(array('label'=> $langs->trans('DeleteLinkedProduct'),'type'=> 'checkbox', 'name' => 'delete_product', 'value' => false)), 0, 1 ); } } elseif ($action === 'copy') { 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); } $comb2val = new ProductCombination2ValuePair($db); if ($productCombinations) { ?> '; print '
'; print ''.$langs->trans('NewProductCombination').''; // NewVariant if ($productCombinations) { print ''.$langs->trans('PropagateVariant').''; } // Too much bugged page. /* print ''.$langs->trans('ProductCombinationGenerator').''; */ print '
'; print ''; $arrayofselected = is_array($toselect) ? $toselect : array(); // List of variants print ''; print ''; print ''; print ''; print ''; // List of mass actions available /* $arrayofmassactions = array( 'presend'=>$langs->trans("SendByMail"), 'builddoc'=>$langs->trans("PDFMerge"), ); if ($user->rights->product->supprimer) $arrayofmassactions['predelete']=''.$langs->trans("Delete"); if (in_array($massaction, array('presend','predelete'))) $arrayofmassactions=array(); $massactionbutton=$form->selectMassAction('', $arrayofmassactions); */ $aaa = ''; if (count($productCombinations)) { $aaa = ''; $aaa .= ''; $aaa .= ''; } $massactionbutton = $aaa; $title = $langs->trans("ProductCombinations"); print_barre_liste($title, 0, $_SERVER["PHP_SELF"], '', $sortfield, $sortorder, $aaa, 0); print '
'; ?> isProduct()) print''; ?> '; $searchpicto = $form->showCheckAddButtons('checkforselect', 1); print $searchpicto; print ''; ?> fetch($currcomb->fk_product_child); print ''; print ''; print ''; print ''; if ($object->isProduct()) { print ''; } print ''; print ''; print ''; print ''; print ''; } } else { print ''; } print '
trans('Product') ?> trans('Combination') ?> trans('PriceImpact') ?> '.$langs->trans('WeightImpact').'trans('OnSell') ?> trans('OnBuy') ?>
'.$prodstatic->getNomUrl(1).''; $productCombination2ValuePairs = $comb2val->fetchByFkCombination($currcomb->id); $iMax = count($productCombination2ValuePairs); for ($i = 0; $i < $iMax; $i++) { echo dol_htmlentities($productCombination2ValuePairs[$i]); if ($i !== ($iMax - 1)) { echo ', '; } } print ''.($currcomb->variation_price >= 0 ? '+' : '').price($currcomb->variation_price).($currcomb->variation_price_percentage ? ' %' : '').''.($currcomb->variation_weight >= 0 ? '+' : '').price($currcomb->variation_weight).' '.measuringUnitString(0, 'weight', $prodstatic->weight_units).''.$prodstatic->getLibStatut(2, 0).''.$prodstatic->getLibStatut(2, 1).''; print ''.img_edit().''; print ''.img_delete().''; print ''; if ($productCombinations || $massactionbutton || $massaction) // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined { $selected = 0; if (in_array($prodstatic->id, $arrayofselected)) $selected = 1; print ''; } print '
'.$langs->trans("None").'
'; print '
'; print '
'; } } else { llxHeader(); // not found } // End of page llxFooter(); $db->close();