|
@@ -14,6 +14,7 @@ use app\dao\order\StoreOrderDao;
|
|
|
use app\jobs\BatchHandleJob;
|
|
|
use app\jobs\order\AutoOrderUnpaidCancelJob;
|
|
|
use app\jobs\order\SpliteStoreOrderJob;
|
|
|
+use app\model\system\config\SystemConfig;
|
|
|
use app\services\activity\combination\StorePinkServices;
|
|
|
use app\services\activity\coupon\StoreCouponIssueServices;
|
|
|
use app\services\BaseServices;
|
|
@@ -29,6 +30,7 @@ use app\services\store\SystemStoreServices;
|
|
|
use app\services\system\config\ConfigServices;
|
|
|
use app\services\system\form\SystemFormServices;
|
|
|
use app\services\user\UserAwardIntegralServices;
|
|
|
+use app\services\user\UserBrokerageServices;
|
|
|
use app\services\user\UserInvoiceServices;
|
|
|
use app\services\user\UserServices;
|
|
|
use app\services\product\product\StoreProductReplyServices;
|
|
@@ -2749,4 +2751,202 @@ HTML;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 地区返佣
|
|
|
+ * @param $orderInfo
|
|
|
+ * @param $userInfo
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function backAreaBrokerage()
|
|
|
+ {
|
|
|
+ //商城分销功能是否开启 0关闭1开启
|
|
|
+ if (!sys_config('brokerage_func_status')) return true;
|
|
|
+
|
|
|
+ if (sys_config('area_award_time') <= 0) return true;
|
|
|
+
|
|
|
+ $last_time = sys_config('area_award_last_time');
|
|
|
+ if ($last_time + (3600 * (sys_config('area_award_time'))) > time()) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ //冻结时间
|
|
|
+ $broken_time = intval(sys_config('extract_time'));
|
|
|
+ $frozen_time = time() + $broken_time * 86400;
|
|
|
+ $userServices = app()->make(UserServices::class);
|
|
|
+ $storeOrderServices = app()->make(StoreOrderServices::class);
|
|
|
+ $res = true;
|
|
|
+
|
|
|
+ // 获取上级推广员信息
|
|
|
+ /** @var UserServices $userServices */
|
|
|
+
|
|
|
+ //找有代理的省
|
|
|
+ $provinces = $userServices->search()->where('area_admin', 3)->column('area_admin_province');
|
|
|
+ $provinces = array_unique($provinces);
|
|
|
+ foreach ($provinces as $value) {
|
|
|
+ $order_price = $storeOrderServices->search(['pid' => 0])->where('order_province', $value)
|
|
|
+ ->whereNotIn('type', [1, 2, 3, 5, 8])
|
|
|
+ ->where('send_area_award', 0)
|
|
|
+ ->where('paid', 1)
|
|
|
+ ->where('is_del', 0)
|
|
|
+ ->where('is_system_del', 0)
|
|
|
+ ->where('refund_status', 0)
|
|
|
+ ->whereIn('status', [2, 3])
|
|
|
+ ->field('sum(total_price-cost) as total_price_sum')
|
|
|
+ ->find();
|
|
|
+ $order_price = $order_price['total_price_sum'] ?? 0;
|
|
|
+ if ($order_price <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //找省代
|
|
|
+ $admins = $userServices->search()->where('area_admin', 3)->where('area_admin_province', $value)->select();
|
|
|
+ if (count($admins) > 0) {
|
|
|
+ $award = bcdiv(bcmul((string)sys_config('province_award'), (string)$order_price, 2), 100, 2);
|
|
|
+ if ($award > 0) {
|
|
|
+ $brokeragePrice = bcdiv($award, (string)count($admins), 2);
|
|
|
+ if ($brokeragePrice > 0) {
|
|
|
+ foreach ($admins as $v) {
|
|
|
+ $spreadPrice = (string)$v['brokerage_price'];
|
|
|
+ // 上级推广员返佣之后的金额
|
|
|
+ $balance = bcadd($spreadPrice, $brokeragePrice, 2);
|
|
|
+ // 添加佣金记录
|
|
|
+ /** @var UserBrokerageServices $userBrokerageServices */
|
|
|
+ $userBrokerageServices = app()->make(UserBrokerageServices::class);
|
|
|
+ $type = 'get_area_award';
|
|
|
+ $res1 = $userBrokerageServices->income($type, $v['uid'], [
|
|
|
+ 'pay_price' => floatval($order_price),
|
|
|
+ 'area' => $value . '省级',
|
|
|
+ 'number' => floatval($brokeragePrice),
|
|
|
+ 'frozen_time' => $frozen_time
|
|
|
+ ], $balance, 0);
|
|
|
+ // 添加用户佣金
|
|
|
+ $res2 = $userServices->bcInc($v['uid'], 'brokerage_price', $brokeragePrice, 'uid');
|
|
|
+ $res = $res1 && $res2 && $res;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $citys = $userServices->search()->where('area_admin', 2)->select();
|
|
|
+ $pc = [];
|
|
|
+ foreach ($citys as $v) {
|
|
|
+ $p = $v['area_admin_province'];
|
|
|
+ $c = $v['area_admin_city'];
|
|
|
+ if (!in_array(compact('p', 'c'), $pc)) {
|
|
|
+ $pc[] = compact('p', 'c');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach ($pc as $value) {
|
|
|
+ $order_price = $storeOrderServices->search(['pid' => 0])->where('order_province', $value['p'])->where('order_city', $value['c'])
|
|
|
+ ->whereNotIn('type', [1, 2, 3, 5, 8])
|
|
|
+ ->where('send_area_award', 0)
|
|
|
+ ->where('paid', 1)
|
|
|
+ ->where('is_del', 0)
|
|
|
+ ->where('is_system_del', 0)
|
|
|
+ ->where('refund_status', 0)
|
|
|
+ ->whereIn('status', [2, 3])
|
|
|
+ ->field('sum(total_price-cost) as total_price_sum')
|
|
|
+ ->find();
|
|
|
+ $order_price = $order_price['total_price_sum'] ?? 0;
|
|
|
+ if ($order_price <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //找市代
|
|
|
+ $admins = $userServices->search()->where('area_admin', 2)->where('area_admin_province', $value['p'])
|
|
|
+ ->where('area_admin_city', $value['c'])->select();
|
|
|
+ if (count($admins) > 0) {
|
|
|
+ $award = bcdiv(bcmul((string)sys_config('city_award'), (string)$order_price, 2), 100, 2);
|
|
|
+ if ($award > 0) {
|
|
|
+ $brokeragePrice = bcdiv($award, (string)count($admins), 2);
|
|
|
+ if ($brokeragePrice > 0) {
|
|
|
+ foreach ($admins as $v) {
|
|
|
+ $spreadPrice = (string)$v['brokerage_price'];
|
|
|
+ // 上级推广员返佣之后的金额
|
|
|
+ $balance = bcadd($spreadPrice, $brokeragePrice, 2);
|
|
|
+ // 添加佣金记录
|
|
|
+ /** @var UserBrokerageServices $userBrokerageServices */
|
|
|
+ $userBrokerageServices = app()->make(UserBrokerageServices::class);
|
|
|
+ $type = 'get_area_brokerage';
|
|
|
+ $res1 = $userBrokerageServices->income($type, $v['uid'], [
|
|
|
+ 'pay_price' => floatval($order_price),
|
|
|
+ 'area' => $value['p'] . $value['c'] . '级',
|
|
|
+ 'number' => floatval($brokeragePrice),
|
|
|
+ 'frozen_time' => $frozen_time
|
|
|
+ ], $balance, 0);
|
|
|
+ // 添加用户佣金
|
|
|
+ $res2 = $userServices->bcInc($v['uid'], 'brokerage_price', $brokeragePrice, 'uid');
|
|
|
+ $res = $res1 && $res2 && $res;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $citys = $userServices->search()->where('area_admin', 1)->select();
|
|
|
+ $pcd = [];
|
|
|
+ foreach ($citys as $v) {
|
|
|
+ $p = $v['area_admin_province'];
|
|
|
+ $c = $v['area_admin_city'];
|
|
|
+ $d = $v['area_admin_district'];
|
|
|
+ if (!in_array(compact('p', 'c', 'd'), $pcd)) {
|
|
|
+ $pcd[] = compact('p', 'c', 'd');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach ($pcd as $value) {
|
|
|
+
|
|
|
+ $order_price = $storeOrderServices->search(['pid' => 0])
|
|
|
+ ->where('order_province', $value['p'])
|
|
|
+ ->where('order_city', $value['c'])
|
|
|
+ ->where('order_district', $value['d'])
|
|
|
+ ->whereNotIn('type', [1, 2, 3, 5, 8])
|
|
|
+ ->where('send_area_award', 0)
|
|
|
+ ->where('paid', 1)
|
|
|
+ ->where('is_del', 0)
|
|
|
+ ->where('is_system_del', 0)
|
|
|
+ ->where('refund_status', 0)
|
|
|
+ ->whereIn('status', [2, 3])
|
|
|
+ ->field('sum(total_price-cost) as total_price_sum')
|
|
|
+ ->find();
|
|
|
+ $order_price = $order_price['total_price_sum'] ?? 0;
|
|
|
+ if ($order_price <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ //找区代
|
|
|
+ $admins = $userServices->search()->where('area_admin', 1)
|
|
|
+ ->where('area_admin_province', $value['p'])
|
|
|
+ ->where('area_admin_city', $value['c'])
|
|
|
+ ->where('area_admin_district', $value['d'])
|
|
|
+ ->select();
|
|
|
+ if (count($admins) > 0) {
|
|
|
+ $award = bcdiv(bcmul((string)sys_config('district_award'), (string)$order_price, 2), 100, 2);
|
|
|
+ if ($award > 0) {
|
|
|
+ $brokeragePrice = bcdiv($award, (string)count($admins), 2);
|
|
|
+ if ($brokeragePrice > 0) {
|
|
|
+ foreach ($admins as $v) {
|
|
|
+ $spreadPrice = (string)$v['brokerage_price'];
|
|
|
+ // 上级推广员返佣之后的金额
|
|
|
+ $balance = bcadd($spreadPrice, $brokeragePrice, 2);
|
|
|
+ // 添加佣金记录
|
|
|
+ /** @var UserBrokerageServices $userBrokerageServices */
|
|
|
+ $userBrokerageServices = app()->make(UserBrokerageServices::class);
|
|
|
+ $type = 'get_area_brokerage';
|
|
|
+ $res1 = $userBrokerageServices->income($type, $v['uid'], [
|
|
|
+ 'pay_price' => floatval($order_price),
|
|
|
+ 'area' => $value['p'] . $value['c'] . $value['d'] . '级',
|
|
|
+ 'number' => floatval($brokeragePrice),
|
|
|
+ 'frozen_time' => $frozen_time
|
|
|
+ ], $balance, 0);
|
|
|
+ // 添加用户佣金
|
|
|
+ $res2 = $userServices->bcInc($v['uid'], 'brokerage_price', $brokeragePrice, 'uid');
|
|
|
+ $res = $res1 && $res2 && $res;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $res
|
|
|
+ && $storeOrderServices->search()->where('send_area_award', 0)->update(['send_area_award' => 1])
|
|
|
+ && SystemConfig::where('menu_name', 'area_award_last_time')->update(['value' => time()]);
|
|
|
+ }
|
|
|
}
|