/**
  * Set the available quantity for a given item.
  *
  * @param Mage_CatalogInventory_Model_Stock_Item
  * @param int $quantity the amount to set
  * @return bool true if the quantity has changed for this product
  */
 protected function updateProductQuantity(Mage_CatalogInventory_Model_Stock_Item $stockItem, $quantity)
 {
     $oldQty = $stockItem->getQty();
     $change = $oldQty !== $quantity;
     if ($change) {
         $stockItem->setQty($quantity);
     }
     return $change;
 }
Example #2
0
 public function testAfterLoad()
 {
     $this->_inventory->expects($this->once())->method('getIsInStock')->will($this->returnValue(1));
     $this->_inventory->expects($this->once())->method('getQty')->will($this->returnValue(5));
     $object = new Varien_Object();
     $this->_model->afterLoad($object);
     $data = $object->getData();
     $this->assertEquals(1, $data[self::ATTRIBUTE_NAME]['is_in_stock']);
     $this->assertEquals(5, $data[self::ATTRIBUTE_NAME]['qty']);
 }
 /**
  * Before save prepare process
  *
  * @return Demac_MultiLocationInventory_Model_CatalogInventory_Stock_Item
  */
 protected function _beforeSave()
 {
     parent::_beforeSave();
     Mage::dispatchEvent('model_save_before', array('object' => $this));
     Mage::dispatchEvent($this->_eventPrefix . '_save_before', $this->_getEventData());
     return $this;
 }
Example #4
0
 /**
  * Retrieve stock identifier
  *
  * @todo multi stock
  * @return int
  */
 public function getStockId()
 {
     if (!empty($this->_data['stock_id'])) {
         return $this->_data['stock_id'];
     } else {
         return parent::getStockId();
     }
 }
Example #5
0
 /**
  * @param Mage_CatalogInventory_Model_Stock_Item $stockItem the stock item that needs to be constructed
  * @param $productSku a string representing the SKU for product
  * @return array
  */
 protected function _createStockItem($stockItem, $productSku)
 {
     $shippingOrigin = Mage::getStoreConfig('shipping/origin');
     $locationName = '';
     if (isset($shippingOrigin['country_id'])) {
         $locationName .= $shippingOrigin['country_id'];
     }
     $locationName .= ":";
     if (isset($shippingOrigin['postcode'])) {
         $locationName .= $shippingOrigin['postcode'];
     }
     $locationName .= ":";
     if (isset($shippingOrigin['region_id'])) {
         $locationName .= $shippingOrigin['region_id'];
     }
     $data = array('sku' => $productSku, 'quantity' => (int) $stockItem->getQty(), 'locationName' => $locationName);
     return $data;
 }
 /**
  * Load item data by product
  *
  * @param   mixed $product
  * @return  Mage_CatalogInventory_Model_Stock_Item
  */
 public function loadByProduct($product)
 {
     if ($product instanceof Mage_Catalog_Model_Product) {
         $this->_getResource()->loadByProduct($this, $product);
         $this->setOrigData();
         return $this;
     } else {
         return parent::loadByProduct($product);
     }
 }
Example #7
0
 public function testSetGetProduct()
 {
     $this->assertNull($this->_model->getProduct());
     $productOne = new Varien_Object();
     $this->_model->setData('product', $productOne);
     $this->assertSame($productOne, $this->_model->getProduct());
     $productTwo = new Varien_Object();
     $this->_model->setProduct($productTwo);
     $this->assertSame($productTwo, $this->_model->getProduct());
 }
Example #8
0
 /**
  * @param Mage_CatalogInventory_Model_Stock_Item $stockItem
  *
  * @return float|int
  */
 protected function _getStackPriceMultiplicator($stockItem)
 {
     $priceMultiplier = 1;
     if ($stockItem->getEnableQtyIncrements()) {
         $stackQuantity = ceil($stockItem->getQtyIncrements());
         if ($stackQuantity > 1) {
             $priceMultiplier = $stackQuantity;
         }
     }
     return $priceMultiplier;
 }
