Skip to content

Commit

Permalink
Merge pull request #19 from lukepolo/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
lukepolo committed Nov 8, 2015
2 parents 3fcb291 + f1dbd92 commit 491aa78
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 117 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ $coupon = new \LukePOLO\LaraCart\Coupons\Fixed($coupon->CouponCode, $coupon->Cou
'description' => $coupon->Description
]);

LaraCart::applyCoupon($coupon);
LaraCart::addCoupon($coupon);

// To remove
LaraCart::removeCoupon($code);
Expand Down
6 changes: 6 additions & 0 deletions scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tools:
external_code_coverage: false
checks:
php:
code_rating: true
duplication: true
69 changes: 16 additions & 53 deletions src/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use LukePOLO\LaraCart\Exceptions\InvalidPrice;
use LukePOLO\LaraCart\Exceptions\InvalidQuantity;
use LukePOLO\LaraCart\Exceptions\UnknownItemProperty;

/**
* Class CartItem
Expand Down Expand Up @@ -58,7 +57,7 @@ public function __construct($id, $name, $qty, $price, $options = [], $lineItem =
*/
public function __get($option)
{
if ($option == 'price') {
if ($option == LaraCart::PRICE) {
return $this->getPrice();
} else {
return array_get($this->options, $option);
Expand Down Expand Up @@ -112,22 +111,16 @@ public function __isset($option)
*/
public function generateHash($force = false)
{
if ($force === true) {
$this->itemHash = null;
}

if ($this->lineItem === false) {
$this->itemHash = null;

$cartItemArray = (array)$this;

if (empty($cartItemArray['options']) === false) {
ksort($cartItemArray['options']);
}
ksort($cartItemArray['options']);

$this->itemHash = app('generateCartHash', $cartItemArray);
} elseif (empty($this->itemHash) === true) {
$this->itemHash = app('generateRandomCartItemHash');
$this->itemHash = app(LaraCart::HASH, $cartItemArray);
} elseif ($force || empty($this->itemHash) === true) {
$this->itemHash = app(LaraCart::RANHASH);
}
return $this->itemHash;
}
Expand Down Expand Up @@ -182,27 +175,14 @@ public function getPrice($tax = false, $format = true)
$price = $this->price;

foreach ($this->subItems as $subItem) {

if (isset($subItem->price)) {
$price += $subItem->price;
}

if (empty($subItem->items) === false) {
foreach ($subItem->items as $item) {
$price += $item->getPrice($tax, false);
}
}
$price += $subItem->getPrice(false);
}

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

if ($format) {
return \App::make('laracart')->formatMoney($price, $this->locale, $this->internationalFormat);
} else {
return $price;
}
return \App::make(LaraCart::SERVICE)->formatMoney($price, $this->locale, $this->internationalFormat, $format);
}

/**
Expand All @@ -211,30 +191,26 @@ public function getPrice($tax = false, $format = true)
* @param $key
* @param $value
*
* @throws InvalidQuantity | InvalidPrice | UnknownItemProperty
* @throws InvalidQuantity | InvalidPrice
*
* @return string $itemHash
*/
public function update($key, $value)
{
switch ($key) {
case 'qty':
case LaraCart::QTY:
if (is_int($value) === false) {
throw new InvalidQuantity();
}
break;
case 'price':
case LaraCart::PRICE:
if (is_numeric($value) === false || preg_match('/\.(\d){3}/', $value)) {
throw new InvalidPrice();
}
break;
}

if (isset($this->$key) === true) {
$this->$key = $value;
} else {
throw new UnknownItemProperty();
}
$this->$key = $value;

return $this->generateHash();
}
Expand All @@ -255,12 +231,7 @@ public function subTotal($tax = false, $format = true, $withDiscount = true)
$total -= $this->getDiscount(false);
}

if ($format) {

return \App::make('laracart')->formatMoney($total, $this->locale, $this->internationalFormat);
} else {
return $total;
}
return \App::make(LaraCart::SERVICE)->formatMoney($total, $this->locale, $this->internationalFormat, $format);
}


Expand All @@ -277,19 +248,15 @@ public function subItemsTotal($tax = false, $format = true)
$total = 0;
foreach ($this->subItems as $item) {
if (isset($item->price)) {
$total += array_get($item->options, 'price');
$total += array_get($item->options, LaraCart::PRICE);
}
}

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

if ($format) {
return \App::make('laracart')->formatMoney($total, $this->locale, $this->internationalFormat);
} else {
return $total;
}
return \App::make(LaraCart::SERVICE)->formatMoney($total, $this->locale, $this->internationalFormat, $format);
}

