jh-admin/addon/dividemoney/model/DividemoneyAccount.php

394 lines
14 KiB
PHP

<?php
namespace addon\dividemoney\model;
use addon\aliapp\model\MinCode;
use addon\cypay\model\Pay as cyPayModel;
use app\model\DbModel;
use addon\wechatpay\model\Pay as PayModel;
class DividemoneyAccount extends DbModel
{
protected $autoWriteTimestamp = 'int';
public function getDividemoneyAccountPageList($condition, $page = 1, $page_size = PAGE_LIST_ROWS, $field = '*', $order = 'create_time desc')
{
$res = model('dividemoney_account')->pageList($condition, $field, $order, $page, $page_size);
return $this->success($res);
}
public function getDividemoneyAccountInfo($condition, $field = '*')
{
$res = model('dividemoney_account')->getInfo($condition, $field);
return $this->success($res);
}
/***
* 绑定支付宝账号
* @param $data
* @return void
*/
public function BingAlipayAccount($site_id, $data, $type = 'userId')
{
$receiver_list['out_request_no'] = date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
$receiver_list['receiver_list'][] = [
'type' => $type,
'account' => $data['account'],
'name' => $data['realname'],
];
$micode = new MinCode($site_id);
$res = $micode->requestApi('alipay.trade.royalty.relation.bind', $receiver_list)['alipay_trade_royalty_relation_bind_response'];
if ($res['code'] != '10000') {
return $this->error('', $res['sub_msg']);
} else {
$_where = [
['site_id', '=', $site_id],
['is_platform', '=', 0],
['account', '=', $data['account']]
];
$arr = $this->where($_where)->find();
if ($arr) {
$data['site_id'] = $site_id;
$data['account_type'] = 'userId';
$data['is_platform'] = 0;
$data['states'] = 1;
$arr = model('dividemoney_account')->update($data, $_where);
} else {
$data['site_id'] = $site_id;
$data['account_type'] = 'userId';
$data['is_platform'] = 0;
$data['states'] = 1;
$data['is_system'] = 0;
$arr = model('dividemoney_account')->add($data);
}
return $this->success($arr);
}
}
/***
* 绑定微信账号
* @param $data
* @return void
*/
public function BingWechatAccount($site_id, $data, $relation_type = 'PARTNER')
{
$PayModel = new PayModel(1, $site_id, 'v3');
$model = $PayModel->getApp();
$config = $PayModel->getPayConfig();
$BodyData = [
'appid' => $config['app_id'],
'type' => $data['account_type'],
'account' => $data['account'],
'name' => $model->encryptor($data['realname']),
'relation_type' => $relation_type,
];
if ($config['is_isp']) {
$BodyData['sub_appid'] = $config['sub_appid'];
$BodyData['sub_mchid'] = $config['sub_mchid'];
}
try {
$res = $model->requestApi('/v3/profitsharing/receivers/add', $BodyData);
if ($res['code'] != 0) {
return $this->error('', $res['message']);
} else {
$_where = [
['site_id', '=', $site_id],
['is_platform', '=', 0],
['account', '=', $data['account']]
];
$arr = $this->where($_where)->find();
if ($arr) {
$data['site_id'] = $site_id;
$data['is_platform'] = 0;
$data['states'] = 1;
model('dividemoney_account')->update($data, $_where);
} else {
$data['site_id'] = $site_id;
$data['is_platform'] = 0;
$data['states'] = 1;
$data['is_system'] = 0;
model('dividemoney_account')->add($data);
}
return $this->success();
}
} catch (\Exception $e) {
return $this->error('', $e->getMessage());
}
}
/***
* 畅捷分账绑定
* @param $site_id
* @param $data
* @param $relation_type
* @return array
*/
public function BingCyPayAccount($site_id, $data, $relation_type = 'PARTNER')
{
$PayModel = new cyPayModel($site_id);
$out_request_no = date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
$receiver_list = [
[
'receiver_name' => $data['realname'],
'receiver_account' => $data['account'],
]
];
try {
$res = $PayModel->relationBindAccounts($out_request_no, $receiver_list);
if ($res['code'] != 0) {
return $this->error('', $res['message']);
} else {
$_where = [
['site_id', '=', $site_id],
['account', '=', $data['account']]
];
$arr = $this->where($_where)->find();
if ($arr) {
$data['site_id'] = $site_id;
$data['states'] = 1;
$data['refuse'] = $out_request_no;
model('dividemoney_account')->update($data, $_where);
} else {
$data['refuse'] = $out_request_no;
$data['site_id'] = $site_id;
$data['states'] = 1;
model('dividemoney_account')->add($data);
}
return $this->success();
}
} catch (\Exception $e) {
return $this->error('', $e->getMessage());
}
}
/***
* 绑定到会员
* @param $site_id
* @param $data
* @param $channel_type
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function BingMemberAccount($site_id, $data, $channel_type = 'member')
{
$_where = [
['site_id', '=', $site_id],
['account', '=', $data['member_id']],
['account_type', '=', 'balance']
];
$id = $data['id'] ?? 0;
if (empty($data['reason'])) {
$data['reason'] = '商家自定义分账';
}
if ($id == 0) {
$data['reason'] .= $data['account'];
} else {
$_where = [
['id', '=', $id],
['site_id', '=', $site_id],
];
}
$data['account_type'] = 'balance';
$data['account'] = $data['member_id'];
$data['update_time'] = time();
$arr = $this->where($_where)->find();
if ($arr) {
$data['site_id'] = $site_id;
$data['states'] = 1;
$res = model('dividemoney_account')->update($data, $_where);
} else {
$data['site_id'] = $site_id;
$data['states'] = 1;
$data['create_time'] = time();
$data['update_time'] = time();
$res = model('dividemoney_account')->add($data);
}
return $this->success($res, '绑定成功');
}
/***
* 计算分账佣金
* @param $param
* @return array|void
*/
public function getBingAccountList($param = [])
{
if (isset($param['site_id']) && isset($param['pay_type'])) {
$site_id = $param['site_id'];
$pay_type = $param['pay_type'];
$where = [
['site_id', '=', $site_id],
['is_platform', '=', 0],
['states', '=', 1],
['bonus_type', '<>', 'store'],
];
$Account = model('dividemoney_account')->getList(array_merge($where, [['channel_type', '=', 'member']]), '*', 'create_time desc');
if ($pay_type == 'wechatpay') {
$where[] = ['channel_type', 'in', ['wechat', 'weapp', 'wechatpay']];
} else if ($pay_type == 'alipay') {
$where[] = ['channel_type', 'in', ['alipay', 'aliapp']];
} else if (in_array($pay_type, ['cywechatpay', 'cyalipay'])) {
$where[] = ['channel_type', '=', 'cypay'];
} else if ($Account) {
$where[] = ['id', 'notin', array_column($Account, 'id')];
}
$AccountPay = model('dividemoney_account')->getList($where, '*', 'create_time desc');
$Account = array_merge($Account, $AccountPay);
if ($Account) {
$config_model = new Config();
$info = $config_model->getConfig($param['site_id']);
$is_divide = $info['data']['value']['is_divide'];
$accounts = [];
foreach ($Account as $k => $v) {
$pay_money = $param['pay_money'];
if ($v['account_type'] == 'balance') {
$pay_money += $param['balance_money'];
}
if ($v['commission_type'] == 'cost') {
$cost_money = model('order_goods')->getColumn(['order_id' => $param['order_id'], 'site_id' => $param['site_id']], 'cost_price,price', 'order_goods_id');
$pay_money = 0;
foreach ($cost_money as $k1 => $item) {
if ($item['cost_price'] <= 0 || $item['price'] <= $item['cost_price']) {
$pay_money += $item['price'];
} else if ($item['cost_price'] / $item['price'] < 0.2) {
$pay_money += $item['price'] * 0.2;
} else {
$pay_money += $item['cost_price'];
}
}
}
if ($v['is_system']) {
$accounts[] = $this->getMoneyArr($param, $v, $pay_money, $v['divide_rate'], 3, '营运佣金');
} else if ($is_divide == 1) {
$accounts[] = $this->getMoneyArr($param, $v, $pay_money, $v['divide_rate'], $v['is_platform'], $v['reason'] ?: '商家自定义分账');
}
}
return $accounts;
}
}
}
/***
* 整理数据
* @param $data
* @param $ag_site_id代理商
* @param $price 产品价格
* @param $fee_commission 费率
* @param $reason 备注
* @return array
*/
private function getMoneyArr($data, $accInfo, $price, $fee_commission, $platform_type = 2, $reason = '服务商服务费')
{
if ($accInfo['commission_type'] == 'fixfee') {
$money = $fee_commission;
} else {
$money = $price * $fee_commission / 100;
if ($money < 0.01) {
$money = 0.01;
}
}
//判断是否按产品抽佣
$acc = [
'site_id' => $accInfo['site_id'],
'ag_site_id' => $accInfo['ag_site_id'],
'is_platform' => $platform_type,
'order_id' => $data['order_id'],
'member_id' => $data['member_id'],
'out_trade_no' => $data['out_trade_no'],
'trade_no' => $data['trade_no'] ?? '', //订单号
'is_video_number' => $data['is_video_number'] ?: 0,
'fee_commission' => $fee_commission,
'pay_money' => $price,//支付金额,
'amount' => $money,
'realname' => $accInfo['realname'],
'account' => $accInfo['account'],
'account_type' => $accInfo['account_type'],
'channel_type' => $accInfo['channel_type'],
'promotion_type' => $data['promotion_type'],
'reason' => $reason
];
if ($money <= 0 || !$acc['account'] || !$acc['realname']) { //未设置账号不分佣
$acc = [];
}
return $acc;
}
/***
* 获取绑定对应商户
* @param $site_id
* @param $pay_type
* @return array
*/
public function getBingAccount($site_id, $pay_type = '')
{
$where = [
['site_id', '=', $site_id],
['is_platform', '=', 0],
['states', '=', 1],
];
if ($pay_type == 'wechatpay') {
$where[] = ['channel_type', 'in', ['wechat', 'weapp', 'wechatpay']];
} else if ($pay_type == 'alipay') {
$where[] = ['channel_type', 'in', ['alipay', 'aliapp']];
} else {
return [];
}
$res = model('dividemoney_account')->getList($where, '*', 'create_time desc');
if ($res) {
$accounts = [];
foreach ($res as $k => $v) {
if ($pay_type == 'wechatpay') {
$alipay_account_name = '';
$settlement_alipay_account = '';
$wecaht_account_name = $v['realname'];
$settlement_wecaht_mch_id = $v['account'];
} else if ($pay_type == 'alipay') {
$alipay_account_name = $v['realname'];
$settlement_alipay_account = $v['account'];
$wecaht_account_name = '';
$settlement_wecaht_mch_id = '';
}
$accounts[] = [
'is_commission' => 1,
'realname' => $v['realname'],
'mobile' => '',
'alipay_account_name' => $alipay_account_name,
'settlement_alipay_account' => $settlement_alipay_account,
'wecaht_account_name' => $wecaht_account_name,
'settlement_wecaht_mch_id' => $settlement_wecaht_mch_id,
'fee_commission' => $v['divide_rate'],
'is_platform' => 0,
'reason' => $v['reason']
];
}
return $accounts;
} else {
return [];
}
}
public function getDivideList($where, $field = '*', $order = 'id desc')
{
$list = model('dividemoney_account')->getList($where, $field, $order);
return $list;
}
/***
* 删除
* @param $condition
* @return int
*/
public function deleteDividemoneyAccount($condition)
{
$res = model('dividemoney_account')->delete($condition);
return $this->success($res);
}
}