| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\Cash;
- use app\models\CashExt;
- use app\models\IntegralAppreciationCashLog;
- use app\models\IntegralAppreciationUser;
- use app\models\IntegralAppreciationUserIntegralLog;
- use app\models\Lg;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\jobs\LgCashJob;
- use app\models\QueueAccountLog;
- use app\models\SaasUser;
- use app\models\ShareGroupPurchaseUser;
- use app\models\Shop;
- use app\models\ShopSetting;
- use app\models\ShopShare;
- use app\models\SuperSalesUser;
- use app\models\TeamGrades;
- use app\models\User;
- use app\utils\OrderNo;
- use yii\base\Model;
- use yii\helpers\Json;
- use app\models\AccountLog;
- use app\models\Store;
- use app\models\StoreMoneyLog;
- use app\models\WechatConfig;
- use app\modules\admin\controllers\ShareController;
- use app\utils\Alipay\Alipay;
- use app\utils\LgApi;
- use app\utils\Notice\NoticeSend;
- use yii\helpers\VarDumper;
- class CashForm extends Model
- {
- public $user_id;
- public $store_id;
- public $cash;
- public $name;
- public $mobile;
- public $pay_type;
- public $form_id;
- public $type;
- public $bank_name;
- public $is_queue;
- public $cash_type = 0;
- public function rules()
- {
- return [
- [['cash'], 'required'],
- [['name', 'mobile', 'pay_type'], 'required', 'on' => 'CASH'],
- [['cash'], 'number', 'min' => 0,],
- [['cash_type','is_queue'], '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()) {
- $cash_service_charge = 0;
- $integral_appreciation_price = 0;
- $integral_appreciation_result = [];
- $cash_price = $this->cash;
- //兼容团队业绩分红
- if (intval($this->cash_type) !== Cash::IS_CASH_TYPE_TEAM_GRADES) {
- $setting = Option::get('share_basic_setting', $this->store_id);
- $setting = $setting ? Json::decode($setting['value']) : [];
- $cash_max_day = floatval($setting['cash_max_day']['value']);
- $cash_max_single_day = floatval($setting['cash_max_single_day']['value']);
- $cash_service_charge = floatval($setting['cash_service_charge']['value']);
- if (in_array(intval($this->cash_type), [Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES, Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION])) {
- if (intval($this->cash_type) === Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE) {
- $setting = Option::get('share_group_setting', $this->store_id);
- } elseif (intval($this->cash_type) === Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
- $setting = Option::get('integral_appreciation_setting', $this->store_id);
- $integral_appreciation_price = $this->cash;
- $integral_appreciation_result = CashExt::integral_appreciation_cash($this->cash, $this->store_id);
- $this->cash = $integral_appreciation_result['cash_price'];
- } else {
- $setting = Option::get('super_sales_setting', $this->store_id);
- }
- $setting = $setting ? Json::decode($setting['value']) : [];
- $cash_max_day = floatval($setting['cash_max_day']);
- $cash_max_single_day = floatval($setting['cash_max_single_day']);
- $cash_service_charge = floatval($setting['cash_service_charge']);
- }
- $user_id = $this->user_id;
- if ($cash_max_day) {
- $query = Cash::find()->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'status' => [0, 1, 2, 5],
- ]);
- if (intval($this->cash_type) === Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE) {
- $query->andWhere(['cash_type' => Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE]);
- } elseif (intval($this->cash_type) === Cash::IS_CASH_TYPE_SUPER_SALES) {
- $query->andWhere(['cash_type' => Cash::IS_CASH_TYPE_SUPER_SALES]);
- } else {
- $query->andWhere(['cash_type' => Cash::IS_CASH_TYPE_SHARE]);
- }
- $cash_sum = $query->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 . '元'
- ];
- }
- }
- if ($cash_max_single_day) {
- $cash_sum = Cash::find()->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'status' => [0, 1, 2, 5],
- 'user_id' => $user_id,
- 'cash_type' => $this->cash_type
- ])->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_single_day_ = $cash_max_single_day - ($cash_sum ?: 0);
- if ($this->cash > $cash_max_single_day_) {
- return [
- 'code' => 1,
- 'msg' => '每人每天提现金额不能超过'. $cash_max_single_day .'元'
- ];
- }
- }
- }
- $user = User::findOne(['id' => $this->user_id, 'store_id' => $this->store_id]);
- if (!$user) {
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- if (intval($this->cash_type) !== Cash::IS_CASH_TYPE_TEAM_GRADES) {
- // 判断最小提现金
- if ($this->cash_type == 1) {
- $share_setting = ShopSetting::find()->where(['store_id' => $this->store_id])->one()->toArray();
- } else {
- if (in_array(intval($this->cash_type), [Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES, Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION])) {
- $share_setting = [
- 'min_money' => $setting['min_money'],
- 'auto_money' => 0,
- ];
- } else {
- $share_setting = [
- 'min_money' => $setting['min_money']['value'],
- 'auto_money' => $setting['auto_money']['value'],
- ];
- }
- }
- if ($this->cash < $share_setting['min_money']) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能小于' . $share_setting['min_money'] . '元'
- ];
- }
- }
- // 判断提现金额
- if (in_array($this->cash_type, [0, Cash::IS_CASH_TYPE_TEAM_GRADES, Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES]) && $user->price < $this->cash) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能超过剩余金额'
- ];
- } elseif(($this->cash_type == Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION)){
- $integralAppreciationUser = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
- if ($integralAppreciationUser->integral < $cash_price) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能超过剩余金额'
- ];
- }
- $user_total_integral = IntegralAppreciationUser::find()->where(['store_id' => $this->store_id])->sum('integral') ?: 0;
- if (bcsub($user_total_integral, $cash_price, 2) <= 0.1) {
- return [
- 'code' => 1,
- 'msg' => '提现金额受限'
- ];
- }
- } elseif(($this->cash_type == 1) && $user->tuan_price < $this->cash){
- return [
- 'code' => 1,
- 'msg' => '提现金额不能超过剩余金额'
- ];
- }
- if (intval($this->cash_type) === Cash::IS_CASH_TYPE_TEAM_GRADES) {
- $userTeamGrades = TeamGrades::findOne(['user_id' => get_user_id(), 'is_delete' => 0]);
- if (!$userTeamGrades) {
- return [
- 'code' => 1,
- 'msg' => '非团队业绩成员'
- ];
- }
- if ($userTeamGrades->price < $this->cash) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不足'
- ];
- }
- }
- if (in_array(intval($this->cash_type), [Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES])) {
- if (intval($this->cash_type) === Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE) {
- $activityUser = ShareGroupPurchaseUser::findOne(['user_id' => get_user_id()]);
- if (empty($activityUser)) {
- return [
- 'code' => 1,
- 'msg' => '未参与37拼购活动'
- ];
- }
- } else {
- $activityUser = SuperSalesUser::findOne(['user_id' => get_user_id()]);
- }
- if ($activityUser->price < $this->cash) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不足'
- ];
- }
- }
- if (intval($this->cash_type) === Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
- $integral_appreciation_user = IntegralAppreciationUser::findOne(['user_id' => get_user_id()]);
- if (empty($integral_appreciation_user)) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不足'
- ];
- }
- if ($integral_appreciation_user->integral < $integral_appreciation_price) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不足'
- ];
- }
- }
- // 判断是否存在未完成提现
- $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['user_id' => $this->user_id,
- 'store_id' => $this->store_id, 'cash_type' => $this->cash_type ?: 0])->exists();
- if ($exit) {
- return [
- 'code' => 1,
- 'msg' => '尚有未完成的提现申请'
- ];
- }
- if ($this->cash_type == 1) {
- $shop = Shop::findOne(['user_id'=> $this->user_id, 'store_id' => $this->store_id, 'is_delete' => 0, 'shop_audit' => 1]);
- $shop_form = new ShopShare();
- $shop_form->store_id = $this->store_id;
- $shop_form->shop_id = $shop->id;
- $shop_form->type = 0;
- $shop_form->amount = -$this->cash;
- $shop_form->the_desc = '自提点'.$user['nickname'].'提现';
- $shop_form->created_at = time();
- $shop_form->status = 1;
- $shop_form->save();
- }
- $t = \Yii::$app->db->beginTransaction();
- // $cash_service_charge = intval($this->cash_type) === Cash::IS_CASH_TYPE_TEAM_GRADES ? 0 :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 = $this->user_id;
- $cash->store_id = $this->store_id;
- if (in_array($this->cash_type, [0, Cash::IS_CASH_TYPE_TEAM_GRADES, Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES, Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION])) {
- $saas_user = get_saas_user();
- $withdraw_method = Json::decode($saas_user->withdraw_method);
- $withdraw_method = array_column((array)$withdraw_method, null, 'type');
- if ($this->type == 'money') {
- $cash->type = 3;
- } else {
- 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'];
- }
- if ($this->type == 'lg') {
- //灵工提现
- $lg_info = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0,'status'=>1])->one();
- $cash->type = 4;
- $cash->name = $lg_info->name;
- $cash->mobile = $lg_info->mobile;
- }
- }
- } 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->type == Cash::TYPE_RECHARGE ? 0 : $cash_service_charge;
- $cash->cash_type = $this->cash_type;
- if ($cash->save()) {
- //如果是团长提现,扣除会员里的团长佣金字段 add by 甜心100 mrlu 20190110
- if ($this->cash_type == 1) {
- $user->tuan_price -= $this->cash;
- } elseif ($this->cash_type == 0) {
- $user->price -= $this->cash;
- }
- if ($this->cash_type == Cash::IS_CASH_TYPE_TEAM_GRADES) {
- $userTeamGrades->price -= $this->cash;
- if (!$userTeamGrades->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($userTeamGrades->firstErrors))
- ];
- };
- $user->price -= $this->cash;
- }
- if (intval($this->cash_type) === Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
- $integral_appreciation_user->integral -= $integral_appreciation_price;
- if (!$integral_appreciation_user->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($integral_appreciation_user->firstErrors))
- ];
- };
- $integral_appreciation_cash = new IntegralAppreciationCashLog();
- $integral_appreciation_cash->user_id = $this->user_id;
- $integral_appreciation_cash->cash_id = $cash->id;
- $integral_appreciation_cash->integral = $integral_appreciation_price;
- $integral_appreciation_cash->integral_price = $integral_appreciation_result['integral_price'];;
- $integral_appreciation_cash->reflux_profit = $integral_appreciation_result['integral_payout_pool_profit'];
- $integral_appreciation_cash->reflux_price = $integral_appreciation_result['payout_pool_price'];
- $integral_appreciation_cash->cash_price = $this->cash;
- if (!$integral_appreciation_cash->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($integral_appreciation_user->firstErrors))
- ];
- };
- $result = IntegralAppreciationUserIntegralLog::saveIntegralLog($integral_appreciation_user->id, $integral_appreciation_price, IntegralAppreciationUserIntegralLog::TYPE_EXPEND, IntegralAppreciationUserIntegralLog::SOURCE_TYPE_WITHDRAW);
- if ($result['code']) {
- $t->rollBack();
- return $result;
- }
- // $user->price -= $this->cash;
- }
- if (in_array(intval($this->cash_type), [Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES])) {
- $activityUser->price -= $this->cash;
- if (!$activityUser->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($activityUser->firstErrors))
- ];
- };
- $user->price -= $this->cash;
- }
- if (!$user->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- if ($cash->type == Cash::TYPE_RECHARGE || ($this->cash_type == 0 && $this->cash <= $share_setting['auto_money'] && $share_setting['auto_money'] > 0)) {
- // TODO 在设置金额内,分销/充值佣金提现自动打款
- debug_log([$cash->id, $cash->type == Cash::TYPE_RECHARGE ? 4 : 2, $this->store_id],'cash.log');
- $aa = $this->confirm($cash->id, $cash->type == Cash::TYPE_RECHARGE ? 4 : 2, $this->store_id);
- debug_log($aa,'cash.log');
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '申请成功'
- ];
- } else {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- } else {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- }
- public function confirm($id, $status, $store_id)
- {
- $store = Store::findOne($store_id);
- $cash = Cash::findOne([
- 'id' => $id,
- 'is_delete' => Cash::IS_DELETE_NO,
- 'store_id' => $store_id
- ]);
- if($status == Cash::STATUS_GIVEN){
- $store_wechat_cash = json_decode(Option::get('store_wechat_cash', $store_id, 'store')['value'], true);
- $val = 0;
- foreach ((array)$store_wechat_cash as $value) {
- if (empty($value)) {
- $val = 1;
- break;
- } else {
- $val = 2;
- break;
- }
- }
- if ($store->is_platform_transfers == 0 && $val == 2) {
- return [
- 'code' => 1,
- 'msg' => '未开启平台转账'
- ];
- } elseif ($store->is_platform_transfers == 1 && $val == 1) {
- return [
- 'code' => 1,
- 'msg' => '未配置平台微信提现设置'
- ];
- }
- }
- if (!$cash->order_no) {
- $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;
- }
- }
- $cash->order_no = $order_no;
- $cash->save();
- }
- $res = [];
- $cashExt = CashExt::findOne(['cash_id' => $cash['id'], 'cash_price_type' => CashExt::CASH_PRICE_TYPE_AMOUNT]);
- if ($cashExt) {
- $price = $cashExt->real_price;
- } else {
- $price = Cash::getServiceMoney($cash);
- }
- $servePrice = $cash->price * ($cash->service_charge / 100);
- if ($store->store_balance < $price && $store->is_platform_transfers == 1) {
- return [
- 'code' => 1,
- 'msg' => '店铺剩余提现余额不足'
- ];
- }
- $wechat_type = 1;
- $wechat_cash = Option::get('wechat_cash', $store_id, 'store')['value'];
- // if (empty($wechat_cash)) {
- // $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
- // if ($wechat_cash) {
- // $wechat_type = 0;
- // }
- // }
- foreach ((array)json_decode($wechat_cash, true) as $value) {
- if (empty($value)) {
- $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
- if ($wechat_cash) {
- $wechat_type = 0;
- }
- break;
- }
- }
- $t = \Yii::$app->db->beginTransaction();
- if ($status == Cash::STATUS_GIVEN) { //微信自动打款
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_WX;
- $user = User::findOne(['id' => $cash->user_id]);
- $data = [
- 'partner_trade_no' => $cash->order_no,
- 'openid' => $user->wechat_open_id,
- 'check_name' => 'NO_CHECK',
- 'amount' => $price * 100,
- 'desc' => '转账'
- ];
- if ((int)$cash->type === 0) {
- if (\Yii::$app->prod_is_dandianpu() && !\app\models\Store::hasIncoming($store_id)) {
- //商城是否进件
- //商城余额是否充足
- $storeMoney = \app\models\StoreCash::getMaxCash($store);
- if ($storeMoney < $price) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '操作错误,商城余额不足! 当前余额:¥' . $storeMoney
- ];
- }
- //扣除商城余额
- $subMoney = \app\models\Store::subMoney($store, $price);
- if (!$subMoney) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常,subMoney失败1',
- ];
- }
- } else {
- if (\Yii::$app->prod_is_duli()) {
- $WechatConfig = WechatConfig::findOne(['store_id' => get_store_id()]);
- if (empty($WechatConfig->mch_id) || empty($WechatConfig->pay_key) || empty($WechatConfig->app_id) || empty($WechatConfig->cert_pem) || empty($WechatConfig->key_pem)) {
- return [
- 'code' => 1,
- 'msg' => "后台参数配置错误,请检查参数后重试"
- ];
- }
- }
- }
- if ($wechat_type == 0) {
- $before = $store->store_balance;
- $store->store_balance -= $price;
- $store->price -= $price;
- $store->store_withdrawn_cash += $price;
- $after = $store->store_balance;
- if ($store->save()) {
- StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHARE, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type);
- }
- }
- $wechat = \Yii::$app->controller->wechatPay;
- // $res = $wechat->transfer->toBalance($data);
- $res = (new \app\utils\WechatMerchant\WxV3($wechat))->transferBatches(get_store_id(), $data);
- //判断是否使用新版本转账 增加转账标识
- $wechat_cash = json_decode($wechat_cash, true);
- if (intval($wechat_cash['is_open']) === 2) {
- if (!$res['code']) {
- $cash->is_platform_transfers = intval($store->is_platform_transfers);
- $cash->wx_cash_type = Cash::WX_CASH_TYPE_NEW;
- $cash->wx_cash_state = $res['data']['state'];
- $cash->wx_cash_result_info = json_encode($res['data'], JSON_UNESCAPED_UNICODE);
- $cash->save();
- }
- }
- NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '微信自动打款', ($cash->price - $price));
- } elseif ((int)$cash->type === 1) {
- $order = (object)[
- 'store_id' => $cash->store_id,
- 'order_no' => $cash->order_no,
- 'pay_price' => sprintf("%.2f", $price),
- 'name' => $cash->name
- ];
- $result = Alipay::transfer($order, $cash->mobile);
- if (isset($result['code']) && $result['code'] == 1) {
- if (strpos($result['msg'], 'aop.invalid-app-auth-token-no-api')) {
- $result['msg'] = '接口未授权,请前往支付宝开放平台查询是否开通产品或授权支付宝转账产品';
- }
- if (strpos($result['msg'], 'PAYEE_NOT_EXIST')) {
- $result['msg'] = '收款账号不存在或姓名有误,建议核实账号和姓名是否准确';
- }
- if (strpos($result['msg'], 'BALANCE_IS_NOT_ENOUGH')) {
- $result['msg'] = '商户余额不足';
- }
- return $result;
- } else {
- // TODO 扣除店铺提现金额
- if ($wechat_type == 0) {
- $before = $store->store_balance;
- $store->store_balance -= $price;
- $store->price -= $price;
- $store->store_withdrawn_cash += $price;
- $after = $store->store_balance;
- if ($store->save()) {
- StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHARE, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type);
- }
- }
- }
- $cash->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '成功'
- ];
- } elseif ((int)$cash->type === 4) {
- $user = \app\models\User::findOne($cash->user_id);
- $saas_user = \app\models\SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
- $lg_info = Lg::find()->where(['user_id' => $saas_user->id, 'status' => 1, 'is_delete' => 0])->one();
- $lgApi = new LgApi($cash->store_id);
- //灵工提现
- $post_data = [
- 'store_id' => $cash->store_id,
- 'outTradeNo' => $cash->order_no, //唯一批次号
- 'accNo' => bcmul($price, 100),
- 'amt' => bcmul($price, 100),
- 'name' => $lg_info->name,
- 'certCard' => $lg_info->cert_card //身份证号
- ];
- $result = $lgApi->FlexiblePay($post_data);
- if (isset($result['status']) && $result['status'] == 999) {
- return $result;
- }
- $cash->status = 6; //灵工待打款
- $cash->save();
- $t->commit();
- //灵工提现接口调用后消息队列查询状态
- \queue_push(new LgCashJob(['id' => $cash->id, 'store_id' => $store_id, 'type' => 0, 'retry' => 5]), 60);
- return [
- 'code' => 0,
- 'msg' => '成功'
- ];
- }
- } elseif ($status == Cash::STATUS_HAND) { //手动打款
- // TODO 扣除店铺提现金额
- if ($wechat_type == 0) {
- $before = $store->store_balance;
- $store->store_balance -= $price;
- $store->price -= $price;
- $store->store_withdrawn_cash += $price;
- $after = $store->store_balance;
- if ($store->save()) {
- StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHARE, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type);
- } else {
- $t->rollBack();
- }
- }
- $cash->status = Cash::STATUS_HAND;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_HAND;
- if ($cash->type == Cash::TYPE_RECHARGE) {
- $user = User::findOne(['id' => $cash->user_id]);
- $cashExt = CashExt::findOne(['cash_id' => $cash->id, 'cash_price_type' => CashExt::CASH_PRICE_TYPE_AMOUNT]);
- if ($cashExt && $cashExt->real_price > 0) {
- AccountLog::saveLog($cash->user_id, $cashExt->real_price, 2, 1, 0, 0, '佣金提现打款');
- }
- }
- $res['result_code'] = "SUCCESS";
- NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '手动打款', ($cash->price - $price));
- }
- if (isset($res['result_code']) && $res['result_code'] == 'SUCCESS') {
- $cash->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '成功'
- ];
- } else {
- debug_log('shibai','auto_money.log');
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => !empty($res['err_code_des']) ? $res['err_code_des'] : '请稍后重试',
- 'data' => $res
- ];
- }
- }
- public function cashWorker()
- {
- $this->cash_type = Cash::IS_CASH_TYPE_WORKER;
- if ($this->validate()) {
- $user = User::findOne(['id' => $this->user_id, 'store_id' => $this->store_id]);
- if (!$user) {
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- $key = 'cash_user_' . $user->id;
- if (cache()->exists($key)) {
- return [
- 'code' => 1,
- 'msg' => '请勿重复提交'
- ];
- }
- cache()->set($key, 1, 60);
- // 判断最小提现金
- if ($this->cash < 0.01) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能小于' . 0.01 . '元'
- ];
- }
- // 判断提现金额
- if ($user->price < $this->cash) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能超过剩余金额'
- ];
- }
- // 判断是否存在未完成提现
- $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['user_id' => $this->user_id,
- 'store_id' => $this->store_id, 'cash_type' => $this->cash_type])->exists();
- if ($exit) {
- // return [
- // 'code' => 1,
- // 'msg' => '尚有未完成的提现申请'
- // ];
- }
- $t = \Yii::$app->db->beginTransaction();
- $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 = $this->user_id;
- $cash->store_id = $this->store_id;
- $saas_user = get_saas_user();
- $withdraw_method = Json::decode($saas_user->withdraw_method);
- $withdraw_method = array_column((array)$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']['bank_branch'] ?? '';
- }
- if ($this->type == 'money') {
- $cash->type = 3;
- }
- $cash->pay_time = 0;
- $cash->service_charge = 0;
- $cash->cash_type = $this->cash_type;
- if (!$cash->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- $user->price -= $this->cash;
- if (!$user->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '申请成功'
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- }
- public function cashBalance() {
- $this->cash_type = Cash::IS_CASH_TYPE_BALANCE;
- if ($this->validate()) {
- $user = User::findOne(['id' => $this->user_id, 'store_id' => $this->store_id]);
- if (!$user) {
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- $balancToCash = Option::get(OptionSetting::BALANCE_TO_CASH, $this->store_id, 'recharge', 0)['value'];
- if (!$balancToCash) {
- return [
- 'code' => 1,
- 'msg' => '提现功能未开启'
- ];
- }
- // 判断提现金额
- if ($user->money < $this->cash) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能超过剩余金额'
- ];
- }
- $balancToCashSet = json_decode(Option::get(OptionSetting::BALANCE_TO_CASH_SETTING, $this->store_id, 'recharge', '{}')['value'], true);
- if (!isset($balancToCashSet['cash_service_charge'])) {
- return [
- 'code' => 1,
- 'msg' => '请先配置提现设置'
- ];
- }
- // 判断最小提现金
- if ($this->cash < $balancToCashSet['min_money']) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能小于' . $balancToCashSet['min_money'] . '元'
- ];
- }
- // 判断是否存在未完成提现
- $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['user_id' => $this->user_id,
- 'store_id' => $this->store_id, 'cash_type' => $this->cash_type])->exists();
- if ($exit) {
- // return [
- // 'code' => 1,
- // 'msg' => '尚有未完成的提现申请'
- // ];
- }
- if ($balancToCashSet['cash_max_single_day']) {
- $cash_sum = Cash::find()->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'status' => [0, 1, 2, 5],
- 'user_id' => $this->user_id,
- 'cash_type' => $this->cash_type,
- ])->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_single_day_ = $balancToCashSet['cash_max_single_day'] - ($cash_sum ?: 0);
- if ($this->cash > $cash_max_single_day_) {
- return [
- 'code' => 1,
- 'msg' => '每人每天提现金额不能超过'. $balancToCashSet['cash_max_single_day'] .'元'
- ];
- }
- }
- if ($balancToCashSet['cash_max_day']) {
- $cash_sum = Cash::find()->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'status' => [0, 1, 2, 5],
- 'cash_type' => $this->cash_type,
- ])->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_ = $balancToCashSet['cash_max_day'] - ($cash_sum ?: 0);
- if ($this->cash > $cash_max_day_) {
- return [
- 'code' => 1,
- 'msg' => '超过平台每天限额'. $balancToCashSet['cash_max_day'] .'元'
- ];
- }
- }
- $t = \Yii::$app->db->beginTransaction();
- $cash = new Cash();
- $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH);
- $cash->is_delete = 0;
- $cash->status = 0;
- $cash->price = $this->cash;
- $cash->service_charge = $balancToCashSet['cash_service_charge'];
- $cash->created_at = time();
- $cash->user_id = $this->user_id;
- $cash->store_id = $this->store_id;
- $saas_user = get_saas_user();
- $withdraw_method = Json::decode($saas_user->withdraw_method);
- $withdraw_method = array_column((array)$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'];
- }
- if ($this->type == 'lg') {
- //灵工提现
- $lg_info = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0,'status'=>1])->one();
- $cash->type = 4;
- $cash->name = $lg_info->name;
- $cash->mobile = $lg_info->mobile;
- }
- $cash->pay_time = 0;
- $cash->cash_type = $this->cash_type;
- if (!$cash->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常,提现保存失败'
- ];
- }
- // 免单返现提现记录
- if ($this->is_queue){
- $queue_account_log = new QueueAccountLog();
- $queue_account_log->store_id = $this->store_id;
- $queue_account_log->user_id = $this->user_id;
- $queue_account_log->order_id = $cash->id;
- $queue_account_log->amount = $this->cash;
- $queue_account_log->desc = '免单提现';
- $queue_account_log->log_type = 2;
- $queue_account_log->type = 1;
- $queue_account_log->created_at = time();
- $queue_account_log->save();
- }
- if ($this->cash_type == 7 && $this->cash <= $balancToCashSet['auto_money'] && $balancToCashSet['auto_money'] > 0) {
- // TODO 在设置金额内,余额提现自动打款
- $this->confirm($cash->id, 2, $this->store_id);
- }
- $saveLog = AccountLog::saveLog($this->user_id, $cash->price, AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_EXPEND, 0, 0, "余额提现,单号:{$cash->order_no}");
- if (!$saveLog) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常,账户保存失败'
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '申请成功'
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- }
- 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;
- }
- public function saveNew()
- {
- if ($this->validate()) {
- $cash_price = $this->cash;
- $cash = Option::get(OptionSetting::CASH_SETTING, get_store_id(), 'store')['value'];
- $setting = $cash ? Json::decode($cash, true) : [];
- $cash_max_day = floatval($setting['cash_max_day']);
- $cash_max_single_day = floatval($setting['cash_max_single_day']);
- $cash_service_charge = floatval($setting['cash_service_charge']);
- $integral_appreciation_price = 0;
- $integral_appreciation_result = [];
- if (intval($this->cash_type) === Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
- $integral_appreciation_price = $this->cash;
- $integral_appreciation_result = CashExt::integral_appreciation_cash($this->cash, $this->store_id);
- $this->cash = $integral_appreciation_result['cash_price'];
- }
- if ($cash_max_day) {
- $query = Cash::find()->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'status' => [0, 1, 2, 5],
- ]);
- $cash_sum = $query->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' => '超过平台提现限额,请明日提现'
- ];
- }
- }
- if ($cash_max_single_day) {
- $cash_sum = Cash::find()->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'status' => [0, 1, 2, 5],
- 'user_id' => $this->user_id,
- ])->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_single_day_ = $cash_max_single_day - ($cash_sum ?: 0);
- if ($this->cash > $cash_max_single_day_) {
- return [
- 'code' => 1,
- 'msg' => '每人每天提现金额不能超过' . $cash_max_single_day . '元'
- ];
- }
- }
- $user = User::findOne(['id' => $this->user_id, 'store_id' => $this->store_id]);
- if (!$user) {
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- $share_setting = [
- 'min_money' => $setting['min_money'],
- 'auto_money' => 0,
- ];
- if ($this->cash < $share_setting['min_money']) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能小于' . $share_setting['min_money'] . '元'
- ];
- }
- // 判断提现金额
- if (in_array($this->cash_type, [0, Cash::IS_CASH_TYPE_TEAM_GRADES, Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES, 3]) && $user->price < $this->cash) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能超过剩余金额!'
- ];
- } elseif (($this->cash_type == Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION)) {
- $integralAppreciationUser = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
- if ($integralAppreciationUser->integral < $cash_price) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能超过剩余金额。'
- ];
- }
- $user_total_integral = IntegralAppreciationUser::find()->where(['store_id' => $this->store_id])->sum('integral') ?: 0;
- if (bcsub($user_total_integral, $cash_price, 2) <= 0.1) {
- return [
- 'code' => 1,
- 'msg' => '提现金额受限'
- ];
- }
- } elseif (($this->cash_type == 1) && $user->tuan_price < $this->cash) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不能超过剩余金额'
- ];
- }
- if (intval($this->cash_type) === Cash::IS_CASH_TYPE_TEAM_GRADES) {
- $userTeamGrades = TeamGrades::findOne(['user_id' => get_user_id(), 'is_delete' => 0]);
- if (!$userTeamGrades) {
- return [
- 'code' => 1,
- 'msg' => '非团队业绩成员'
- ];
- }
- if ($userTeamGrades->price < $this->cash) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不足'
- ];
- }
- }
- if (in_array(intval($this->cash_type), [Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES])) {
- if (intval($this->cash_type) === Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE) {
- $activityUser = ShareGroupPurchaseUser::findOne(['user_id' => get_user_id()]);
- if (empty($activityUser)) {
- return [
- 'code' => 1,
- 'msg' => '未参与37拼购活动'
- ];
- }
- } else {
- $activityUser = SuperSalesUser::findOne(['user_id' => get_user_id()]);
- }
- if ($activityUser->price < $this->cash) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不足'
- ];
- }
- }
- if (intval($this->cash_type) === Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
- $integral_appreciation_user = IntegralAppreciationUser::findOne(['user_id' => get_user_id()]);
- if (empty($integral_appreciation_user)) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不足'
- ];
- }
- if ($integral_appreciation_user->integral < $integral_appreciation_price) {
- return [
- 'code' => 1,
- 'msg' => '提现金额不足'
- ];
- }
- }
- $t = \Yii::$app->db->beginTransaction();
- $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 = $this->user_id;
- $cash->store_id = $this->store_id;
- $saas_user = get_saas_user();
- $withdraw_method = Json::decode($saas_user->withdraw_method);
- $withdraw_method = array_column((array)$withdraw_method, null, 'type');
- if ($this->type == 'money') {
- $cash->type = 3;
- } else {
- 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'];
- }
- if ($this->type == 'lg') {
- //灵工提现
- $lg_info = Lg::find()->where(['user_id' => $saas_user->id, 'is_delete' => 0, 'status' => 1])->one();
- $cash->type = 4;
- $cash->name = $lg_info->name;
- $cash->mobile = $lg_info->mobile;
- }
- }
- $cash->pay_time = 0;
- // 如果是 提现方式是余额的时候 不收取手续费
- $cash->service_charge = $cash->type == Cash::TYPE_RECHARGE ? 0 : $cash_service_charge;
- $cash->cash_type = $this->cash_type;
- if ($cash->save()) {
- //如果是团长提现,扣除会员里的团长佣金字段
- if ($this->cash_type == 1) {
- $user->tuan_price -= $this->cash;
- } else {
- $user->price -= $this->cash;
- }
- if ($this->cash_type == Cash::IS_CASH_TYPE_TEAM_GRADES) {
- $userTeamGrades->price -= $this->cash;
- if (!$userTeamGrades->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($userTeamGrades->firstErrors))
- ];
- };
- $user->price -= $this->cash;
- }
- if (intval($this->cash_type) === Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
- $integral_appreciation_user->integral -= $integral_appreciation_price;
- if (!$integral_appreciation_user->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($integral_appreciation_user->firstErrors))
- ];
- };
- $integral_appreciation_cash = new IntegralAppreciationCashLog();
- $integral_appreciation_cash->user_id = $this->user_id;
- $integral_appreciation_cash->cash_id = $cash->id;
- $integral_appreciation_cash->integral = $integral_appreciation_price;
- $integral_appreciation_cash->integral_price = $integral_appreciation_result['integral_price'];;
- $integral_appreciation_cash->reflux_profit = $integral_appreciation_result['integral_payout_pool_profit'];
- $integral_appreciation_cash->reflux_price = $integral_appreciation_result['payout_pool_price'];
- $integral_appreciation_cash->cash_price = $this->cash;
- if (!$integral_appreciation_cash->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($integral_appreciation_user->firstErrors))
- ];
- };
- $result = IntegralAppreciationUserIntegralLog::saveIntegralLog($integral_appreciation_user->id, $integral_appreciation_price, IntegralAppreciationUserIntegralLog::TYPE_EXPEND, IntegralAppreciationUserIntegralLog::SOURCE_TYPE_WITHDRAW);
- if ($result['code']) {
- $t->rollBack();
- return $result;
- }
- // $user->price -= $this->cash;
- }
- if (in_array(intval($this->cash_type), [Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES])) {
- $activityUser->price -= $this->cash;
- if (!$activityUser->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($activityUser->firstErrors))
- ];
- };
- $user->price -= $this->cash;
- }
- if (!$user->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- if ($cash->type == Cash::TYPE_RECHARGE || ($this->cash_type == 0 && $this->cash <= $share_setting['auto_money'] && $share_setting['auto_money'] > 0)) {
- // TODO 在设置金额内,分销/充值佣金提现自动打款
- debug_log([$cash->id, $cash->type == Cash::TYPE_RECHARGE ? 4 : 2, $this->store_id], 'cash.log');
- $aa = $this->confirm($cash->id, $cash->type == Cash::TYPE_RECHARGE ? 4 : 2, $this->store_id);
- debug_log($aa, 'cash.log');
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '申请成功'
- ];
- } else {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- } else {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- }
- }
|