Skip to content

Commit

Permalink
chore(CQRS): add new create free shipping discount command
Browse files Browse the repository at this point in the history
  • Loading branch information
tleon committed Jan 27, 2025
1 parent fe115bd commit 86ebce9
Show file tree
Hide file tree
Showing 24 changed files with 1,368 additions and 34 deletions.
6 changes: 4 additions & 2 deletions classes/CartRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class CartRuleCore extends ObjectModel
public $active = true;
public $date_add;
public $date_upd;
public string $type = '';

protected static $cartAmountCache = [];

Expand Down Expand Up @@ -148,6 +149,7 @@ class CartRuleCore extends ObjectModel
'active' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'date_add' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
'date_upd' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
'type' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
/* Lang fields */
'name' => [
'type' => self::TYPE_HTML,
Expand Down Expand Up @@ -511,8 +513,8 @@ public static function getCustomerCartRules(
* in the cart rule. So he will be able to use it.
*/
$validAddressExists = Db::getInstance()->getValue('
SELECT crc.id_cart_rule
FROM ' . _DB_PREFIX_ . 'cart_rule_country crc
SELECT crc.id_cart_rule
FROM ' . _DB_PREFIX_ . 'cart_rule_country crc
INNER JOIN ' . _DB_PREFIX_ . 'address a
ON a.id_customer = ' . (int) $id_customer . ' AND
a.deleted = 0 AND
Expand Down
4 changes: 3 additions & 1 deletion install-dev/data/db_structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ CREATE TABLE `PREFIX_cart_rule` (
`active` tinyint(1) unsigned NOT NULL DEFAULT '0',
`date_add` datetime NOT NULL,
`date_upd` datetime NOT NULL,
`type` varchar(128) DEFAULT NULL,
PRIMARY KEY (`id_cart_rule`),
KEY `id_customer` (
`id_customer`, `active`, `date_to`
Expand All @@ -213,7 +214,8 @@ CREATE TABLE `PREFIX_cart_rule` (
`date_to`
),
KEY `date_from` (`date_from`),
KEY `date_to` (`date_to`)
KEY `date_to` (`date_to`),
KEY `type` (`type`)
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8mb4 COLLATION;

/* Localized name assocatied with a promo code */
Expand Down
60 changes: 60 additions & 0 deletions src/Adapter/CartRule/CartRuleBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Adapter\CartRule;

use CartRule;
use DateTimeImmutable;
use PrestaShop\PrestaShop\Core\Domain\Discount\Command\AddDiscountCommand;
use PrestaShop\PrestaShop\Core\Domain\Discount\Command\AddFreeShippingDiscountCommand;
use PrestaShop\PrestaShop\Core\Util\DateTime\DateTime as DateTimeUtil;

class CartRuleBuilder
{
public function build(AddDiscountCommand $command): CartRule
{
$cartRule = new CartRule();
$validFrom = $command->getValidFrom() ?: new DateTimeImmutable();
$validTo = $command->getValidTo() ?: $validFrom->modify('+1 month');

$cartRule->name = $command->getLocalizedNames();
$cartRule->description = $command->getDescription();
$cartRule->code = $command->getCode();
$cartRule->highlight = $command->isHighlightInCart();
$cartRule->partial_use = $command->allowPartialUse();
$cartRule->priority = $command->getPriority();
$cartRule->active = $command->isActive();
$cartRule->id_customer = $command->getCustomerId()?->getValue();
$cartRule->date_from = $validFrom->format(DateTimeUtil::DEFAULT_DATETIME_FORMAT);
$cartRule->date_to = $validTo->format(DateTimeUtil::DEFAULT_DATETIME_FORMAT);
$cartRule->quantity = $command->getTotalQuantity();
$cartRule->quantity_per_user = $command->getQuantityPerUser();
$cartRule->type = $command->getDiscountType()->getValue();
$cartRule->free_shipping = $command instanceof AddFreeShippingDiscountCommand;

return $cartRule;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Adapter\Discount\CommandHandler;

use PrestaShop\PrestaShop\Adapter\CartRule\CartRuleBuilder;
use PrestaShop\PrestaShop\Adapter\CartRule\Repository\CartRuleRepository;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\Discount\Command\AddFreeShippingDiscountCommand;
use PrestaShop\PrestaShop\Core\Domain\Discount\CommandHandler\AddFreeShippingDiscountHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Discount\ValueObject\DiscountId;

#[AsCommandHandler]
class AddFreeShippingDiscountHandler implements AddFreeShippingDiscountHandlerInterface
{
public function __construct(
private readonly CartRuleRepository $cartRuleRepository,
private readonly CartRuleBuilder $cartRuleBuilder
) {
}

public function handle(AddFreeShippingDiscountCommand $command): DiscountId
{
$BuiltCartRule = $this->cartRuleBuilder->build($command);
$discount = $this->cartRuleRepository->add($BuiltCartRule);

return new DiscountId((int) $discount->id);
}
}
72 changes: 72 additions & 0 deletions src/Adapter/Discount/QueryHandler/GetDiscountForEditingHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Adapter\Discount\QueryHandler;

use DateTimeImmutable;
use Exception;
use PrestaShop\PrestaShop\Adapter\CartRule\Repository\CartRuleRepository;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsQueryHandler;
use PrestaShop\PrestaShop\Core\Domain\CartRule\ValueObject\CartRuleId;
use PrestaShop\PrestaShop\Core\Domain\Customer\ValueObject\CustomerId;
use PrestaShop\PrestaShop\Core\Domain\Customer\ValueObject\NoCustomerId;
use PrestaShop\PrestaShop\Core\Domain\Discount\Query\GetDiscountForEditing;
use PrestaShop\PrestaShop\Core\Domain\Discount\QueryHandler\GetDiscountForEditingHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Discount\QueryResult\DiscountForEditing;

#[AsQueryHandler]
class GetDiscountForEditingHandler implements GetDiscountForEditingHandlerInterface
{
public function __construct(
protected readonly CartRuleRepository $cartRuleRepository
) {
}

/**
* @throws Exception
*/
public function handle(GetDiscountForEditing $query): DiscountForEditing
{
$cartRuleId = new CartRuleId($query->discountId->getValue());
$cartRule = $this->cartRuleRepository->get($cartRuleId);

return new DiscountForEditing(
$query->discountId,
$cartRule->priority,
$cartRule->active,
new DateTimeImmutable($cartRule->date_from),
new DateTimeImmutable($cartRule->date_to),
$cartRule->quantity,
$cartRule->quantity_per_user,
$cartRule->description,
$cartRule->code,
(int) $cartRule->id_customer !== NoCustomerId::NO_CUSTOMER_ID_VALUE ? new CustomerId((int) $cartRule->id_customer) : new NoCustomerId(),
$cartRule->highlight,
$cartRule->partial_use,
$cartRule->type
);
}
}
Loading

0 comments on commit 86ebce9

Please sign in to comment.