Skip to content

Commit

Permalink
[shopsys] administrator now can see list of last ten orders on custom…
Browse files Browse the repository at this point in the history
…er edit page (#3365)
  • Loading branch information
TomasLudvik committed Aug 28, 2024
1 parent b686cf0 commit 5b56f33
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 60 deletions.
2 changes: 1 addition & 1 deletion assets/js/admin/components/AdvancedSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class AdvancedSearch {
}

static updateValueByOperator ($rulesContainer, $rule, operator) {
$rule.find('.js-advanced-search-rule-value').toggle(operator !== 'notSet');
$rule.find('.js-advanced-search-rule-value').toggle(operator !== 'notSet' && operator !== 'notRegistered');
}

static init ($container) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct()
AdvancedSearchFilterInterface::OPERATOR_CONTAINS => t('include'),
AdvancedSearchFilterInterface::OPERATOR_NOT_CONTAINS => t('doesn\'t include'),
AdvancedSearchFilterInterface::OPERATOR_NOT_SET => t('not entered'),
AdvancedSearchFilterInterface::OPERATOR_NOT_REGISTERED => t('not registered customer'),
AdvancedSearchFilterInterface::OPERATOR_IS => t('is'),
AdvancedSearchFilterInterface::OPERATOR_IS_NOT => t('not'),
AdvancedSearchFilterInterface::OPERATOR_BEFORE => t('before'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCityFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCreateDateFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCustomerIdFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderEmailFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderLastNameFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderNameFilter;
Expand Down Expand Up @@ -33,5 +34,6 @@ public function __construct()
$this->addFilterTranslation(OrderLastNameFilter::NAME, t('Customer last name'));
$this->addFilterTranslation(OrderEmailFilter::NAME, t('Customer email address'));
$this->addFilterTranslation(OrderCityFilter::NAME, t('Customer city'));
$this->addFilterTranslation(OrderCustomerIdFilter::NAME, t('Customer ID'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Shopsys\FrameworkBundle\Form\Constraints\UniqueBillingAddress;
use Shopsys\FrameworkBundle\Form\CustomerUserListType;
use Shopsys\FrameworkBundle\Form\DeliveryAddressListType;
use Shopsys\FrameworkBundle\Form\OrderListType;
use Shopsys\FrameworkBundle\Model\Customer\Customer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
Expand Down Expand Up @@ -36,6 +37,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'allowAdd' => true,
'deleteConfirmMessage' => t('Do you really want to remove this delivery address?'),
])
->add('orders', OrderListType::class, [
'customer' => $options['customer'],
])
->add('save', SubmitType::class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'customerUser' => $options['customerUser'],
]);
$builder->add('orders', OrderListType::class, [
'customerUser' => $options['customerUser'],
'customer' => $options['customerUser']->getCustomer(),
]);
}

Expand Down
24 changes: 16 additions & 8 deletions src/Form/OrderListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Shopsys\FrameworkBundle\Form;

