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

261 lines
9.0 KiB
PHP

<?php
/**
* Zapisywanie minimalnej ceny produtków.
*
* @author Marek Jakubowicz m@sote.pl
*/
/**
* Opercje związane z st_product_price_history_min
*/
class stPriceHistoryMin
{
var $product_min = NULL; // instancja rekordu dla podanego product_id
/**
* Konstruktor
* @param integer $product_id ID Produktu
*
* @return true
*/
public function __construct($product_id)
{
$this->product_id = $product_id;
// ustaw obiekt, żeby nie wywoływać kilku zapytań dla tej samej instancji obiektu
$c = new Criteria();
$c->add(ProductPriceHistoryMinPeer::PRODUCT_ID, $this->product_id);
$c->addDescendingOrderByColumn(ProductPriceHistoryMinPeer::CREATED_AT);
$last = ProductPriceHistoryMinPeer::doSelectOne($c);
if (! empty($last)) $this->product_min = $last;
else $this->product_min = NULL;
return true;
}
/**
* Sprawdż czy jest zapisana ostatnia cena produktu w st_prodcut_price_history_min
*
* @return bool - true jest wpis z ceną, false - nie ma
*/
public function isMinProductPrice()
{
$c = new Criteria();
$c->add(ProductPriceHistoryMinPeer::PRODUCT_ID, $this->product_id);
$c->addDescendingOrderByColumn(ProductPriceHistoryMinPeer::CREATED_AT);
$last = ProductPriceHistoryMinPeer::doSelectOne($c);
if (! empty($last))
{
return true;
} else
{
return false;
}
}
/**
* Odczytaj wartość price_change.
* @return string wartość up|down|NULL
*/
public function getPriceChange()
{
if (is_null($this->product_min)) return NULL;
$price_change = $this->product_min->getPriceChange();
if ($price_change == 'up') return 'up';
if ($price_change == 'down') return 'down';
return NULL;
}
/**
* Odczytaj datę minimalnej ceny 30 dni przed zmianą
*/
public function getDatePreviousMinPrice()
{
if (is_null($this->product_min)) return NULL;
$date_previous_min_price = $this->product_min->getDatePreviousMinPrice();
// zamien format 2022-12-01 10:49:11 -> 2022-12-01
$date = date_create($date_previous_min_price);
$date_formated = date_format($date,"Y-m-d");
return $date_formated;
}
/**
* Odczytaj datę ostatniej zmiany
*/
public function getDateLastPriceChange()
{
if (is_null($this->product_min)) return NULL;
$date_last_price_change = $this->product_min->getDateLastPriceChange();
// zamien format 2022-12-01 10:49:11 -> 2022-12-01
$date = date_create($date_last_price_change);
$date_formated = date_format($date,"Y-m-d");
return $date_formated;
}
/**
* Odczytaj ostatnią cenę produktu z st_prodcu_price_history_min
* Ostatnia cena zapisana tutaj to cena 30 dni przed zmianą.
*
* @return float ostatnia cena brutto
*/
public function getMinProductPriceBrutto()
{
$c = new Criteria();
$c->add(ProductPriceHistoryMinPeer::PRODUCT_ID, $this->product_id);
$c->addDescendingOrderByColumn(ProductPriceHistoryMinPeer::CREATED_AT);
$last = ProductPriceHistoryMinPeer::doSelectOne($c);
if (! empty($last))
{
$price_brutto = $last->getPriceBrutto();
return $price_brutto;
} else
{
return false;
}
}
/**
* Odczytaj ostatnie dane z last dla produktu
*
* @return object ProductPriceHistoryMin
*/
public function getMinProduct()
{
$c = new Criteria();
$c->add(ProductPriceHistoryMinPeer::PRODUCT_ID, $this->product_id);
$c->addDescendingOrderByColumn(ProductPriceHistoryMinPeer::CREATED_AT);
$last = ProductPriceHistoryMinPeer::doSelectOne($c);
if (! empty($last))
{
return $last;
} else return NULL;
}
/**
* Zapisz cenę produktu w st_prodcu_price_history_min. Dodanie wpisu.
*
* @param object $product stProduct - odwołanie bezpośrednie referencja
* @param string $change Infomrmacja czy cena wzrosła 'up', czyt spadla 'down'
* @return NULL
*/
public function addMinProductPrice($product,$change)
{
$price = $product->getPrice();
$price_netto = $product->getPriceNetto();
$price_brutto = $product->getPriceBrutto();
$main_price = $product->getMainPrice();
$main_price_netto = $product->getMainPriceNetto();
$main_price_brutto = $product->getMainPriceBrutto();
$price_currency = $product->getPrice();
if (empty($price_currency))
{
$price_currency = $product->getPriceBrutto();
}
// dodatkowe informacje do zapisu informacji z kiedy byla brana data i kiedy nastapila aktualizacja
$date_previous_min_price = $product->getCreatedAt();
$date_last_price_change = date("Y-m-d");
$currency_id = $product->getCurrencyId();
if (empty($currency_id)) $currency_id = $this->default_currency;
$product_min = new ProductPriceHistoryMin();
$product_min->setProductId($this->product_id);
$product_min->setPrice($price_currency);
$product_min->setPriceNetto($price_netto);
$product_min->setPriceBrutto($price_brutto);
$product_min->setMainPrice($main_price);
$product_min->setMainPriceBrutto($main_price_brutto);
$product_min->setMainPriceNetto($main_price_netto);
$product_min->setCurrencyId($currency_id);
$product_min->setPriceChange($change);
$product_min->setDatePreviousMinPrice($date_previous_min_price);
$product_min->setDateLastPriceChange($date_last_price_change);
$product_min->save();
// zaktualizuj dane w produkcie stProduct->opt_price_history_change - wartosc $change
$x = new Criteria();
$x->add(ProductPeer::ID, $this->product_id);
$product_opt = ProductPeer::doSelectOne($x);
$product_opt->setOptPriceHistoryChange($change);
$product_opt->disablePriceHistorySave(); // zabezpieczenie przed infinite loop
$product_opt->save();
return NULL;
}
/**
* Zapisz cenę produktu w st_prodcu_price_history_min. Aktuaizacja.
*
* @param object $product stProduct - odwołanie bezpośrednie referencja
* @param string $change Infomrmacja czy cena wzrosła 'up', czyt spadla 'down'
* @return NULL
*/
public function updateMinProductPrice($product,$change)
{
$price = $product->getPrice();
$price_netto = $product->getPriceNetto();
$price_brutto = $product->getPriceBrutto();
$main_price = $product->getMainPrice();
$main_price_netto = $product->getMainPriceNetto();
$main_price_brutto = $product->getMainPriceBrutto();
$price_currency = $product->getPrice();
if (empty($price_currency))
{
$price_currency = $product->getPriceBrutto();
}
// dodatkowe informacje do zapisu informacji z kiedy byla brana data i kiedy nastapila aktualizacja
$date_previous_min_price = $product->getCreatedAt();
$date_last_price_change = date("Y-m-d");
$currency_id = $product->getCurrencyId();
if (empty($currency_id)) $currency_id = $this->default_currency;
$c = new Criteria();
$c->add(ProductPriceHistoryMinPeer::PRODUCT_ID, $this->product_id);
$c->addDescendingOrderByColumn(ProductPriceHistoryMinPeer::CREATED_AT);
$product_min = ProductPriceHistoryMinPeer::doSelectOne($c);
$product_min->setProductId($this->product_id);
$product_min->setPrice($price_currency);
$product_min->setPriceNetto($price_netto);
$product_min->setPriceBrutto($price_brutto);
$product_min->setMainPrice($main_price);
$product_min->setMainPriceNetto($main_price_netto);
$product_min->setMainPriceBrutto($main_price_brutto);
$product_min->setCurrencyId($currency_id);
$product_min->setPriceChange($change);
$product_min->setDatePreviousMinPrice($date_previous_min_price);
$product_min->setDateLastPriceChange($date_last_price_change);
$product_min->save();
// zaktualizuj dane w produkcie stProduct->opt_price_history_change - wartosc $change
$x = new Criteria();
$x->add(ProductPeer::ID, $this->product_id);
$product_opt = ProductPeer::doSelectOne($x);
$product_opt->setOptPriceHistoryChange($change);
$product_opt->disablePriceHistorySave(); // zabezpieczenie przed infinite loop
$product_opt->save();
// $this->getLogger()->info('Product Min id: %id% Updated',array('%id%'=>$id));
return NULL;
}
}