bztang-admin/plugins/area-dividend/src/models/AreaDividendAgent.php

261 lines
7.5 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: yanglei
* Date: 2017/4/27
* Time: 上午9:16
*/
namespace Yunshop\AreaDividend\models;
use app\common\facades\Setting;
use app\common\models\BaseModel;
use app\common\services\finance\PointService;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use Yunshop\AreaDividend\services\AgentService;
use Yunshop\Commission\models\AgentLevel;
/**
* Class AreaDividendAgent
* @package Yunshop\AreaDividend\models
* @property float count_order_amount
* @property float count_settle_amount
* @property float unsettled_dividend_amount
*/
class AreaDividendAgent extends BaseModel
{
use SoftDeletes;
public $table = 'yz_area_dividend_agent';
public $timestamps = true;
protected $guarded = [''];
public $StatusService;
public $LevelService;
public $Rate;
protected $appends = ['status_name', 'level_name', 'rate'];
protected $search_fields = ['province_name', 'city_name', 'district_name', 'street_name'];
public static function getAgents($search)
{
$agentModel = self::uniacid();
if (!empty($search['member'])) {
$agentModel->whereHas('hasOneMember', function ($query) use ($search) {
return $query->searchLike($search['member']);
});
}
$agentModel->with(['hasOneMember']);
$agentModel->where('status', '1');
if (!empty($search['area_name'])) {
$agentModel->searchLike($search['area_name']);
}
if (!empty($search['agent_level'])) {
$agentModel->where('agent_level', $search['agent_level']);
}
if ($search['is_time']) {
if ($search['time']) {
$range = [strtotime($search['time']['start']), strtotime($search['time']['end'])];
$agentModel->whereBetween('created_at', $range);
}
}
$agentModel->with(['hasOneDividend' => function ($query) {
$query->selectRaw('member_id, sum(amount) as total_amount')
->selectRaw('sum(if(status = 0 ,dividend_amount, 0)) as unsettled')
->selectRaw('sum(if(status = 1 ,dividend_amount, 0)) as settle')
->selectRaw('sum(order_amount) as total_order_amount')->groupBy('member_id');
}]);
return $agentModel;
}
public static function getApplyAgents()
{
return self::uniacid()
->with(['hasOneMember'])
->where('status', '0');
}
public static function getApplyAgentsBySearch($search)
{
$model = self::uniacid()
->with(['hasOneMember'])
->where('status', '0');
if (!empty($search['member'])) {
$model->whereHas('hasOneMember', function ($q) use ($search) {
$q->searchLike($search['member']);
});
}
return $model;
}
public function hasOneDividend()
{
return $this->hasOne(AreaDividend::class,'member_id', 'member_id');
}
public function getStatusNameAttribute()
{
if (!isset($this->StatusService)) {
$this->StatusService = AgentService::createStatusService($this);
}
return $this->StatusService;
}
public function getLevelNameAttribute()
{
if (!isset($this->LevelService)) {
$this->LevelService = AgentService::createLevelService($this);
}
return $this->LevelService;
}
/**
* Common: 获取器 —— 分润比例
* Author: wu-hui
* Time: 2022/09/27 17:38
* @return mixed|void
*/
public function getRateAttribute(){
if (!isset($this->Rate)) return AgentLevel::getUserRate($this->member_id,$this->agent_level,FALSE);
if ($this->has_ratio == 1) $this->Rate = $this->ratio;
return $this->Rate;
}
public function hasOneMember()
{
return $this->hasOne('app\common\models\Member', 'uid', 'member_id');
}
public static function getAreaAgentByAddressId($id, $level)
{
return self::uniacid()
->where('status', '1')
->where('agent_level', $level)
->where(function ($query) use ($id) {
return $query->where('province_id', $id)
->orWhere('city_id', $id)
->orWhere('district_id', $id)
->orWhere('street_id', $id);
});
}
public static function updatedAgentById($data, $id)
{
return self::uniacid()
->where('id', $id)
->update($data);
}
public static function getAgentByUserId($uid)
{
return self::uniacid()->where('user_id', $uid);
}
public static function getAgentByMemberId($membeId)
{
return self::uniacid()
->where('member_id',$membeId);
}
public static function daletedAgency($id)
{
return self::where('id', $id)
->delete();
}
public static function getLevelList()
{
return $level = [
'0' => [
'level' => '0',
'level_name' => '无效',
],
'1' => [
'level' => '1',
'level_name' => '省代理',
],
'2' => [
'level' => '2',
'level_name' => '市代理',
],
'3' => [
'level' => '3',
'level_name' => '区代理',
],
'4' => [
'level' => '4',
'level_name' => '街道代理',
],
];
}
public static function awardPoint($dividendRate, $level, $member_id, $order, $set)
{
$ratio = $dividendRate['rate_' . $level]['point_ratio'];
if ($ratio < 0) {
$ratio = 0;
}
// dump($dividendRate);
$calculatePrice = $order->price;
// dump('order_price:'.$calculatePrice);
if ($set['calculate_type'] == 1) {
// dump('利润');
$costPrice = $order->dispatch_price;
foreach ($order->hasManyOrderGoods as $orderGoods) {
$costPrice += $orderGoods->goods_cost_price;
}
// dump('costPrice:'.$costPrice);
$calculatePrice = $order->price - $costPrice;
}
// dump($calculatePrice);
// dump('ratio:'.$ratio);
if ($set['award_point_type'] == 1) {
// dump('固定');
$point = $ratio;
if ($set['calculate_type'] == 1) {
$point = 0;
foreach ($order->hasManyOrderGoods as $orderGoods) {
$point += $orderGoods->total * $ratio;
}
}
} else {
// dump('比例');
$point = $calculatePrice * $ratio / 100;
}
// dump($point);
$point_data = [
'point_income_type' => PointService::POINT_INCOME_GET,
'point_mode' => PointService::POINT_MODE_TEAM,
'member_id' => $member_id,
'point' => $point,
'remark' => '区域分红奖励'
];
// dump($point_data);
$point_service = new PointService($point_data);
$point_service->changePoint();
}
public static function getSpecifiedAgent($agnetAddressId, $column, $level)
{
return AreaDividendAgent::uniacid()
->where($column, $agnetAddressId)
->where('agent_level', $level)
->where('status', 1)
->where('manage', 1)
->first();
}
}