Files
grzanieplus.pl/plugins/stDepositoryPlugin/lib/stDepositiory.class.php
2025-03-12 17:06:23 +01:00

230 lines
6.5 KiB
PHP

<?php
/**
* SOTESHOP/stDepositoryPlugin
*
* Ten plik należy do aplikacji stDepositoryPlugin opartej na licencji (Professional License SOTE).
* Nie zmieniaj tego pliku, jeśli chcesz korzystać z automatycznych aktualizacji oprogramowania.
* Jeśli chcesz wprowadzać swoje modyfikacje do programu, zapoznaj się z dokumentacją, jak zmieniać
* oprogramowanie bez zmiany kodu bazowego http://www.sote.pl/modifications
*
* @package stDepositoryPlugin
* @subpackage libs
* @copyright SOTE (www.sote.pl)
* @license http://www.sote.pl/license/sote (Professional License SOTE)
* @version $Id: stDepositiory.class.php 617 2009-04-09 13:02:31Z michal $
* @author Marcin Olejniczak <marcin.olejniczak@sote.pl>
*/
/**
* Klasa stDepository
*
* @author Marcin Olejniczak <marcin.olejniczak@sote.pl>
*
* @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();
}
}