Skip to content

Commit

Permalink
Merge branch 'master' of github.com:lukepolo/laracart
Browse files Browse the repository at this point in the history
  • Loading branch information
lukepolo committed Dec 19, 2016
2 parents fb4605f + 42c6270 commit 46933a8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ php:
- 5.5
- 5.6
- 7.0

- 7.1

addons:
code_climate:
repo_token: 653d80c9cb760f12e0ea14fc523f37cc07d95e69559913242810859ac65c1feb
Expand Down
6 changes: 4 additions & 2 deletions src/CartFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ public function __construct($amount, $taxable = false, $options = [])
/**
* Gets the formatted amount.
*
* @param bool $format
*
* @return string
*/
public function getAmount()
public function getAmount($format = true)
{
return LaraCart::formatMoney($this->amount, $this->locale, $this->internationalFormat);
return LaraCart::formatMoney($this->amount, $this->locale, $this->internationalFormat, $format);
}
}
25 changes: 21 additions & 4 deletions src/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,20 @@ public function removeSubItem($subItemHash)
*
* @param bool $format
* @param bool $taxedItemsOnly
* @param bool $withTax
*
* @return string
*/
public function price($format = true, $taxedItemsOnly = false)
public function price($format = true, $taxedItemsOnly = false, $withTax = false)
{
$total = $this->price + $this->subItemsTotal(false, $taxedItemsOnly);

if ($withTax) {
$total += $this->tax * $total;
}

return LaraCart::formatMoney(
$this->price + $this->subItemsTotal(false, $taxedItemsOnly),
$total,
$this->locale,
$this->internationalFormat, $format
);
Expand All @@ -196,17 +203,22 @@ public function price($format = true, $taxedItemsOnly = false)
* @param bool $format
* @param bool $withDiscount
* @param bool $taxedItemsOnly
* @param bool $withTax
*
* @return string
*/
public function subTotal($format = true, $withDiscount = true, $taxedItemsOnly = false)
public function subTotal($format = true, $withDiscount = true, $taxedItemsOnly = false, $withTax = false)
{
$total = $this->price(false, $taxedItemsOnly) * $this->qty;

if ($withDiscount) {
$total -= $this->getDiscount(false);
}

if ($withTax) {
$total += $this->tax();
}

return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format);
}

Expand All @@ -215,17 +227,22 @@ public function subTotal($format = true, $withDiscount = true, $taxedItemsOnly =
*
* @param bool $format
* @param bool $taxedItemsOnly
* @param bool $withTax
*
* @return string
*/
public function subItemsTotal($format = true, $taxedItemsOnly = false)
public function subItemsTotal($format = true, $taxedItemsOnly = false, $withTax = false)
{
$total = 0;

foreach ($this->subItems as $subItem) {
$total += $subItem->price(false, $taxedItemsOnly);
}

if ($withTax) {
$total += $this->tax * $total;
}

return LaraCart::formatMoney($total, $this->locale, $this->internationalFormat, $format);
}

Expand Down
12 changes: 10 additions & 2 deletions src/LaraCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ public function update()
$this->authManager->user()->save();
}

$this->session->reflash();

$this->session->save();

$this->events->fire('laracart.update', $this->cart);
Expand Down Expand Up @@ -583,12 +585,18 @@ public function taxTotal($format = true)
*
* @param bool $format
* @param bool $withDiscount
* @param bool $withTax
* @param bool $withFees
*
* @return string
*/
public function total($format = true, $withDiscount = true, $withTax = true)
public function total($format = true, $withDiscount = true, $withTax = true, $withFees = true)
{
$total = $this->subTotal(false) + $this->feeTotals(false);
$total = $this->subTotal(false);

if ($withFees) {
$total += $this->feeTotals(false);
}

if ($withDiscount) {
$total -= $this->totalDiscount(false);
Expand Down
2 changes: 2 additions & 0 deletions tests/ItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ public function testItemPriceAndQty()

$this->assertEquals(3, $item->qty);
$this->assertEquals(10, $item->price(false));
$this->assertEquals(10.7, $item->price(false, false, true)); // return item price with tax
$this->assertEquals(30, $item->subTotal(false));
$this->assertEquals(32.1, $item->subTotal(false, true, false, true)); // return subtotal with tax
}

/**
Expand Down
9 changes: 7 additions & 2 deletions tests/SubItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function testSubItemTotal()

$this->assertEquals('$2.50', $item->subItemsTotal());
$this->assertEquals('2.50', $item->subItemsTotal(false));
$this->assertEquals('2.68', $item->subItemsTotal(false, false, true)); // with tax
}

/**
Expand All @@ -57,9 +58,12 @@ public function testSubItemItemsTotal()
]);

$this->assertEquals(3, $item->subItemsTotal(false));
$this->assertEquals(3.21, $item->subItemsTotal(false, false, true)); // with tax

$this->assertEquals(14, $item->subTotal(false));
$this->assertEquals(14.98, $item->subTotal(false, true, false, true)); // with tax
$this->assertEquals(14, $item->price(false));
$this->assertEquals(14.98, $item->price(false, false, true)); // with tax
}

/**
Expand All @@ -84,9 +88,12 @@ public function testSubItemsSubItemsTotal()
]);

$this->assertEquals(3, $item->subItemsTotal(false));
$this->assertEquals(3.21, $item->subItemsTotal(false, false, true)); // with tax

$this->assertEquals(14, $item->subTotal(false));
$this->assertEquals(14.98, $item->subTotal(false, true, false, true)); // with tax
$this->assertEquals(14, $item->price(false));
$this->assertEquals(14.98, $item->price(false, false, true)); // with tax
}

/**
Expand All @@ -108,7 +115,6 @@ public function testAddSubItemItems()

$this->containsOnlyInstancesOf(LukePOLO\LaraCart\CartItem::class, $subItem->items);


$this->assertEquals('$12.50', $subItem->price());
}

Expand All @@ -131,7 +137,6 @@ public function testAddSubItemItemsWithQty()

$this->containsOnlyInstancesOf(LukePOLO\LaraCart\CartItem::class, $subItem->items);


$this->assertEquals('$12.50', $subItem->price());

$this->assertEquals('13.50', $this->laracart->subTotal(false));
Expand Down

0 comments on commit 46933a8

Please sign in to comment.