Files
2025-03-12 17:06:23 +01:00

665 lines
18 KiB
PHP

<?php
class AllegroApiOffer extends BaseObject implements Persistent
{
/**
* Oferta allegro
*
* @var object|null
*/
protected $allegroOffer;
/**
* Wybrane opcje produktu
*
* @var array|null
*/
protected $selectedProductOptionsIds = null;
/**
* Oferta Allegro po stronie sklepu
*
* @var AllegroAuction
*/
protected $allegroAuction = null;
public function buildPkeyCriteria()
{
$c = new Criteria();
$c->add(constant($this->getPeer().'::ID'), $this->getId());
return $c;
}
public function getPeer()
{
return get_class($this).'Peer';
}
public function getPrimaryKeyFields($keyType = BasePeer::TYPE_FIELDNAME)
{
$fields = call_user_func(array($this->getPeer(), 'translateFieldName'), 'id', BasePeer::TYPE_FIELDNAME, $keyType);
return array($fields);
}
public function setAfterSalesServices(array $afterSalesServices)
{
foreach ($afterSalesServices as $index => $service)
{
if (empty($service['id']))
{
$service = null;
}
$afterSalesServices[$index] = $service;
}
$this->__call('setAfterSalesServices', [$afterSalesServices]);
}
public function __toString()
{
return $this->getAdminGeneratorTitle();
}
/**
* Undocumented function
*
* @param object|null $allegroOffer
*/
public function __construct($allegroOffer = null)
{
$this->allegroOffer = $allegroOffer;
if (null === $this->allegroOffer)
{
$this->allegroOffer = stAllegroApi::arrayToObject(array(
'product' => null,
'category' => null,
'parameters' => null,
'publication' => null,
'afterSalesServices' => null,
'sizeTable' => null,
'discounts' => null,
'location' => null,
'images' => null,
'tax' => null,
'name' => null,
'external' => null,
'sellingMode' => null,
'stock' => null,
'description' => null,
'delivery' => null,
'payments' => null,
));
}
try
{
if (!isset($allegroOffer->location))
{
$config = stConfig::getInstance('stAllegroBackend');
$this->setLocation(array(
'city' => $config->get('allegro_pl_city'),
'countryCode' => 'PL',
'postCode' => $config->get('allegro_pl_post_code'),
'province' => strtoupper($config->get('allegro_pl_state')),
));
}
}
catch (Exception $e)
{
}
}
public function getAdminGeneratorTitle()
{
return $this->getName() . ' ('.$this->getId().')';
}
public function __clone()
{
if (!$this->isNew())
{
$duration = $this->getPublication()->duration;
$this->allegroOffer->publication = null;
$this->setPublication(array(
'duration' => $duration,
'status' => stAllegroApi::STATUS_INACTIVE,
));
unset($this->allegroOffer->validation);
unset($this->allegroOffer->updatedAt);
unset($this->allegroOffer->createdAt);
$this->setNew(true);
$allegroAuction = new AllegroAuction();
$allegroAuction->fromArray($this->getAllegroAuction()->toArray());
$allegroAuction->setId(null);
$allegroAuction->setCreatedAt(null);
$allegroAuction->setUpdatedAt(null);
$allegroAuction->setCommands(null);
$allegroAuction->setEnded(null);
$allegroAuction->setEndedAt(null);
$this->allegroAuction = $allegroAuction;
$this->allegroOffer->id = null;
}
}
public function setFromArray(array $data)
{
foreach ($data as $name => $value)
{
$property = 'set'.lcfirst(sfInflector::camelize($name));
if ($name == 'description' && $value && !is_array($value))
{
$value = json_decode($value);
}
$this->$property($value);
}
}
public function getPrimaryKey()
{
return $this->getId();
}
public function setPrimaryKey($primaryKey)
{
$this->allegroOffer->id = $primaryKey;
}
/**
* Zwraca GUID oferty
*
* @return string
*/
public function getId()
{
return isset($this->allegroOffer->id) ? $this->allegroOffer->id : null;
}
public function getAllowSync()
{
return $this->getAllegroAuction()->getAllowSync();
}
public function setAllowSync($allowSync)
{
$this->getAllegroAuction()->setAllowSync($allowSync);
}
/**
* Zwraca ofertę Allegro po stronie sklepu
*
* @return AllegroAuction
*/
public function getAllegroAuction()
{
if (null === $this->allegroAuction)
{
$allegroAuction = $this->getId() ? AllegroAuctionPeer::retrieveByAuctionNumber($this->getId()) : null;
if (null === $allegroAuction)
{
$allegroAuction = new AllegroAuction();
$allegroAuction->setName($this->getName());
$allegroAuction->setAuctionId($this->getId());
}
$this->allegroAuction = $allegroAuction;
}
return $this->allegroAuction;
}
/**
* Wystaw ofertę
*
* @return $this
* @throws stAllegroException
* @throws PropelException
*/
public function publish()
{
$api = stAllegroApi::getInstance();
$response = $api->publishOffers(array($this->getId()));
$this->getAllegroAuction()->addCommand($response->id, 'publish');
$this->getAllegroAuction()->setEnded(null);
$this->getAllegroAuction()->setEndedAt(null);
$this->getAllegroAuction()->save();
return $this;
}
/**
* Zakończ ofertę
*
* @return $this
* @throws stAllegroException
* @throws PropelException
*/
public function end()
{
$api = stAllegroApi::getInstance();
$api->publishOffers(array($this->getId()), false);
$this->getAllegroAuction()->setAllowSync(false);
$this->getAllegroAuction()->save();
return $this;
}
public function getStatusLabel()
{
$enum = stAllegroApi::getStatusList();
return isset($enum[$this->getPublication()->status]) ? $enum[$this->getPublication()->status] : '';
}
public function getPrimaryImage()
{
return $this->allegroOffer->primaryImage ? $this->allegroOffer->primaryImage->url : null;
}
public function getStockSold()
{
return $this->getStock()->sold;
}
public function getStockAvailable()
{
return $this->getStock()->available;
}
public function getShopProductStock()
{
$stock = null;
if ($this->getShopProduct())
{
stNewProductOptions::clearCache($this->getShopProduct());
$this->getAllegroAuction()->getProductOptionsArray();
$stock = $this->getShopProduct()->getStock();
}
return $stock;
}
/**
* Ustawia powiązanie produktu z ofertą
*
* @param Product $product
* @return self
*/
public function setShopProduct(Product $product = null)
{
if (null !== $product)
{
$this->getAllegroAuction()->setProduct($product);
$this->setShopProductId($product->getId());
}
return $this;
}
/**
* Zwraca powiązany produkt z ofertą
*
* @return Product
*/
public function getShopProduct()
{
return $this->getAllegroAuction()->getProduct();
}
public function getShopProductCode()
{
return $this->getAllegroAuction()->getProduct() ? $this->getAllegroAuction()->getProduct()->getCode() : null;
}
public function setProductId($id)
{
$this->setShopProductId($id);
}
public function getProductId()
{
return $this->getShopProductId();
}
/**
* Ustawia id powiązanego produktu z ofertą
*
* @param int $id
* @return self
*/
public function setShopProductId($id)
{
$this->getAllegroAuction()->setProductId($id);
$this->setExternal(array('id' => $id));
return $this;
}
/**
* Zwraca id powiązanego produktu z ofertą
*
* @return int
*/
public function getShopProductId()
{
return $this->getShopProduct() ? $this->getShopProduct()->getId() : null;
}
/**
* Zwraca nazwę
*
* @return string|null
*/
public function getName()
{
return $this->allegroOffer ? $this->allegroOffer->name : null;
}
/**
* Ustawia nazwę
*
* @param string $name Nazwa oferty
*
* @return self
*/
public function setName(string $name)
{
$this->allegroOffer->name = $name;
return $this;
}
public function setParameters(array $parameters)
{
$this->allegroOffer->parameters = array();
if (is_numeric(key($parameters)))
{
foreach ($parameters as $parameter)
{
$this->allegroOffer->parameters[$parameter['id']] = stAllegroApi::arrayToObject($parameter);
}
}
else
{
$this->allegroOffer->parameters = $this->transformOfferParameters($parameters);
}
}
public function setShopProductOptions($v)
{
$this->getAllegroAuction()->setProductOptions($v);
}
public function getShopProductOptions()
{
return $this->getAllegroAuction()->getProductOptions();
}
public function isAdminGeneratorActionVisible($name)
{
return $name == 'preview' && $this->getPublication()->status == stAllegroApi::STATUS_ACTIVE ||
$name == 'duplicate' && ($this->getPublication()->status == stAllegroApi::STATUS_ACTIVE || $this->getPublication()->status == stAllegroApi::STATUS_ENDED) ||
$name == '_delete';
}
public function getIsSystemDefault()
{
return $this->getPublication()->status != stAllegroApi::STATUS_INACTIVE;
}
public function getOfferUrl()
{
return stAllegroApi::getOfferUrl($this->getId());
}
public function delete($con = null)
{
stAllegroApi::getInstance()->deleteDraftOffer($this->getId());
}
public function save($con = null)
{
$config = stConfig::getInstance('stAllegroBackend');
$productChanged = $this->getAllegroAuction()->isColumnModified(AllegroAuctionPeer::PRODUCT_ID) || $this->getAllegroAuction()->isColumnModified(AllegroAuctionPeer::PRODUCT_OPTIONS);
if ($this->isNew())
{
$response = stAllegroApi::getInstance()->createOffer($this->allegroOffer);
$this->allegroOffer = $response;
if (null === $this->allegroAuction)
{
$this->allegroAuction = new AllegroAuction();
$this->allegroAuction->setName($response->name);
}
$this->allegroAuction->setAuctionId($response->id);
$this->allegroAuction->save();
$this->setNew(false);
}
else
{
$this->allegroAuction->save();
$product = $this->getAllegroAuction()->getProduct();
if ($productChanged)
{
$this->getAllegroAuction()->getProductOptionsArray();
if ($config->get('offer_sync_product_price'))
{
$commision = new AllegroCommission();
$this->allegroOffer->sellingMode->price->amount = $commision->calculatePrice($product->getPriceBrutto());
}
}
$response = stAllegroApi::getInstance()->updateOffer($this->getId(), $this->allegroOffer);
if ($response->validation && $response->validation->errors)
{
$this->allegroOffer->validation = $response->validation;
}
else
{
$this->allegroOffer = $response;
if ($productChanged)
{
$priceModifiers = $product->getPriceModifiers();
/**
* @see BasketProduct::setPriceModifiers()
**/
foreach ($priceModifiers as $index => $value) {
if (isset($value['custom']['label'])) {
$label = $value['custom']['label'];
unset($value['custom']['label']);
$value['label'] = $label;
$priceModifiers[$index] = $value;
}
}
$stmt = Propel::getConnection()->prepareStatement(sprintf('UPDATE %s SET %s = ?, %s = ?, %s = ?, %s = ?, %s = ? WHERE %s = ?', OrderProductPeer::TABLE_NAME, OrderProductPeer::CODE, OrderProductPeer::IMAGE, OrderProductPeer::NAME, OrderProductPeer::PRODUCT_ID, OrderProductPeer::PRICE_MODIFIERS, OrderProductPeer::ALLEGRO_AUCTION_ID));
$stmt->setString(1, $product->getCode());
$stmt->setString(2, $product->getOptImage());
$stmt->setString(3, $config->get('import_product_name', 'offer') == 'offer' ? $this->getName() : $product->getName());
$stmt->setInt(4, $product->getId());
$stmt->setString(5, serialize($priceModifiers));
$stmt->setString(6, $this->getId());
$stmt->executeQuery();
}
}
}
return true;
}
public function __call($name, $arguments)
{
$type = substr($name, 0, 3);
$property = lcfirst(substr($name, 3));
if (property_exists($this->allegroOffer, $property))
{
if ($type == 'get')
{
return $this->allegroOffer->$property;
}
elseif ($type == 'set')
{
$this->setAllegroApiParameter($property, $arguments[0]);
return;
}
}
throw new \BadMethodCallException('Method does not exist.');
}
protected function setAllegroApiParameter($name, $value)
{
if (is_array($value))
{
if (!isset($value[0]))
{
$this->allegroOffer->$name = stAllegroApi::arrayToObject(array_merge((array)$this->allegroOffer->$name, $value), true);
}
else
{
$this->allegroOffer->$name = stAllegroApi::arrayToObject($value, true);
}
}
else
{
$this->allegroOffer->$name = !empty($value) ? $value : null;
}
}
protected function transformOfferParameters(array $data)
{
$parameters = array();
foreach ($data as $type => $values)
{
switch($type)
{
case 'dictionary':
foreach ($values as $id => $value)
{
if ($value && $value['value'])
{
$parameter = new stdClass();
$parameter->id = $id;
$parameter->valuesIds = is_array($value['value']) ? $value['value'] : array($value['value']);
if (isset($value['custom']) && !empty($value['custom']))
{
$parameter->values = array($value['custom']);
}
$parameters[$id] = $parameter;
}
}
break;
case 'float':
case 'integer':
foreach ($values as $id => $value)
{
$parameter = new stdClass();
$parameter->id = $id;
if (is_array($value))
{
$parameter->rangeValue = new stdClass();
$parameter->rangeValue->from = $value['from'];
$parameter->rangeValue->to = $value['to'];
if ($value['from'] !== "" || $value['to'] !== "")
{
$parameters[$id] = $parameter;
}
}
else
{
$parameter->values = array($value);
if ($value !== "")
{
$parameters[$id] = $parameter;
}
}
}
break;
case 'string':
foreach ($values as $id => $value)
{
$parameter = new stdClass();
$parameter->id = $id;
if (is_array($value))
{
$parameter->values = array();
foreach ($value as $v)
{
if ($v)
{
$parameter->values[] = $v;
}
}
if ($parameter->values)
{
$parameters[$id] = $parameter;
}
}
else
{
$parameter->values = array($value);
if ($value)
{
$parameters[$id] = $parameter;
}
}
}
break;
}
}
return $parameters;
}
}