309 行
13 KiB
PHP
可执行文件
309 行
13 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace Distribution\Model;
|
|
|
|
/**
|
|
* 分销商品销售表
|
|
* Class OrderModel
|
|
* @package Distribution\Model
|
|
*/
|
|
class OrderModel extends \Common\Model\BaseModel
|
|
{
|
|
protected $tableName = 'distribution_order';
|
|
protected $fields = array(
|
|
'id', //int(11) NOT NULL AUTO_INCREMENT,
|
|
'bill_id', //int(11) NOT NULL DEFAULT '0' COMMENT '订单id',
|
|
'did', //int(11) NOT NULL DEFAULT '0' COMMENT '一级分销员ID',
|
|
'pid', //int(11) NOT NULL DEFAULT '0' COMMENT '二级分销员ID',
|
|
'sell_first_money', //decimal(10,2) DEFAULT '0.00' COMMENT '商品一级佣金',
|
|
'sell_second_money', //decimal(10,2) DEFAULT '0.00' COMMENT '商品二级佣金',
|
|
'withdraw_state', //tinyint(1) DEFAULT '0' COMMENT '结算情况 0未结算 1已结算 2已失效',
|
|
'first_refund_money', //decimal(10,2) DEFAULT '0.00' COMMENT '一级佣金被退金额',
|
|
'second_refund_money', //decimal(10,2) DEFAULT '0.00' COMMENT '二级佣金被退金额',
|
|
'settlement_date', //int(11) DEFAULT 0 NULL COMMENT '结算时间',
|
|
);
|
|
|
|
// 获取订单
|
|
public function getDistributionOrder($where, $field, $page = 1, $limit = 10, $order)
|
|
{
|
|
$list = $this->alias('diso')
|
|
->join('ls_distribution_sell as dis_sell on dis_sell.settled_id = diso.id')
|
|
->join('ls_retail_bill_order as rbo on rbo.id = diso.bill_id')
|
|
->join('ls_retail_sell as rs on rs.id = dis_sell.sell_id')
|
|
->join('ls_retail_xcxvip as rx on rx.id = rbo.uid')
|
|
->field($field)
|
|
->where($where)
|
|
->order($order)
|
|
->page($page, $limit)
|
|
->select();
|
|
|
|
$total = $this->alias('diso')
|
|
->join('ls_retail_bill_order as rbo on rbo.id = diso.bill_id')
|
|
->join('ls_retail_xcxvip as rx on rx.id = rbo.uid')
|
|
->join('ls_distribution_sell as dis_sell on dis_sell.settled_id = diso.id')
|
|
->join('ls_retail_sell as rs on rs.id = dis_sell.sell_id')
|
|
->where($where)
|
|
->count();
|
|
|
|
$result['list'] = $list;
|
|
$result['count'] = $total;
|
|
return $result;
|
|
}
|
|
|
|
// 获取订单-新
|
|
public function getDistriOrder($where, $field, $page = 1, $limit = 10, $order)
|
|
{
|
|
$list = $this->alias('diso')
|
|
->join('ls_retail_bill_order as rbo on rbo.id = diso.bill_id', 'LEFT')
|
|
->join('ls_retail_xcxvip as rx on rx.id = rbo.uid', 'LEFT')
|
|
->field($field)
|
|
->where($where)
|
|
->order($order)
|
|
->page($page, $limit)
|
|
->select();
|
|
|
|
$total = $this->alias('diso')
|
|
->join('ls_retail_bill_order as rbo on rbo.id = diso.bill_id', 'LEFT')
|
|
->join('ls_retail_xcxvip as rx on rx.id = rbo.uid', 'LEFT')
|
|
->where($where)
|
|
->count();
|
|
|
|
$result['list'] = $list;
|
|
$result['count'] = $total;
|
|
return $result;
|
|
}
|
|
|
|
// 预估佣金计算--前台合并接口用
|
|
public function userCommissionExpected($distributor_id, $merchant_id)
|
|
{
|
|
$where['rbo.merchant_id'] = $merchant_id;
|
|
$where['diso.withdraw_state'] = ['NEQ', 2];
|
|
|
|
$commission['today'] = $this->userCommissionExpectedByTime($where, $distributor_id, 3, 1);
|
|
$commission['yesterday'] = $this->userCommissionExpectedByTime($where, $distributor_id, 3, 2);
|
|
$commission['month'] = $this->userCommissionExpectedByTime($where, $distributor_id, 3, 3);
|
|
$commission['last_month'] = $this->userCommissionExpectedByTime($where, $distributor_id, 3, 4);
|
|
return $commission;
|
|
}
|
|
|
|
|
|
/* *
|
|
* 获取用户佣金
|
|
* type 1=一级佣金,2=二级佣金,3=所有佣金
|
|
* times 1=今日,2=昨日,3=本月,4=上月,5/other=所有
|
|
* */
|
|
public function userCommissionExpectedByTime($where, $distributor_id, $type = 3, $times = 1)
|
|
{
|
|
if ($times == 1) {
|
|
$beginEnd = $this->getTimestampByDemand($times);
|
|
$where['rbo.add_time'] = ['between', [$beginEnd['begin'], $beginEnd['last']]];
|
|
} else if ($times == 2) {
|
|
$beginEnd = $this->getTimestampByDemand($times);
|
|
$where['rbo.add_time'] = ['between', [$beginEnd['begin'], $beginEnd['last']]];
|
|
} else if ($times == 3) {
|
|
$beginEnd = $this->getTimestampByDemand($times);
|
|
$where['rbo.add_time'] = ['between', [$beginEnd['begin'], $beginEnd['last']]];
|
|
} else if ($times == 4) {
|
|
$beginEnd = $this->getTimestampByDemand($times);
|
|
$where['rbo.add_time'] = ['between', [$beginEnd['begin'], $beginEnd['last']]];
|
|
} else {
|
|
|
|
}
|
|
|
|
if ($type == 1) {
|
|
$where['diso.did'] = $distributor_id;
|
|
$field = 'sum(diso.sell_first_money) as commission, sum(diso.first_refund_money) as refund_commission';
|
|
} else if ($type == 2) {
|
|
$where['diso.pid'] = $distributor_id;
|
|
$field = 'sum(diso.sell_second_money) as commission, sum(diso.second_refund_money) as refund_commission';
|
|
} else {
|
|
$field = '';
|
|
}
|
|
|
|
$where['rbo.pay_state'] = 1;
|
|
$where['rbo.order_type'] = 1;
|
|
|
|
$Commission = $this->alias('diso')
|
|
->field($field)
|
|
->join('ls_retail_bill_order as rbo on rbo.id = diso.bill_id', 'LEFT')
|
|
->where($where)
|
|
->select();
|
|
|
|
if ($type == 1) {
|
|
return (float)$Commission[0]['commission'] - (float)$Commission[0]['refund_commission'];
|
|
} else if ($type == 2) {
|
|
return (float)$Commission[0]['commission'] - (float)$Commission[0]['refund_commission'];
|
|
} else if ($type == 3) {
|
|
$first = $this->userCommissionExpectedByTime($where, $distributor_id, 1, $times);
|
|
$second = $this->userCommissionExpectedByTime($where, $distributor_id, 2, $times);
|
|
return round($first + $second, 2);
|
|
} else {
|
|
return 0;
|
|
}
|
|
|
|
}
|
|
|
|
// 获取未结算订单
|
|
public function commissionSettlementOrder($where, $field)
|
|
{
|
|
$list = $this->alias('diso')
|
|
->join('ls_retail_bill_order as rbo on rbo.id = diso.bill_id', 'LEFT')
|
|
->join('ls_retail_bill_online as rbon on rbon.bill_id = diso.bill_id', 'LEFT')
|
|
->join('ls_distributor as disbutor on disbutor.id = diso.did', 'LEFT')
|
|
->field($field)
|
|
->where($where)
|
|
->select();
|
|
return $list;
|
|
}
|
|
|
|
// 分销员的客户数
|
|
public function distributorAndCustomer($where)
|
|
{
|
|
$num = $this->alias('diso')
|
|
->join('ls_retail_bill_order as rbo on rbo.id = diso.bill_id', 'LEFT')
|
|
->where($where)
|
|
->count();
|
|
return $num ? $num : 0;
|
|
}
|
|
|
|
/**
|
|
* 退款佣金处理
|
|
* @param $bill_info
|
|
* old_bill_id 原购买订单的ID
|
|
* new_bill_id 退款产生的新的订单ID
|
|
* new_sell_id 退款产生的新的订单的商品明细ID
|
|
* @return array
|
|
*/
|
|
public function refundToCommission($bill_info)
|
|
{
|
|
$bill_id = $bill_info['old_bill_id'];//原订单ID
|
|
$new_bill_id = $bill_info['new_bill_id'];//新退款订单ID
|
|
$new_sell_id = $bill_info['new_sell_id'];//新退款商品记录ID(数组)
|
|
|
|
if (!$bill_id || !$new_bill_id || !$new_sell_id)['status' => 0, 'msg' => '必要参数缺失'];
|
|
|
|
// 佣金处理
|
|
$orderField = [
|
|
'sell_first_money',
|
|
'sell_second_money',
|
|
'first_refund_money',
|
|
'second_refund_money'
|
|
];
|
|
//原佣金记录
|
|
$OrderModel = new OrderModel();
|
|
$orderInfo = $OrderModel->getFind(['bill_id' => $bill_id, 'withdraw_state' => 0], $orderField);
|
|
|
|
if (!$orderInfo) return ['status' => 0, 'msg' => '该订单不是分销订单'];
|
|
|
|
//获取分销商品退款列表
|
|
$retailSellList = M('retail_sell as s1')
|
|
->join('ls_retail_sell as s2 on s1.id=s2.sell_id')
|
|
->join('ls_distribution_sell as s3 on s3.sell_id=s1.id')
|
|
->where(['s2.id' => ['in', $new_sell_id]])
|
|
->field('s3.*,s2.id as new_sell_id,s2.sell_id,s2.num,s1.refund_num,s1.num as all_num,s2.all_money')
|
|
->select();
|
|
|
|
if (!$retailSellList) ['status' => 0, 'msg' => '退款商品中不存在分销商品'];
|
|
|
|
//计算佣金
|
|
$all_first_refund_money = 0;//本次总的退款一级佣金
|
|
$all_second_refund_money = 0;//本次总的退款二级佣金
|
|
$newSellList = [];//存储新插入的分销商品退款佣金记录
|
|
foreach ($retailSellList as $val) {
|
|
$data = [
|
|
'sell_id' => $val['new_sell_id'],
|
|
'old_sell_id' => $val['sell_id'],
|
|
'bill_id' => $new_bill_id,
|
|
'dis_goods_id' => $val['dis_goods_id'],
|
|
'first_balance_rate' => $val['first_balance_rate'],
|
|
'second_balance_rate' => $val['second_balance_rate'],
|
|
'total_amount' => $val['all_money'],
|
|
];
|
|
|
|
if ($val['num'] * 1 == $val['all_num'] * 1) {
|
|
//一次性全部退商品
|
|
$data['first_refund_money'] = $val['sell_first_money'];
|
|
$data['second_refund_money'] = $val['sell_second_money'];
|
|
} else if (bcadd($val['num'], $val['refund_num'], 2) == $val['all_num']) {
|
|
//之前退过,本次把剩余的全部退掉
|
|
$data['first_refund_money'] = bcsub($val['sell_first_money'], $val['first_refund_money'], 2);
|
|
$data['second_refund_money'] = bcsub($val['sell_second_money'], $val['sell_second_money'], 2);
|
|
} else {
|
|
//仅退一部分
|
|
$data['first_refund_money'] = bcmul($val['sell_first_money'] / $val['all_num'], $val['num'], 2);
|
|
$data['second_refund_money'] = bcmul($val['sell_second_money'] / $val['all_num'], $val['num'], 2);
|
|
}
|
|
//总退款佣金累加
|
|
$all_first_refund_money += $data['first_refund_money'];
|
|
$all_second_refund_money += $data['second_refund_money'];
|
|
$newSellList[] = $data;
|
|
}
|
|
|
|
//生成新的退款佣金订单记录
|
|
$orderData = [
|
|
'bill_id' => $new_bill_id,
|
|
'did' => $orderInfo['did'],
|
|
'pid' => $orderInfo['pid'],
|
|
'first_refund_money' => round($all_first_refund_money, 2),
|
|
'second_refund_money' => round($all_second_refund_money, 2),
|
|
];
|
|
$order_id = $OrderModel->addOrder($orderData);
|
|
|
|
//插入退款商品记录
|
|
$SellModel = new SellModel();
|
|
foreach ($newSellList as &$value) {
|
|
//更新旧数据的退款佣金
|
|
$SellModel->toSetInc(['sell_id' => $value['old_sell_id']], 'first_refund_money', $value['first_refund_money']);
|
|
$SellModel->toSetInc(['sell_id' => $value['old_sell_id']], 'second_refund_money', $value['second_refund_money']);
|
|
//插入新的商品记录
|
|
$value['settled_id'] = $order_id;
|
|
unset($value['old_sell_id']);
|
|
$SellModel->addOrder($value);
|
|
}
|
|
|
|
// 更新原订单退款金额、剩余佣金
|
|
$oUpData = [
|
|
'first_refund_money' => bcadd($orderInfo['first_refund_money'], $all_first_refund_money, 2),
|
|
'second_refund_money' => bcadd($orderInfo['second_refund_money'], $all_second_refund_money, 2),
|
|
];
|
|
//佣金全部退还,订单状态改为已失效
|
|
if (bcsub($oUpData['first_refund_money'], $orderInfo['sell_first_money'], 2) == 0
|
|
&& bcsub($oUpData['second_refund_money'], $orderInfo['sell_second_money'], 2) == 0) {
|
|
$oUpData['withdraw_state'] = 2;
|
|
}
|
|
$result = $OrderModel->updateOrder(['bill_id' => $bill_id], $oUpData);
|
|
|
|
|
|
if ($result) {
|
|
return ['status' => 1, 'msg' => '退款处理成功'];
|
|
} else {
|
|
return ['status' => 0, 'msg' => '处理失败,请稍后重试'];
|
|
}
|
|
}
|
|
|
|
|
|
/* ******************************************* 下面是一些方法 ******************************************** */
|
|
private function getTimestampByDemand($type = 1)
|
|
{
|
|
// php获取今日开始时间戳和结束时间戳
|
|
$Timestamp[1]['begin'] = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
|
|
$Timestamp[1]['last'] = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
|
|
|
|
// php获取昨日起始时间戳和结束时间戳
|
|
$Timestamp[2]['begin'] = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
|
|
$Timestamp[2]['last'] = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
|
|
|
|
// php获取本月起始时间戳和结束时间戳
|
|
$Timestamp[3]['begin'] = mktime(0, 0, 0, date('m'), 1, date('Y'));
|
|
$Timestamp[3]['last'] = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
|
|
|
|
// php获取上月起始时间戳和结束时间戳
|
|
$Timestamp[4]['begin'] = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
|
|
$Timestamp[4]['last'] = mktime(23, 59, 59, date('m') - 1, date('t'), date('Y'));
|
|
|
|
return $Timestamp[$type];
|
|
|
|
}
|
|
|
|
}
|