'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], ]; } } }