Homparty v1.0
This commit is contained in:
parent
ac0d5b781e
commit
c73299e52e
40 changed files with 1234 additions and 908 deletions
|
|
@ -5,6 +5,8 @@ namespace App\Services;
|
|||
|
||||
|
||||
use App\Models\Homeparty;
|
||||
use App\Models\Product;
|
||||
use App\Models\ShippingCountry;
|
||||
|
||||
class HomepartyCart
|
||||
{
|
||||
|
|
@ -26,6 +28,9 @@ class HomepartyCart
|
|||
public static $ek_price = 0;
|
||||
public static $income_price = 0;
|
||||
|
||||
private static $shipping_total = 0;
|
||||
private static $shipping_net_total = 0;
|
||||
|
||||
private static $homeparty;
|
||||
private static $userCarts = [];
|
||||
|
||||
|
|
@ -34,10 +39,14 @@ class HomepartyCart
|
|||
public static $is_bonus = false;
|
||||
public static $is_bonus_coupon = false;
|
||||
|
||||
|
||||
private static $bonus_coupon = 0;
|
||||
private static $bonus_value = 30;
|
||||
|
||||
private static $bonus_price = 0;
|
||||
private static $voucher_price = 0;
|
||||
public static $voucher_name = "";
|
||||
|
||||
|
||||
private static $bonus_diff = 0;
|
||||
private static $bonus_points_diff = 0;
|
||||
private static $bonus_coupon_fault = 0;
|
||||
|
|
@ -51,7 +60,6 @@ class HomepartyCart
|
|||
500 => 35,
|
||||
600 => 40,
|
||||
700 => 50,
|
||||
|
||||
];
|
||||
public static function calculateHomeparty(Homeparty $homeparty){
|
||||
|
||||
|
|
@ -62,7 +70,9 @@ class HomepartyCart
|
|||
}
|
||||
self::$userCarts[$homeparty_user->id] = new HomepartyUserCart($homeparty_user);
|
||||
self::addCart(self::$userCarts[$homeparty_user->id]);
|
||||
|
||||
}
|
||||
self::caluclateShipping();
|
||||
self::caluclateBonus();
|
||||
self::calculateBonusHost();
|
||||
}
|
||||
|
|
@ -80,15 +90,16 @@ class HomepartyCart
|
|||
}
|
||||
|
||||
|
||||
public static function caluclateBonus(){
|
||||
private static function caluclateBonus(){
|
||||
|
||||
if(self::$price >= 200){
|
||||
self::$is_bonus = true;
|
||||
$proportional_voucher = Product::whereIdentifier('proportional_voucher')->first();
|
||||
self::$voucher_price = $proportional_voucher->price;
|
||||
self::$voucher_name = $proportional_voucher->getLang('name');
|
||||
|
||||
//TODO get from PRODUCT
|
||||
self::$bonus_price = 11.90;
|
||||
self::$price += self::$bonus_price;
|
||||
self::$price_net += 10;
|
||||
self::$price += self::$voucher_price;
|
||||
self::$price_net += $proportional_voucher->getPriceWith(true, false);
|
||||
|
||||
$bonus_tmp = self::$price - self::$bonus_value;
|
||||
|
||||
|
|
@ -118,7 +129,7 @@ class HomepartyCart
|
|||
|
||||
}
|
||||
|
||||
public static function calculateBonusHost(){
|
||||
private static function calculateBonusHost(){
|
||||
if(self::$is_bonus){
|
||||
$bonus_total = self::$bonus_value + self::$bonus_coupon;
|
||||
$user_cart = self::getUserCart(self::$user_host_id);
|
||||
|
|
@ -133,11 +144,140 @@ class HomepartyCart
|
|||
$bonus_percent = (100/$user_cart->price*$bonus_total);
|
||||
self::$bonus_points_diff = round($user_cart->points/100*$bonus_percent);
|
||||
$user_cart->points -= self::$bonus_points_diff;
|
||||
$user_cart->price -= $bonus_total;
|
||||
|
||||
$user_cart->price -= $bonus_total;
|
||||
$user_cart->price_net -= ($bonus_total / config('app.main_tax'));
|
||||
self::$price -= $bonus_total;
|
||||
self::$price_net -= ($bonus_total / config('app.main_tax'));
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static function caluclateShipping(){
|
||||
|
||||
//weight to the orders
|
||||
//first host
|
||||
$host_user_cart = self::getUserCart(self::$user_host_id);
|
||||
$host_user_cart->shipping_weight = $host_user_cart->weight;
|
||||
|
||||
foreach (self::$homeparty->homeparty_users as $homeparty_user){
|
||||
if(!$homeparty_user->is_host){
|
||||
$user_cart = self::$userCarts[$homeparty_user->id];
|
||||
if($homeparty_user->getDelivery() === 'direct'){
|
||||
$user_cart->shipping_weight += $user_cart->weight;
|
||||
}else{
|
||||
$host_user_cart->shipping_weight += $user_cart->weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self::calculateShippingPrice();
|
||||
|
||||
}
|
||||
|
||||
private static function calculateShippingPrice(){
|
||||
|
||||
foreach (self::$homeparty->homeparty_users as $homeparty_user) {
|
||||
$shipping = $homeparty_user->getShipping();
|
||||
$user_cart = self::$userCarts[$homeparty_user->id];
|
||||
if(!$shipping){
|
||||
return;
|
||||
}
|
||||
if($user_cart->shipping_weight === 0){
|
||||
$shipping_price = $shipping->shipping_prices->first();
|
||||
$shipping_price->price = 0;
|
||||
$shipping_price->shipping_price_net = 0;
|
||||
}else{
|
||||
//sec by weight
|
||||
$shipping_price = self::shippingPriceByWeight($shipping->shipping_prices, $user_cart->shipping_weight);
|
||||
|
||||
//default
|
||||
if(!$shipping_price){
|
||||
$shipping_price = $shipping->shipping_prices->first();
|
||||
}
|
||||
}
|
||||
|
||||
if($shipping_price){
|
||||
$price = $shipping_price->price;
|
||||
$user_cart->shipping_price = $price;
|
||||
$user_cart->shipping_tax_rate = $shipping_price->tax_rate;
|
||||
$user_cart->shipping_price_net = round($price / ((100+$shipping_price->tax_rate) / 100), 2);
|
||||
$user_cart->shipping_tax = round($price / (100+$shipping_price->tax_rate) * 100, 2);
|
||||
}
|
||||
|
||||
//on the end, add prices for porto
|
||||
$user_cart->price += $user_cart->shipping_price;
|
||||
$user_cart->price_net += $user_cart->shipping_price_net;
|
||||
self::$price += $user_cart->shipping_price;
|
||||
self::$price_net += $user_cart->shipping_price_net;
|
||||
|
||||
self::$shipping_total += $user_cart->shipping_price;
|
||||
self::$shipping_net_total += $user_cart->shipping_price_net;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static function shippingPriceByWeight($prices, $weight){
|
||||
foreach ($prices as $price){
|
||||
if($price->weight_from > 0 && $price->weight_to > 0){
|
||||
if($weight >= $price->weight_from && $weight <= $price->weight_to){
|
||||
return $price;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static function store($identifier, $date){
|
||||
|
||||
$data = [
|
||||
'date' => $date,
|
||||
'identifier' => $identifier,
|
||||
'points' => self::$points,
|
||||
'price' => round(self::$price, 2),
|
||||
'price_net' => round(self::$price_net, 2),
|
||||
'ek_price' => round(self::$ek_price, 2),
|
||||
'income_price' => round(self::$income_price, 2),
|
||||
'user_host_id' => self::$user_host_id,
|
||||
'is_bonus' => self::$is_bonus,
|
||||
'is_bonus_coupon' => self::$is_bonus_coupon,
|
||||
'bonus_value' => self::$bonus_value,
|
||||
'bonus_coupon' => self::$bonus_coupon,
|
||||
'bonus_total' => self::$bonus_value + self::$bonus_coupon,
|
||||
'shipping_price' => self::$shipping_total,
|
||||
'shipping_price_net' => self::$shipping_net_total,
|
||||
'bonus_points_diff' => self::$bonus_points_diff,
|
||||
'voucher_price' => self::$voucher_price,
|
||||
'voucher_name' => self::$voucher_name,
|
||||
];
|
||||
$user_data = [];
|
||||
foreach (self::$homeparty->homeparty_users as $homeparty_user){
|
||||
|
||||
$user_cart = self::$userCarts[$homeparty_user->id];
|
||||
$user_data[$homeparty_user->id] = [
|
||||
'is_host' => $homeparty_user->is_host,
|
||||
'points' => $user_cart->points,
|
||||
'price' => round($user_cart->price, 2),
|
||||
'price_net' => round($user_cart->price_net, 2),
|
||||
'ek_price' => round($user_cart->ek_price, 2),
|
||||
'income_price' => round($user_cart->income_price, 2),
|
||||
'weight' => $user_cart->weight,
|
||||
'shipping_weight' => $user_cart->shipping_weight,
|
||||
'shipping_price' => round($user_cart->shipping_price, 2),
|
||||
'shipping_tax_rate' => round($user_cart->shipping_tax_rate, 2),
|
||||
'shipping_price_net' => round($user_cart->shipping_price_net, 2),
|
||||
'shipping_tax' => $user_cart->shipping_tax,
|
||||
];
|
||||
}
|
||||
$data['user_carts'] = $user_data;
|
||||
self::$homeparty->order = $data;
|
||||
self::$homeparty->save();
|
||||
|
||||
}
|
||||
|
||||
public static function getUserCartHost(){
|
||||
return self::getUserCart(self::$user_host_id);
|
||||
}
|
||||
|
|
@ -172,7 +312,7 @@ class HomepartyCart
|
|||
}
|
||||
public static function getFormattedBonusPrice()
|
||||
{
|
||||
return formatNumber(self::$bonus_price);
|
||||
return formatNumber(self::$voucher_price);
|
||||
}
|
||||
public static function getFormattedBonusStart(){
|
||||
return formatNumber(self::$bonus_start);
|
||||
|
|
@ -211,330 +351,7 @@ class HomepartyCart
|
|||
return formatNumber( self::$bonus_points_diff, 0 );
|
||||
|
||||
}
|
||||
/* public static function getTaxRate()
|
||||
{
|
||||
return config('cart.tax');
|
||||
}
|
||||
|
||||
public function putYardExtra($key, $value){
|
||||
|
||||
$content = $this->getYContent();
|
||||
$content->put($key, $value);
|
||||
$this->ysession->put($this->yinstance, $content);
|
||||
}
|
||||
|
||||
public function getYardExtra($key){
|
||||
$content = $this->getYContent();
|
||||
if ($content->has($key)){
|
||||
return $content->get($key);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getShippingCountryName(){
|
||||
|
||||
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
|
||||
if($shippingCountry && $shippingCountry->country){
|
||||
return $shippingCountry->country->getLocated();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
public function getShippingCountryCountryId()
|
||||
{
|
||||
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
|
||||
if($shippingCountry && $shippingCountry->country){
|
||||
return $shippingCountry->country->id;
|
||||
}
|
||||
return 1; //default DE
|
||||
}
|
||||
|
||||
public function getShippingCountryId()
|
||||
{
|
||||
return $this->shipping_country_id;
|
||||
}
|
||||
|
||||
|
||||
public function getYContent()
|
||||
{
|
||||
if (is_null($this->ysession->get($this->yinstance))) {
|
||||
return new Collection([]);
|
||||
}
|
||||
return $this->ysession->get($this->yinstance);
|
||||
}
|
||||
|
||||
|
||||
public function reCalculateShippingPrice(){
|
||||
$this->calculateShippingPrice();
|
||||
}
|
||||
|
||||
public function setShippingCountryWithPrice($shipping_country_id, $shipping_is_for = 'ot')
|
||||
{
|
||||
$this->shipping_country_id = $shipping_country_id;
|
||||
$this->putYardExtra('shipping_country_id', $shipping_country_id);
|
||||
|
||||
$this->shipping_is_for = $shipping_is_for;
|
||||
$this->putYardExtra('shipping_is_for', $shipping_is_for);
|
||||
|
||||
$this->calculateShippingPrice();
|
||||
|
||||
}
|
||||
|
||||
private function calculateShippingPrice(){
|
||||
|
||||
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
|
||||
if(!$shippingCountry){
|
||||
return;
|
||||
}
|
||||
$shipping = $shippingCountry->shipping;
|
||||
|
||||
if($this->weight() == 0){
|
||||
$shipping_price = $shipping->shipping_prices->first();
|
||||
$shipping_price->price = 0;
|
||||
$shipping_price->price_comp = 0;
|
||||
}else{
|
||||
//first by price
|
||||
$shipping_price = $this->shippingPriceByTotal($shipping->shipping_prices, $this->total(2, '.', ''));
|
||||
//sec by weight
|
||||
if(!$shipping_price){
|
||||
$shipping_price = $this->shippingPriceByWeight($shipping->shipping_prices, $this->weight());
|
||||
}
|
||||
//default
|
||||
if(!$shipping_price){
|
||||
$shipping_price = $shipping->shipping_prices->first();
|
||||
}
|
||||
}
|
||||
if($shipping_price){
|
||||
$price = $shipping_price->price;
|
||||
$this->num_comp = 0;
|
||||
if($this->shipping_is_for === 'me'){
|
||||
$price = $shipping_price->price_comp;
|
||||
$this->num_comp = $shipping_price->num_comp;
|
||||
|
||||
}
|
||||
$this->shipping_price = $price;
|
||||
$this->shipping_tax_rate = $shipping_price->tax_rate;
|
||||
$this->shipping_price_net = round($price / ((100+$shipping_price->tax_rate) / 100), 2);
|
||||
$this->shipping_tax = round($price / (100+$shipping_price->tax_rate) * 100, 2);
|
||||
|
||||
$this->putYardExtra('num_comp', $this->num_comp);
|
||||
$this->putYardExtra('shipping_price', $this->shipping_price);
|
||||
$this->putYardExtra('shipping_tax_rate', $this->shipping_tax_rate);
|
||||
$this->putYardExtra('shipping_tax', $this->shipping_tax);
|
||||
$this->putYardExtra('shipping_price_net', $this->shipping_price_net);
|
||||
}
|
||||
}
|
||||
|
||||
private function shippingPriceByTotal($prices, $total){
|
||||
foreach ($prices as $price){
|
||||
if($price->total_from > 0 && $price->total_to > 0){
|
||||
if($total >= $price->total_from && $total <= $price->total_to){
|
||||
return $price;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private function shippingPriceByWeight($prices, $weight){
|
||||
foreach ($prices as $price){
|
||||
if($price->weight_from > 0 && $price->weight_to > 0){
|
||||
if($weight >= $price->weight_from && $weight <= $price->weight_to){
|
||||
return $price;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function shipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
return $this->numberFormat($this->shipping_price, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
public function shippingNet($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
return $this->numberFormat($this->shipping_price_net, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
//
|
||||
private function shippingTax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
return $this->numberFormat($this->shipping_tax, $decimals, $decimalPoint, $thousandSeperator);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//netto
|
||||
public function subtotalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$subtotal = (float) $this->shipping_price_net + $this->subtotal(2, '.', '');
|
||||
return $this->numberFormat($subtotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
|
||||
public function taxWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$total = $this->totalWithShipping(2, '.', '');
|
||||
// $totalTax = (float) $this->tax(2, '.', '') + $this->shipping_tax;
|
||||
$totalTax = $this->subtotalWithShipping(2, '.', '');
|
||||
return $this->numberFormat(($total - $totalTax), $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
|
||||
public function totalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$total = (float) ($this->total(2, '.', '')) + $this->shipping_price;
|
||||
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
|
||||
public function weight($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$content = $this->getContent();
|
||||
$total = $content->reduce(function ($total, CartItem $cartItem) {
|
||||
return $total + ($cartItem->options->weight ? ($cartItem->options->weight*$cartItem->qty) : 0);
|
||||
}, 0);
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
public function points()
|
||||
{
|
||||
$content = $this->getContent();
|
||||
$total = $content->reduce(function ($total, CartItem $cartItem) {
|
||||
return $total + ($cartItem->options->points ? ($cartItem->options->points * $cartItem->qty) : 0);
|
||||
}, 0);
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
public function compCount()
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$count = parent::count();
|
||||
$comp_count = $content->reduce(function ($comp_count, CartItem $cartItem) {
|
||||
return $cartItem->options->comp ? $comp_count + 1 : $comp_count;
|
||||
}, 0);
|
||||
return $count-$comp_count;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$subTotal = $content->reduce(function ($subTotal, CartItem $cartItem) {
|
||||
$price_net = $cartItem->price / ((100 + $cartItem->taxRate) / 100);
|
||||
return $subTotal + ($cartItem->qty * $price_net);
|
||||
}, 0);
|
||||
|
||||
return $this->numberFormat($subTotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
|
||||
public function getCartItemByProduct($product_id, $set_price='with'){
|
||||
if($product = Product::find($product_id)) {
|
||||
$image = "";
|
||||
if ($product->images->count()) {
|
||||
$image = $product->images->first()->slug;
|
||||
}
|
||||
$price = $product->price;
|
||||
if($set_price === 'with'){
|
||||
$price = $product->getPriceWith(false, true);
|
||||
}
|
||||
$cartItem = $this->getCartItem($product->id, $product->getLang('name'), 1, $price, ['image' => $image, 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points]);
|
||||
$content = $this->getContent();
|
||||
|
||||
if ($content->has($cartItem->rowId)){
|
||||
return $content->get($cartItem->rowId);
|
||||
}
|
||||
return $cartItem;
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function getCartItem($id, $name = null, $qty = null, $price = null, array $options = []){
|
||||
if ($id instanceof Buyable) {
|
||||
$cartItem = CartItem::fromBuyable($id, $qty ?: []);
|
||||
} elseif (is_array($id)) {
|
||||
$cartItem = CartItem::fromArray($id);
|
||||
} else {
|
||||
$cartItem = CartItem::fromAttributes($id, $name, $price, $options);
|
||||
}
|
||||
return $cartItem;
|
||||
}
|
||||
|
||||
public function destroy()
|
||||
{
|
||||
$this->ysession->remove($this->yinstance);
|
||||
parent::destroy();
|
||||
|
||||
}
|
||||
|
||||
public function rowPriceNet(CartItem $row, $decimals = null, $decimalPoint = null, $thousandSeperator = null){
|
||||
$price = round($row->price / ((100 + $row->taxRate) /100), 4);
|
||||
return $this->numberFormat($price, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
public function rowSubtotalNet(CartItem $row, $decimals = null, $decimalPoint = null, $thousandSeperator = null){
|
||||
$price = round($row->price / ((100 + $row->taxRate) /100), 4);
|
||||
return $this->numberFormat(($price * $row->qty), $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
public function getNumComp(){
|
||||
return $this->num_comp;
|
||||
}
|
||||
|
||||
public function getCompProductBy($comp, $product_id=false){
|
||||
foreach ($this->content() as $row) {
|
||||
if($row->options->comp == $comp) {
|
||||
return $row->options->product_id;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getContentByOrder(){
|
||||
$ret = [];
|
||||
$comp = [];
|
||||
foreach ($this->content() as $row) {
|
||||
if($row->options->comp){
|
||||
$comp[100+$row->options->comp] = $row;
|
||||
}else{
|
||||
$ret[] = $row;
|
||||
}
|
||||
}
|
||||
ksort($comp);
|
||||
$ret = array_merge($ret, $comp);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
*/
|
||||
protected function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
|
||||
{
|
||||
if(is_null($decimals)){
|
||||
|
|
|
|||
|
|
@ -32,17 +32,34 @@ class HomepartyUserCart
|
|||
public $ek_price;
|
||||
public $income_price;
|
||||
|
||||
public $weight;
|
||||
public $shipping_weight;
|
||||
|
||||
|
||||
public $shipping_price;
|
||||
public $shipping_tax_rate;
|
||||
public $shipping_price_net;
|
||||
public $shipping_tax;
|
||||
|
||||
|
||||
|
||||
private $homepartyUser;
|
||||
|
||||
public function __construct(HomepartyUser $homepartyUser)
|
||||
{
|
||||
$this->homepartyUser = $homepartyUser;
|
||||
//is all total
|
||||
$this->points = 0;
|
||||
$this->price = 0;
|
||||
$this->price_net = 0;
|
||||
$this->ek_price = 0;
|
||||
$this->income_price = 0;
|
||||
$this->weight = 0;
|
||||
$this->shipping_weight = 0;
|
||||
$this->shipping_price = 0;
|
||||
$this->shipping_tax_rate = 0;
|
||||
$this->shipping_price_net = 0;
|
||||
$this->shipping_tax = 0;
|
||||
$this->calculateUserCart();
|
||||
}
|
||||
|
||||
|
|
@ -54,6 +71,7 @@ class HomepartyUserCart
|
|||
$this->price_net += $order_item->geTotalPriceNet();
|
||||
$this->ek_price += $order_item->geTotalEKPrice();
|
||||
$this->income_price += $order_item->geTotalIncomePrice();
|
||||
$this->weight += ($order_item->product->weight * $order_item->qty);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -82,330 +100,12 @@ class HomepartyUserCart
|
|||
{
|
||||
return formatNumber($this->income_price);
|
||||
}
|
||||
/* public static function getTaxRate()
|
||||
|
||||
public function getFormattedShippingPrice()
|
||||
{
|
||||
return config('cart.tax');
|
||||
}
|
||||
|
||||
public function putYardExtra($key, $value){
|
||||
|
||||
$content = $this->getYContent();
|
||||
$content->put($key, $value);
|
||||
$this->ysession->put($this->yinstance, $content);
|
||||
return formatNumber($this->shipping_price);
|
||||
}
|
||||
|
||||
public function getYardExtra($key){
|
||||
$content = $this->getYContent();
|
||||
if ($content->has($key)){
|
||||
return $content->get($key);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getShippingCountryName(){
|
||||
|
||||
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
|
||||
if($shippingCountry && $shippingCountry->country){
|
||||
return $shippingCountry->country->getLocated();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
public function getShippingCountryCountryId()
|
||||
{
|
||||
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
|
||||
if($shippingCountry && $shippingCountry->country){
|
||||
return $shippingCountry->country->id;
|
||||
}
|
||||
return 1; //default DE
|
||||
}
|
||||
|
||||
public function getShippingCountryId()
|
||||
{
|
||||
return $this->shipping_country_id;
|
||||
}
|
||||
|
||||
|
||||
public function getYContent()
|
||||
{
|
||||
if (is_null($this->ysession->get($this->yinstance))) {
|
||||
return new Collection([]);
|
||||
}
|
||||
return $this->ysession->get($this->yinstance);
|
||||
}
|
||||
|
||||
|
||||
public function reCalculateShippingPrice(){
|
||||
$this->calculateShippingPrice();
|
||||
}
|
||||
|
||||
public function setShippingCountryWithPrice($shipping_country_id, $shipping_is_for = 'ot')
|
||||
{
|
||||
$this->shipping_country_id = $shipping_country_id;
|
||||
$this->putYardExtra('shipping_country_id', $shipping_country_id);
|
||||
|
||||
$this->shipping_is_for = $shipping_is_for;
|
||||
$this->putYardExtra('shipping_is_for', $shipping_is_for);
|
||||
|
||||
$this->calculateShippingPrice();
|
||||
|
||||
}
|
||||
|
||||
private function calculateShippingPrice(){
|
||||
|
||||
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
|
||||
if(!$shippingCountry){
|
||||
return;
|
||||
}
|
||||
$shipping = $shippingCountry->shipping;
|
||||
|
||||
if($this->weight() == 0){
|
||||
$shipping_price = $shipping->shipping_prices->first();
|
||||
$shipping_price->price = 0;
|
||||
$shipping_price->price_comp = 0;
|
||||
}else{
|
||||
//first by price
|
||||
$shipping_price = $this->shippingPriceByTotal($shipping->shipping_prices, $this->total(2, '.', ''));
|
||||
//sec by weight
|
||||
if(!$shipping_price){
|
||||
$shipping_price = $this->shippingPriceByWeight($shipping->shipping_prices, $this->weight());
|
||||
}
|
||||
//default
|
||||
if(!$shipping_price){
|
||||
$shipping_price = $shipping->shipping_prices->first();
|
||||
}
|
||||
}
|
||||
if($shipping_price){
|
||||
$price = $shipping_price->price;
|
||||
$this->num_comp = 0;
|
||||
if($this->shipping_is_for === 'me'){
|
||||
$price = $shipping_price->price_comp;
|
||||
$this->num_comp = $shipping_price->num_comp;
|
||||
|
||||
}
|
||||
$this->shipping_price = $price;
|
||||
$this->shipping_tax_rate = $shipping_price->tax_rate;
|
||||
$this->shipping_price_net = round($price / ((100+$shipping_price->tax_rate) / 100), 2);
|
||||
$this->shipping_tax = round($price / (100+$shipping_price->tax_rate) * 100, 2);
|
||||
|
||||
$this->putYardExtra('num_comp', $this->num_comp);
|
||||
$this->putYardExtra('shipping_price', $this->shipping_price);
|
||||
$this->putYardExtra('shipping_tax_rate', $this->shipping_tax_rate);
|
||||
$this->putYardExtra('shipping_tax', $this->shipping_tax);
|
||||
$this->putYardExtra('shipping_price_net', $this->shipping_price_net);
|
||||
}
|
||||
}
|
||||
|
||||
private function shippingPriceByTotal($prices, $total){
|
||||
foreach ($prices as $price){
|
||||
if($price->total_from > 0 && $price->total_to > 0){
|
||||
if($total >= $price->total_from && $total <= $price->total_to){
|
||||
return $price;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private function shippingPriceByWeight($prices, $weight){
|
||||
foreach ($prices as $price){
|
||||
if($price->weight_from > 0 && $price->weight_to > 0){
|
||||
if($weight >= $price->weight_from && $weight <= $price->weight_to){
|
||||
return $price;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function shipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
return $this->numberFormat($this->shipping_price, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
public function shippingNet($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
return $this->numberFormat($this->shipping_price_net, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
//
|
||||
private function shippingTax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
return $this->numberFormat($this->shipping_tax, $decimals, $decimalPoint, $thousandSeperator);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//netto
|
||||
public function subtotalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$subtotal = (float) $this->shipping_price_net + $this->subtotal(2, '.', '');
|
||||
return $this->numberFormat($subtotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
|
||||
public function taxWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$total = $this->totalWithShipping(2, '.', '');
|
||||
// $totalTax = (float) $this->tax(2, '.', '') + $this->shipping_tax;
|
||||
$totalTax = $this->subtotalWithShipping(2, '.', '');
|
||||
return $this->numberFormat(($total - $totalTax), $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
|
||||
public function totalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$total = (float) ($this->total(2, '.', '')) + $this->shipping_price;
|
||||
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
|
||||
public function weight($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$content = $this->getContent();
|
||||
$total = $content->reduce(function ($total, CartItem $cartItem) {
|
||||
return $total + ($cartItem->options->weight ? ($cartItem->options->weight*$cartItem->qty) : 0);
|
||||
}, 0);
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
public function points()
|
||||
{
|
||||
$content = $this->getContent();
|
||||
$total = $content->reduce(function ($total, CartItem $cartItem) {
|
||||
return $total + ($cartItem->options->points ? ($cartItem->options->points * $cartItem->qty) : 0);
|
||||
}, 0);
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
public function compCount()
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$count = parent::count();
|
||||
$comp_count = $content->reduce(function ($comp_count, CartItem $cartItem) {
|
||||
return $cartItem->options->comp ? $comp_count + 1 : $comp_count;
|
||||
}, 0);
|
||||
return $count-$comp_count;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
|
||||
{
|
||||
$content = $this->getContent();
|
||||
|
||||
$subTotal = $content->reduce(function ($subTotal, CartItem $cartItem) {
|
||||
$price_net = $cartItem->price / ((100 + $cartItem->taxRate) / 100);
|
||||
return $subTotal + ($cartItem->qty * $price_net);
|
||||
}, 0);
|
||||
|
||||
return $this->numberFormat($subTotal, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
|
||||
public function getCartItemByProduct($product_id, $set_price='with'){
|
||||
if($product = Product::find($product_id)) {
|
||||
$image = "";
|
||||
if ($product->images->count()) {
|
||||
$image = $product->images->first()->slug;
|
||||
}
|
||||
$price = $product->price;
|
||||
if($set_price === 'with'){
|
||||
$price = $product->getPriceWith(false, true);
|
||||
}
|
||||
$cartItem = $this->getCartItem($product->id, $product->getLang('name'), 1, $price, ['image' => $image, 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points]);
|
||||
$content = $this->getContent();
|
||||
|
||||
if ($content->has($cartItem->rowId)){
|
||||
return $content->get($cartItem->rowId);
|
||||
}
|
||||
return $cartItem;
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function getCartItem($id, $name = null, $qty = null, $price = null, array $options = []){
|
||||
if ($id instanceof Buyable) {
|
||||
$cartItem = CartItem::fromBuyable($id, $qty ?: []);
|
||||
} elseif (is_array($id)) {
|
||||
$cartItem = CartItem::fromArray($id);
|
||||
} else {
|
||||
$cartItem = CartItem::fromAttributes($id, $name, $price, $options);
|
||||
}
|
||||
return $cartItem;
|
||||
}
|
||||
|
||||
public function destroy()
|
||||
{
|
||||
$this->ysession->remove($this->yinstance);
|
||||
parent::destroy();
|
||||
|
||||
}
|
||||
|
||||
public function rowPriceNet(CartItem $row, $decimals = null, $decimalPoint = null, $thousandSeperator = null){
|
||||
$price = round($row->price / ((100 + $row->taxRate) /100), 4);
|
||||
return $this->numberFormat($price, $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
public function rowSubtotalNet(CartItem $row, $decimals = null, $decimalPoint = null, $thousandSeperator = null){
|
||||
$price = round($row->price / ((100 + $row->taxRate) /100), 4);
|
||||
return $this->numberFormat(($price * $row->qty), $decimals, $decimalPoint, $thousandSeperator);
|
||||
}
|
||||
|
||||
public function getNumComp(){
|
||||
return $this->num_comp;
|
||||
}
|
||||
|
||||
public function getCompProductBy($comp, $product_id=false){
|
||||
foreach ($this->content() as $row) {
|
||||
if($row->options->comp == $comp) {
|
||||
return $row->options->product_id;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getContentByOrder(){
|
||||
$ret = [];
|
||||
$comp = [];
|
||||
foreach ($this->content() as $row) {
|
||||
if($row->options->comp){
|
||||
$comp[100+$row->options->comp] = $row;
|
||||
}else{
|
||||
$ret[] = $row;
|
||||
}
|
||||
}
|
||||
ksort($comp);
|
||||
$ret = array_merge($ret, $comp);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
*/
|
||||
protected function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
|
||||
{
|
||||
if(is_null($decimals)){
|
||||
|
|
|
|||
|
|
@ -120,8 +120,14 @@ class Payment
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
if($shopping_order->homeparty){
|
||||
$shopping_order->setUserHistoryValue(['status' => 9]);
|
||||
$shopping_order->homeparty->completed = 1;
|
||||
$shopping_order->homeparty->save();
|
||||
}
|
||||
return $send_link;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue