request->post(); $this->requestValidate($post, [ [['apply_cash', 'bank_user_name', 'bank_card', 'bank_name', 'bank_account_type', 'user_type'], 'required'] ]); if ($post['apply_cash']<3){ throw new \yii\db\Exception('申请提现金额最少不能小于3元'); } $user = \Yii::$app->user->identity; if ($user->role != UserRoleEnum::STORE_ADMIN) { throw new Exception('您还不是诊所管理员,没有权限申请打款'); } if ($post['user_type'] != 1) throw new Exception('用户类型错误'); if ($post['bank_account_type'] == 1 || $post['bank_account_type'] == 5) { if (empty($post['bank_no'])) { throw new Exception('bank_no不能为空'); } } else { if (substr($post['bank_card'], 0, 2) != '62') { if (empty($post['bank_no'])) { throw new Exception('bank_no不能为空'); } } } $t = \Yii::$app->db->beginTransaction(); try { $order_no = 'DK' . time() . rand(111111, 999999); $Account = CashAccount::find()->where([ 'user_id' => $user->store_id, 'user_type' => 1 ])->one(); if (!$Account) throw new Exception('账号不存在'); if (bcsub($Account->able_cash ,$Account->frozen_cash,2) < $post['apply_cash']) { throw new Exception('sorry,您的账户没有那么多提现金额'); } $CashAccount = CashAccount::find()->where([ 'user_id' => $user->store_id, 'user_type' => 1 ])->one(); $CashAccount->frozen_cash = bcadd($post['apply_cash'], $CashAccount->frozen_cash, 2); $CashAccount->last_apply_time = date('Y-m-d H:i:s', time()); $CashAccount->saveOrFail(); // TODO 暂时关闭手续费 扣除平台的手续费 $pQianliuwu=bcdiv(0.65,100,4); $pShouxufei=bcadd(bcmul($post['apply_cash'],$pQianliuwu,2),2,3); $shouxufei = 0; // $Platform->updateAllCounters(['able_cash'=>1]); // $Platform->updateAllCounters(['total_cash'=>1]); $CashApply = new CashApply(); $CashApply->user_id = $user->store_id;//诊所id $CashApply->user_type = 1;//用户类型1诊所2总部 $CashApply->order_no = $order_no;//申请提现金额 $CashApply->apply_cash = $post['apply_cash'];//申请提现金额 $CashApply->charge_cash =$shouxufei;//手续费 $CashApply->true_cash = bcsub($CashApply->apply_cash, $shouxufei, 2);//实际到账金额 $CashApply->apply_time = date('Y-m-d H:i:s', time()); $CashApply->saveOrFail(); // 记录代付的手续费 $chargeCpr = new ChargeCashPayRecord(); $chargeCpr->order_no = $order_no; $chargeCpr->store_id = $user->store_id;//诊所id $chargeCpr->charge_cash = $pShouxufei;//手续费 $chargeCpr->type = 1;//用户类型1诊所2总部 $chargeCpr->status = 1; $chargeCpr->saveOrFail(); $log = new Log(); $log->admin_id = $user->uid; $log->operate_time = date('Y-m-d H:i:s', time()); $log->type = '诊所申请打款'; $log->mold = 3;//财务操作 $log->content = '诊所管理员ID:' . $user->uid . '申请打款:' . $post['apply_cash'] . '元,订单号为:' . $order_no . ',开户人姓名为:' . $post['bank_user_name'] . ',银行卡号:' . $post['bank_card'] . ',开户行名称:' . $post['bank_name'] . ',银行账户类型:' . $post['bank_account_type'] . ',账户的冻结金额由原来的:' . $Account->frozen_cash . '元变为:' . $CashAccount->frozen_cash . '元'; $log->save(); \Yii::$app->db->createCommand()->update('yii_log', ['content' => Json::encode($log->attributes)], ['id' => $log->attributes['id']])->execute(); $t->commit(); return ['申请成功']; } catch (\Exception $e) { $t->rollBack(); throw new Exception($e->getMessage()); } } /** * @doc-name 平台申请提现操作 * @doc-param string apply_cash 申请提现金额 * @doc-param string bank_user_name 开户人姓名 * @doc-param string bank_card 银行卡号 * @doc-param string bank_name 开户行名称 * @doc-param int bank_account_type 银行账户类型 1:对公,2:对私,5:存折 * @doc-param string bank_no 银行联行号bank_account_type=1或5或非62开头的对私银行账户时必选 / optional * @doc-param int user_type 用户类型1诊所2总部 */ public function actionPlatformApply() { // 获取前端传递的POST数据 $post = \Yii::$app->request->post(); // 对POST数据进行验证,确保必要的字段不为空 $this->requestValidate($post, [ [['apply_cash', 'bank_user_name', 'bank_card', 'bank_name', 'bank_account_type', 'user_type'], 'required'] ]); // 检查申请提现金额是否大于等于3元 if ($post['apply_cash'] < 3) { throw new \yii\db\Exception('申请提现金额最少不能小于3元'); } // 获取当前用户信息 $user = \Yii::$app->user->identity; // 检查用户角色是否为超级管理员 if ($user->role != UserRoleEnum::SUPER_ADMIN) throw new \yii\base\Exception('您还不是超级管理员,没有权限申请打款'); // 检查用户类型是否正确 if ($post['user_type'] != 2) throw new \yii\db\Exception('用户类型错误'); // 对公账户和私人账户的额外验证 if ($post['bank_account_type'] == 1 || $post['bank_account_type'] == 5) { if (empty($post['bank_no'])) { throw new \yii\base\Exception('bank_no不能为空'); } } else { if (substr($post['bank_card'], 0, 2) != '62') { if (empty($post['bank_no'])) { throw new Exception('bank_no不能为空'); } } } // 数据库事务开始 $t = \Yii::$app->db->beginTransaction(); try { // 生成订单号 $order_no = 'DK' . time() . rand(111111, 999999); // 查询账户信息 $Account = CashAccount::find()->where([ 'user_type' => 2, 'user_id' => 0, ])->one(); if (!$Account) throw new Exception('账号不存在'); // 检查账户是否有足够的可提现金额 if (bcsub($Account->able_cash, $Account->frozen_cash, 2) < $post['apply_cash']) throw new Exception('sorry,您的账户没有那么多提现金额'); // 更新账户的冻结金额 $CashAccount = CashAccount::find()->where([ 'user_type' => 2, //平台 'user_id' => 0, ])->one(); $CashAccount->frozen_cash = bcadd($post['apply_cash'], $CashAccount->frozen_cash, 2); $CashAccount->last_apply_time = date('Y-m-d H:i:s', time()); $CashAccount->saveOrFail(); // 计算手续费 $pQianliuwu = bcdiv(0.65, 100, 4); $pShouxufei = bcadd(bcmul($post['apply_cash'], $pQianliuwu, 3), 0, 2); // TODO 暂时不计算手续费 $shouxufei = '0'; // 创建提现申请记录 $CashApply = new CashApply(); $CashApply->user_type = 2; $CashApply->user_id = 0; //用户ID $CashApply->order_no = $order_no; $CashApply->apply_cash = $post['apply_cash']; //申请提现金额 $CashApply->charge_cash = $shouxufei; //手续费 $CashApply->true_cash = bcsub($CashApply->apply_cash, $shouxufei, 2); //实际到账金额 $CashApply->apply_time = date('Y-m-d H:i:s', time()); $CashApply->saveOrFail(); // 记录代付的手续费 $chargeCpr = new ChargeCashPayRecord(); $chargeCpr->order_no = $order_no; $chargeCpr->store_id = 0;//诊所id $chargeCpr->charge_cash = $pShouxufei;//手续费 $chargeCpr->type = 2;//用户类型1诊所2总部 $chargeCpr->status = 1; $chargeCpr->saveOrFail(); // 记录操作日志 $log = new Log(); $log->admin_id = $user->uid; $log->operate_time = date('Y-m-d H:i:s', time()); $log->type = '平台申请打款'; $log->content = '平台ID:' . $user->uid . '申请打款:' . $post['apply_cash'] . '元,订单号为::' . $order_no . ',开户人姓名为:' . $post['bank_user_name'] . ',银行卡号:' . $post['bank_card'] . ',开户行名称:' . $post['bank_name'] . ',银行账户类型:' . $post['bank_account_type'] . ',账户冻结金额由原来的:' . $Account->frozen_cash . '元变为:' . $CashAccount->frozen_cash . '元'; $log->mold = 3; //财务操作 $log->save(); // 更新日志记录 \Yii::$app->db->createCommand()->update('yii_log', ['content' => Json::encode($log->attributes)], ['id' => $log->attributes['id']])->execute(); // 提交事务 $t->commit(); return ['申请成功']; } catch (\Exception $e) { // 回滚事务 $t->rollBack(); throw new Exception($e->getMessage()); } } /** * @doc-name 分账申请打款审核(超级管理员)--通过 * @doc-param int id 申请id * @doc-param int user_type 类型1诊所2总部 * @doc-param string check_result 审核结果 */ public function actionApplyAgree() { // 获取前端传递的POST数据 $post = \Yii::$app->request->post(); // 对POST数据进行验证,确保必要的字段不为空 $this->requestValidate($post, [ [['id', 'check_result', 'user_type'], 'required'], ]); // 获取当前用户身份 $user = \Yii::$app->user->identity; // 检查用户角色,如果不是超级管理员,则抛出异常 if ($user->role != UserRoleEnum::SUPER_ADMIN) throw new Exception('你不是超级管理员,没有分账申请打款审核权限'); // 根据POST数据查找现金申请信息,确保该申请存在且待审核 $CashApply = CashApply::find()->where([ 'id' => $post['id'], 'user_type' => $post['user_type'], 'check_status' => 1 ])->one(); if (!$CashApply) throw new Exception('申请不存在'); // 根据用户类型(诊所或平台)查找对应的账户信息和配置 if ($post['user_type'] == 1) { // 诊所 $Account = CashAccount::find()->where([ 'user_id' => $CashApply->user_id, 'user_type' => $CashApply->user_type ])->one(); $config = Store::find()->where(['id' => $CashApply->user_id])->one(); } else { // 平台 $Account = CashAccount::find()->where([ 'user_id' => 0, 'user_type' => $CashApply->user_type ])->one(); $config = Admin::find()->where(['role' => UserRoleEnum::SUPER_ADMIN])->one(); } // 确保诊所或平台的账号存在 if (!$Account) throw new Exception('诊所或平台的账号不存在'); // 再次确认现金账户信息 $CashAccount = CashAccount::find()->where([ 'user_type' => $CashApply->user_type, 'user_id' => $CashApply->user_id ])->one(); // 检查银行账户配置是否完整,如果有任一配置项为空,则解冻申请提现的金额,并记录审核失败 if (empty($config['bank_user_name']) || empty($config['bank_card']) || empty($config['bank_name']) || empty($config['bank_account_type'])) { $CashApply->check_id = $user->uid; $CashApply->check_status = 4; // 提现失败 $CashApply->check_result = '银行卡相关配置不完善'; $CashApply->check_time = date('Y-m-d H:i:s', time()); $CashApply->dakuan_status = -1; // 失败 $CashApply->dakuan_time = date('Y-m-d H:i:s', time()); $CashApply->saveOrFail(); // 创建日志记录,记录此次审核操作的详细信息 $log = new Log(); $log->admin_id = $user->uid; $log->operate_time = date('Y-m-d H:i:s', time()); $log->type = '分账申请打款审核'; // TODO 暂时关闭手续费 // $log->content = '超级管理员:' . $user->uid . '打款操作,订单号:' . $CashApply->order_no . ',打款金额:' . bcadd($CashApply->true_cash, 2, 2) . '元,开户人姓名:' . $config->bank_user_name . ',银行卡号:' . $config->bank_card . ',银行名称:' . $config->bank_name . ',银行账户类型:' . $config->bank_account_type . ',打款失败,原因:银行卡相关配置不完善'; $log->content = '超级管理员:' . $user->uid . '打款操作,订单号:' . $CashApply->order_no . ',打款金额:' . $CashApply->true_cash . '元,开户人姓名:' . $config->bank_user_name . ',银行卡号:' . $config->bank_card . ',银行名称:' . $config->bank_name . ',银行账户类型:' . $config->bank_account_type . ',打款失败,原因:银行卡相关配置不完善'; $log->mold = 3; // 财务操作 $log->saveOrFail(); // 更新日志内容到数据库 \Yii::$app->db->createCommand()->update('yii_log', ['content' => Json::encode($log->attributes)], ['id' => $log->attributes['id']])->execute(); throw new \yii\db\Exception('银行卡相关配置不能为空'); } $sendPrice = $CashApply->user_id == 0? bcadd($CashApply->true_cash, 0, 2): bcadd($CashApply->true_cash, 1, 2); // 准备打款参数 $param = [ 'order_no' => $CashApply->order_no, // 订单号 'apply_cash' => $sendPrice, // 实际打款金额 1元是银联手续费 自己的账户不需要手续费 'bank_name' => $config->bank_name, // 银行名称 'bank_account_type' => $config->bank_account_type, // 银行账户类型 'bank_no' => $config->bank_no, // 银行账号 'bank_user_name' => $config->bank_user_name, // 开户人姓名 'bank_card' => $config->bank_card, // 银行卡号 ]; // 执行打款操作 $res = EplPayService::getInstance()->withDraw($param); // 处理打款失败情况 if ($res['returnCode'] != '0000') { try { // 开始事务 $transaction = \Yii::$app->db->beginTransaction(); // 更新用户账户信息,解冻申请提现的金额 $CashAccount->frozen_cash = bcsub($CashAccount->frozen_cash, $CashApply->apply_cash, 2); $CashAccount->saveOrFail(); // 更新提现申请状态为失败 $CashApply->check_id = $user->uid; $CashApply->check_status = 4; // 提现失败状态码 $CashApply->check_result = $res['returnMsg']; $CashApply->check_time = date('Y-m-d H:i:s', time()); $CashApply->dakuan_status = -1; // 打款失败状态码 $CashApply->dakuan_time = date('Y-m-d H:i:s', time()); $CashApply->saveOrFail(); // 记录代付的手续费 $this->updateChargeCashPayRecordStatusByOrderNo($CashApply->order_no, 3); // 记录日志 $log = new Log(); $log->admin_id = $user->uid; $log->operate_time = date('Y-m-d H:i:s', time()); $log->type = '分账申请打款审核'; // TODO 暂时关闭手续费 // $log->content = '打款失败:超级管理员:' . $user->uid . '打款操作,订单号:' . $CashApply->order_no . ',打款金额:' . bcadd($CashApply->true_cash, 2, 2) . '元,开户人姓名:' . $config->bank_user_name . ',银行卡号:' . $config->bank_card . ',银行名称:' . $config->bank_name . ',银行账户类型:' . $config->bank_account_type . ',打款失败,原因:' . $res['returnMsg']; $log->content = '打款失败:超级管理员:' . $user->uid . '打款操作,订单号:' . $CashApply->order_no . ',打款金额:' . $CashApply->true_cash . '元,开户人姓名:' . $config->bank_user_name . ',银行卡号:' . $config->bank_card . ',银行名称:' . $config->bank_name . ',银行账户类型:' . $config->bank_account_type . ',打款失败,原因:' . $res['returnMsg']; $log->mold = 3; // 财务操作状态码 $log->saveOrFail(); // 更新日志表内容 \Yii::$app->db->createCommand()->update('yii_log', ['content' => Json::encode($log->attributes)], ['id' => $log->attributes['id']])->execute(); // 提交事务 $transaction->commit(); return $res; } catch (\Exception $e) { // 回滚事务 $transaction->rollBack(); throw new \yii\db\Exception($e->getMessage()); } } else { // 处理打款成功情况 $t = \Yii::$app->db->beginTransaction(); try { // 更新用户账户信息,扣除账户余额,解冻申请提现的金额,增加已提现金额和手续费 $CashAccount->able_cash = bcsub($CashAccount->able_cash, $CashApply->apply_cash, 2); // $CashAccount->frozen_cash = bcsub($CashAccount->frozen_cash, $CashApply->apply_cash, 2); // $CashAccount->withdrawn_cash = bcadd($CashAccount->withdrawn_cash, $CashApply->true_cash, 2); $CashAccount->charge_cash = bcadd($CashAccount->charge_cash, $CashApply->charge_cash, 2); $CashAccount->saveOrFail(); // 更新提现申请状态为成功 $CashApply->check_id = $user->uid; $CashApply->check_status = 2; // 提现成功状态码 $CashApply->check_result = $post['check_result']; $CashApply->check_time = date('Y-m-d H:i:s', time()); $CashApply->dakuan_status = 1; // 打款成功状态码 $CashApply->dakuan_time = date('Y-m-d H:i:s', time()); $CashApply->saveOrFail(); // 记录代付的手续费 $this->updateChargeCashPayRecordStatusByOrderNo($CashApply->order_no, 2, $param['apply_cash']); // 记录日志 $log = new Log(); $log->admin_id = $user->uid; $log->operate_time = date('Y-m-d H:i:s', time()); $log->type = '分账申请打款审核'; $log->content = $user->uid . '通过了ID为:' . $post['id'] . '的打款申请,打款结果:成功'; $log->mold = 3; // 财务操作状态码 $log->save(); // 更新日志表内容 \Yii::$app->db->createCommand()->update('yii_log', ['content' => Json::encode($log->attributes)], ['id' => $log->attributes['id']])->execute(); // 提交事务 $t->commit(); return ['审核成功']; } catch (\Exception $e) { // 回滚事务 $t->rollBack(); throw new Exception($e->getMessage()); } } } /** * @doc-name 分账申请打款审核(超级管理员)--拒绝 * @doc-param int id 申请id * @doc-param int user_type 类型1诊所2总部 * @doc-param string check_result 审核结果 */ public function actionApplyRefuse() { // 获取前端传递的POST数据 $post = \Yii::$app->request->post(); // 对POST数据进行验证 $this->requestValidate($post, [ [['id', 'check_result', 'user_type'], 'required'], ]); // 获取当前用户身份 $user = \Yii::$app->user->identity; // 检查用户角色,如果不是超级管理员则抛出异常 if ($user->role != UserRoleEnum::SUPER_ADMIN) throw new Exception('你不是超级管理员,没有分账申请打款审核权限'); // 根据ID和用户类型查找现金申请信息,如果状态为1(可能代表待审核),则获取该申请,否则抛出异常 $CashApply = CashApply::find()->where([ 'id' => $post['id'], 'user_type' => $post['user_type'], 'check_status' => 1, ])->one(); if (!$CashApply) throw new Exception('申请不存在'); // 查找用户的账号信息,如果不存在抛出异常 $Account = CashAccount::find()->where([ 'user_id' => $CashApply->user_id, 'user_type' => $CashApply->user_type ])->one(); if (!$Account) throw new Exception('平台或技术方的账号不存在'); // 开启数据库事务 $t = \Yii::$app->db->beginTransaction(); try { // 设置申请的审核人为当前用户,并将审核状态设置为拒绝,保存审核结果和时间 $CashApply->check_id = $user->uid; $CashApply->check_status = 3; // 拒绝状态 $CashApply->check_result = $post['check_result']; $CashApply->check_time = date('Y-m-d H:i:s', time()); $CashApply->saveOrFail(); // TODO 这里的逻辑会导致查询出来的账号信息永远是第一个用户的信息,需要修改 // $CashAccount = CashAccount::find()->where([ // 'user_type' => $CashApply->user_type // ])->one(); // 更新用户账号的冻结金额 $CashAccount = CashAccount::find()->where([ 'user_id' => $CashApply->user_id, 'user_type' => $CashApply->user_type ])->one(); $CashAccount->frozen_cash = bcsub($CashAccount->frozen_cash, $CashApply->apply_cash, 2); $CashAccount->saveOrFail(); // 记录代付的手续费 $this->updateChargeCashPayRecordStatusByOrderNo($CashApply->order_no, 3); // 记录操作日志 $log = new Log(); $log->admin_id = $user->uid; $log->operate_time = date('Y-m-d H:i:s', time()); $log->type = '分账申请打款审核'; $log->content = '管理员:' . $user->uid . '拒绝了ID为:' . $post['id'] . '的打款申请,可提现账户ID为:' . $Account->id . '的账户的冻结的金额由' . $Account->frozen_cash . '变为' . $CashAccount->frozen_cash; $log->mold = 3; // 财务操作 $log->save(); // 更新日志内容到数据库 \Yii::$app->db->createCommand()->update('yii_log', ['content' => Json::encode($log->attributes)], ['id' => $log->attributes['id']])->execute(); // 提交事务 $t->commit(); // 返回操作结果 return ['成功拒绝']; } catch (\Exception $e) { // 如果有异常则回滚事务,并抛出异常 $t->rollBack(); throw new Exception($e->getMessage()); } } /** * 更新代付记录状态 * * @param $orderNo * @param $status * @param string $chargeCash // 在status = 2的时候是冻结金额 * @return void * @throws Exception */ private function updateChargeCashPayRecordStatusByOrderNo($orderNo, $status, $chargeCash = '') { if (empty($orderNo) || empty($status)) throw new Exception('参数错误'); // 记录代付的手续费 $chargeCpr = $this->getChargeCashPayRecordStatusByOrderNo($orderNo); $chargeCpr->status = $status; $chargeCpr->saveOrFail(); } /** * 根据订单号获取代付记录 * * @param $orderNo * @return array|\yii\db\ActiveRecord|null */ public function getChargeCashPayRecordStatusByOrderNo($orderNo) { return ChargeCashPayRecord::find()->where([ 'status' => 1, 'order_no' => $orderNo ])->one(); } /** * @doc-name 分账打款 * @doc-param int apply_id 提现申请ID * @doc-param string order_no 订单号 * @doc-param string order_no 订单号 * @doc-param string bank_user_name 开户人姓名 * @doc-param string bank_card 银行卡号 * @doc-param string bank_name 银行名称 * @doc-param string bank_account_type 1对公2对私5存 */ public function actionPayment() { $post = \Yii::$app->request->post(); $user = \Yii::$app->user->identity; if ($user->role != UserRoleEnum::SUPER_ADMIN) throw new \yii\db\Exception('您不是超级管理员,没权限打款'); $this->requestValidate($post, [ [['apply_id', 'order_no', 'bank_user_name', 'bank_card', 'bank_name', 'bank_account_type'], 'required'] ]); $CashApply = CashApply::find()->where(['id' => $post['apply_id']])->one(); if (!$CashApply) throw new Exception('提现申请不存在'); if ($CashApply->check_status != 2) {//不是已通过状态 throw new Exception('提现申请不是已通过状态,您还不能打款'); } $CashAccount = CashAccount::find()->where([ 'user_type' => $CashApply->user_type, 'user_id' => $CashApply->user_id ])->one(); if (!$CashAccount) throw new Exception('账户不存在'); $post['apply_cash'] = $CashApply->apply_cash; $t = \Yii::$app->db->beginTransaction(); try { //打款 $res = EplPayService::getInstance()->withDraw($post); //打款失败 if ($res['returnCode'] != 0000) { $CashAccount->frozen_cash = bcsub($CashAccount->frozen_cash, $CashApply->apply_cash, 2);//申请提现冻结的金额 // $CashAccount->able_cash=bcadd($CashAccount->able_cash,$CashApply->apply_cash,2);//可提现金额 $CashAccount->saveOrFail(); $CashApply->dakuan_status = -1; $CashApply->dakuan_time = date('Y-m-d H:i:s', time()); $CashApply->saveOrFail(); $log = new Log(); $log->admin_id = $user->uid; $log->operate_time = date('Y-m-d H:i:s', time()); $log->type = '超级管理员'; $log->content = '打款失败:超级管理员:' . $user->uid . '打款操作,订单号:' . $post['order_no'] . ',打款金额:' . $post['apply_cash'] . '元,开户人姓名:' . $post['bank_user_name'] . ',银行卡号:' . $post['bank_card'] . ',银行名称:' . $post['bank_name'] . ',银行账户类型:' . $post['bank_account_type'] . '打款失败:失败原因' . $res; $log->mold = 3;//财务操作 $log->saveOrFail(); \Yii::$app->db->createCommand()->update('yii_log', ['content' => Json::encode($log->attributes)], ['id' => $log->attributes['id']])->execute(); return $res; } //打款成功 $CashAccount->able_cash = bcsub($CashAccount->able_cash, $CashApply->apply_cash, 2);//账户余额 $CashAccount->frozen_cash = bcsub($CashAccount->frozen_cash, $CashApply->apply_cash, 2);//申请提现冻结的金额 $CashAccount->withdrawn_cash = bcadd($CashAccount->withdrawn_cash, $CashApply->apply_cash, 2);//已提现金额 $CashAccount->charge_cash = bcadd($CashAccount->charge_cash, $CashApply->charge_cash, 2);//手续费 $CashAccount->saveOrFail(); $CashApply->dakuan_status = 1; $CashApply->dakuan_time = date('Y-m-d H:i:s', time()); $log = new Log(); $log->admin_id = $user->uid; $log->operate_time = date('Y-m-d H:i:s', time()); $log->type = '超级管理员'; $log->content = '打款成功:超级管理员:' . $user->uid . '打款操作:订单号:' . $post['order_no'] . ',打款金额:' . $post['apply_cash'] . '元,开户人姓名:' . $post['bank_user_name'] . ',银行卡号:' . $post['bank_card'] . ',银行名称:' . $post['bank_name'] . ',银行账户类型:' . $post['bank_account_type']; $log->mold = 3;//财务操作 $log->saveOrFail(); \Yii::$app->db->createCommand()->update('yii_log', ['content' => Json::encode($log->attributes)], ['id' => $log->attributes['id']])->execute(); $t->commit(); } catch (\Exception $e) { $t->rollBack(); throw new Exception($e->getMessage()); } } /** * @doc-name 分账资金流水 * @doc-param string type 1增2减 / optional * @doc-param string content 内容 / optional * @doc-return mixed @List{id,order_id-int-订单id,user_id-int-业务员id或仓库id其他为0,user_type-int-用户类型 1诊所 2平台,type-int-1增2减,amount-float-金额,content-string-内容,created_at-string-流水时间} 快递公司 * @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据 */ public function actionDivideFundWater() { $get = \Yii::$app->request->get(); $user = \Yii::$app->user->identity; $query = LedgerLog::find()->orderBy(['id' => SORT_DESC]); if ($user->role == UserRoleEnum::STORE_ADMIN) { $query->andWhere(['user_type' => 1]); } if ($user->role == UserRoleEnum::SUPER_ADMIN) { $query->andWhere(['user_type' => [1, 2]]); } if (!empty($get['type'])) { $query->andWhere(['type' => $get['type']]); } if (!empty($get['content'])) { $query->andWhere(['like', 'content', $get['content']]); } $this->field = [ LedgerLog::class => [ 'id', 'order_id', 'user_id', 'username' => function ($m) { return Admin::find()->select(['username'])->where(['uid' => $m->user_id])->column(); }, 'user_type', 'type', 'amount', 'content', 'created_at' => function ($m) { return date('Y-m-d H:i:s', $m->created_at); } ] ]; return $this->create($query, $get); } /** * @doc-name 首页数据 * @doc-return mixed @List{id,user_id-int-用户ID,total_cash-float-累计收益,able_cash-int-账户余额,frozen_cash-int-申请提现冻结的金额,withdrawn_cash-int-已提现金额,wait_cash-float-待结算收益,charge_cash-float-手续费,user_type-int-用户类型 1诊所 2平台,last_apply_time-string-最近提现申请时间,account-string-账户} 首页数据 * @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据 */ public function actionHomeData() { $get = \Yii::$app->request->get(); $QUERY = CashAccount::find(); $this->field = [ CashAccount::class => [ 'id', 'user_id', 'total_cash', 'able_cash', 'frozen_cash', 'withdrawn_cash', 'wait_cash', 'charge_cash', 'account' => function ($m) { if ($m->user_id == 0) { return Admin::find()->where(['role' => UserRoleEnum::SUPER_ADMIN])->one(); } else { return Store::find()->where(['id' => $m->user_id])->one(); } }, 'user_type', 'last_apply_time' ] ]; return $this->create($QUERY, $get); } /** * @doc-name 平台的提现账户 * @doc-return string user_id 用户 * @doc-return string total_cash 累计收益 * @doc-return string able_cash 账户余额 * @doc-return string frozen_cash 申请提现冻结的金额 * @doc-return string withdrawn_cash 待结算收益 * @doc-return string wait_cash 已提现金额 * @doc-return string charge_cash 手续费 * @doc-return string user_type 用户类型 1诊所 2平台 * @doc-return string last_apply_time 最近提现申请时间 */ public function actionPlatformAccount() { $get = \Yii::$app->request->get(); $user = \Yii::$app->user->identity; $CashAccount = CashAccount::find()->where(['user_id' => 0, 'user_type' => 2])->one(); if (!$CashAccount) throw new \yii\db\Exception('账户不存在'); $price=Ledger::find()->where(['status'=>0,'user_type'=>2,'user_id'=>0])->sum('money'); $CashAccount->total_cash=bcadd(bcadd($CashAccount->able_cash,$CashAccount->withdrawn_cash,2),$CashAccount->charge_cash,2); $CashAccount->wait_cash=$price; $CashAccount->saveOrFail(); return $CashAccount; } /** * @doc-name 诊所提现账户 * @doc-param int store_id 诊所ID * @doc-return string user_id 用户 * @doc-return string total_cash 累计收益 * @doc-return string able_cash 账户余额 * @doc-return string frozen_cash 申请提现冻结的金额 * @doc-return string withdrawn_cash 待结算收益 * @doc-return string wait_cash 已提现金额 * @doc-return string charge_cash 手续费 * @doc-return string user_type 用户类型 1诊所2平台 * @doc-return string last_apply_time 最近提现申请时间 */ public function actionStoreAccount() { $get = \Yii::$app->request->get(); $this->requestValidate($get, [ ['store_id', 'required'], ]); $store = Store::find()->where(['id' => $get['store_id']])->one(); if (!$store) throw new \yii\db\Exception('诊所不存在'); $CashAccount = CashAccount::find()->where(['user_id' => $get['store_id'], 'user_type' => 1])->one(); if (!$CashAccount) throw new \yii\db\Exception('账户不存在'); $price=Ledger::find()->where(['status'=>0,'user_type'=>1,'user_id'=>$CashAccount->user_id])->sum('money'); $CashAccount->total_cash=bcadd(bcadd($CashAccount->able_cash,$CashAccount->withdrawn_cash,2),$CashAccount->charge_cash,2); $CashAccount->wait_cash=$price??0; $CashAccount->saveOrFail(); return $CashAccount; } /** * @doc-name 分账--审核管理列表 * @doc-param int status 1待审核2审核通过3审核拒绝 * @doc-return mixed @List{id,user_id-int-用户ID,user_type-int-用户类型1诊所2总部,order_no-int-订单,apply_cash-float-申请提现金额,true_cash-float-实际到账金额,charge_cash-string-手续费,check_id-string-关联admin表 审核人ID,check_status-string-审核状态0待审核1审核中2已通过3已拒绝,check_result-string-审核结果/拒绝原因,check_time-string-审核时间,apply_time-string-申请时间,dakuan_status-string-打款状态-1失败0未知1成功,dakuan_time-string-打款时间,payee-string-收款人} 审核管理列表 * @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据 */ public function actionCheckManage() { $get = \Yii::$app->request->get(); $this->requestValidate($get, [ ['status', 'required'] ]); $query = CashApply::find()->orderBy(['id' => SORT_DESC]); switch ($get['status']) { case 1: $query->andWhere(['check_status' => [0, 1]]); break; case 2: $query->andWhere(['check_status' => 2]); break; case 3: $query->andWhere(['check_status' => 3]); break; default: throw new \yii\db\Exception('参数错误'); } if(!empty($get['start_time']) && !empty($get['end_time'])){ $start_time=strtotime($get['start_time'].' '.'00:00:00'); $end_time=strtotime($get['end_time'].' '.'23:59:59'); $query->andWhere(['between','created_at',$start_time,$end_time]); } if(!empty($get['name'])){ if($get['name'] == '平台'){ $query->andWhere(['user_id' => 0]); }else{ $store = Store::find()->where(['name'=>$get['name']])->asArray()->one(); if($store){ $query->andWhere(['user_id' => intval($store['id'])]); }else{ $query->andWhere(['user_id' => -1]); } } } $this->field = [ CashApply::class => [ 'id', 'user_id', 'user_type', 'order_no', 'apply_cash', 'true_cash', 'charge_cash', 'check_id', 'check_status', 'check_result', 'check_time', 'apply_time', 'dakuan_status', 'dakuan_time', 'payee' => function ($m) { if ($m->user_id == 0) { return Admin::find()->where(['role' => UserRoleEnum::SUPER_ADMIN])->one(); } else { return Store::find()->where(['id' => $m->user_id])->one(); } } ] ]; return $this->create($query, $get); } /** * @doc-name 导出提现审核 */ public function actionExportWithDraw() { \Yii::$app->response->format = Response::FORMAT_RAW; $params = \Yii::$app->request->get(); $parameter['header'] = ['订单号', '平台/诊所', '收款人', '申请提现金额', '审核状态', '时间']; $parameter['data'] = CashApply::inventory($params,\Yii::$app->user->identity->store_id); ExportService::ExportByCors($parameter); } /** * @doc-name 分账--财务数据 * @doc-param float total_cash 累计收益 * @doc-param float able_cash 账户余额 * @doc-param float withdrawn_cash 已体现 * @doc-param float charge_cash 手续费 * @doc-param float wait_cash 待结算 */ public function actionFenzhangFinance() { //总收入 $total_price=CashAccount::find()->sum('total_cash'); //账户余额 $account_price=CashAccount::find()->sum('able_cash'); $withdrawn_cash=CashAccount::find()->sum('withdrawn_cash'); $charge_cash=CashAccount::find()->sum('charge_cash'); // $wait_cash=CashAccount::find()->sum('wait_cash'); $wait_cash=Ledger::find()->where(['status'=>0])->sum('money'); return [ 'total_cash'=>$total_price, 'able_cash'=>$account_price, 'withdrawn_cash'=>$withdrawn_cash, 'charge_cash'=>$charge_cash, 'wait_cash'=>$wait_cash??0, ]; } /** * @doc-name 分账--财务数据的明细 */ public function actionFenzhangFinanceDetail() { $CashApply=CashApply::find()->orderBy(['id'=>SORT_DESC])->all(); $Ledger=Ledger::find()->orderBy(['id'=>SORT_DESC])->with(['productOrder'])->asArray()->all(); return [ 'CashApply'=>$CashApply, 'Ledger'=>$Ledger, ]; } /** * @doc-name 平台/诊所提现管理 * @doc-return mixed @List{id,user_id-int-用户ID,user_type-int-用户类型1诊所2总部,order_no-int-订单,apply_cash-float-申请提现金额,true_cash-float-实际到账金额,charge_cash-string-手续费,check_id-string-关联admin表 审核人ID,check_status-string-审核状态0待审核1审核中2已通过3已拒绝,check_result-string-审核结果/拒绝原因,check_time-string-审核时间,apply_time-string-申请时间,dakuan_status-string-打款状态-1失败0未知1成功,dakuan_time-string-打款时间,payee-string-收款人} 平台/诊所提现管理 * @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据 */ public function actionWithdrawnManage() { $get = \Yii::$app->request->get(); $user = \Yii::$app->user->identity; $order_no=$get['order_no']; $query = CashApply::find()->alias('c')->orderBy(['id' => SORT_DESC]); if (!empty($order_no)){ $query->andWhere(['c.order_no'=>$order_no]); } if (!empty($get['start_time']) && !empty($get['end_time'])){ $query->andWhere(['between','c.apply_time',$get['start_time'],$get['end_time']]); } if ($user->role==UserRoleEnum::SUPER_ADMIN){//平台 $query->andWhere(['c.user_id'=>0,'c.user_type'=>2]); }else{ $query->andWhere(['c.user_id'=>$user->store_id,'c.user_type'=>1]); } $this->field = [ CashApply::class => [ 'id', 'user_id', 'user_type', 'order_no', 'apply_cash', 'true_cash', 'charge_cash', 'check_id', 'check_status', 'check_result', 'check_time', 'apply_time', 'dakuan_status', 'dakuan_time', 'payee' => function ($m) { if ($m->user_id == 0) { return Admin::find()->where(['role' => UserRoleEnum::SUPER_ADMIN])->one(); } else { return Store::find()->where(['id' => $m->user_id])->one(); } }, 'type' => function ($c) {//结算类别 return '提现记录'; }, 'status' => function ($ca) { if ($ca->check_status == 1 ) { return '审核中'; } elseif($ca->check_status == 2) { return '审核通过,提现成功'; }elseif($ca->check_status == 3) { return '申请已拒绝,手续费未扣'; }elseif($ca->check_status == 4 ) { return '审核通过,提现失败'; }else{ return '未知'; } }, 'tixian_person'=>function($m){ if ($m->user_type==1)return '诊所'; if ($m->user_type==2)return '平台'; }, ], ]; return $this->create($query, $get); } /** * @doc-name 平台/诊所结算列表 * @doc-param string order_no 产品订单号 * @doc-param string register_no 挂号订单号 * @doc-param string start_time 开始时间 * @doc-param string end_time 结束时间 * @doc-return mixed @List{id,user_id-int-用户ID,user_type-int-用户类型1诊所2总部,order_no-int-订单,order_id-int-订单id,su_id-int-医生id,drugstore_id-int-仓库id,drug_id-string-药品id,status-string-结算状态 0待结算 1已结算 2已取消,money-string-分佣金额,time-string-时间} 平台/诊所是否结算管理 * @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据 */ public function actionJiesuanList() { $get = \Yii::$app->request->get(); $user = \Yii::$app->user->identity; $order_no=$get['order_no']?? ''; $register_no=$get['register_no']?? ''; $start_time=strtotime($get['start_time']); $end_time=strtotime($get['end_time']); $query = Ledger::find()->alias('l')->where(['l.status'=>[0,1]])->orderBy(['l.id' => SORT_DESC]); if (!empty($order_no)){ $query->joinWith(['productOrder'=>function($p)use($order_no){ $p->where(['order_no'=>$order_no]); }]); } if (!empty($register_no)){ $query->joinWith(['register'=>function($p)use($register_no){ $p->where(['order_no'=>$register_no]); }]); } if (!empty($start_time) && !empty($end_time)){ if ($start_time==$end_time){//筛选某一天 $time=FuncHelper::getDayBE($get['start_time']); $end_time=$time[1]; } $query->andWhere(['between','l.created_at',$start_time,$end_time]); } if ($user->role==UserRoleEnum::SUPER_ADMIN){//平台 $query->andWhere(['l.user_id'=>0,'l.user_type'=>2])->groupBy('l.order_id,l.order_type'); }else{ $query->andWhere(['l.user_id'=>$user->store_id,'l.user_type'=>1])->groupBy('l.order_id,l.order_type'); } $this->field = [ Ledger::class => [ 'id', 'user_id','order_id', 'user_type','su_id', 'drugstore_id', 'drug_id', 'status', 'money'=> function ($m) { return $this->actionTotal($m->order_id,$m->user_type,$m->order_type); }, 'order_no'=> function ($order) { if ($order->order_type == 1) { return ProductOrder::find()->select(['order_no'])->where(['id' => $order->order_id])->column(); } if ($order->order_type == 2) { return Register::find()->select(['order_no'])->where(['id' => $order->order_id])->column(); } }, 'type'=>function($l){ if ($l->order_type==1){ return '产品订单'; }else{ return '挂号订单'; } }, 'fee_type'=>function($f){ if ($f->fee_type==1){ return '药品费用'; }elseif($f->fee_type==2){ return '挂号费用'; }elseif($f->fee_type==3){ return '快递费用'; }elseif($f->fee_type==4){ return '代煎费用'; }else{ return '其他'; } }, 'price_from'=>'productOrder.user.nickname', 'time'=>function($le){ return date('Y-m-d H:i:s',$le->created_at); } ], ]; return $this->create($query, $get); } /** * 获取結算订单金额 * * @param $order_id * @param $user_type * @param $order_type * @return bool|int|mixed|string */ public function actionTotal($order_id='',$user_type=1,$order_type='') { $money = Ledger::find()->alias('l')->where(['l.order_id' => $order_id, 'l.status' => [0, 1]])->andWhere(['l.user_type'=>$user_type,'l.order_type'=>$order_type])->sum('money'); return $money ?? 0; } }