| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\models\Cash;
- use app\models\Option;
- use app\models\SaasUser;
- use app\models\Shop;
- use app\models\ShopSetting;
- use app\models\ShopShare;
- use app\models\User;
- use app\utils\OrderNo;
- use yii\base\Model;
- use yii\helpers\Json;
- class CashForm extends Model
- {
- public $user_id;
- public $saas_id;
- public $store_id;
- public $cash;
- public $name;
- public $mobile;
- public $pay_type;
- public $form_id;
- public $type;
- public $bank_name;
- public $cash_type = 0;
- public function rules()
- {
- return [
- [['cash'], 'required'],
- [['name', 'mobile', 'pay_type'], 'required', 'on' => 'CASH'],
- [['cash'], 'number', 'min' => 0,],
- [['cash_type'], 'integer'],
- [['name', 'mobile', 'form_id', 'type'], 'trim'],
- [['pay_type'], 'in', 'range' => [0, 1, 2, 3]],
- [['bank_name'], 'string'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'cash' => '提现金额',
- 'name' => '姓名',
- 'pay_type' => '提现方式',
- 'mobile' => '账号',
- 'bank_name' => '开户行',
- ];
- }
- public function save()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $setting = Option::get('share_basic_setting', $this->store_id);
- $setting = $setting ? Json::decode($setting['value']) : [];
- $cash_max_day = floatval($setting['cash_max_day']['value']);
- if ($cash_max_day) {
- $cash_sum = Cash::find()->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'status' => [0, 1, 2, 5],
- ])->andWhere([
- 'AND',
- ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
- ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
- ])->sum('price');
- $cash_max_day = $cash_max_day - ($cash_sum ? $cash_sum : 0);
- if ($this->cash > $cash_max_day) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能超过' . $cash_max_day . '元'
- ];
- }
- }
- $saas = SaasUser::findOne(['id' => $this->saas_id]);
- if (!$saas) {
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- $share_setting = [
- 'min_money' => $setting['min_money']['value'],
- ];
- if ($this->cash < $share_setting['min_money']) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能小于' . $share_setting['min_money'] . '元'
- ];
- }
-
- // 判断提现金额
- if ($this->cash_type == 0 && $saas->price < $this->cash) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能超过剩余金额'
- ];
- }
- $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['saas_id' => $this->saas_id,
- 'store_id' => $this->store_id,'cash_type' => 0])->exists();
- if ($exit) {
- // return [
- // 'code' => 1,
- // 'msg' => '尚有未完成的提现申请'
- // ];
- }
- $t = \Yii::$app->db->beginTransaction();
- $cash_service_charge = floatval($setting['cash_service_charge']['value']);
- $cash = new Cash();
- $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH);
- $cash->is_delete = 0;
- $cash->status = 0;
- $cash->price = $this->cash;
- $cash->created_at = time();
- $cash->user_id = 0;
- $cash->saas_id = $this->saas_id;
- $cash->store_id = $this->store_id;
- if ($this->cash_type == 0) {
- $saas_user = get_saas_user();
- $withdraw_method = Json::decode($saas_user->withdraw_method);
- $withdraw_method = array_column($withdraw_method, null, 'type');
- if ($this->type == 'wechat') {
- $cash->type = 0;
- $cash->name = $withdraw_method['wechat']['name'];
- $cash->mobile = $withdraw_method['wechat']['account'];
- }
- if ($this->type == 'alipay') {
- $cash->type = 1;
- $cash->name = $withdraw_method['alipay']['name'];
- $cash->mobile = $withdraw_method['alipay']['account'];
- }
- if ($this->type == 'bank_card') {
- $cash->type = 2;
- $cash->name = $withdraw_method['bank_card']['name'];
- $cash->mobile = $withdraw_method['bank_card']['account'];
- $cash->bank_name = $withdraw_method['bank_card']['bank'];
- $cash->bank_branch = $withdraw_method['bank_card']['branch'];
- }
- } else {
- $cash->type = $this->pay_type;
- $cash->name = $this->name;
- $cash->mobile = $this->mobile;
- $cash->bank_name = $this->bank_name;
- }
- $cash->pay_time = 0;
- $cash->service_charge = $cash_service_charge;
- $cash->cash_type = $this->cash_type;
- if ($cash->save()) {
- if ($this->cash_type == 0) {
- $saas->price -= $this->cash;
- }
- if (!$saas->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '申请成功'
- ];
- } else {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- }
- private function getOrderNo()
- {
- $order_no = null;
- while (true) {
- $order_no = date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
- if (!$exist_order_no) {
- break;
- }
- }
- return $order_no;
- }
- public static function getPayTyle()
- {
- $store_id = get_store_id();
- $setting = Option::get('share_basic_setting', $store_id);
- $setting = $setting ? Json::decode($setting['value']) : [];
-
- $res['pay_type'] = $setting['pay_type']['value'];
- $res['bank'] = $setting['bank']['value'];
- $res['remaining_sum'] = $setting['remaining_sum']['value'];
- $cash_last = Cash::find()->where(['store_id' => $store_id, 'user_id' => get_user_id(), 'is_delete' => 0])
- ->orderBy(['id' => SORT_DESC])->select(['name', 'mobile', 'type', 'bank_name'])->asArray()->one();
- $res['cash_last'] = $cash_last;
- $cash_max_day = floatval($setting['cash_max_day']['value']);
- if ($cash_max_day) {
- $cash_sum = Cash::find()->where([
- 'store_id' => $store_id,
- 'is_delete' => 0,
- 'status' => [0, 1, 2, 5],
- ])->andWhere([
- 'AND',
- ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
- ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
- ])->sum('price');
- $cash_max_day = $cash_max_day - ($cash_sum ? $cash_sum : 0);
- $res['cash_max_day'] = max(0, floatval(sprintf('%.2f', $cash_max_day)));
- } else {
- $res['cash_max_day'] = -1;
- }
- $cashServiceCharge = floatval($setting['cash_service_charge']['value']);
- $res['cash_service_charge'] = $cashServiceCharge;
- if($cashServiceCharge == 0){
- $res['service_content'] = "";
- }else{
- $res['service_content'] = "提现需要加收{$cashServiceCharge}%手续费";
- }
- $payTypeList = [
- [
- 'name' => '微信',
- 'is_show' => false
- ],
- [
- 'name' => '支付宝',
- 'is_show' => false
- ],
- [
- 'name' => '银行卡',
- 'is_show' => false
- ],
- [
- 'name' => '余额',
- 'is_show' => false
- ],
- ];
- switch($res['pay_type']){
- case 0:
- $payTypeList[0]['is_show'] = true;
- break;
- case 1:
- $payTypeList[1]['is_show'] = true;
- break;
- case 2:
- $payTypeList[0]['is_show'] = true;
- $payTypeList[1]['is_show'] = true;
- break;
- default:
- break;
- }
- if($res['bank'] && $res['bank'] == 1){
- $payTypeList[2]['is_show'] = true;
- }
- if($res['remaining_sum'] && $res['remaining_sum'] == 1){
- $payTypeList[3]['is_show'] = true;
- }
- $res['pay_type_list'] = $payTypeList;
- return $res;
- }
- }
|