291 lines
13 KiB
PHP
291 lines
13 KiB
PHP
<?php
|
|
|
|
class ReceiverOrderSubsidyEntity
|
|
{
|
|
public $id;
|
|
public $orderId;
|
|
public $userId;
|
|
public $cityId;
|
|
public $bizId;
|
|
public $brandId;
|
|
public $seriesId;
|
|
public $billImg;
|
|
public $businessImg;
|
|
public $ifcheck;
|
|
public $checkTime;
|
|
public $status;
|
|
public $couponId;
|
|
|
|
public function __construct()
|
|
{
|
|
$ci = &get_instance();
|
|
$ci->load->helper('string');
|
|
$ci->load->model('agent/auto_product_coupon_model');
|
|
$ci->load->model('agent/receiver_order_subsidy_model');
|
|
$ci->load->model('agent/auto_product_model');
|
|
$ci->load->model('agent/pingan/pingan_users_cmmssn_model');
|
|
$ci->load->model('agent/pingan/pingan_users_model');
|
|
$ci->load->model('agent/organization/organization_cmmssn_model', 'mdOrganizationCmmssn');
|
|
$ci->load->model('agent/organization/organization_model');
|
|
$ci->load->model('receiver/order/receiver_orders_model');
|
|
$ci->load->model('receiver/receiver_clues_model');
|
|
$ci->load->model('biz/biz_account_model', 'account_model');
|
|
$ci->load->model('biz/biz_accountLog_model', 'accountlog_model');
|
|
}
|
|
|
|
/**
|
|
* 补贴审核
|
|
* @param $status
|
|
* @param $reason
|
|
* @return MyResponse
|
|
*/
|
|
public function ifCheckSubsidy($status, $reason = '')
|
|
{
|
|
$ci = &get_instance();
|
|
$ci->receiver_orders_model->set_db('ssdb');
|
|
$ci->receiver_clues_model->set_db('ssdb');
|
|
$ci->account_model->set_db('ssdb');
|
|
$ci->accountlog_model->set_db('ssdb');
|
|
$ci->receiver_order_subsidy_model->db->trans_begin();
|
|
$ci->account_model->db->trans_begin();
|
|
try {
|
|
$data = [
|
|
'ifcheck' => $status ? Receiver_order_subsidy_model::IF_CHECK_YES : Receiver_order_subsidy_model::IF_CHECK_NO_PASS,
|
|
'checkTime' => date('Y-m-d H:i:s')
|
|
];
|
|
if ($data['ifcheck'] == Receiver_order_subsidy_model::IF_CHECK_NO_PASS && !$reason) {
|
|
throw new Exception("请输入审核未通过原因");
|
|
}
|
|
$reason && $data['reason'] = $reason;
|
|
if ($data['ifcheck'] == Receiver_order_subsidy_model::IF_CHECK_YES) { //判断订单是否完成
|
|
$order = $ci->receiver_orders_model->get(['id' => $this->orderId]);
|
|
if ($order['status'] != 3) {
|
|
throw new Exception("门店未确认");
|
|
}
|
|
}
|
|
$result = $ci->receiver_order_subsidy_model->update($data, ['id' => $this->id]);
|
|
if (!$result) {
|
|
throw new Exception("保存失败");
|
|
}
|
|
if ($data['ifcheck'] == Receiver_order_subsidy_model::IF_CHECK_YES) { //审核通过扣除商家CPS金额
|
|
$result = $this->productCps();
|
|
if (!$result->isSuccess()) {
|
|
throw new Exception("扣除商家金额失败");
|
|
}
|
|
}
|
|
$ci->receiver_order_subsidy_model->db->trans_commit();
|
|
$ci->account_model->db->trans_commit();
|
|
return new MyResponse(EXIT_SUCCESS, '保存成功');
|
|
} catch (Exception $e) {
|
|
$ci->receiver_order_subsidy_model->db->trans_rollback();
|
|
$ci->account_model->db->trans_rollback();
|
|
return new MyResponse(EXIT_ERROR, $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return MyResponse
|
|
*/
|
|
public function productCps()
|
|
{
|
|
$log_path = "product_biz_cps.log";
|
|
$ci = &get_instance();
|
|
$ci->load->library('bizAccount');
|
|
try {
|
|
$productCoupon = $ci->auto_product_coupon_model->get(['id' => $this->couponId]);
|
|
$product = $ci->auto_product_model->get(['id' => $productCoupon['product_id']]);
|
|
if (!$product) {
|
|
throw new Exception("产品不存在");
|
|
}
|
|
$money = $product['cpsMoney'];
|
|
if ($money <= 0) {
|
|
debug_log("产品CPS金额:" . $money, $log_path);
|
|
return new MyResponse(EXIT_SUCCESS, '保存成功');
|
|
}
|
|
$bizAccount = new BizAccount();
|
|
$account = $bizAccount->getAccountBizId($this->bizId, true);
|
|
$leftMoney = $account['money_left'] - $money;
|
|
$upData = [
|
|
"money_left = money_left-$money" => null
|
|
];
|
|
$where = [
|
|
"id" => $account['id']
|
|
];
|
|
$upAccount = $ci->account_model->update($upData, $where);
|
|
if (!(!is_bool($upAccount) && $upAccount)) {
|
|
debug_log("扣除金额失败" . $ci->account_model->db->last_query(), $log_path);
|
|
throw new Exception('扣除金额失败');
|
|
}
|
|
$ck = md5(time() . random_string() . $this->bizId
|
|
);
|
|
$logData = [
|
|
'account_id' => $account['id'],
|
|
'trade_type' => BizAccount::TRADE_TYPE_USE,
|
|
'money_type' => BizAccount::MONEY_TYPE_ORDER,
|
|
'money_out' => $money,
|
|
'money_left' => $leftMoney,
|
|
'ck' => $ck,
|
|
'descrip' => '订单完成',
|
|
'c_time' => time(),
|
|
'jsondata' => json_encode([]),
|
|
'target_id' => $product['id'],
|
|
'comm_status' => Biz_accountLog_model::COMM_STATUS_NOT,
|
|
];
|
|
$ret = $ci->accountlog_model->add($logData);
|
|
if (!$ret) {
|
|
throw new Exception('写入交易日志失败');
|
|
}
|
|
debug_log("扣除商家金额成功", $log_path);
|
|
return new MyResponse(EXIT_SUCCESS, '保存成功');
|
|
} catch (Exception $e) {
|
|
debug_log($e->getMessage(), $log_path);
|
|
return new MyResponse(EXIT_ERROR, $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 分佣
|
|
* @return MyResponse
|
|
*/
|
|
public function commission()
|
|
{
|
|
$log_path = 'commission.log';
|
|
$log_dir = 'order_subsidy';
|
|
$ci = &get_instance();
|
|
try {
|
|
debug_log("开始分佣:" . $this->id, $log_path, $log_dir);
|
|
$cmmssn = $ci->pingan_users_cmmssn_model->get(['cfId' => $this->orderId, 'cfType' => Pingan_users_cmmssn_model::CF_TYPE_ORDER_OWN]);
|
|
if ($cmmssn) {
|
|
throw new Exception("订单已经存在分佣记录");
|
|
}
|
|
$productCoupon = $ci->auto_product_coupon_model->get(['id' => $this->couponId]);
|
|
$product = $ci->auto_product_model->get(['id' => $productCoupon['product_id']]);
|
|
if (!$product) {
|
|
throw new Exception("产品不存在");
|
|
}
|
|
$money = $product['orderCommission'];
|
|
debug_log("订单分佣金额:" . $money, $log_path, $log_dir);
|
|
if ($money <= 0) { //不需要分佣
|
|
$ci->receiver_order_subsidy_model->update(['commStatus' => Receiver_order_subsidy_model::COMM_STATUS_NOT], ['id' => $this->id]);
|
|
return new MyResponse(EXIT_SUCCESS, "不需要分佣,分佣金额为:{$money}");
|
|
}
|
|
$order = $ci->receiver_orders_model->get(['id' => $this->orderId]);
|
|
if (!$order) {
|
|
throw new Exception("订单不存在");
|
|
}
|
|
$clues = $ci->receiver_clues_model->get(['id' => $order['clue_id'], 'cf_id' => 38]);
|
|
if (!$clues) {
|
|
throw new Exception("线索不存在");
|
|
}
|
|
if (!$clues['out_id']) {
|
|
throw new Exception("报名来源不存在");
|
|
}
|
|
$sourceId = $clues['out_id'];
|
|
$pinanUser = $ci->pingan_users_model->get(['id' => $clues['pingan_user_id']]);
|
|
if (!$pinanUser['id']) {
|
|
throw new Exception("线索不存在绑定用户关系");
|
|
}
|
|
//获取分佣配置
|
|
$cmmssn = $ci->mdOrganizationCmmssn->get(['teamId' => $pinanUser['orgTeamId'], 'status' => 0]);
|
|
if (!$cmmssn) {
|
|
throw new Exception("分佣配置不存在");
|
|
}
|
|
//团队设置分佣等级
|
|
$level = $ci->organization_model->getTeamLevel($pinanUser['orgTeamId']);
|
|
$brokerage1 = $money * ($cmmssn['orderLevel1'] / 100) ?: 0;
|
|
$brokerage2 = $money * ($cmmssn['orderLevel2'] / 100) ?: 0;
|
|
$brokerage3 = $money * ($cmmssn['orderLevel3'] / 100) ?: 0;
|
|
$brokerage4 = $money * ($cmmssn['orderLevel4'] / 100) ?: 0;
|
|
debug_log('分佣金额:' . json_encode([$brokerage1, $brokerage2, $brokerage3, $brokerage4]), $log_path, $log_dir);
|
|
$add_data = [];
|
|
if ($brokerage1 && $clues['area_id']) { //一级佣金
|
|
$add_data[] = [
|
|
'cfId' => $this->orderId,
|
|
'cfType' => Pingan_users_cmmssn_model::CF_TYPE_ORDER_TEAM,
|
|
'money' => $brokerage1,
|
|
'pinganUserId' => $clues['area_id'],
|
|
'teamId' => 0,
|
|
'depId' => 0,
|
|
'areaId' => $clues['area_id'],
|
|
'createTime' => date('Y-m-d H:i:s'),
|
|
'bizId' => $this->bizId,
|
|
'sourceId' => $sourceId
|
|
];
|
|
}
|
|
if ($brokerage2 && $clues['dep_id']) { //二级佣金
|
|
$add_data[] = [
|
|
'cfId' => $this->orderId,
|
|
'cfType' => Pingan_users_cmmssn_model::CF_TYPE_ORDER_TEAM,
|
|
'money' => $brokerage2,
|
|
'pinganUserId' => $clues['dep_id'],
|
|
'teamId' => 0,
|
|
'depId' => $clues['dep_id'],
|
|
'areaId' => $clues['area_id'] ?: 0,
|
|
'createTime' => date('Y-m-d H:i:s'),
|
|
'bizId' => $this->bizId,
|
|
'sourceId' => $sourceId
|
|
];
|
|
}
|
|
if ($level >= 4) { //4级
|
|
if ($brokerage3 && $clues['team_id']) {
|
|
$add_data[] = [
|
|
'cfId' => $this->orderId,
|
|
'cfType' => Pingan_users_cmmssn_model::CF_TYPE_ORDER_TEAM,
|
|
'money' => $brokerage3,
|
|
'pinganUserId' => $clues['team_id'],
|
|
'teamId' => $clues['team_id'],
|
|
'depId' => $clues['dep_id'],
|
|
'areaId' => $clues['area_id'] ?: 0,
|
|
'createTime' => date('Y-m-d H:i:s'),
|
|
'bizId' => $this->bizId,
|
|
'sourceId' => $sourceId
|
|
];
|
|
}
|
|
if ($brokerage4 && $clues['pingan_user_id']) {
|
|
$add_data[] = [
|
|
'cfId' => $this->orderId,
|
|
'cfType' => Pingan_users_cmmssn_model::CF_TYPE_ORDER_OWN,
|
|
'money' => $brokerage4,
|
|
'pinganUserId' => $clues['pingan_user_id'],
|
|
'teamId' => $clues['team_id'],
|
|
'depId' => $clues['dep_id'],
|
|
'areaId' => $clues['area_id'] ?: 0,
|
|
'createTime' => date('Y-m-d H:i:s'),
|
|
'bizId' => $this->bizId,
|
|
'sourceId' => $sourceId
|
|
];
|
|
}
|
|
} else { //三级
|
|
if ($brokerage3 && $clues['pingan_user_id']) {
|
|
$add_data[] = [
|
|
'cfId' => $this->orderId,
|
|
'cfType' => Pingan_users_cmmssn_model::CF_TYPE_ORDER_OWN,
|
|
'money' => $brokerage3,
|
|
'pinganUserId' => $clues['team_id'],
|
|
'teamId' => 0,
|
|
'depId' => $clues['dep_id'],
|
|
'areaId' => $clues['area_id'] ?: 0,
|
|
'createTime' => date('Y-m-d H:i:s'),
|
|
'bizId' => $this->bizId,
|
|
'sourceId' => $sourceId
|
|
];
|
|
}
|
|
}
|
|
debug_log('分佣数据:' . json_encode($add_data, JSON_UNESCAPED_UNICODE), $log_path, $log_dir);
|
|
if (count($add_data)) {
|
|
$result = $ci->pingan_users_cmmssn_model->add_batch($add_data);
|
|
if (!$result) {
|
|
throw new Exception("保存失败");
|
|
}
|
|
}
|
|
$ci->receiver_order_subsidy_model->update(['commStatus' => Receiver_order_subsidy_model::COMM_STATUS_SUCCESS], ['id' => $this->id]);
|
|
debug_log('分佣结束', $log_path, $log_dir);
|
|
return new MyResponse(EXIT_SUCCESS, '保存成功');
|
|
} catch (Exception $e) {
|
|
$ci->receiver_order_subsidy_model->update(['commStatus' => Receiver_order_subsidy_model::COMM_STATUS_FAIL], ['id' => $this->id]);
|
|
debug_log($e->getMessage(), $log_path, $log_dir);
|
|
return new MyResponse(EXIT_ERROR, $e->getMessage());
|
|
}
|
|
|
|
}
|
|
} |