Skip to content

Commit

Permalink
[shopsys] promo code for free transport and payment (#3625)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitek-rostislav authored Dec 17, 2024
2 parents 0e9e75a + fc1c2cd commit 537faab
Show file tree
Hide file tree
Showing 37 changed files with 522 additions and 120 deletions.
23 changes: 23 additions & 0 deletions assets/js/admin/components/PromoCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Register from '../../common/utils/Register';

export default class PromoCode {

constructor () {
this.$discountTypeInputGroup = $('.js-promo-code-discount-type');
this.$discountTypeInputGroup.on('change', this.handleDiscountTypeChange.bind(this));
this.handleDiscountTypeChange();
}

handleDiscountTypeChange () {
this.$discountTypeInputGroup.find('input[type=radio]:checked').each(function () {
$('.js-promo-code-limits-group').toggle($(this).val() !== 'free_transport_payment');
});
}

static init () {
// eslint-disable-next-line no-new
new PromoCode();
}
}

(new Register()).registerCallback(PromoCode.init, 'PromoCode.init');
1 change: 1 addition & 0 deletions assets/js/admin/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import './productsPicker';
import './ProductsPickerWindow';
import './MultiplePicker';
import './PickerWindow';
import './PromoCode';
import './PromoCodeFlags';
import './PromoCodeGroup';
import './PromoCodeLimits';
Expand Down
10 changes: 10 additions & 0 deletions src/Form/Admin/Order/OrderFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ private function createBasicInformationGroup(FormBuilderInterface $builder, Orde
],
]);

$promoCode = $order->getPromoCode();

if ($promoCode !== null) {
$builderBasicInformationGroup
->add('promoCode', DisplayOnlyType::class, [
'label' => t('Promo code'),
'data' => $promoCode,
]);
}

return $builderBasicInformationGroup;
}

Expand Down
22 changes: 16 additions & 6 deletions src/Form/Admin/PromoCode/PromoCodeFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Shopsys\FrameworkBundle\Model\Order\PromoCode\PromoCode;
use Shopsys\FrameworkBundle\Model\Order\PromoCode\PromoCodeData;
use Shopsys\FrameworkBundle\Model\Order\PromoCode\PromoCodeFacade;
use Shopsys\FrameworkBundle\Model\Order\PromoCode\PromoCodeTypeEnum;
use Shopsys\FrameworkBundle\Model\Pricing\Group\PricingGroupFacade;
use Shopsys\FrameworkBundle\Model\Product\Brand\BrandFacade;
use Symfony\Component\Form\AbstractType;
Expand Down Expand Up @@ -46,12 +47,14 @@ class PromoCodeFormType extends AbstractType
* @param \Shopsys\FrameworkBundle\Component\Domain\AdminDomainTabsFacade $adminDomainTabsFacade
* @param \Shopsys\FrameworkBundle\Model\Pricing\Group\PricingGroupFacade $pricingGroupFacade
* @param \Shopsys\FrameworkBundle\Model\Product\Brand\BrandFacade $brandFacade
* @param \Shopsys\FrameworkBundle\Model\Order\PromoCode\PromoCodeTypeEnum $promoCodeTypeEnum
*/
public function __construct(
private readonly PromoCodeFacade $promoCodeFacade,
private readonly AdminDomainTabsFacade $adminDomainTabsFacade,
private readonly PricingGroupFacade $pricingGroupFacade,
private readonly BrandFacade $brandFacade,
private readonly PromoCodeTypeEnum $promoCodeTypeEnum,
) {
}

