Files
pingan_new/app/Models/Coupon.php

130 lines
2.9 KiB
PHP

<?php
namespace App\Models;
use App\Models\Traits\BelongsToOutlet;
use App\Models\Traits\BelongsToUser;
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()
{
switch ($this->status) {
case 0:
return '未使用';
break;
case 2:
return '核销成功';
break;
case 3:
return '核销失败';
break;
case 4:
return '已分润';
break;
default:
return '未知状态';
break;
}
}
public function getProfitTextAttribute()
{
switch ($this->is_profit) {
case 0:
return '未打款';
break;
case 1:
return '已打款';
break;
default:
return '未知状态';
break;
}
}
/**
* 是否可以分润
* @author 玄尘 2020-03-13
* @return bool [type] [description]
*/
public function canProfit()
{
return ($this->is_profit === 0 && $this->status == 2);
}
/**
* 分润
* @author 玄尘 2020-03-13
* @return bool|string [type] [description]
*/
public function profit()
{
$log = AccountLog::where('source->coupon_id', $this->id)->first();
if (!$log && $this->user) {
return $this->user->account->rule('freeze', $this->profit, false, [
'coupon_id' => $this->id,
'redemptionCode' => $this->redemptionCode,
]);
return true;
} else {
return '已经分润过了';
}
}
/**
* 打款
* @author 玄尘 2020-03-13
* @return bool|string [type] [description]
*/
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');
}
}