mivita/app/Models/ShoppingOrder.php
2022-04-14 13:14:36 +02:00

480 lines
17 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* App\Models\ShoppingOrder
*
* @property int $id
* @property int $shopping_user_id
* @property int|null $auth_user_id
* @property int $country_id
* @property int $user_shop_id
* @property float|null $total
* @property float|null $shipping
* @property float|null $subtotal
* @property float|null $tax_rate
* @property float|null $tax
* @property float|null $total_shipping
* @property int|null $weight
* @property int|null $paid
* @property string|null $txaction
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \App\User|null $auth_user
* @property-read \App\Models\Country $country
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShoppingOrderItem[] $shopping_order_items
* @property-read int|null $shopping_order_items_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShoppingPayment[] $shopping_payments
* @property-read int|null $shopping_payments_count
* @property-read \App\Models\ShoppingUser $shopping_user
* @property-read \App\Models\UserShop $user_shop
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereAuthUserId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereCountryId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder wherePaid($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereShipping($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereShoppingUserId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereSubtotal($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTax($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTaxRate($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTotal($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTotalShipping($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTxaction($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereUserShopId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereWeight($value)
* @mixin \Eloquent
* @property int|null $payment_for
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder wherePaymentFor($value)
* @property int|null $member_id
* @property string|null $mode
* @property-read \App\User|null $member
* @property-read \App\Models\UserHistory|null $user_history
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereMemberId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereMode($value)
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property string|null $user_deleted_at
* @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingOrder onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereUserDeletedAt($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingOrder withTrashed()
* @method static \Illuminate\Database\Query\Builder|\App\Models\ShoppingOrder withoutTrashed()
* @property-read \App\Models\ShippingCountry $shipping_country
* @property float|null $shipping_net
* @property float|null $subtotal_ws
* @property int|null $points
* @property int|null $shipped
* @property string|null $tracking
* @property string|null $wp_invoice_path
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder wherePoints($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereShipped($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereShippingNet($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereSubtotalWs($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereTracking($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingOrder whereWpInvoicePath($value)
* @property int|null $homeparty_id
* @property array|null $wp_notice
* @property-read \App\Models\Homeparty|null $homeparty
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereHomepartyId($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingOrder whereWpNotice($value)
*/
class ShoppingOrder extends Model
{
protected $table = 'shopping_orders';
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $fillable = [
'shopping_user_id',
'auth_user_id',
'member_id',
'homeparty_id',
'payment_for',
'country_id',
'user_shop_id',
'total',
'subtotal',
'shipping',
'shipping_net',
'subtotal_ws',
'tax',
'tax_split',
'total_shipping',
'points',
'weight',
'paid',
'txaction',
'wp_invoice_path',
'wp_notice',
'mode',
'shipped',
'tracking'
];
protected $casts = [
'wp_notice' => 'array',
'tax_split' => 'array',
];
public static $shippedTypes = [
0 => 'offen',
1 => 'in Bearbeitung',
2 => 'versendet',
3 => 'abgeschlossen',
10 => 'storniert'
];
public static $apiShippedTypes = [
0 => 'open', //(Fullfilment durch Händler)',
1 => 'process', //(Fullfilment durch MIVITA: nicht Versand)
2 => 'sent', //(Fullfilment durch MIVITA: Versand erfolgt)'
3 => 'close', //(Fullfilment durch MIVITA: Versand erfolgt)',
10 => 'cancel'
];
public static $shippedColors = [
0 => 'warning',
1 => 'info',
2 => 'success',
3 => 'secondary',
10 => 'danger',
];
public static $paymentForTypes = [
0 => '',
1 => 'Registrierung',
2 => 'Mitgliedschaft',
3 => 'Bestellung',
4 => 'Kundenbestellung',
5 => 'Homeparty',
6 => 'Shop',
7 => 'extern',
10 => '',
];
public static $paymentForColors = [
0 => 'default',
1 => 'warning',
2 => 'warning',
3 => 'secondary',
4 => 'info',
5 => 'dark',
6 => 'secondary',
7 => 'dark',
8 => 'info',
9 => 'default',
10 => 'info',
11 => 'default'
];
public function shopping_user()
{
return $this->belongsTo('App\Models\ShoppingUser','shopping_user_id');
}
public function country()
{
return $this->belongsTo('App\Models\ShippingCountry','country_id');
}
public function shipping_country()
{
return $this->belongsTo('App\Models\ShippingCountry','country_id');
}
public function homeparty()
{
return $this->belongsTo('App\Models\Homeparty','homeparty_id');
}
public function user_shop()
{
return $this->belongsTo('App\Models\UserShop','user_shop_id');
}
//can null
public function member()
{
return $this->belongsTo('App\User','member_id');
}
//can null
public function auth_user()
{
return $this->belongsTo('App\User','auth_user_id');
}
public function user_history()
{
return $this->hasOne('App\Models\UserHistory','shopping_order_id')->latest();
}
public function user_invoice(){
return $this->hasOne('App\Models\UserInvoice', 'shopping_order_id', '');
}
public function shopping_order_items(){
return $this->hasMany('App\Models\ShoppingOrderItem', 'shopping_order_id');
}
public function shopping_payments(){
return $this->hasMany('App\Models\ShoppingPayment', 'shopping_order_id');
}
public function user_sales_volume(){
return $this->hasMany('App\Models\UserSalesVolume', 'shopping_order_id');
}
public function user_sales_volume_no_userid(){
return $this->hasMany('App\Models\UserSalesVolume', 'shopping_order_id')->where('user_id', '=', NULL)->first();
}
public function setUserHistoryValue($values = []){
if($user_history = $this->user_history){
foreach ($values as $key=>$val){
$user_history->{$key} = $val;
}
$user_history->save();
}
}
public function getLastShoppingPayment($key=false){
$shopping_payment = $this->shopping_payments->last();
if($shopping_payment){
if($key === 'getPaymentType'){
return $shopping_payment->getPaymentType();
}
if($key === 'reference'){
return $shopping_payment->reference;
}
}
return "";
}
public function getShippedType(){
return isset(self::$shippedTypes[$this->shipped]) ? self::$shippedTypes[$this->shipped] : "";
}
public function getAPIShippedType(){
return isset(self::$apiShippedTypes[$this->shipped]) ? self::$apiShippedTypes[$this->shipped] : "free";
}
public function getShippedColor(){
return isset(self::$shippedColors[$this->shipped]) ? self::$shippedColors[$this->shipped] : "default";
}
public function getFormattedTotal()
{
return formatNumber($this->attributes['total']);
}
public function getFormattedSubtotal()
{
return formatNumber($this->attributes['subtotal']);
}
public function getFormattedShipping()
{
return formatNumber($this->attributes['shipping']);
}
public function getFormattedShippingNet()
{
return formatNumber($this->attributes['shipping_net']);
}
public function getFormattedSubtotalWs()
{
return formatNumber($this->attributes['subtotal_ws']);
}
public function getFormattedSubtotalShipping()
{
return formatNumber($this->attributes['subtotal_shipping']);
}
public function getFormattedTax()
{
return formatNumber($this->attributes['tax']);
}
public function getFormattedTotalShipping()
{
return formatNumber($this->attributes['total_shipping']);
}
public function getPriceVkNetBy($product_id)
{
if($product = Product::find($product_id)){
if($this->shipping_country && $this->shipping_country->country){
return $product->getPriceWith(true, false, $this->shipping_country->country);
}
}
return 0;
}
public function getPaymentForType(){
return isset(self::$paymentForTypes[$this->payment_for]) ? self::$paymentForTypes[$this->payment_for] : "";
}
public function getPaymentForColor(){
return isset(self::$paymentForColors[$this->payment_for]) ? self::$paymentForColors[$this->payment_for] : "";
}
public function getUserDiscount()
{
if($this->auth_user && $this->auth_user->user_level){
return $this->auth_user->user_level->getFormattedMargin();
}
if($this->member && $this->member->user_level){
return $this->member->user_level->getFormattedMargin();
}
return 0;
}
public function getItemsCount(){
$count = 0;
if($this->shopping_order_items){
foreach ($this->shopping_order_items as $shopping_order_item){
$count += $shopping_order_item->qty;
}
}
return $count;
}
public function isInvoice(){
return $this->user_invoice ? true : false;
}
public function getStatusByOrder(){
if($this->payment_for){
if($this->payment_for === 6){ //Kunde-Shop
return 2;
}
return 1;
}
return 0;
}
public function makeHomepartyTaxSplit()
{
$tax_split = [];
if($this->homeparty){
foreach($this->homeparty->homeparty_order_items as $item){
$tax_rate = intval($item->tax_rate);
if($tax_rate > 0){
$vk_tax = round((($item->price - $item->price_net) * $item->qty), 2);
$ek_tax = round((($item->ek_price - $item->ek_price_net) * $item->qty), 2);
if(isset($tax_split[$tax_rate])){
$tax_split[$tax_rate]['vk_tax'] = round($tax_split[$tax_rate]['vk_tax'] + $vk_tax, 2);
$tax_split[$tax_rate]['ek_tax'] = round($tax_split[$tax_rate]['ek_tax'] + $ek_tax, 2);
}else{
$tax_split[$tax_rate] = ['vk_tax' => $vk_tax, 'ek_tax' => $ek_tax];
}
}
}
$order_vk_tax = 0;
$order_ek_tax = 0;
if($this->homeparty->order){
if(isset($this->homeparty->order['ek_price_net'])){
$order_vk_tax = round((($this->homeparty->order['price'] - $this->homeparty->order['price_net'])), 2);
$order_ek_tax = round((($this->homeparty->order['ek_price'] - $this->homeparty->order['ek_price_net'])), 2);
}
}
if(isset($tax_split[16])){
$tax_split[16] = ['vk_tax' => $order_vk_tax, 'ek_tax' => $order_ek_tax];
}
if(isset($tax_split[19])){
$tax_split[19] = ['vk_tax' => $order_vk_tax, 'ek_tax' => $order_ek_tax];
}
if(isset($tax_split[5])){
if(!isset($tax_split[16])){
$tax_split[16] = ['vk_tax' => $order_vk_tax, 'ek_tax' => $order_ek_tax];
}
$tax_split[16]['vk_tax'] = round($tax_split[16]['vk_tax'] - $tax_split[5]['vk_tax'], 2);
$tax_split[16]['ek_tax'] = round($tax_split[16]['ek_tax'] - $tax_split[5]['ek_tax'], 2);
}
if(isset($tax_split[7])){
if(!isset($tax_split[19])){
$tax_split[19] = ['vk_tax' => $order_vk_tax, 'ek_tax' => $order_ek_tax];
}
$tax_split[19]['vk_tax'] = round($tax_split[19]['vk_tax'] - $tax_split[7]['vk_tax'], 2);
$tax_split[19]['ek_tax'] = round($tax_split[19]['ek_tax'] - $tax_split[7]['ek_tax'], 2);
}
foreach($tax_split as $key=>$value){
$tax_split[$key]['vk_tax'] = number_format($value['vk_tax'], 2);
$tax_split[$key]['ek_tax'] = number_format($value['ek_tax'], 2);
}
}
if(!isset($tax_split[16]) && !isset($tax_split[19])){
$tax_split = NULL;
}
$this->tax_split = $tax_split;
$this->save();
}
public function makeTaxSplit()
{
$tax_split = NULL;
if($this->tax > 0){
$tax_split = [];
foreach($this->shopping_order_items as $item){
$tax_rate = intval($item->tax_rate);
if($tax_rate > 0){
$tax_split[$tax_rate] = isset($tax_split[$tax_rate]) ?
round($tax_split[$tax_rate] + ($item->tax * $item->qty), 2) :
round(($item->tax * $item->qty), 2);
}
}
if(isset($tax_split[16])){
$tax_split[16] = $this->tax;
}
if(isset($tax_split[19])){
$tax_split[19] = $this->tax;
}
if(isset($tax_split[5])){
if(!isset($tax_split[16])){
$tax_split[16] = $this->tax;
}
$tax_split[16] = round($tax_split[16] - $tax_split[5], 2);
}
if(isset($tax_split[7])){
if(!isset($tax_split[19])){
$tax_split[19] = $this->tax;
}
$tax_split[19] = round($tax_split[19] - $tax_split[7], 2);
}
foreach($tax_split as $key=>$value){
$tax_split[$key] = number_format($value, 2);
}
}
$this->tax_split = $tax_split;
$this->save();
}
}