cxgj/models/Cash.php
2023-11-27 09:45:13 +08:00

389 lines
13 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\models;
use Yii;
use app\components\SysConst;
use app\models\Model;
/**
* This is the model class for table "{{%cash}}".
*
* @property int $id ID
* @property int $cx_mch_id 平台商户ID
* @property int $scene 场景类型
* @property int $user_id 用户ID
* @property float $money 金额
* @property float $service_charge 提现费用
* @property string $order_no 订单号
* @property int $status 状态0=待处理1=已同意2=已拒绝
* @property int $transfer_status 转账状态0=待转账1=已转账2=拒绝转账
* @property string|null $wechat_account 微信账号
* @property string|null $wechat_account_name 微信账号持有者姓名
* @property string|null $alipay_account 支付宝账号
* @property string|null $alipay_account_name 支付宝账号持有者姓名
* @property string|null $bank_name 银行名称
* @property string|null $bank_card_no 银行卡号
* @property string|null $bank_card_name 银行卡持卡人姓名
* @property int|null $cash_type 提现类型
* @property int|null $pay_type 转账类型
* @property int|null $pay_time 转账时间
* @property int|null $created_at 添加时间
* @property int|null $updated_at 更新时间
* @property int|null $deleted_at 删除时间
* @property int|null $is_delete 是否删除0=否1=是
* @property int|null $review_time 审核时间
* @property string|null $review_comment 审核意见
* @property string|null $remark 备注
* @property string|null $bank_card_phone 备注
* @property string|null $bank_card_bank 备注
*/
class Cash extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%cash}}';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['order_no', 'wechat_account', 'wechat_account_name', 'alipay_account', 'alipay_account_name', 'bank_name', 'bank_card_no', 'bank_card_name', 'review_comment', 'remark'], 'trim'],
[['cx_mch_id', 'scene', 'user_id', 'status', 'transfer_status', 'cash_type', 'pay_type', 'pay_time', 'created_at', 'updated_at', 'deleted_at', 'is_delete', 'review_time'], 'integer'],
[['user_id', 'order_no'], 'required'],
[['money', 'service_charge'], 'number'],
[['order_no'], 'string', 'max' => 64],
[['wechat_account', 'wechat_account_name', 'alipay_account', 'alipay_account_name', 'bank_name', 'bank_card_no', 'bank_card_name','bank_card_phone','bank_card_bank'], 'string', 'max' => 256],
[['review_comment', 'remark'], 'string', 'max' => 2048],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'cx_mch_id' => '平台商户ID',
'scene' => '场景类型',
'user_id' => '用户ID',
'money' => '金额',
'service_charge' => '提现费用',
'order_no' => '订单号',
'status' => '状态0=待处理1=已同意2=已拒绝',
'transfer_status' => '转账状态0=待转账1=已转账2=拒绝转账',
'wechat_account' => '微信账号',
'wechat_account_name' => '微信账号持有者姓名',
'alipay_account' => '支付宝账号',
'alipay_account_name' => '支付宝账号持有者姓名',
'bank_name' => '银行名称',
'bank_card_no' => '银行卡号',
'bank_card_name' => '银行卡持有者姓名',
'cash_type' => '提现类型',
'pay_type' => '转账类型',
'pay_time' => '转账时间',
'created_at' => '添加时间',
'updated_at' => '更新时间',
'deleted_at' => '删除时间',
'is_delete' => '是否删除0=否1=是',
'review_time' => '审核时间',
'review_comment' => '审核意见',
'remark' => '备注',
];
}
public function beforeSave($insert) {
if(parent::beforeSave($insert)){
if($this->isNewRecord){
$this->created_at = time();
}
$this->updated_at = time();
if($this->is_delete == 1)
$this->deleted_at = time();
$this->htmlTagFilter();
return true;
} else {
return false;
}
}
public function htmlTagFilter()
{
$this->wechat_account = Model::htmlTagFilter($this->wechat_account);
$this->wechat_account_name = Model::htmlTagFilter($this->wechat_account_name);
$this->alipay_account = Model::htmlTagFilter($this->alipay_account);
$this->alipay_account_name = Model::htmlTagFilter($this->alipay_account_name);
$this->bank_name = Model::htmlTagFilter($this->bank_name);
$this->bank_card_no = Model::htmlTagFilter($this->bank_card_no);
$this->bank_card_name = Model::htmlTagFilter($this->bank_card_name);
$this->review_comment = Model::htmlTagFilter($this->review_comment);
$this->remark = Model::htmlTagFilter($this->remark);
}
public static function getScene($scene)
{
$labels = self::getSceneLabels();
return isset($labels[$scene]) ? $labels[$scene] : "未知";
}
public static function getSceneLabels()
{
$labels = Balance::getBalanceSceneLabels();
foreach ($labels as $key => $val){
$labels[$key] = $val."提现";
}
return $labels;
}
public static function getStatus($status)
{
$labels = self::getStatusLabels();
return isset($labels[$status]) ? $labels[$status] : "未知";
}
public static function getStatusLabels()
{
return [
'0' => '待处理',
'1' => '已同意',
'2' => '已拒绝',
];
}
public static function getTransferStatus($status)
{
$labels = self::getTransferStatusLabels();
return isset($labels[$status]) ? $labels[$status] : "未知";
}
public static function getTransferStatusLabels()
{
return [
'0' => '待转账',
'1' => '已转账',
'2' => '拒绝转账',
];
}
public static $cxCashTypeAuto = 0; //自动打款
public static $cxCashTypeWx = 1; //提现到微信
public static $cxCashTypeAli = 2; //提现到支付宝
public static $cxCashTypeBank = 3; //提现到银行卡
public static function getCashTypeLabels()
{
return [
"0" => [
"type" => 0,
"name" => "自动打款",
"show" => true,
"disabled" => false,
],
"1" => [
"type" => 1,
"name" => "微信线下转账",
"show" => true,
"disabled" => false,
],
"2" => [
"type" => 2,
"name" => "支付宝线下转账",
"show" => true,
"disabled" => false,
],
"3" => [
"type" => 3,
"name" => "银行卡线下转账",
"show" => true,
"disabled" => false,
],
];
}
/**
* 提现类型信息
* @param array $cash_types 提现类型
*/
public static function getCashTypes($cash_types)
{
$labels = self::getCashTypeLabels();
foreach ($labels as $key => $val){
if(!in_array($key, $cash_types))
$val['disabled'] = true;
$labels[$key] = $val;
}
return $labels;
}
public static function getCashType($cash_type)
{
$labels = self::getCashTypeLabels();
return isset($labels[$cash_type]) ? $labels[$cash_type]['name'] : "未知";
}
public static $cxPayTypeWxpay = 1; //微信支付
public static $cxPayTypeAlipay = 2; //支付宝支付
public static $cxPayTypeBank = 3; //银行卡支付
public static function getPayTypeLabels()
{
return [
"1" => [
"type" => 1,
"name" => "微信",
"show" => true,
"disabled" => false,
],
"2" => [
"type" => 2,
"name" => "支付宝",
"show" => true,
"disabled" => false,
],
"3" => [
"type" => 3,
"name" => "银行卡",
"show" => true,
"disabled" => false,
],
];
}
public static function getPayType($pay_type)
{
$labels = self::getPayTypeLabels();
return isset($labels[$pay_type]) ? $labels[$pay_type]['name'] : "未知";
}
//提现规则检查
public static function checkCashSetting($scene, $cx_mch_id = 0)
{
$cash_setting = CashSetting::findOne([
'cx_mch_id' => $cx_mch_id,
'scene' => $scene,
]);
if($cash_setting == null){
return Model::asReturnError("提现规则未设置", self::getCashTypeLabels());
}
$service_charge = $cash_setting->service_charge / 100;
$service_charge = sprintf("%.2f", $service_charge);
$data = [
'min_cash' => $cash_setting->min_cash,
'max_cash' => $cash_setting->max_cash,
'day_max_cash' => $cash_setting->day_max_cash,
'service_charge' => $service_charge,
'cash_types' => json_decode($cash_setting->cash_types,true)
];
return Model::asReturnSuccess("ok", $data);
}
/**
* 提现金额检查
* @param integer $user_id 用户ID
* @param number $money 提现金额
* @param integer $scene 场景类型
* @param integer $cx_mch_id 平台商户ID
*/
public static function checkCashMoney($user_id, $money, $scene, $cx_mch_id = 0)
{
$temp = Cash::find()
->where([
'cx_mch_id' => $cx_mch_id,
'scene' => $scene,
'user_id' => $user_id,
'status' => [0,1],
'transfer_status' => 0,
])
->one();
if($temp != null){
return Model::asReturnError("已有提现在审核中或待打款");
}
$res = self::checkCashSetting($scene, $cx_mch_id);
if($res['code'] != 0)
return $res;
$cash_setting = $res['data'];
if($money <= 0){
return Model::asReturnError("提现金额需大于0元");
}
if($money < $cash_setting['min_cash'])
return Model::asReturnError("提现金额不能小于{$cash_setting['min_cash']}");
if($money > $cash_setting['max_cash'])
return Model::asReturnError("提现金额不能大于{$cash_setting['max_cash']}");
//每日提现金额
$day_cash_money = Cash::find()
->where([
'cx_mch_id' => $cx_mch_id,
'scene' => $scene,
'user_id' => $user_id,
'status' => [0,1],
'transfer_status' => [0,1],
])
->andWhere([
'FROM_UNIXTIME(created_at,"%Y-%m-%d")' => date('Y-m-d')
])
->sum('money');
$day_cash_money = empty($day_cash_money) ? 0 : $day_cash_money;
if($day_cash_money + $money > $cash_setting['day_max_cash'])
return Model::asReturnError("每日提现金额不能大于{$cash_setting['day_max_cash']}");
$service_charge = $money * $cash_setting['service_charge'];
$service_charge = sprintf("%.2f", $service_charge);
$data = [];
$data['service_charge'] = $service_charge;
$data['cash_setting'] = $cash_setting;
return Model::asReturnSuccess("ok", $data);
}
public static function checkAutoTransfer($user_id)
{
$user = User::findOne([
'id' => $user_id,
'is_delete' => 0,
'status' => User::STATUS_NORMAL,
]);
if($user == null)
return Model::asReturnError("用户不存在");
if($user->bindWxmp)
return Model::asReturnSuccess();
//@TODO 其他
return Model::asReturnError("请选择其他提现方式");
}
/**
* 检查用户账户余额
*/
public static function checkUserBalance($user_id, $scene, $cash_money, $cx_mch_id = 0)
{
//账户余额
$balance = Balance::findOne([
'cx_mch_id' => $cx_mch_id,
'scene' => $scene,
'user_id' => $user_id,
'is_delete' => 0,
]);
if($balance == null)
return Model::asReturnError("账户余额不足");
$account_balance = $balance->account_balance_with;
if($cash_money > $account_balance)
return Model::asReturnError("账户余额不足,最多提现{$account_balance}");
return Model::asReturnSuccess();
}
}