Example #9
0
 /**
  * Prepare stock item data for save
  *
  * @param Mage_CatalogInventory_Model_Stock_Item $item
  * @param Mage_Catalog_Model_Product $product
  * @return Mage_CatalogInventory_Model_Observer
  */
 protected function _prepareItemForSave($item, $product)
 {
     $item->addData($product->getStockData())->setProduct($product)->setProductId($product->getId())->setStockId($item->getStockId());
     if (!is_null($product->getData('stock_data/min_qty')) && is_null($product->getData('stock_data/use_config_min_qty'))) {
         $item->setData('use_config_min_qty', false);
     }
     if (!is_null($product->getData('stock_data/min_sale_qty')) && is_null($product->getData('stock_data/use_config_min_sale_qty'))) {
         $item->setData('use_config_min_sale_qty', false);
     }
     if (!is_null($product->getData('stock_data/max_sale_qty')) && is_null($product->getData('stock_data/use_config_max_sale_qty'))) {
         $item->setData('use_config_max_sale_qty', false);
     }
     if (!is_null($product->getData('stock_data/backorders')) && is_null($product->getData('stock_data/use_config_backorders'))) {
         $item->setData('use_config_backorders', false);
     }
     if (!is_null($product->getData('stock_data/notify_stock_qty')) && is_null($product->getData('stock_data/use_config_notify_stock_qty'))) {
         $item->setData('use_config_notify_stock_qty', false);
     }
     return $this;
 }
Example #10
0
 /**
  * Check qty increments
  *
  * @param int|float                              $qty
  * @param Mage_CatalogInventory_Model_Stock_Item $stockItem
  *
  * @return Varien_Object
  */
 public function checkQtyIncrements($stockItem, $qty)
 {
     $result = new Varien_Object();
     $qtyIncrements = $stockItem->getQtyIncrements();
     if ($qtyIncrements && Mage::helper('core')->getExactDivision($qty, $qtyIncrements) != 0) {
         $result->setHasError(true)->setQuoteMessage(Mage::helper('cataloginventory')->__('Some of the products cannot be ordered in the requested quantity.'))->setErrorCode('qty_increments')->setQuoteMessageIndex('qty');
         $result->setMessage(Mage::helper('cataloginventory')->__('This product is available for purchase in increments of %s only.', $qtyIncrements * 1));
     }
     return $result;
 }
Example #11
0
 /**
  * Test if stock item quantity properly saved after import
  *
  * @magentoDataFixture Mage/Catalog/_files/multiple_products.php
  */
 public function testSaveStockItemQty()
 {
     $existingProductIds = array(10, 11, 12);
     $stockItems = array();
     foreach ($existingProductIds as $productId) {
         $stockItem = new Mage_CatalogInventory_Model_Stock_Item();
         $stockItem->loadByProduct($productId);
         $stockItems[$productId] = $stockItem;
     }
     $source = new Mage_ImportExport_Model_Import_Adapter_Csv(__DIR__ . '/_files/products_to_import.csv');
     $this->_model->setParameters(array('behavior' => Mage_ImportExport_Model_Import::BEHAVIOR_REPLACE, 'entity' => 'catalog_product'))->setSource($source)->isDataValid();
     $this->_model->importData();
     /** @var $stockItmBeforeImport Mage_CatalogInventory_Model_Stock_Item */
     foreach ($stockItems as $productId => $stockItmBeforeImport) {
         /** @var $stockItemAfterImport Mage_CatalogInventory_Model_Stock_Item */
         $stockItemAfterImport = new Mage_CatalogInventory_Model_Stock_Item();
         $stockItemAfterImport->loadByProduct($productId);
         $this->assertEquals($stockItmBeforeImport->getQty(), $stockItemAfterImport->getQty());
         unset($stockItemAfterImport);
     }
     unset($stockItems, $stockItem);
 }
 /**
  * Check if the given quote item is allocatable.
  *
  * @param  Mage_CatalogInventory_Model_Stock_Item
  * @return bool
  */
 protected function isAllocatable(Mage_CatalogInventory_Model_Stock_Item $stockItem)
 {
     return (int) $stockItem->getBackorders() === Mage_CatalogInventory_Model_Stock::BACKORDERS_NO || (int) $stockItem->getQty();
 }
