82 lines
2.7 KiB
PHP
82 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace External\ZNZBundle\Synchronizers;
|
|
|
|
use Query\Operator;
|
|
|
|
class ProducerSynchronizer extends BaseSynchronizer
|
|
{
|
|
protected static string $type = 'producer';
|
|
|
|
public static function getHandledTables(): array
|
|
{
|
|
return [
|
|
'ProduktVyrobce' => 'processProducer',
|
|
];
|
|
}
|
|
|
|
public function processProducer(array $item): void
|
|
{
|
|
if ($this->isDeleteMessage($item)) {
|
|
if ($producerId = $this->znzUtil->getMapping(static::getType(), (int) $item['meta']['unique_id'])) {
|
|
sqlQueryBuilder()
|
|
->delete('producers')
|
|
->where(Operator::equals(['id' => $producerId]))
|
|
->execute();
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
$znzId = $item['IdVyrobce'];
|
|
|
|
if (!($producerId = $this->znzUtil->getMapping(static::getType(), $znzId))) {
|
|
$producerId = sqlGetConnection()->transactional(function () use ($znzId, $item) {
|
|
// nejdriv zkusim najit vyrobce podle nazvu (kvuli starymu zpusoby pracovan s vyrobci)
|
|
$producerId = sqlQueryBuilder()
|
|
->select('id')
|
|
->from('producers')
|
|
->where(Operator::equals(['name' => $item['Znacka']]))
|
|
->execute()->fetchOne();
|
|
|
|
// pokud jsem nenasel vyrobce podle nazvu, tak ho zalozim
|
|
if (!$producerId) {
|
|
sqlQueryBuilder()
|
|
->insert('producers')
|
|
->directValues(
|
|
[
|
|
'name' => $item['Znacka'],
|
|
]
|
|
)
|
|
->execute();
|
|
|
|
$producerId = (int) sqlInsertId();
|
|
}
|
|
|
|
// vytvorim mapping
|
|
$this->znzUtil->createMapping(static::getType(), $znzId, $producerId);
|
|
|
|
return $producerId;
|
|
});
|
|
}
|
|
|
|
// aktualizace dat vyrobce
|
|
sqlQueryBuilder()
|
|
->update('producers')
|
|
->directValues(
|
|
[
|
|
'name' => $item['Znacka'],
|
|
'company_name' => $item['Nazev'] ?? null,
|
|
'company_street' => $item['Ulice'] ?? null,
|
|
'company_city' => $item['Misto'] ?? null,
|
|
'company_zip' => $item['PSC'] ?? null,
|
|
'company_country' => $item['IdZeme'] ?? null,
|
|
'company_web' => $item['WWW'] ?? null,
|
|
'company_email' => $item['Email'] ?? null,
|
|
]
|
|
)
|
|
->where(Operator::equals(['id' => $producerId]))
|
|
->execute();
|
|
}
|
|
}
|