This commit is contained in:
Prashant Singh 2019-11-14 16:46:36 +05:30
parent 58a0bad4c9
commit e35d3ec1cd
3 changed files with 23 additions and 32 deletions

View File

@ -9,23 +9,13 @@ abstract class Action
*/
protected $rule;
/**
* Empty collection instance for keeping final list of items
*/
protected $matchedItems;
public function __construct()
{
$this->matchedItems = collect();
}
abstract public function calculate($rule);
/**
* To find the eligble items for the current rule,
*
* @param CartRule $rule
* @return Collection $matchedItems
* @return Collection $eligibleItems
*/
public function getEligibleItems()
{
@ -35,13 +25,13 @@ abstract class Action
$items = $cart->items()->get();
$eligibleItems = collect();
if ($this->rule->action_type == 'whole_cart_to_percent' || $this->rule->action_type == 'whole_cart_to_fixed_amount')
$this->matchedItems = $items;
$this->eligibleItems = $items;
if (! $rule->uses_attribute_conditions) {
$this->matchedItems = $items;
return $this->matchedItems;
return $items;
} else {
$productIDs = explode(',', $rule->product_ids);
@ -51,15 +41,15 @@ abstract class Action
foreach ($childrens as $children) {
if ($children->product_id == $productID)
$this->matchedItems->push($children);
$eligibleItems->push($item);
}
if ($item->product_id == $productID)
$this->matchedItems->push($item);
$eligibleItems->push($item);
}
}
return $this->matchedItems;
return $eligibleItems;
}
}

View File

@ -8,8 +8,6 @@ class FixedAmount extends Action
{
public function __construct($rule)
{
parent::__construct();
$this->rule = $rule;
}
@ -24,9 +22,12 @@ class FixedAmount extends Action
foreach ($eligibleItems as $item) {
$report = array();
$report['item_id'] = $item->id;
$report['child_items'] = collect();
$perItemDiscount = $rule->disc_amount / $eligibleItems->count();
$itemPrice = $item->base_price;
$itemQuantity = $item->quantity;
@ -54,7 +55,7 @@ class FixedAmount extends Action
$itemDiscount = $childBaseTotal / ($item->base_total / 100);
$children->discount = ($itemDiscount / 100) * $rule->disc_amount;
$children->discount = ($itemDiscount / 100) * $perItemDiscount;
$children->discount = $children->base_total > $children->discount ? $children->discount : $children->base_total;
@ -65,7 +66,7 @@ class FixedAmount extends Action
$report['product_id'] = $item->product_id;
}
$discount = round($rule->disc_amount, 4) * $discQuantity;
$discount = round($perItemDiscount, 4) * $discQuantity;
$discount = $discount <= $itemPrice * $discQuantity ? $discount : $itemPrice * $discQuantity;
$report['discount'] = $discount;

View File

@ -8,8 +8,6 @@ class PercentOfProduct extends Action
{
public function __construct($rule)
{
parent::__construct();
/**
* Setting the rule getting applied
*/
@ -34,23 +32,23 @@ class PercentOfProduct extends Action
if ($applicability) {
$eligibleItems = $this->getEligibleItems();
$applicableDiscount = \Cart::getCart()->base_sub_total * ($rule->disc_amount / 100);
foreach ($eligibleItems as $item) {
$report = array();
$report['item_id'] = $item->id;
$report['child_items'] = collect();
$perItemDiscount = $applicableDiscount / $eligibleItems->count();
$itemPrice = $item->base_price;
$itemQuantity = $item->quantity;
$discQuantity = $this->rule->disc_quantity;
$discQuantity = $rule->disc_quantity;
$discQuantity = $itemQuantity <= $discQuantity ? $itemQuantity : $discQuantity;
$discountAmount = ($this->rule->disc_amount > 100) ? 100 : $this->rule->disc_amount;
$discount = $itemPrice * ($discountAmount / 100) * $discQuantity;
$discount = $discount <= $itemPrice * $discQuantity ? $discount : $itemPrice * $discQuantity;
if ($item->product->getTypeInstance()->isComposite()) {
$isQtyZero = true;
@ -71,7 +69,7 @@ class PercentOfProduct extends Action
$itemDiscount = $childBaseTotal / ($item->base_total / 100);
$children->discount = ($itemDiscount / 100) * ($item->base_total * ($this->rule->disc_amount / 100));
$children->discount = ($itemDiscount / 100) * $perItemDiscount;
$children->discount = $children->base_total > $children->discount ? $children->discount : $children->base_total;
@ -82,8 +80,10 @@ class PercentOfProduct extends Action
$report['product_id'] = $item->product_id;
}
$report['discount'] = $discount;
$discount = round($perItemDiscount, 4) * $discQuantity;
$discount = $discount <= $itemPrice * $discQuantity ? $discount : $itemPrice * $discQuantity;
$report['discount'] = $discount;
$report['formatted_discount'] = core()->currency($discount);
$impact->push($report);