*/ /** * Klasa stDepository * * @author Marcin Olejniczak * * @package stDepositoryPlugin * @subpackage libs */ class stDepository { /** * Zwiększanie stanu magazynowego produktu * * @param object $product * @param integer $quantity */ public static function increase($product, $quantity) { $id = is_object($product)? $product->getId() : $product; if ($id) { $con = Propel::getConnection(); $con->executeQuery(sprintf('UPDATE %1$s SET %2$s = %2$s + %3$s WHERE %4$s = %5$s', ProductPeer::TABLE_NAME, ProductPeer::STOCK, $quantity, ProductPeer::ID, $id )); AllegroAuctionPeer::updateRequiresSync($id); $dispatcher = stEventDispatcher::getInstance(); if (!empty($dispatcher->getListeners('stDepository.increase'))) { $dispatcher->notify(new sfEvent(null, 'stDepository.increase', ['product' => $product, 'quantity' => $quantity])); } } } /** * Zmniejszanie stanu magazynowego produktu * * @param object $product * @param integer $quantity */ public static function decrease($product, $quantity) { $id = is_object($product) ? $product->getId() : $product; if ($id) { $con = Propel::getConnection(); $con->executeQuery(sprintf('UPDATE %1$s SET %2$s = GREATEST(0, %2$s - %3$s) WHERE %4$s = %5$s', ProductPeer::TABLE_NAME, ProductPeer::STOCK, $quantity, ProductPeer::ID, $id )); AllegroAuctionPeer::updateRequiresSync($id); $dispatcher = stEventDispatcher::getInstance(); if (!empty($dispatcher->getListeners('stDepository.decrease'))) { $dispatcher->notify(new sfEvent(null, 'stDepository.decrease', ['product' => $product, 'quantity' => $quantity])); } } } public static function increaseForOrderProduct(OrderProduct $item, $quantity = null) { if (null === $quantity) { $quantity = $item->getQuantity(); } if ($item->hasPriceModifiers() && $item->getProduct()->hasStockManagmentWithOptions()) { self::increaseProductOptionsForOrderProduct($item); } else { self::increase($item->getProductId(), $quantity); } foreach ($item->getOrderProductHasSets() as $set) { if ($set->getProductId() != $item->getProductId()) { self::increase($set->getProductId(), $quantity); } } } public static function decreaseForOrderProduct(OrderProduct $item, $quantity = null) { if (null === $quantity) { $quantity = $item->getQuantity(); } if ($item->hasPriceModifiers() && $item->getProduct()->hasStockManagmentWithOptions()) { self::decreaseProductOptionsForOrderProduct($item); } else { self::decrease($item->getProductId(), $quantity); } foreach ($item->getOrderProductHasSets() as $set) { if ($set->getProductId() != $item->getProductId()) { self::decrease($set->getProductId(), $quantity); } } } protected static function increaseProductOptionsForOrderProduct($item) { $ids = self::getProductOptionsIds($item); if ($ids) { stDepository::increaseProductOptions($ids, $item->getQuantity()); ProductOptionsValuePeer::updateStock($item->getProductId()); } } protected static function decreaseProductOptionsForOrderProduct($item) { $ids = self::getProductOptionsIds($item); if ($ids) { stDepository::decreaseProductOptions($ids, $item->getQuantity()); ProductOptionsValuePeer::updateStock($item->getProductId()); } } public static function set($product, $quantity) { $id = is_object($product) ? $product->getId() : $product; if ($id) { $con = Propel::getConnection(); $ps = $con->prepareStatement(sprintf('UPDATE %s SET %s = ? WHERE %s = ?', ProductPeer::TABLE_NAME, ProductPeer::STOCK, ProductPeer::ID )); $ps->set(1, $quantity); $ps->setInteger(2, $id); $ps->executeQuery(); $dispatcher = stEventDispatcher::getInstance(); if (!empty($dispatcher->getListeners('stDepository.set'))) { $dispatcher->notify(new sfEvent(null, 'stDepository.set', ['product' => $product, 'quantity' => $quantity])); } } } public static function increaseProductOptions($ids, $quantity) { $con = Propel::getConnection(); $con->executeQuery(sprintf('UPDATE %1$s SET %2$s = %2$s + %3$s WHERE %4$s IN (%5$s)', ProductOptionsValuePeer::TABLE_NAME, ProductOptionsValuePeer::STOCK, $quantity, ProductOptionsValuePeer::ID, implode(',', $ids) )); } public static function decreaseProductOptions($ids, $quantity) { $con = Propel::getConnection(); $con->executeQuery(sprintf('UPDATE %1$s SET %2$s = GREATEST(0, %2$s - %3$s) WHERE %4$s IN (%5$s)', ProductOptionsValuePeer::TABLE_NAME, ProductOptionsValuePeer::STOCK, $quantity, ProductOptionsValuePeer::ID, implode(',', $ids) )); } /** * Pobranie stanu magazynowego produktu * * @param integer $product_id * @return integer */ public static function getStock($product_id) { $product = ProductPeer::retrieveByPK($product_id); return $product->getStock(); } }