Example #13
0
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Magento
 * @package     Mage_Catalog
 * @subpackage  performance_tests
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
// Extract product set id
$productResource = Mage::getModel('Mage_Catalog_Model_Product');
$entityType = $productResource->getResource()->getEntityType();
$sets = Mage::getResourceModel('Mage_Eav_Model_Resource_Entity_Attribute_Set_Collection')->setEntityTypeFilter($entityType->getId())->load();
$setId = null;
foreach ($sets as $setInfo) {
    $setId = $setInfo->getId();
    break;
}
if (!$setId) {
    throw new Exception('No attributes sets for product found.');
}
// Create product
$product = new Mage_Catalog_Model_Product();
$product->setTypeId('simple')->setAttributeSetId($setId)->setWebsiteIds(array(1))->setName('Product 1')->setShortDescription('Product 1 Short Description')->setWeight(1)->setDescription('Product 1 Description')->setSku('product_1')->setPrice(10)->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)->setTaxClassId(0)->save();
$stockItem = new Mage_CatalogInventory_Model_Stock_Item();
$stockItem->setProductId($product->getId())->setTypeId($product->getTypeId())->setStockId(Mage_CatalogInventory_Model_Stock::DEFAULT_STOCK_ID)->setIsInStock(1)->setQty(10000)->setUseConfigMinQty(1)->setUseConfigBackorders(1)->setUseConfigMinSaleQty(1)->setUseConfigMaxSaleQty(1)->setUseConfigNotifyStockQty(1)->setUseConfigManageStock(1)->setUseConfigQtyIncrements(1)->setUseConfigEnableQtyInc(1)->save();
 public function insertStockMovement(Mage_CatalogInventory_Model_Stock_Item $stockItem, $message = '')
 {
     if ($stockItem->getId()) {
         Mage::getModel('bubble_stockmovements/stock_movement')->setItemId($stockItem->getId())->setUser($this->_getUsername())->setUserId($this->_getUserId())->setIsAdmin((int) Mage::getSingleton('admin/session')->isLoggedIn())->setQty($stockItem->getQty())->setIsInStock((int) $stockItem->getIsInStock())->setMessage($message)->save();
         Mage::getModel('catalog/product')->load($stockItem->getProductId())->cleanCache();
     }
 }
Example #15
0
 /**
  * Checks whether requested quantity is allowed taking into account that some amount already added to quote.
  * Returns TRUE if everything is okay
  * Returns array in below format on error:
  * [
  *  'status' => string (see Enterprise_Checkout_Helper_Data::ADD_ITEM_STATUS_FAILED_* constants),
  *  'qty_max_allowed' => int (optional, if 'status'==ADD_ITEM_STATUS_FAILED_QTY_ALLOWED)
  * ]
  *
  * @param Mage_CatalogInventory_Model_Stock_Item $stockItem
  * @param Mage_Catalog_Model_Product             $product
  * @param float                                  $requestedQty
  * @return array|true
  */
 public function getQtyStatus(Mage_CatalogInventory_Model_Stock_Item $stockItem, Mage_Catalog_Model_Product $product, $requestedQty)
 {
     $result = $stockItem->checkQuoteItemQty($requestedQty, $requestedQty);
     if ($result->getHasError()) {
         $return = array();
         switch ($result->getErrorCode()) {
             case 'qty_increments':
                 $status = Enterprise_Checkout_Helper_Data::ADD_ITEM_STATUS_FAILED_QTY_INCREMENTS;
                 $return['qty_increments'] = $stockItem->getQtyIncrements();
                 break;
             case 'qty_min':
                 $status = Enterprise_Checkout_Helper_Data::ADD_ITEM_STATUS_FAILED_QTY_ALLOWED_IN_CART;
                 $return['qty_min_allowed'] = $stockItem->getMinSaleQty();
                 break;
             case 'qty_max':
                 $status = Enterprise_Checkout_Helper_Data::ADD_ITEM_STATUS_FAILED_QTY_ALLOWED_IN_CART;
                 $return['qty_max_allowed'] = $stockItem->getMaxSaleQty();
                 break;
             default:
                 $status = Enterprise_Checkout_Helper_Data::ADD_ITEM_STATUS_FAILED_QTY_ALLOWED;
                 $return['qty_max_allowed'] = $stockItem->getStockQty();
         }
         $return['status'] = $status;
         $return['error'] = $result->getMessage();
         return $return;
     }
     return true;
 }
 /**
  * Update the stock item "is_in_stock" status
  * @param Mage_CatalogInventory_Model_Stock_Item $stockItem Stock item for the product being updated
  * @param int $qty Inventory quantity stock item is being set to
  * @return self
  */
 protected function _updateItemIsInStock(Mage_CatalogInventory_Model_Stock_Item $stockItem, $qty)
 {
     $stockItem->setIsInStock($qty > $stockItem->getMinQty() ? 1 : 0);
     return $this;
 }
