Files
2025-08-02 16:30:27 +02:00

222 lines
8.4 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\DropshipBundle\Resources\upgrade;
use KupShop\DropshipBundle\Transfer\BaseLinkerTransfer;
use KupShop\DropshipBundle\Transfer\ExpandoTransfer;
use KupShop\DropshipBundle\Transfer\GenericTransfer;
use KupShop\DropshipBundle\Transfer\MallTransfer;
use KupShop\DropshipBundle\Util\TransferLocator;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use Query\Operator;
class DropshipUpgrade extends \UpgradeNew
{
public function check_DropshipmentTable(): bool
{
return $this->checkTableExists('dropshipment');
}
/** Add 'dropshipment' table */
public function upgrade_DropshipmentTable(): void
{
$locator = ServiceContainer::getService(TransferLocator::class);
$types = array_map(fn ($x) => '"'.$x.'"', $locator->getTypes());
sqlQuery('CREATE TABLE dropshipment (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
type ENUM('.implode(',', $types).') NOT NULL,
source_url VARCHAR(255) NOT NULL,
name VARCHAR(50) NOT NULL,
active TINYINT DEFAULT 1,
configuration LONGTEXT DEFAULT NULL,
transformation LONGTEXT DEFAULT NULL,
data MEDIUMTEXT DEFAULT NULL,
last_sync DATETIME DEFAULT NULL
)');
$this->upgradeOK();
}
public function check_OrderDropshipment(): bool
{
return $this->checkTableExists('order_dropshipment');
}
/** Add table `order_dropshipment` */
public function upgrade_OrderDropshipment(): void
{
sqlQuery('CREATE TABLE order_dropshipment
(
id_order INT(11) UNSIGNED NOT NULL,
id_dropshipment INT(11),
id_external VARCHAR(250) NOT NULL,
data MEDIUMTEXT DEFAULT NULL,
UNIQUE INDEX `order_dropshipment_id` (id_dropshipment, id_external),
CONSTRAINT `FK_order_dropshipment_id_order` FOREIGN KEY (`id_order`) REFERENCES `orders` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_order_dropshipment_id_dropshipment` FOREIGN KEY (`id_dropshipment`) REFERENCES `dropshipment` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
);');
$this->upgradeOK();
}
public function check_DropshipmentTypeEnum(): bool
{
$locator = ServiceContainer::getService(TransferLocator::class);
return $this->checkEnumOptions('dropshipment', 'type', $locator->getTypes());
}
/** Update dropshipment.type enum */
public function upgrade_DropshipmentTypeEnum(): void
{
$locator = ServiceContainer::getService(TransferLocator::class);
$this->updateEnumOptions('dropshipment', 'type', $locator->getTypes());
$this->upgradeOK();
}
public function check_DropshipmentInSettings(): bool
{
$settings = \Settings::getDefault();
$dropship = $settings->loadValue('dropship');
return !empty($dropship);
}
/** Move dropshipment configuration from `settings` into `dropshipment` table */
public function upgrade_DropshipmentInSettings(): void
{
$settings = \Settings::getDefault();
$dropship = $settings->loadValue('dropship');
// mall to dropshipment table
if (!empty($dropship['mall']['api_key'])) {
$mallDropshipmentId = sqlGetConnection()->transactional(function () use ($dropship) {
sqlQueryBuilder()
->insert('dropshipment')
->directValues(
[
'type' => MallTransfer::getType(),
'source_url' => 'https://partners.mallgroup.com/orders.xml?client_id='.$dropship['mall']['api_key'],
'name' => 'MALL',
'active' => 1,
'configuration' => json_encode($dropship['mall'] ?? []),
]
)->execute();
return (int) sqlInsertId();
});
// naplnim order_dropshipment objednavkama z MALLu
sqlQuery('INSERT IGNORE INTO order_dropshipment (id_order, id_dropshipment, id_external, data)
SELECT
id,
'.$mallDropshipmentId.' as id_dropshipment,
JSON_VALUE(note_admin, \'$.mall.order_id\'),
JSON_EXTRACT(note_admin, \'$.mall\')
FROM orders
WHERE FIND_IN_SET("DSM", flags);');
}
// expando to dropshipment table
if (!empty($dropship['expando']['api_key'])) {
$expandoDropshipmentId = sqlGetConnection()->transactional(function () use ($dropship) {
sqlQueryBuilder()
->insert('dropshipment')
->directValues(
[
'type' => ExpandoTransfer::getType(),
'source_url' => 'https://app.expan.do/api/v2/orderfeed?days=3&access_token='.$dropship['expando']['api_key'],
'name' => 'Expando',
'active' => 1,
'configuration' => json_encode($dropship['expando'] ?? []),
]
)->execute();
return (int) sqlInsertId();
});
// naplnim order_dropshipment objednavkama z Expanda
sqlQuery('INSERT IGNORE INTO order_dropshipment (id_order, id_dropshipment, id_external, data)
SELECT
id,
'.$expandoDropshipmentId.' as id_dropshipment,
JSON_VALUE(note_admin, \'$.expando.orderId\'),
JSON_EXTRACT(note_admin, \'$.expando\')
FROM orders
WHERE FIND_IN_SET("DSE", flags);');
}
$settings->deleteValue('dropship');
$this->upgradeOK();
}
public function check_invoiceNumberIdColumn(): bool
{
return findModule(\Modules::INVOICES) && $this->checkColumnExists('dropshipment', 'id_invoice_number');
}
/** Add dropshipment.id_invoice_number column */
public function upgrade_invoiceNumberIdColumn(): void
{
if (findModule(\Modules::INVOICES)) {
sqlQuery('ALTER TABLE dropshipment ADD COLUMN id_invoice_number INT DEFAULT NULL');
sqlQuery('ALTER TABLE dropshipment ADD FOREIGN KEY (id_invoice_number) REFERENCES invoice_numbers (id) ON DELETE CASCADE ON UPDATE CASCADE');
$this->upgradeOK();
}
}
public function check_GenericTypeMappingGroups(): bool
{
return sqlQueryBuilder()
->select('id')
->from('dropshipment')
->andWhere(Operator::inStringArray([BaseLinkerTransfer::getType(), GenericTransfer::getType()], 'type'))
->andWhere('configuration NOT LIKE "%ignore_on_mapping_not_found%"')
->execute()->rowCount() > 0;
}
/** Update dropship config for types: generic and baselinker */
public function upgrade_GenericTypeMappingGroups(): void
{
$qb = sqlQueryBuilder()
->select('id, configuration')
->from('dropshipment')
->andWhere(Operator::inStringArray([BaseLinkerTransfer::getType(), GenericTransfer::getType()], 'type'))
->andWhere('configuration NOT LIKE "%ignore_on_mapping_not_found%"');
foreach ($qb->execute() as $item) {
$configuration = json_decode($item['configuration'] ?: '', true) ?: [];
$configuration['ignore_on_mapping_not_found'] = 'N';
if (!array_key_exists('mappingGroups', $configuration)
&& (array_key_exists('deliveries', $configuration) || array_key_exists('payments', $configuration))) {
$group = [
'name' => 'Výchozí',
'deliveries' => $configuration['deliveries'],
'payments' => $configuration['payments'],
];
$configuration['mappingGroups'] = [$group];
unset($configuration['deliveries'], $configuration['payments']);
}
sqlQueryBuilder()
->update('dropshipment')
->directValues(['configuration' => json_encode($configuration)])
->where(Operator::equals(['id' => $item['id']]))
->execute();
}
$this->upgradeOK();
}
}