211 lines
No EOL
8.7 KiB
PHP
211 lines
No EOL
8.7 KiB
PHP
<?php
|
|
namespace App\Services;
|
|
|
|
use App\Models\ShoppingCollectOrder;
|
|
use stdClass;
|
|
use App\Models\ShoppingOrder;
|
|
use Auth;
|
|
|
|
class ShopApiOrderCart
|
|
{
|
|
|
|
public $shoppingCollectOrder;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->shoppingCollectOrder = new ShoppingCollectOrder();
|
|
$this->shoppingCollectOrder->shipping = 0;
|
|
$this->shoppingCollectOrder->shipping_net = 0;
|
|
$this->shoppingCollectOrder->shipping_tax = 0;
|
|
$this->shoppingCollectOrder->points = 0;
|
|
$this->shoppingCollectOrder->price_total_net = 0;
|
|
$this->shoppingCollectOrder->price_total = 0;
|
|
$this->shoppingCollectOrder->tax_total = 0;
|
|
$this->shoppingCollectOrder->qty_total = 0;
|
|
|
|
$this->shoppingCollectOrder->tax_split = [];
|
|
$this->shoppingCollectOrder->orders = [];
|
|
$this->shoppingCollectOrder->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->shoppingCollectOrder->shipping += $shopping_order->shipping;
|
|
$this->shoppingCollectOrder->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->shoppingCollectOrder->shop_items[$shop_item_id])){
|
|
$shop_item = $this->shoppingCollectOrder->shop_items[$shop_item_id];
|
|
if ($shop_item instanceof stdClass) {
|
|
$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 = round($user_price_net, 2);
|
|
$shop_item->user_price_total_net = round($user_price_net_qty, 2);
|
|
$shop_item->user_tax = round($user_tax, 2);
|
|
$shop_item->user_tax_total = round($user_tax_qty, 2);
|
|
$shop_item->points_total = ($item->points * $item->qty);
|
|
}
|
|
|
|
//only for tax split / tax and price on calculate function
|
|
|
|
$this->shoppingCollectOrder->addTaxToSplit($tax_rate, $user_tax_qty);
|
|
$this->shoppingCollectOrder->addNetToSplit($tax_rate, $user_price_net_qty);
|
|
$this->shoppingCollectOrder->tax_total += $user_tax_qty;
|
|
$this->shoppingCollectOrder->price_total_net += $user_price_net_qty;
|
|
$this->shoppingCollectOrder->points += ($item->points * $item->qty);
|
|
$this->shoppingCollectOrder->qty_total += $item->qty;
|
|
|
|
$this->shoppingCollectOrder->addShopItem($shop_item_id, $shop_item);
|
|
}
|
|
$this->shoppingCollectOrder->addOrder($order);
|
|
}
|
|
|
|
public function calculate(){
|
|
|
|
$this->shoppingCollectOrder->shipping_tax = round($this->shoppingCollectOrder->shipping - $this->shoppingCollectOrder->shipping_net, 2);
|
|
$this->shoppingCollectOrder->tax_total += $this->shoppingCollectOrder->shipping_tax;
|
|
//add shipping tax to split
|
|
$this->shoppingCollectOrder->addTaxToSplit(config('app.main_tax_rate'), $this->shoppingCollectOrder->shipping_tax);
|
|
$this->shoppingCollectOrder->addNetToSplit(config('app.main_tax_rate'), $this->shoppingCollectOrder->shipping_net);
|
|
|
|
$this->shoppingCollectOrder->price_total_net += $this->shoppingCollectOrder->shipping_net;
|
|
$this->shoppingCollectOrder->price_total = round($this->shoppingCollectOrder->tax_total + $this->shoppingCollectOrder->price_total_net, 2);
|
|
}
|
|
|
|
public function store(){
|
|
$this->shoppingCollectOrder->user_id = \Auth::user()->id;
|
|
$this->shoppingCollectOrder->status = 1;
|
|
//remove shopping_order
|
|
$temp = [];
|
|
foreach($this->orders as $order){
|
|
$order->shopping_order = null;
|
|
$temp[] = $order;
|
|
}
|
|
$this->shoppingCollectOrder->orders = $temp;
|
|
$this->shoppingCollectOrder->save();
|
|
}
|
|
|
|
//price brutto calu with
|
|
private function calcuPriceWith($price, $tax_rate = null, $discount = null){
|
|
$tax_dec = ($tax_rate + 100) / 100;
|
|
$price = $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 __get($property) {
|
|
if (property_exists($this->shoppingCollectOrder, $property)) {
|
|
return $this->shoppingCollectOrder->$property;
|
|
}
|
|
if (isset($this->shoppingCollectOrder->{$property})) {
|
|
return $this->shoppingCollectOrder->{$property};
|
|
}
|
|
}
|
|
|
|
public function getTotalTax()
|
|
{
|
|
return $this->shoppingCollectOrder->tax_total;
|
|
}
|
|
|
|
public function getTotalPriceNetto()
|
|
{
|
|
return $this->shoppingCollectOrder->price_total_net;
|
|
}
|
|
|
|
public function getTotalPrice()
|
|
{
|
|
return $this->shoppingCollectOrder->price_total;
|
|
}
|
|
|
|
public function getTaxSplit()
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
public static function finishOrder(ShoppingCollectOrder $shoppingCollectOrder){
|
|
|
|
//get orders an set
|
|
foreach($shoppingCollectOrder->orders as $order){
|
|
$ShoppingOrder = ShoppingOrder::findOrFail($order['order_id']);
|
|
$ShoppingOrder->api_status = 2; //bestellt
|
|
$api_notice = $ShoppingOrder->api_notice;
|
|
$api_notice['shopping_order_id'] = $shoppingCollectOrder->shopping_order_id;
|
|
$ShoppingOrder->api_notice = $api_notice;
|
|
$ShoppingOrder->txaction = 'extern_paid';
|
|
$ShoppingOrder->save();
|
|
}
|
|
$shoppingCollectOrder->status = 2; //order
|
|
$shoppingCollectOrder->save();
|
|
}
|
|
|
|
} |