use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser;
use Shopsys\FrameworkBundle\Model\Customer\Customer;
use Shopsys\FrameworkBundle\Model\Localization\Localization;
use Shopsys\FrameworkBundle\Model\Order\OrderFacade;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
Expand All @@ -15,20 +16,25 @@ class OrderListType extends AbstractType
{
/**
* @param \Shopsys\FrameworkBundle\Model\Order\OrderFacade $orderFacade
* @param \Shopsys\FrameworkBundle\Model\Localization\Localization $localization
*/
public function __construct(private readonly OrderFacade $orderFacade)
{
public function __construct(
private readonly OrderFacade $orderFacade,
private readonly Localization $localization,
) {
}

/**
* @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('customerUser')
->setAllowedTypes('customerUser', CustomerUser::class)
$resolver->setRequired(['customer', 'limit'])
->setAllowedTypes('customer', Customer::class)
->setAllowedTypes('limit', 'int')
->setDefaults([
'mapped' => false,
'limit' => 10,
]);
}

Expand All @@ -37,10 +43,12 @@ public function configureOptions(OptionsResolver $resolver)
* @param \Symfony\Component\Form\FormInterface $form
* @param array $options
*/
public function buildView(FormView $view, FormInterface $form, array $options)
public function buildView(FormView $view, FormInterface $form, array $options): void
{
parent::buildView($view, $form, $options);

$view->vars['orders'] = $this->orderFacade->getCustomerUserOrderList($options['customerUser']);
$view->vars['orders'] = $this->orderFacade->getLastCustomerOrdersByLimit($options['customer'], $options['limit'], $this->localization->getAdminLocale());
$view->vars['customer'] = $options['customer'];
$view->vars['limit'] = $options['limit'];
}
}
1 change: 1 addition & 0 deletions src/Model/AdvancedSearch/AdvancedSearchFilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface AdvancedSearchFilterInterface
public const OPERATOR_CONTAINS = 'contains';
public const OPERATOR_NOT_CONTAINS = 'notContains';
public const OPERATOR_NOT_SET = 'notSet';
public const OPERATOR_NOT_REGISTERED = 'notRegistered';
public const OPERATOR_IS = 'is';
public const OPERATOR_IS_NOT = 'isNot';
public const OPERATOR_BEFORE = 'before';
Expand Down
4 changes: 4 additions & 0 deletions src/Model/AdvancedSearch/OrderAdvancedSearchConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCityFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCreateDateFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCustomerIdFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderEmailFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderLastNameFilter;
use Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderNameFilter;
Expand All @@ -30,6 +31,7 @@ class OrderAdvancedSearchConfig extends AdvancedSearchConfig
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderLastNameFilter $orderLastNameFilter
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderEmailFilter $orderEmailFilter
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCityFilter $orderCityFilter
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter\OrderCustomerIdFilter $orderCustomerIdFilter
* @throws \Shopsys\FrameworkBundle\Model\AdvancedSearch\Exception\AdvancedSearchFilterAlreadyExistsException
*/
public function __construct(
Expand All @@ -44,6 +46,7 @@ public function __construct(
OrderLastNameFilter $orderLastNameFilter,
OrderEmailFilter $orderEmailFilter,
OrderCityFilter $orderCityFilter,
OrderCustomerIdFilter $orderCustomerIdFilter,
) {
parent::__construct();

Expand All @@ -58,5 +61,6 @@ public function __construct(
$this->registerFilter($orderPhoneNumberFilter);
$this->registerFilter($orderStreetFilter);
$this->registerFilter($orderCityFilter);
$this->registerFilter($orderCustomerIdFilter);
}
}
100 changes: 100 additions & 0 deletions src/Model/AdvancedSearchOrder/Filter/OrderCustomerIdFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Model\AdvancedSearchOrder\Filter;

use Doctrine\ORM\QueryBuilder;
use Shopsys\FrameworkBundle\Component\FlashMessage\FlashMessage;
use Shopsys\FrameworkBundle\Model\AdvancedSearch\AdvancedSearchFilterInterface;
use Shopsys\FrameworkBundle\Model\Customer\CustomerFacade;
use Shopsys\FrameworkBundle\Model\Customer\Exception\CustomerNotFoundException;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;

class OrderCustomerIdFilter implements AdvancedSearchFilterInterface
{
public const string NAME = 'customerId';

/**
* @param \Shopsys\FrameworkBundle\Model\Customer\CustomerFacade $customerFacade
* @param \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface $flashBag
*/
public function __construct(
protected readonly CustomerFacade $customerFacade,
protected readonly FlashBagInterface $flashBag,
) {
}

/**
* @return string
*/
public function getName(): string
{
return self::NAME;
}

/**
* @return string[]
*/
public function getAllowedOperators(): array
{
return [
self::OPERATOR_IS,
self::OPERATOR_NOT_REGISTERED,
];
}

/**
* @return string
*/
public function getValueFormType(): string
{
return NumberType::class;
}

/**
* @return array
*/
public function getValueFormOptions(): array
{
return [];
}

/**
* @param \Doctrine\ORM\QueryBuilder $queryBuilder
* @param \Shopsys\FrameworkBundle\Model\AdvancedSearch\AdvancedSearchRuleData[] $rulesData
*/
public function extendQueryBuilder(QueryBuilder $queryBuilder, $rulesData): void
{
$customerIds = [];

foreach ($rulesData as $index => $ruleData) {
if ($ruleData->operator === self::OPERATOR_NOT_REGISTERED) {
$queryBuilder->andWhere('o.customer IS NULL');

continue;
}

try {
$customer = $this->customerFacade->getById((int)$ruleData->value);
$customerIds[] = $customer->getId();
} catch (CustomerNotFoundException) {
$this->flashBag->add(
FlashMessage::KEY_ERROR,
t(
'Customer with ID %customerId% not found.',
['%customerId%' => $ruleData->value],
),
);
}
}

if (count($customerIds) === 0) {
return;
}

$queryBuilder->andWhere('o.customer IN(:customer_id_filter)');
$queryBuilder->setParameter('customer_id_filter', $customerIds);
}
}
12 changes: 12 additions & 0 deletions src/Model/Order/OrderFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Shopsys\FrameworkBundle\Form\Admin\QuickSearch\QuickSearchFormData;
use Shopsys\FrameworkBundle\Model\Administrator\Security\AdministratorFrontSecurityFacade;
use Shopsys\FrameworkBundle\Model\Cart\CartFacade;
use Shopsys\FrameworkBundle\Model\Customer\Customer;
use Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser;
use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser;
use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUserFacade;
Expand Down Expand Up @@ -191,6 +192,17 @@ public function getCustomerUserOrderList(CustomerUser $customerUser): array
return $this->orderRepository->getCustomerUserOrderList($customerUser);
}

/**
* @param \Shopsys\FrameworkBundle\Model\Customer\Customer $customer
* @param int $limit
* @param string $locale
* @return \Shopsys\FrameworkBundle\Model\Order\Order[]
*/
public function getLastCustomerOrdersByLimit(Customer $customer, int $limit, string $locale): array
{
return $this->orderRepository->getLastCustomerOrdersByLimit($customer, $limit, $locale);
}

/**
* @param string $email
* @param int $domainId
Expand Down
22 changes: 22 additions & 0 deletions src/Model/Order/OrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\ORM\QueryBuilder;
use Shopsys\FrameworkBundle\Component\String\DatabaseSearching;
use Shopsys\FrameworkBundle\Form\Admin\QuickSearch\QuickSearchFormData;
use Shopsys\FrameworkBundle\Model\Customer\Customer;
use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser;
use Shopsys\FrameworkBundle\Model\Order\Exception\OrderNotFoundException;
use Shopsys\FrameworkBundle\Model\Order\Listing\OrderListAdminRepository;
Expand Down Expand Up @@ -182,6 +183,27 @@ public function getCustomerUserOrderList(CustomerUser $customerUser)
->getQuery()->execute();
}

/**
* @param \Shopsys\FrameworkBundle\Model\Customer\Customer $customer
* @param int $limit
* @param string $locale
* @return \Shopsys\FrameworkBundle\Model\Order\Order[]
*/
public function getLastCustomerOrdersByLimit(Customer $customer, int $limit, string $locale): array
{
return $this->createOrderQueryBuilder()
->select('o, os, ost, c')
->join('o.status', 'os')
->join('os.translations', 'ost', Join::WITH, 'ost.locale = :locale')
->join('o.currency', 'c')
->andWhere('o.customer = :customer')
->orderBy('o.createdAt', 'DESC')
->setParameter('customer', $customer)
->setParameter('locale', $locale)
->setMaxResults($limit)
->getQuery()->execute();
}

/**
* @param string $email
* @param int $domainId
Expand Down
15 changes: 12 additions & 3 deletions src/Resources/translations/messages.cs.po
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,9 @@ msgstr "Byl upraven zákazník <strong><a href=\"{{ url }}\">{{ name }}</a></str
msgid "Customer <strong>{{ name }}</strong> deleted"
msgstr "Zákazník <strong>{{ name }}</strong> byl smazán"

msgid "Customer ID"
msgstr "ID zákazníka"

msgid "Customer activation"
msgstr "Aktivace zákazníka"

Expand Down Expand Up @@ -904,9 +907,6 @@ msgstr "Příjmení zákazníka"
msgid "Customer name"
msgstr "Jméno zákazníka"

msgid "Customer orders"
msgstr "Objednávky zákazníka"

msgid "Customer phone number"
msgstr "Telefonní číslo zákazníka"

Expand Down Expand Up @@ -934,6 +934,9 @@ msgstr "Skupina práv"
msgid "Customer users"
msgstr "Uživatelé"

msgid "Customer with ID %customerId% not found."
msgstr "Zákazník s ID %customerId% nebyl nalezen."

msgid "Customers"
msgstr "Zákazníci"

Expand Down Expand Up @@ -1933,6 +1936,9 @@ msgstr "Jazykové konstanty - vše"
msgid "Language constants - view"
msgstr "Jazykové konstanty - zobrazení"

msgid "Last %limit% customer orders"
msgstr "Posledních %limit% objednávek zákazníka"

msgid "Last action time"
msgstr "Čas poslední akce"

Expand Down Expand Up @@ -3943,6 +3949,9 @@ msgstr "není"
msgid "not entered"
msgstr "není zadáno"

msgid "not registered customer"
msgstr "neregistrovaný zákazník"

msgid "numbers"
msgstr "čísla"

Expand Down
Loading

0 comments on commit 5b56f33

Please sign in to comment.