209 lines
6.1 KiB
PHP
209 lines
6.1 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Created by Reliese Model.
|
|
*/
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use App\Services\Util;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
/**
|
|
* Class PromotionAdmin
|
|
*
|
|
* @property int $id
|
|
* @property string $name
|
|
* @property string|null $description
|
|
* @property Carbon|null $from
|
|
* @property Carbon|null $to
|
|
* @property float|null $max_bugdet
|
|
* @property int|null $max_items
|
|
* @property bool $active
|
|
* @property Carbon|null $created_at
|
|
* @property Carbon|null $updated_at
|
|
* @property Collection|Product[] $products
|
|
* @property Collection|PromotionUserProduct[] $promotion_user_products
|
|
* @property Collection|PromotionUser[] $promotion_users
|
|
* @package App\Models
|
|
* @property-read int|null $products_count
|
|
* @property-read int|null $promotion_user_products_count
|
|
* @property-read int|null $promotion_users_count
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereActive($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereDescription($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereFrom($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereMaxBugdet($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereMaxItems($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereName($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereTo($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereUpdatedAt($value)
|
|
* @mixin \Eloquent
|
|
* @property int|null $type
|
|
* @property bool $shop
|
|
* @property-read Collection|\App\Models\PromotionAdminProduct[] $promotion_admin_products
|
|
* @property-read int|null $promotion_admin_products_count
|
|
* @property-read Collection|\App\Models\PromotionAdminProduct[] $promotion_admin_products_active
|
|
* @property-read int|null $promotion_admin_products_active_count
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereShop($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|PromotionAdmin whereType($value)
|
|
*/
|
|
class PromotionAdmin extends Model
|
|
{
|
|
protected $table = 'promotion_admins';
|
|
|
|
protected $casts = [
|
|
'max_bugdet' => 'float',
|
|
'max_items' => 'int',
|
|
'active' => 'bool',
|
|
'type' => 'int',
|
|
'shop' => 'bool',
|
|
|
|
|
|
];
|
|
|
|
protected $dates = [
|
|
'from',
|
|
'to'
|
|
];
|
|
|
|
protected $fillable = [
|
|
'type',
|
|
'name',
|
|
'description',
|
|
'from',
|
|
'to',
|
|
'shop',
|
|
'max_bugdet',
|
|
'max_items',
|
|
'active'
|
|
];
|
|
|
|
public $promotionType = [
|
|
1 => 'verschenken',
|
|
2 => 'Nachlass (not built in!)',
|
|
3 => 'Gewinnspiel (not built in!)',
|
|
];
|
|
|
|
public function products()
|
|
{
|
|
return $this->belongsToMany(Product::class, 'promotion_admin_products')
|
|
->withPivot('id', 'own_price', 'price', 'tax', 'price_old', 'calcu_commission', 'max_items', 'used_items', 'shipping', 'active')
|
|
->withTimestamps();
|
|
}
|
|
|
|
|
|
public function promotion_admin_products()
|
|
{
|
|
return $this->hasMany(PromotionAdminProduct::class);
|
|
}
|
|
|
|
public function promotion_admin_products_active()
|
|
{
|
|
return $this->hasMany(PromotionAdminProduct::class)->where('active', 1);
|
|
}
|
|
|
|
public function promotion_user_products()
|
|
{
|
|
return $this->hasMany(PromotionUserProduct::class);
|
|
}
|
|
|
|
public function promotion_users()
|
|
{
|
|
return $this->hasMany(PromotionUser::class);
|
|
}
|
|
|
|
public function getPromotionType(){
|
|
if(isset($this->promotionType[$this->type])){
|
|
return $this->promotionType[$this->type];
|
|
}
|
|
}
|
|
|
|
public function isActive(){
|
|
if($this->active){
|
|
if($this->from && Carbon::parse($this->from)->gt(Carbon::now()->startOfDay())){
|
|
return false;
|
|
}
|
|
if($this->to && Carbon::parse($this->to)->lt(Carbon::now()->startOfDay())){
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function getFromAttribute($value)
|
|
{
|
|
if(!$value){ return ""; }
|
|
return Carbon::parse($value)->format(\Util::formatDateDB());
|
|
}
|
|
|
|
public function getFromRaw(){
|
|
return isset($this->attributes['from']) ? $this->attributes['from'] : NULL;
|
|
}
|
|
|
|
public function setFromAttribute( $value ) {
|
|
$this->attributes['from'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL;
|
|
}
|
|
|
|
public function getToAttribute($value)
|
|
{
|
|
if(!$value){ return ""; }
|
|
return Carbon::parse($value)->format(\Util::formatDateDB());
|
|
}
|
|
|
|
public function getToRaw(){
|
|
return isset($this->attributes['to']) ? $this->attributes['to'] : NULL;
|
|
}
|
|
|
|
public function setToAttribute( $value ) {
|
|
$this->attributes['to'] = isset($value) ? (new Carbon($value))->format('Y-m-d') : NULL;
|
|
}
|
|
|
|
|
|
public function setMaxBugdetAttribute( $value ) {
|
|
|
|
$this->attributes['max_bugdet'] = $value ? Util::reFormatNumber($value) : null;
|
|
}
|
|
|
|
public function getFormattedMaxBugdet()
|
|
{
|
|
return isset($this->attributes['max_bugdet']) ? Util::formatNumber($this->attributes['max_bugdet']) : "";
|
|
}
|
|
|
|
|
|
public function canDelete(){
|
|
|
|
if($this->promotion_users->count() === 0 && $this->promotion_user_products->count() === 0){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
public static function getActiveAdminPromotionsAsArray(){
|
|
$PromotionAdmins = PromotionAdmin::where('active', true)->get();
|
|
$ret = [];
|
|
foreach($PromotionAdmins as $PromotionAdmin){
|
|
$temp = $PromotionAdmin->name;
|
|
$temp .= $PromotionAdmin->from ? " | vom: ".$PromotionAdmin->from : '';
|
|
$temp .= $PromotionAdmin->to ? " | bis: ".$PromotionAdmin->to : '';
|
|
$ret[$PromotionAdmin->id] = $temp;
|
|
}
|
|
/*->where(function ($query) {
|
|
$query->where('from', '<', Carbon::now())
|
|
->orWhereNull('from');
|
|
})
|
|
->where(function ($query) {
|
|
$query->where('to', '>=', Carbon::now())
|
|
->orWhereNull('to');
|
|
});*/
|
|
return $ret;
|
|
}
|
|
}
|