Expand Down Expand Up @@ -109,11 +112,11 @@ private function buildBaseGroup(FormBuilderInterface $builder): void
->add('discountType', ChoiceType::class, [
'expanded' => true,
'multiple' => false,
'choices' => [
t('Percents') => PromoCode::DISCOUNT_TYPE_PERCENT,
t('Nominal') => PromoCode::DISCOUNT_TYPE_NOMINAL,
],
'choices' => $this->promoCodeTypeEnum->getAllIndexedByTranslations(),
'label' => t('Discount type'),
'attr' => [
'class' => 'js-promo-code-discount-type',
],
])
->add('remainingUses', IntegerType::class, [
'label' => t('Remaining number of uses'),
Expand All @@ -139,10 +142,12 @@ private function buildLimitsFormGroup(FormBuilderInterface $builder): void
'constraints' => [
new Constraints\NotBlank([
'message' => 'Please enter discount percentage',
'groups' => [self::VALIDATION_GROUP_TYPE_PERCENT, self::VALIDATION_GROUP_TYPE_NOMINAL],
]),
new Constraints\Range([
'min' => 0,
'max' => 100,
'groups' => [self::VALIDATION_GROUP_TYPE_PERCENT, self::VALIDATION_GROUP_TYPE_NOMINAL],
]),
],
'invalid_message' => 'Please enter whole number.',
Expand All @@ -151,6 +156,10 @@ private function buildLimitsFormGroup(FormBuilderInterface $builder): void

$limitsGroup = $builder->create('limitsGroup', GroupType::class, [
'label' => t('Apply according to the total price of the order'),
'js_container' => [
'container_class' => 'js-promo-code-limits-group',
'data_type' => null,
],
]);

$limitsGroup->add(
Expand All @@ -166,6 +175,7 @@ private function buildLimitsFormGroup(FormBuilderInterface $builder): void
new Constraints\Count([
'min' => 1,
'minMessage' => 'Please enter at least one discount limit',
'groups' => [self::VALIDATION_GROUP_TYPE_PERCENT, self::VALIDATION_GROUP_TYPE_NOMINAL],
]),
],
]),
Expand Down Expand Up @@ -352,9 +362,9 @@ public function configureOptions(OptionsResolver $resolver): void
/** @var \Shopsys\FrameworkBundle\Model\Order\PromoCode\PromoCodeData $promoCodeData */
$promoCodeData = $form->getData();

if ($promoCodeData->discountType === PromoCode::DISCOUNT_TYPE_NOMINAL) {
if ($promoCodeData->discountType === PromoCodeTypeEnum::DISCOUNT_TYPE_NOMINAL) {
$validationGroups[] = self::VALIDATION_GROUP_TYPE_NOMINAL;
} else {
} elseif ($promoCodeData->discountType === PromoCodeTypeEnum::DISCOUNT_TYPE_PERCENT) {
$validationGroups[] = self::VALIDATION_GROUP_TYPE_PERCENT;
}

Expand Down
1 change: 1 addition & 0 deletions src/Form/Admin/PromoCode/PromoCodeLimitType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'constraints' => [
new Constraints\NotBlank([
'message' => 'Please enter limit from',
'groups' => [PromoCodeFormType::VALIDATION_GROUP_TYPE_PERCENT, PromoCodeFormType::VALIDATION_GROUP_TYPE_NOMINAL],
]),
],
]);
Expand Down
28 changes: 28 additions & 0 deletions src/Migrations/Version20241126152128.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Shopsys\MigrationBundle\Component\Doctrine\Migrations\AbstractMigration;

class Version20241126152128 extends AbstractMigration
{
/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function up(Schema $schema): void
{
$this->sql('ALTER TABLE promo_codes ALTER discount_type TYPE VARCHAR(25)');
$this->sql('UPDATE promo_codes SET discount_type = \'percent\' WHERE discount_type = \'1\'');
$this->sql('UPDATE promo_codes SET discount_type = \'nominal\' WHERE discount_type = \'2\'');
}

/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function down(Schema $schema): void
{
}
}
27 changes: 27 additions & 0 deletions src/Migrations/Version20241128111224.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Shopsys\MigrationBundle\Component\Doctrine\Migrations\AbstractMigration;

class Version20241128111224 extends AbstractMigration
{
/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function up(Schema $schema): void
{
$this->sql('ALTER TABLE orders ADD free_transport_and_payment_applied BOOLEAN NOT NULL DEFAULT FALSE');
$this->sql('ALTER TABLE orders ALTER free_transport_and_payment_applied DROP DEFAULT');
}

/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function down(Schema $schema): void
{
}
}
15 changes: 15 additions & 0 deletions src/Model/Order/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ class Order
*/
protected $customer;

/**
* @var bool|null
* @ORM\Column(type="boolean")
*/
protected $freeTransportAndPaymentApplied;

/**
* @param \Shopsys\FrameworkBundle\Model\Order\OrderData $orderData
* @param string $orderNumber
Expand Down Expand Up @@ -501,6 +507,14 @@ public function getPaymentTransactions()
return $this->paymentTransactions->getValues();
}

/**
* @return bool
*/
public function isFreeTransportAndPaymentApplied()
{
return $this->freeTransportAndPaymentApplied;
}

/**
* @param \Shopsys\FrameworkBundle\Model\Order\OrderData $orderData
*/
Expand Down Expand Up @@ -544,6 +558,7 @@ protected function fillCommonFields(OrderData $orderData): void
$this->setDeliveryAddress($orderData);

$this->promoCode = $orderData->promoCode;
$this->freeTransportAndPaymentApplied = $orderData->freeTransportAndPaymentApplied;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Model/Order/OrderData.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,18 @@ class OrderData
*/
public $password;

/**
* @var bool
*/
public $freeTransportAndPaymentApplied;