Example #17
0
 protected function _beforeSave()
 {
     return parent::_beforeSave();
 }
 /**
  * Return true when the quantity of inventory stock item is greater than zero
  * otherwise return false.
  *
  * @param  Mage_CatalogInventory_Model_Stock_Item
  * @return bool
  */
 protected function isAllowedInventoryDetail(Mage_CatalogInventory_Model_Stock_Item $stock)
 {
     return $stock->getQty() > 0;
 }
 /**
  * If the inventory configuration allow order item to be backorderable simply check if the
  * Manage stock for the item is greater than zero. Otherwise, if the inventory configuration
  * do not allow order items to be backorderable, then ensure the item is not backorder and has
  * manage stock.
  *
  * @param  Mage_CatalogInventory_Model_Stock_Item
  * @return bool
  */
 protected function isManagedStock(Mage_CatalogInventory_Model_Stock_Item $stock)
 {
     return ($this->_config->isBackorderable || (int) $stock->getBackorders() === Mage_CatalogInventory_Model_Stock::BACKORDERS_NO) && $stock->getManageStock() > 0;
 }
Example #20
0
 /**
  * Set inventory data to custom attribute
  *
  * @param Varien_Object $object
  * @return Mage_Eav_Model_Entity_Attribute_Backend_Abstract
  */
 public function afterLoad($object)
 {
     $this->_inventory->loadByProduct($object);
     $object->setData($this->getAttribute()->getAttributeCode(), array('is_in_stock' => $this->_inventory->getIsInStock(), 'qty' => $this->_inventory->getQty()));
     return parent::afterLoad($object);
 }
 public function insertStockHistory(Mage_CatalogInventory_Model_Stock_Item $stockItem, $message = '')
 {
     Mage::getModel('jr_stockhistory/stock_history')->setItemId($stockItem->getId())->setUser($this->_getUsername())->setUserId($this->_getUserId())->setQty($stockItem->getQty())->setIsInStock((int) $stockItem->getIsInStock())->setMessage($message)->save();
     Mage::getModel('catalog/product')->load($stockItem->getProductId())->cleanCache();
 }
 /**
  * Change Stock Item status process
  *
  * @param Mage_CatalogInventory_Model_Stock_Item $item
  * @return Mage_CatalogInventory_Model_Stock_Status
  */
 public function changeItemStatus(Mage_CatalogInventory_Model_Stock_Item $item)
 {
     $productId = $item->getProductId();
     if (!($productType = $item->getProductTypeId())) {
         $productType = $this->getProductType($productId);
     }
     $status = (int) $item->getIsInStock();
     $qty = (int) $item->getQty();
     $this->_processChildren($productId, $productType, $qty, $status, $item->getStockId());
     $this->_processParents($productId, $item->getStockId());
     return $this;
 }
Example #23
0
 public function subtractQty($qty)
 {
     if (Mage::helper('udropship')->hasMageFeature('stock_can_subtract_qty') || $this->canSubtractQty()) {
         return parent::subtractQty($qty);
     }
     return $this;
 }
Example #24
0
 /**
  * @test
  */
 public function testCataloginventoryStockItemSaveAfter()
 {
     $this->_fpc->save('product1', 'product1_cache_id', array(sha1('product_1')));
     $this->_fpc->save('product2', 'product2_cache_id', array(sha1('product_2')));
     $item = new Mage_CatalogInventory_Model_Stock_Item();
     $item->setStockStatusChangedAuto(true);
     $item->setProductId(1);
     Mage::dispatchEvent('cataloginventory_stock_item_save_after', array('item' => $item));
     $this->assertFalse($this->_fpc->load('product1_cache_id'));
     $this->assertEquals('product2', $this->_fpc->load('product2_cache_id'));
 }
