gruene-seele/app/Models/PromotionAdmin.php
2021-10-15 16:35:47 +02:00

181 lines
5.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
*/
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 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(){
$query = PromotionAdmin::where('active', true)
->where(function ($query) {
$query->where('from', '<', Carbon::now())
->orWhereNull('from');
})
->where(function ($query) {
$query->where('to', '>=', Carbon::now())
->orWhereNull('to');
});
return $query->pluck('name', 'id')->toArray();
}
}