<?php
namespace app\common\model\dealer;
use think\Hook;
use app\common\model\BaseModel;
use app\common\enum\OrderType as OrderTypeEnum;
class Order extends BaseModel
{
protected $name = 'dealer_order';
public static function init()
{
parent::init();
$static = new static;
Hook::listen('DealerOrder', $static);
}
public function user()
{
return $this->belongsTo('app\common\model\User');
}
public function dealerFirst()
{
return $this->belongsTo('User', 'first_user_id');
}
public function dealerSecond()
{
return $this->belongsTo('User', 'second_user_id');
}
public function dealerThird()
{
return $this->belongsTo('User', 'third_user_id');
}
public function getOrderTypeAttr($value)
{
$types = OrderTypeEnum::getTypeName();
return ['text' => $types[$value], 'value' => $value];
}
public static function detail($where)
{
return static::get($where);
}
public static function getDetailByOrderId($orderId, $orderType)
{
return static::detail(['order_id' => $orderId, 'order_type' => $orderType]);
}
public static function grantMoney($order, $orderType = OrderTypeEnum::MASTER)
{
if ($order['order_status']['value'] != 30) {
return false;
}
$settleDays = Setting::getItem('settlement', $order['wxapp_id'])['settle_days'];
$deadlineTime = $order['receipt_time'] + ((int)$settleDays * 86400);
if ($settleDays > 0 && $deadlineTime > time()) {
return false;
}
$model = self::getDetailByOrderId($order['order_id'], $orderType);
if (!$model || $model['is_settled'] == 1) {
return false;
}
$capital = $model->getCapitalByOrder($order);
$model['first_user_id'] > 0 && User::grantMoney($model['first_user_id'], $capital['first_money']);
$model['second_user_id'] > 0 && User::grantMoney($model['second_user_id'], $capital['second_money']);
$model['third_user_id'] > 0 && User::grantMoney($model['third_user_id'], $capital['third_money']);
return $model->save([
'order_price' => $capital['orderPrice'],
'first_money' => $capital['first_money'],
'second_money' => $capital['second_money'],
'third_money' => $capital['third_money'],
'is_settled' => 1,
'settle_time' => time()
]);
}
protected function getCapitalByOrder($order)
{
$setting = Setting::getItem('commission', $order['wxapp_id']);
$level = Setting::getItem('basic', $order['wxapp_id'])['level'];
$capital = [
'orderPrice' => bcsub($order['pay_price'], $order['express_price'], 2),
'first_money' => 0.00,
'second_money' => 0.00,
'third_money' => 0.00
];
foreach ($order['goods'] as $goods) {
if ($this->checkGoodsRefund($goods)) {
continue;
}
$goodsPrice = min($capital['orderPrice'], $goods['total_pay_price']);
$goodsCapital = $this->calculateGoodsCapital($setting, $goods, $goodsPrice);
$level >= 1 && $capital['first_money'] += $goodsCapital['first_money'];
$level >= 2 && $capital['second_money'] += $goodsCapital['second_money'];
$level == 3 && $capital['third_money'] += $goodsCapital['third_money'];
}
return $capital;
}
private function calculateGoodsCapital($setting, $goods, $goodsPrice)
{
if ($goods['is_ind_dealer'] == false) {
return [
'first_money' => $goodsPrice * ($setting['first_money'] * 0.01),
'second_money' => $goodsPrice * ($setting['second_money'] * 0.01),
'third_money' => $goodsPrice * ($setting['third_money'] * 0.01)
];
}
if ($goods['dealer_money_type'] == 10) {
return [
'first_money' => $goodsPrice * ($goods['first_money'] * 0.01),
'second_money' => $goodsPrice * ($goods['second_money'] * 0.01),
'third_money' => $goodsPrice * ($goods['third_money'] * 0.01)
];
} else {
return [
'first_money' => $goods['total_num'] * $goods['first_money'],
'second_money' => $goods['total_num'] * $goods['second_money'],
'third_money' => $goods['total_num'] * $goods['third_money']
];
}
}
private function checkGoodsRefund($goods)
{
return !empty($goods['refund'])
&& $goods['refund']['type']['value'] == 10
&& $goods['refund']['is_agree']['value'] != 20;
}
}