195 lines
5.2 KiB
PHP
195 lines
5.2 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 Coupon
|
|
*
|
|
* @property int $id
|
|
* @property string $number
|
|
* @property int $customer_id
|
|
* @property int $booking_id
|
|
* @property float $value
|
|
* @property Carbon $issue_date
|
|
* @property Carbon $valid_date
|
|
* @property bool $is_redeemed
|
|
* @property Carbon $redeem_date
|
|
* @property Carbon $created_at
|
|
* @property Carbon $updated_at
|
|
* @property Booking $booking
|
|
* @property Customer $customer
|
|
* @property Collection|Booking[] $bookings
|
|
* @package App\Models
|
|
* @property-read int|null $bookings_count
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon whereBookingId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon whereCustomerId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon whereIsRedeemed($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon whereIssueDate($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon whereNumber($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon whereRedeemDate($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon whereUpdatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon whereValidDate($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon whereValue($value)
|
|
* @property string|null $text
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Coupon whereText($value)
|
|
* @property-read \App\Models\BookingDocument|null $booking_document
|
|
* @mixin \Eloquent
|
|
*/
|
|
class Coupon extends Model
|
|
{
|
|
protected $connection = 'mysql';
|
|
|
|
protected $table = 'coupon';
|
|
|
|
protected $casts = [
|
|
'customer_id' => 'int',
|
|
'booking_id' => 'int',
|
|
'value' => 'float',
|
|
'is_redeemed' => 'bool'
|
|
];
|
|
|
|
protected $dates = [
|
|
'issue_date',
|
|
'valid_date',
|
|
'redeem_date'
|
|
];
|
|
|
|
protected $fillable = [
|
|
'number',
|
|
'customer_id',
|
|
'booking_id',
|
|
'value',
|
|
'issue_date',
|
|
'valid_date',
|
|
'is_redeemed',
|
|
'redeem_date',
|
|
'text'
|
|
];
|
|
|
|
/**
|
|
* The "booted" method of the model.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
static::created(function ($model) {
|
|
$model->number = sprintf(
|
|
'%02u-%05u-%05u-%02u%02u',
|
|
date('y'),
|
|
$model->customer_id,
|
|
$model->id,
|
|
date('H'),
|
|
date('i')
|
|
);
|
|
$model->save();
|
|
});
|
|
}
|
|
|
|
public function create($product)
|
|
{
|
|
logger($product->id);
|
|
}
|
|
|
|
public function booking()
|
|
{
|
|
return $this->belongsTo(Booking::class);
|
|
}
|
|
|
|
public function customer()
|
|
{
|
|
return $this->belongsTo(Customer::class);
|
|
}
|
|
|
|
public function booking_document()
|
|
{
|
|
return $this->hasOne(BookingDocument::class, 'coupon_id', 'id');
|
|
}
|
|
|
|
|
|
public function bookings()
|
|
{
|
|
return $this->hasMany(Booking::class);
|
|
}
|
|
|
|
public function isLegal(){
|
|
//TODO
|
|
// return strtotime(date('Y-m-d')) <= strtotime($this->getValidDate());
|
|
|
|
return false;
|
|
}
|
|
|
|
public function getValueFormatted()
|
|
{
|
|
return Util::_number_format($this->attributes['value']);
|
|
}
|
|
|
|
public function getValueRaw()
|
|
{
|
|
return $this->attributes['value'];
|
|
}
|
|
|
|
public function setValueAttribute($value)
|
|
{
|
|
$this->attributes['value'] = Util::_clean_float($value);
|
|
}
|
|
|
|
public function getIssueDateFormatted()
|
|
{
|
|
return isset($this->attributes['issue_date']) ? Carbon::parse($this->attributes['issue_date'])->format('d.m.Y') : '';
|
|
}
|
|
|
|
public function setIssueDateAttribute($value)
|
|
{
|
|
if (!$value) {
|
|
$this->attributes['issue_date'] = null;
|
|
} else {
|
|
$this->attributes['issue_date'] = Carbon::parse($value)->format('Y-m-d');
|
|
}
|
|
}
|
|
|
|
public function getValidDateFormatted()
|
|
{
|
|
return isset($this->attributes['valid_date']) ? Carbon::parse($this->attributes['valid_date'])->format('d.m.Y') : '';
|
|
}
|
|
|
|
public function setValidDateAttribute($value)
|
|
{
|
|
if (!$value) {
|
|
$this->attributes['valid_date'] = null;
|
|
} else {
|
|
$this->attributes['valid_date'] = Carbon::parse($value)->format('Y-m-d');
|
|
}
|
|
}
|
|
|
|
public function getRedeemDateFormatted()
|
|
{
|
|
return isset($this->attributes['redeem_date']) ? Carbon::parse($this->attributes['redeem_date'])->format('d.m.Y') : '';
|
|
}
|
|
|
|
public function setRedeemDateAttribute($value)
|
|
{
|
|
if (!$value) {
|
|
$this->attributes['redeem_date'] = null;
|
|
} else {
|
|
$this->attributes['redeem_date'] = Carbon::parse($value)->format('Y-m-d');
|
|
}
|
|
}
|
|
|
|
|
|
}
|