/**
Expand All @@ -302,16 +269,12 @@ public function subItemsTotal($tax = false, $format = true)
public function getDiscount($format = true)
{
// TODO - move to main laracart should not be in here
if (\App::make('laracart')->findCoupon($this->code)) {
if (\App::make(LaraCart::SERVICE)->findCoupon($this->code)) {
$discount = $this->discount;
} else {
$discount = 0;
}

if ($format) {
return \App::make('laracart')->formatMoney($discount, $this->locale, $this->internationalFormat);
} else {
return $discount;
}
return \App::make(LaraCart::SERVICE)->formatMoney($discount, $this->locale, $this->internationalFormat, $format);
}
}
26 changes: 17 additions & 9 deletions src/CartSubItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@
*/
class CartSubItem
{
private $itemHash;

public $locale;
public $price;
public $price = 0;
public $items = [];
public $options = [];
public $internationalFormat;
private $itemHash;

/**
* @param $options
*/
public function __construct($options)
{
$this->itemHash = app('generateCartHash', $options);
if (isset($options['price']) === true) {
$this->price = floatval($options['price']);
$this->itemHash = app(LaraCart::HASH, $options);
if (isset($options[LaraCart::PRICE]) === true) {
$this->price = floatval($options[LaraCart::PRICE]);
}
$this->options = $options;
}
Expand Down Expand Up @@ -52,13 +54,19 @@ public function getHash()
/**
* Gets the formatted price
*
* @return string
* @param bool|true $format
*
* @return mixed
*/
public function getPrice()
public function getPrice($format = true)
{
if(!empty($this->price)) {
return \App::make('laracart')->formatMoney($this->price, $this->locale, $this->internationalFormat);
$price = $this->price;

foreach ($this->items as $item) {
$price += $item->getPrice(false, false);
}

return \App::make(LaraCart::SERVICE)->formatMoney($price, $this->locale, $this->internationalFormat, $format);
}

/**
Expand Down
62 changes: 33 additions & 29 deletions src/LaraCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
*/
class LaraCart implements LaraCartContract
{
CONST SERVICE = 'laracart';
CONST QTY = 'qty';
CONST PRICE = 'price';
CONST HASH = 'generateCartHash';
CONST RANHASH = 'generateRandomCartItemHash';

public $cart;

/**
Expand Down Expand Up @@ -73,25 +79,23 @@ public function update()
* @param $number
* @param $locale
* @param $internationalFormat
* @param $format
*
* @return string
*/
public function formatMoney($number, $locale = null, $internationalFormat = null)
public function formatMoney($number, $locale = null, $internationalFormat = null, $format = true)
{
if (empty($locale) === true) {
$locale = config('laracart.locale', 'en_US.UTF-8');
}
if ($format) {

if (empty($internationalFormat) === true) {
$internationalFormat = config('laracart.international_format', false);
}
setlocale(LC_MONETARY, empty($locale) ? $locale : config('laracart.locale', 'en_US.UTF-8'));

setlocale(LC_MONETARY, $locale);
if (empty($internationalFormat) === true) {
$internationalFormat = config('laracart.international_format', false);
}

if ($internationalFormat) {
return money_format('%i', $number);
return money_format($internationalFormat ? '%i' : '%n', $number);
} else {
return money_format('%n', $number);
return number_format($number, 2);
}
}

Expand Down Expand Up @@ -354,7 +358,7 @@ public function findCoupon($code)
*
* @param CouponContract $coupon
*/
public function applyCoupon(CouponContract $coupon)
public function addCoupon(CouponContract $coupon)
{
$this->cart->coupons[$coupon->code] = $coupon;

Expand Down Expand Up @@ -426,11 +430,11 @@ public function removeFee($name)
/**
* Gets all the fee totals
*
* @param bool|true $formatted
* @param bool|true $format
*
* @return string
*/
public function getFeeTotals($formatted = true)
public function feeTotals($format = true)
{
$feeTotal = 0;

Expand All @@ -441,7 +445,7 @@ public function getFeeTotals($formatted = true)
}
}

if ($formatted) {
if ($format) {
return $this->formatMoney($feeTotal);
} else {
return number_format($feeTotal, 2);
Expand All @@ -451,18 +455,18 @@ public function getFeeTotals($formatted = true)
/**
* Gets the total amount discounted
*
* @param bool|true $formatted
* @param bool|true $format
*
* @return int|string
*/
public function getTotalDiscount($formatted = true)
public function totalDiscount($format = true)
{
$total = 0;
foreach ($this->cart->coupons as $coupon) {
$total += $coupon->discount();
}

if ($formatted) {
if ($format) {
return $this->formatMoney($total);
} else {
return $total;
Expand All @@ -473,15 +477,15 @@ public function getTotalDiscount($formatted = true)
/**
* Gets the total tax for the cart
*
* @param bool|true $formatted
* @param bool|true $format
*
* @return string
*/
public function taxTotal($formatted = true)
public function taxTotal($format = true)
{
$totalTax = $this->total(false, false) - $this->subTotal(false, false, false) - $this->getFeeTotals(false);
$totalTax = $this->total(false, false) - $this->subTotal(false, false, false) - $this->feeTotals(false);

if ($formatted) {
if ($format) {
return $this->formatMoney($totalTax);
} else {
return number_format($totalTax, 2);
Expand All @@ -492,12 +496,12 @@ public function taxTotal($formatted = true)
* Gets the subtotal of the cart with or without tax
*
* @param bool|false $tax
* @param bool|true $formatted
* @param bool|true $format
* @param bool|true $withDiscount
*
* @return string
*/
public function subTotal($tax = false, $formatted = true, $withDiscount = true)
public function subTotal($tax = false, $format = true, $withDiscount = true)
{
$total = 0;
if ($this->count() != 0) {
Expand All @@ -506,7 +510,7 @@ public function subTotal($tax = false, $formatted = true, $withDiscount = true)
}
}

if ($formatted) {
if ($format) {
return $this->formatMoney($total);
} else {
return number_format($total, 2);
Expand All @@ -517,15 +521,15 @@ public function subTotal($tax = false, $formatted = true, $withDiscount = true)
/**
* Gets the total of the cart with or without tax
*
* @param bool|true $formatted
* @param bool|true $format
* @param bool|true $withDiscount
* @return string
*/
public function total($formatted = true, $withDiscount = true)
public function total($format = true, $withDiscount = true)
{
$total = $this->subTotal(true, false, $withDiscount) + $this->getFeeTotals(false);
$total = $this->subTotal(true, false, $withDiscount) + $this->feeTotals(false);

if ($formatted) {
if ($format) {
return $this->formatMoney($total);
} else {
return number_format($total, 2);
Expand Down
Loading

0 comments on commit 491aa78

Please sign in to comment.