270 lines
9.1 KiB
PHP
270 lines
9.1 KiB
PHP
<?php
|
|
/**
|
|
* Zadania cron do Harmonogramu zadan związane z historią cen produktów.
|
|
* @author m@sote.pl
|
|
*/
|
|
|
|
/**
|
|
* Zapisywanie historii cen dla opcji 'full' w konfiguracji produktow.
|
|
*/
|
|
class stPriceHistoryTask extends stTask
|
|
{
|
|
/**
|
|
* W tej metodzie zwracamy ile rekordów/danych zamierzamy wykonać
|
|
*/
|
|
public function count(): int
|
|
{
|
|
if (! $this->isFullMode())
|
|
{
|
|
// Jeśli jest tryb uproszczony, wykonaj tylko 1 krok i zadanie w finished() - kasowanie starych danych
|
|
return 1;
|
|
}
|
|
|
|
// return 2;
|
|
$c = new Criteria();
|
|
$c->add(ProductPeer::ACTIVE, 1);
|
|
$count = ProductPeer::doCount($c);
|
|
|
|
return $count;
|
|
}
|
|
|
|
/**
|
|
* Metoda wykonywana na początku
|
|
*/
|
|
public function started()
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
/**
|
|
* Logger - w trybie dev
|
|
* @param string $message - dane do Logger()
|
|
* @param array $data - dane do Logger()
|
|
*/
|
|
private function Logger($message,$data=NULL)
|
|
{
|
|
if (sfConfig::get('sf_debug'))
|
|
{
|
|
// zapisuj dodatkowe logi tylko w trybie dev
|
|
$this->getLogger()->info($message,$data);
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
/**
|
|
* Metoda wykonywana na końcu
|
|
*/
|
|
public function finished()
|
|
{
|
|
// wyczyść FastCache, wymagane tylko dla trybu Full
|
|
if ($this->isFullMode())
|
|
{
|
|
stFastCacheManager::clearCache();
|
|
$this->Logger('FastCache wyczyszczony');
|
|
}
|
|
|
|
// usuwanie starych wpisów z st_product_price_history
|
|
$this->deleteOldData();
|
|
|
|
return NULL;
|
|
}
|
|
|
|
/**
|
|
* W tej metodzie wykonujemy swoje operacje na danych
|
|
* @param typ opis
|
|
* @return typ opis
|
|
*/
|
|
public function execute(int $offset): int
|
|
{
|
|
// if (! $this->isFullMode())
|
|
// {
|
|
// // dla trybu uproszczonego wykonaj 1 krok
|
|
// return $offset+1;
|
|
// }
|
|
|
|
$limit = 100; // ilość odczytywanych rekordów w 1 iteracji
|
|
|
|
// $this->getLogger()->info('Przykładowy komunikat z %parameterem%', array(
|
|
// '%parameterem%' => 'Jakaś wartość'
|
|
// ));
|
|
// $this->getLogger()->info('Przykładowa informacja która pojawi się w logach');
|
|
// $this->getLogger()->error('Przykładowy błąd jaki pojawi się w logach');
|
|
// $this->getLogger()->warning('Przykładowe ostrzeżenie jakie pojawi się w logach');
|
|
|
|
// Umożliwia ustawienie parametru, którego wartość można pobrać na dowolnym etapie wykonuwania zadania
|
|
// wartość może być dowolnego typu array, object, string itd...
|
|
// $this->setParameter('nazwa_parametru', 'dowolna wartość');
|
|
// $this->getParameter('nazwa_parametru');
|
|
|
|
if ($this->isCLI())
|
|
{
|
|
// Umożliwia sprawdzenie czy zadanie jest uruchomione w trybie CLI (Command Line Interface)
|
|
}
|
|
|
|
// Odczytaj domyślną waltę w sklepie
|
|
$c = new Criteria();
|
|
$c->add(CurrencyPeer::MAIN, 1);
|
|
$currency = CurrencyPeer::doSelectOne($c);
|
|
$this->default_currency = $currency->getID();
|
|
|
|
$c = new Criteria();
|
|
$c->add(ProductPeer::ACTIVE, 1);
|
|
$c->setLimit($limit);
|
|
$c->setOffset($offset);
|
|
$products = ProductPeer::doSelect($c);
|
|
foreach ($products as $product)
|
|
{
|
|
$this->checkProduct($product);
|
|
$offset=$offset+1;
|
|
}
|
|
|
|
// $this->getLogger()->info('<< Next iteration >> ');
|
|
|
|
return $offset;
|
|
}
|
|
|
|
|
|
/**
|
|
* Usuń wpisy starsze niż 60 dni
|
|
* @return NULL
|
|
*/
|
|
private function deleteOldData()
|
|
{
|
|
$date_delete = date('Y-m-d', strtotime('-60 day', strtotime(date('Y-m-d'))));
|
|
|
|
$c = new Criteria();
|
|
$c->add(ProductPriceHistoryPeer::CREATED_AT, $date_delete, Criteria::LESS_THAN);
|
|
$delete = ProductPriceHistoryPeer::doDelete($c);
|
|
|
|
$this->getLogger()->info('Usunięcie starych wpisów powyżej 60 dni.');
|
|
|
|
return NULL;
|
|
}
|
|
|
|
/**
|
|
* Sprawdz konfigurację historii cen i typ, czy jest włączona opcja full.
|
|
* @return bool - true - jeśli opcja jest fullo, false - simple/inna
|
|
*/
|
|
private function isFullMode()
|
|
{
|
|
// // Odczytanie konfiguracji
|
|
// $product_config = stConfig::getInstance('stProduct');
|
|
// $price_history_type=$product_config->get('price_history_type');
|
|
|
|
// // $this->Logger('Typ historii cen: %price_history_type%',array('%price_history_type%'=>$price_history_type));
|
|
|
|
// if ($price_history_type=='full')
|
|
// {
|
|
// return true;
|
|
// } else return false;
|
|
|
|
// po zmianach w STX-1439 system zawsze zwaraca full
|
|
return 'full';
|
|
}
|
|
|
|
/**
|
|
* Sprawdź 1 produkt i wykonaj operacje związane z historią cen
|
|
*
|
|
* @param $product object stProduct - odwołanie bezpośrednie referencja
|
|
* @return NULL
|
|
*/
|
|
private function checkProduct($product)
|
|
{
|
|
$price_history_last = new stPriceHistoryLast();
|
|
$price_history_last->setDefaultCurrency($this->default_currency);
|
|
|
|
$id = $product->getId();
|
|
$name = $product->getName();
|
|
|
|
// $currency_id = $product->getCurrencyId();
|
|
// if (empty($currency_id)) $currency_id = $this->default_currency;
|
|
|
|
$price = $product->getPrice();
|
|
$price_netto = $product->getPriceNetto();
|
|
$price_brutto = $product->getPriceBrutto();
|
|
|
|
// Sprawdzamy, czy jest juz wpis w st_prodcu_price_history_last (last)
|
|
if ($price_history_last->isLastProductPrice($id))
|
|
{
|
|
$price_brutto_last = $price_history_last->getLastProductPriceBrutto($id);
|
|
$price_brutto = $product->getFrontendPriceBrutto(); // odczytaj cenę po frontendzie
|
|
|
|
if ($price_brutto_last!=$price_brutto)
|
|
{
|
|
// odczytaj cenę z wczoraj
|
|
$price_history = new stPriceHistory();
|
|
$price_brutto_yesterday = $price_history->getPriceBruttoYesterday($id,$price_brutto_last);
|
|
|
|
if ($price_brutto<$price_brutto_yesterday)
|
|
{
|
|
// wykryto obniżenie ceny
|
|
// zapisz dodatkowo najniższą cenę z ostatnich 30 dni
|
|
$this->Logger('Lower price detected for product %id%',array('%id%'=>$id));
|
|
$last = $price_history_last->getLastProduct($id);
|
|
$price_history->saveLowestPrice($product,$id,'down');
|
|
} elseif ($price_brutto>$price_brutto_yesterday)
|
|
{
|
|
// wykryto zwiększenie ceny
|
|
// zapisz dodatkowo najniższą cenę z ostatnich 30 dni
|
|
$this->Logger('Higher price detected for product %id%',array('%id%'=>$id));
|
|
$last = $price_history_last->getLastProduct($id);
|
|
$price_history->saveLowestPrice($product,$id,'up');
|
|
}
|
|
|
|
$this->Logger('Price changed for product %id%',array('%id%'=>$id));
|
|
|
|
// Aktualizuj dane w st_product_price_history
|
|
$this->updateProductPrice($product);
|
|
|
|
// Aktualiuj dane w last
|
|
$price_history_last->updateLastProductPrice($product);
|
|
|
|
} else
|
|
{
|
|
// Nie zmieniła się cena od poprzedniego wpisu w last.
|
|
// Nic nie zapisuj.
|
|
}
|
|
|
|
} else {
|
|
// Nie ma wpisu w last. Dodaj wpis.
|
|
$price_history_last->addLastProductPrice($product);
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
/**
|
|
* Aktualizuj dane w st_product_price_history, wywołanie zapisania danych, które używane jest w metodzie Save() produktu.
|
|
*
|
|
* @param $product object Product - aktualne dane produktu
|
|
* @return NULL;
|
|
*/
|
|
private function updateProductPrice($product)
|
|
{
|
|
$price_history_last = new stPriceHistoryLast();
|
|
$price_history_last->setDefaultCurrency($this->default_currency);
|
|
|
|
// Kopia aktuanych danych produktu + zmiana na wartości odczytane z ostatniej zmiany w last
|
|
$id = $product->getId();
|
|
$product_before = $product;
|
|
$last = $price_history_last->getLastProduct($id);
|
|
|
|
// Aktualizuj dane pobrane z last. Do aktualizacji przekazywany jest product_before innego obiektu, ktyory zaweira wykorzytywane metody:
|
|
// metody:
|
|
// - getCreatedAt()
|
|
// - getCurrencyPrice()
|
|
// - getPriceBrutto()
|
|
// - getPriceNetto()
|
|
// - getId()
|
|
// - getUpdatedAt()
|
|
// - getCurrencyId()
|
|
|
|
$price_history = new stPriceHistory();
|
|
$price_history->modelUpdate($product,$last);
|
|
|
|
// $this->getLogger()->info('ProductPriceHistory Updated for Product id: %id%',array('%id%'=>$id));
|
|
|
|
return NULL;
|
|
}
|
|
} |