Files
interblue.pl/modules/x13allegro/controllers/admin/AdminXAllegroAuctionsEditController.php
2024-10-25 14:16:28 +02:00

273 lines
12 KiB
PHP

<?php
require_once (dirname(__FILE__) . '/../../x13allegro.php');
/**
* @deprecated 6.4.0
*/
class AdminXAllegroAuctionsEditController extends XAllegroController
{
protected $allegroAutoLogin = true;
protected $_auctions = array();
public function __construct()
{
parent::__construct();
$this->tabAccess = Profile::getProfileAccess($this->context->employee->id_profile, Tab::getIdFromClassName('AdminXAllegroAuctionsEdit'));
$this->tpl_folder = 'x_allegro_auctions_edit/';
}
public function initToolbar()
{
parent::initToolbar();
$this->toolbar_btn['back'] = array(
'href' => $this->context->link->getAdminLink('AdminXAllegroAuctionsList') . '&show=' . Tools::getValue('show'),
'desc' => $this->l('Powrót do listy aukcji')
);
unset($this->toolbar_btn['new']);
}
public function initPageHeaderToolbar()
{
$this->page_header_toolbar_btn['xallegro_back'] = array(
'href' => $this->context->link->getAdminLink('AdminXAllegroAuctionsList') . '&show=' . Tools::getValue('show'),
'desc' => $this->l('Powrót do listy aukcji'),
'icon' => 'process-icon-arrow-left icon-arrow-left',
);
parent::initPageHeaderToolbar();
}
public function init()
{
parent::init();
$auctionsIds = XAllegroAuction::getIdAuctionsByAllegroId(explode(',', Tools::getValue('id_auction')));
$data = array();
if ($auctionsIds)
{
foreach ($auctionsIds as $id)
{
$auction = new XAllegroAuction($id['id_xallegro_auction']);
$auction->productObj = new Product($auction->id_product, true, $this->context->language->id, $auction->id_shop);
if (!Validate::isLoadedObject($auction->productObj)) {
$this->errors[] = sprintf($this->l('Aukcja %s, powiązanie odnosi się do nieistniejacego produktu.'), '<b>#' . $auction->id_auction . '</b>');
continue;
}
$item = $this->allegroApi->showItemInfoExt($auction->id_auction);
$auction->item = json_decode($auction->item);
// @todo fixme - pobiera aktualna cenne z Allegro
$auction->price_buy_now = $item->itemListInfoExt->itBuyNowPrice;
if (!$auction->item) {
$auction->item = new StdClass();
$auction->item->title = $item->itemListInfoExt->itName;
}
$combinations = $auction->productObj->getAttributesResume($this->context->language->id);
if ($combinations) {
foreach ($combinations as $combination) {
if ($combination['id_product_attribute'] == $auction->id_product_attribute) {
$auction->productObj->quantity = Product::getQuantity($auction->productObj->id, $combination['id_product_attribute']);
$auction->productObj->price_buy_now = XAllegroProduct::getProductStaticPrice($auction->productObj->id, $combination['id_product_attribute'], $this->context);
$auction->productObj->name_attribute = $combination['attribute_designation'];
break;
}
}
}
else {
$auction->productObj->quantity = Product::getQuantity($auction->productObj->id, 0);
$auction->productObj->price_buy_now = XAllegroProduct::getProductStaticPrice($auction->productObj->id, 0, $this->context);
}
$auction->productObj->price_buy_now_default = $auction->productObj->price_buy_now;
$auction->productObj->price_buy_now = XAllegroProduct::calculatePrice($auction->productObj->price_buy_now);
$auction->productObj->quantity_max = XAllegroApiTools::calculateMaxQuantity($auction->productObj->quantity);
$auction->productObj->quantity_oos = XAllegroProduct::setOOS($auction->productObj->out_of_stock);
$data[$auction->id_auction] = $auction;
}
}
$this->_auctions = $data;
}
public function initContent()
{
parent::initContent();
$this->context->smarty->assign(array(
'toolbar_title' => $this->toolbar_title,
'toolbar_btn' => $this->toolbar_btn,
'toolbar_scroll' => true,
'show' => Tools::getValue('show'),
'id_auction' => Tools::getValue('id_auction'),
'quantity_check' => (int)XAllegroConfiguration::get('QUANITY_CHECK'),
'price_markup' => (bool)(XAllegroConfiguration::get('MARKUP_PERCENT') || XAllegroConfiguration::get('MARKUP_VALUE')),
'auctions' => $this->_auctions,
'token' => $this->token
));
}
public function postProcess()
{
if (Tools::isSubmit('submitEditAuctions'))
{
if (!Tools::getValue('item')) {
$this->errors[] = $this->l('Nie wybrano żadnych przedmiotów.');
return parent::postProcess();
}
$newItem = Tools::getValue('item');
/** @var XAllegroAuction $auction */
foreach ($this->_auctions as &$auction)
{
$fields =
$confirmations =
$errors =
$not_changed = array();
$update_price = false;
$update = false;
$message = $this->l('Aukcja') . ' <strong>#' . $auction->id_auction . '</strong><br/>';
if (isset($newItem[$auction->id_auction])
&& isset($newItem[$auction->id_auction]['enabled'])
&& $newItem[$auction->id_auction]['enabled']
) {
if (abs($newItem[$auction->id_auction]['price_buy_now'] - $auction->price_buy_now) > 0.000001)
{
$old_price = $auction->price_buy_now;
$auction->price_buy_now = (float)str_replace(',', '.', $newItem[$auction->id_auction]['price_buy_now']);
$update_price = true;
}
if ((int)$newItem[$auction->id_auction]['quantity'] != (int)$auction->quantity)
{
$item = $this->allegroApi->showItemInfoExt($auction->id_auction);
$stock_available = StockAvailable::getQuantityAvailableByProduct($auction->id_product, $auction->id_product_attribute, $auction->id_shop);
$quantity_sold = $item->itemListInfoExt->itStartingQuantity - $item->itemListInfoExt->itQuantity;
$oldQuantity = (int)$auction->quantity;
$newQuantity = (int)$newItem[$auction->id_auction]['quantity'];
if ($newQuantity > $stock_available && XAllegroConfiguration::get('QUANITY_CHECK') && !(int)$newItem[$auction->id_auction]['quantity_oos']) {
$errors[] = $this->l('Podana ilość jest większą niż dostępna.');
}
else {
$fields[] = array(
'fid' => XAllegroApi::FIELD_QUANTITY,
'fvalueInt' => $newQuantity
);
$auction->quantity = (int)$newQuantity;
$auction->quantity_start = (int)($newQuantity + $quantity_sold);
$update = true;
}
}
}
if ($update || $update_price)
{
if ($update_price) {
$this->allegroApi->put('offers/{offerId}/change-price-commands/{commandId}', array(
'offerId' => $auction->id_auction,
'commandId' => UUID::uuid4()
), array(
'input' => array(
'buyNowPrice' => array(
'amount' => $auction->price_buy_now,
'currency' => 'PLN'
)
)
));
$response = $this->allegroApi->getResponse();
if (empty($response->output->errors)) {
$confirmations[] = $this->l('Nowa cena:') . ' ' . number_format($auction->price_buy_now, 2, ',', '') . 'zł';
XAllegroLogger::getInstance()
->setType('PUT')
->setCode('panel-price-allegro')
->setAuction($auction->id_auction)
->setAccount($this->allegroApi->account->id)
->setMessage(array(
'old_price' => $old_price,
'new_price' => $auction->price_buy_now
))
->log();
}
}
if ($update)
{
$fields = $this->allegroApi->fillEmptyFidValues($fields);
$result = $this->allegroApi->changeItemFields($auction->id_auction, $fields);
if ($result) {
foreach ($result->changedItem->itemFields->item as $item) {
if ($item->fid == XAllegroApi::FIELD_QUANTITY) {
$confirmations[] = $this->l('Nowa ilość dostepna ilość:') . ' ' . $item->fvalueInt;
}
}
XAllegroLogger::getInstance()
->setType('PUT')
->setCode('panel-auction-quantity')
->setAuction($auction->id_auction)
->setAccount($this->allegroApi->account->id)
->setMessage(array(
'old_quantity' => $oldQuantity,
'new_start_quantity' => (int)($newQuantity + $quantity_sold),
'new_quantity' => (int)$newQuantity
))
->log();
}
else {
$errors = array_merge($errors, $this->allegroApi->getErrors());
}
}
}
else {
$not_changed[] = $auction->id_auction;
}
if (empty($errors) && !in_array($auction->id_auction, $not_changed))
{
$auction->item = json_encode($auction->item);
$auction->save();
if (!empty($confirmations)) {
$this->confirmations[] = '<p>' . $message . implode('<br/>', $confirmations) . '<br/><br/>' .
$this->l('Zmiany będą widoczne na portalu Allegro w ciągu 5 minut.'). '</p>';
}
}
else {
$this->errors[] = '<p>' . $message . implode('<br/>', $errors) . '</p>';
}
}
if (!empty($not_changed)) {
$this->informations[] = $this->l('Nie wprowadzono zmian dla aukcji:') . ' ' . implode(', ', $not_changed);
}
Tools::redirectAdmin($this->context->link->getAdminLink('AdminXAllegroAuctionsList') . '&show=' . Tools::getValue('show'));
}
return parent::postProcess();
}
}