Improved cart rule package code style

This commit is contained in:
Jitendra Singh 2020-02-19 17:19:52 +05:30
parent a72401dbba
commit 9c78f3b4db
7 changed files with 180 additions and 145 deletions

View File

@ -107,8 +107,9 @@ class CartRule
foreach ($cart->items()->get() as $item) {
$this->process($item);
if ($item->children()->count() && $item->product->getTypeInstance()->isChildrenCalculated())
if ($item->children()->count() && $item->product->getTypeInstance()->isChildrenCalculated()) {
$this->devideDiscount($item);
}
}
$this->processShippingDiscount($cart);
@ -127,16 +128,18 @@ class CartRule
{
static $cartRules;
if ($cartRules)
if ($cartRules) {
return $cartRules;
}
$customerGroupId = null;
if (Cart::getCurrentCustomer()->check()) {
$customerGroupId = Cart::getCurrentCustomer()->user()->customer_group_id;
} else {
if ($customerGuestGroup = $this->customerGroupRepository->findOneByField('code', 'guest'))
if ($customerGuestGroup = $this->customerGroupRepository->findOneByField('code', 'guest')) {
$customerGroupId = $customerGuestGroup->id;
}
}
$cartRules = $this->cartRuleRepository->scopeQuery(function($query) use ($customerGroupId) {
@ -170,21 +173,23 @@ class CartRule
if (strlen($cart->coupon_code)) {
$coupon = $this->cartRuleCouponRepository->findOneWhere([
'cart_rule_id' => $rule->id,
'code' => $cart->coupon_code,
'code' => $cart->coupon_code,
]);
if ($coupon) {
if ($coupon->usage_limit && $coupon->times_used >= $coupon->usage_limit)
if ($coupon->usage_limit && $coupon->times_used >= $coupon->usage_limit) {
return false;
}
if ($cart->customer_id && $coupon->usage_per_customer) {
$couponUsage = $this->cartRuleCouponUsageRepository->findOneWhere([
'cart_rule_coupon_id' => $coupon->id,
'customer_id' => $cart->customer_id
'customer_id' => $cart->customer_id
]);
if ($couponUsage && $couponUsage->times_used >= $coupon->usage_per_customer)
if ($couponUsage && $couponUsage->times_used >= $coupon->usage_per_customer) {
return false;
}
}
} else {
return false;
@ -197,11 +202,12 @@ class CartRule
if ($rule->usage_per_customer) {
$ruleCustomer = $this->cartRuleCustomerRepository->findOneWhere([
'cart_rule_id' => $rule->id,
'customer_id' => $cart->customer_id,
'customer_id' => $cart->customer_id,
]);
if ($ruleCustomer && $ruleCustomer->times_used >= $rule->usage_per_customer)
if ($ruleCustomer && $ruleCustomer->times_used >= $rule->usage_per_customer) {
return false;
}
}
return true;
@ -226,11 +232,13 @@ class CartRule
$appliedRuleIds = [];
foreach ($this->getCartRules() as $rule) {
if (! $this->canProcessRule($rule))
if (! $this->canProcessRule($rule)) {
continue;
}
if (! $this->validator->validate($rule, $item))
if (! $this->validator->validate($rule, $item)) {
continue;
}
$quantity = $rule->discount_quantity ? min($item->quantity, $rule->discount_quantity) : $item->quantity;
@ -279,8 +287,9 @@ class CartRule
break;
case 'buy_x_get_y':
if (! $rule->discount_step || $rule->discount_amount > $rule->discount_step)
if (! $rule->discount_step || $rule->discount_amount > $rule->discount_step) {
break;
}
$buyAndDiscountQty = $rule->discount_step + $rule->discount_amount;
@ -290,8 +299,9 @@ class CartRule
$discountQty = $qtyPeriod * $rule->discount_amount;
if ($freeQty > $rule->discount_step)
if ($freeQty > $rule->discount_step) {
$discountQty += $freeQty - $rule->discount_step;
}
$discountAmount = $discountQty * $item->price;
@ -305,8 +315,9 @@ class CartRule
$appliedRuleIds[$rule->id] = $rule->id;
if ($rule->end_other_rules)
if ($rule->end_other_rules) {
break;
}
}
$item->applied_cart_rule_ids = join(',', $appliedRuleIds);
@ -332,8 +343,9 @@ class CartRule
*/
public function processShippingDiscount($cart)
{
if (! $selectedShipping = $cart->selected_shipping_rate)
if (! $selectedShipping = $cart->selected_shipping_rate) {
return;
}
$selectedShipping->discount_amount = 0;
$selectedShipping->base_discount_amount = 0;
@ -341,14 +353,17 @@ class CartRule
$appliedRuleIds = [];
foreach ($this->getCartRules() as $rule) {
if (! $this->canProcessRule($rule))
if (! $this->canProcessRule($rule)) {
continue;
}
if (! $this->validator->validate($rule, $cart))
if (! $this->validator->validate($rule, $cart)) {
continue;
}
if (! $rule || ! $rule->apply_to_shipping)
if (! $rule || ! $rule->apply_to_shipping) {
continue;
}
$discountAmount = $baseDiscountAmount = 0;
@ -381,8 +396,9 @@ class CartRule
$appliedRuleIds[$rule->id] = $rule->id;
if ($rule->end_other_rules)
if ($rule->end_other_rules) {
break;
}
}
$selectedShipping->save();
@ -408,8 +424,9 @@ class CartRule
*/
public function processFreeShippingDiscount($cart)
{
if (! $selectedShipping = $cart->selected_shipping_rate)
if (! $selectedShipping = $cart->selected_shipping_rate) {
return;
}
$selectedShipping->discount_amount = 0;
@ -418,14 +435,17 @@ class CartRule
$appliedRuleIds = [];
foreach ($this->getCartRules() as $rule) {
if (! $this->canProcessRule($rule))
if (! $this->canProcessRule($rule)) {
continue;
}
if (! $this->validator->validate($rule, $cart))
if (! $this->validator->validate($rule, $cart)) {
continue;
}
if (! $rule || ! $rule->free_shipping)
if (! $rule || ! $rule->free_shipping) {
continue;
}
$selectedShipping->price = 0;
@ -435,8 +455,9 @@ class CartRule
$appliedRuleIds[$rule->id] = $rule->id;
if ($rule->end_other_rules)
if ($rule->end_other_rules) {
break;
}
}
$cartAppliedCartRuleIds = array_merge(explode(',', $cart->applied_cart_rule_ids), $appliedRuleIds);
@ -463,8 +484,9 @@ class CartRule
$totalPrice = $totalBasePrice = $validCount = 0;
foreach ($items as $item) {
if (! $this->canProcessRule($rule, $item))
if (! $this->canProcessRule($rule, $item)) {
continue;
}
$quantity = $rule->discount_quantity ? min($item->quantity, $rule->discount_quantity) : $item->quantity;
@ -475,7 +497,7 @@ class CartRule
$this->itemTotals[$rule->id] = [
'base_total_price' => $totalBasePrice,
'total_items' => $validCount,
'total_items' => $validCount,
];
}
}
@ -490,13 +512,15 @@ class CartRule
{
$cart = Cart::getCart();
if (! $cart->coupon_code)
if (! $cart->coupon_code) {
return;
}
$coupon = $this->cartRuleCouponRepository->findOneByField('code', $cart->coupon_code);
if (! $coupon || ! in_array($coupon->cart_rule_id, explode(',', $cart->applied_cart_rule_ids)))
if (! $coupon || ! in_array($coupon->cart_rule_id, explode(',', $cart->applied_cart_rule_ids))) {
Cart::removeCouponCode();
}
}
/**
@ -511,8 +535,9 @@ class CartRule
$ratio = $item->base_total != 0 ? $child->base_total / $item->base_total : 0;
foreach (['discount_amount', 'base_discount_amount'] as $column) {
if (! $item->{$column})
if (! $item->{$column}) {
continue;
}
$child->{$column} = round(($item->{$column} * $ratio), 4);

View File

@ -83,16 +83,16 @@ class CartRuleController extends Controller
public function store()
{
$this->validate(request(), [
'name' => 'required',
'channels' => 'required|array|min:1',
'customer_groups' => 'required|array|min:1',
'coupon_type' => 'required',
'name' => 'required',
'channels' => 'required|array|min:1',
'customer_groups' => 'required|array|min:1',
'coupon_type' => 'required',
'use_auto_generation' => 'required_if:coupon_type,==,1',
'coupon_code' => 'required_if:use_auto_generation,==,0',
'starts_from' => 'nullable|date',
'ends_till' => 'nullable|date|after_or_equal:starts_from',
'action_type' => 'required',
'discount_amount' => 'required|numeric'
'coupon_code' => 'required_if:use_auto_generation,==,0',
'starts_from' => 'nullable|date',
'ends_till' => 'nullable|date|after_or_equal:starts_from',
'action_type' => 'required',
'discount_amount' => 'required|numeric'
]);
$data = request()->all();
@ -131,16 +131,16 @@ class CartRuleController extends Controller
public function update(Request $request, $id)
{
$this->validate(request(), [
'name' => 'required',
'channels' => 'required|array|min:1',
'customer_groups' => 'required|array|min:1',
'coupon_type' => 'required',
'name' => 'required',
'channels' => 'required|array|min:1',
'customer_groups' => 'required|array|min:1',
'coupon_type' => 'required',
'use_auto_generation' => 'required_if:coupon_type,==,1',
'coupon_code' => 'required_if:use_auto_generation,==,0',
'starts_from' => 'nullable|date',
'ends_till' => 'nullable|date|after_or_equal:starts_from',
'action_type' => 'required',
'discount_amount' => 'required|numeric'
'coupon_code' => 'required_if:use_auto_generation,==,0',
'starts_from' => 'nullable|date',
'ends_till' => 'nullable|date|after_or_equal:starts_from',
'action_type' => 'required',
'discount_amount' => 'required|numeric'
]);
$cartRule = $this->cartRuleRepository->findOrFail($id);
@ -191,13 +191,14 @@ class CartRuleController extends Controller
public function generateCoupons()
{
$this->validate(request(), [
'coupon_qty' => 'required|integer|min:1',
'coupon_qty' => 'required|integer|min:1',
'code_length' => 'required|integer|min:10',
'code_format' => 'required'
]);
if (! request('id'))
if (! request('id')) {
return response()->json(['message' => trans('admin::app.promotions.cart-rules.cart-rule-not-defind-error')], 400);
}
$this->cartRuleCouponRepository->generateCoupons(request()->all(), request('id'));

View File

@ -44,8 +44,9 @@ class CartRuleCouponController extends Controller
foreach ($couponIds as $couponId) {
$coupon = $this->cartRuleCouponRepository->find($couponId);
if ($coupon)
if ($coupon) {
$this->cartRuleCouponRepository->delete($couponId);
}
}
session()->flash('success', trans('admin::app.promotions.cart-rules.mass-delete-success'));

View File

@ -76,8 +76,9 @@ class Order
*/
public function manageCartRule($order)
{
if (! $order->discount_amount)
if (! $order->discount_amount) {
return;
}
$cartRuleIds = explode(',', $order->applied_cart_rule_ids);
@ -86,16 +87,18 @@ class Order
foreach ($cartRuleIds as $ruleId) {
$rule = $this->cartRuleRepository->find($ruleId);
if (! $rule)
if (! $rule) {
continue;
}
$rule->update(['times_used' => $rule->times_used + 1]);
if (! $order->customer_id)
if (! $order->customer_id) {
continue;
}
$ruleCustomer = $this->cartRuleCustomerRepository->findOneWhere([
'customer_id' => $order->customer_id,
'customer_id' => $order->customer_id,
'cart_rule_id' => $ruleId
]);
@ -103,15 +106,16 @@ class Order
$this->cartRuleCustomerRepository->update(['times_used' => $ruleCustomer->times_used + 1], $ruleCustomer->id);
} else {
$this->cartRuleCustomerRepository->create([
'customer_id' => $order->customer_id,
'customer_id' => $order->customer_id,
'cart_rule_id' => $ruleId,
'times_used' => 1
'times_used' => 1
]);
}
}
if (! $order->coupon_code)
if (! $order->coupon_code) {
return;
}
$coupon = $this->cartRuleCouponRepository->findOneByField('code', $order->coupon_code);
@ -120,7 +124,7 @@ class Order
if ($order->customer_id) {
$couponUsage = $this->cartRuleCouponUsageRepository->findOneWhere([
'customer_id' => $order->customer_id,
'customer_id' => $order->customer_id,
'cart_rule_coupon_id' => $coupon->id
]);
@ -128,9 +132,9 @@ class Order
$this->cartRuleCouponUsageRepository->update(['times_used' => $couponUsage->times_used + 1], $couponUsage->id);
} else {
$this->cartRuleCouponUsageRepository->create([
'customer_id' => $order->customer_id,
'customer_id' => $order->customer_id,
'cart_rule_coupon_id' => $coupon->id,
'times_used' => 1
'times_used' => 1
]);
}
}

View File

@ -58,8 +58,9 @@ class CartRule extends Model implements CartRuleContract
{
$coupon = $this->coupon_code()->first();
if (! $coupon)
if (! $coupon) {
return;
}
return $coupon->code;
}

View File

@ -18,7 +18,7 @@ class CartRuleCouponRepository extends Repository
protected $charsets = [
'alphanumeric' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
'alphabetical' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'numeric' => '0123456789'
'numeric' => '0123456789'
];
/**
@ -44,12 +44,12 @@ class CartRuleCouponRepository extends Repository
for ($i = 0; $i < $data['coupon_qty']; $i++) {
parent::create([
'cart_rule_id' => $cartRuleId,
'code' => $data['code_prefix'] . $this->getRandomString($data['code_format'], $data['code_length']) . $data['code_suffix'],
'usage_limit' => $cartRule->uses_per_coupon ?? 0,
'cart_rule_id' => $cartRuleId,
'code' => $data['code_prefix'] . $this->getRandomString($data['code_format'], $data['code_length']) . $data['code_suffix'],
'usage_limit' => $cartRule->uses_per_coupon ?? 0,
'usage_per_customer' => $cartRule->usage_per_customer ?? 0,
'is_primary' => 0,
'expired_at' => $cartRule->ends_till ?: null
'is_primary' => 0,
'expired_at' => $cartRule->ends_till ?: null
]);
}
}

View File

@ -139,12 +139,12 @@ class CartRuleRepository extends Repository
if ($data['coupon_type'] && ! $data['use_auto_generation']) {
$this->cartRuleCouponRepository->create([
'cart_rule_id' => $cartRule->id,
'code' => $data['coupon_code'],
'usage_limit' => $data['usage_per_customer'] ?? 0,
'cart_rule_id' => $cartRule->id,
'code' => $data['coupon_code'],
'usage_limit' => $data['usage_per_customer'] ?? 0,
'usage_per_customer' => $data['usage_per_customer'] ?? 0,
'is_primary' => 1,
'expired_at' => $data['ends_till'] ?: null
'is_primary' => 1,
'expired_at' => $data['ends_till'] ?: null
]);
}
@ -181,28 +181,28 @@ class CartRuleRepository extends Repository
if ($cartRuleCoupon) {
$this->cartRuleCouponRepository->update([
'code' => $data['coupon_code'],
'usage_limit' => $data['uses_per_coupon'] ?? 0,
'code' => $data['coupon_code'],
'usage_limit' => $data['uses_per_coupon'] ?? 0,
'usage_per_customer' => $data['usage_per_customer'] ?? 0,
'expired_at' => $data['ends_till'] ?: null
'expired_at' => $data['ends_till'] ?: null
], $cartRuleCoupon->id);
} else {
$this->cartRuleCouponRepository->create([
'cart_rule_id' => $cartRule->id,
'code' => $data['coupon_code'],
'usage_limit' => $data['uses_per_coupon'] ?? 0,
'cart_rule_id' => $cartRule->id,
'code' => $data['coupon_code'],
'usage_limit' => $data['uses_per_coupon'] ?? 0,
'usage_per_customer' => $data['usage_per_customer'] ?? 0,
'is_primary' => 1,
'expired_at' => $data['ends_till'] ?: null
'is_primary' => 1,
'expired_at' => $data['ends_till'] ?: null
]);
}
} else {
$this->cartRuleCouponRepository->deleteWhere(['is_primary' => 1, 'cart_rule_id' => $cartRule->id]);
$this->cartRuleCouponRepository->getModel()->where('cart_rule_id', $cartRule->id)->update([
'usage_limit' => $data['uses_per_coupon'] ?? 0,
'usage_limit' => $data['uses_per_coupon'] ?? 0,
'usage_per_customer' => $data['usage_per_customer'] ?? 0,
'expired_at' => $data['ends_till'] ?: null
'expired_at' => $data['ends_till'] ?: null
]);
}
} else {
@ -221,88 +221,88 @@ class CartRuleRepository extends Repository
{
$attributes = [
[
'key' => 'cart',
'label' => trans('admin::app.promotions.cart-rules.cart-attribute'),
'key' => 'cart',
'label' => trans('admin::app.promotions.cart-rules.cart-attribute'),
'children' => [
[
'key' => 'cart|base_sub_total',
'type' => 'price',
'key' => 'cart|base_sub_total',
'type' => 'price',
'label' => trans('admin::app.promotions.cart-rules.subtotal')
], [
'key' => 'cart|items_qty',
'type' => 'integer',
'key' => 'cart|items_qty',
'type' => 'integer',
'label' => trans('admin::app.promotions.cart-rules.total-items-qty')
], [
'key' => 'cart|payment_method',
'type' => 'select',
'key' => 'cart|payment_method',
'type' => 'select',
'options' => $this->getPaymentMethods(),
'label' => trans('admin::app.promotions.cart-rules.payment-method')
'label' => trans('admin::app.promotions.cart-rules.payment-method')
], [
'key' => 'cart|shipping_method',
'type' => 'select',
'key' => 'cart|shipping_method',
'type' => 'select',
'options' => $this->getShippingMethods(),
'label' => trans('admin::app.promotions.cart-rules.shipping-method')
'label' => trans('admin::app.promotions.cart-rules.shipping-method')
], [
'key' => 'cart|postcode',
'type' => 'text',
'key' => 'cart|postcode',
'type' => 'text',
'label' => trans('admin::app.promotions.cart-rules.shipping-postcode')
], [
'key' => 'cart|state',
'type' => 'select',
'key' => 'cart|state',
'type' => 'select',
'options' => $this->groupedStatesByCountries(),
'label' => trans('admin::app.promotions.cart-rules.shipping-state')
'label' => trans('admin::app.promotions.cart-rules.shipping-state')
], [
'key' => 'cart|country',
'type' => 'select',
'key' => 'cart|country',
'type' => 'select',
'options' => $this->getCountries(),
'label' => trans('admin::app.promotions.cart-rules.shipping-country')
'label' => trans('admin::app.promotions.cart-rules.shipping-country')
]
]
], [
'key' => 'cart_item',
'label' => trans('admin::app.promotions.cart-rules.cart-item-attribute'),
'key' => 'cart_item',
'label' => trans('admin::app.promotions.cart-rules.cart-item-attribute'),
'children' => [
[
'key' => 'cart_item|base_price',
'type' => 'price',
'key' => 'cart_item|base_price',
'type' => 'price',
'label' => trans('admin::app.promotions.cart-rules.price-in-cart')
], [
'key' => 'cart_item|quantity',
'type' => 'integer',
'key' => 'cart_item|quantity',
'type' => 'integer',
'label' => trans('admin::app.promotions.cart-rules.qty-in-cart')
], [
'key' => 'cart_item|base_total_weight',
'type' => 'decimal',
'key' => 'cart_item|base_total_weight',
'type' => 'decimal',
'label' => trans('admin::app.promotions.cart-rules.total-weight')
], [
'key' => 'cart_item|base_total',
'type' => 'price',
'key' => 'cart_item|base_total',
'type' => 'price',
'label' => trans('admin::app.promotions.cart-rules.subtotal')
]
]
], [
'key' => 'product',
'label' => trans('admin::app.promotions.cart-rules.product-attribute'),
'key' => 'product',
'label' => trans('admin::app.promotions.cart-rules.product-attribute'),
'children' => [
[
'key' => 'product|category_ids',
'type' => 'multiselect',
'label' => trans('admin::app.promotions.cart-rules.categories'),
'key' => 'product|category_ids',
'type' => 'multiselect',
'label' => trans('admin::app.promotions.cart-rules.categories'),
'options' => $categories = $this->categoryRepository->getCategoryTree()
], [
'key' => 'product|children::category_ids',
'type' => 'multiselect',
'label' => trans('admin::app.promotions.cart-rules.children-categories'),
'key' => 'product|children::category_ids',
'type' => 'multiselect',
'label' => trans('admin::app.promotions.cart-rules.children-categories'),
'options' => $categories
], [
'key' => 'product|parent::category_ids',
'type' => 'multiselect',
'label' => trans('admin::app.promotions.cart-rules.parent-categories'),
'key' => 'product|parent::category_ids',
'type' => 'multiselect',
'label' => trans('admin::app.promotions.cart-rules.parent-categories'),
'options' => $categories
], [
'key' => 'product|attribute_family_id',
'type' => 'select',
'label' => trans('admin::app.promotions.cart-rules.attribute_family'),
'key' => 'product|attribute_family_id',
'type' => 'select',
'label' => trans('admin::app.promotions.cart-rules.attribute_family'),
'options' => $this->getAttributeFamilies()
]
]
@ -318,30 +318,32 @@ class CartRuleRepository extends Repository
$options = $attribute->options;
}
if ($attribute->validation == 'decimal')
if ($attribute->validation == 'decimal') {
$attributeType = 'decimal';
}
if ($attribute->validation == 'numeric')
if ($attribute->validation == 'numeric') {
$attributeType = 'integer';
}
$attributes[2]['children'][] = [
'key' => 'product|' . $attribute->code,
'type' => $attribute->type,
'label' => $attribute->name,
'key' => 'product|' . $attribute->code,
'type' => $attribute->type,
'label' => $attribute->name,
'options' => $options
];
$attributes[2]['children'][] = [
'key' => 'product|children::' . $attribute->code,
'type' => $attribute->type,
'label' => trans('admin::app.promotions.cart-rules.attribute-name-children-only', ['attribute_name' => $attribute->name]),
'key' => 'product|children::' . $attribute->code,
'type' => $attribute->type,
'label' => trans('admin::app.promotions.cart-rules.attribute-name-children-only', ['attribute_name' => $attribute->name]),
'options' => $options
];
$attributes[2]['children'][] = [
'key' => 'product|parent::' . $attribute->code,
'type' => $attribute->type,
'label' => trans('admin::app.promotions.cart-rules.attribute-name-parent-only', ['attribute_name' => $attribute->name]),
'key' => 'product|parent::' . $attribute->code,
'type' => $attribute->type,
'label' => trans('admin::app.promotions.cart-rules.attribute-name-parent-only', ['attribute_name' => $attribute->name]),
'options' => $options
];
}
@ -362,7 +364,7 @@ class CartRuleRepository extends Repository
$object = app($paymentMethod['class']);
$methods[] = [
'id' => $object->getCode(),
'id' => $object->getCode(),
'admin_name' => $object->getTitle()
];
}
@ -383,7 +385,7 @@ class CartRuleRepository extends Repository
$object = app($shippingMethod['class']);
$methods[] = [
'id' => $object->getCode(),
'id' => $object->getCode(),
'admin_name' => $object->getTitle()
];
}
@ -402,7 +404,7 @@ class CartRuleRepository extends Repository
foreach ($this->taxCategoryRepository->all() as $taxCategory) {
$taxCategories[] = [
'id' => $taxCategory->id,
'id' => $taxCategory->id,
'admin_name' => $taxCategory->name,
];
}
@ -421,7 +423,7 @@ class CartRuleRepository extends Repository
foreach ($this->attributeFamilyRepository->all() as $attributeFamily) {
$attributeFamilies[] = [
'id' => $attributeFamily->id,
'id' => $attributeFamily->id,
'admin_name' => $attributeFamily->name,
];
}
@ -440,7 +442,7 @@ class CartRuleRepository extends Repository
foreach ($this->countryRepository->all() as $country) {
$countries[] = [
'id' => $country->code,
'id' => $country->code,
'admin_name' => $country->name,
];
}
@ -463,13 +465,14 @@ class CartRuleRepository extends Repository
['code', 'default_name as admin_name']
)->toArray();
if (! count($countryStates))
if (! count($countryStates)) {
continue;
}
$collection[] = [
'id' => $country->code,
'id' => $country->code,
'admin_name' => $country->name,
'states' => $countryStates
'states' => $countryStates
];
}