*
* 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->load("products");
$langs->load('other');
$id = GETPOST('id', 'int');
$ref = GETPOST('ref');
$form = new Form($db);
// Security check
$fieldvalue = (! empty($id) ? $id : $ref);
$fieldtype = (! empty($ref) ? 'ref' : 'rowid');
$result=restrictedArea($user,'produit|service',$fieldvalue,'product&product','','',$fieldtype);
$prodattr = new ProductAttribute($db);
$prodattrval = new ProductAttributeValue($db);
$product = new Product($db);
$product->fetch($id);
if (!$product->isProduct()) {
header('Location: '.dol_buildpath('/product/card.php?id='.$product->id, 2));
exit();
}
/**
* Posible combinations. Format.
* attrval => array(
* valueid => array(
* price => ''
* weight => ''
* )
* )
*/
$combinations = GETPOST('combinations', 'array');
$price_var_percent = (bool) GETPOST('price_var_percent');
$donotremove = true;
if ($_POST) {
$donotremove = (bool) GETPOST('donotremove');
//We must check if all those given combinations actually exist
$sanitized_values = array();
foreach ($combinations as $attr => $val) {
if ($prodattr->fetch($attr) > 0) {
foreach ($val as $valueid => $content) {
if ($prodattrval->fetch($valueid) > 0) {
$sanitized_values[$attr][$valueid] = $content;
}
}
}
}
if ($sanitized_values) {
require DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
$adapted_values = array();
//Adapt the array to the cartesian function
foreach ($sanitized_values as $attr => $val) {
$adapted_values[$attr] = array_keys($val);
}
$db->begin();
$combination = new ProductCombination($db);
$delete_prev_comb_res = 1;
if (!$donotremove) {
$delete_prev_comb_res = $combination->deleteByFkProductParent($id);
}
//Current combinations will be deleted
if ($delete_prev_comb_res > 0) {
$res = 1;
foreach (cartesianArray($adapted_values) as $currcomb)
{
$res = $combination->createProductCombination($product, $currcomb, $sanitized_values, $price_var_percent);
if ($res < 0) {
$error++;
setEventMessages($combination->error, $combination->errors, 'errors');
break;
}
}
if ($res > 0) {
$db->commit();
setEventMessage($langs->trans('RecordSaved'));
header('Location: '.dol_buildpath('/variants/combinations.php?id='.$id, 2));
exit;
}
} else {
setEventMessage($langs->trans('ErrorDeletingGeneratedProducts'), 'errors');
}
$db->rollback();
} else {
setEventMessage($langs->trans('ErrorFieldsRequired'), 'errors');
}
}
/*
* View
*/
if (! empty($id) || ! empty($ref)) {
$object = new Product($db);
$result = $object->fetch($id, $ref);
llxHeader("", "", $langs->trans("CardProduct".$object->type));
if ($result > 0)
{
$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, 0, $picto);
$linkback = ''.$langs->trans("BackToList").'';
$object->next_prev_filter=" fk_product_type = ".$object->type;
dol_banner_tab($object, 'ref', $linkback, ($user->societe_id?0:1), 'ref', '', '', '', 0, '', '', 1);
dol_fiche_end();
}
print_fiche_titre($langs->trans('ProductCombinationGenerator'));
$dictionary_attr = array();
foreach ($prodattr->fetchAll() as $attr) {
$dictionary_attr[$attr->id] = $attr;
foreach ($prodattrval->fetchAllByProductAttribute($attr->id) as $attrval) {
$dictionary_attr[$attr->id]->values[$attrval->id] = $attrval;
}
}
?>