Files
kupshop/bundles/KupShop/POSBundle/Util/PosEntity.php
2025-08-02 16:30:27 +02:00

325 lines
10 KiB
PHP

<?php
namespace KupShop\POSBundle\Util;
use KupShop\GraphQLBundle\ApiPos\Types\PosModule;
use KupShop\GraphQLBundle\ApiPos\Types\PosPermission;
use KupShop\KupShopBundle\Context\CountryContext;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Context\DomainContext;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Context\PriceLevelContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\PricelistBundle\Context\PricelistContext;
use Query\Operator;
#[\AllowDynamicProperties]
class PosEntity
{
/** @var int POS ID */
private int $id;
/** @var ?string POS NAME */
private ?string $name;
/** @var ?int CASH DELIVERY TYPE */
private ?int $cash_delivery_type;
/** @var ?int CARD DELIVERY TYPE */
private ?int $card_delivery_type;
/** @var ?int INVOICE DELIVERY TYPE */
private ?int $invoice_delivery_type;
/** @var ?int CUSTOM DELIVERY TYPE */
private ?int $custom_delivery_type;
/** @var ?array PARSED JSON DATA */
private ?array $data;
/** @var ?array LOCATIONS */
private ?array $warehouseLocations = [];
/** @var ?array POSITIONS */
private ?array $warehousePositions = [];
/** @var ?int POSITION (id_base_position) */
private ?int $idBasePosition = null;
/** @var ?int POSITION (id_return_table) */
private ?int $idReturnTable = null;
public function createFromDB($id): static
{
if (empty($id) || !empty($this->id)) {
return $this;
}
$row = sqlQueryBuilder()
->select('*')
->from('pos')
->where(Operator::equals(['id' => $id]))
->execute()
->fetchAssociative();
if (empty($row['id'])) {
throw new \Exception('Pos not found');
}
return $this->createFromArray($row);
}
public function createFromArray(array $data): static
{
if (empty($data['id']) || !empty($this->id)) {
return $this;
}
$this->id = $data['id'];
$this->name = $data['name'] ?? null;
$this->cash_delivery_type = $data['cash_delivery_type'] ?? null;
$this->card_delivery_type = $data['card_delivery_type'] ?? null;
$this->invoice_delivery_type = $data['invoice_delivery_type'] ?? null;
$this->custom_delivery_type = $data['custom_delivery_type'] ?? null;
$this->data = json_decode($data['data'] ?? '{}', true) ?? null;
return $this;
}
public function getCheckedModules(): array
{
$modules = [];
$modules[] = new PosModule(\Modules::BONUS_PROGRAM, findModule(\Modules::BONUS_PROGRAM));
$modules[] = new PosModule(\Modules::STORES, findModule(\Modules::STORES));
$modules[] = new PosModule(\Modules::WAREHOUSE, findModule(\Modules::WAREHOUSE));
$modules[] = new PosModule(\Modules::PRODUCTS_SERIAL_NUMBERS, findModule(\Modules::PRODUCTS_SERIAL_NUMBERS));
$modules[] = new PosModule(\Modules::SKEET, findModule(\Modules::SKEET));
$modules[] = new PosModule(\Modules::SUB_TERMINAL, findModule(\Modules::NEW_POS, \Modules::SUB_TERMINAL) ?? false);
return $modules;
}
public function getCheckedPermissions(): array
{
$permissions = [];
$permissions[] = new PosPermission('POS_APP_SETTINGS', findRight('POS_APP_SETTINGS'));
$permissions[] = new PosPermission('POS_DISCOUNT_SETTINGS', findRight('POS_DISCOUNT_SETTINGS'));
$permissions[] = new PosPermission('POS_PURCHASE_WITH_NEGATIVE_PIECES',
!findModule(\Modules::WAREHOUSE) && findRight('POS_PURCHASE_WITH_NEGATIVE_PIECES')
);
$permissions[] = new PosPermission('POS_PURCHASE_SKIP_PIECES_CHECK',
!findModule(\Modules::WAREHOUSE) && findRight('POS_PURCHASE_SKIP_PIECES_CHECK')
);
$permissions[] = new PosPermission('POS_IS_LOCAL_DEVELOPMENT', isLocalDevelopment());
return $permissions;
}
public function activateContexts(): static
{
$domainContext = Contexts::get(DomainContext::class);
if (($this->data['domain_context'] ?? false) || (key_exists('domain_context', $this->data) && $this->data['domain_context'] == '0')) {
$domainContext->activate($this->data['domain_context']);
} else {
$domainContext->clearCache();
}
$countryContext = Contexts::get(CountryContext::class);
if ($this->data['country_context'] ?? false) {
$countryContext->activate($this->data['country_context']);
} else {
$countryContext->activate($countryContext->getActiveId());
}
if (findModule(\Modules::PRICELISTS)) {
$priceListContext = Contexts::get(PricelistContext::class);
if ($this->data['pricelist_context'] ?? false) {
$priceListContext->activate($this->data['pricelist_context']);
} else {
$priceListContext->clearCache();
}
}
if (findModule(\Modules::CURRENCIES)) {
$currencyContext = Contexts::get(CurrencyContext::class);
if ($this->data['currency_context'] ?? false) {
$currencyContext->activate($this->data['currency_context']);
} else {
$currencyContext->activate($currencyContext->getDefaultId());
}
$languageContext = Contexts::get(LanguageContext::class);
if ($this->data['language_context'] ?? false) {
$languageContext->activate($this->data['language_context']);
} else {
$languageContext->activate($languageContext->getDefaultId());
}
}
if (findModule(\Modules::PRICE_LEVELS)) {
$priceLevelContext = Contexts::get(PriceLevelContext::class);
if ($this->data['pricelevel_context'] ?? false) {
$priceLevelContext->activate($this->data['pricelevel_context']);
} else {
$priceLevelContext->clearPriceLevelSession();
$priceLevelContext->clearCache();
}
}
return $this;
}
public function getCurrencyID(): ?string
{
return $this->data['currency_context'] ?? null;
}
public function getId(): ?int
{
return $this->id ?? null;
}
public function getName(): ?string
{
return $this->name;
}
/** @return ?array */
public function getCashDeliveryType(): ?int
{
return $this->cash_delivery_type;
}
/** @return ?array */
public function getCardDeliveryType(): ?int
{
return $this->card_delivery_type;
}
public function getInvoiceDeliveryType(): ?int
{
return $this->invoice_delivery_type;
}
public function getCustomDeliveryType(): ?int
{
return $this->custom_delivery_type;
}
public function getData(): ?array
{
return $this->data;
}
public function getVirtualBox(): ?string
{
return $this->data['virtual_box'] ?? null;
}
public function getStores(): ?array
{
return $this->data['stores'] ?? [];
}
public function getWarehouseLocations(): ?array
{
if (!findModule(\Modules::WAREHOUSE)) {
return [];
}
if (empty($this->warehouseLocations)) {
if (findModule(\Modules::STORES)) {
$this->warehouseLocations = json_decode(sqlQueryBuilder()
->select("JSON_EXTRACT(data, '$.warehouse_locations') as locations")
->from('stores')
->where(Operator::inIntArray($this->getStores(), 'id'))
->execute()
->fetchOne(), true) ?? [];
} else {
$locations = [];
foreach (sqlQueryBuilder()
->select('id')
->from('warehouse_locations')
->orderBy('sort_index', 'ASC')
->execute()
->fetchAllAssociative() as $location) {
$locations[] = $location['id'];
}
$this->warehouseLocations = ($locations) ?: [];
}
}
return $this->warehouseLocations;
}
public function getWarehousePositions($incomeAndReturnPositions = true): ?array
{
if (!findModule(\Modules::WAREHOUSE)) {
return [];
}
if (empty($this->warehousePositions)) {
$positions = [];
$positionsQb = sqlQueryBuilder()
->select('id')
->from('warehouse_positions')
->andWhere('id_location IN (:ids)')
->setParameter('ids', $this->getWarehouseLocations(), \Doctrine\DBAL\Connection::PARAM_INT_ARRAY)
->addOrderBy('FIELD(id_location, :ids)');
foreach ($positionsQb->execute()->fetchAllAssociative() as $position) {
$positions[] = $position['id'];
}
if (findModule(\Modules::STORES) && $incomeAndReturnPositions) {
$this->loadAdditionalWarehousePositions();
$positions = array_unique(array_merge($positions, [$this->getIdBasePosition(), $this->getIdReturnTable()]));
}
$this->warehousePositions = ($positions) ?: [];
}
return $this->warehousePositions;
}
private function loadAdditionalWarehousePositions(): void
{
if ($this->idBasePosition || $this->idReturnTable) {
return;
}
$additionalPositions = sqlQueryBuilder()
->select('id_base_position, id_return_table')
->from('stores')
->where(Operator::inStringArray($this->getStores(), 'id'))
->execute()
->fetchAssociative();
$this->idBasePosition = $additionalPositions['id_base_position'] ?? null;
$this->idReturnTable = $additionalPositions['id_return_table'] ?? null;
}
public function getTerminalData(): ?array
{
return $this->data['terminal'] ?? null;
}
public function getIdBasePosition(): ?int
{
$this->loadAdditionalWarehousePositions();
return $this->idBasePosition;
}
public function getIdReturnTable(): ?int
{
$this->loadAdditionalWarehousePositions();
return $this->idReturnTable;
}
}