142 lines
4.9 KiB
PHP
142 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace app\models;
|
|
|
|
use app\components\SysConst;
|
|
use Yii;
|
|
|
|
/**
|
|
* This is the model class for table "{{%store_earnings}}".
|
|
*
|
|
* @property int $id
|
|
* @property int $store_id 门店id
|
|
* @property int $user_id 收益来源用户id
|
|
* @property int $order_id 订单id
|
|
* @property float $money 收益金额
|
|
* @property int|null $status 状态0未发放1已发放
|
|
* @property int|null $created_at
|
|
* @property int|null $updated_at
|
|
* @property int|null $is_delete
|
|
* @property int|null $deleted_at
|
|
* @property int|null $card_id
|
|
*/
|
|
class StoreEarnings extends \yii\db\ActiveRecord
|
|
{
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public static function tableName()
|
|
{
|
|
return '{{%store_earnings}}';
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['store_id', 'user_id', 'order_id', 'money'], 'required'],
|
|
[['store_id', 'user_id', 'order_id', 'status', 'created_at', 'updated_at', 'is_delete', 'deleted_at','card_id'], 'integer'],
|
|
[['money'], 'number'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function attributeLabels()
|
|
{
|
|
return [
|
|
'id' => 'ID',
|
|
'store_id' => '门店id',
|
|
'user_id' => '收益来源用户id',
|
|
'order_id' => '订单id',
|
|
'money' => '收益金额',
|
|
'status' => '状态0未发放1已发放',
|
|
'created_at' => 'Created At',
|
|
'updated_at' => 'Updated At',
|
|
'is_delete' => 'Is Delete',
|
|
'deleted_at' => 'Deleted At',
|
|
'card_id' => '银行卡',
|
|
];
|
|
}
|
|
|
|
//状态0未分账1已发送分账2已发送完结3分账失败4分账完成5接口异常
|
|
public static function statusLabels()
|
|
{
|
|
return [
|
|
'0' => '待发放',
|
|
'1' => '已发放',
|
|
];
|
|
}
|
|
|
|
//计算门店收入并写入
|
|
public static function get_earnings($order)
|
|
{
|
|
$order_id = $order->id;
|
|
if(empty($order)){
|
|
$msg = "订单不存在!";
|
|
\Yii::info("计算门店收入:{$order_id}->".$msg,'earnings');
|
|
return Model::asReturnError($msg);
|
|
}
|
|
if(empty($order->total_pay_price)){
|
|
\Yii::info("计算门店收入:{$order_id}->".'实际支付:'.$order->total_pay_price,'earnings');
|
|
return Model::asReturnSuccess('ok',['id' => 0,'card_id' => 0,'money' => 0]);
|
|
}
|
|
|
|
$total_pay_price = $order->total_pay_price;
|
|
$store_id = $order->store_id;
|
|
if(empty($store_id)){
|
|
$msg = "所属门店异常!";
|
|
\Yii::info("计算门店收入:{$order_id}->".$msg,'earnings');
|
|
return Model::asReturnError($msg);
|
|
}
|
|
|
|
$store = Store::findOne(['id' => $store_id,'is_delete'=>0]);
|
|
if(empty($store)){
|
|
\Yii::info("计算门店收入:{$order_id}->{$store_id}门店不存在,无法计算门店收益",'earnings');
|
|
$msg = "所选门店不存在!";
|
|
return Model::asReturnError($msg);
|
|
}
|
|
|
|
if(empty($store->ratio)){
|
|
$msg = "{$store_id}-{$store->name}-门店收益比例未设置,无法计算门店收益";
|
|
\Yii::info("计算门店收入:{$order_id}->".$msg,'earnings');
|
|
return Model::asReturnSuccess('ok',['id' => 0,'card_id' => 0,'money' => 0]);
|
|
}
|
|
$card_id = 0;
|
|
$ratio = $store->ratio / 100;
|
|
$money = sprintf('%.2f', $total_pay_price * $ratio);
|
|
|
|
if(empty($money) || $money <= 0){
|
|
$msg = "{$store_id}-{$store->name}-门店收益为{$money}";
|
|
\Yii::info("计算门店收入:{$order_id}->".$msg,'earnings');
|
|
return Model::asReturnSuccess('ok',['id' => 0,'card_id' => 0,'money' => 0]);
|
|
}
|
|
|
|
$earnings_all = StoreEarnings::find()->where(['store_id'=>$store_id,'user_id' => $order->user_id,'order_id' => $order->id,'is_delete' => 0])->exists();
|
|
if(!$earnings_all){
|
|
\Yii::$app->db->createCommand()->update(StoreEarnings::tableName(), ['is_delete' => 1], ['store_id'=>$store_id,'user_id' => $order->user_id,'order_id' => $order->id,'is_delete' => 0])->execute();
|
|
}
|
|
$model = new StoreEarnings();
|
|
$model->store_id = $store_id;
|
|
$model->user_id = $order->user_id;
|
|
$model->order_id = $order->id;
|
|
$model->money = $money;
|
|
$model->status = 1;
|
|
$model->created_at = time();
|
|
$model->updated_at = 0;
|
|
$model->is_delete = 0;
|
|
$model->deleted_at = 0;
|
|
$model->card_id = $card_id;
|
|
if(!$model->save()){
|
|
$model_r = Model::getModelErrorInfo($model);
|
|
\Yii::info("计算门店收入:{$order_id}->".$model_r['msg'],'earnings');
|
|
return $model_r;
|
|
}
|
|
return Model::asReturnSuccess('ok',['id' => $model->id,'card_id' => $card_id,'money' => $money]);
|
|
}
|
|
|
|
}
|