public function __construct()
{
$this->deliveryAddressSameAsBillingAddress = false;
$this->paymentTransactionRefunds = [];
$this->heurekaAgreement = false;
$this->isCompanyCustomer = false;
$this->freeTransportAndPaymentApplied = false;

$this->totalPrice = new Price(Money::zero(), Money::zero());
}
Expand Down
1 change: 1 addition & 0 deletions src/Model/Order/OrderDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ protected function fillFromOrder(OrderData $orderData, Order $order): void
$orderData->heurekaAgreement = $order->isHeurekaAgreement();
$orderData->trackingNumber = $order->getTrackingNumber();
$orderData->promoCode = $order->getPromoCode();
$orderData->freeTransportAndPaymentApplied = $order->isFreeTransportAndPaymentApplied();
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Model/Order/OrderFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ public function changeOrderPayment(Order $order, Payment $payment): void
$order->getCurrency(),
$order->getTotalProductsPrice(),
$order->getDomainId(),
$order->isFreeTransportAndPaymentApplied(),
);

$orderPaymentData = $this->orderItemDataFactory->create(OrderItemTypeEnum::TYPE_PAYMENT);
Expand Down
11 changes: 11 additions & 0 deletions src/Model/Order/PlaceOrderFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Shopsys\FrameworkBundle\Model\Order\Item\OrderItemTypeEnum;
use Shopsys\FrameworkBundle\Model\Order\Mail\OrderMailFacade;
use Shopsys\FrameworkBundle\Model\Order\Messenger\PlacedOrderMessageDispatcher;
use Shopsys\FrameworkBundle\Model\Order\PromoCode\PromoCodeFacade;
use Shopsys\FrameworkBundle\Model\Order\Status\OrderStatusRepository;

class PlaceOrderFacade
Expand All @@ -28,6 +29,7 @@ class PlaceOrderFacade
* @param \Shopsys\FrameworkBundle\Model\Order\Messenger\PlacedOrderMessageDispatcher $placedOrderMessageDispatcher
* @param \Shopsys\FrameworkBundle\Model\Newsletter\NewsletterFacade $newsletterFacade
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUserFacade $customerUserFacade
* @param \Shopsys\FrameworkBundle\Model\Order\PromoCode\PromoCodeFacade $promoCodeFacade
*/
public function __construct(
protected readonly OrderStatusRepository $orderStatusRepository,
Expand All @@ -40,6 +42,7 @@ public function __construct(
protected readonly PlacedOrderMessageDispatcher $placedOrderMessageDispatcher,
protected readonly NewsletterFacade $newsletterFacade,
protected readonly CustomerUserFacade $customerUserFacade,
protected readonly PromoCodeFacade $promoCodeFacade,
) {
}

Expand All @@ -56,6 +59,14 @@ public function placeOrder(
$discount->promoCode->decreaseRemainingUses();
}

if ($orderData->freeTransportAndPaymentApplied && $orderData->promoCode !== null) {
$promoCode = $this->promoCodeFacade->findPromoCodeByCodeAndDomain($orderData->promoCode, $orderData->domainId);

if ($promoCode !== null && $promoCode->isFreeTransportAndPaymentType()) {
$promoCode->decreaseRemainingUses();
}
}

$order = $this->createOrderOnly($orderData);

$customerUser = $order->getCustomerUser();
Expand Down
14 changes: 14 additions & 0 deletions src/Model/Order/Processing/OrderInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,18 @@ public function getDomainConfig(): DomainConfig
{
return $this->domainConfig;
}

/**
* @return bool
*/
public function isFreeTransportAndPaymentPromoCodeApplied(): bool
{
foreach ($this->getPromoCodes() as $appliedPromoCode) {
if ($appliedPromoCode->isFreeTransportAndPaymentType()) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ abstract protected function createAndAddOrderItemData(
): void;

/**
* @return int[]
* @return string[]
*/
abstract protected function getSupportedTypes(): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function handle(
$currency,
$orderProcessingData->orderData->getProductsTotalPriceAfterAppliedDiscounts(),
$domainId,
$orderProcessingData->orderData->freeTransportAndPaymentApplied,
);

$orderItemData = $this->createPaymentItemData($paymentPrice, $payment, $orderProcessingData->getDomainConfig());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function handle(
$orderProcessingData->orderData->getProductsTotalPriceAfterAppliedDiscounts(),
$domainId,
$cartTotalWeight,
$orderProcessingData->orderData->freeTransportAndPaymentApplied,
);
} catch (TransportPriceNotFoundException) {
return $orderProcessingStack->processNext($orderProcessingData);
Expand Down
Loading

0 comments on commit 537faab

Please sign in to comment.