194 lines
5.9 KiB
PHP
194 lines
5.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Created by Reliese Model.
|
|
*/
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use App\Services\Util;
|
|
use Auth;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
/**
|
|
* Class PromotionAdminProduct
|
|
*
|
|
* @property int $id
|
|
* @property int $promotion_admin_id
|
|
* @property int $product_id
|
|
* @property bool $own_price
|
|
* @property float|null $price
|
|
* @property float|null $tax
|
|
* @property float|null $price_old
|
|
* @property bool $calcu_commission
|
|
* @property int|null $max_items
|
|
* @property int|null $used_items
|
|
* @property bool $shipping
|
|
* @property bool $active
|
|
* @property Carbon|null $created_at
|
|
* @property Carbon|null $updated_at
|
|
* @property Product $product
|
|
* @property PromotionAdmin $promotion_admin
|
|
* @property Collection|PromotionUserProduct[] $promotion_user_products
|
|
* @package App\Models
|
|
* @property-read int|null $promotion_user_products_count
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereActive($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereCalcuCommission($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereMaxItems($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereOwnPrice($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct wherePrice($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct wherePriceOld($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereProductId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct wherePromotionAdminId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereShipping($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereTax($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereUpdatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdminProduct whereUsedItems($value)
|
|
* @mixin \Eloquent
|
|
*/
|
|
class PromotionAdminProduct extends Model
|
|
{
|
|
protected $table = 'promotion_admin_products';
|
|
|
|
protected $casts = [
|
|
'promotion_admin_id' => 'int',
|
|
'product_id' => 'int',
|
|
'own_price' => 'bool',
|
|
'price' => 'float',
|
|
'tax' => 'float',
|
|
'price_old' => 'float',
|
|
'calcu_commission' => 'bool',
|
|
'max_items' => 'int',
|
|
'used_items' => 'int',
|
|
'shipping' => 'bool',
|
|
'active' => 'bool'
|
|
];
|
|
|
|
protected $fillable = [
|
|
'promotion_admin_id',
|
|
'product_id',
|
|
'own_price',
|
|
'price',
|
|
'tax',
|
|
'price_old',
|
|
'calcu_commission',
|
|
'max_items',
|
|
'used_items',
|
|
'shipping',
|
|
'active'
|
|
];
|
|
|
|
public function product()
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
|
|
public function promotion_admin()
|
|
{
|
|
return $this->belongsTo(PromotionAdmin::class);
|
|
}
|
|
|
|
public function promotion_user_products()
|
|
{
|
|
return $this->hasMany(PromotionUserProduct::class);
|
|
}
|
|
|
|
public function setPriceAttribute( $value ) {
|
|
|
|
$this->attributes['price'] = $value !== null ? Util::reFormatNumber($value) : null;
|
|
}
|
|
|
|
public function getFormattedPrice()
|
|
{
|
|
return isset($this->attributes['price']) ? Util::formatNumber($this->attributes['price']) : "";
|
|
}
|
|
|
|
public function getRealPrice()
|
|
{
|
|
if($this->own_price && $this->price !== null){
|
|
return $this->price;
|
|
}
|
|
if($this->product && $this->product->price){
|
|
return $this->product->price;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public function getFormattedRealPrice()
|
|
{
|
|
return Util::formatNumber($this->getRealPrice());
|
|
}
|
|
|
|
/*price calcu Marign single_commission Factor*/
|
|
private function calcPriceCommissionFactor($price){
|
|
//einzelrabatt aus produkt
|
|
$margin = 100;
|
|
if(isset($this->product) && $this->product->single_commission){
|
|
$margin = $this->product->value_commission;
|
|
}else{
|
|
//rabatt aus user level
|
|
$user = Auth::user();
|
|
if($user && $user->user_level && $user->user_level->user_level_margins){
|
|
if($user_level_margin = $user->user_level->user_level_margins_re->first()){
|
|
$margin = $user_level_margin->trading_margin;
|
|
}
|
|
}
|
|
}
|
|
$margin = (100 - $margin) / 100;
|
|
$price = $price * $margin;
|
|
return $price;
|
|
}
|
|
/*price net*/
|
|
private function calcPriceNet($price){
|
|
$tax = isset($this->product->tax) ? $this->product->tax : 0;
|
|
$tax_rate = ($tax + 100) / 100;
|
|
return $price / $tax_rate;
|
|
}
|
|
//price calu with
|
|
public function getPriceWith(Bool $net = true){
|
|
$price = $this->getRealPrice();
|
|
$price = $net ? $this->calcPriceNet($price) : $price;
|
|
$price = $this->calcu_commission ? $this->calcPriceCommissionFactor($price) : $price;
|
|
return round($price, 2);
|
|
}
|
|
/*out*/
|
|
public function getFormattedPriceWith(Bool $net = true)
|
|
{
|
|
return Util::formatNumber($this->getPriceWith($net));
|
|
}
|
|
|
|
public function canDelete(){
|
|
if($this->promotion_user_products->count() === 0){
|
|
return true;
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
public function getPromotionUserProducts($user_promotion, $key = false)
|
|
{
|
|
$user_promotion_product = $user_promotion->hasPromotionUserProducts($this->id);
|
|
if($user_promotion_product){
|
|
if(!$key){
|
|
return $user_promotion_product;
|
|
}
|
|
if(isset($user_promotion_product->{$key})){
|
|
return $user_promotion_product->{$key};
|
|
}else{
|
|
return 0;
|
|
}
|
|
}
|
|
return 0;
|
|
//return $this->hasMany(PromotionUserProduct::class)->where('promotion_admin_product_id', $promotion_admin_product->id)->first();
|
|
}
|
|
|
|
|
|
|
|
}
|