2020-04-28 16:15:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-05-09 10:38:20 +00:00
|
|
|
namespace Tests\Unit\CartRule;
|
2020-04-28 16:15:40 +00:00
|
|
|
|
|
|
|
|
use UnitTester;
|
|
|
|
|
use Codeception\Example;
|
|
|
|
|
use Faker\Factory;
|
|
|
|
|
use Illuminate\Support\Facades\Config;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Facades\Event;
|
|
|
|
|
use Webkul\CartRule\Models\CartRule;
|
|
|
|
|
use Webkul\CartRule\Models\CartRuleCoupon;
|
2022-02-07 10:36:38 +00:00
|
|
|
use Helper\Bagisto;
|
2020-04-28 16:15:40 +00:00
|
|
|
use Webkul\Customer\Models\Customer;
|
|
|
|
|
use Webkul\Customer\Models\CustomerAddress;
|
2021-10-05 08:39:30 +00:00
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
2020-04-28 16:15:40 +00:00
|
|
|
use Webkul\Product\Repositories\ProductDownloadableLinkRepository;
|
|
|
|
|
use Webkul\Sales\Repositories\DownloadableLinkPurchasedRepository;
|
|
|
|
|
use Webkul\Sales\Repositories\OrderItemRepository;
|
|
|
|
|
use Webkul\Sales\Repositories\OrderRepository;
|
|
|
|
|
use Webkul\Tax\Models\TaxCategory;
|
|
|
|
|
use Webkul\Tax\Models\TaxMap;
|
|
|
|
|
use Webkul\Tax\Models\TaxRate;
|
|
|
|
|
use Cart;
|
|
|
|
|
|
|
|
|
|
class cartRuleWithCoupon
|
|
|
|
|
{
|
|
|
|
|
public $cartRule;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $coupon;
|
|
|
|
|
|
|
|
|
|
public function __construct(CartRule $cartRule, CartRuleCoupon $coupon)
|
|
|
|
|
{
|
|
|
|
|
$this->cartRule = $cartRule;
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->coupon = $coupon;
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 08:39:30 +00:00
|
|
|
class expectedCartItem implements Arrayable
|
2020-04-28 16:15:40 +00:00
|
|
|
{
|
2020-05-05 13:46:02 +00:00
|
|
|
public const ITEM_DISCOUNT_AMOUNT_PRECISION = 4;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const ITEM_TAX_AMOUNT_PRECISION = 4;
|
|
|
|
|
|
|
|
|
|
public $cart_id;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $product_id;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $quantity = 1;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $price = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $base_price = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $total = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $base_total = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $tax_percent = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $tax_amount = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $base_tax_amount = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $coupon_code = null;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $discount_percent = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $discount_amount = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $base_discount_amount = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $applied_cart_rule_ids = '';
|
|
|
|
|
|
|
|
|
|
public function __construct(int $cartId, int $productId)
|
|
|
|
|
{
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->cart_id = $cartId;
|
2020-04-28 16:15:40 +00:00
|
|
|
$this->product_id = $productId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function calcTotals(): void
|
|
|
|
|
{
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->total = $this->quantity * $this->price;
|
2020-04-28 16:15:40 +00:00
|
|
|
$this->base_total = $this->quantity * $this->price;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function calcTaxAmounts(): void
|
|
|
|
|
{
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->tax_amount = round($this->quantity * $this->price * $this->tax_percent / 100, self::ITEM_TAX_AMOUNT_PRECISION);
|
2021-10-05 08:39:30 +00:00
|
|
|
$this->base_tax_amount = round($this->quantity * $this->price * $this->tax_percent / 100, self::ITEM_TAX_AMOUNT_PRECISION);
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function calcFixedDiscountAmounts(float $discount, float $baseDiscount, string $code, int $cartRuleId): void
|
|
|
|
|
{
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->discount_amount = $this->quantity * $discount;
|
|
|
|
|
$this->base_discount_amount = $this->quantity * $baseDiscount;
|
|
|
|
|
$this->coupon_code = $code;
|
|
|
|
|
$this->applied_cart_rule_ids = (string)$cartRuleId;
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function calcPercentageDiscountAmounts(float $discount, string $code, int $cartRuleId): void
|
|
|
|
|
{
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->discount_percent = $discount;
|
|
|
|
|
$this->discount_amount = round(($this->total + $this->tax_amount) * $this->discount_percent / 100, self::ITEM_DISCOUNT_AMOUNT_PRECISION);
|
|
|
|
|
$this->base_discount_amount = round(($this->base_total + $this->base_tax_amount) * $this->discount_percent / 100, self::ITEM_DISCOUNT_AMOUNT_PRECISION);
|
|
|
|
|
$this->coupon_code = $code;
|
|
|
|
|
$this->applied_cart_rule_ids = (string)$cartRuleId;
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
2021-10-05 08:39:30 +00:00
|
|
|
|
|
|
|
|
public function toArray(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
2021-10-06 12:26:08 +00:00
|
|
|
'cart_id' => $this->cart_id,
|
|
|
|
|
'product_id' => $this->product_id,
|
|
|
|
|
'quantity' => $this->quantity,
|
|
|
|
|
'price' => $this->price,
|
|
|
|
|
'base_price' => $this->base_price,
|
|
|
|
|
'total' => $this->total,
|
|
|
|
|
'base_total' => $this->base_total,
|
|
|
|
|
'tax_percent' => $this->tax_percent,
|
|
|
|
|
'tax_amount' => $this->tax_amount,
|
|
|
|
|
'base_tax_amount' => $this->base_tax_amount,
|
|
|
|
|
'coupon_code' => $this->coupon_code,
|
|
|
|
|
'discount_percent' => $this->discount_percent,
|
|
|
|
|
'discount_amount' => $this->discount_amount,
|
|
|
|
|
'base_discount_amount' => $this->base_discount_amount,
|
2021-10-05 08:39:30 +00:00
|
|
|
'applied_cart_rule_ids' => $this->applied_cart_rule_ids,
|
|
|
|
|
];
|
|
|
|
|
}
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class expectedCart
|
|
|
|
|
{
|
|
|
|
|
public const CART_TOTAL_PRECISION = 2;
|
|
|
|
|
|
|
|
|
|
public $customer_id;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $id;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $items_count = 0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $items_qty = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $sub_total = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $tax_total = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $discount_amount = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $grand_total = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $base_sub_total = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $base_tax_total = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $base_discount_amount = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $base_grand_total = 0.0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $coupon_code = null;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $applied_cart_rule_ids = '';
|
|
|
|
|
|
|
|
|
|
public function __construct(int $cartId, int $customerId)
|
|
|
|
|
{
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->id = $cartId;
|
2020-04-28 16:15:40 +00:00
|
|
|
$this->customer_id = $customerId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function applyCoupon(int $cartRuleId, string $couponCode): void
|
|
|
|
|
{
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->coupon_code = $couponCode;
|
|
|
|
|
$this->applied_cart_rule_ids = (string)$cartRuleId;
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function finalizeTotals(): void
|
|
|
|
|
{
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->sub_total = round($this->sub_total, self::CART_TOTAL_PRECISION);
|
|
|
|
|
$this->tax_total = round($this->tax_total, self::CART_TOTAL_PRECISION);
|
2020-05-05 13:46:02 +00:00
|
|
|
$this->discount_amount = round($this->discount_amount, self::CART_TOTAL_PRECISION);
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->grand_total = round($this->sub_total + $this->tax_total - $this->discount_amount, self::CART_TOTAL_PRECISION);
|
2020-04-28 16:15:40 +00:00
|
|
|
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->base_sub_total = round($this->base_sub_total, self::CART_TOTAL_PRECISION);
|
|
|
|
|
$this->base_tax_total = round($this->base_tax_total, self::CART_TOTAL_PRECISION);
|
2020-05-05 13:46:02 +00:00
|
|
|
$this->base_discount_amount = round($this->base_discount_amount, self::CART_TOTAL_PRECISION);
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->base_grand_total = round($this->base_sub_total + $this->base_tax_total - $this->base_discount_amount, self::CART_TOTAL_PRECISION);
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function toArray(): array
|
|
|
|
|
{
|
2021-10-06 12:26:08 +00:00
|
|
|
return (array)$this;
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 08:39:30 +00:00
|
|
|
class expectedOrder implements Arrayable
|
2020-04-28 16:15:40 +00:00
|
|
|
{
|
|
|
|
|
public $status;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $customer_email;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $customer_first_name;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $customer_vat_id;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $coupon_code;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $total_item_count;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $total_qty_ordered;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $grand_total;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $base_grand_total;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $sub_total;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $base_sub_total;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $discount_amount;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $base_discount_amount;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $tax_amount;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $base_tax_amount;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $customer_id;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $cart_id;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $applied_cart_rule_ids;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $shipping_method;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $shipping_amount;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $base_shipping_amount;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public $shipping_discount_amount;
|
|
|
|
|
|
|
|
|
|
public function __construct(expectedCart $expectedCart, Customer $customer, int $cartId)
|
|
|
|
|
{
|
2021-10-06 12:26:08 +00:00
|
|
|
$this->status = 'pending';
|
|
|
|
|
$this->customer_email = $customer->email;
|
|
|
|
|
$this->customer_first_name = $customer->first_name;
|
|
|
|
|
$this->customer_vat_id = $customer->vat_id;
|
|
|
|
|
$this->coupon_code = $expectedCart->coupon_code;
|
|
|
|
|
$this->total_item_count = $expectedCart->items_count;
|
|
|
|
|
$this->total_qty_ordered = $expectedCart->items_qty;
|
|
|
|
|
$this->grand_total = $expectedCart->grand_total;
|
|
|
|
|
$this->base_grand_total = $expectedCart->base_grand_total;
|
|
|
|
|
$this->sub_total = $expectedCart->sub_total;
|
|
|
|
|
$this->base_sub_total = $expectedCart->base_sub_total;
|
|
|
|
|
$this->discount_amount = $expectedCart->discount_amount;
|
|
|
|
|
$this->base_discount_amount = $expectedCart->base_discount_amount;
|
|
|
|
|
$this->tax_amount = $expectedCart->tax_total;
|
|
|
|
|
$this->base_tax_amount = $expectedCart->base_tax_total;
|
|
|
|
|
$this->customer_id = $customer->id;
|
|
|
|
|
$this->cart_id = $cartId;
|
|
|
|
|
$this->applied_cart_rule_ids = $expectedCart->applied_cart_rule_ids;
|
|
|
|
|
$this->shipping_method = null;
|
|
|
|
|
$this->shipping_amount = null;
|
|
|
|
|
$this->base_shipping_amount = null;
|
2020-04-28 16:15:40 +00:00
|
|
|
$this->shipping_discount_amount = null;
|
|
|
|
|
}
|
2021-10-05 08:39:30 +00:00
|
|
|
|
|
|
|
|
public function toArray(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
2021-10-06 12:26:08 +00:00
|
|
|
'status' => $this->status,
|
|
|
|
|
'customer_email' => $this->customer_email,
|
|
|
|
|
'customer_first_name' => $this->customer_first_name,
|
|
|
|
|
'customer_vat_id' => $this->customer_vat_id,
|
|
|
|
|
'coupon_code' => $this->coupon_code,
|
|
|
|
|
'total_item_count' => $this->total_item_count,
|
|
|
|
|
'total_qty_ordered' => $this->total_qty_ordered,
|
|
|
|
|
'grand_total' => $this->grand_total,
|
|
|
|
|
'base_grand_total' => $this->base_grand_total,
|
|
|
|
|
'sub_total' => $this->sub_total,
|
|
|
|
|
'base_sub_total' => $this->base_sub_total,
|
|
|
|
|
'discount_amount' => $this->discount_amount,
|
|
|
|
|
'base_discount_amount' => $this->base_discount_amount,
|
|
|
|
|
'tax_amount' => $this->tax_amount,
|
|
|
|
|
'base_tax_amount' => $this->base_tax_amount,
|
|
|
|
|
'customer_id' => $this->customer_id,
|
|
|
|
|
'cart_id' => $this->cart_id,
|
|
|
|
|
'applied_cart_rule_ids' => $this->applied_cart_rule_ids,
|
|
|
|
|
'shipping_method' => $this->shipping_method,
|
|
|
|
|
'shipping_amount' => $this->shipping_amount,
|
|
|
|
|
'base_shipping_amount' => $this->base_shipping_amount,
|
2021-10-05 08:39:30 +00:00
|
|
|
'shipping_discount_amount' => $this->shipping_discount_amount,
|
|
|
|
|
];
|
|
|
|
|
}
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CartRuleCest
|
|
|
|
|
{
|
|
|
|
|
private $products;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
private $sessionToken;
|
|
|
|
|
|
|
|
|
|
public const PRODUCT_PRICE = 13.57;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const REDUCED_PRODUCT_PRICE = 7.21;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const TAX_RATE = 18.5;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const REDUCED_TAX_RATE = 5.5;
|
|
|
|
|
|
|
|
|
|
public const DISCOUNT_AMOUNT_FIX = 3.37;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const DISCOUNT_AMOUNT_PERCENT = 7.5;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const DISCOUNT_AMOUNT_FIX_FULL = 999999.99;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const DISCOUNT_AMOUNT_CART = 8.33;
|
|
|
|
|
|
|
|
|
|
public const ACTION_TYPE_FIXED = "by_fixed";
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const ACTION_TYPE_PERCENTAGE = "by_percent";
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const ACTION_TYPE_CART_FIXED = "cart_fixed";
|
|
|
|
|
|
|
|
|
|
public const PRODUCT_FREE = 0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const PRODUCT_NOT_FREE = 1;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const PRODUCT_NOT_FREE_REDUCED_TAX = 2;
|
|
|
|
|
|
|
|
|
|
public const TAX_CATEGORY = 0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const TAX_REDUCED_CATEGORY = 1;
|
|
|
|
|
|
|
|
|
|
public const COUPON_FIXED = 0;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const COUPON_FIXED_FULL = 1;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const COUPON_PERCENTAGE = 2;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const COUPON_PERCENTAGE_FULL = 3;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
public const COUPON_FIXED_CART = 4;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function getCartWithCouponScenarios(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
2020-08-12 09:31:10 +00:00
|
|
|
// [
|
|
|
|
|
// 'name' => 'check cart coupon',
|
|
|
|
|
// 'productSequence' => [
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// ],
|
|
|
|
|
// 'withCoupon' => true,
|
|
|
|
|
// 'couponScenario' => [
|
|
|
|
|
// 'scenario' => self::COUPON_FIXED_CART,
|
|
|
|
|
// 'products' => [
|
|
|
|
|
// ],
|
|
|
|
|
// ],
|
|
|
|
|
// 'checkOrder' => true,
|
|
|
|
|
// ],
|
2020-04-28 16:15:40 +00:00
|
|
|
// ohne coupon
|
|
|
|
|
[
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => 'PRODUCT_FREE no coupon',
|
2020-04-28 16:15:40 +00:00
|
|
|
'productSequence' => [
|
|
|
|
|
self::PRODUCT_FREE,
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'withCoupon' => false,
|
|
|
|
|
'checkOrder' => false,
|
2020-04-28 16:15:40 +00:00
|
|
|
],
|
|
|
|
|
[
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => 'PRODUCT_NOT_FREE no coupon',
|
2020-04-28 16:15:40 +00:00
|
|
|
'productSequence' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'withCoupon' => false,
|
|
|
|
|
'checkOrder' => false,
|
2020-04-28 16:15:40 +00:00
|
|
|
],
|
|
|
|
|
// fixer Coupon für ein Produkt (Warenkorb wird nicht 0)
|
|
|
|
|
[
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => 'PRODUCT_NOT_FREE fix coupon',
|
2020-04-28 16:15:40 +00:00
|
|
|
'productSequence' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'withCoupon' => true,
|
|
|
|
|
'couponScenario' => [
|
2020-04-28 16:15:40 +00:00
|
|
|
'scenario' => self::COUPON_FIXED,
|
|
|
|
|
'products' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'checkOrder' => false,
|
2020-04-28 16:15:40 +00:00
|
|
|
],
|
|
|
|
|
[
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => 'check fix coupon on product with quantity=2',
|
2020-04-28 16:15:40 +00:00
|
|
|
'productSequence' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'withCoupon' => true,
|
|
|
|
|
'couponScenario' => [
|
2020-04-28 16:15:40 +00:00
|
|
|
'scenario' => self::COUPON_FIXED,
|
|
|
|
|
'products' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'checkOrder' => false,
|
2020-04-28 16:15:40 +00:00
|
|
|
],
|
2020-08-12 09:31:10 +00:00
|
|
|
// [
|
|
|
|
|
// 'name' => 'check fix coupon applied to two products',
|
|
|
|
|
// 'productSequence' => [
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// ],
|
|
|
|
|
// 'withCoupon' => true,
|
|
|
|
|
// 'couponScenario' => [
|
|
|
|
|
// 'scenario' => self::COUPON_FIXED,
|
|
|
|
|
// 'products' => [
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
|
|
|
|
// ],
|
|
|
|
|
// ],
|
|
|
|
|
// 'checkOrder' => true,
|
|
|
|
|
// ],
|
2020-04-28 16:15:40 +00:00
|
|
|
// prozenturaler Coupon für ein Produkt (Warenkorb wird nicht 0)
|
|
|
|
|
[
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => 'PRODUCT_NOT_FREE percentage coupon',
|
2020-04-28 16:15:40 +00:00
|
|
|
'productSequence' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'withCoupon' => true,
|
|
|
|
|
'couponScenario' => [
|
2020-04-28 16:15:40 +00:00
|
|
|
'scenario' => self::COUPON_PERCENTAGE,
|
|
|
|
|
'products' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'checkOrder' => false,
|
2020-04-28 16:15:40 +00:00
|
|
|
],
|
|
|
|
|
[
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => 'check percentage coupon on product with quantity=2',
|
2020-04-28 16:15:40 +00:00
|
|
|
'productSequence' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'withCoupon' => true,
|
|
|
|
|
'couponScenario' => [
|
2020-04-28 16:15:40 +00:00
|
|
|
'scenario' => self::COUPON_PERCENTAGE,
|
|
|
|
|
'products' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'checkOrder' => false,
|
2020-04-28 16:15:40 +00:00
|
|
|
],
|
2020-08-12 09:31:10 +00:00
|
|
|
// [
|
|
|
|
|
// 'name' => 'check percentage coupon applied to two products',
|
|
|
|
|
// 'productSequence' => [
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// ],
|
|
|
|
|
// 'withCoupon' => true,
|
|
|
|
|
// 'couponScenario' => [
|
|
|
|
|
// 'scenario' => self::COUPON_PERCENTAGE,
|
|
|
|
|
// 'products' => [
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
|
|
|
|
// ],
|
|
|
|
|
// ],
|
|
|
|
|
// 'checkOrder' => true,
|
|
|
|
|
// ],
|
2020-04-28 16:15:40 +00:00
|
|
|
// fixer Coupon für ein Produkt (Warenkorb wird 0)
|
|
|
|
|
[
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => 'PRODUCT_NON_SUB_NOT_FREE fix coupon to zero',
|
2020-04-28 16:15:40 +00:00
|
|
|
'productSequence' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'withCoupon' => true,
|
|
|
|
|
'couponScenario' => [
|
2020-04-28 16:15:40 +00:00
|
|
|
'scenario' => self::COUPON_FIXED_FULL,
|
|
|
|
|
'products' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'checkOrder' => false,
|
2020-04-28 16:15:40 +00:00
|
|
|
],
|
|
|
|
|
[
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => 'check fix coupon to zero on product with quantity=2',
|
2020-04-28 16:15:40 +00:00
|
|
|
'productSequence' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'withCoupon' => true,
|
|
|
|
|
'couponScenario' => [
|
2020-04-28 16:15:40 +00:00
|
|
|
'scenario' => self::COUPON_FIXED_FULL,
|
|
|
|
|
'products' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'checkOrder' => false,
|
2020-04-28 16:15:40 +00:00
|
|
|
],
|
2020-08-12 09:31:10 +00:00
|
|
|
// [
|
|
|
|
|
// 'name' => 'check fix coupon to zero applied to two products',
|
|
|
|
|
// 'productSequence' => [
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// ],
|
|
|
|
|
// 'withCoupon' => true,
|
|
|
|
|
// 'couponScenario' => [
|
|
|
|
|
// 'scenario' => self::COUPON_FIXED_FULL,
|
|
|
|
|
// 'products' => [
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
|
|
|
|
// ],
|
|
|
|
|
// ],
|
|
|
|
|
// 'checkOrder' => true,
|
|
|
|
|
// ],
|
2020-04-28 16:15:40 +00:00
|
|
|
// prozenturaler Coupon für ein Produkt (Warenkorb wird 0)
|
|
|
|
|
[
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => 'PRODUCT_NOT_FREE percentage coupon to zero',
|
2020-04-28 16:15:40 +00:00
|
|
|
'productSequence' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'withCoupon' => true,
|
|
|
|
|
'couponScenario' => [
|
2020-04-28 16:15:40 +00:00
|
|
|
'scenario' => self::COUPON_PERCENTAGE_FULL,
|
|
|
|
|
'products' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'checkOrder' => false,
|
2020-04-28 16:15:40 +00:00
|
|
|
],
|
|
|
|
|
[
|
2021-10-06 12:26:08 +00:00
|
|
|
'name' => 'check percentage coupon to zero on product with quantity=2',
|
2020-04-28 16:15:40 +00:00
|
|
|
'productSequence' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'withCoupon' => true,
|
|
|
|
|
'couponScenario' => [
|
2020-04-28 16:15:40 +00:00
|
|
|
'scenario' => self::COUPON_PERCENTAGE_FULL,
|
|
|
|
|
'products' => [
|
|
|
|
|
self::PRODUCT_NOT_FREE,
|
|
|
|
|
],
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'checkOrder' => false,
|
2020-04-28 16:15:40 +00:00
|
|
|
],
|
2020-08-12 09:31:10 +00:00
|
|
|
// [
|
|
|
|
|
// 'name' => 'check percentage coupon to zero applied to two products',
|
|
|
|
|
// 'productSequence' => [
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// ],
|
|
|
|
|
// 'withCoupon' => true,
|
|
|
|
|
// 'couponScenario' => [
|
|
|
|
|
// 'scenario' => self::COUPON_PERCENTAGE_FULL,
|
|
|
|
|
// 'products' => [
|
|
|
|
|
// self::PRODUCT_NOT_FREE,
|
|
|
|
|
// self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
|
|
|
|
// ],
|
|
|
|
|
// ],
|
|
|
|
|
// 'checkOrder' => true,
|
|
|
|
|
// ],
|
2020-04-28 16:15:40 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-05 08:39:30 +00:00
|
|
|
* @param \UnitTester $I
|
|
|
|
|
* @param \Codeception\Example $scenario
|
2020-04-28 16:15:40 +00:00
|
|
|
*
|
|
|
|
|
* @dataProvider getCartWithCouponScenarios
|
|
|
|
|
* @group slow_unit
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
public function checkCartWithCoupon(UnitTester $I, Example $scenario): void
|
|
|
|
|
{
|
|
|
|
|
$faker = Factory::create();
|
|
|
|
|
|
|
|
|
|
config(['app.default_country' => 'DE']);
|
|
|
|
|
|
|
|
|
|
$customer = $I->have(Customer::class);
|
2021-10-05 08:39:30 +00:00
|
|
|
auth()
|
|
|
|
|
->guard('customer')
|
|
|
|
|
->loginUsingId($customer->id);
|
2020-04-28 16:15:40 +00:00
|
|
|
Event::dispatch('customer.after.login', $customer['email']);
|
|
|
|
|
|
|
|
|
|
$this->sessionToken = $faker->uuid;
|
|
|
|
|
session(['_token' => $this->sessionToken]);
|
|
|
|
|
|
2021-10-06 12:26:08 +00:00
|
|
|
$taxCategories = $this->generateTaxCategories($I);
|
2020-04-28 16:15:40 +00:00
|
|
|
$this->products = $this->generateProducts($I, $scenario['productSequence'], $taxCategories);
|
|
|
|
|
|
|
|
|
|
$cartRuleWithCoupon = null;
|
|
|
|
|
if ($scenario['withCoupon']) {
|
|
|
|
|
$cartRuleWithCoupon = $this->generateCartRuleWithCoupon($I, $scenario['couponScenario']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($scenario['productSequence'] as $productIndex) {
|
|
|
|
|
$data = [
|
2021-10-06 12:26:08 +00:00
|
|
|
'_token' => session('_token'),
|
2020-04-28 16:15:40 +00:00
|
|
|
'product_id' => $this->products[$productIndex]->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'quantity' => 1,
|
2020-04-28 16:15:40 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
cart()->addProduct($this->products[$productIndex]->id, $data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($scenario['withCoupon']) {
|
|
|
|
|
$expectedCartCoupon = $cartRuleWithCoupon->coupon->code;
|
2021-10-06 12:26:08 +00:00
|
|
|
$I->comment('I try to use coupon code ' . $expectedCartCoupon);
|
2021-10-05 08:39:30 +00:00
|
|
|
cart()
|
2022-07-11 12:21:33 +00:00
|
|
|
->setCouponCode($expectedCartCoupon->toString())
|
2021-10-05 08:39:30 +00:00
|
|
|
->collectTotals();
|
2020-04-28 16:15:40 +00:00
|
|
|
} else {
|
|
|
|
|
$I->comment('I have no coupon');
|
|
|
|
|
$expectedCartCoupon = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cart = cart()->getCart();
|
|
|
|
|
$I->assertEquals($expectedCartCoupon, $cart->coupon_code);
|
|
|
|
|
|
|
|
|
|
$expectedCartItems = $this->getExpectedCartItems($scenario, $cartRuleWithCoupon, $cart->id);
|
|
|
|
|
$expectedCartItems = $this->checkMaxDiscount($expectedCartItems);
|
|
|
|
|
|
|
|
|
|
foreach ($expectedCartItems as $expectedCartItem) {
|
2021-10-05 08:39:30 +00:00
|
|
|
/**
|
|
|
|
|
* @var $expectedCartItem \Tests\Unit\CartRule\expectedCartItem
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
$I->seeRecord('cart_items', $expectedCartItem->toArray());
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$expectedCart = $this->getExpectedCart($cart->id, $expectedCartItems, $cartRuleWithCoupon);
|
|
|
|
|
$I->seeRecord(\Webkul\Checkout\Models\Cart::class, $expectedCart->toArray());
|
|
|
|
|
|
|
|
|
|
if ($scenario['checkOrder']) {
|
|
|
|
|
$I->wantTo('create and check order from cart');
|
|
|
|
|
|
|
|
|
|
$customerAddress = $I->have(CustomerAddress::class, [
|
|
|
|
|
'first_name' => $customer->first_name,
|
2021-10-06 12:26:08 +00:00
|
|
|
'last_name' => $customer->last_name,
|
|
|
|
|
'country' => 'DE',
|
2020-04-28 16:15:40 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$billing = [
|
2021-10-06 12:26:08 +00:00
|
|
|
'address1' => $customerAddress->address1,
|
2020-04-28 16:15:40 +00:00
|
|
|
'use_for_shipping' => true,
|
2021-10-06 12:26:08 +00:00
|
|
|
'first_name' => $customerAddress->first_name,
|
|
|
|
|
'last_name' => $customerAddress->last_name,
|
|
|
|
|
'email' => $customer->email,
|
|
|
|
|
'company_name' => $customerAddress->company_name,
|
|
|
|
|
'city' => $customerAddress->city,
|
|
|
|
|
'postcode' => $customerAddress->postcode,
|
|
|
|
|
'country' => $customerAddress->country,
|
|
|
|
|
'state' => $customerAddress->state,
|
|
|
|
|
'phone' => $customerAddress->phone,
|
2020-04-28 16:15:40 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$shipping = [
|
2021-10-06 12:26:08 +00:00
|
|
|
'address1' => '',
|
2020-04-28 16:15:40 +00:00
|
|
|
'first_name' => $customerAddress->first_name,
|
2021-10-06 12:26:08 +00:00
|
|
|
'last_name' => $customerAddress->last_name,
|
|
|
|
|
'email' => $customer->email,
|
2020-04-28 16:15:40 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
cart()->saveCustomerAddress([
|
2021-10-06 12:26:08 +00:00
|
|
|
'billing' => $billing,
|
2020-04-28 16:15:40 +00:00
|
|
|
'shipping' => $shipping,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
cart()->saveShippingMethod('free_free');
|
|
|
|
|
cart()->savePaymentMethod(['method' => 'mollie_creditcard']);
|
|
|
|
|
$I->assertFalse(cart()->hasError());
|
2021-10-06 12:26:08 +00:00
|
|
|
$orderItemRepository = new OrderItemRepository(app());
|
|
|
|
|
$downloadableLinkRepository = new ProductDownloadableLinkRepository(app());
|
2021-10-05 08:39:30 +00:00
|
|
|
$downloadableLinkPurchasedRepository = new DownloadableLinkPurchasedRepository($downloadableLinkRepository, app());
|
2021-10-06 12:26:08 +00:00
|
|
|
$orderRepository = new OrderRepository($orderItemRepository, $downloadableLinkPurchasedRepository, app());
|
2020-04-28 16:15:40 +00:00
|
|
|
|
|
|
|
|
$orderRepository->create(cart()->prepareDataForOrder());
|
|
|
|
|
$expectedOrder = new expectedOrder($expectedCart, $customer, $cart->id);
|
2021-10-05 08:39:30 +00:00
|
|
|
$I->seeRecord('orders', $expectedOrder->toArray());
|
2020-04-28 16:15:40 +00:00
|
|
|
|
2022-07-11 12:21:33 +00:00
|
|
|
auth()->guard('customer')->logout();
|
|
|
|
|
|
|
|
|
|
cart()->setCart(null);
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-05 13:46:02 +00:00
|
|
|
public function checkExampleCase(UnitTester $I)
|
|
|
|
|
{
|
|
|
|
|
config(['app.default_country' => 'DE']);
|
|
|
|
|
|
|
|
|
|
$faker = Factory::create();
|
|
|
|
|
|
|
|
|
|
$customer = $I->have(Customer::class);
|
|
|
|
|
|
2021-10-05 08:39:30 +00:00
|
|
|
auth()
|
|
|
|
|
->guard('customer')
|
|
|
|
|
->loginUsingId($customer->id);
|
2020-05-05 13:46:02 +00:00
|
|
|
Event::dispatch('customer.after.login', $customer['email']);
|
|
|
|
|
|
|
|
|
|
$this->sessionToken = $faker->uuid;
|
|
|
|
|
session(['_token' => $this->sessionToken]);
|
|
|
|
|
|
|
|
|
|
$tax = $I->have(TaxRate::class, [
|
2021-10-06 12:26:08 +00:00
|
|
|
'country' => 'DE',
|
2020-05-05 13:46:02 +00:00
|
|
|
'tax_rate' => 19.0,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$taxCategorie = $I->have(TaxCategory::class);
|
|
|
|
|
|
|
|
|
|
$I->have(TaxMap::class, [
|
2021-10-06 12:26:08 +00:00
|
|
|
'tax_rate_id' => $tax->id,
|
2020-05-05 13:46:02 +00:00
|
|
|
'tax_category_id' => $taxCategorie->id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$productConfig = [
|
|
|
|
|
'attributeValues' => [
|
2021-10-06 12:26:08 +00:00
|
|
|
'price' => 23.92,
|
2020-05-05 13:46:02 +00:00
|
|
|
'tax_category_id' => $taxCategorie->id,
|
|
|
|
|
],
|
|
|
|
|
];
|
2022-02-07 10:36:38 +00:00
|
|
|
$product = $I->haveProduct(Bagisto::SIMPLE_PRODUCT, $productConfig);
|
2020-05-05 13:46:02 +00:00
|
|
|
|
|
|
|
|
$ruleConfig = [
|
2021-10-06 12:26:08 +00:00
|
|
|
'action_type' => self::ACTION_TYPE_PERCENTAGE,
|
2020-05-05 13:46:02 +00:00
|
|
|
'discount_amount' => 100,
|
2021-10-06 12:26:08 +00:00
|
|
|
'conditions' => [
|
2020-05-05 13:46:02 +00:00
|
|
|
[
|
2021-10-06 12:26:08 +00:00
|
|
|
'attribute' => 'product|sku',
|
|
|
|
|
'value' => $product->sku,
|
|
|
|
|
'operator' => '==',
|
2020-05-05 13:46:02 +00:00
|
|
|
'attribute_type' => 'text',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
];
|
2021-10-06 12:26:08 +00:00
|
|
|
$cartRule = $I->have(CartRule::class, $ruleConfig);
|
2020-05-05 13:46:02 +00:00
|
|
|
|
2021-10-05 08:39:30 +00:00
|
|
|
DB::table('cart_rule_channels')
|
|
|
|
|
->insert([
|
|
|
|
|
'cart_rule_id' => $cartRule->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'channel_id' => core()->getCurrentChannel()->id,
|
2021-10-05 08:39:30 +00:00
|
|
|
]);
|
2020-05-05 13:46:02 +00:00
|
|
|
|
|
|
|
|
$guestCustomerGroup = $I->grabRecord('customer_groups', ['code' => 'guest']);
|
2021-10-05 08:39:30 +00:00
|
|
|
DB::table('cart_rule_customer_groups')
|
|
|
|
|
->insert([
|
2021-10-06 12:26:08 +00:00
|
|
|
'cart_rule_id' => $cartRule->id,
|
2021-10-05 08:39:30 +00:00
|
|
|
'customer_group_id' => $guestCustomerGroup['id'],
|
|
|
|
|
]);
|
2020-05-05 13:46:02 +00:00
|
|
|
|
|
|
|
|
$generalCustomerGroup = $I->grabRecord('customer_groups', ['code' => 'general']);
|
2021-10-05 08:39:30 +00:00
|
|
|
DB::table('cart_rule_customer_groups')
|
|
|
|
|
->insert([
|
2021-10-06 12:26:08 +00:00
|
|
|
'cart_rule_id' => $cartRule->id,
|
2021-10-05 08:39:30 +00:00
|
|
|
'customer_group_id' => $generalCustomerGroup['id'],
|
|
|
|
|
]);
|
2020-05-05 13:46:02 +00:00
|
|
|
|
|
|
|
|
$coupon = $I->have(CartRuleCoupon::class, [
|
2021-10-06 12:26:08 +00:00
|
|
|
'code' => 'AWESOME',
|
2020-05-05 13:46:02 +00:00
|
|
|
'cart_rule_id' => $cartRule->id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$data = [
|
2021-10-06 12:26:08 +00:00
|
|
|
'_token' => session('_token'),
|
2021-10-05 08:39:30 +00:00
|
|
|
'product_id' => $product->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'quantity' => 1,
|
2020-05-05 13:46:02 +00:00
|
|
|
];
|
|
|
|
|
cart()->addProduct($product->id, $data);
|
2021-10-05 08:39:30 +00:00
|
|
|
cart()
|
|
|
|
|
->setCouponCode('AWESOME')
|
|
|
|
|
->collectTotals();
|
2020-05-05 13:46:02 +00:00
|
|
|
|
2021-10-06 12:26:08 +00:00
|
|
|
$cart = cart()->getCart();
|
2021-10-05 08:39:30 +00:00
|
|
|
$cartItem = $cart->items()
|
|
|
|
|
->first();
|
2020-05-05 13:46:02 +00:00
|
|
|
|
|
|
|
|
$I->assertEquals('AWESOME', $cartItem['coupon_code']);
|
|
|
|
|
$I->assertEquals(23.92, $cartItem['price']);
|
|
|
|
|
$I->assertEquals(19.0, $cartItem['tax_percent']);
|
|
|
|
|
$I->assertEquals(4.5448, $cartItem['tax_amount']);
|
|
|
|
|
$I->assertEquals(28.4648, $cartItem['discount_amount']);
|
|
|
|
|
|
|
|
|
|
$I->assertEquals('AWESOME', $cart->coupon_code);
|
|
|
|
|
$I->assertEquals(23.92, $cart->sub_total);
|
|
|
|
|
$I->assertEquals(4.54, $cart->tax_total);
|
|
|
|
|
$I->assertEquals(28.46, $cart->discount_amount);
|
|
|
|
|
// 23.92 + 4.54 - 28.46 = 0.00
|
|
|
|
|
$I->assertEquals(0.00, $cart->grand_total);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 16:15:40 +00:00
|
|
|
/**
|
2021-10-05 08:39:30 +00:00
|
|
|
* @param \Codeception\Example $scenario
|
|
|
|
|
* @param \Tests\Unit\Category\cartRuleWithCoupon $cartRuleWithCoupon
|
|
|
|
|
* @param int $cartID
|
2020-04-28 16:15:40 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2020-05-05 13:46:02 +00:00
|
|
|
private function getExpectedCartItems(Example $scenario, ?cartRuleWithCoupon $cartRuleWithCoupon, int $cartID): array
|
|
|
|
|
{
|
2020-04-28 16:15:40 +00:00
|
|
|
$cartItems = [];
|
|
|
|
|
|
|
|
|
|
foreach ($scenario['productSequence'] as $key => $item) {
|
2021-10-05 08:39:30 +00:00
|
|
|
$pos = $this->array_find('product_id', $this->products[$scenario['productSequence'][$key]]->id, $cartItems);
|
2020-04-28 16:15:40 +00:00
|
|
|
|
|
|
|
|
if ($pos === null) {
|
2021-10-05 08:39:30 +00:00
|
|
|
$cartItem = new expectedCartItem($cartID, $this->products[$scenario['productSequence'][$key]]->id);
|
2020-04-28 16:15:40 +00:00
|
|
|
} else {
|
|
|
|
|
$cartItem = $cartItems[$pos];
|
|
|
|
|
$cartItem->quantity++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch ($item) {
|
|
|
|
|
case self::PRODUCT_FREE:
|
|
|
|
|
$cartItem->tax_percent = self::TAX_RATE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case self::PRODUCT_NOT_FREE:
|
2021-10-06 12:26:08 +00:00
|
|
|
$cartItem->price = self::PRODUCT_PRICE;
|
|
|
|
|
$cartItem->base_price = self::PRODUCT_PRICE;
|
2020-04-28 16:15:40 +00:00
|
|
|
$cartItem->tax_percent = self::TAX_RATE;
|
|
|
|
|
|
|
|
|
|
$cartItem->calcTotals();
|
|
|
|
|
$cartItem->calcTaxAmounts();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case self::PRODUCT_NOT_FREE_REDUCED_TAX:
|
2021-10-06 12:26:08 +00:00
|
|
|
$cartItem->price = self::REDUCED_PRODUCT_PRICE;
|
|
|
|
|
$cartItem->base_price = self::REDUCED_PRODUCT_PRICE;
|
2020-04-28 16:15:40 +00:00
|
|
|
$cartItem->tax_percent = self::REDUCED_TAX_RATE;
|
|
|
|
|
|
|
|
|
|
$cartItem->calcTotals();
|
|
|
|
|
$cartItem->calcTaxAmounts();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($scenario['withCoupon']) {
|
|
|
|
|
switch ($scenario['couponScenario']['scenario']) {
|
|
|
|
|
case self::COUPON_FIXED:
|
|
|
|
|
foreach ($scenario['couponScenario']['products'] as $couponItem) {
|
|
|
|
|
if ($item === $couponItem) {
|
2021-10-05 08:39:30 +00:00
|
|
|
$cartItem->calcFixedDiscountAmounts(self::DISCOUNT_AMOUNT_FIX, self::DISCOUNT_AMOUNT_FIX, $cartRuleWithCoupon->coupon->code, $cartRuleWithCoupon->cartRule->id);
|
2020-04-28 16:15:40 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case self::COUPON_FIXED_FULL:
|
|
|
|
|
foreach ($scenario['couponScenario']['products'] as $couponItem) {
|
|
|
|
|
if ($item === $couponItem) {
|
2021-10-05 08:39:30 +00:00
|
|
|
$cartItem->calcFixedDiscountAmounts(self::DISCOUNT_AMOUNT_FIX_FULL, self::DISCOUNT_AMOUNT_FIX_FULL, $cartRuleWithCoupon->coupon->code, $cartRuleWithCoupon->cartRule->id);
|
2020-04-28 16:15:40 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case self::COUPON_PERCENTAGE:
|
|
|
|
|
foreach ($scenario['couponScenario']['products'] as $couponItem) {
|
|
|
|
|
if ($item === $couponItem) {
|
2021-10-05 08:39:30 +00:00
|
|
|
$cartItem->calcPercentageDiscountAmounts(self::DISCOUNT_AMOUNT_PERCENT, $cartRuleWithCoupon->coupon->code, $cartRuleWithCoupon->cartRule->id);
|
2020-04-28 16:15:40 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case self::COUPON_PERCENTAGE_FULL:
|
|
|
|
|
foreach ($scenario['couponScenario']['products'] as $couponItem) {
|
|
|
|
|
if ($item === $couponItem) {
|
2021-10-05 08:39:30 +00:00
|
|
|
$cartItem->calcPercentageDiscountAmounts(100.0, $cartRuleWithCoupon->coupon->code, $cartRuleWithCoupon->cartRule->id);
|
2020-04-28 16:15:40 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($pos === null) {
|
|
|
|
|
$cartItems[] = $cartItem;
|
|
|
|
|
} else {
|
|
|
|
|
$cartItems[$pos] = $cartItem;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($scenario['withCoupon'] && $scenario['couponScenario']['scenario'] === self::COUPON_FIXED_CART) {
|
2021-10-06 12:26:08 +00:00
|
|
|
$totals = $this->calcTotals($cartItems);
|
2020-04-28 16:15:40 +00:00
|
|
|
$cartItems = $this->splitDiscountToItems($cartItems, $cartRuleWithCoupon, $totals);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $cartItems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function calcTotals(array $cartItems): array
|
|
|
|
|
{
|
|
|
|
|
$result = [
|
2021-10-06 12:26:08 +00:00
|
|
|
'subTotal' => 0.0,
|
2020-04-28 16:15:40 +00:00
|
|
|
'baseSubTotal' => 0.0,
|
|
|
|
|
];
|
|
|
|
|
foreach ($cartItems as $expectedCartItem) {
|
2021-10-06 12:26:08 +00:00
|
|
|
$result['subTotal'] += $expectedCartItem->total;
|
2020-04-28 16:15:40 +00:00
|
|
|
$result['baseSubTotal'] += $expectedCartItem->base_total;
|
|
|
|
|
}
|
2021-10-06 12:26:08 +00:00
|
|
|
$result['subTotal'] = round($result['subTotal'], expectedCart::CART_TOTAL_PRECISION);
|
2020-04-28 16:15:40 +00:00
|
|
|
$result['baseSubTotal'] = round($result['baseSubTotal'], expectedCart::CART_TOTAL_PRECISION);
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function splitDiscountToItems(
|
|
|
|
|
array $cartItems,
|
|
|
|
|
cartRuleWithCoupon $cartRuleWithCoupon,
|
|
|
|
|
array $totals
|
|
|
|
|
): array {
|
2021-10-06 12:26:08 +00:00
|
|
|
$discountAmount = self::DISCOUNT_AMOUNT_CART;
|
2020-04-28 16:15:40 +00:00
|
|
|
$baseDiscountAmount = self::DISCOUNT_AMOUNT_CART;
|
|
|
|
|
// split coupon amount to cart items
|
|
|
|
|
$length = count($cartItems) - 1;
|
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
2021-10-05 08:39:30 +00:00
|
|
|
$cartItems[$i]->discount_amount = round(self::DISCOUNT_AMOUNT_CART * $cartItems[$i]->total / $totals['subTotal'], expectedCartItem::ITEM_DISCOUNT_AMOUNT_PRECISION);
|
2021-10-06 12:26:08 +00:00
|
|
|
$discountAmount -= $cartItems[$i]->discount_amount;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
|
|
|
|
$cartItems[$i]->base_discount_amount = round(self::DISCOUNT_AMOUNT_CART * $cartItems[$i]->base_total / $totals['baseSubTotal'], expectedCartItem::ITEM_DISCOUNT_AMOUNT_PRECISION);
|
2021-10-06 12:26:08 +00:00
|
|
|
$baseDiscountAmount -= $cartItems[$i]->discount_amount;
|
2021-10-05 08:39:30 +00:00
|
|
|
|
2021-10-06 12:26:08 +00:00
|
|
|
$cartItems[$i]->coupon_code = $cartRuleWithCoupon->coupon->code;
|
|
|
|
|
$cartItems[$i]->applied_cart_rule_ids = (string)$cartRuleWithCoupon->cartRule->id;
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-06 12:26:08 +00:00
|
|
|
$cartItems[$length]->discount_amount = $discountAmount;
|
2020-04-28 16:15:40 +00:00
|
|
|
$cartItems[$length]->base_discount_amount = $baseDiscountAmount;
|
|
|
|
|
|
2021-10-06 12:26:08 +00:00
|
|
|
$cartItems[$length]->coupon_code = $cartRuleWithCoupon->coupon->code;
|
|
|
|
|
$cartItems[$length]->applied_cart_rule_ids = (string)$cartRuleWithCoupon->cartRule->id;
|
2020-04-28 16:15:40 +00:00
|
|
|
|
|
|
|
|
return $cartItems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-05 08:39:30 +00:00
|
|
|
* @param array $expectedCartItems
|
2020-04-28 16:15:40 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function checkMaxDiscount(array $expectedCartItems): array
|
|
|
|
|
{
|
|
|
|
|
foreach ($expectedCartItems as $key => $cartItem) {
|
2021-10-05 08:39:30 +00:00
|
|
|
$itemGrandTotal = round($cartItem->total + $cartItem->tax_amount, expectedCartItem::ITEM_DISCOUNT_AMOUNT_PRECISION);
|
2020-04-28 16:15:40 +00:00
|
|
|
if ($cartItem->discount_amount > $itemGrandTotal) {
|
|
|
|
|
$expectedCartItems[$key]->discount_amount = $itemGrandTotal;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 08:39:30 +00:00
|
|
|
$itemBaseGrandTotal = round($cartItem->base_total + $cartItem->base_tax_amount, expectedCartItem::ITEM_DISCOUNT_AMOUNT_PRECISION);
|
2020-04-28 16:15:40 +00:00
|
|
|
if ($cartItem->base_discount_amount > $itemBaseGrandTotal) {
|
|
|
|
|
$expectedCartItems[$key]->base_discount_amount = $itemBaseGrandTotal;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $expectedCartItems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-05 08:39:30 +00:00
|
|
|
* @param int $cartId
|
|
|
|
|
* @param array $expectedCartItems
|
2020-04-28 16:15:40 +00:00
|
|
|
*
|
2021-10-05 08:39:30 +00:00
|
|
|
* @param \Tests\Unit\Category\cartRuleWithCoupon $cartRuleWithCoupon
|
2020-04-28 16:15:40 +00:00
|
|
|
*
|
|
|
|
|
* @return \Tests\Unit\Category\expectedCart
|
|
|
|
|
*/
|
2020-05-05 13:46:02 +00:00
|
|
|
private function getExpectedCart(int $cartId, array $expectedCartItems, ?cartRuleWithCoupon $cartRuleWithCoupon): expectedCart
|
|
|
|
|
{
|
2021-10-05 08:39:30 +00:00
|
|
|
$cart = new expectedCart($cartId, auth()
|
|
|
|
|
->guard('customer')
|
|
|
|
|
->user()->id);
|
2020-04-28 16:15:40 +00:00
|
|
|
|
|
|
|
|
if ($cartRuleWithCoupon) {
|
2021-10-05 08:39:30 +00:00
|
|
|
$cart->applyCoupon($cartRuleWithCoupon->cartRule->id, $cartRuleWithCoupon->coupon->code);
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($expectedCartItems as $cartItem) {
|
|
|
|
|
$cart->items_count++;
|
|
|
|
|
$cart->items_qty += $cartItem->quantity;
|
|
|
|
|
|
2021-10-06 12:26:08 +00:00
|
|
|
$cart->sub_total += $cartItem->total;
|
|
|
|
|
$cart->tax_total += $cartItem->tax_amount;
|
2020-04-28 16:15:40 +00:00
|
|
|
$cart->discount_amount += $cartItem->discount_amount;
|
|
|
|
|
|
2021-10-06 12:26:08 +00:00
|
|
|
$cart->base_sub_total += $cartItem->base_total;
|
|
|
|
|
$cart->base_tax_total += $cartItem->base_tax_amount;
|
2020-04-28 16:15:40 +00:00
|
|
|
$cart->base_discount_amount += $cartItem->base_discount_amount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cart->finalizeTotals();
|
|
|
|
|
|
|
|
|
|
return $cart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-05 08:39:30 +00:00
|
|
|
* @param \UnitTester $I
|
2020-04-28 16:15:40 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function generateTaxCategories(UnitTester $I): array
|
|
|
|
|
{
|
2021-10-06 12:26:08 +00:00
|
|
|
$result = [];
|
2020-04-28 16:15:40 +00:00
|
|
|
$country = strtoupper(Config::get('app.default_country')) ?? 'DE';
|
|
|
|
|
foreach ($this->getTaxRateSpecifications() as $taxSpec => $taxRate) {
|
|
|
|
|
$tax = $I->have(TaxRate::class, [
|
2021-10-06 12:26:08 +00:00
|
|
|
'country' => $country,
|
2020-04-28 16:15:40 +00:00
|
|
|
'tax_rate' => $taxRate,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$taxCategorie = $I->have(TaxCategory::class);
|
|
|
|
|
|
|
|
|
|
$I->have(TaxMap::class, [
|
2021-10-06 12:26:08 +00:00
|
|
|
'tax_rate_id' => $tax->id,
|
2020-04-28 16:15:40 +00:00
|
|
|
'tax_category_id' => $taxCategorie->id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$result[$taxSpec] = $taxCategorie->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-05 08:39:30 +00:00
|
|
|
* @param \UnitTester $I
|
|
|
|
|
* @param array $scenario
|
|
|
|
|
* @param array $taxCategories
|
2020-04-28 16:15:40 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
private function generateProducts(UnitTester $I, array $scenario, array $taxCategories): array
|
|
|
|
|
{
|
2021-10-06 12:26:08 +00:00
|
|
|
$products = [];
|
2020-04-28 16:15:40 +00:00
|
|
|
$productSpecs = $this->getProductSpecifications();
|
|
|
|
|
|
|
|
|
|
foreach ($scenario as $item) {
|
2021-10-06 12:26:08 +00:00
|
|
|
$productConfig = $this->makeProductConfig($productSpecs[$item], $taxCategories);
|
2020-04-28 16:15:40 +00:00
|
|
|
$products[$item] = $I->haveProduct($productSpecs[$item]['productType'], $productConfig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $products;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-05 08:39:30 +00:00
|
|
|
* @param \UnitTester $I
|
|
|
|
|
* @param array $couponConfig
|
2020-04-28 16:15:40 +00:00
|
|
|
*
|
|
|
|
|
* @return \Tests\Unit\Category\cartRuleWithCoupon
|
|
|
|
|
*/
|
|
|
|
|
private function generateCartRuleWithCoupon(UnitTester $I, array $couponConfig): cartRuleWithCoupon
|
|
|
|
|
{
|
|
|
|
|
$faker = Factory::create();
|
|
|
|
|
|
|
|
|
|
$couponSpecifications = $this->getCouponSpecifications();
|
2021-10-06 12:26:08 +00:00
|
|
|
$ruleConfig = $this->makeRuleConfig($couponSpecifications[$couponConfig['scenario']], $this->products, $couponConfig['products']);
|
2022-07-11 12:21:33 +00:00
|
|
|
|
2021-10-06 12:26:08 +00:00
|
|
|
$cartRule = $I->have(CartRule::class, $ruleConfig);
|
2021-10-05 08:39:30 +00:00
|
|
|
|
|
|
|
|
DB::table('cart_rule_channels')
|
|
|
|
|
->insert([
|
|
|
|
|
'cart_rule_id' => $cartRule->id,
|
2021-10-06 12:26:08 +00:00
|
|
|
'channel_id' => core()->getCurrentChannel()->id,
|
2021-10-05 08:39:30 +00:00
|
|
|
]);
|
2020-04-28 16:15:40 +00:00
|
|
|
|
|
|
|
|
$guestCustomerGroup = $I->grabRecord('customer_groups', ['code' => 'guest']);
|
2021-10-05 08:39:30 +00:00
|
|
|
DB::table('cart_rule_customer_groups')
|
|
|
|
|
->insert([
|
2021-10-06 12:26:08 +00:00
|
|
|
'cart_rule_id' => $cartRule->id,
|
2021-10-05 08:39:30 +00:00
|
|
|
'customer_group_id' => $guestCustomerGroup['id'],
|
|
|
|
|
]);
|
2020-04-28 16:15:40 +00:00
|
|
|
|
|
|
|
|
$generalCustomerGroup = $I->grabRecord('customer_groups', ['code' => 'general']);
|
2021-10-05 08:39:30 +00:00
|
|
|
DB::table('cart_rule_customer_groups')
|
|
|
|
|
->insert([
|
2021-10-06 12:26:08 +00:00
|
|
|
'cart_rule_id' => $cartRule->id,
|
2021-10-05 08:39:30 +00:00
|
|
|
'customer_group_id' => $generalCustomerGroup['id'],
|
|
|
|
|
]);
|
2020-04-28 16:15:40 +00:00
|
|
|
|
|
|
|
|
$coupon = $I->have(CartRuleCoupon::class, [
|
|
|
|
|
'cart_rule_id' => $cartRule->id,
|
|
|
|
|
]);
|
|
|
|
|
|
2021-10-05 08:39:30 +00:00
|
|
|
return new cartRuleWithCoupon($cartRule, $coupon);
|
2020-04-28 16:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-05 08:39:30 +00:00
|
|
|
* @param array $productSpec
|
|
|
|
|
* @param array $taxCategories
|
2020-04-28 16:15:40 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function makeProductConfig(array $productSpec, array $taxCategories): array
|
|
|
|
|
{
|
|
|
|
|
$result = [
|
|
|
|
|
'productInventory' => [
|
|
|
|
|
'qty' => 100,
|
|
|
|
|
],
|
2021-10-06 12:26:08 +00:00
|
|
|
'attributeValues' => [
|
|
|
|
|
'price' => 0.0,
|
2020-04-28 16:15:40 +00:00
|
|
|
'tax_category_id' => $taxCategories[self::TAX_CATEGORY],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($productSpec['reducedTax']) {
|
|
|
|
|
if (array_key_exists(self::TAX_REDUCED_CATEGORY, $taxCategories)) {
|
|
|
|
|
$result['attributeValues']['tax_category_id'] = $taxCategories[self::TAX_REDUCED_CATEGORY];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$productSpec['freeOfCharge']) {
|
|
|
|
|
if ($productSpec['reducedTax']) {
|
|
|
|
|
$result['attributeValues']['price'] = self::REDUCED_PRODUCT_PRICE;
|
|
|
|
|
} else {
|
|
|
|
|
$result['attributeValues']['price'] = self::PRODUCT_PRICE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-05 08:39:30 +00:00
|
|
|
* @param array $ruleSpec
|
|
|
|
|
* @param array $products
|
|
|
|
|
* @param array $couponableProducts
|
2020-04-28 16:15:40 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function makeRuleConfig(array $ruleSpec, array $products, array $couponableProducts): array
|
|
|
|
|
{
|
|
|
|
|
foreach ($couponableProducts as $item) {
|
|
|
|
|
$conditions[] = [
|
2021-10-06 12:26:08 +00:00
|
|
|
'value' => $products[$item]->sku,
|
|
|
|
|
'operator' => '==',
|
|
|
|
|
'attribute' => 'product|sku',
|
2020-04-28 16:15:40 +00:00
|
|
|
'attribute_type' => 'text',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = [
|
2021-10-06 12:26:08 +00:00
|
|
|
'action_type' => $ruleSpec['actionType'],
|
2020-04-28 16:15:40 +00:00
|
|
|
'discount_amount' => $ruleSpec['discountAmount'],
|
2021-10-06 12:26:08 +00:00
|
|
|
'conditions' => $conditions ?? null,
|
2020-04-28 16:15:40 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function getProductSpecifications(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
'productScenario' => self::PRODUCT_FREE,
|
2022-02-07 10:36:38 +00:00
|
|
|
'productType' => Bagisto::SIMPLE_PRODUCT,
|
2021-10-06 12:26:08 +00:00
|
|
|
'freeOfCharge' => true,
|
|
|
|
|
'reducedTax' => false,
|
2020-04-28 16:15:40 +00:00
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'productScenario' => self::PRODUCT_NOT_FREE,
|
2022-02-07 10:36:38 +00:00
|
|
|
'productType' => Bagisto::SIMPLE_PRODUCT,
|
2021-10-06 12:26:08 +00:00
|
|
|
'freeOfCharge' => false,
|
|
|
|
|
'reducedTax' => false,
|
2020-04-28 16:15:40 +00:00
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'productScenario' => self::PRODUCT_NOT_FREE_REDUCED_TAX,
|
2022-02-07 10:36:38 +00:00
|
|
|
'productType' => Bagisto::SIMPLE_PRODUCT,
|
2021-10-06 12:26:08 +00:00
|
|
|
'freeOfCharge' => false,
|
|
|
|
|
'reducedTax' => true,
|
2020-04-28 16:15:40 +00:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function getCouponSpecifications(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
'couponScenario' => self::COUPON_FIXED,
|
2021-10-06 12:26:08 +00:00
|
|
|
'actionType' => self::ACTION_TYPE_FIXED,
|
2020-04-28 16:15:40 +00:00
|
|
|
'discountAmount' => self::DISCOUNT_AMOUNT_FIX,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'couponScenario' => self::COUPON_FIXED_FULL,
|
2021-10-06 12:26:08 +00:00
|
|
|
'actionType' => self::ACTION_TYPE_FIXED,
|
2020-04-28 16:15:40 +00:00
|
|
|
'discountAmount' => self::DISCOUNT_AMOUNT_FIX_FULL,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'couponScenario' => self::COUPON_PERCENTAGE,
|
2021-10-06 12:26:08 +00:00
|
|
|
'actionType' => self::ACTION_TYPE_PERCENTAGE,
|
2020-04-28 16:15:40 +00:00
|
|
|
'discountAmount' => self::DISCOUNT_AMOUNT_PERCENT,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'couponScenario' => self::COUPON_PERCENTAGE_FULL,
|
2021-10-06 12:26:08 +00:00
|
|
|
'actionType' => self::ACTION_TYPE_PERCENTAGE,
|
2020-04-28 16:15:40 +00:00
|
|
|
'discountAmount' => 100.0,
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'couponScenario' => self::COUPON_FIXED_CART,
|
2021-10-06 12:26:08 +00:00
|
|
|
'actionType' => self::ACTION_TYPE_CART_FIXED,
|
2020-04-28 16:15:40 +00:00
|
|
|
'discountAmount' => self::DISCOUNT_AMOUNT_CART,
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function getTaxRateSpecifications(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
2021-10-06 12:26:08 +00:00
|
|
|
self::TAX_CATEGORY => self::TAX_RATE,
|
2020-04-28 16:15:40 +00:00
|
|
|
self::TAX_REDUCED_CATEGORY => self::REDUCED_TAX_RATE,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-05 08:39:30 +00:00
|
|
|
* @param string $param
|
2020-04-28 16:15:40 +00:00
|
|
|
* @param $needleValue
|
2021-10-05 08:39:30 +00:00
|
|
|
* @param array $data
|
2020-04-28 16:15:40 +00:00
|
|
|
*
|
|
|
|
|
* @return int|null
|
|
|
|
|
*/
|
|
|
|
|
private function array_find(string $param, $needleValue, array $data): ?int
|
|
|
|
|
{
|
|
|
|
|
foreach ($data as $pos => $object) {
|
|
|
|
|
if ($object->$param === $needleValue) {
|
|
|
|
|
return $pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|