ajaxReturn($res); } } /** * 获取分销员的客户 */ public function getMyCustomer() { $page = I('post.page') ? I('post.page') : 1; $limit = I('post.limit') ? I('post.limit') : 10; $search = I('post.search'); $LinkViewModel = new LinkViewModel(); $distributor_id = $this->verifyDistributor(self::$uid, self::$merchant_id); $map = [ 'distributor_id' => $distributor_id, 'status' => 1, ]; if ($search) { $map['nickname'] = ['like', '%' . $search . '%']; } $field = [ 'add_time', 'uid', 'nickname', 'avatar', ]; $list = $LinkViewModel->getGroupSelect($map, 'uid', $field, 'add_time desc', $page . ',' . $limit); $count = $LinkViewModel->getGroupCount($map, 'uid'); if ($list) { foreach ($list as &$val) { $val['add_time'] = date('Y-m-d H:i:s', $val['add_time']); } $res['status'] = 1; $res['msg'] = '数据获取成功!'; $res['data'] = [ 'list' => $list, 'count' => $count, ]; } else { $res['status'] = 1; $res['msg'] = '暂无数据!'; $res['data'] = [ 'list' => [], 'count' => 0, ]; } $this->ajaxReturn($res); } /** * 获取分销订单-新版 * 2020年12月18日09:44:58 * @param specified 客户或分销员的ID * @param search 搜索内容 * @param page 页码 * @param limit 页长 * @param type 1=我的分销员,2=我的客户,other=我所有的分销订单 * @param withdraw_state 0未结算 1已结算 3全部 */ public function getDistriOrder() { $specified = I('post.specified'); $search = I('post.search'); $page = I('post.page') ? I('post.page') : 1; $limit = I('post.limit') ? I('post.limit') : 10; $type = I('post.type'); $withdraw_state = I('post.withdraw_state') ? I('post.withdraw_state') : 0; $uid = self::$uid; $merchant_id = self::$merchant_id; $SellModel = new SellModel(); $OrderModel = new OrderModel(); $ConfigModel = new ConfigModel(); $DistributorModel = new DistributorModel(); $BillOnlineModel = new RetailBillOnlineModel(); $RetailSellModel = new RetailSellModel(); $distributor_id = $this->verifyDistributor($uid, $merchant_id); $config = $ConfigModel->getFind(['merchant_id' => $merchant_id], 'balance_date, is_self_buying'); if ($type == 1) { // 获取下单用户的uid $uid_info = $DistributorModel->getFind(['id' => $specified], 'uid'); $where['_complex'] = [ [ 'diso.did' => $specified, 'diso.pid' => $distributor_id ], [ 'diso.did' => $distributor_id, 'rbo.uid' => $uid_info['uid'] ], '_logic' => 'or' ]; } else if ($type == 2) { $where['diso.did'] = $distributor_id; // 一级分销佣金 $where['rbo.uid'] = $specified; // 下单用户 } else { $where['diso.did|diso.pid'] = $distributor_id; // All in分销订单 } if ($search) { $where['rbo.order_no|rx.nickname'] = ['like', "%{$search}%"]; } if ($withdraw_state == 1) { $where['diso.withdraw_state'] = 1; } else if ($withdraw_state == 0) { $where['diso.withdraw_state'] = 0; } else if ($withdraw_state == 3) { $where['diso.withdraw_state'] = ['NEQ', 2]; } $where['rbo.merchant_id'] = $merchant_id; $where['rbo.order_type'] = 1; // 购买订单 $where['rbo.pay_state'] = 1; $field = [ 'diso.did', 'diso.bill_id', 'diso.withdraw_state', 'diso.sell_first_money', 'diso.sell_second_money', 'diso.first_refund_money', 'diso.second_refund_money', 'diso.settlement_date', 'rbo.order_no', 'rbo.add_time', 'rbo.type', 'rx.nickname', 'rx.avatar', ]; $orderList = $OrderModel->getDistriOrder($where, $field, $page, $limit, 'diso.id desc'); if ($orderList['list']) { foreach ($orderList['list'] as &$value) { // 订单下所有出售的分销商品 $value['sellList'] = $SellModel->getSelect(['bill_id' => $value['bill_id']]); foreach ($value['sellList'] as &$val) { $goodsInfo = $RetailSellModel->getFind(['id' => $val['sell_id']], 'goods_id,goods_img_id'); $RetailGoodsModel = new RetailGoodsModel(); $val['image_path'] = getGoodsImage($goodsInfo['goods_img_id'], self::$store_id); $val['total_amount'] = round($val['total_amount'], 2); $val['goods_name'] = $RetailGoodsModel->getFindField(['id' => $goodsInfo['goods_id']], 'goods_name'); $val['dis_goods_id'] = $goodsInfo['goods_id']; // first_refund_money second_refund_money bcsub if ($type == 1) { if ($value['did'] != $specified) { // 非自购订单 $val['amount_commission'] = bcsub($val['sell_first_money'], $val['first_refund_money'], 2); } else { $val['amount_commission'] = bcsub($val['sell_second_money'], $val['second_refund_money'], 2); } } else if ($type == 2) { $val['amount_commission'] = bcsub($val['sell_first_money'], $val['first_refund_money'], 2); } else { if ($value['did'] == $distributor_id) { $val['amount_commission'] = bcsub($val['sell_first_money'], $val['first_refund_money'], 2); } else { $val['amount_commission'] = bcsub($val['sell_second_money'], $val['second_refund_money'], 2); } } unset($val['sell_first_money']); unset($val['sell_second_money']); unset($val['first_refund_money']); unset($val['second_refund_money']); } if ($value['withdraw_state'] == 0) { $confirm_time = $BillOnlineModel->getFind(['bill_id' => $value['bill_id']], 'receiving_goods_time'); if ($confirm_time['receiving_goods_time']) { $value['expect_time'] = $config['balance_date'] * 86400 + $confirm_time['receiving_goods_time']; $value['expect_time'] = date('Y-m-d H:i:s', $value['expect_time']); } else { $value['expect_time'] = ''; } } else { $value['expect_time'] = date('Y-m-d H:i:s', $value['settlement_date']); } // 时间处理 $value['add_time'] = date('Y-m-d H:i:s', $value['add_time']); if ($value['settlement_date']) { $value['settlement_date'] = date('Y-m-d H:i:s', $value['settlement_date']); } unset($value['did']); unset($value['vid']); unset($value['type']); unset($value['sell_first_money']); unset($value['sell_second_money']); unset($value['first_refund_money']); unset($value['second_refund_money']); } $result = [ 'status' => 1, 'msg' => '订单获取成功', 'data' => $orderList, ]; } else { $result = [ 'status' => 1, 'msg' => '暂无数据', 'data' => [], ]; } $this->ajaxReturn($result); } // 预估佣金计算 public function userCommissionExpected() { $type = I('post.type') ? I('post.type') : 3; $times = I('post.times') ? I('post.times') : 1; // 预留 $distributor_id = I('post.distributor_id'); // 理论上是当前登录用户 $merchant_id = self::$merchant_id; $uid = self::$uid; $OrderModel = new OrderModel(); if (!$distributor_id) { $distributor_id = $this->verifyDistributor($uid, $merchant_id); } $where['rbo.merchant_id'] = $merchant_id; $where['rbo.pay_state'] = 1; // 已支付订单进入预算 $where['rbo.order_type'] = 1; // 购买订单 $where['diso.withdraw_state'] = ['NEQ', 2]; $commission['today'] = $OrderModel->userCommissionExpectedByTime($where, $distributor_id, $type, 1); $commission['yesterday'] = $OrderModel->userCommissionExpectedByTime($where, $distributor_id, $type, 2); $commission['month'] = $OrderModel->userCommissionExpectedByTime($where, $distributor_id, $type, 3); $commission['last_month'] = $OrderModel->userCommissionExpectedByTime($where, $distributor_id, $type, 4); $res['status'] = 1; $res['msg'] = '佣金取成功!'; $res['data'] = $commission; $this->ajaxReturn($res); } /* ******************************************* 下面是一些方法 ************************`******************** */ private function verifyDistributor($uid, $merchant_id) { $DistributorModel = new DistributorModel(); $distributor_id = $DistributorModel->verifyUserDistributor($uid, $merchant_id); if ($distributor_id) { return $distributor_id; } else { $res['status'] = 0; $res['msg'] = 'Authentication failed,Please try again!'; $res['data'] = ''; $this->ajaxReturn($res); } } }