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; protected $rule;
/**
* Empty collection instance for keeping final list of items
*/
protected $matchedItems;
public function __construct()
{
$this->matchedItems = collect();
}
abstract public function calculate($rule); abstract public function calculate($rule);
/** /**
* To find the eligble items for the current rule, * To find the eligble items for the current rule,
* *
* @param CartRule $rule * @param CartRule $rule
* @return Collection $matchedItems * @return Collection $eligibleItems
*/ */
public function getEligibleItems() public function getEligibleItems()
{ {
@ -35,13 +25,13 @@ abstract class Action
$items = $cart->items()->get(); $items = $cart->items()->get();
$eligibleItems = collect();
if ($this->rule->action_type == 'whole_cart_to_percent' || $this->rule->action_type == 'whole_cart_to_fixed_amount') 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) { if (! $rule->uses_attribute_conditions) {
$this->matchedItems = $items; return $items;
return $this->matchedItems;
} else { } else {
$productIDs = explode(',', $rule->product_ids); $productIDs = explode(',', $rule->product_ids);
@ -51,15 +41,15 @@ abstract class Action
foreach ($childrens as $children) { foreach ($childrens as $children) {
if ($children->product_id == $productID) if ($children->product_id == $productID)
$this->matchedItems->push($children); $eligibleItems->push($item);
} }
if ($item->product_id == $productID) 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) public function __construct($rule)
{ {
parent::__construct();
$this->rule = $rule; $this->rule = $rule;
} }
@ -24,9 +22,12 @@ class FixedAmount extends Action
foreach ($eligibleItems as $item) { foreach ($eligibleItems as $item) {
$report = array(); $report = array();
$report['item_id'] = $item->id; $report['item_id'] = $item->id;
$report['child_items'] = collect(); $report['child_items'] = collect();
$perItemDiscount = $rule->disc_amount / $eligibleItems->count();
$itemPrice = $item->base_price; $itemPrice = $item->base_price;
$itemQuantity = $item->quantity; $itemQuantity = $item->quantity;
@ -54,7 +55,7 @@ class FixedAmount extends Action
$itemDiscount = $childBaseTotal / ($item->base_total / 100); $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; $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; $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; $discount = $discount <= $itemPrice * $discQuantity ? $discount : $itemPrice * $discQuantity;
$report['discount'] = $discount; $report['discount'] = $discount;

View File

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