123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- declare (strict_types = 1);
- namespace app\common\service\commission;
- use app\api\model\user\CommissionsDetail;
- use app\common\model\Order;
- use app\common\model\OrderGoods;
- use app\common\model\OrderRefund;
- use app\common\service\BaseService;
- use app\common\service\Message;
- use app\console\model\user\WithdrawMoneyLog;
- use Exception;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- /**
- * 确认收货后分佣下发
- * Class Refund
- * @package app\common\service\order
- */
- class GiveOutCommission extends BaseService
- {
- /**
- * 确认收货后分佣派发
- * @param $orderId int 订单id
- * @return bool
- */
- public function testConfirmOrderCommission(int $orderId): bool
- {
- $commissionDetails = CommissionsDetail::alias('cm')
- ->leftJoin('order og','cm.order_id=og.order_id')
- ->where('cm.order_id',$orderId)
- ->where('cm.clearing_status',0)
- ->where('og.commission_settlement_time','>',0)
- ->where('og.commission_settlement_time','<',time())
- ->where('og.commission_settlement_status',0)
- ->field('cm.id,cm.order_id,cm.user_id,cm.clearing_money,cm.commission_percent,og.order_no')
- ->select();
- if ($commissionDetails->isEmpty()){
- return true;
- }
- $data = [];
- $infos = [];
- try {
- foreach ($commissionDetails as $detail){
- if(self::endCommissionDetail($detail,$data) === false){
- $infos[] = '分佣失败commissionId::'.$detail->id;
- }
- }
- if (count($data)){
- Order::whereIn('order_id',$data)->update(['commission_settlement_status'=>1]);
- }
- }catch (Exception $e){
- $msg = __METHOD__.",orderId::".$orderId.'结算分佣失败'.$e->getMessage();
- log_record($msg,'error');
- Message::wxRobot($msg);
- return false;
- }
- if (count($infos)){
- $infoStr = json_encode($infos);
- log_record('info::'.$infoStr,'error');
- Message::wxRobot($infoStr);
- }
- return true;
- }
- /**
- * 处理单条待结算分佣记录
- * @param $detail
- * @param $data
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function endCommissionDetail($detail,&$data): bool
- {
- $orderGoods = OrderGoods::field('order_goods_id,total_num,total_pay_price,rice_card_money,total_pay_price,frozen_status')
- ->where('order_id',$detail->order_id)->select();
- $frozenS = $orderGoods->column('frozen_status');
- $s = array_search(1,$frozenS);//查找是否有冻结中的订单商品
- if (!($s === false)){
- log_record(__METHOD__.':orderGoods frozen orderId:'.$detail->order_id,'error');
- return false;
- }
- $saleVolume = '0';
- $refundCnt = 0;
- foreach ($orderGoods as $og){
- $rate = 1;
- $refund = OrderRefund::where('order_goods_id',$og->order_goods_id)
- ->where('finance_refund',10)->find();
- $ogSale = bcadd($og['total_pay_price'],$og['rice_card_money'],4);
- if ($refund){
- $refundCnt += 1;
- $rate = bcdiv(strval($og['total_num'] - $refund['goods_num']),strval($og['total_num']),4);
- }
- if ($rate <= 1 && $rate >= 0){
- $saleVolume = bcadd(bcmul($ogSale,strval($rate),4),strval($saleVolume),4);
- }
- }
- $clearing_money = bcmul($saleVolume,strval($detail->commission_percent/100),4);
- $can_withdraw_money = WithdrawMoneyLog::addNewLog($detail->user_id,50,$clearing_money,'',$detail->order_id);
- if ($can_withdraw_money === false){
- log_record(__METHOD__.':failed orderId:'.$detail->order_id,'error');
- return false;
- }
- $remark = $refundCnt>0?'订单退部分扣除部分佣金':'';
- // 结算金额累计
- CommissionsDetail::where('id', $detail->id)
- ->update(['clearing_money_amount'=>$can_withdraw_money,
- 'clearing_money'=>$clearing_money,
- 'order_sale_volume'=>$saleVolume,
- 'clearing_status'=>1,
- 'remark'=>$remark,
- 'update_time'=>time()]);
- if (!in_array($detail->order_id,$data)){
- $data[] = $detail->order_id;
- }
- return true;
- }
- }
|