253 lines
8.4 KiB
PHP
253 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace External\VarioBundle\Uploaders;
|
|
|
|
use External\VarioBundle\Exception\SynchronizerException;
|
|
use Query\Operator;
|
|
|
|
class UsersUploader extends BaseUploader
|
|
{
|
|
protected static $type = 'CreateOrUpdateCustomer';
|
|
protected string $table = 'users';
|
|
protected static array $actualizeItems = ['Identificator', 'CustomerName', 'Email', 'IC', 'DIC', 'Telephone1', 'Telephone2', 'FirstName', 'Surname', 'Addresses'];
|
|
protected static bool $insertNewUser = true;
|
|
|
|
protected function prepareData(array $data): ?object
|
|
{
|
|
if (empty($data['id_vario'])) {
|
|
$data['id_vario'] = $this->findUser($this->getIdentifier($data), $data['email']);
|
|
}
|
|
|
|
return $this->createObject($data);
|
|
}
|
|
|
|
public function findUser($identifier = null, $email = null): ?string
|
|
{
|
|
$customer = $identifier ? $this->client->getClient()->GetCustomersByCriteria($identifier, null, null, null, null, null, null, null, null, null, false) : null;
|
|
|
|
/*if (!$customer && $email) {
|
|
$customer = $this->client->getClient()->GetCustomersByCriteria(null, null, $email, null, null, null, null, null, null, null, false);
|
|
}*/
|
|
|
|
if (!empty($customer[0])) {
|
|
return $this->helper->trimVarioId($customer[0]->ID);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function createObject($data): ?\stdClass
|
|
{
|
|
if (!$customer = $this->client->getClient()->GetCustomer($data['id_vario'])) {
|
|
if (!self::$insertNewUser) {
|
|
return null;
|
|
}
|
|
$customer = $this->createEmptyObject();
|
|
} else {
|
|
$customer->Actualize = self::$actualizeItems;
|
|
}
|
|
|
|
$customerTmp = $customer;
|
|
if (empty($customer->Identificator)) {
|
|
$customer->Identificator = $this->getIdentifier($data);
|
|
}
|
|
$customer->CustomerName = empty($data['firm']) ? $data['name'].' '.$data['surname'] : $data['firm'];
|
|
$customer->Email = $data['email'];
|
|
$customer->IC = $data['ico'];
|
|
$customer->DIC = $data['dic'];
|
|
$customer->Telephone1 = $data['phone'] ?? '';
|
|
$customer->Telephone2 = $data['mobile'] ?? '';
|
|
|
|
$customer->CountryISO = $data['country'];
|
|
|
|
$customer->Addresses = [];
|
|
foreach (['atPrimary', 'atDelivery'] as $address) {
|
|
$customer->Addresses[] = $this->createAddressObject($data, $address);
|
|
}
|
|
|
|
$customer->FirstName = $data['name'];
|
|
$customer->Surname = $data['surname'];
|
|
|
|
return $customer;
|
|
}
|
|
|
|
public function createEmptyObject(): \stdClass
|
|
{
|
|
$user = new \stdClass();
|
|
|
|
$user->ID = '';
|
|
$user->Book = $this->getBook();
|
|
$user->CustomerType = $this->getCustomerType();
|
|
$user->CustomerTypeName = '';
|
|
$user->DiscountID = '';
|
|
$user->Discount = '';
|
|
$user->MainCustomerID = '';
|
|
$user->Identificator = '';
|
|
$user->CustomerName = '';
|
|
$user->Email = '';
|
|
$user->WWW = '';
|
|
$user->IC = '';
|
|
$user->DIC = '';
|
|
$user->CompanyRegister = '';
|
|
$user->VATPayer = true;
|
|
$user->CorporateBody = false;
|
|
$user->Telephone1 = '';
|
|
$user->Telephone2 = '';
|
|
$user->Fax1 = '';
|
|
$user->Fax2 = '';
|
|
$user->SalesAgent = '';
|
|
$user->InvoiceDue = 14;
|
|
$user->Interest = '';
|
|
$user->Credit = '';
|
|
$user->PriceListID = '';
|
|
$user->Delivery = '';
|
|
$user->SettlementType = '';
|
|
$user->Language = '';
|
|
$user->Note = '';
|
|
$user->Category = '';
|
|
$user->EAN = '';
|
|
$user->Supplier = false;
|
|
$user->Customer = false;
|
|
$user->Data1 = '';
|
|
$user->Data2 = '';
|
|
$user->Number1 = '';
|
|
$user->Number2 = '';
|
|
$user->CountryISO = '';
|
|
$user->SupplierVarNumber = '';
|
|
$user->CustomerVarNumber = '';
|
|
$user->State = '';
|
|
|
|
$user->Addresses[] = [];
|
|
$user->Banks = [];
|
|
|
|
$user->Title = '';
|
|
$user->FirstName = '';
|
|
$user->SecondName = '';
|
|
$user->Surname = '';
|
|
$user->TitleAfter = '';
|
|
$user->Vocative = '';
|
|
$user->SurnameVocative = '';
|
|
$user->PriceGroup = '';
|
|
$user->Position = '';
|
|
$user->Send = '';
|
|
$user->Login = '';
|
|
$user->Password = '';
|
|
$user->DateOfBirth = '';
|
|
$user->BirthNumber = '';
|
|
$user->UserFields = '';
|
|
$user->CameFrom = '';
|
|
$user->ContactObtainDatelt = '';
|
|
$user->ContactObtainDate = '';
|
|
$user->Active = true;
|
|
$user->Actualize = [];
|
|
|
|
return $user;
|
|
}
|
|
|
|
protected function getCustomerType(): string
|
|
{
|
|
return 'OS';
|
|
}
|
|
|
|
protected function getBook(): string
|
|
{
|
|
return '';
|
|
}
|
|
|
|
public function createAddressObject($data, $type = 'atPrimary', $fromOrder = false): \stdClass
|
|
{
|
|
$address = new \stdClass();
|
|
|
|
if ($type == 'atPrimary') {
|
|
$prefix = '';
|
|
if ($fromOrder) {
|
|
$prefix = 'invoice_';
|
|
}
|
|
$addressName = 'Fakturační adresa';
|
|
} elseif ($type == 'atDelivery') {
|
|
$prefix = 'delivery_';
|
|
$addressName = 'Doručovací adresa';
|
|
} else {
|
|
throw new SynchronizerException('Unknown address type!');
|
|
}
|
|
|
|
$address->ID = '';
|
|
$address->AddressType = $type;
|
|
$address->AddressName = $addressName;
|
|
$address->Street = $data[$prefix.'street'];
|
|
$address->City = $data[$prefix.'city'];
|
|
$address->ZIP = $data[$prefix.'zip'];
|
|
$address->Country = '';
|
|
$address->CountryISO = $data[$prefix.'country'];
|
|
$address->Address = $address->Street."\n".$address->ZIP.' '.$address->City."\n".$address->Country;
|
|
$address->UseOnDocuments = true;
|
|
if ($type == 'atDelivery') {
|
|
$address->UseOnDocuments = false;
|
|
}
|
|
|
|
return $address;
|
|
}
|
|
|
|
protected function getIdentifier(array $data): string
|
|
{
|
|
$identifier = "WPJ{$data['id']}";
|
|
|
|
return trim($identifier);
|
|
}
|
|
|
|
protected function afterInsertUpdateAction(string $varioId = '', $data = null, $synchronizeData = null): void
|
|
{
|
|
if (!empty($data['id'])) {
|
|
sqlQueryBuilder()->insert('vario_users')->directValues([
|
|
'id_vario' => $varioId,
|
|
'id_user' => $data['id'],
|
|
'identifier' => $synchronizeData->Identificator,
|
|
])->onDuplicateKeyUpdate(['id_vario'])->execute();
|
|
|
|
sqlQueryBuilder()
|
|
->update('users')
|
|
->set('custom_data', 'JSON_SET(COALESCE(custom_data, \'{}\'), \'$.lastSyncToVario\', \''.date('Y-m-d H:i:s').'\')')
|
|
->where(Operator::equals(['id' => $data['id']]))
|
|
->execute();
|
|
}
|
|
}
|
|
|
|
public function createUnregistredUserFromOrder(\Order $order): string
|
|
{
|
|
$identifier = $this->getIdentifierUnregistredUser($order);
|
|
if ($customer = $this->client->getClient()->GetCustomersByCriteria($identifier, null, null, null, null, null, null, null, null, null, false)) {
|
|
// Zkontrolujeme ještě radši uživatele podle identifkatoru v případě, že se nepovedlo založit obj., ale uživatel se založil
|
|
if (!empty($customer[0])) {
|
|
return $this->helper->trimVarioId($customer[0]->ID);
|
|
}
|
|
}
|
|
|
|
$customer = $this->createEmptyObject();
|
|
$customer->Identificator = $identifier;
|
|
$customer->CustomerName = empty($order['invoice_firm']) ? $order['invoice_name'].' '.$order['invoice_surname'] : $order['invoice_firm'];
|
|
$customer->Email = $order['invoice_email'];
|
|
$customer->IC = $order['invoice_ico'];
|
|
$customer->DIC = $order['invoice_dic'];
|
|
$customer->Telephone1 = $order['invoice_phone'] ?? '';
|
|
|
|
$customer->CountryISO = $order['invoice_country'];
|
|
|
|
$customer->FirstName = $order['invoice_name'];
|
|
$customer->Surname = $order['invoice_surname'];
|
|
|
|
$customer->Addresses = [];
|
|
foreach (['atPrimary', 'atDelivery'] as $address) {
|
|
$customer->Addresses[] = $this->createAddressObject($order, $address, true);
|
|
}
|
|
|
|
return $this->prepareVarioID($this->synchronizeData($customer)) ?: '';
|
|
}
|
|
|
|
protected function getIdentifierUnregistredUser(\Order $order): string
|
|
{
|
|
$identifier = "WPJ_obj: {$order['invoice_email']}";
|
|
|
|
return trim($identifier);
|
|
}
|
|
}
|