*
* 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.'/attributes/class/ProductAttribute.class.php';
require_once DOL_DOCUMENT_ROOT.'/attributes/class/ProductAttributeValue.class.php';
require_once DOL_DOCUMENT_ROOT.'/attributes/class/ProductCombination.class.php';
require_once DOL_DOCUMENT_ROOT.'/attributes/class/ProductCombination2ValuePair.class.php';
$langs->load("products");
$langs->load("other");
$var = false;
$id = GETPOST('id', 'int');
$valueid = GETPOST('valueid', 'int');
$ref = GETPOST('ref');
$weight_impact = (float) GETPOST('weight_impact');
$price_impact = (float) GETPOST('price_impact');
$price_impact_percent = (bool) GETPOST('price_impact_percent');
$form = new Form($db);
$action = GETPOST('action');
// 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);
$product = new Product($db);
$product->fetch($id);
if (!$product->isProduct()) {
header('Location: '.dol_buildpath('/product/card.php?id='.$product->id, 2));
die;
}
$prodcomb = new ProductCombination($db);
$prodcomb2val = new ProductCombination2ValuePair($db);
$productCombination2ValuePairs1 = array();
if ($_POST) {
if ($action == 'add') {
$features = GETPOST('features', 'array');
if (!$features) {
setEventMessage($langs->trans('ErrorFieldsRequired'), 'errors');
} else {
$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();
if (!$prodcomb->fetchByProductCombination2ValuePairs($id, $sanit_features)) {
if (ProductCombination::createProductCombination($product, $sanit_features, array(), $price_impact_percent, $price_impact, $weight_impact)) {
$db->commit();
setEventMessage($langs->trans('RecordSaved'));
header('Location: '.dol_buildpath('/attributes/combinations.php?id='.$id, 2));
die;
} else {
setEventMessage($langs->trans('CoreErrorMessage'), 'errors');
}
} else {
setEventMessage($langs->trans('ErrorRecordAlreadyExists'), 'errors');
}
$db->rollback();
}
} elseif ($action == 'bulk_actions') {
$prodarray = array_keys(GETPOST('select', 'array'));
$bulkaction = GETPOST('bulk_action');
$error = 0;
$prodstatic = new Product($db);
$db->begin();
foreach ($prodarray as $prodid) {
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($prodstatic->id);
} else {
break;
}
if ($res <= 0) {
$error++;
break;
}
}
if ($error) {
$db->rollback();
if ($prodstatic->error) {
setEventMessage($langs->trans($prodstatic->error), 'errors');
} else {
setEventMessage($langs->trans('CoreErrorMessage'), 'errors');
}
} else {
$db->commit();
setEventMessage($langs->trans('RecordSaved'));
}
} else {
if ($prodcomb->fetch($valueid) < 0) {
dol_print_error($db, $langs->trans('ErrorRecordNotFound'));
die;
}
$prodcomb->variation_price_percentage = $price_impact_percent;
$prodcomb->variation_price = $price_impact;
$prodcomb->variation_weight = $weight_impact;
if ($prodcomb->update() > 0) {
setEventMessage($langs->trans('RecordSaved'));
header('Location: '.dol_buildpath('/attributes/combinations.php?id='.$id, 2));
die;
} else {
setEventMessage($langs->trans('CoreErrorMessage'), 'errors');
}
}
}
$productCombinations = $prodcomb->fetchAllByFkProductParent($id);
if ($action === 'confirm_deletecombination') {
if ($prodcomb->fetch($valueid) > 0) {
$db->begin();
if ($prodcomb->delete() > 0 && $prodstatic->fetch($prodcomb->fk_product_child) > 0 && $prodstatic->delete() > 0) {
$db->commit();
setEventMessage($langs->trans('RecordSaved'));
header('Location: '.dol_buildpath('/attributes/combinations.php?id='.$product->id, 2));
die;
}
$db->rollback();
setEventMessage($langs->trans('ProductCombinationAlreadyUsed'), 'errors');
$action = '';
}
} elseif ($action === 'edit') {
if ($prodcomb->fetch($valueid) < 0) {
dol_print_error($db, $langs->trans('ErrorRecordNotFound'));
die;
}
$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 != $product->ref) {
if ($prodcomb->copyAll($product->id, $prodstatic) > 0) {
header('Location: '.dol_buildpath('/attributes/combinations.php?id='.$prodstatic->id, 2));
die;
} else {
setEventMessage($langs->trans('ErrorCopyProductCombinations'), 'errors');
}
}
} else {
setEventMessage($langs->trans('ErrorDestinationProductNotFound'), 'errors');
}
}
/*
* View
*/
if (! empty($id) || ! empty($ref)) {
$object = new Product($db);
$result = $object->fetch($id, $ref);
llxHeader("", "", $langs->trans("CardProduct".$object->type));
if ($result) {
$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, 0, $picto);
print '
';
// Reference
print '';
print '| '.$langs->trans("Ref").' | ';
print $form->showrefnav($object, 'id', '', 0);
print ' | ';
print '
';
// Label
print '| '.$langs->trans("Label").' | '.$object->label.' |
';
// Status (to sell)
print '| '.$langs->trans("Status").' ('.$langs->trans("Sell").') | ';
print $object->getLibStatut(2, 0);
print ' |
';
// Status (to buy)
print '| '.$langs->trans("Status").' ('.$langs->trans("Buy").') | ';
print $object->getLibStatut(2, 1);
print ' |
';
print '
';
dol_fiche_end();
}
if ($action == 'add' || ($action == 'edit')) {
if ($action == 'add') {
$title = $langs->trans('NewProductCombination');
} else {
$title = $langs->trans('EditProductCombination');
}
print_fiche_titre($title);
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;
}
?>
fetch($valueid) > 0) {
$form = new Form($db);
$prodstatic->fetch($prodcomb->fk_product_child);
print $form->formconfirm(
"combinations.php?id=".$id."&valueid=".$valueid,
$langs->trans('Delete'),
$langs->trans('ProductCombinationDeleteDialog', $prodstatic->getNomUrl(1)),
"confirm_deletecombination",
'',
0,
1
);
}
} elseif ($action === 'copy') {
$form = new Form($db);
print $form->formconfirm(
'combinations.php?id='.$id,
$langs->trans('CloneCombinationsProduct'),
$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 '';
}
}
llxFooter();