Skip to content

Commit

Permalink
beta
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitexus committed Nov 28, 2023
1 parent 1d629cb commit b18c009
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 50 deletions.
4 changes: 4 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
discomp2abraflexi (0.1.0) UNRELEASED; urgency=low

[ Vítězslav Dvořák ]
* Initial release. Closes: #nnnn
<nnnn is the bug number of your ITP>

[ vitex ]
* 0.2.0 Beta Version

-- Vítězslav Dvořák <[email protected]> Sat, 18 Nov 2023 10:46:07 +0100
119 changes: 69 additions & 50 deletions src/Discomp/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

namespace SpojeNet\Discomp;

use DateTime;
use Exception;
use Swoole\MySQL\Exception as Exception2;
use AbraFlexi\RO;
use AbraFlexi\RW;

/**
* Description of Importer
*
Expand Down Expand Up @@ -88,13 +94,13 @@ class Importer extends \Ease\Sand

/**
*
* @var \AbraFlexi\RW
* @var RW
*/
private $atributType;

/**
*
* @var \AbraFlexi\RW
* @var RW
*/
private $atribut;

Expand All @@ -120,15 +126,14 @@ public function __construct()
$this->sokoban = new \AbraFlexi\Cenik(null, ['ignore404' => true]);
$this->sokoban->setObjectName('Pricelist');
$this->suplier = \AbraFlexi\RO::code(\Ease\Shared::cfg('ABRAFLEXI_DISCOMP_CODE', 'DISCOMP'));
$this->pricer = new \AbraFlexi\Dodavatel(['firma' => $this->suplier, 'poznam' => 'Import: ' . \Ease\Shared::AppName() . ' ' . \Ease\Shared::AppVersion()], ['evidence' => 'dodavatel', 'autoload' => false]);
$this->pricer = new \AbraFlexi\Dodavatel(['firma' => $this->suplier, 'poznam' => 'Import: ' . \Ease\Shared::AppName() . ' ' . \Ease\Shared::AppVersion() . "\nhttps://github.com/Spoje-NET/discomp2abraflexi"], ['evidence' => 'dodavatel', 'autoload' => false]);
$this->category = new \AbraFlexi\StromCenik();
$this->atribut = new \AbraFlexi\RW(null, ['evidence' => 'atribut']);
$this->atributType = new \AbraFlexi\RW(null, ['evidence' => 'typ-atributu', 'ignore404' => true]);

$this->atribut = new RW(null, ['evidence' => 'atribut']);
$this->atributType = new RW(null, ['evidence' => 'typ-atributu', 'ignore404' => true]);
$this->atributType->setObjectName('AttributeType');
if (\Ease\Shared::cfg('APP_DEBUG', false)) {
$this->logBanner();
}

