diff --git a/agent/admin/controllers/auto/Ucenter.php b/agent/admin/controllers/auto/Ucenter.php index 9adfc546..f7bbe5e3 100644 --- a/agent/admin/controllers/auto/Ucenter.php +++ b/agent/admin/controllers/auto/Ucenter.php @@ -198,8 +198,18 @@ class Ucenter extends BaseController { $id = $this->get('id'); $userCoupon = $this->auto_user_coupon_model->get(['id' => $id, 'userId' => $this->userId]); + $where = ['cf_uid' => $this->userId, 'app_id' => Receiver_clues_model::APP_ID_ACTIVITY, 'out_id' => $userCoupon['productId']]; + $cluesRow = $this->clues_model->get($where); + $cityRow = $this->area_model->get(['city_id' => $cluesRow['city_id']]); + $brand = $this->auto_brand_model->get(['id' => $cluesRow['brand_id']], 'name'); + $series = $this->auto_series_model->get(['id' => $cluesRow['series_id']], 'name'); $data = [ - 'mobile' => mobile_asterisk($userCoupon['mobile']) + 'mobile' => mobile_asterisk($userCoupon['mobile']), + 'city' => $cityRow['province_name'] . '/' . $cityRow['city_name'], + 'cityId' => $cluesRow['city_id'] ?: '', + 'brandId' => $cluesRow['brand_id'] ?: '', + 'seriesId' => $cluesRow['series_id'] ?: '', + 'model' => "{$brand['name']} {$series['name']}" ]; $this->return_response($data); } diff --git a/agent/admin/controllers/pingan/receiver/Clues.php b/agent/admin/controllers/pingan/receiver/Clues.php index 407eac7b..9aabc124 100644 --- a/agent/admin/controllers/pingan/receiver/Clues.php +++ b/agent/admin/controllers/pingan/receiver/Clues.php @@ -116,8 +116,11 @@ class Clues extends BaseController $cfrom2 .= '-' . $product['title']; } }; + $dataInfo['cfrom'] = $cfrom2; $dataInfo['buyTimeList'] = Receiver_clues_model::BUY_TIME_LIST; + $users = $this->pingan_users_model->get(['id' => $re['pingan_user_id']], 'id,userCode,username,centerNumber,orgName'); + $dataInfo['belongUser'] = $users ?: []; $this->return_response($dataInfo); } @@ -243,7 +246,7 @@ class Clues extends BaseController $areaUserIdsString = implode(',', $areaUserIds); !$areaUserIdsString && $areaUserIdsString = 0; $where["area_id in ({$areaUserIdsString})"] = null; - }elseif($_SESSION['groupType'] == Pingan_users_model::GROUP_TYPE_CENTER_CONTACT){ //中心对接人 + } elseif ($_SESSION['groupType'] == Pingan_users_model::GROUP_TYPE_CENTER_CONTACT) { //中心对接人 $centerNumber = $_SESSION['centerNumber']; $areaUserIds = $this->pingan_users_model->select(['centerNumber' => $centerNumber, 'groupType' => Pingan_users_model::GROUP_TYPE_AREA], '', 0, 0, 'id'); $areaUserIdsString = implode(',', $areaUserIds); diff --git a/api/controllers/Welcome.php b/api/controllers/Welcome.php index 7f088f7c..e12b1d7d 100644 --- a/api/controllers/Welcome.php +++ b/api/controllers/Welcome.php @@ -13,98 +13,10 @@ class Welcome extends CI_Controller public function test() { - $this->load->model('biz/biz_account_model', 'account_model'); - $this->load->model('biz/biz_accountLog_model', 'accountlog_model'); - $this->load->library('bizAccount'); - $this->load->helper('string'); - $uid = 1; - $bizId = 10; - $brokerage1 = 10; - $brokerage2 = 20; - $brokerage3 = 30; - $brokerage4 = 40; - $totalMoney = $brokerage1 + $brokerage2 + $brokerage3 + $brokerage4; - $ck = md5(time() . random_string() . $uid); // 16 字节 = 32 字符 - try { - $bizAccount = new BizAccount(); - $account = $bizAccount->getAccountBizId($bizId, true); - if (!$account) { - throw new Exception('账号不存在', 0); - } - $leftMoney = $account['money_left']; - if ($leftMoney < $totalMoney) { - throw new Exception('余额不足', 0); - } - $this->account_model->db->trans_begin(); - $upData = [ - "money_left = money_left-$totalMoney" => null - ]; - $where = [ - "money_left >= $totalMoney" => null - ]; - $upAccount = $this->account_model->update($upData, $where); - if (!(!is_bool($upAccount) && $upAccount)) { -// debug_log("[error]# ". $this->coupon_model->db->last_query(), __FUNCTION__, $this->log_dir); - throw new Exception('余额不足', 0); - } - $logData = []; - if ($brokerage1 > 0) { - $leftMoney = $leftMoney - $brokerage1; - $logData[] = [ - 'account_id' => $account['id'], - 'trade_type' => BizAccount::TRADE_TYPE_USE, - 'money_out' => $brokerage1, - 'money_left' => $leftMoney, - 'ck' => $ck, - 'descrip' => '订单支付', - 'c_time' => time(), - ]; - } - if ($brokerage2 > 0) { - $leftMoney = $leftMoney - $brokerage2; - $logData[] = [ - 'account_id' => $account['id'], - 'trade_type' => BizAccount::TRADE_TYPE_USE, - 'money_out' => $brokerage2, - 'money_left' => $leftMoney, - 'ck' => $ck, - 'descrip' => '订单支付', - 'c_time' => time(), - ]; - } - if ($brokerage3 > 0) { - $leftMoney = $leftMoney - $brokerage3; - $logData[] = [ - 'account_id' => $account['id'], - 'trade_type' => BizAccount::TRADE_TYPE_USE, - 'money_out' => $brokerage3, - 'money_left' => $leftMoney, - 'ck' => $ck, - 'descrip' => '订单支付', - 'c_time' => time(), - ]; - } - if ($brokerage4 > 0) { - $leftMoney = $leftMoney - $brokerage4; - $logData[] = [ - 'account_id' => $account['id'], - 'trade_type' => BizAccount::TRADE_TYPE_USE, - 'money_out' => $brokerage4, - 'money_left' => $leftMoney, - 'ck' => $ck, - 'descrip' => '订单支付', - 'c_time' => time(), - ]; - } - $ret = $this->accountlog_model->add_batch($logData); - if (!$ret) { - throw new Exception('写入日志失败', 0); - } - $this->account_model->db->trans_commit(); - print_r('保存成功'); - } catch (Exception $e) { - $this->account_model->db->trans_rollback(); - print_r($e->getMessage()); - } + $this->load->model('receiver/receiver_customers_model', 'customers_model'); + $rid = 17418; + $bizId = 57; + $res = $this->customers_model->Commissions($rid, $bizId); + var_dump($res); } } diff --git a/common/models/receiver/Receiver_clues_model.php b/common/models/receiver/Receiver_clues_model.php index f27ac4b9..b33ea271 100644 --- a/common/models/receiver/Receiver_clues_model.php +++ b/common/models/receiver/Receiver_clues_model.php @@ -12,6 +12,7 @@ class Receiver_clues_model extends HD_Model { private $table_name = 'lc_receiver_clues'; const APP_ID_ACTIVITY = 1; //来源活动 + const CF_ID2_PRODUCT = 40; // cf_id2 来源产品 //意向购车周期 const BUY_TIME_LIST = [1 => '3天内', 2 => '一周内', 3 => '15天内', 4 => '1个月内', 5 => '超过1个月']; diff --git a/common/models/receiver/Receiver_customers_model.php b/common/models/receiver/Receiver_customers_model.php index 1b689955..d2cda9b1 100644 --- a/common/models/receiver/Receiver_customers_model.php +++ b/common/models/receiver/Receiver_customers_model.php @@ -349,7 +349,9 @@ class Receiver_customers_model extends HD_Model throw new Exception('更新优惠券状态失败'); } //线索分佣 - $res = $this->Commissions($row['rid'], $bizId); +// $res = $this->Commissions($row['rid'], $bizId); + //扣除门店金额 + $res = $this->deductBizMoney($row['rid'], $bizId); if (!$res->isSuccess()) { throw new Exception($res->getMessage()); } @@ -364,6 +366,78 @@ class Receiver_customers_model extends HD_Model } } + public function deductBizMoney($cluesId, $bizId) + { + $this->load->helper('string'); + $this->load->model('biz/biz_account_model', 'account_model'); + $this->load->model('biz/biz_accountLog_model', 'accountlog_model'); + $this->load->library('bizAccount'); + $this->load->model('receiver/receiver_clues_model'); + $this->load->model('agent/auto_product_model'); + $this->auto_product_model->set_db('agentdb'); + $log_path = 'deductBizMoney.log'; + $log_dir = 'clues'; + try { + debug_log("开始扣除商家金额:线索id:{$cluesId}_门店id:{$bizId}", $log_path, $log_dir); + $clues = $this->receiver_clues_model->get(['id' => $cluesId, 'app_id' => Receiver_clues_model::APP_ID_ACTIVITY]); + if (!$clues) { + throw new Exception("线索不存在"); + } + if (!$clues['out_id']) { + throw new Exception("报名来源不存在"); + } + $bizMoney = 0; //商家扣除金额 + if ($clues['cf2_id'] == Receiver_clues_model::CF_ID2_PRODUCT) { //产品来源 + $product = $this->auto_product_model->get(['id' => $clues['out_id']]); + $bizMoney = $product['cluesCommission']; + } + if ($bizMoney <= 0) { + debug_log("扣除金额结束:扣除金额不能小于等于0,{$bizMoney}", $log_path, $log_dir); + return new MyResponse(EXIT_SUCCESS, '保存成功'); + } + $bizAccount = new BizAccount(); + $account = $bizAccount->getAccountBizId($bizId, true); + $leftMoney = $account['money_left']; + if ($leftMoney < $bizMoney) { + throw new Exception("余额不足"); + } + $upData = [ + "money_left = money_left-$bizMoney" => null + ]; + $where = [ + "money_left >= " => $bizMoney + ]; + $upAccount = $this->account_model->update($upData, $where); + if (!(!is_bool($upAccount) && $upAccount)) { + var_dump($this->account_model->db->last_query()); + debug_log("扣除金额失败" . $this->account_model->db->last_query(), $log_path, $log_dir); + throw new Exception('余额不足', 0); + } + $ck = md5(time() . random_string() . $bizId); + $logData = [ + 'account_id' => $account['id'], + 'trade_type' => BizAccount::TRADE_TYPE_USE, + 'money_type' => BizAccount::MONEY_TYPE_CLUES, + 'money_out' => $bizMoney, + 'money_left' => $leftMoney, + 'ck' => $ck, + 'descrip' => '解锁线索', + 'c_time' => time(), + 'jsondata' => json_encode(['clues_id' => $cluesId]), + 'target_id' => $cluesId + ]; + $ret = $this->accountlog_model->add($logData); + if (!$ret) { + throw new Exception('写入交易日志失败', 0); + } + debug_log("扣除金额结束:{$bizMoney}", $log_path, $log_dir); + return new MyResponse(EXIT_SUCCESS, '保存成功'); + } catch (Exception $e) { + debug_log($e->getMessage(), $log_path, $log_dir); + return new MyResponse(EXIT_ERROR, $e->getMessage()); + } + } + /** * 分佣 * @param $cluesId 线索id @@ -416,7 +490,7 @@ class Receiver_customers_model extends HD_Model $level = $this->organization_model->getTeamLevel($pinanUser['orgTeamId']); $brokerage1 = $brokerage2 = $brokerage3 = $brokerage4 = 0; $sourceId = 0; - if ($clues['cf2_id'] == 39) { //产品来源 + if ($clues['cf2_id'] == Receiver_clues_model::CF_ID2_PRODUCT) { //产品来源 $product = $this->auto_product_model->get(['id' => $clues['out_id']]); if ($product['cluesCommission'] <= 0) { debug_log("结束分佣:产品未配置线索总用金额", $log_path, $log_dir);