<?php
namespace App\Controller\EasyAdmin\Ozon;
use App\Entity\OzonClient;
use App\Service\AdminUser\Type\AdminUserRoleType;
use App\Service\Ozon\Type\OzonWarhouseAdditionalSettings;
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class OzonClientCrudController extends AbstractCrudController
{
private UrlGeneratorInterface $urlGenerator;
public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
public static function getEntityFqcn(): string
{
return OzonClient::class;
}
/**
* @param SearchDto $searchDto
* @param EntityDto $entityDto
* @param FieldCollection $fields
* @param FilterCollection $filters
* @return QueryBuilder
*/
public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
{
$response = $this->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters);
if(!in_array(AdminUserRoleType::ROLE_ADMIN, $this->getUser()->getRoles())) {
$response
->andWhere('entity.id IN (:clients)')
->setParameter('clients', $this->getUser()->getOzonClients());
}
return $response
->orderBy('entity.id', 'DESC')
;
}
public function configureCrud(Crud $crud): Crud
{
return $crud
->setSearchFields([
'id', 'name', 'url', 'storeUrl', 'apiClientId', 'apiKey', 'storeFbs', 'storeFbo', 'orderType',
'orderPriceDeflection', 'minStock', 'legalName'
])
->setDefaultSort(['id' => 'DESC'])
->setPaginatorPageSize(50)
->setEntityLabelInSingular('Торговая площадка')
->setEntityLabelInPlural('Торговые площадки')
->setEntityPermission(AdminUserRoleType::ROLE_OZON_CLIENT_VIEW);
}
public function configureActions(Actions $actions): Actions
{
$getHidePriceProducts = Action::new('getLastPostings', 'Получить заказы за 24ч')
->linkToUrl(function (OzonClient $client): string {
return $this->urlGenerator->generate(
'ozon_posting_client_load',
['id' => $client->getId()],
UrlGeneratorInterface::ABSOLUTE_PATH
);
});
$actions
->add(Crud::PAGE_INDEX, $getHidePriceProducts);
if(!in_array(AdminUserRoleType::ROLE_OZON_CLIENT_EDIT, $this->getUser()->getRoles())) {
$actions->disable(Crud::PAGE_NEW, Crud::PAGE_EDIT);
}
return $actions;
}
public function configureFields(string $pageName): iterable
{
$name = TextField::new('name', 'Название');
$legalName = TextField::new('legalName', 'Юр. лицо');
$storeUrl = TextField::new('storeUrl', 'Url\\Адрес магазина');
$url = TextField::new('url', 'URL');
$apiClientId = TextField::new('apiClientId', 'Client ID');
$apiKey = TextField::new('apiKey', 'API Key');
$retailCrmId = TextField::new('retailCrmId', 'API Key RetailCRM');
$site = TextField::new('retailCrmSite', 'Site RetailCRM');
$omsStoreId = TextField::new('omsStoreId', 'oms store');
$omsApiKey = TextField::new('omsApiKey', 'oms api key');
$omsPassword = TextField::new('omsPassword', 'oms password');
// $storeFbsObject = Field::new('storeFbsObject', 'Склад FBS');
// $storeFboObject = Field::new('storeFboObject', 'Склад FBO');
// $orderTypeObject = Field::new('orderTypeObject', 'Тип заказа');
$isEnabled = Field::new('isEnabled', 'Активно');
$isEnabledOrders = Field::new('isEnabledOrders', 'Заказы в 1C');
$isEnabledStatuses = Field::new('isEnabledStatuses', 'Статусы в 1C');
$startOrdersTm = DateTimeField::new('startOrdersTm', 'Передавать заказы начиная с');
$orderPriceDeflection = IntegerField::new('orderPriceDeflection', 'Отклонение стоимости заказа, %');
$isEnabledStocks = Field::new('isEnabledStocks', 'Обновление остатков');
$isEnabledPrice = Field::new('isEnabledPrice', 'Обновление цен');
$useNightPrice = Field::new('useNightPrice', 'Ночные цены');
$currencyCode = IntegerField::new('currencyCode', 'Валюта магазина');
$id = IntegerField::new('id', 'ID');
$storeFbs = TextField::new('storeFbs', 'Склад FBS');
$storeFbo = TextField::new('storeFbo', 'Склад FBO');
$orderType = TextField::new('orderType', 'Тип заказа');
$currency = TextareaField::new('currency', 'Валюта');
$targets = ArrayField::new('targets', 'Базы 1C');
$company = ChoiceField::new('legalName', 'Юр. лицо')
->setChoices([
'ООО “Гипер”' => 'ООО “Гипер”',
'ООО “Фирменные Решения”' => 'ООО “Фирменные Решения”',
'ООО “Официальный магазин”' => 'ООО “Официальный магазин”',
'ИП Кунгуров' => 'ИП Кунгуров',
])
->allowMultipleChoices(false)
->renderExpanded(false);
$thankyouMsg = TextareaField::new('thankyouMessage', 'Текст сообщения после вручения заказа');
$thankyouMsgIsSend = Field::new('isThankyouMsgSend', 'Отправлять сообщение после вручения заказа');
switch ($pageName) {
case Crud::PAGE_INDEX:
$fields = [
$id, $name, $legalName, $url, $apiClientId, $apiKey, $storeFbs, $storeFbo, $orderType, $currency,
$targets, $isEnabled, $isEnabledOrders, $isEnabledStatuses, $isEnabledPrice, $isEnabledStocks, $company, $omsStoreId
];
break;
case Crud::PAGE_DETAIL:
$fields = [
$id, $name, $legalName, $url, $apiClientId, $apiKey, $retailCrmId, $site, $storeFbs, $storeFbo, $orderType, $currency,
$targets, $isEnabled, $isEnabledOrders, $isEnabledStatuses, $isEnabledPrice, $isEnabledStocks, $company,
$thankyouMsgIsSend, $thankyouMsg, $omsStoreId, $omsApiKey, $omsPassword
];
break;
case Crud::PAGE_NEW:
case Crud::PAGE_EDIT:
$fields = [
$name, $legalName, $storeUrl, $url, $apiClientId, $apiKey, $retailCrmId, $site, $storeFbs, $storeFbo, $orderType,
$isEnabled, $isEnabledOrders, $isEnabledStatuses, $startOrdersTm, $orderPriceDeflection,
$isEnabledStocks, $isEnabledPrice, $currencyCode, $company,
$thankyouMsgIsSend, $thankyouMsg, $omsStoreId, $omsApiKey, $omsPassword
];
break;
default:
$fields = [];
}
return $fields;
}
}