369 lines
9.8 KiB
PHP
369 lines
9.8 KiB
PHP
<?php
|
||
/**
|
||
* Created by PhpStorm.
|
||
* Author:
|
||
* Date: 2017/3/27
|
||
* Time: 下午1:52
|
||
*/
|
||
|
||
namespace app\common\models;
|
||
|
||
|
||
use app\common\observers\income\IncomeObserver;
|
||
|
||
/**
|
||
* @property int status
|
||
* @property float amount
|
||
* @property string incometable_type
|
||
*
|
||
* Class Income
|
||
* @package app\common\models
|
||
*/
|
||
class Income extends BaseModel
|
||
{
|
||
protected $table = 'yz_member_income';
|
||
|
||
|
||
protected $guarded = [''];
|
||
|
||
protected $appends = ['status_name', 'pay_status_name'];
|
||
/**
|
||
* @var Withdraw withdraw
|
||
*/
|
||
private $withdraw;
|
||
/**
|
||
* 提现状态:未提现
|
||
*/
|
||
const STATUS_INITIAL = 0;
|
||
|
||
|
||
/**
|
||
* 提现状态:已提现
|
||
*/
|
||
const STATUS_WITHDRAW = 1;
|
||
|
||
|
||
/**
|
||
* 打款状态:无效
|
||
*/
|
||
const PAY_STATUS_INVALID = -1;
|
||
|
||
|
||
/**
|
||
* 打款状态:未审核
|
||
*/
|
||
const PAY_STATUS_INITIAL = 0;
|
||
|
||
|
||
/**
|
||
* 打款状态:未打款
|
||
*/
|
||
const PAY_STATUS_WAIT = 1;
|
||
|
||
|
||
/**
|
||
* 打款状态:已打款
|
||
*/
|
||
const PAY_STATUS_FINISH = 2;
|
||
|
||
|
||
/**
|
||
* 打款状态:已驳回
|
||
*/
|
||
const PAY_STATUS_REJECT = 3;
|
||
|
||
/**
|
||
* 分帐插件使用,['order_id' => '', 'mark' => '']
|
||
*
|
||
* @var array
|
||
*/
|
||
public $separate = [];
|
||
|
||
|
||
/**
|
||
* 提现状态名称集合
|
||
*
|
||
* @var array
|
||
*/
|
||
public static $statusComment = [
|
||
self::STATUS_INITIAL => '未提现',
|
||
self::STATUS_WITHDRAW => '已提现',
|
||
];
|
||
|
||
|
||
/**
|
||
* 打款状态名称集合
|
||
*
|
||
* @var array
|
||
*/
|
||
public static $payStatusComment = [
|
||
self::PAY_STATUS_INVALID => '无效',
|
||
self::PAY_STATUS_INITIAL => '未审核',
|
||
self::PAY_STATUS_WAIT => '未打款',
|
||
self::PAY_STATUS_FINISH => '已打款',
|
||
self::PAY_STATUS_REJECT => '已驳回',
|
||
];
|
||
|
||
public static function boot()
|
||
{
|
||
parent::boot();
|
||
self::observe(IncomeObserver::class);
|
||
}
|
||
|
||
/**
|
||
* 通过 $status 值获取 $status 名称
|
||
*
|
||
* @param $status
|
||
* @return mixed|string
|
||
*/
|
||
public static function getStatusComment($status)
|
||
{
|
||
return isset(static::$statusComment[$status]) ? static::$statusComment[$status] : '';
|
||
}
|
||
|
||
|
||
/**
|
||
* 通过 $pay_way 值获取 $pay_status 名称
|
||
*
|
||
* @param $pay_status
|
||
* @return mixed|string
|
||
*/
|
||
public static function getPayWayComment($pay_status)
|
||
{
|
||
return isset(static::$payStatusComment[$pay_status]) ? static::$payStatusComment[$pay_status] : '';
|
||
}
|
||
|
||
|
||
/**
|
||
* 通过字段 status 输出 status_name ;
|
||
*
|
||
* @return string
|
||
*/
|
||
public function getStatusNameAttribute()
|
||
{
|
||
return static::getStatusComment($this->attributes['status']);
|
||
}
|
||
|
||
|
||
/**
|
||
* 通过字段 pay_status 输出 pay_status_name ;
|
||
*
|
||
* @return string
|
||
*/
|
||
public function getPayStatusNameAttribute()
|
||
{
|
||
return static::getPayWayComment($this->attributes['pay_status']);
|
||
}
|
||
|
||
|
||
/**
|
||
* 可提现收入检索条件
|
||
*
|
||
* @param $query
|
||
* @return mixed
|
||
*/
|
||
public function scopeCanWithdraw($query)
|
||
{
|
||
return $query->where('status', static::STATUS_INITIAL);
|
||
}
|
||
|
||
|
||
/**
|
||
* 关联会员数据表
|
||
*
|
||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||
*/
|
||
public function member()
|
||
{
|
||
return $this->belongsTo('app\common\models\Member', 'member_id', 'uid');
|
||
}
|
||
|
||
//todo 以下代码未检查 yitian :: 2017-11-14
|
||
|
||
/**
|
||
* @param $id
|
||
* @return mixed
|
||
*/
|
||
public static function getIncomeFindId($id)
|
||
{
|
||
return self::find($id);
|
||
}
|
||
|
||
/**
|
||
* @param $id
|
||
* @return mixed
|
||
*/
|
||
public static function getIncomeById($id)
|
||
{
|
||
return self::uniacid()
|
||
->where('id', $id);
|
||
|
||
}
|
||
|
||
/**
|
||
* @param $ids
|
||
* @return mixed
|
||
*/
|
||
public static function getIncomeByIds($ids)
|
||
{
|
||
return self::uniacid()
|
||
->whereIn('id', explode(',', $ids));
|
||
}
|
||
|
||
|
||
public function incometable()
|
||
{
|
||
return $this->morphTo();
|
||
}
|
||
|
||
/**
|
||
* @return mixed
|
||
*/
|
||
public static function getIncomes()
|
||
{
|
||
return self::uniacid();
|
||
}
|
||
|
||
public static function getIncomeInMonth($search)
|
||
{
|
||
$model = self::select('create_month');
|
||
$model->uniacid();
|
||
$model->with(['hasManyIncome' => function ($query) use ($search) {
|
||
$query->select('id', 'create_month', 'incometable_type', 'type_name', 'amount', 'created_at');
|
||
if ($search['type']) {
|
||
$query->where('incometable_type', $search['type']);
|
||
}
|
||
$query->where('member_id', \YunShop::app()->getMemberId());
|
||
$query->orderBy('id', 'desc');
|
||
return $query->get();
|
||
}]);
|
||
$model->groupBy('create_month');
|
||
$model->orderBy('create_month', 'desc');
|
||
return $model;
|
||
}
|
||
|
||
public static function getDetailById($id)
|
||
{
|
||
$model = self::uniacid();
|
||
$model->select('detail');
|
||
$model->where('id', $id);
|
||
return $model;
|
||
}
|
||
|
||
public static function getWithdraw($type, $typeId, $status)
|
||
{
|
||
return self::where('type', 'commission')
|
||
->where('member_id', \YunShop::app()->getMemberId())
|
||
->whereIn('id', explode(',', $typeId))
|
||
->update(['status' => $status]);
|
||
}
|
||
|
||
public static function updatedWithdraw($type, $typeId, $status)
|
||
{
|
||
return self::where('member_id', \YunShop::app()->getMemberId())
|
||
->whereIn('id', explode(',', $typeId))
|
||
->update(['status' => $status]);
|
||
}
|
||
|
||
public static function updatedIncomeStatus($type, $typeId, $status)
|
||
{
|
||
return self::where('member_id', \YunShop::app()->getMemberId())
|
||
->whereIn('id', explode(',', $typeId))
|
||
->update(['status' => $status]);
|
||
}
|
||
|
||
public function hasManyIncome()
|
||
{
|
||
return $this->hasMany(self::class, "create_month", "create_month");
|
||
}
|
||
|
||
public function hasManyOrder()
|
||
{
|
||
return $this->hasOne(Order::class, "order_sn", "order_sn");
|
||
}
|
||
|
||
public static function updatedIncomePayStatus($id, $updatedData)
|
||
{
|
||
return self::where('id', $id)
|
||
->update($updatedData);
|
||
}
|
||
|
||
public static function getIncomesList($search)
|
||
{
|
||
if($search['select']){
|
||
$selects = ['id', 'create_month', 'incometable_type', 'type_name', 'amount', 'created_at','detail','incometable_id'];
|
||
}else{
|
||
$selects = ['id', 'create_month', 'incometable_type', 'type_name', 'amount', 'created_at'];
|
||
}
|
||
|
||
$model = self::uniacid();
|
||
$model->select($selects);
|
||
if ($search['type']) {
|
||
$model->where('incometable_type', $search['type']);
|
||
}
|
||
$model->where('member_id', \YunShop::app()->getMemberId());
|
||
$model->orderBy('id', 'desc');
|
||
|
||
return $model;
|
||
}
|
||
|
||
/**
|
||
* @return Withdraw
|
||
*/
|
||
public function withdraw()
|
||
{
|
||
if (!isset($this->withdraw)) {
|
||
$this->withdraw = Withdraw::where('type_id', 'like', "%{$this->id}%")->where('type', $this->incometable_type)->first();
|
||
}
|
||
return $this->withdraw;
|
||
}
|
||
|
||
// 获取累计收入(包括已提现收入)
|
||
// $contentType:cultural_space=文创空间、broker=非文创空间
|
||
public static function getAllIncome($uid,$contentType = 'all'){
|
||
// 这里查询不包括 经销商、分销商、股东分红相关佣金
|
||
$otherTotal = (float)Income::uniacid()
|
||
->whereNotIn('dividend_code',[1,2,64,201])
|
||
->where('member_id', $uid)
|
||
->sum('amount');
|
||
// 获取经销商提成 分红类型:0=分红佣金,1=平级奖,2=感恩奖励,3=额外分红,4=生态建设,5=生态贡献(生态建设平级奖),6=生态服务
|
||
$teamDividendTotal = (float)Income::uniacid()
|
||
->leftjoin('yz_team_dividend','yz_team_dividend.id','=','yz_member_income.incometable_id')
|
||
->where('yz_member_income.dividend_code', 2)
|
||
->where('yz_member_income.member_id', $uid)
|
||
->when($contentType != 'all',function($query) use ($contentType){
|
||
if($contentType == 'cultural_space') $query->whereIn('yz_team_dividend.type',[4,5,6]);
|
||
else $query->whereNotIn('yz_team_dividend.type',[4,5,6]);
|
||
})
|
||
->sum('yz_member_income.amount');
|
||
// 获取分销商提成 0=普通分销商订单,1=文创分销商订单
|
||
$commissionTotal = (float)Income::uniacid()
|
||
->leftjoin('yz_commission_order','yz_commission_order.id','=','yz_member_income.incometable_id')
|
||
->where('yz_member_income.dividend_code', 1)
|
||
->where('yz_member_income.member_id', $uid)
|
||
->when($contentType != 'all',function($query) use ($contentType){
|
||
if($contentType == 'cultural_space') $query->where('yz_commission_order.level_type',1);
|
||
else $query->where('yz_commission_order.level_type',0);
|
||
})
|
||
->sum('yz_member_income.amount');
|
||
// 获取股东分红提成 收益类型:0=股权分红,1=加权收益 权证兑换
|
||
$shareholderDividendTotal = (float)Income::uniacid()
|
||
->leftjoin('yz_shareholder_dividend','yz_shareholder_dividend.id','=','yz_member_income.incometable_id')
|
||
->where('yz_member_income.dividend_code', 64)
|
||
->where('yz_member_income.member_id', $uid)
|
||
->when($contentType != 'all',function($query) use ($contentType){
|
||
if($contentType == 'cultural_space') $query->where('yz_shareholder_dividend.income_type',1);
|
||
else $query->where('yz_shareholder_dividend.income_type',0);
|
||
})
|
||
->sum('yz_member_income.amount');
|
||
// 总计
|
||
return array_sum([$otherTotal,$teamDividendTotal,$commissionTotal,$shareholderDividendTotal]);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} |