gruene-seele/app/Models/PaymentMethod.php
2022-01-18 18:30:14 +01:00

120 lines
3.9 KiB
PHP

<?php
/**
* Created by Reliese Model.
*/
namespace App\Models;
use App\Services\Type;
use App\Services\Util;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Class PaymentMethod
*
* @property int $id
* @property string $name
* @property string $short
* @property int $show_at
* @property int $pos
* @property bool $active
* @property bool $default
* @property Carbon $created_at
* @property Carbon $updated_at
* @package App\Models
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereActive($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereDefault($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod wherePos($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereShort($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereShowAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereUpdatedAt($value)
* @mixin \Eloquent
* @property string|null $max_price
* @method static \Illuminate\Database\Eloquent\Builder|PaymentMethod whereMaxPrice($value)
* @property array $show_on
* @method static \Illuminate\Database\Eloquent\Builder|PaymentMethod whereShowOn($value)
*/
class PaymentMethod extends Model
{
protected $table = 'payment_methods';
protected $casts = [
'show_at' => 'int',
'pos' => 'int',
'active' => 'bool',
'default' => 'bool',
'show_on' => 'array',
];
protected $fillable = [
'name',
'short',
'show_at',
'show_on',
'max_price',
'pos',
'default',
'active'
];
public function getShowAtType(){
return isset(Type::$payShowATs[$this->show_at]) ? Type::$payShowATs[$this->show_at] : '-';
}
public function getShowOnTypes(){
$ret = [];
if($this->show_on){
foreach($this->show_on as $show){
$ret[] = isset(Type::$payShowONs[$show]) ? Type::$payShowONs[$show] : '-';
}
}
return $ret;
}
public static function getDefaultAsArray($short=false){
return PaymentMethod::where('active', true)->where('default', true)->pluck('id');
}
public function setMaxPriceAttribute($value)
{
$this->attributes['max_price'] = $value ? Util::reFormatNumber($value) : null;
}
public function getFormattedMaxPrice()
{
return isset($this->attributes['max_price']) ? Util::formatNumber($this->attributes['max_price']) : "";
}
/**
* @param $short //PP FNC VOR CC SB ...
* @param $user_payment_methods //PaymentMethod [] IDs
* @param $total //Full price
*/
public static function isShowPaymentMethod($short, $user_payment_methods, $total = 0){
$payment_method = PaymentMethod::whereShort($short)->first();
if($payment_method && $payment_method->active){
if(in_array($payment_method->id, $user_payment_methods)){
if($total > 0 && $payment_method->max_price > 0){
if($payment_method->max_price >= $total){
return true;
}
}else{
return true;
}
}
}
return false;
}
}