Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/dont caulculate discount twice #212

Merged
merged 9 commits into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,219 changes: 622 additions & 597 deletions build/logs/clover.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function tax($amountNotTaxable = 0)
return $totalTax - $amountNotTaxable;
}

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

/**
Expand Down
8 changes: 6 additions & 2 deletions src/LaraCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,13 +569,17 @@ public function taxTotal($format = true, $withFees = true)
$totalDiscount = $this->totalDiscount(false, false);

if ($this->count() != 0) {
/**
* @var
* @var CartItem $item
*/
foreach ($this->getItems() as $index => $item) {
if ($discounted >= $totalDiscount) {
$totalTax += $item->tax();
} else {
$itemPrice = $item->subTotal(false);
$itemPrice = $item->subTotal(false, config('laracart.discountTaxable', false));
if (($discounted + $itemPrice) > $totalDiscount) {
$totalTax += config('laracart.discountTaxable', true) ? $item->tax() : $item->tax($totalDiscount - $discounted);
$totalTax += config('laracart.discountTaxable', false) ? $item->tax() : $item->tax($totalDiscount - $discounted);
}

$discounted += $itemPrice;
Expand Down
8 changes: 6 additions & 2 deletions src/Traits/CouponTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,18 @@ public function checkValidTimes(Carbon $startDate, Carbon $endDate, $throwErrors
* Sets a discount to an item with what code was used and the discount amount.
*
* @param CartItem $item
* @param $discountAmount
*
* @throws InvalidPrice
*/
public function setDiscountOnItem(CartItem $item)
public function setDiscountOnItem(CartItem $item, $discountAmount)
{
if (!is_numeric($discountAmount)) {
throw new InvalidPrice('You must use a discount amount.');
}
$this->appliedToCart = false;
$item->code = $this->code;
$item->discount = $this->discount();
$item->discount = $discountAmount;
$item->couponInfo = $this->options;
}
}
11 changes: 0 additions & 11 deletions src/config/laracart.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,6 @@
*/
'multiple_coupons' => false,

/*
|
| **** DEPRECATED IN 1.3 ****
|
|--------------------------------------------------------------------------
| Applied message when using getMessage on a coupon
|--------------------------------------------------------------------------
|
*/
'coupon_applied_message' => 'Coupon Applied',

/*
|--------------------------------------------------------------------------
| The default item model for your relations
Expand Down
22 changes: 11 additions & 11 deletions tests/CouponsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,33 +191,33 @@ public function testCheckValidTimes()
*/
public function testSetDiscountOnItem()
{
$item = $this->addItem(2, 30);
$item = $this->addItem(1, 100);

$fixedCoupon = new LukePOLO\LaraCart\Coupons\Fixed('10OFF', 10);

$this->laracart->addCoupon($fixedCoupon);

$coupon = $this->laracart->findCoupon('10OFF');

$this->assertEquals('53.50', $this->laracart->total(false));
$this->assertEquals(90 * 1.07, $this->laracart->total(false));

$coupon->setDiscountOnItem($item, 10.00);

$this->assertEquals('10OFF', $item->code);

$item = $this->addItem();
$this->assertEquals(90 * 1.07, $this->laracart->total(false));

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

$this->assertEquals('54.57', $this->laracart->total(false));
$this->assertEquals(90 + (100 * .07), $this->laracart->total(false));

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

$this->assertEquals('55.27', $this->laracart->total(false));
$this->assertEquals(90 * 1.07, $this->laracart->total(false));

$this->laracart->removeCoupon('10OFF');

$this->assertEquals('65.27', $this->laracart->total(false));
$this->assertEquals(107, $this->laracart->total(false));

$this->assertNull($item->code);
$this->assertEquals(0, $item->discount);
Expand All @@ -237,7 +237,7 @@ public function testDiscountTotals()

$coupon = $this->laracart->findCoupon('10OFF');

$coupon->setDiscountOnItem($item);
$coupon->setDiscountOnItem($item, 10.00);

$this->assertEquals('10OFF', $item->code);

Expand All @@ -261,7 +261,7 @@ public function testCouponsNotTaxableItem()
$this->assertEquals('20%', $percentCoupon->displayValue());
$this->assertEquals('0.20', $percentCoupon->discount());

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

$this->assertEquals(0, $this->laracart->taxTotal(false));

Expand All @@ -284,11 +284,11 @@ public function testCouponsTaxableItem()
$this->assertEquals('20%', $percentCoupon->displayValue());
$this->assertEquals('0.20', $percentCoupon->discount());

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

$this->assertEquals('0.06', $this->laracart->taxTotal(false));
$this->assertEquals('0.07', $this->laracart->taxTotal(false));

$this->assertEquals('0.86', $this->laracart->total(false));
$this->assertEquals('0.87', $this->laracart->total(false));
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/TotalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,22 @@ public function testTaxTotalWithDiscounts()

$this->laracart->addCoupon($coupon);
}

public function testDoubleDiscounts()
{
$item = $this->laracart->add(1, 'Test Product', 1, 100, ['tax' => 0.21]);

$coupon = new LukePOLO\LaraCart\Coupons\Percentage('test', 0.05, [
'name' => '5% off',
'description' => '5% off test',
]);

$this->laracart->addCoupon($coupon);
$coupon->setDiscountOnItem($item, $item->price(false) * $coupon->value);

$this->assertEquals(95, $this->laracart->subTotal(false));
$this->assertEquals(5, $this->laracart->totalDiscount(false));
$this->assertEquals(19.95, $this->laracart->taxTotal(false));
$this->assertEquals(114.95, $this->laracart->total(false));
}
}