From 886fcda18e28c2a451902e8a073d0af725494459 Mon Sep 17 00:00:00 2001 From: mDeflotte Date: Tue, 18 Oct 2011 15:43:28 +0000 Subject: [PATCH] // Decrement stock available on order git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9430 b9a71923-0436-4b27-9f14-aed3839534dd --- classes/PaymentModule.php | 12 ++++++------ classes/Product.php | 17 ++++------------- classes/stock/StockAvailable.php | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index 56d4c191e..95eeb9707 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -192,8 +192,8 @@ abstract class PaymentModuleCore extends Module $productQuantity = (int)(Product::getQuantity((int)($product['id_product']), ($product['id_product_attribute'] ? (int)($product['id_product_attribute']) : NULL))); $quantityInStock = ($productQuantity - (int)($product['cart_quantity']) < 0) ? $productQuantity : (int)($product['cart_quantity']); if ($id_order_state != Configuration::get('PS_OS_CANCELED') AND $id_order_state != Configuration::get('PS_OS_ERROR')) - { - if (Product::updateQuantity($product, (int)$order->id)) + { + if (StockAvailable::updateQuantity($product['id_product'], $product['id_product_attribute'], -(int)$product['cart_quantity'])) $product['stock_quantity'] -= $product['cart_quantity']; if ($product['stock_quantity'] < 0 && Configuration::get('PS_STOCK_MANAGEMENT')) $outOfStock = true; @@ -223,11 +223,11 @@ abstract class PaymentModuleCore extends Module $tax_calculator = $tax_manager->getTaxCalculator(); } - $ecotaxTaxRate = 0; - if (!empty($product['ecotax'])) - $ecotaxTaxRate = Tax::getProductEcotaxRate($order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}); + $ecotaxTaxRate = 0; + if (!empty($product['ecotax'])) + $ecotaxTaxRate = Tax::getProductEcotaxRate($order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}); - $product_price = (float)Product::getPriceStatic((int)($product['id_product']), false, ($product['id_product_attribute'] ? (int)($product['id_product_attribute']) : NULL), (Product::getTaxCalculationMethod((int)($order->id_customer)) == PS_TAX_EXC ? 2 : 6), NULL, false, false, $product['cart_quantity'], false, (int)($order->id_customer), (int)($order->id_cart), (int)($order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}), $specificPrice, false, false); + $product_price = (float)Product::getPriceStatic((int)($product['id_product']), false, ($product['id_product_attribute'] ? (int)($product['id_product_attribute']) : NULL), (Product::getTaxCalculationMethod((int)($order->id_customer)) == PS_TAX_EXC ? 2 : 6), NULL, false, false, $product['cart_quantity'], false, (int)($order->id_customer), (int)($order->id_cart), (int)($order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}), $specificPrice, false, false); $quantityDiscount = SpecificPrice::getQuantityDiscount((int)$product['id_product'], $this->context->shop->getID(), (int)$cart->id_currency, (int)$vat_address->id_country, (int)$customer->id_default_group, (int)$product['cart_quantity']); $unitPrice = Product::getPriceStatic((int)$product['id_product'], true, ($product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL), 2, NULL, false, true, 1, false, (int)$order->id_customer, NULL, (int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}); $quantityDiscountValue = $quantityDiscount ? ((Product::getTaxCalculationMethod((int)$order->id_customer) == PS_TAX_EXC ? Tools::ps_round($unitPrice, 2) : $unitPrice) - $tax_calculator->addTaxes($quantityDiscount['price'])) : 0.00; diff --git a/classes/Product.php b/classes/Product.php index fb7f3c302..071b8ec63 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -906,7 +906,7 @@ class ProductCore extends ObjectModel * @param boolean $default Is default attribute for product * @return mixed $id_product_attribute or false */ - public function addProductAttribute($price, $weight, $unit_impact, $ecotax, $quantity, $id_images, $reference, $supplier_reference, $ean13, $default, $location = null, $upc = null) + public function addProductAttribute($price, $weight, $unit_impact, $ecotax, $id_images, $reference, $supplier_reference, $ean13, $default, $location = null, $upc = null) { if (!$this->id) return; @@ -931,13 +931,10 @@ class ProductCore extends ObjectModel $id_product_attribute = Db::getInstance()->Insert_ID(); - $this->setStock($quantity, $id_product_attribute); Product::updateDefaultAttribute($this->id); if (!$id_product_attribute) return false; - $this->addStockMvt((int)$quantity, 1, $id_product_attribute); - if (empty($id_images)) return (int)($id_product_attribute); $query = 'INSERT INTO `'._DB_PREFIX_.'product_attribute_image` (`id_product_attribute`, `id_image`) VALUES '; @@ -949,9 +946,9 @@ class ProductCore extends ObjectModel return (int)($id_product_attribute); } - public function addCombinationEntity($wholesale_price, $price, $weight, $unit_impact, $ecotax, $quantity, $id_images, $reference, $supplier_reference, $ean13, $default, $location = null, $upc = null) + public function addCombinationEntity($wholesale_price, $price, $weight, $unit_impact, $ecotax, $id_images, $reference, $supplier_reference, $ean13, $default, $location = null, $upc = null) { - $id_product_attribute = $this->addProductAttribute($price, $weight, $unit_impact, $ecotax, $quantity, $id_images, $reference, $supplier_reference, $ean13, $default, $location, $upc); + $id_product_attribute = $this->addProductAttribute($price, $weight, $unit_impact, $ecotax, $id_images, $reference, $supplier_reference, $ean13, $default, $location, $upc); $result = Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'product_attribute` SET `wholesale_price` = '.(float)$wholesale_price.' WHERE `id_product_attribute` = '.(int)$id_product_attribute); if (!$id_product_attribute OR !$result) return false; @@ -1037,7 +1034,7 @@ class ProductCore extends ObjectModel * @param string $minimal_quantity Minimal quantity * @return array Update result */ - public function updateProductAttribute($id_product_attribute, $wholesale_price, $price, $weight, $unit, $ecotax, $quantity, $id_images, $reference, $supplier_reference, $ean13, $default, $location = null, $upc = null, $minimal_quantity, $available_date) + public function updateProductAttribute($id_product_attribute, $wholesale_price, $price, $weight, $unit, $ecotax, $id_images, $reference, $supplier_reference, $ean13, $default, $location = null, $upc = null, $minimal_quantity, $available_date) { Db::getInstance()->execute(' DELETE FROM `'._DB_PREFIX_.'product_attribute_combination` @@ -1061,12 +1058,6 @@ class ProductCore extends ObjectModel 'available_date' => pSQL($available_date) ); - if ($quantity) - { - $data['quantity'] = (int)$quantity; - $this->setStock($data['quantity'], $id_product_attribute); - } - if (!Db::getInstance()->AutoExecute(_DB_PREFIX_.'product_attribute', $data, 'UPDATE', '`id_product_attribute` = '.(int)($id_product_attribute)) OR !Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'product_attribute_image` WHERE `id_product_attribute` = '.(int)($id_product_attribute))) return false; if ($quantity) diff --git a/classes/stock/StockAvailable.php b/classes/stock/StockAvailable.php index 1b8161e0a..771255a70 100644 --- a/classes/stock/StockAvailable.php +++ b/classes/stock/StockAvailable.php @@ -263,4 +263,19 @@ class StockAvailableCore extends ObjectModel WHERE id_stock_available = '.(int)$id_stock_available); } } + + /** + * For a given id_product and id_product_attribute update quantity the quantity available + */ + public static function updateQuantity($id_product, $id_product_attribute, $delta_quantity, $id_shop = null) + { + $id_stock = self::getIdStockAvailable($id_product, $id_product_attribute, $id_shop); + + if (!$id_stock) + return false; + + $stock_available = new StockAvailable($id_stock); + $stock_available->quantity = $stock_available->quantity + $delta_quantity; + $stock_available->save(); + } } \ No newline at end of file