User Order all Margins / Checkout

This commit is contained in:
Kevin Adametz 2021-01-22 15:54:51 +01:00
parent a96d7d5c77
commit 224bf9e951
92 changed files with 3551 additions and 561 deletions

View file

@ -65,6 +65,29 @@ class HTMLHelper
}
public static function setContentReadMore($content){
$sep = '##mehr lesen##';
if(strpos($content, $sep) !== false){
$name = 'collapse_'.random_int(1000, 10000);
$split = explode($sep, $content);
$first = isset($split[0]) ? $split[0] : "";
$text = isset($split[1]) ? $split[1] : "";
$content = $first;
$content .= '<br><a class="btn btn-primary btn-sm mt-2 collapsed" data-toggle="collapse" href="#'.$name.'" role="button" aria-expanded="false" aria-controls="'.$name.'">
<span class="if-collapsed">'.__('mehr lesen').'</span>
<span class="if-not-collapsed">'.__('weniger lesen').'</span>
</a>';
$content .= '<div class="collapse" id="'.$name.'">
<div class="card card-body pt-0">
'.$text.'
</div>
</div>';
}
return $content;
}
public static function getRolesOptions(){
$ret = "";
foreach (self::$roles as $role_id => $value){

View file

@ -141,6 +141,21 @@ class Payment
}
}
//if the order has action
if($shopping_order->shopping_user->is_from === 'user_order'){
//is margin -> set paid
$shopping_order->shopping_order_margin->paid = true;
$shopping_order->shopping_order_margin->save();
//is payment credit, reduce
if($shopping_order->shopping_order_margin->from_payment_credit > 0){
$new_credit = $shopping_order->auth_user->payment_credit - $shopping_order->shopping_order_margin->from_payment_credit;
$new_credit = $new_credit < 0 ? 0 : $new_credit;
$shopping_order->auth_user->payment_credit = $new_credit;
$shopping_order->auth_user->save();
}
}
return $send_link;
}

View file

