// Product Suppliers : Debug and optimizations
git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@10589 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
@@ -121,10 +121,10 @@ class ProductSupplierCore extends ObjectModel
|
||||
* For a given product, retrieves its suppliers
|
||||
*
|
||||
* @param int $id_product
|
||||
* @param int $id_lang
|
||||
* @param int $group_by_supplier
|
||||
* @return array
|
||||
*/
|
||||
public static function getSupplierCollection($id_product, $group_by_product = true)
|
||||
public static function getSupplierCollection($id_product, $group_by_supplier = true)
|
||||
{
|
||||
// build query
|
||||
$query = new DbQuery();
|
||||
@@ -132,11 +132,28 @@ class ProductSupplierCore extends ObjectModel
|
||||
$query->from('product_supplier ps');
|
||||
$query->where('ps.id_product = '.(int)$id_product);
|
||||
|
||||
if ($group_by_product)
|
||||
if ($group_by_supplier)
|
||||
$query->groupBy('ps.id_supplier');
|
||||
|
||||
$results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
|
||||
|
||||
return ObjectModel::hydrateCollection('ProductSupplier', $results);
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$res = parent::delete();
|
||||
|
||||
if ($res && $this->id_product_attribute == 0)
|
||||
{
|
||||
$items = self::getSupplierCollection($this->id_product, false);
|
||||
foreach ($items as &$item)
|
||||
{
|
||||
if ($item->id_product_attribute > 0)
|
||||
$item->delete();
|
||||
}
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1975,7 +1975,7 @@ class AdminProductsControllerCore extends AdminController
|
||||
*/
|
||||
public function postProcessFormSuppliers()
|
||||
{
|
||||
if (Validate::isLoadedObject($product = new Product((int)(Tools::getValue('id_product')))))
|
||||
if (Validate::isLoadedObject($product = new Product((int)Tools::getValue('id_product'))))
|
||||
{
|
||||
// Get all available suppliers
|
||||
$suppliers = Supplier::getSuppliers();
|
||||
@@ -1995,9 +1995,12 @@ class AdminProductsControllerCore extends AdminController
|
||||
$suppliers_to_associate[] = $supplier['id_supplier'];
|
||||
|
||||
// Delete already associated suppliers if needed
|
||||
foreach ($associated_suppliers as &$associated_supplier)
|
||||
foreach ($associated_suppliers as $key => &$associated_supplier)
|
||||
if (!in_array($associated_supplier->id_supplier, $suppliers_to_associate))
|
||||
{
|
||||
$associated_supplier->delete();
|
||||
unset($associated_suppliers[$key]);
|
||||
}
|
||||
|
||||
// Associate suppliers
|
||||
foreach ($suppliers_to_associate as $id)
|
||||
@@ -2014,6 +2017,8 @@ class AdminProductsControllerCore extends AdminController
|
||||
$product_supplier->id_product_attribute = 0;
|
||||
$product_supplier->id_supplier = $id;
|
||||
$product_supplier->save();
|
||||
|
||||
$associated_suppliers[] = $product_supplier;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2026,17 +2031,6 @@ class AdminProductsControllerCore extends AdminController
|
||||
|
||||
$this->confirmations[] = $this->l('Suppliers of the product have been updated');
|
||||
|
||||
$this->postProcessFormSupplierReferences();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Post traitment for supplier product references
|
||||
*/
|
||||
public function postProcessFormSupplierReferences()
|
||||
{
|
||||
if (Validate::isLoadedObject($product = new Product((int)(Tools::getValue('id_product')))))
|
||||
{
|
||||
// Get all id_product_attribute
|
||||
$attributes = $product->getAttributesResume($this->context->language->id);
|
||||
if (empty($attributes))
|
||||
@@ -2045,28 +2039,22 @@ class AdminProductsControllerCore extends AdminController
|
||||
'attribute_designation' => ''
|
||||
);
|
||||
|
||||
// get associated suppliers
|
||||
$associated_suppliers = ProductSupplier::getSupplierCollection($product->id);
|
||||
|
||||
// Get already associated suppliers and force to retreive product declinaisons
|
||||
$product_supplier_collection = ProductSupplier::getSupplierCollection($product->id, false);
|
||||
|
||||
// Manage references
|
||||
foreach ($attributes as $attribute)
|
||||
foreach ($associated_suppliers as $supplier)
|
||||
if (Tools::isSubmit('supplier_reference_'.$attribute['id_product'].'_'.$attribute['id_product_attribute'].'_'.$supplier->id_supplier))
|
||||
if (Tools::isSubmit('supplier_reference_'.$product->id.'_'.$attribute['id_product_attribute'].'_'.$supplier->id_supplier))
|
||||
{
|
||||
$reference = Tools::getValue('supplier_reference_'.$attribute['id_product'].'_'.$attribute['id_product_attribute'].'_'.$supplier->id_supplier, '');
|
||||
$reference = Tools::getValue('supplier_reference_'.$product->id.'_'.$attribute['id_product_attribute'].'_'.$supplier->id_supplier, '');
|
||||
|
||||
if (!empty($reference))
|
||||
{
|
||||
$existing_reference = ProductSupplier::getProductSupplierReference($attribute['id_product'], $attribute['id_product_attribute'], $supplier->id_supplier);
|
||||
$existing_id = (int)ProductSupplier::getIdByProductAndSupplier($product->id, $attribute['id_product_attribute'], $supplier->id_supplier);
|
||||
|
||||
if (empty($existing_reference))
|
||||
if ($existing_id <= 0)
|
||||
{
|
||||
//create new record
|
||||
$product_supplier_entity = new ProductSupplier();
|
||||
$product_supplier_entity->id_product = $attribute['id_product'];
|
||||
$product_supplier_entity->id_product = $product->id;
|
||||
$product_supplier_entity->id_product_attribute = $attribute['id_product_attribute'];
|
||||
$product_supplier_entity->id_supplier = $supplier->id_supplier;
|
||||
$product_supplier_entity->product_supplier_reference = pSQL($reference);
|
||||
@@ -2075,21 +2063,14 @@ class AdminProductsControllerCore extends AdminController
|
||||
else
|
||||
{
|
||||
//update existing record
|
||||
foreach ($product_supplier_collection as &$psc)
|
||||
if ($psc->id_product == $attribute['id_product']
|
||||
&& $psc->id_product_attribute == $attribute['id_product_attribute']
|
||||
&& $psc->id_supplier == $supplier->id_supplier
|
||||
)
|
||||
{
|
||||
$reference = pSQL($reference);
|
||||
$product_supplier_entity = new ProductSupplier($existing_id);
|
||||
$reference = pSQL($reference);
|
||||
|
||||
if ($reference != $psc->product_supplier_reference)
|
||||
{
|
||||
$psc->product_supplier_reference = pSQL($reference);
|
||||
$psc->update();
|
||||
}
|
||||
break;
|
||||
}
|
||||
if ($product_supplier_entity->product_supplier_reference != $reference)
|
||||
{
|
||||
$product_supplier_entity->product_supplier_reference = pSQL($reference);
|
||||
$product_supplier_entity->update();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3199,6 +3180,7 @@ class AdminProductsControllerCore extends AdminController
|
||||
$attributes = $obj->getAttributesResume($this->context->language->id);
|
||||
if (empty($attributes))
|
||||
$attributes[] = array(
|
||||
'id_product' => $this->object->id,
|
||||
'id_product_attribute' => 0,
|
||||
'attribute_designation' => ''
|
||||
);
|
||||
|
||||
@@ -541,7 +541,7 @@ class AdminSupplyOrdersControllerCore extends AdminController
|
||||
'title' => $this->l('Creation date'),
|
||||
'width' => 150,
|
||||
'align' => 'right',
|
||||
'type' => 'datetime',
|
||||
'type' => 'date',
|
||||
'havingFilter' => true,
|
||||
'filter_key' => 'a!date_add'
|
||||
),
|
||||
@@ -549,7 +549,7 @@ class AdminSupplyOrdersControllerCore extends AdminController
|
||||
'title' => $this->l('Last modification date'),
|
||||
'width' => 150,
|
||||
'align' => 'right',
|
||||
'type' => 'datetime',
|
||||
'type' => 'date',
|
||||
'havingFilter' => true,
|
||||
'filter_key' => 'a!date_upd'
|
||||
),
|
||||
@@ -557,7 +557,7 @@ class AdminSupplyOrdersControllerCore extends AdminController
|
||||
'title' => $this->l('Delivery date'),
|
||||
'width' => 150,
|
||||
'align' => 'right',
|
||||
'type' => 'datetime',
|
||||
'type' => 'date',
|
||||
'havingFilter' => true,
|
||||
'filter_key' => 'a!date_delivery_expected'
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user