140 行
5.5 KiB
PHP
可执行文件
140 行
5.5 KiB
PHP
可执行文件
<?php
|
|
|
|
/**
|
|
* 小程序--自动结算
|
|
* @author nianyi
|
|
* @create_time 2020年08月12日
|
|
*/
|
|
|
|
namespace Distribution\Api;
|
|
|
|
|
|
use Common\Model\RetailMessageModel;
|
|
|
|
use Distribution\Model\ConfigModel;
|
|
use Distribution\Model\OrderModel;
|
|
use Distribution\Model\DistributorModel;
|
|
use Distribution\Model\SettledModel;
|
|
|
|
class AutoSettlement
|
|
{
|
|
|
|
/* ******************************************* 订单佣金处理Start ******************************************** */
|
|
// 获取开启分销插件的商户
|
|
public function getPlugMerchant()
|
|
{
|
|
$configModel = new ConfigModel();
|
|
$merchant_arr = $configModel->getPlugMerchant();
|
|
if ($merchant_arr) {
|
|
foreach ($merchant_arr as $value) {
|
|
$this->commissionSettlement($value['merchant_id']);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 结算!
|
|
public function commissionSettlement($merchant_id)
|
|
{
|
|
$ConfigModel = new ConfigModel();
|
|
$OrderModel = new OrderModel();
|
|
$DistributorModel = new DistributorModel();
|
|
$MessageModel = new RetailMessageModel();
|
|
|
|
// 获取系统配置结算周期
|
|
$config = $ConfigModel->getFind(['merchant_id' => $merchant_id], 'balance_date');
|
|
$settlement_date = bcsub(time(), $config['balance_date'] * 86400);
|
|
|
|
$where = [
|
|
'rbo.merchant_id' => $merchant_id,
|
|
'rbo.order_type' => 1,
|
|
'rbo.pay_state' => 1,
|
|
'diso.withdraw_state' => 0,
|
|
[
|
|
['rbo.type' => 1, 'rbon.receiving_goods_time' => ['LT', $settlement_date]],
|
|
['rbo.type' => 2, 'rbo.finish_time' => ['LT', $settlement_date]],
|
|
'_logic' => 'OR',
|
|
],
|
|
];
|
|
$field = [
|
|
'rbo.uid',
|
|
'rbo.all_money',
|
|
'diso.id',
|
|
'diso.bill_id',
|
|
'diso.did',
|
|
'diso.pid',
|
|
'diso.sell_first_money',
|
|
'diso.sell_second_money',
|
|
'diso.first_refund_money',
|
|
'diso.second_refund_money',
|
|
'rbon.delivery_fee',
|
|
'disbutor.uid as dis_uid',
|
|
];
|
|
$result = $OrderModel->commissionSettlementOrder($where, $field);
|
|
if ($result) {
|
|
$distributor_arr = [];
|
|
foreach ($result as &$value) {
|
|
// 可提现金额
|
|
$DistributorModel->toSetInc(['id' => $value['did']], 'settable_amount', bcsub($value['sell_first_money'], $value['first_refund_money'], 2));
|
|
$DistributorModel->toSetInc(['id' => $value['pid']], 'settable_amount', bcsub($value['sell_second_money'], $value['second_refund_money'], 2));
|
|
|
|
// 分销订单数 累计佣金 自购商品金额
|
|
$DistributorModel->toSetInc(['id' => $value['did']], 'total_commission', $value['sell_first_money'] - $value['first_refund_money']);
|
|
$DistributorModel->toSetInc(['id' => $value['did']], 'total_indent', 1);
|
|
if ($value['dis_uid'] == $value['uid']) { // 自购
|
|
$DistributorModel->toSetInc(['id' => $value['did']], 'total_myself_money', $value['all_money'] - $value['delivery_fee']);
|
|
} else {
|
|
$DistributorModel->toSetInc(['id' => $value['pid']], 'total_commission', $value['sell_second_money'] - $value['second_refund_money']);
|
|
$DistributorModel->toSetInc(['id' => $value['pid']], 'total_indent', 1);
|
|
}
|
|
|
|
// 客户数 -- 用户有无在此分销员名下下单
|
|
$orderMap = [
|
|
'diso.withdraw_state' => 1,
|
|
'diso.did' => $value['did'],
|
|
'rbo.uid' => $value['uid']
|
|
];
|
|
$orderNum = $OrderModel->distributorAndCustomer($orderMap);
|
|
if ($orderNum == 0) {
|
|
$DistributorModel->toSetInc(['id' => $value['did']], 'total_client', 1);
|
|
}
|
|
|
|
// 更新结算时间与结算状态
|
|
$OrderModel->updateOrder(['bill_id' => $value['bill_id']], ['withdraw_state' => 1, 'settlement_date' => time()]);
|
|
|
|
echo '用户' . $value['did'] . '结算结束<br>';
|
|
echo '用户' . $value['pid'] . '结算结束<br>';
|
|
|
|
if (empty($distributor_arr[$value['did']])) $distributor_arr[$value['did']] = 0;
|
|
if (empty($distributor_arr[$value['pid']])) $distributor_arr[$value['pid']] = 0;
|
|
$distributor_arr[$value['did']] += bcsub($value['sell_first_money'], $value['first_refund_money'], 2);
|
|
$distributor_arr[$value['pid']] += bcsub($value['sell_second_money'], $value['second_refund_money'], 2);
|
|
}
|
|
$SettledModel = new SettledModel();
|
|
foreach ($distributor_arr as $key => $val) {
|
|
// 获取分销员的uid
|
|
$uid = $DistributorModel->getFind(['id' => $key], 'uid');
|
|
$item = array(
|
|
'did' => $key,
|
|
'settled_price' => $val,
|
|
'add_time' => time(),
|
|
);
|
|
$SettledModel->addOrder($item);//添加结算记录
|
|
// 消息推送
|
|
$MessageData = [
|
|
'type' => 6,
|
|
'uid' => $uid['uid'],
|
|
'title' => '分销佣金已结算',
|
|
'content' => '您本次结算' . $val . '元',
|
|
'add_time' => time()
|
|
];
|
|
|
|
$MessageModel->addOrder($MessageData);
|
|
}
|
|
}
|
|
|
|
$res['status'] = 1;
|
|
$res['msg'] = '结算处理成功!';
|
|
return $res;
|
|
}
|
|
}
|