@ -43,10 +43,10 @@ class Slim {
if (empty($value)) {return null;}
// If magic quotes enabled
if (get_magic_quotes_gpc()) {
/* if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
*/
// The data is posted as a JSON String so to be used it needs to be deserialized first
$data = json_decode($value);

View file

@ -0,0 +1,87 @@
<?php
namespace App\Services;
use App\Models\ShoppingOrderMargin;
use App\User;
use Carbon;
class UserMarign
{
public static function getMontlyPrice(User $user, $date = null, $format = false){
$now = $date ? Carbon::parse($date) : Carbon::now();
$startDay = $now->startOfMonth()->toDateString();
$endDay = $now->endOfMonth()->toDateString();
$sum_net_amount = ShoppingOrderMargin::whereUserId($user->id)
->whereBetween('from', [$startDay, $endDay])
->wherePaid(true)
->whereCancellation(false)
->sum('net_price');
if($format){
$sum_net_amount = Util::formatNumber($sum_net_amount);
}
return $sum_net_amount;
}
public static function getMontlyPriceOpen(User $user, $date = null, $format = false){
$now = $date ? Carbon::parse($date) : Carbon::now();
$startDay = $now->startOfMonth()->toDateString();
$endDay = $now->endOfMonth()->toDateString();
$sum_net_amount = ShoppingOrderMargin::whereUserId($user->id)
->whereBetween('from', [$startDay, $endDay])
->wherePaid(false)
->whereCancellation(false)
->sum('net_price');
if($format){
$sum_net_amount = Util::formatNumber($sum_net_amount);
}
return $sum_net_amount;
}
public static function getMontlyAmount(User $user, $date = null, $format = false){
$now = $date ? Carbon::parse($date) : Carbon::now();
$startDay = $now->startOfMonth()->toDateString();
$endDay = $now->endOfMonth()->toDateString();
$sum_net_amount = ShoppingOrderMargin::whereUserId($user->id)
->whereBetween('from', [$startDay, $endDay])
->wherePaid(true)
->whereCancellation(false)
->sum('net_amount');
if($format){
$sum_net_amount = Util::formatNumber($sum_net_amount);
}
return $sum_net_amount;
}
public static function getMontlyPartnerCommission(User $user, $date = null, $format = false){
$now = $date ? Carbon::parse($date) : Carbon::now();
$startDay = $now->startOfMonth()->toDateString();
$endDay = $now->endOfMonth()->toDateString();
$sum_net_amount = ShoppingOrderMargin::whereMSponsorId($user->id)
->whereBetween('from', [$startDay, $endDay])
->wherePaid(true)
->whereCancellation(false)
->sum('net_partner_commission');
if($format){
$sum_net_amount = Util::formatNumber($sum_net_amount);
}
return $sum_net_amount;
}
//monthy amount, sum
}

View file

@ -3,6 +3,9 @@ namespace App\Services;
use App\Models\Product;
use App\Models\ShippingCountry;
use App\Services\Yard\Commission;
use App\Services\Yard\Margin;
use App\Services\Yard\MarginItems;
use Gloudemans\Shoppingcart\Cart;
use Gloudemans\Shoppingcart\CartItem;
use Gloudemans\Shoppingcart\Contracts\Buyable;
@ -23,6 +26,13 @@ class Yard extends Cart
private $ysession;
private $yinstance;
private $shopping_data = [];
private $user;
private $payment_credit;
private $yard_commission;
private $yard_margin;
private $global_tax_rate = 19;
public function __construct(SessionManager $session, Dispatcher $events)
{
@ -52,6 +62,25 @@ class Yard extends Cart
$this->shipping_is_for = $this->getYardExtra('shipping_is_for');
}
if($this->getYardExtra('shopping_user')){
$this->user = $this->getYardExtra('shopping_user');
}
if($this->getYardExtra('user')){
$this->user = $this->getYardExtra('user');
}
if($this->getYardExtra('payment_credit')){
$this->payment_credit = $this->getYardExtra('payment_credit');
}
if($this->getYardExtra('yard_commission')){
$this->yard_commission = $this->getYardExtra('yard_commission');
}
if($this->getYardExtra('yard_margin')){
$this->yard_margin = $this->getYardExtra('yard_margin');
}
/*if($this->getYardExtra('num_comp')){
$this->num_comp = $this->getYardExtra('num_comp');
}*/
@ -123,10 +152,111 @@ class Yard extends Cart
}
public function reCalculateShippingPrice(){
public function reCalculate(){
$this->calculateShippingPrice();
}
/* * ***** */
public function calculateMargins(){
//get user monthy amount
$monthy_amount = UserMarign::getMontlyAmount($this->user);
$content = $this->getContent();
/*single + partner Commissions */
//get price net commission, add amount, partner (total) from products is single_commission
$yard_commission = new Commission();
$yard_commission = $content->reduce(function ($yard_commission, CartItem $cartItem) {
$price_net = $cartItem->price / ((100 + $cartItem->taxRate) / 100);
if($cartItem->options->single_commission){
$value_commission = $price_net / 100 * $cartItem->options->value_commission;
$partner_commission = $price_net / 100 * $cartItem->options->partner_commission;
$price_net_commission = ($cartItem->qty * $price_net) - ($cartItem->qty * $value_commission);
$yard_commission->single_price_net += ($cartItem->qty * $price_net);
$yard_commission->single_value_commission += ($cartItem->qty * $value_commission);
if($this->user->user_level->partner_provision){
$yard_commission->single_partner_commission += ($cartItem->qty * $partner_commission);
}
$yard_commission->single_price_net_commission += $price_net_commission;
//
if($cartItem->options->amount_commission){
$yard_commission->single_amount_commission += $price_net_commission;
}
}else{
$yard_commission->price_net = $yard_commission->price_net + ($cartItem->qty * $price_net);
}
return $yard_commission;
}, $yard_commission);
$this->yard_commission = $yard_commission;
$this->putYardExtra('yard_commission', $this->yard_commission);
//dump($monthy_amount);
//add to $monthy_amount für max level margins
$start_monthy_amount = $monthy_amount + $yard_commission->single_amount_commission;
$end_monthy_amount = $start_monthy_amount + $yard_commission->price_net;
//dump($start_monthy_amount);
//dump($end_monthy_amount);
$margin = new Margin();
$rest_amount = 0;
$range = 0;
$price_net = $yard_commission->price_net;
if($yard_commission->price_net > 0 && $this->user && $this->user->user_level && $this->user->user_level->user_level_margins){
foreach ($this->user->user_level->user_level_margins_re as $user_level_margin) {
//total split?
if ($end_monthy_amount >= $user_level_margin->price_from) {
if(!isset($balance)){
if($start_monthy_amount >= $user_level_margin->price_from){
$balance = $yard_commission->price_net;
$rest_amount = 0;
}else{
$balance = $end_monthy_amount - $user_level_margin->price_from;
$rest_amount = $end_monthy_amount - ($start_monthy_amount + $balance);
}
}else{
$range = $last_limit-$user_level_margin->price_from;
if($rest_amount >= $range){
$balance = $range;
}else{
$balance = $rest_amount;
}
$rest_amount = $rest_amount-$balance;
}
$last_limit = $user_level_margin->price_from;
if($balance != 0){
$margin->add($user_level_margin->price_from, [
'price_net' => $price_net,
'range' => $range,
'rest_amount' => $rest_amount,
'balance' => $balance,
'trading_margin'=> $user_level_margin->trading_margin,
'commission'=> $user_level_margin->commission,
]);
}
}
}
}
$margin->setCommission($this->yard_commission);
$margin->calculate();
$this->yard_margin = $margin;
$this->putYardExtra('yard_margin', $this->yard_margin);
//dump($margin);
}
public function getYardMargin(){
return $this->yard_margin;
}
public function getYardCommission(){
return $this->yard_commission;
}
/* * ***** */
public function setShippingCountryWithPrice($shipping_country_id, $shipping_is_for = 'ot')
{
$this->shipping_country_id = $shipping_country_id;
@ -139,6 +269,21 @@ class Yard extends Cart
}
public function setShoppingUser($user, $payment_credit = false){
$this->user = $user;
$this->payment_credit = $payment_credit;
$this->putYardExtra('user', $user);
$this->putYardExtra('payment_credit', $payment_credit);
}
public function getPaymentCredit(){
if($this->payment_credit && $this->user->payment_credit > 0){
return $this->user->payment_credit;
}
return 0;
}
private function calculateShippingPrice(){
$shippingCountry = ShippingCountry::find($this->shipping_country_id);
@ -151,10 +296,8 @@ class Yard extends Cart
$shipping_price->price = 0;
$shipping_price->price_comp = 0;
}else{
//first by price
$shipping_price = $this->shippingPriceByTotal($shipping->shipping_prices, $this->total(2, '.', ''));
$shipping_price = $this->shippingPriceBySubTotal($shipping->shipping_prices, $this->subtotal(2, '.', ''));
//sec by weight
if(!$shipping_price){
$shipping_price = $this->shippingPriceByWeight($shipping->shipping_prices, $this->weight());
@ -186,7 +329,7 @@ class Yard extends Cart
}
}
private function shippingPriceByTotal($prices, $total){
private function shippingPriceBySubTotal($prices, $total){
foreach ($prices as $price){
if($price->total_from > 0 && $price->total_to > 0){
if($total >= $price->total_from && $total <= $price->total_to){
@ -243,19 +386,56 @@ class Yard extends Cart
public function taxWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$total = $this->totalWithShipping(2, '.', '');
// $total = $this->totalWithShipping(2, '.', '');
// $totalTax = (float) $this->tax(2, '.', '') + $this->shipping_tax;
$totalTax = $this->subtotalWithShipping(2, '.', '');
return $this->numberFormat(($total - $totalTax), $decimals, $decimalPoint, $thousandSeperator);
$subtotal_shipping = $this->subtotalWithShipping(2, '.', '');
return $this->numberFormat(($subtotal_shipping / 100 * $this->global_tax_rate), $decimals, $decimalPoint, $thousandSeperator);
}
public function totalWithShipping($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$total = (float) ($this->total(2, '.', '')) + $this->shipping_price;
//$total = (float) ($this->total(2, '.', '')) + $this->shipping_price;
$subtotal_shipping = $this->subtotalWithShipping(2, '.', '');
$tax = $subtotal_shipping / 100 * $this->global_tax_rate;
$total = ($subtotal_shipping + $tax);
if($PaymentCredit = $this->getPaymentCredit()){
$total = $total - $PaymentCredit;
if($total < 0){
$total = 0;
}
}
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
}
public function totalWithShippingWithoutCredit($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
//$total = (float) ($this->total(2, '.', '')) + $this->shipping_price;
$subtotal_shipping = $this->subtotalWithShipping(2, '.', '');
$tax = $subtotal_shipping / 100 * $this->global_tax_rate;
$total = ($subtotal_shipping + $tax);
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
}
public function totalfromCredit($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
$rest = 0;
if ($PaymentCredit = $this->getPaymentCredit()) {
$subtotal_shipping= $this->subtotalWithShipping(2, '.', '');
$tax = $subtotal_shipping/ 100 * $this->global_tax_rate;
$total = ($subtotal_shipping+ $tax);
$rest = $PaymentCredit - $total;
if($rest < 0){
$rest = $PaymentCredit;
}else{
$rest = $PaymentCredit - $rest;
}
}
return $this->numberFormat($rest, $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Get the total price of the items in the cart.
*
@ -270,7 +450,6 @@ class Yard extends Cart
$total = $content->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->options->weight ? ($cartItem->options->weight*$cartItem->qty) : 0);
}, 0);
return $total;
}
@ -280,7 +459,6 @@ class Yard extends Cart
$total = $content->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->options->points ? ($cartItem->options->points * $cartItem->qty) : 0);
}, 0);
return $total;
}
@ -312,10 +490,8 @@ class Yard extends Cart
if ($withFees === true) {
$fees = $this->feeTotal(null, null, null, true);
$total = $total + $fees;
}
return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
}
@ -340,7 +516,6 @@ class Yard extends Cart
if ($withFees === true) {
$fees = $this->feeTax();
$tax = $tax + floatval($fees);
}
@ -356,7 +531,7 @@ class Yard extends Cart
* @param string $thousandSeperator
* @return float
*/
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null, $discount = true)
{
$content = $this->getContent();
@ -365,6 +540,9 @@ class Yard extends Cart
return $subTotal + ($cartItem->qty * $price_net);
}, 0);
if($discount && $this->yard_margin){
$subTotal -= $this->yard_margin->net_discount;
}
return $this->numberFormat($subTotal, $decimals, $decimalPoint, $thousandSeperator);
}
@ -379,7 +557,17 @@ class Yard extends Cart
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]);
$cartItem = $this->getCartItem($product->id, $product->getLang('name'), 1, $price,
[
'image' => $image,
'slug' => $product->slug,
'weight' => $product->weight,
'single_commission' => $product->single_commission,
'amount_commission' => $product->amount_commission,
'value_commission' => $product->value_commission,
'partner_commission' => $product->partner_commission,
]
);
$content = $this->getContent();
if ($content->has($cartItem->rowId)){

View file

@ -0,0 +1,34 @@
<?php
namespace App\Services\Yard;
use App\Services\Util;
class Commission
{
public $price_net;
public $single_price_net;
public $single_value_commission;
public $single_partner_commission;
public $single_amount_commission;
public $single_price_net_commission;
public function __construct()
{
$this->price_net = 0;
$this->single_price_net = 0;
$this->single_amount_commission = 0;
$this->single_partner_commission = 0;
$this->single_value_commission = 0;
$this->single_price_net_commission = 0;
}
public function isCommission(){
return ($this->single_value_commission > 0) ? true : false;
}
public function getFormatted($key)
{
return isset($this->{$key}) ? Util::formatNumber($this->{$key}) : "";
}
}

View file

@ -0,0 +1,104 @@
<?php
namespace App\Services\Yard;
use App\Services\Util;
use Illuminate\Support\Collection;
class Margin
{
public $items;
public $commission;
public $net_discount;
public $net_partner_commission;
public $net_price;
public $net_amount;
public function __construct()
{
$this->items = new Collection;
}
public function add($price_from, $values){
$this->items->push(new MarginItems($price_from, $values));
}
public function setCommission(Commission $commission){
$this->commission = $commission;
}
public function isMargin(){
return count($this->items) ? true : false;
}
public function calculate(){
if($this->commission){
$this->net_discount += $this->commission->single_value_commission;
$this->net_partner_commission += $this->commission->single_partner_commission;
$this->net_amount += $this->commission->single_amount_commission;
$this->net_price += $this->commission->single_price_net_commission;
}
if($this->isMargin()){
foreach ($this->items as $item){
$this->net_discount += $item->value_margin;
$this->net_partner_commission += $item->value_commission;
$this->net_amount += $item->new_price_net;
$this->net_price += $item->new_price_net;
}
}
}
public function getFormatted($key)
{
return isset($this->{$key}) ? Util::formatNumber($this->{$key}) : "";
}
public function toArray()
{
return [
'items' => $this->items,
'commission' => $this->commission,
'net_discount' => $this->net_discount,
'net_partner_commission' => $this->net_partner_commission,
'net_price' => $this->net_price,
'net_amount' => $this->net_amount,
];
}
public function restore($content)
{
// Unserialize the content (either array if new, or collection if old)
$storedContent = unserialize($content);
if (is_array($storedContent)) {
$this->fromArray($storedContent);
}
//TODO - check this
// If the old approach and is Collection, push into existing items
/* if ($storedContent instanceof Collection) {
foreach ($storedContent as $item) {
$this->items->put($item);
}
}*/
}
public function fromArray($array)
{
$this->items = $array['items'];
$this->commission = $array['commission'];
$this->net_discount = $array['net_discount'];
$this->net_partner_commission = $array['net_partner_commission'];
$this->net_price = $array['net_price'];
$this->net_amount = $array['net_amount'];
return $this;
}
}

View file

@ -0,0 +1,46 @@
<?php
namespace App\Services\Yard;
use App\Services\Util;
class MarginItems
{
public $price_from;
public $range;
public $rest_amount;
public $balance;
public $value_margin;
public $new_price_net;
public $trading_margin;
public $value_commission;
public $commission;
public function __construct($price_from, $values)
{
$this->price_from = $price_from;
$this->range = $values['range'];
$this->rest_amount = $values['rest_amount'];
$this->balance = $values['balance'];
$this->trading_margin = $values['trading_margin'];
$this->commission = $values['commission'];
$this->calculate();
}
public function calculate(){
$this->value_margin = $this->balance / 100 * $this->trading_margin;
$this->new_price_net = $this->balance - $this->value_margin;
if($this->commission > 0){
$this->value_commission = $this->balance / 100 * $this->commission;
}
}
public function getFormatted($key)
{
return isset($this->{$key}) ? Util::formatNumber($this->{$key}) : "";
}
}