mivita/app/Services/Yard.php
2019-01-07 00:15:41 +01:00

187 lines
No EOL
5.6 KiB
PHP

<?php
namespace App\Services;
use App\Models\ShippingCountry;
use \Gloudemans\Shoppingcart\Cart;
use Gloudemans\Shoppingcart\CartItem;
use Illuminate\Session\SessionManager;
use Illuminate\Contracts\Events\Dispatcher;
class Yard extends Cart
{
private $shipping = 0;
private $shipping_country_id = 0;
public function __construct(SessionManager $session, Dispatcher $events)
{
parent::__construct($session, $events);
}
public static function getTaxRate()
{
return config('cart.tax');
}
/**
* @param $shipping
*/
public function setShipping($shipping)
{
$this->shipping = floatval($shipping); ;
}
public function setShippingCountry($shipping_country_id)
{
$this->shipping_country_id = $shipping_country_id ;
if($this->shipping_country_id > 0){
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
$shipping = $shippingCountry->shipping;
$price = $shipping->prices->first();
if($price){
$this->setShipping($price->price);
}
}
}
public function getShippingCountry()
{
return $this->shipping_country_id;
}
/**
* @param null $decimals
* @param null $decimalPoint
* @param null $thousandSeperator
* @return string
*/
public function shipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->shipping, $decimals, $decimalPoint, $thousandSeperator);
}
//
private function shippingTax($taxRate = -19){
return ($this->shipping * ($taxRate / 100));
}
private function shippingSub($taxRate){
return ($this->shipping + $this->shippingTax($taxRate));
}
public function subtotalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$taxRate =config('cart.tax');
$total = floatval($this->total(2, '.', '')) + $this->shipping;
$totalTax = $total/ (100 + $taxRate) * $taxRate;
return $this->numberFormat(($total - $totalTax), $decimals, $decimalPoint, $thousandSeperator);
}
public function taxWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$taxRate =config('cart.tax');
$total = floatval($this->total(2, '.', '')) + $this->shipping;
$totalTax = $total/ (100 + $taxRate) * $taxRate;
return $this->numberFormat($totalTax, $decimals, $decimalPoint, $thousandSeperator);
}
public function totalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$total = floatval($this->total(2, '.', '')) + $this->shipping;
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Get the total price of the items in the cart.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
* @return string
*/
public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();
$total = $content->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->qty * $cartItem->price);
}, 0);
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Get the total tax of the items in the cart.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
* @return float
*/
public function tax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();
$tax = $content->reduce(function ($tax, CartItem $cartItem) {
$priceTax = $cartItem->price / (100 + $cartItem->taxRate) * $cartItem->taxRate;
return $tax + ($cartItem->qty * $priceTax);
}, 0);
return $this->numberFormat($tax, $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Get the subtotal (total - tax) of the items in the cart.
*
* @param int $decimals
* @param string $decimalPoint
* @param string $thousandSeperator
* @return float
*/
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$content = $this->getContent();
$subTotal = $content->reduce(function ($subTotal, CartItem $cartItem) {
$priceTax = $cartItem->price / (100 + $cartItem->taxRate) * $cartItem->taxRate;
return $subTotal + ($cartItem->qty * ($cartItem->price - $priceTax));
}, 0);
return $this->numberFormat($subTotal, $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Get the Formated number
*
* @param $value
* @param $decimals
* @param $decimalPoint
* @param $thousandSeperator
* @return string
*/
protected function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
{
if(is_null($decimals)){
$decimals = is_null(config('cart.format.decimals')) ? 2 : config('cart.format.decimals');
}
if(is_null($decimalPoint)){
$decimalPoint = is_null(config('cart.format.decimal_point')) ? '.' : config('cart.format.decimal_point');
}
if(is_null($thousandSeperator)){
$thousandSeperator = is_null(config('cart.format.thousand_seperator')) ? ',' : config('cart.format.thousand_seperator');
}
return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
}
}