Files
xk-api-yii/admin/controllers/base/DivideAccountController.php
2024-09-19 11:32:39 +08:00

916 lines
41 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace admin\controllers\base;
use admin\components\BaseAdminController;
use admin\controllers\AuthController;
use admin\models\Admin;
use common\enums\UserRoleEnum;
use common\helpers\FuncHelper;
use common\models\CashAccount;
use common\models\CashApply;
use common\models\Ledger;
use common\models\LedgerLog;
use common\models\Log;
use common\models\Register;
use common\models\Store;
use common\modelsgii\ProductOrder;
use common\services\EplPayService;
use yii\base\Exception;
use yii\helpers\Json;
use yii\web\Response;
use common\services\ExportService;
/**
* @doc-module 分账
*/
class DivideAccountController extends BaseAdminController
{
/**
* @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 actionStoreApply()
{
$post = \Yii::$app->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();
$Platform= CashAccount::find()->where([
'user_id' => 0,
'user_type' => 2
])->one();
// $CashAccount->able_cash = bcsub($CashAccount->able_cash, $post['apply_cash'], 2);
$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();
$qianliuwu=bcdiv(0.65,100,4);
$shouxufei=bcadd(bcmul($post['apply_cash'],$qianliuwu,2),2,3);
// $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();
$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 = \Yii::$app->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::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->able_cash = bcsub($CashAccount->able_cash, $post['apply_cash'], 2);
$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();
$qianliuwu=bcdiv(0.65,100,4);
$shouxufei=bcadd(bcmul($post['apply_cash'],$qianliuwu,2),2,3);
$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();
$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 = \Yii::$app->request->post();
$this->requestValidate($post, [
[['id', 'check_result', 'user_type'], 'required'],
]);
$user = \Yii::$app->user->identity;
if ($user->role != UserRoleEnum::SUPER_ADMIN) throw new Exception('你不是超级管理员,没有分账申请打款审核权限');
$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'])){
$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 = '银行卡相关配置不完善';
$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 = '分账申请打款审核';
$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->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('银行卡相关配置不能为空');
}
$param=[
'order_no'=>$CashApply->order_no,
'apply_cash'=>bcadd($CashApply->true_cash,1,2),
'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();
$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 . '打款操作,订单号:' . $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->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->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 = '分账申请打款审核';
$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 = \Yii::$app->request->post();
$this->requestValidate($post, [
[['id', 'check_result', 'user_type'], 'required'],
]);
$user = \Yii::$app->user->identity;
if ($user->role != UserRoleEnum::SUPER_ADMIN) throw new Exception('你不是超级管理员,没有分账申请打款审核权限');
$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();
$CashAccount = CashAccount::find()->where([
'user_type' => $CashApply->user_type
])->one();
$CashAccount->frozen_cash = bcsub($CashAccount->frozen_cash, $CashApply->apply_cash, 2);
$CashAccount->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 . '拒绝了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());
}
}
/**
* @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-医生iddrugstore_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);
}
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;
}
}