API Shopping Order backend
This commit is contained in:
parent
7a040c3e19
commit
d01b4bd560
24 changed files with 1584 additions and 34 deletions
191
app/Services/ShopApiOrderCart.php
Normal file
191
app/Services/ShopApiOrderCart.php
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
<?php
|
||||
namespace App\Services;
|
||||
|
||||
use stdClass;
|
||||
use App\Models\ShoppingOrder;
|
||||
|
||||
class ShopApiOrderCart
|
||||
{
|
||||
|
||||
public $shipping;
|
||||
public $shipping_net;
|
||||
public $shipping_tax;
|
||||
public $points;
|
||||
public $points_total; //check
|
||||
public $price_total_net;
|
||||
public $price_total;
|
||||
public $tax_total;
|
||||
public $tax_split;
|
||||
public $qty_total;
|
||||
public $orders = [];
|
||||
|
||||
public $shop_items = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->shipping = 0;
|
||||
$this->shipping_net = 0;
|
||||
$this->shipping_tax = 0;
|
||||
$this->points_total = 0;
|
||||
$this->price_total_net = 0;
|
||||
$this->price_total = 0;
|
||||
$this->tax_total = 0;
|
||||
$this->tax_split = [];
|
||||
$this->qty_total = 0;
|
||||
|
||||
$this->shop_items = [];
|
||||
|
||||
}
|
||||
|
||||
public function add(ShoppingOrder $shopping_order){
|
||||
|
||||
$order = new stdClass();
|
||||
$order->order_id = $shopping_order->id;
|
||||
$order->wp_order_number = $shopping_order->shopping_user->wp_order_number;
|
||||
$order->billing_address = $this->setOrderAdress('billing', $shopping_order->shopping_user);
|
||||
$order->shipping_address = $this->setOrderAdress('shipping', $shopping_order->shopping_user);
|
||||
$order->shopping_order = $shopping_order;
|
||||
|
||||
$this->shipping += $shopping_order->shipping;
|
||||
$this->shipping_net += $shopping_order->shipping_net;
|
||||
|
||||
foreach ($shopping_order->shopping_order_items as $item){
|
||||
|
||||
$tax_rate = intval($item->product->tax);
|
||||
$user_price_net = $this->calcuPriceWith($item->price, $tax_rate, $item->discount);
|
||||
$user_price_net_qty = round($user_price_net * $item->qty, 2);
|
||||
$user_tax = $this->calcuTaxWith($user_price_net, $tax_rate);
|
||||
$user_tax_qty = round($user_tax * $item->qty, 2);
|
||||
|
||||
$shop_item_id = $item->product->id.'-'.$item->price;
|
||||
//set to item
|
||||
if(isset($this->shop_items[$shop_item_id])){
|
||||
$shop_item = $this->shop_items[$shop_item_id];
|
||||
$shop_item->user_price_total_net += $user_price_net_qty;
|
||||
$shop_item->user_tax_total += $user_tax_qty;
|
||||
$shop_item->points_total += ($item->points * $item->qty);
|
||||
$shop_item->qty += $item->qty;
|
||||
}else{
|
||||
$shop_item = new stdClass();
|
||||
$shop_item->pid = $item->product->id;
|
||||
|
||||
$shop_item->article = $item->product->wp_number;
|
||||
$shop_item->number = $item->product->number;
|
||||
$shop_item->name = $item->product->name;
|
||||
$shop_item->qty = $item->qty;
|
||||
|
||||
$shop_item->tax_rate = $tax_rate;
|
||||
$shop_item->points = $item->points;
|
||||
$shop_item->user_price_net = $user_price_net;
|
||||
$shop_item->user_price_total_net = $user_price_net_qty;
|
||||
$shop_item->user_tax = $user_tax;
|
||||
$shop_item->user_tax_total = $user_tax_qty;
|
||||
$shop_item->points_total = ($item->points * $item->qty);
|
||||
}
|
||||
|
||||
//only for tax split / tax and price on calculate function
|
||||
|
||||
$tax = isset($this->tax_split[$tax_rate]) ? $this->tax_split[$tax_rate] : 0;
|
||||
$tax += $user_tax_qty;
|
||||
$this->tax_split[$tax_rate] = $tax;
|
||||
$this->tax_total += $user_tax_qty;
|
||||
|
||||
$this->price_total_net += $user_price_net_qty;
|
||||
$this->points_total += ($item->points * $item->qty);
|
||||
$this->qty_total += $item->qty;
|
||||
|
||||
$this->shop_items[$shop_item_id] = $shop_item;
|
||||
}
|
||||
$this->orders[] = $order;
|
||||
}
|
||||
|
||||
public function calculate(){
|
||||
|
||||
$this->shipping_tax = round($this->shipping - $this->shipping_net, 2);
|
||||
$this->tax_total += $this->shipping_tax;
|
||||
//add shipping tax to split
|
||||
$tax = isset($this->tax_split[config('app.shipping_tax')]) ? $this->tax_split[config('app.shipping_tax')] : 0;
|
||||
$tax += $this->shipping_tax;
|
||||
$this->tax_split[config('app.shipping_tax')] = $tax;
|
||||
|
||||
$this->price_total_net += $this->shipping_net;
|
||||
$this->price_total = round($this->tax_total + $this->price_total_net, 2);
|
||||
|
||||
//$this->price_total += round($user_price * $item->qty, 2);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function store(){
|
||||
//TODO
|
||||
}
|
||||
|
||||
//price brutto calu with
|
||||
private function calcuPriceWith($price, $tax_rate = null, $discount = null){
|
||||
$tax_dec = ($tax_rate + 100) / 100;
|
||||
$price / $tax_dec;
|
||||
$margin = (($discount -100)*-1) / 100;
|
||||
$price = $price * $margin;
|
||||
return round($price, 2);
|
||||
}
|
||||
|
||||
private function calcuTaxWith($price, $tax_rate = null){
|
||||
$tax_dec = ($tax_rate + 100) / 100;
|
||||
$tax = ($price * $tax_dec) - $price;
|
||||
return round($tax, 2);
|
||||
}
|
||||
|
||||
private function setOrderAdress($from, $shopping_user){
|
||||
$ret = "";
|
||||
if($from === 'billing'){
|
||||
$ret .= $shopping_user->billing_company ? 'Firma: '.$shopping_user->billing_company.' | ' : '';
|
||||
$ret .= \App\Services\HTMLHelper::getSalutationLang($shopping_user->billing_salutation).' ';
|
||||
$ret .= $shopping_user->billing_firstname.' ';
|
||||
$ret .= $shopping_user->billing_lastname.' | ';
|
||||
$ret .= $shopping_user->billing_address.' | ';
|
||||
$ret .= $shopping_user->billing_zipcode.' ';
|
||||
$ret .= $shopping_user->billing_city.' | ';
|
||||
$ret .= $shopping_user->billing_country->getLocated().' | ';
|
||||
$ret .= $shopping_user->billing_email;
|
||||
|
||||
}
|
||||
if($from === 'shipping'){
|
||||
if($shopping_user->same_as_billing == 1){
|
||||
return 'Lieferadresse ist gleich Rechnungsadresse';
|
||||
}
|
||||
$ret .= $shopping_user->shipping_company ? 'Firma: '.$shopping_user->shipping_company.' | ' : '';
|
||||
$ret .= \App\Services\HTMLHelper::getSalutationLang($shopping_user->shipping_salutation).' ';
|
||||
$ret .= $shopping_user->shipping_firstname.' ';
|
||||
$ret .= $shopping_user->shipping_lastname.' | ';
|
||||
$ret .= $shopping_user->shipping_address.' | ';
|
||||
$ret .= $shopping_user->shipping_zipcode.' ';
|
||||
$ret .= $shopping_user->shipping_city.' | ';
|
||||
$ret .= $shopping_user->shipping_country->getLocated().' | ';
|
||||
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getTotalTax()
|
||||
{
|
||||
return $this->tax_total;
|
||||
}
|
||||
|
||||
public function getTotalPriceNetto()
|
||||
{
|
||||
return $this->price_total_net;
|
||||
}
|
||||
|
||||
public function getTotalPrice()
|
||||
{
|
||||
return $this->price_total;
|
||||
}
|
||||
|
||||
|
||||
public function getTaxSplit()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue