Files
pingan_unionpay_new/app/Models/Coupon.php
2023-05-16 11:23:35 +08:00

188 lines
4.2 KiB
PHP

<?php
namespace App\Models;
use App\Models\Traits\BelongsToOutlet;
use App\Models\Traits\BelongsToUser;
use Illuminate\Database\Eloquent\Relations\HasOne;
class Coupon extends Model
{
use BelongsToUser, BelongsToOutlet;
protected $dates = [
'paid_at',
];
const TYPE_PINGAN = 1;
const TYPE_YSD = 2;
const TYPES = [
self::TYPE_PINGAN => '平安券',
self::TYPE_YSD => '自有券',
];
//状态
public function getStatusTextAttribute(): string
{
switch ($this->status) {
case 0:
return '未使用';
break;
case 2:
return '核销成功';
break;
case 3:
return '核销失败';
break;
case 4:
return '已分润';
break;
case 5:
return '已撤销';
break;
default:
return '未知状态';
break;
}
}
public function getProfitTextAttribute(): string
{
switch ($this->is_profit) {
case 0:
return '未打款';
break;
case 1:
return '已打款';
break;
default:
return '未知状态';
break;
}
}
/**
* 是否可以分润
*
* @return bool [type] [description]
* @author 玄尘 2020-03-13
*/
public function canProfit(): bool
{
return ($this->is_profit === 0 && $this->status == 2);
}
/**
* 分润
*
* @return bool|string [type] [description]
* @author 玄尘 2020-03-13
*/
public function profit()
{
$log = AccountLog::where('source->coupon_id', $this->id)->first();
if (! $log && $this->user && $this->profit) {
return $this->user->account->rule('freeze', $this->profit, false, [
'coupon_id' => $this->id,
'redemptionCode' => $this->redemptionCode,
]);
return true;
} else {
return '已经分润过了';
}
}
/**
* Notes: 撤销
*
* @Author: 玄尘
* @Date : 2020/10/12 13:56
*/
public function reversal()
{
$this->status = 5;
$this->remark = '撤销成功';
$this->save();
if ($this->profit > 0) {
return $this->user->account->rule('refreeze', -$this->profit, false, [
'coupon_id' => $this->id,
'redemptionCode' => $this->redemptionCode,
]);
} else {
return '不需要操作';
}
}
/**
* Notes: 撤销分润
*
* @Author: 玄尘
* @Date : 2020/10/9 14:07
*/
public function reprofit()
{
if ($this->profit > 1) {
return $this->user->account->rule('refreeze', -$this->profit, false, [
'coupon_id' => $this->id,
'redemptionCode' => $this->redemptionCode,
]);
} else {
return '不需要操作';
}
}
/**
* 打款
*
* @return bool|string [type] [description]
* @author 玄尘 2020-03-13
*/
public function sendMoney()
{
if ($this->canProfit()) {
$res = $this->user->account->rule('profit', $this->profit, false, [
'coupon_id' => $this->id,
'redemptionCode' => $this->redemptionCode,
]);
if ($res === true) {
$this->is_profit = 1;
$this->paid_at = now();
$this->save();
return true;
} else {
return $res;
}
} else {
return '不可打款';
}
}
/**
* Notes: 格式化时间 年-月-日
*
* @Author: 玄尘
* @Date : 2020/4/7 16:24
* @return mixed
*/
public function getCreateDayAttribute()
{
return $this->created_at->format('Y-m-d');
}
/*
* 关联自有券
*/
public function activityCoupon(): HasOne
{
return $this->hasOne(ActivityCoupon::class, 'code', 'redemptionCode');
}
}