Skip to content

Commit

Permalink
Bugfix for percentage coupon on item (#246)
Browse files Browse the repository at this point in the history
* Bugfix for percentage coupon on item
  • Loading branch information
Roywcm authored and lukepolo committed Oct 29, 2018
1 parent 20ba86c commit 6c75332
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function subTotal($format = true, $withDiscount = true, $taxedItemsOnly =
}

if ($withTax) {
$total += $this->tax(0, false);
$total += $this->tax(0, false, false, $withDiscount);
}

return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format);
Expand Down Expand Up @@ -324,9 +324,10 @@ public function addCoupon(CouponContract $coupon)
*
* @return int|mixed
*/
public function tax($amountNotTaxable = 0, $grossTax = true, $rounded = false)
public function tax($amountNotTaxable = 0, $grossTax = true, $rounded = false, $withDiscount = true)
{
$totalDiscount = $this->getDiscount(false);
$discountTaxable = ($withDiscount) ? !config('laracart.discountTaxable', false) : false;
$totalDiscount = ($withDiscount) ? $this->getDiscount(false) : 0;

if (!$this->taxable) {
$amountNotTaxable = $this->price * $this->qty;
Expand Down Expand Up @@ -360,9 +361,9 @@ public function tax($amountNotTaxable = 0, $grossTax = true, $rounded = false)
return $totalTax - $amountNotTaxable;
}

$totalTax = $this->tax * ($this->subTotal(false, !config('laracart.discountTaxable', false), true) - $amountNotTaxable);
$totalTax = $this->tax * ($this->subTotal(false, $discountTaxable, true, false) - $amountNotTaxable);

if (config('laracart.discountsAlreadyTaxed', false)) {
if (config('laracart.discountsAlreadyTaxed', false) && $withDiscount) {
$totalTax = $totalTax - ($this->getDiscount(false) - ($this->getDiscount(false) / (1 + $this->tax)));
}

Expand Down
41 changes: 41 additions & 0 deletions tests/CouponsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,47 @@ public function testAddPercentageCoupon()
$this->assertEquals(.19, $this->laracart->taxTotal(false));
}

/**
* Test the percentage coupons on item with tax.
*/
public function testAddPercentageCouponOnTaxItem()
{
$this->app['config']->set('laracart.tax_by_item', false);
$this->app['config']->set('laracart.tax_item_before_discount', true);
$this->app['config']->set('laracart.discountsAlreadyTaxed', false);
$this->app['config']->set('laracart.discountTaxable', false);

$item = $this->addItem(1, 10);

$percentCoupon = new LukePOLO\LaraCart\Coupons\Percentage('10%OFF', '.1');

$this->laracart->addCoupon($percentCoupon);
$percentCoupon->setDiscountOnItem($item);

$this->assertEquals($percentCoupon, $this->laracart->findCoupon('10%OFF'));

$this->assertEquals('10%', $percentCoupon->displayValue());
$this->assertEquals('0.89', $percentCoupon->discount());
$this->assertEquals(9.56, $this->laracart->total(false));
$this->assertEquals(.63, $this->laracart->taxTotal(false));

$this->app['config']->set('laracart.discountTaxable', true);

$this->assertEquals('10%', $percentCoupon->displayValue());
$this->assertEquals('0.89', $percentCoupon->discount());
$this->assertEquals(9.63, $this->laracart->total(false));
$this->assertEquals(.7, $this->laracart->taxTotal(false));

$this->app['config']->set('laracart.tax_by_item', true);

$this->assertEquals('10%', $percentCoupon->displayValue());
$this->assertEquals('0.89', $percentCoupon->discount());
$this->assertEquals(9.63, $this->laracart->total(false));
$this->assertEquals(.7, $this->laracart->taxTotal(false));

$this->assertCount(1, $this->laracart->getCoupons());
}

/**
* Test the fixed coupons.
*/
Expand Down

0 comments on commit 6c75332

Please sign in to comment.