From f86a17b738c577612e1edf698512fcd4cb08ed81 Mon Sep 17 00:00:00 2001 From: 466589256 <466589256@qq.com> Date: Wed, 20 Apr 2022 12:44:44 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/shopro/controller/commission/Order.php | 25 +++ addons/shopro/model/commission/Order.php | 148 +++++++++++++++++- 2 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 addons/shopro/controller/commission/Order.php diff --git a/addons/shopro/controller/commission/Order.php b/addons/shopro/controller/commission/Order.php new file mode 100644 index 00000000..7866efca --- /dev/null +++ b/addons/shopro/controller/commission/Order.php @@ -0,0 +1,25 @@ +request->get(); + + $rewards = CommissionOrder::getList($params); + + $this->success('获取成功', $rewards); + } +} diff --git a/addons/shopro/model/commission/Order.php b/addons/shopro/model/commission/Order.php index 789b3e49..40dcefaf 100644 --- a/addons/shopro/model/commission/Order.php +++ b/addons/shopro/model/commission/Order.php @@ -2,29 +2,169 @@ namespace addons\shopro\model\commission; +use addons\shopro\model\User; + +use think\Db; use think\Model; class Order extends Model { + const COMMISSION_ORDER_STATUS_NO = 0; // 不计入 + const COMMISSION_ORDER_STATUS_YES = 1; // 已计入 + const COMMISSION_ORDER_STATUS_BACK = -1; // 已退回 + const COMMISSION_ORDER_STATUS_CANCEL = -2; // 已取消 // 表名 protected $name = 'shopro_commission_order'; - + // 自动写入时间戳字段 protected $autoWriteTimestamp = true; // 定义时间戳字段名 protected $createTime = 'createtime'; protected $updateTime = 'updatetime'; protected $deleteTime = false; + protected $append = [ + 'commission_order_status_text', + // 'commission_reward_status_text', + ]; + + /** + * 获取分销订单明细 + * + * @return array + */ + public static function getList($params) + { + extract($params); + $user = User::info(); + $agentId = $user->id; + $agentId =56; + + // 获取佣金记录 + $oldModes = closeStrict(['ONLY_FULL_GROUP_BY']); + $orders = self::buildSearch($params)->field('id, order_id, buyer_id')->with(['buyer', 'order', 'commission_order' => function ($query) use($agentId){ + $query->with(['reward' =>function($query) use($agentId) { + $query->where('agent_id', $agentId); + }]); + }])->group('order_id')->order('id', 'desc')->paginate($per_page ?? 10); + recoverStrict($oldModes); + + // 获取查询条件的总佣金 + $type = $type ?? 'all'; + if ($type == 'all') { + // 收入,包含待入账 + $amount = self::buildSearch($params)->yes()->sum('amount'); + } else { + $amount = self::buildSearch($params)->sum('amount'); + } + + return [ + 'orders' => $orders, + 'amount' => $amount, + ]; + } + + + /** + * 构建查询条件 + */ + private static function buildSearch($params) + { + $user = User::info(); + extract($params); + + $date = isset($date) ? explode('-', $date) : []; + $start = $date[0] ? strtotime($date[0]) : strtotime(date('Y-m') . '-01'); + $end = $date[1] ? (strtotime($date[1]) + 86399) : strtotime(date('Y-m-d')) + 86399; + + $rewards = self::where('agent_id', $user->id); + + $type = $type ?? 'all'; + if ($type != 'all' && in_array($type, ['yes', 'back', 'cancel'])) { + $rewards = $rewards->{$type}(); + } + + $rewards = $rewards->whereBetween('createtime', [$start, $end]); + + return $rewards; + } + + /** + * 已计入 + */ + public function scopeYes($query) + { + return $query->where('commission_order_status', self::COMMISSION_ORDER_STATUS_YES); + } + /** + * 已退回 + */ + public function scopeBack($query) + { + return $query->where('commission_order_status', self::COMMISSION_ORDER_STATUS_BACK); + } + + /** + * 已取消 + */ + public function scopeCancel($query) + { + return $query->where('commission_order_status', self::COMMISSION_ORDER_STATUS_CANCEL); + } + + /** + * 不计入 + */ + public function scopeNo($query) + { + return $query->where('commission_order_status', self::COMMISSION_ORDER_STATUS_NO); + } + + public function buyer() + { + return $this->belongsTo(\addons\shopro\model\User::class, 'buyer_id')->field('id, avatar, nickname'); + } - public function user() + public function order() { - return $this->belongsTo(\addons\shopro\model\User::class, 'buyer_id')->field('id,avatar,nickname'); + return $this->belongsTo(\app\api\model\wanlshop\Order::class, 'order_id'); } + public function commissionOrder() + { + return $this->hasMany(Order::class, 'order_id', 'order_id'); + } + + public function reward() + { + return $this->hasOne(\addons\shopro\model\commission\Reward::class, 'commission_order_id'); + } public function orderItem() { - return $this->belongsTo(\addons\shopro\model\OrderItem::class, 'order_item_id')->field('id,user_id,order_id,goods_id,goods_title, goods_sku_text,goods_image,goods_original_price,goods_price,goods_num'); + return $this->belongsTo(\addons\shopro\model\OrderItem::class, 'order_item_id')->field('id,user_id,order_id,goods_id,goods_title, goods_sku_text,goods_image,goods_original_price,goods_price,goods_num,refund_status, aftersale_status,dispatch_status,dispatch_type'); + } + + public function getCommissionOrderStatusTextAttr($value, $data) + { + $status_name = ''; + + if (isset($data['commission_order_status'])) { + switch ($data['commission_order_status']) { + case self::COMMISSION_ORDER_STATUS_NO: + $status_name = '不计入'; + break; + case self::COMMISSION_ORDER_STATUS_YES: + $status_name = '已计入'; + break; + case self::COMMISSION_ORDER_STATUS_BACK: + $status_name = '已退回'; + break; + case self::COMMISSION_ORDER_STATUS_CANCEL: + $status_name = '已取消'; + break; + } + } + return $status_name; } } -- Gitee From a356072d0d94ba44a32f7f0958d240d67a4fb768 Mon Sep 17 00:00:00 2001 From: 466589256 <466589256@qq.com> Date: Wed, 20 Apr 2022 15:45:01 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E4=BA=8C=E7=BB=B4?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/shopro/library/Wechat.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/addons/shopro/library/Wechat.php b/addons/shopro/library/Wechat.php index 2e9845cb..44a9f43c 100644 --- a/addons/shopro/library/Wechat.php +++ b/addons/shopro/library/Wechat.php @@ -18,7 +18,10 @@ class Wechat extends Model public function __construct($platform) { - $this->config = Config::getEasyWechatConfig($platform); +// $this->config = Config::getEasyWechatConfig($platform); + $conf = '{"name":"小春B2C","avatar":"","qrcode":"","app_id":"wx4642baf9efc771ed","secret":"f6784e7050ca5b785255e1e91abb48e4","auto_login":"0"}'; + $this->config = json_decode($conf, true); + switch ($platform) { case 'wxOfficialAccount': $this->app = Factory::officialAccount($this->config); -- Gitee From 4d7bbc1177837fa71400214e348d85feb5bc485c Mon Sep 17 00:00:00 2001 From: 466589256 <466589256@qq.com> Date: Thu, 21 Apr 2022 01:41:23 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=88=86=E4=BD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/shopro/hooks.php | 182 +++++++++--------- addons/wanlshop/library/WanlPay/WanlPay.php | 3 + application/api/controller/Index.php | 5 +- application/api/controller/wanlshop/Order.php | 11 +- application/api/model/wanlshop/Order.php | 1 + .../common/behavior/wanlshop/hooks.php | 2 +- .../listener/commission/CommissionHook.php | 36 ++-- .../common/library/commission/Agent.php | 9 +- .../common/library/commission/Commission.php | 23 +-- .../index/model/wanlshop/OrderGoods.php | 7 +- 10 files changed, 149 insertions(+), 130 deletions(-) diff --git a/addons/shopro/hooks.php b/addons/shopro/hooks.php index f769c912..3fb394d8 100644 --- a/addons/shopro/hooks.php +++ b/addons/shopro/hooks.php @@ -1,96 +1,96 @@ [ // 订单创建前 - 'addons\\shopro\\listener\\order\\Create' - ], - 'order_create_after' => [ // 订单创建后 - 'addons\\shopro\\listener\\order\\Create' - ], - 'order_payed_after' => [ // 订单支付成功 - 'addons\\shopro\\listener\\order\\Payed' - ], - - // 订单关闭 - 'order_close_before' => [ // 订单关闭前 - ], - 'order_close_after' => [ // 订单关闭后 - 'addons\\shopro\\listener\\order\\Invalid' - ], - - // 订单取消 - 'order_cancel_before' => [ // 订单取消前 - ], - 'order_cancel_after' => [ // 订单取消后 - 'addons\\shopro\\listener\\order\\Invalid' - ], - - // 订单发货 - 'order_send_before' => [ // 订单发货前 - ], - 'order_send_after' => [ // 订单发货后 - 'addons\\shopro\\listener\\order\\Send' - ], - - // 订单确认收货 - 'order_confirm_before' => [ // 订单确认收货前 - ], - 'order_confirm_after' => [ // 订单确认收货后 - 'addons\\shopro\\listener\\order\\Confirm' - ], - - // 订单完成事件 - 'order_finish' => [], - - // 订单评价 - 'order_comment_before' => [ // 订单评价前 - ], - 'order_comment_after' => [ // 订单评价后 - 'addons\\shopro\\listener\\order\\Comment' - ], - - // 订单退款 - 'order_refund_before' => [ // 订单退款前 - 'addons\\shopro\\listener\\order\\Refund' - ], - 'order_refund_after' => [ // 订单退款后 - 'addons\\shopro\\listener\\order\\Refund' - ], - - // 售后完成 - 'aftersale_finish_before' => [ // 售后完成前 - ], - 'aftersale_finish_after' => [ // 售后完成后 - ], - - // 售后拒绝 - 'aftersale_refuse_before' => [ // 售后拒绝前 - ], - 'aftersale_refuse_after' => [ // 售后拒绝后 - ], - - // 售后变动,(包含完成,拒绝) - 'aftersale_change' => [ // 售后变动 - 'addons\\shopro\\listener\\order\\Aftersale' - ], - - // 活动更新 - 'activity_update_after' => [ // 活动更新后 - 'addons\\shopro\\listener\\activity\\Update' - ], - 'activity_delete_after' => [ // 活动删除之后 - 'addons\\shopro\\listener\\activity\\Update' - ], - - // 拼团 - 'activity_groupon_finish' => [ // 拼团成功 - 'addons\\shopro\\listener\\activity\\Groupon' - ], - 'activity_groupon_fail' => [ // 拼团失败,超时,后台手动解散等 - 'addons\\shopro\\listener\\activity\\Groupon' - ] -]; +//$defaultHooks = [ +// // 订单创建 +// 'order_create_before' => [ // 订单创建前 +// 'addons\\shopro\\listener\\order\\Create' +// ], +// 'order_create_after' => [ // 订单创建后 +// 'addons\\shopro\\listener\\order\\Create' +// ], +// 'order_payed_after' => [ // 订单支付成功 +// 'addons\\shopro\\listener\\order\\Payed' +// ], +// +// // 订单关闭 +// 'order_close_before' => [ // 订单关闭前 +// ], +// 'order_close_after' => [ // 订单关闭后 +// 'addons\\shopro\\listener\\order\\Invalid' +// ], +// +// // 订单取消 +// 'order_cancel_before' => [ // 订单取消前 +// ], +// 'order_cancel_after' => [ // 订单取消后 +// 'addons\\shopro\\listener\\order\\Invalid' +// ], +// +// // 订单发货 +// 'order_send_before' => [ // 订单发货前 +// ], +// 'order_send_after' => [ // 订单发货后 +// 'addons\\shopro\\listener\\order\\Send' +// ], +// +// // 订单确认收货 +// 'order_confirm_before' => [ // 订单确认收货前 +// ], +// 'order_confirm_after' => [ // 订单确认收货后 +// 'addons\\shopro\\listener\\order\\Confirm' +// ], +// +// // 订单完成事件 +// 'order_finish' => [], +// +// // 订单评价 +// 'order_comment_before' => [ // 订单评价前 +// ], +// 'order_comment_after' => [ // 订单评价后 +// 'addons\\shopro\\listener\\order\\Comment' +// ], +// +// // 订单退款 +// 'order_refund_before' => [ // 订单退款前 +// 'addons\\shopro\\listener\\order\\Refund' +// ], +// 'order_refund_after' => [ // 订单退款后 +// 'addons\\shopro\\listener\\order\\Refund' +// ], +// +// // 售后完成 +// 'aftersale_finish_before' => [ // 售后完成前 +// ], +// 'aftersale_finish_after' => [ // 售后完成后 +// ], +// +// // 售后拒绝 +// 'aftersale_refuse_before' => [ // 售后拒绝前 +// ], +// 'aftersale_refuse_after' => [ // 售后拒绝后 +// ], +// +// // 售后变动,(包含完成,拒绝) +// 'aftersale_change' => [ // 售后变动 +// 'addons\\shopro\\listener\\order\\Aftersale' +// ], +// +// // 活动更新 +// 'activity_update_after' => [ // 活动更新后 +// 'addons\\shopro\\listener\\activity\\Update' +// ], +// 'activity_delete_after' => [ // 活动删除之后 +// 'addons\\shopro\\listener\\activity\\Update' +// ], +// +// // 拼团 +// 'activity_groupon_finish' => [ // 拼团成功 +// 'addons\\shopro\\listener\\activity\\Groupon' +// ], +// 'activity_groupon_fail' => [ // 拼团失败,超时,后台手动解散等 +// 'addons\\shopro\\listener\\activity\\Groupon' +// ] +//]; // 分销相关钩子 $commissionHooks = [ diff --git a/addons/wanlshop/library/WanlPay/WanlPay.php b/addons/wanlshop/library/WanlPay/WanlPay.php index a91e1b9f..6bd2b14d 100755 --- a/addons/wanlshop/library/WanlPay/WanlPay.php +++ b/addons/wanlshop/library/WanlPay/WanlPay.php @@ -644,6 +644,9 @@ class WanlPay model('app\api\model\wanlshop\groups\Groups')->isUpdate()->saveAll($groups_list); model('app\api\model\wanlshop\groups\Team')->saveAll($groups_team_list); } + //计算佣金 + $data = ['order' => $model_order, 'user' => []]; + \think\Hook::listen('order_payed_after', $data); } catch (\Exception $e) { return ['code' => 10008 ,'msg' => $e->getMessage()]; } diff --git a/application/api/controller/Index.php b/application/api/controller/Index.php index 6d61ad7d..8d1e0a8a 100755 --- a/application/api/controller/Index.php +++ b/application/api/controller/Index.php @@ -4,6 +4,7 @@ namespace app\api\controller; use app\admin\model\Banner; use app\admin\model\LeaveMessage; + use app\common\controller\Api; use app\common\library\Hook; @@ -19,7 +20,9 @@ class Index extends Api { Hook::register(); - $data = ['a' => time()]; + $order = \app\api\model\wanlshop\Order::get(1); + $user = \app\api\model\User::get($order -> user_id); + $data = ['order' => $order, 'user' => []]; \think\Hook::listen('order_payed_after', $data); } diff --git a/application/api/controller/wanlshop/Order.php b/application/api/controller/wanlshop/Order.php index ee23cc0f..6a77b5bf 100755 --- a/application/api/controller/wanlshop/Order.php +++ b/application/api/controller/wanlshop/Order.php @@ -231,8 +231,12 @@ class Order extends Api } // 更新退款 $order->save(['state' => 4,'taketime' => time()],['id' => $id]); + //计算 佣金 + \think\Hook::listen('order_confirm_after', $hookData); $this->success('ok', $order ? true : false); - } + + + } $this->error(__('非法请求')); } @@ -421,7 +425,10 @@ class Order extends Api 'score_deliver' => bcdiv(array_sum($deliver), count($deliver), 1), 'score_logistics' => bcdiv(array_sum($logistics), count($logistics), 1) ]); - $this->success('ok',[]); + //发放佣金 + \think\Hook::listen('order_finish', $params); + + $this->success('ok',[]); } $this->error(__('非法请求')); } diff --git a/application/api/model/wanlshop/Order.php b/application/api/model/wanlshop/Order.php index 8d491084..ce2e3001 100755 --- a/application/api/model/wanlshop/Order.php +++ b/application/api/model/wanlshop/Order.php @@ -99,6 +99,7 @@ class Order extends Model } + public function user() { return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0); diff --git a/application/common/behavior/wanlshop/hooks.php b/application/common/behavior/wanlshop/hooks.php index f9adc024..6c74a7c2 100644 --- a/application/common/behavior/wanlshop/hooks.php +++ b/application/common/behavior/wanlshop/hooks.php @@ -119,4 +119,4 @@ if (file_exists(ROOT_PATH . 'addons/shopro/listener/commission')) { $defaultHooks = array_merge_recursive($defaultHooks, $commissionHooks); } -return $defaultHooks; +return $commissionHooks; diff --git a/application/common/behavior/wanlshop/listener/commission/CommissionHook.php b/application/common/behavior/wanlshop/listener/commission/CommissionHook.php index a25a27f7..216da8aa 100644 --- a/application/common/behavior/wanlshop/listener/commission/CommissionHook.php +++ b/application/common/behavior/wanlshop/listener/commission/CommissionHook.php @@ -25,32 +25,30 @@ class CommissionHook // 订单支付成功 $order = $params['order']; // 重新查询订单 - $order = Order::with('goods')->where('id', $order['id'])->find(); + $order = Order::with(['goods'=>['goods_sku']])->where('id', $order['id'])->find(); if (!$order) return false; $agent = new AgentLibrary($order['user_id']); if (!$agent->user) return false; - // 加用户消费金额 - if ($order['total_fee'] > 0) { - $agent->user->setInc('total_consume', $order['total_fee']); - } +// // 加用户消费金额 +// if ($order['total_fee'] > 0) { +// $agent->user->setInc('total_consume', $order['total_fee']); +// } // 绑定关系 $agent->bindUserRelation('pay'); - - if ($order['type'] !== 'score') { // 积分商品订单不参与分销 // 先记录分佣 再处理记录业绩、升级等情况 - $items = $order ? $order['item'] : []; - foreach ($items as $item) { - $commission = new CommissionLibrary($item); - // 检查能否分销 - if ($commission->checkAndSetCommission()) { - // 添加分销订单 - $commissionOrder = $commission->addCommissionOrder(); - // 执行奖金计划 - $commission->runCommissionPlan($commissionOrder); - // 支付后拨款 - (new RewardLibrary())->runCommissionRewardByOrder('payed', $commissionOrder->id); - } + $items = $order ? $order -> goods : []; + foreach ($items as $item) { + $item -> user_id = $order -> user_id; + $commission = new CommissionLibrary($item); + // 检查能否分销 + if ($commission->checkAndSetCommission()) { + // 添加分销订单 + $commissionOrder = $commission->addCommissionOrder(); + // 执行奖金计划 + $commission->runCommissionPlan($commissionOrder); + // 支付后拨款 + (new RewardLibrary())->runCommissionRewardByOrder('payed', $commissionOrder->id); } } diff --git a/application/common/library/commission/Agent.php b/application/common/library/commission/Agent.php index e52189eb..169b9013 100644 --- a/application/common/library/commission/Agent.php +++ b/application/common/library/commission/Agent.php @@ -67,6 +67,7 @@ class Agent public function isAgentAvaliable() { $status = $this->getAgentStatus(); + if (in_array($status, [self::AGENT_STATUS_NORMAL, self::AGENT_STATUS_FREEZE])) { return true; } @@ -170,12 +171,13 @@ class Agent $this->parentAgentId = 0; $parent_user_id = $this->user->parent_user_id; // 未绑定分销商,从分享记录查找最近的分销商 - if ($parent_user_id === null) { + if (empty($parent_user_id)) { $sharedLog = Share::checkLatestShareLogByAgent($this->user->id); if ($sharedLog) { $parent_user_id = $sharedLog['share_id']; } } + if ($parent_user_id > 0) { // 再次检查上级分销商是否可用 $parentAgent = AgentModel::avaliable()->where(['user_id' => $parent_user_id])->find(); @@ -184,7 +186,6 @@ class Agent } } } - return $this->parentAgentId; } @@ -288,9 +289,10 @@ class Agent } // 该用户已经有上级 - if ($this->user->parent_user_id !== null) { + if (!empty($this->user->parent_user_id)) { return false; } + if ($bindAgentId) { $bindAgent = new Agent($bindAgentId); } else { @@ -299,6 +301,7 @@ class Agent $bindAgent = new Agent($sharedLog['share_id']); } } + if (!isset($bindAgent) || !$bindAgent->isAgentAvaliable()) { return false; } diff --git a/application/common/library/commission/Commission.php b/application/common/library/commission/Commission.php index b7319168..b65042e7 100644 --- a/application/common/library/commission/Commission.php +++ b/application/common/library/commission/Commission.php @@ -59,7 +59,7 @@ class Commission } // 获取商品分销佣金规则 - $this->goods = new Goods($this->item['goods_id'], $this->item['goods_sku_price_id']); + $this->goods = new Goods($this->item['goods_id'], $this->item['goods_sku_id']); // 商品有独立分销设置,覆盖默认系统配置 if ($this->goods->getCommissionConfig()) { @@ -77,7 +77,6 @@ class Commission if (!$this->buyer->getParentAgentId() && !$this->selfBuy) { return false; } - // 获取商品结算价格(四舍五入精确到分) $this->amount = $this->getGoodsCommissionAmount(); // 没有分润的必要了 @@ -93,14 +92,15 @@ class Commission { $commissionType = $this->_config->getCommissionPriceType(); $amount = round(0, 2); - switch ($commissionType) { - case 'pay_price': - $amount = $this->item['pay_price']; - break; - case 'goods_price': - $amount = round($this->item['goods_price'] * $this->item['goods_num'], 2); - break; - } +// switch ($commissionType) { +// case 'pay_price': +// $amount = $this->item['pay_price']; +// break; +// case 'goods_price': +// $amount = round($this->item['goods_price'] * $this->item['goods_num'], 2); +// break; +// } + $amount = round(bcsub($this->item -> goods_sku -> price,$this->item -> goods_sku -> factory_price) * $this->item['number'], 2); return $amount; } @@ -111,6 +111,7 @@ class Commission public function addCommissionOrder() { $commissionOrder = OrderModel::where('order_item_id', $this->item['id'])->find(); + // 已添加过分销订单 if ($commissionOrder) { return $commissionOrder; @@ -175,7 +176,7 @@ class Commission $currentAgentLevel = $currentAgent->getAgentLevel(); $currentCommissionLevelRule = $this->goods->getCommissionLevelRule($currentAgentLevel, $currentCommissionLevel); if ($currentCommissionLevelRule) { - $commission = $this->goods->caculateGoodsCommission($currentCommissionLevelRule, $this->amount, $this->item['goods_num']); + $commission = $this->goods->caculateGoodsCommission($currentCommissionLevelRule, $this->amount, $this->item['number']); if ($commission > 0) { $commissionRewardParams = [ 'order_id' => $commissionOrder->order_id, diff --git a/application/index/model/wanlshop/OrderGoods.php b/application/index/model/wanlshop/OrderGoods.php index b99ecdc7..a60e9406 100755 --- a/application/index/model/wanlshop/OrderGoods.php +++ b/application/index/model/wanlshop/OrderGoods.php @@ -32,8 +32,11 @@ class OrderGoods extends Model { return ['normal' => __('Normal'), 'hidden' => __('Hidden')]; } - - + + public function goodsSku() + { + return $this->hasOne( \app\index\model\wanlshop\GoodsSku::class, 'id', 'goods_sku_id'); + } public function getStatusTextAttr($value, $data) { -- Gitee