Files
spacestation/common/libraries/entity/ReceiverOrderSubsidyEntity.php
T
2025-08-01 09:11:36 +08:00

274 lines
12 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/auto_product_model');
$ci->load->model('agent/receiver_order_subsidy_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');
$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');
}
/**
* 补贴审核
* @param $status
* @param $reason
* @return MyResponse
*/
public function ifCheckSubsidy($status, $reason = '')
{
$ci = &get_instance();
$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());
}
}
public function cmmssn()
{
$ci = &get_instance();
try {
$log_path = 'cmmssn.log';
$log_dir = 'pingan';
debug_log("开始分佣:" . $this->id, $log_path, $log_dir);
$cmmssn = $ci->pingan_users_cmmssn_model->get(['orderId' => $this->orderId]);
if ($cmmssn) {
throw new Exception("订单已经存在分佣记录");
}
$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("报名来源不存在");
}
$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' => 1]);
if (!$cmmssn) {
throw new Exception("分佣配置不存在");
}
//团队设置分佣等级
$level = $ci->organization_model->getTeamLevel($pinanUser['orgTeamId']);
$brokerage1 = $brokerage2 = $brokerage3 = $brokerage4 = 0;
$sourceId = 0;
if ($clues['cf2_id'] == 39) { //产品来源
$product = $ci->auto_product_model->get(['id' => $clues['out_id']]);
$brokerage1 = $product['orderCommission'] * ($cmmssn['orderLevel1'] / 100) ?: 0;
$brokerage2 = $product['orderCommission'] * ($cmmssn['orderLevel2'] / 100) ?: 0;
$brokerage3 = $product['orderCommission'] * ($cmmssn['orderLevel3'] / 100) ?: 0;
$brokerage4 = $product['orderCommission'] * ($cmmssn['orderLevel4'] / 100) ?: 0;
$sourceId = $product['id'] ?: 0;
} elseif ($clues['cf2_id'] == 40) { //活动获取分佣配置
}
$add_data = [];
if ($brokerage1 && $clues['area_id']) { //一级佣金
$add_data[] = [
'cfId' => $this->orderId,
'sourceId' => $sourceId,
'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')
];
}
if ($brokerage2 && $clues['dep_id']) { //二级佣金
$add_data[] = [
'cfId' => $this->orderId,
'sourceId' => $sourceId,
'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')
];
}
if ($level >= 4) { //4级
if ($brokerage3 && $clues['team_id']) {
$add_data[] = [
'cfId' => $this->orderId,
'sourceId' => $sourceId,
'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')
];
}
if ($brokerage4 && $clues['pingan_user_id']) {
$add_data[] = [
'cfId' => $this->orderId,
'sourceId' => $sourceId,
'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')
];
}
} else { //三级
if ($brokerage3 && $clues['pingan_user_id']) {
$add_data[] = [
'cfId' => $this->orderId,
'sourceId' => $sourceId,
'cfType' => Pingan_users_cmmssn_model::CF_TYPE_ORDER_TEAM,
'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')
];
}
}
if (count($add_data)) {
$result = $ci->pingan_users_cmmssn_model->add_batch($add_data);
if (!$result) {
throw new Exception("保存失败");
}
}
return new MyResponse(EXIT_SUCCESS, '保存成功');
} catch (Exception $e) {
debug_log($e->getMessage(), $log_path, $log_dir);
return new MyResponse(EXIT_ERROR, $e->getMessage());
}
}
}