Example #25
0
 /**
  * when saving, update supply needs for product
  *
  */
 protected function _afterSave()
 {
     parent::_afterSave();
     $productId = $this->getProductId();
     //check if stock changed. If so, add a stock movement to
     if ($this->getqty() != $this->getOrigData('qty')) {
         //get product stock from stock movement to check if it is different
         $stockMovementResult = mage::getModel('Purchase/Productstock')->ComputeProductStock($productId);
         if ($this->getqty() != $stockMovementResult) {
             //add stock movement
             $diff = $this->getqty() - $stockMovementResult;
             $model = mage::getModel('Purchase/StockMovement');
             //Cree le movement
             $model->setsm_product_id($productId)->setsm_qty($diff)->setsm_coef($model->GetTypeCoef('adjustment'))->setsm_description('')->setsm_type('adjustment')->setsm_date(date('Y-m-d'))->save();
         }
     }
     //check if we have to update supply needs
     $updateSupplyNeeds = false;
     if ($this->getqty() != $this->getOrigData('qty')) {
         $updateSupplyNeeds = true;
     }
     if ($this->getmin_qty() != $this->getOrigData('min_qty')) {
         $updateSupplyNeeds = true;
     }
     if ($this->getuse_config_min_qty() != $this->getOrigData('use_config_min_qty')) {
         $updateSupplyNeeds = true;
     }
     if ($this->getmanage_stock() != $this->getOrigData('manage_stock')) {
         $updateSupplyNeeds = true;
     }
     if ($this->getuse_config_manage_stock() != $this->getOrigData('use_config_manage_stock')) {
         $updateSupplyNeeds = true;
     }
     if ($this->getnotify_stock_qty() != $this->getOrigData('notify_stock_qty')) {
         $updateSupplyNeeds = true;
     }
     if ($this->getuse_config_notify_stock_qty() != $this->getOrigData('use_config_notify_stock_qty')) {
         $updateSupplyNeeds = true;
     }
     if ($updateSupplyNeeds) {
         Mage::dispatchEvent('purchase_update_supply_needs_for_product', array('product_id' => $productId, 'from' => 'stockitem aftersave'));
     }
 }
Example #26
0
 /**
  * Prepare stock item data for save
  *
  * @param Mage_CatalogInventory_Model_Stock_Item $item
  * @param Mage_Catalog_Model_Product $product
  * @return Mage_CatalogInventory_Model_Observer
  */
 protected function _prepareItemForSave($item, $product)
 {
     $item->addData($product->getStockData())->setProduct($product)->setProductId($product->getId())->setStockId($item->getStockId());
     if (!is_null($product->getData('stock_data/min_qty')) && is_null($product->getData('stock_data/use_config_min_qty'))) {
         $item->setData('use_config_min_qty', false);
     }
     if (!is_null($product->getData('stock_data/min_sale_qty')) && is_null($product->getData('stock_data/use_config_min_sale_qty'))) {
         $item->setData('use_config_min_sale_qty', false);
     }
     if (!is_null($product->getData('stock_data/max_sale_qty')) && is_null($product->getData('stock_data/use_config_max_sale_qty'))) {
         $item->setData('use_config_max_sale_qty', false);
     }
     if (!is_null($product->getData('stock_data/backorders')) && is_null($product->getData('stock_data/use_config_backorders'))) {
         $item->setData('use_config_backorders', false);
     }
     if (!is_null($product->getData('stock_data/notify_stock_qty')) && is_null($product->getData('stock_data/use_config_notify_stock_qty'))) {
         $item->setData('use_config_notify_stock_qty', false);
     }
     $originalQty = $product->getData('stock_data/original_inventory_qty');
     if (strlen($originalQty) > 0) {
         $item->setQtyCorrection($item->getQty() - $originalQty);
     }
     if (!is_null($product->getData('stock_data/enable_qty_increments')) && is_null($product->getData('stock_data/use_config_enable_qty_inc'))) {
         $item->setData('use_config_enable_qty_inc', false);
     }
     if (!is_null($product->getData('stock_data/qty_increments')) && is_null($product->getData('stock_data/use_config_qty_increments'))) {
         $item->setData('use_config_qty_increments', false);
     }
     return $this;
 }
Example #27
0
 /**
  * Creates a new StockMovement object and commits to database.
  *
  * @param Mage_CatalogInventory_Model_Stock_Item $stockItem
  * @param string $message
  * @param null $origQty
  */
 public function insertStockMovement($stockItem, $message = '', $origQty = null)
 {
     if ($stockItem->getId()) {
         $origQty = $origQty !== null ? $origQty : $stockItem->getOriginalInventoryQty();
         // Do not create entry if the quantity hasn't changed
         if ($origQty == $stockItem->getQty()) {
             return;
         }
         Mage::getModel('bubble_stockmovements/stock_movement')->setItemId($stockItem->getId())->setUser($this->_getUsername())->setUserId($this->_getUserId())->setIsAdmin((int) Mage::getSingleton('admin/session')->isLoggedIn())->setQty($stockItem->getQty())->setOriginalQty($origQty !== null ? $origQty : $stockItem->getOriginalInventoryQty())->setIsInStock((int) $stockItem->getIsInStock())->setMessage($message)->save();
     }
 }