$this->addStatusMessage(_('Supplier Exists'), $this->ensureSupplierExists() ? 'success' : 'error');
$this->addStatusMessage(_('Category Root Exists'), $this->ensureCategoryRootExists() ? 'success' : 'error');
}
Expand Down Expand Up @@ -180,25 +185,32 @@ public function freshItems()
{
$freshItems = $this->getFreshItems();
foreach ($freshItems as $pos => $activeItemData) {
$this->sokoban->dataReset();
$discompItemCode = $activeItemData['CODE'];
$this->sokoban->setObjectName('(' . $pos . '/' . count($freshItems) . ') StoreItem:' . $discompItemCode);

if (is_array($activeItemData['PART_NUMBER'])) {
$this->addStatusMessage('WTF? ' . json_encode($activeItemData['PART_NUMBER']), 'debug');
$this->sokoban->addStatusMessage('WTF? ' . json_encode($activeItemData['PART_NUMBER']), 'debug');
continue;
}
$this->sokoban->setDataValue('kod', $activeItemData['PART_NUMBER']);

$recordCheck = $this->sokoban->getColumnsFromAbraFlexi(['dodavatel', 'nazev', 'popis', 'pocetPriloh'], ['id' => \AbraFlexi\RO::code($activeItemData['PART_NUMBER'])]);

$this->sokoban->dataReset();
$this->sokoban->setDataValue('dodavatel', $this->suplier);
if ($activeItemData['ITEM_TYPE'] != 'product') {
echo ''; //TODO
$this->sokoban->addStatusMessage('NO Product' . json_encode($activeItemData['PART_NUMBER']), 'debug');
}
$this->sokoban->setDataValue('typZasobyK', \Ease\Shared::cfg('DISCOMP_TYP_ZASOBY', 'typZasoby.material')); //TODO: ???
$this->sokoban->setDataValue('skladove', true); //TODO: ???
$this->sokoban->setDataValue('kod', $activeItemData['PART_NUMBER']);
$this->sokoban->setDataValue('ean', $discompItemCode);
//$this->sokoban->setDataValue('eanKod', $activeItemData['EAN']);

$this->sokoban->setDataValue('nakupCena', ceil(floatval($activeItemData['PURCHASE_PRICE'])));

if (array_key_exists('STANDARD_PRICE', $activeItemData)) {
$this->sokoban->setDataValue('cenaBezna', $activeItemData['STANDARD_PRICE']);
}

$this->sokoban->setDataValue('nazev', $activeItemData['NAME']);

if (array_key_exists('WARRANTY', $activeItemData)) {
Expand All @@ -214,21 +226,21 @@ public function freshItems()

$this->sokoban->setDataValue('mj1', \AbraFlexi\RO::code($activeItemData['UNIT']));

if ($activeItemData['MANUFACTURER']) {
if (array_key_exists('MANUFACTURER', $activeItemData)) {
$this->sokoban->setDataValue('vyrobce', $this->findManufacturerCode($activeItemData['MANUFACTURER']));
}

if (empty($recordCheck)) {
$this->discomper->addStatusMessage($pos . '/' . count($freshItems) . ' ' . $activeItemData['CODE'] . ': ' . $activeItemData['NAME'] . ' new item', $this->sokoban->insertToAbraFlexi() ? 'success' : 'error');
$this->discomper->addStatusMessage($activeItemData['CODE'] . ': ' . $activeItemData['NAME'] . ' new item', $this->sokoban->insertToAbraFlexi() ? 'success' : 'error');
if (array_key_exists('IMAGES', $activeItemData) && array_key_exists('IMAGE', $activeItemData['IMAGES'])) {
if (is_array($activeItemData['IMAGES']['IMAGE'])) {
foreach ($activeItemData['IMAGES']['IMAGE'] as $imgPos => $imgUrl) {
$stdImg = \AbraFlexi\Priloha::addAttachment($this->sokoban, $discompItemCode . '_' . $imgPos . '.jpg', $this->discomper->getImage($imgUrl), $this->discomper->getResponseMime());
$this->sokoban->addStatusMessage('Img: ' . $this->sokoban . ' ' . $imgUrl, $stdImg->lastResponseCode == 201 ? 'success' : 'error');
$this->sokoban->addStatusMessage(RO::uncode($this->sokoban->getRecordCode()) . ' Img: ' . $imgUrl, $stdImg->lastResponseCode == 201 ? 'success' : 'error');
}
} else {
$stdImg = \AbraFlexi\Priloha::addAttachment($this->sokoban, $discompItemCode . '_' . 0 . '.jpg', $this->discomper->getImage($activeItemData['IMAGES']['IMAGE']), $this->discomper->getResponseMime());
$this->sokoban->addStatusMessage('Img: ' . $this->sokoban . ' ' . $activeItemData['IMAGES']['IMAGE'], $stdImg->lastResponseCode == 201 ? 'success' : 'error');
$this->sokoban->addStatusMessage(RO::uncode($this->sokoban->getRecordCode()) . ' Img: ' . $activeItemData['IMAGES']['IMAGE'], $stdImg->lastResponseCode == 201 ? 'success' : 'error');
}
}
if ($this->sokoban->lastResponseCode == 201) {
Expand All @@ -237,13 +249,13 @@ public function freshItems()
$errors++;
}
} else {
$progressInfo = '(' . $pos . '/' . count($freshItems) . ') ' . $activeItemData['CODE'] . ': ' . $activeItemData['NAME'];
if (array_key_exists('dodavatel', $recordCheck) && ($recordCheck['dodavatel'] == $this->suplier)) {
$this->discomper->addStatusMessage($pos . '/' . count($freshItems) . ' ' . $activeItemData['CODE'] . ': ' . $activeItemData['NAME'] . ' update', $this->sokoban->insertToAbraFlexi() ? 'success' : 'error');
$this->discomper->addStatusMessage($progressInfo . ' update', $this->sokoban->insertToAbraFlexi() ? 'success' : 'error');
} else {
$this->discomper->addStatusMessage($pos . '/' . count($freshItems) . ' ' . $activeItemData['CODE'] . ': ' . $activeItemData['NAME'] . ' already iported', 'info');
$this->discomper->addStatusMessage($progressInfo . ' already enlisted', 'info');
}
}

if (array_key_exists('CATEGORIES', $activeItemData)) {
foreach ($this->prepareCategories($activeItemData['CATEGORIES']['CATEGORY']) as $category) {
$this->category->insertToAbraFlexi(['idZaznamu' => \AbraFlexi\RO::code($activeItemData['PART_NUMBER']), 'uzel' => $this->treeCache[$category]]);
Expand Down Expand Up @@ -279,13 +291,20 @@ public function syncProperty($name, $value)
{
$attributeCode = \AbraFlexi\RO::code(mb_substr($name, -20));
if (empty($this->atributType->loadFromAbraFlexi($attributeCode))) {
$this->atributType->addStatusMessage($this->sokoban . ': Attribute ' . $name . ' created', $this->atributType->sync(['kod' => \AbraFlexi\RO::uncode($attributeCode), 'nazev' => $name, 'typAtributK' => 'typAtribut.retezec']) ? 'success' : 'error');
$this->atributType->addStatusMessage(RO::uncode($this->sokoban->getRecordCode()) . ': Attribute ' . $name . ' created', $this->atributType->sync(['kod' => \AbraFlexi\RO::uncode($attributeCode), 'nazev' => $name, 'typAtributK' => 'typAtribut.retezec']) ? 'success' : 'error');
}
$this->atribut->dataReset();
$this->atribut->setDataValue('valString', $value);
$this->atribut->setDataValue('hodnota', $value);
if (is_float($value)) {
$this->atribut->setDataValue('valNumeric', floatval($value));
} elseif (is_integer($value)) {
$this->atribut->setDataValue('valInteger', intval($value));
} else {
$this->atribut->setDataValue('valString', $value);
}
$this->atribut->setDataValue('cenik', $this->sokoban);
$this->atribut->setDataValue('typAtributu', $this->atributType);
$this->atribut->addStatusMessage($this->sokoban . ': ' . $name . ': ' . $value, $this->atribut->sync() ? 'success' : 'error');
$this->atribut->addStatusMessage(RO::uncode($this->sokoban->getRecordCode()) . ': ' . $name . ': ' . $value, $this->atribut->sync() ? 'success' : 'error');
}

/**
Expand Down Expand Up @@ -398,7 +417,7 @@ public function allTimeItems()
}
if (array_key_exists('EnlargementIs', $stoItem) && $stoItem['EnlargementIs'] == 1) {
$largeImg = \AbraFlexi\Priloha::addAttachment($this->sokoban, $discompItemCode . '.jpg', $this->discomper->getImage($largeImageUrl), $this->discomper->getResponseMime());
$this->sokoban->addStatusMessage($this->sokoban . ' ' . $largeImageUrl, $largeImg->lastResponseCode == 201 ? 'success' : 'error');
$this->sokoban->addStatusMessage(\AbraFlexi\RO::uncode($this->sokoban) . ' ' . $largeImageUrl, $largeImg->lastResponseCode == 201 ? 'success' : 'error');
}

if ($this->sokoban->lastResponseCode == 201) {
Expand Down Expand Up @@ -434,25 +453,25 @@ public function updatePrice($activeItemData)
{
$this->pricer->unsetDataValue('id');
$this->pricer->setDataValue('cenik', $this->sokoban);

$this->pricer->setDataValue('kodIndi', $activeItemData['CODE']);
$priceFound = $this->pricer->loadFromAbraFlexi(['cenik' => $this->sokoban, 'firma' => $this->suplier]);
if (empty($priceFound)) {
$this->pricer->setDataValue('cenik', $this->sokoban);
$this->pricer->setDataValue('firma', $this->suplier);
}
$this->pricer->setDataValue('nakupCena', $activeItemData['PURCHASE_PRICE']); //TODO: Confirm column
$this->pricer->setDataValue('mena', \AbraFlexi\RO::code($activeItemData['CURRENCY']));
$this->pricer->setDataValue('cenik', \AbraFlexi\RO::code($activeItemData['PART_NUMBER']));
$this->pricer->setDataValue('mena', RO::code($activeItemData['CURRENCY']));
$this->pricer->setDataValue('cenik', RO::code($activeItemData['PART_NUMBER']));

if (array_key_exists('STOCK', $activeItemData) && array_key_exists('AMOUNT', $activeItemData['STOCK'])) {
$this->pricer->setDataValue('sumDostupMj', $activeItemData['STOCK']['AMOUNT']);
if (array_key_exists('STOCK', $activeItemData) && array_key_exists('AMOUNT', $activeItemData['STOCK']) && floatval($activeItemData['STOCK']['AMOUNT'])) {
$this->pricer->setDataValue('stavMJ', $activeItemData['STOCK']['AMOUNT']);
} else {
$this->pricer->setDataValue('sumDostupMj', 0);
$this->pricer->setDataValue('stavMJ', 0);
}

try {
$this->pricer->insertToAbraFlexi();
$this->pricer->addStatusMessage('supplier price update: ' . $this->sokoban->getDataValue('kod') . ': ' . $this->pricer->getDataValue('nakupCena') . ' ' . \AbraFlexi\RO::uncode($this->pricer->getDataValue('mena')), $this->pricer->lastResponseCode == 201 ? 'success' : 'error');
$this->pricer->addStatusMessage('supplier price update: ' . RO::uncode($this->sokoban->getRecordCode()) . ': ' . $this->pricer->getDataValue('nakupCena') . ' ' . RO::uncode($this->pricer->getDataValue('mena')), $this->pricer->lastResponseCode == 201 ? 'success' : 'error');
} catch (\AbraFlexi\Exception $exc) {
echo $exc->getTraceAsString();
$this->errors++;
Expand All @@ -463,36 +482,36 @@ public function updatePrice($activeItemData)
* Prepare processing interval
*
* @param string $scope
* @throws Exception
* @throws Exception2
*/
public function scopeToInterval($scope)
{
switch ($scope) {
case 'yesterday':
$this->since = new \DateTime("yesterday");
$this->until = new \DateTime("today");
$this->since = new DateTime("yesterday");
$this->until = new DateTime("today");
break;
case 'last_week':
$this->since = new \DateTime("monday last week");
$this->until = new \DateTime("sunday last week");
$this->since = new DateTime("monday last week");
$this->until = new DateTime("sunday last week");
break;
case 'current_month':
$this->since = new \DateTime("first day of this month");
$this->until = new \DateTime();
$this->since = new DateTime("first day of this month");
$this->until = new DateTime();
break;
case 'last_month':
$this->since = new \DateTime("first day of last month");
$this->until = new \DateTime("last day of last month");
$this->since = new DateTime("first day of last month");
$this->until = new DateTime("last day of last month");
break;

case 'last_two_months':
$this->since = (new \DateTime("first day of last month"))->modify('-1 month');
$this->until = (new \DateTime("last day of last month"));
$this->since = (new DateTime("first day of last month"))->modify('-1 month');
$this->until = (new DateTime("last day of last month"));
break;

case 'previous_month':
$this->since = new \DateTime("first day of -2 month");
$this->until = new \DateTime("last day of -2 month");
$this->since = new DateTime("first day of -2 month");
$this->until = new DateTime("last day of -2 month");
break;

case 'two_months_ago':
Expand Down Expand Up @@ -522,7 +541,7 @@ public function scopeToInterval($scope)
break;

default:
throw new \Exception('Unknown scope ' . $scope);
throw new Exception('Unknown scope ' . $scope);
break;
}
$this->since = $this->since->setTime(0, 0);
Expand All @@ -537,7 +556,7 @@ public function prepareCategories($categoriesRaw)
{
$categories = [];
foreach ($categoriesRaw as $tree) {
$categories[] = $this->categoryBranch(explode(' > ', $tree));
$categories[] = $this->categoryBranch(explode(' > ', 'Discomp > ' . $tree));
}
return $categories;
}
Expand Down Expand Up @@ -567,7 +586,7 @@ public function categoryBranch(array $nodes)
*/
public function createBranchNode(string $node, int $level, string $parent)
{
$kod = \AbraFlexi\RO::code(substr(\Ease\Functions::rip(substr(\AbraFlexi\RO::uncode($parent), 0, 10) . str_replace(' ', '', $node)), 0, 30));
$kod = RO::code(substr(\Ease\Functions::rip(substr(RO::uncode($parent), 0, 10) . str_replace(' ', '', $node)), 0, 30));
if (array_key_exists($level, $this->levels)) {
$this->levels[$level]++;
} else {
Expand All @@ -577,7 +596,7 @@ public function createBranchNode(string $node, int $level, string $parent)
if ($strom->lastResponseCode == 404) {
$strom->setDataValue('id', $kod);
$strom->setDataValue('nazev', $node);
$strom->setDataValue('strom', \AbraFlexi\RO::code(self::$ROOT));
$strom->setDataValue('strom', RO::code(self::$ROOT));
if ($parent) {
$strom->setDataValue('otec', $parent);
}
Expand All @@ -604,7 +623,7 @@ public function ensureCategoryRootExists()
'popis' => 'Discomp Import',
'tabulka' => 'cz.winstrom.vo.cen.Cenik',
];
$root = new \AbraFlexi\RW(\AbraFlexi\RO::code(self::$ROOT), ['evidence' => 'strom-koren', 'ignore404' => true]);
$root = new RW(RO::code(self::$ROOT), ['evidence' => 'strom-koren', 'ignore404' => true]);
return $root->lastResponseCode == 200 ? true : $root->insertToAbraFlexi($discpmpData);
}

Expand All @@ -615,10 +634,10 @@ public function ensureCategoryRootExists()
*/
public function findManufacturerCode(string $manufacturerName)
{
$manufacturerCode = \AbraFlexi\RO::code($manufacturerName);
$manufacturerCode = RO::code($manufacturerName);
$manufacturer = new \AbraFlexi\Adresar($manufacturerCode, ['ignore404' => true]);
if ($manufacturer->lastResponseCode == 404) {
$manufacturer->addStatusMessage(sprintf(_('New Manufacturer %s'), $manufacturerName), $manufacturer->sync(['kod' => \AbraFlexi\RO::uncode($manufacturerName), 'nazev' => $manufacturerName]) ? 'success' : 'error');
$manufacturer->addStatusMessage(sprintf(_('New Manufacturer %s'), $manufacturerName), $manufacturer->sync(['kod' => RO::uncode($manufacturerName), 'nazev' => $manufacturerName]) ? 'success' : 'error');
}
return $manufacturer;
}
Expand Down

0 comments on commit b18c009

Please sign in to comment.