| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053 |
- <?php
- namespace api\modules\v1\controllers\userwages;
- use addons\AvoidTax\common\models\TaxUserAuth;
- use addons\AvoidTax\common\service\UserService;
- use addons\AvoidTax\common\service\XinlongService;
- use addons\Salary\common\logic\PushLogic;
- use addons\Salary\common\logic\UserLogic;
- use addons\Salary\common\models\SalaryMerchant;
- use addons\Salary\common\models\SalaryUserLog;
- use addons\Salary\common\services\WyaServices;
- use addons\XiaoEtong\common\logic\XiaoetongLogic;
- use addons\XiaoEtong\common\models\XiaoEtongUser;
- use common\enums\ApiStatusEnum;
- use common\enums\StatusEnum;
- use common\enums\WithdrawWayEnum;
- use common\globals\GlobalInvoke;
- use common\helpers\ArrayHelper;
- use common\helpers\DateHelper;
- use common\helpers\sms\Sms;
- use common\models\common\CapitalLog;
- use common\models\common\CapitalLogRelation;
- use common\models\common\CommissionLog;
- use common\models\common\CommissionMonthBilling;
- use common\models\common\CommissionYearsBilling;
- use common\models\common\PlatformSetting;
- use common\models\common\UserBankCard;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use common\models\user\UserWallet;
- use common\models\user\UserWithdrawLog;
- use yii\helpers\Json;
- use Yii;
- class UserController extends BaseController
- {
- public $modelClass = '';
- protected $authOptional = ['index', 'commission-list', 'commission-total', 'cash-list', 'cash-detail', 'billing-statistics', 'cash-setting',
- 'withdraw', 'billing-contrast','check-sign','go-to-sign','bind-card','create-account','set-login-password','register-sms-code','month-billing','get-sign-url'
- ];
- public function init()
- {
- parent::init(); // TODO: Change the autogenerated stub
- // $headers = \Yii::$app->request->headers;
- // $mall_sign = '';
- // if (!empty($headers['x-mall-sign'])) $mall_sign = $headers['x-mall-sign'];
- // if (!empty(\Yii::$app->request->get('mall-sign'))) $mall_sign = \Yii::$app->request->get('mall-sign');
- // if (empty($mall_sign)) {
- // return;
- // }
- // $mall = Mall::getMallInfo($mall_sign);
- // if (empty($mall)) {
- // return;
- // }
- // $withdraw_income = \Yii::$app->services->setting->mall->get($mall['id'], 'withdraw_income');
- // if (!empty($withdraw_income['is_login'])) {
- // $this->authOptional = [];
- // }
- }
- //首页
- public function actionIndex()
- {
- $user_wallet = UserWallet::getOne([['user_id' => $this->userwages['id']], ['status' => StatusEnum::ENABLED]], true);
- $data = [
- 'commission_withdrawal' => 0,
- 'commission' => 0,
- 'frozen_commission' => 0,
- 'total_commission' => 0,
- 'commission_list' => [],
- ];
- if (!empty($user_wallet)) {
- $data['commission'] = $user_wallet['commission'];
- $data['commission_withdrawal'] = $user_wallet['commission_withdrawal'];
- $data['frozen_commission'] = $user_wallet['frozen_commission'];
- $data['total_commission'] = $user_wallet['total_commission'];
- }
- $commission_list = CommissionLog::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['user_id' => $this->userwages['id']]
- ],
- 'limit' => '3',
- 'order' => 'id desc',
- 'select' => 'id,change_num,desc,change_type,created_at'
- ]);
- if (!empty($commission_list)) {
- foreach ($commission_list as &$val) {
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- if ($val['change_type'] == 'add') {
- $val['change_type_text'] = '销售收入';
- } else {
- $val['change_type_text'] = '收入提现';
- }
- }
- $data['commission_list'] = $commission_list;
- }
- $data['nickname'] = $this->userwages['nickname'];
- $data['mobile'] = $this->userwages['mobile'];
- $data['avatar_url'] = $this->userwages['avatar_url'];
- $config = Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic');
- $data['is_eject'] = 0;// 是否已签约,0=否,1=是
- $data['is_popup'] = $config['is_sign'] ?? 0;// 是否强制弹窗,0=否,1=是
- $data['url'] = '';
- $data['accountId'] = '';
- $data['flowId'] = '';
- $data['pic_url'] = $config['pic_url'] ?? '';
- $data['link_text'] = $config['link_text'] ?? '-- 七件事集团技术支持 --';
- $data['is_sign'] = 0;
- $data['idcard'] = '';
- if(empty($config['compliance'])){
- $compliance = 'AvoidTax';
- }else{
- $compliance =$config['compliance'];
- }
- $data['compliance'] = $compliance;
- if(MallAddons::isInstall($this->mall_id,$compliance)){
- switch ($compliance){
- case 'AvoidTax':
- $TaxUserAuthModel = TaxUserAuth::find()->where(['user_id' => $this->userwages['id']])->one();
- if (!empty($TaxUserAuthModel)) {
- $system = Yii::$app->services->setting->platform->get('system');
- $redirect_url = ($system['cat_h5_url'] ?? '') . '/#/pages/userWages/index?sign=' . $this->mall['mall_sign'];
- if ($TaxUserAuthModel['status'] == TaxUserAuth::STATUS_NOT) {
- $service = new XinlongService(['mall_id' => $this->mall_id]);
- $url = $service->goToSign($TaxUserAuthModel, $redirect_url);
- $data['url'] = $url;
- $data['accountId'] = $TaxUserAuthModel->accountId;
- $data['flowId'] = $TaxUserAuthModel->flowId;
- } elseif ($TaxUserAuthModel['status'] == TaxUserAuth::STATUS_WAIT) {
- // 开账户
- $service = new XinlongService(['mall_id' => $this->mall_id]);
- $auth_user_model = $service->createAccount($this->userwages['id'], $TaxUserAuthModel['mobile'], $TaxUserAuthModel['id_card_no'], $TaxUserAuthModel['name']);
- // 重新生成签约链接
- $url = $service->goToSign($auth_user_model, $redirect_url);
- $data['url'] = $url;
- $data['accountId'] = $TaxUserAuthModel->accountId;
- $data['flowId'] = $TaxUserAuthModel->flowId;
- } else {
- $data['is_eject'] = 1;
- }
- }
- break;
- case 'XiaoEtong':
- $model = XiaoEtongUser::find()->where(['mall_id' => $this->mall_id, 'user_id' =>$this->userwages['id'], 'status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]])->one();
- if (empty($model)){
- $data['is_sign'] = 0;
- $data['idcard'] = '';
- $data['url'] = '';
- }else{
- $data['idcard'] = $model->idcard;
- if (empty($model->is_sign)) {
- $is_sign = (new XiaoetongLogic())->checkIsSign($this->mall_id, $this->userwages['id'], $model->idcard);
- $data['is_sign'] = $is_sign;
- $data['url'] = (new XiaoetongLogic())->getSignUrl($this->mall_id,$this->userwages['id'], $model->idcard, $model->name, $model->mobile,0,$is_sign);
- }else{
- $data['is_sign'] = $model->is_sign;
- $data['url'] = '';
- }
- }
- break;
- }
- }
- return $this->success('成功', $data);
- }
- //填写资料
- public function actionCreateAccount(){
- $params = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($params, [
- [['mobile','id_card','name'], 'required', 'message' => '参数丢失'],
- ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $config = Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic');
- if(empty($config['compliance'])){
- $compliance = 'AvoidTax';
- }else{
- $compliance =$config['compliance'];
- }
- switch ($compliance){
- case 'AvoidTax':
- $service = new XinlongService(['mall_id'=>$this->mall_id]);
- // 开账户
- $auth_user_model = $service->createAccount($this->userwages['id'],$params['mobile'],$params['id_card'],$params['name']);
- if($auth_user_model == false) return $this->error($service->error);
- $url = $service->goToSign($auth_user_model);
- return $url === false
- ? $this->error($service->error)
- :$this->success('成功', [
- 'url' => $url,
- 'accountId' => $auth_user_model->accountId,
- 'flowId' => $auth_user_model->flowId,
- ]);
- break;
- case 'XiaoEtong':
- try {
- $model = XiaoEtongUser::findOne(['mall_id' => $this->mall_id, 'user_id' => $this->userwages['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) {
- $is_batch_import = 1;
- $is_sign = 0;
- }else{
- $is_sign = $model->is_sign;
- $is_batch_import = 0;
- }
- $idcard = $params['id_card'];
- $name = $params['name'];
- $mobile = $params['mobile'];
- $url = (new XiaoetongLogic())->getSignUrl($this->mall_id, $this->userwages['id'], $idcard, $name, $mobile,$is_batch_import,$is_sign);
- return $this->success('', ['url' => $url,'is_sign'=>$is_sign]);
- } catch (\Exception $e) {
- return $this->error($e->getMessage().$e->getFile().$e->getLine());
- }
- break;
- }
- }
- public function actionRegisterSmsCode()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['mobile'], 'required', 'message' => '请输入手机号码'],
- [['mobile','area_code'], 'integer'],
- [['sms_type'], 'string'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $area_code = '';// 初始化区号
- $gateways = 'domestic';// 默认国内短信
- // 检测是登录并且区号不为空则使用国际短信
- if (!empty($this->userwages) && !empty($this->userwages->area_code)) {
- $gateways = 'international';
- $area_code = $this->userwages->area_code;
- }
- // 判断是否有区号
- if (!empty($params['area_code']) && $params['area_code'] != 86) {
- $gateways = 'international';
- $area_code = $params['area_code'];
- }
- $params['sms_type'] = $params['sms_type'] ?: '';
- $sms = new Sms($this->mall_id, $gateways);
- try {
- if ($sms->sendCaptcha($params['mobile'], $area_code,$params['sms_type']) === false) return $this->error('发送失败');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- return $this->success('发送成功');
- }
- public function actionSetLoginPassword()
- {
- try {
- if (\Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['mobile', 'captcha', 'password', 'confirm_password'], 'required'],
- ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
- [['mobile'], 'integer'],
- ['password', 'string', 'length' => [6, 11]],
- ['confirm_password', 'compare', 'compareAttribute' => 'password', 'message' => '两次密码不一致'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $user = $this->userwages;
- if($user['mobile']!=$params['mobile']) return $this->error('请输入绑定的手机号码');
- //短信验证码
- if (YII_ENV == 'prod') {
- if (!Sms::checkValidateCode($params['mobile'], $params['captcha'],Sms::SMS_TYPE_UPDATE_PASS)) return $this->error('验证码不正确');
- }
- $user = User::findOne(['id'=>$user['id']]);
- $user->password = Yii::$app->getSecurity()->generatePasswordHash(trim($params['password']));
- if ($user->save() === false) return $this->error(User::getStaticError());
- return $this->success('设置成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- //待结算明细与收益明细
- public function actionCommissionList()
- {
- $params = \Yii::$app->request->get();
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['user_id' => $this->userwages['id']];
- if (!empty($params['type']) && $params['type'] == 'frozen') {
- CapitalLog::$capitalType = "commission_frozen";
- $where[] = ['status' => StatusEnum::DISABLED];
- } else {
- CapitalLog::$capitalType = "commission";
- }
- if (!empty($params['date'])) {
- $start_time = strtotime($params['date'] . ' 00:00:00');
- $end_time = strtotime(date('Ymd', strtotime($params['date'] . '+1 month -1 day')) . ' 23:59:59');
- $where[] = ['>=', 'created_at', $start_time];
- $where[] = ['<=', 'created_at', $end_time];
- }
- $params['where'] = $where;
- $params['select'] = 'id,change_num,desc,change_type,created_at';
- $list = CapitalLog::listPage(array_merge($params, ['order' => 'id desc']));
- if (!empty($list['list'])) {
- $ids = array_column($list['list'], 'id');
- CapitalLogRelation::$capitalType = "commission";
- $capitalLog_relation_list = CapitalLogRelation::lists([
- 'where' => [
- ['log_id' => $ids],
- ['source_type' => CapitalLog::$capitalType]
- ],
- 'index' => 'log_id'
- ]);
- foreach ($list['list'] as &$val) {
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- if ($val['change_type'] == 'add') {
- $val['change_type_text'] = '销售收入';
- } else {
- $val['change_type_text'] = '收入提现';
- }
- if(isset($capitalLog_relation_list[$val['id']])){
- $val['desc'] = $capitalLog_relation_list[$val['id']]['describe'];
- }
- }
- }
- return $this->success('操作成功', $list);
- }
- //待结算明细统计
- public function actionCommissionTotal()
- {
- $params = \Yii::$app->request->post();
- CapitalLog::$capitalType = "commission_frozen";
- if (!empty($params['type']) && $params['type'] == 'frozen') {
- CapitalLog::$capitalType = "commission_frozen";
- } else {
- CapitalLog::$capitalType = "commission";
- }
- $query = CapitalLog::find()->where(['mall_id' => $this->mall_id, 'user_id' => $this->userwages['id']]);
- if (!empty($params['date'])) {
- $start_time = strtotime($params['date'] . ' 00:00:00');
- $end_time = strtotime(date('Ymd', strtotime($params['date'] . '+1 month -1 day')) . ' 23:59:59');
- $query->andWhere(['>=', 'created_at', $start_time]);
- $query->andWhere(['<=', 'created_at', $end_time]);
- }
- if (!empty($params['type']) && $params['type'] == 'frozen') {
- CapitalLog::$capitalType = "commission_frozen";
- $query->andWhere(['status' => StatusEnum::DISABLED]);
- $add_total = $query->sum('change_num');
- return $this->success('操作成功', ['add_total' => $add_total, 'sub_total' => 0]);
- } else {
- $query_copy = clone $query;
- $add_total = $query->andWhere(['change_type' => 'add'])->sum('change_num');
- $sub_total = $query_copy->andWhere(['change_type' => 'sub'])->sum('change_num');
- return $this->success('操作成功', ['add_total' => $add_total, 'sub_total' => $sub_total ?? 0]);
- }
- }
- public function actionCashDetail()
- {
- $user_wallet = UserWallet::findOne(['mall_id' => $this->mall_id, 'user_id' => $this->userwages['id']]);
- $data['commission'] = $user_wallet['commission'] ?? 0;//可用佣金
- $start_time = strtotime(date('Y-m-1', time()) . ' 00:00:00');
- $end_time = strtotime(date('Y-m-t', time()) . ' 23:59:59');
- $where = ['user_id' => $this->userwages['id'], 'mall_id' => $this->mall_id, 'from' => 1, 'status' => [UserWithdrawLog::STATUS_ALREADY_TRANS]];
- $commission_withdrawal = UserWithdrawLog::find()->where($where)
- ->sum('num');
- $data['commission_withdrawal'] = $commission_withdrawal ?? 0;//已提现得总佣金
- $month_num = UserWithdrawLog::find()->where($where)
- ->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time])
- ->sum('num');
- $monday_time = strtotime(date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)) . ' 00:00:00');
- $sunday_time = strtotime(date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600)) . ' 23:59:59');
- $week_num = UserWithdrawLog::find()->where($where)
- ->andWhere(['>=', 'created_at', $monday_time])->andWhere(['<=', 'created_at', $sunday_time])
- ->sum('num');
- $data['month_num'] = $month_num ?? 0;
- $data['week_num'] = $week_num ?? 0;
- return $this->success('操作成功', $data);
- }
- //提现明细
- public function actionCashList()
- {
- $params = \Yii::$app->request->get();
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['user_id' => $this->userwages['id']];
- $where[] = ['from' => 1];
- if (isset($params['status']) && $params['status'] !== '') $where[] = ['status' => $params['status']];
- $params['where'] = $where;
- $params['select'] = 'id,mall_id,user_id,order_no,num,actual_num,poundage_num,poundage_ratio,content,status,type';
- $params['order'] = 'id desc';
- $list = UserWithdrawLog::listPage($params);
- $list['status_map'] = UserWithdrawLog::$status_msg;
- $list['type_map'] = UserWithdrawLog::$type_msg;
- return $this->success('操作成功', $list);
- }
- public function actionWithdrawLogList()
- {
- $params = \Yii::$app->request->get();
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['user_id' => $this->userwages['id']];
- if (isset($params['status'])) {
- $where[] = ['=', 'status', $params['status']];
- }
- $params['where'] = $where;
- // $params['select'] = 'id,change_num,desc,change_type,created_at';
- $list = UserWithdrawLog::listPage(array_merge($params, ['order' => 'id desc']));
- if (!empty($list['list'])) {
- foreach ($list['list'] as &$val) {
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- }
- }
- return $this->success('操作成功', $list);
- }
- //账单统计
- public function actionBillingStatistics()
- {
- $params = \Yii::$app->request->post();
- $type = $params['type'] ?? 'month';
- switch ($type) {
- case 'month':
- $result = $this->monthBilling($params);
- return $this->success('操作成功', $result);
- case 'year':
- $result = $this->yearBilling($params);
- return $this->success('操作成功', $result);
- }
- }
- protected function monthBilling($params)
- {
- $current_month = date('Y-m', time());
- if (!empty($params['month'])) {
- $month = $params['month'];
- } else {
- $month = $current_month;
- }
- $data = [
- 'income_nums' => 0,
- 'expenditure_nums' => 0,
- 'expenditure_money' => 0,
- 'income_money' => 0,
- ];
- if ($month == $current_month) {
- //当前月
- $user_id = $this->userwages['id'];
- $start_time = strtotime($current_month . '-01 00:00:00');
- $sql = "SELECT mall_id,user_id,change_type,sum(change_num) as SUM3,count(*) as COUNT4
- FROM qimall_commission_log where created_at>={$start_time} and user_id ={$user_id} GROUP BY mall_id,user_id,change_type";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- switch ($item['change_type']) {
- case 'add':
- $data['income_money'] = $item['SUM3'];
- $data['income_nums'] = $item['COUNT4'];
- break;
- case 'sub':
- $data['expenditure_money'] = $item['SUM3'];
- $data['expenditure_nums'] = $item['COUNT4'];
- break;
- }
- }
- } else {
- $where = [
- ['mall_id' => $this->mall_id], ['user_id' => $this->userwages['id']], ['month' => $month], ['status' => StatusEnum::ENABLED]];
- $res = CommissionMonthBilling::getOne($where, true, [], false, 'income_nums,expenditure_nums,expenditure_money,income_money');
- if (!empty($res)) $data = $res;
- }
- return $data;
- }
- protected function yearBilling($params)
- {
- $current_year = date('Y', time());
- if (!empty($params['year'])) {
- $year = $params['year'];
- } else {
- $year = $current_year;
- }
- $data = [
- 'income_nums' => 0,
- 'expenditure_nums' => 0,
- 'expenditure_money' => 0,
- 'income_money' => 0,
- ];
- if ($current_year == $year) {
- //当前年
- $user_id = $this->userwages['id'];
- $month = date('m', time());
- $start_time = strtotime($year . '-' . $month . '-01 00:00:00');
- $month_arr = [];
- $month = intval($month);
- for ($i = 1; $i < $month; $i++) {
- $tag = '-';
- if ($i < 10) {
- $tag = $tag . '0';
- }
- $month_arr[] = $year . $tag . $i;
- }
- //非当月,除历史统计数据查询
- $list = CommissionMonthBilling::lists([
- 'where' => [
- ['user_id' => $user_id],
- ['month' => $month_arr]
- ],
- 'select' => 'sum(income_money) as income_money,
- sum(income_nums) as income_nums,
- sum(expenditure_money) as expenditure_money,
- sum(expenditure_nums) as expenditure_nums'
- ]);
- foreach ($list as $val) {
- $data['income_money'] += $val['income_money'];
- $data['income_nums'] += $val['income_nums'];
- $data['expenditure_money'] += $val['expenditure_money'];
- $data['expenditure_nums'] += $val['expenditure_nums'];
- }
- //当月从佣金记录表查询
- $sql = "SELECT mall_id,user_id,change_type,sum(change_num) as SUM3,count(*) as COUNT4
- FROM qimall_commission_log where created_at>={$start_time} and user_id ={$user_id} GROUP BY mall_id,user_id,change_type";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as $item) {
- switch ($item['change_type']) {
- case 'add':
- $data['income_money'] = bcadd($data['income_money'], $item['SUM3'], 2);
- $data['income_nums'] = bcadd($data['income_nums'], $item['COUNT4'], 2);
- break;
- case 'sub':
- $data['expenditure_money'] = bcadd($data['expenditure_money'], $item['SUM3'], 2);
- $data['expenditure_nums'] = bcadd($data['expenditure_nums'], $item['COUNT4'], 2);
- break;
- }
- }
- } else {
- $where = [
- ['mall_id' => $this->mall_id], ['user_id' => $this->userwages['id']], ['years' => $year], ['status' => StatusEnum::ENABLED]];
- $res = CommissionYearsBilling::getOne($where, true, [], false, 'income_nums,expenditure_nums,expenditure_money,income_money');
- if (!empty($res)) $data = $res;
- }
- return $data;
- }
- //月账单对比
- public function actionBillingContrast()
- {
- $params = \Yii::$app->request->post();
- $current_month = date('Y-m', time());
- $month = !empty($params['month']) ? $params['month'] : $current_month;
- $tags = '-';
- $date1 = explode($tags, $current_month . '-01');
- $date2 = explode($tags, $month . '-01');
- $cha = abs($date1[0] - $date2[0]) * 12 + ($date1[1] - $date2[1]);
- $month_arr = [];
- if ($cha <= 3) {
- for ($i = 5; $i >= 0; $i--) {
- $temp = "-{$i} Months";
- $month_arr[] = date("Y-m", strtotime($temp));
- }
- } else {
- $start_i = $cha + 2;
- $end_i = $cha - 3;
- for ($i = $start_i; $i >= $end_i; $i--) {
- $temp = "-{$i} Months";
- $month_arr[] = date("Y-m", strtotime($temp));
- }
- }
- //有当月
- $user_id = $this->userwages['id'];
- $where = [
- ['mall_id' => $this->mall_id],
- ['user_id' => $user_id],
- ['month' => $month_arr]
- ];
- $select = 'expenditure_money,income_money,month';
- $list = CommissionMonthBilling::lists(['where' => $where, 'select' => $select, 'index' => 'month']);
- if (in_array($current_month, $month_arr)) {
- $this_month = DateHelper::thisMonth();
- $start_time = $this_month['start'];
- $end_time = $this_month['end'];
- $sql = "SELECT mall_id,user_id,change_type,sum(change_num) as SUM3,count(*) as COUNT4
- FROM qimall_commission_log where created_at <={$end_time} and created_at>={$start_time} and user_id ={$user_id} GROUP BY mall_id,user_id,change_type";
- $log_list = \Yii::$app->db->createCommand($sql)->queryAll();
- $current_month_income_money = 0;
- $current_month_income_nums = 0;
- $current_month_expenditure_money = 0;
- $current_month_expenditure_nums = 0;
- foreach ($log_list as $val) {
- if ($val['change_type'] == 'add') {
- $current_month_income_money = $val['SUM3'];
- // $current_month_income_nums = $val['COUNT4'];
- }
- if ($val['change_type'] == 'sub') {
- $current_month_expenditure_money = $val['SUM3'];
- // $current_month_expenditure_nums = $val['COUNT4'];
- }
- }
- $list[$current_month] = [
- 'expenditure_money' => $current_month_expenditure_money,
- // 'expenditure_nums' => $current_month_expenditure_nums,
- 'income_money' => $current_month_income_money,
- // 'income_nums' => $current_month_income_nums,
- 'month' => $current_month,
- ];
- }
- $data = [];
- foreach ($month_arr as $val) {
- $is_default = 0;
- if ($val == $month) $is_default = 1;
- $temp_arr = explode('-',$val);
- $temp_data = [
- 'expenditure_money' => 0,
- 'income_money' => 0,
- // 'expenditure_nums' => 0,
- // 'income_nums' => 0,
- 'month' =>$val,
- 'is_default' => $is_default];
- if($temp_arr[1]==1||$temp_arr[1]==12){
- $temp_data['month_str'] = $val;
- }else{
- $temp_data['month_str'] = $temp_arr[1];
- }
- if (isset($list[$val])) {
- $temp_arr = explode('-',$list[$val]['month']);
- if($temp_arr[1]==1||$temp_arr[1]==12){
- $list[$val]['month_str'] = $list[$val]['month'];
- }else{
- $list[$val]['month_str'] = $temp_arr[1];
- }
- $list[$val]['is_default'] = $is_default;
- $temp_data = $list[$val];
- }
- $data[] = $temp_data;
- }
- return $this->success('', $data);
- }
- /**
- * 新月度账单接口
- * @Author: lun
- * @DateTime: 2023/3/22 0022 10:14
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- * @throws \yii\db\Exception
- */
- public function actionMonthBilling()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['select_month','roll_month'], 'string'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- // 获取当前月份
- $current_month = date('Y-m', time());
- // 获取月份
- $check_month = $params['select_month'] ?? $params['roll_month'];// 如果有select_month(选中的月份)优先查询这个,否则查询roll_month(左右滚动的月份)
- $month_data = CommissionMonthBilling::getMonthArr($check_month);
- $month_arr = $month_data['month_arr'];
- //有当月
- $user_id = $this->userwages['id'];
- $where = [
- ['mall_id' => $this->mall_id],
- ['user_id' => $user_id],
- ['month' => $month_arr]
- ];
- $select = 'expenditure_money,income_money,month';
- $list = CommissionMonthBilling::lists(['where' => $where, 'select' => $select, 'index' => 'month']);
- if (in_array($current_month, $month_arr)) {
- $this_month = DateHelper::thisMonth();
- $start_time = $this_month['start'];
- $end_time = $this_month['end'];
- $sql = "SELECT mall_id,user_id,change_type,sum(change_num) as SUM3,count(*) as COUNT4
- FROM qimall_commission_log where created_at <={$end_time} and created_at>={$start_time} and user_id ={$user_id} GROUP BY mall_id,user_id,change_type";
- $log_list = \Yii::$app->db->createCommand($sql)->queryAll();
- $current_month_income_money = 0;
- $current_month_expenditure_money = 0;
- foreach ($log_list as $val) {
- if ($val['change_type'] == 'add') {
- $current_month_income_money = $val['SUM3'];
- }
- if ($val['change_type'] == 'sub') {
- $current_month_expenditure_money = $val['SUM3'];
- }
- }
- $list[$current_month] = [
- 'expenditure_money' => $current_month_expenditure_money,
- 'income_money' => $current_month_income_money,
- 'month' => $current_month,
- ];
- }
- $data = [];
- foreach ($month_arr as $val) {
- $temp_arr = explode('-',$val);
- $temp_data = [
- 'expenditure_money' => 0,
- 'income_money' => 0,
- 'month' =>$val,
- ];
- if($temp_arr[1]==1||$temp_arr[1]==12){
- $temp_data['month_str'] = $val;
- }else{
- $temp_data['month_str'] = $temp_arr[1];
- }
- if (isset($list[$val])) {
- $temp_arr = explode('-',$list[$val]['month']);
- if($temp_arr[1]==1||$temp_arr[1]==12){
- $list[$val]['month_str'] = $list[$val]['month'];
- }else{
- $list[$val]['month_str'] = $temp_arr[1];
- }
- $temp_data = $list[$val];
- }
- $data['show_month_arr'][] = $temp_data;
- }
- unset($month_data['month_arr']);
- $data = array_merge($data, $month_data);
- return $this->success('', $data);
- }
- //获取提现信息
- public function actionCashSetting()
- {
- $params = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($params, [
- [['from'], 'required', 'message' => '参数丢失'],
- [['from'], 'in', 'range' => [1, 2]],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- // 用户已存在,更新用户最新的账号金额信息
- $user = User::findOne(['id' => $this->userwages['id'], 'status' => StatusEnum::ENABLED]);
- if (MallAddons::isInstall($this->mall_id, 'Salary') && Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic.is_external')) {
- UserLogic::updateUser($user->toArray(), 2);
- }
- $user_balance = UserWallet::getOne([['user_id' => $this->userwages['id'], 'status' => 1]], true, [], false, 'balance,commission');
- if ($params['from'] == 1) {//佣金
- $payment = \Yii::$app->services->setting->mall->get($this->mall_id, 'withdraw_income');
- if (!$payment) {
- return $this->error('暂未开启提现功能');
- }
- $isCash = $payment["is_income_cash"];
- $cashType = $payment['cash_type'];
- $cashServiceFee = isset($payment["cash_service_fee"]) ? $payment["cash_service_fee"] : 10;
- $cashIncomeServiceFee = isset($payment["cash_income_service_fee"]) ? $payment["cash_income_service_fee"] : 10;
- $minMoney = $payment['min_money'];
- $maxMoney = $payment['day_max_money'];
- $withdrawal_rule = isset($payment['profit_withdrawal_rule']) ? $payment['profit_withdrawal_rule'] : '手续费费率';
- $userMoney = $user_balance['commission'];
- } else {
- $payment = \Yii::$app->services->setting->mall->get($this->mall_id, 'withdraw_balance');
- if (!$payment) {
- return $this->error('暂未开启提现功能');
- }
- $isCash = $payment["balance_cash_status"];
- $cashType = $payment['balance_cash_type'];
- $cashServiceFee = isset($payment["balance_cash_service_fee"]) ? $payment["balance_cash_service_fee"] : 10;
- $minMoney = $payment['balance_min_money'];
- $maxMoney = $payment['balance_day_max_money'];
- $withdrawal_rule = isset($payment['profit_withdrawal_rule']) ? $payment['profit_withdrawal_rule'] : '手续费费率';
- $userMoney = $user_balance['balance'];
- }
- $balance_config = \Yii::$app->services->setting->mall->get($this->mall_id, 'pay.balance');
- $pay_password_status = 0;
- if ($balance_config) {
- $balance_config = Json::decode($balance_config);
- $pay_password_status = isset($balance_config["pay_password_status"]) ? $balance_config["pay_password_status"] : 0;
- }
- if (!$isCash) {
- return $this->error('系统未开启提现功能');
- }
- if (!$cashType) {
- return $this->error('系统未设置提现方式');
- }
- $cash__type_name = [];
- $get_system_setting = PlatformSetting::getConfByGroup('system', true);
- if (!empty($cashType)) {
- $cash_type = Json::decode($cashType);
- $cash__type_name = [];
- $default_withdraw_way = WithdrawWayEnum::getDefaultWithDrawWayMap(WithdrawWayEnum::WITHDRAW_TYPE_2);
- $extension_withdraw_way = GlobalInvoke::exec(GlobalInvoke::INVOKE_GET_WITHDRAW_EXTENSION_WAY, $this->mall_id, ['mall_id' => $this->mall_id]);
- $all_withdraw = array_merge($default_withdraw_way, $extension_withdraw_way);
- $cash_type_keys = array_keys($all_withdraw);
- if (in_array('bank', $cash_type_keys) || in_array('joinpay', $cash_type_keys)) {
- $bank_card_list = UserBankCard::getUserCardList($this->userwages['id'], $this->mall_id);
- }
- foreach ($cash_type as $i => $item) {
- if (isset($all_withdraw[$item])) {
- $taxFee = 0;
- $name = '';
- if (isset($all_withdraw[$item])) {
- if (is_array($all_withdraw[$item])) {
- $name = $all_withdraw[$item]['name'] ?? '';
- $taxFee = $all_withdraw[$item]['taxFee'] ?? 0;
- } else {
- $name = $all_withdraw[$item];
- }
- }
- $cash__type_name[$i]['type'] = $item;
- $cash__type_name[$i]['taxFee'] = $taxFee;
- $cash__type_name[$i]['name'] = $name;
- $cash__type_name[$i]['img'] = $get_system_setting['api_url']['value'] . '/resources/img/payment/' . $item . '.png';
- if ('bank' == $item || 'joinpay' == $item) {
- $cash__type_name[$i]['bank_card_list'] = $bank_card_list;
- }
- }
- }
- }
- $password = User::getOne([['id' => $this->userwages['id']]], true, '', '', 'transaction_password');
- $isTransactionPassword = false;
- $transaction_password = $password['transaction_password'];
- $transaction_password = trim($transaction_password);
- if (!empty($transaction_password)) {
- $isTransactionPassword = true;
- }
- //检测是否需要实名认证
- $need_auth = GlobalInvoke::exec(GlobalInvoke::REAL_AUTH, $this->mall_id, ['user_id' => $this->userwages['id'], 'mall_id' => $this->mall_id, 'type' => 'cash']);
- if ($need_auth === true) {
- //需要先进行实名认证
- return $this->error('请先进行实名认证', [], ApiStatusEnum::CODE_ERROR_NEED_REAL_AUTH);
- }
- $userWithdrawLog = UserWithdrawLog::getOne([
- ['user_id' => $this->userwages['id']],
- ['type' => ['alipay_tax', 'alipay']]
- ], true, [], 'id desc');
- $aliAccountInfo = [];
- if (!empty($userWithdrawLog)) {
- $extra = json_decode($userWithdrawLog['extra'], true);
- if (!empty($extra)) {
- $aliAccountInfo = [
- 'name' => $extra['name'],
- 'mobile' => $extra['mobile'],
- 'id_card' => $extra['id_card'] ?? '',
- ];
- }
- }
- return $this->success('操作成功', [
- 'notice' => [
- 'content' => '',
- 'icon' => ''
- ],
- 'setting' => [
- 'cash_type_name' => $cash__type_name,
- 'cash_service_fee' => $cashServiceFee,
- 'min_money' => $minMoney,
- 'cash_income_service_fee' => $cashIncomeServiceFee,
- 'day_max_money' => $maxMoney,
- 'withdrawal_rule' => '个人所得税',
- 'pay_password_status' => intval($pay_password_status)
- ],
- 'user_info' => [
- 'income' => $userMoney,
- 'is_transaction_password' => $isTransactionPassword
- ],
- 'ali_account_info' => $aliAccountInfo
- ], ApiStatusEnum::CODE_SUCCESS, 0);
- }
- //提交提现
- public function actionWithdraw()
- {
- $params = \Yii::$app->request->post();
- // 检查商户是否存在
- if (MallAddons::isInstall($this->mall_id, 'Salary')) {
- $merchant = SalaryMerchant::findOne(['mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($merchant) || empty($merchant->status)) return $this->error('商户不存在或被禁用');
- }
- $need_auth = GlobalInvoke::exec(GlobalInvoke::REAL_AUTH, $this->mall_id, ['user_id' => $this->userwages['id'], 'mall_id' => $this->mall_id, 'type' => 'cash']);
- if ($need_auth === true) {
- //需要先进行实名认证
- return $this->error('请先进行实名认证', [], ApiStatusEnum::CODE_ERROR_NEED_REAL_AUTH);
- }
- $form = new UserWithdrawLog();
- $params['mall_id'] = $this->mall_id;
- $params['user_id'] = $this->userwages['id'];
- $form->attributes = $params;
- $system = Yii::$app->services->setting->platform->get('system');
- $extra['redirect_url'] = ($system['cat_h5_url'] ?? ''). '/#/pages/userWages/index?sign=' .$this->mall['mall_sign'];
- $data = $form->saveRecord($params, $extra);
- if ($data['state'] && !empty($data['data']['cash_id'])) {
- // 提现申请成功,扣减第三方金额
- if (MallAddons::isInstall($this->mall_id, 'Salary') && !empty($data['data']['cash_id'])) {
- if (Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic.is_external')) {
- (new WyaServices($this->mall_id))->changeCommission($this->userwages, [
- 'id' => $data['data']['cash_id'],
- 'num' => $params['price'],
- ], 21);
- } else {
- // 主动推送到第三方平台
- $push_url = Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic.push_url');
- if ($push_url) {
- $logic = (new PushLogic($this->mall_id));
- $salaryUserLogMobile = SalaryUserLog::findOne(['user_id'=>$this->userwages['id']]);
- $cash = UserWithdrawLog::find()->where(['id' => $data['data']['cash_id']])->asArray()->one();
- $logic->changeCommission($push_url, [
- 'mobile' => $this->userwages['mobile'],
- 'muid' => $salaryUserLogMobile['muid'] ?? '',
- 'order_no' => $cash['order_no'],
- 'money' => $cash['num'],
- 'desc' => '申请提现',
- ], 1);
- }
- }
- }
- return $this->success('操作成功', $data['data']);
- } else {
- return $this->error($data['msg']);
- }
- }
- /**
- * 检查是否签约
- * @GetApi(path="actionCheckSign",summary="actionCheckSign")
- * @FormData(key="参数名|中文名", rule="")
- * @FormData(key="参数名|中文名", rule="")
- * @ApiResponse(code="500", template="error")
- * @ApiResponse(code="200", template="success")
- */
- public function actionCheckSign(){
- $service = new UserService(['mall_id'=>$this->mall_id]);
- $res = $service->checkSign($this->userwages['id']);
- $data['is_sign'] = false;
- if($res != false){
- $data['is_sign'] = true;
- $data = array_merge($res,$data);
- }
- $msg = $res ? '已签约' : '没有签约,请前往签约';
- return $this->success_str($msg,$data);
- }
- /**
- * 去签约
- * @GetApi(path="actionGoToSign",summary="actionGoToSign")
- * @FormData(key="参数名|中文名", rule="")
- * @FormData(key="参数名|中文名", rule="")
- * @ApiResponse(code="500", template="error")
- * @ApiResponse(code="200", template="success")
- */
- public function actionGoToSign(){
- $data = $this->request->post();
- $res = \Yii::$app->services->validate->validate($data,[
- [
- ['mobile','id_card','name','ali_account'], 'required'],
- ],
- [
- 'mobile' => '手机号码',
- 'id_card' => '身份证',
- 'name' => '姓名',
- 'ali_account'=> '支付宝账号',
- ]);
- if( $res === false ) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $service = new UserService(['mall_id'=>$this->mall_id]);
- $url = $service->goToSing( $this->userwages['id'],...array_values($data));
- // 已经签约
- if ( $url === true ) return $this->success('已签约,不需要重复签约',['is_sign'=>true,'url'=>'']);
- // 有错误
- if ( $url === false ) return $this->error('签约失败:'.$service->error);
- // 跳转url
- return $this->success('跳转url进行签约',['is_sign'=>false,'url'=>$url]);
- }
- /**
- * @name:
- * @msg: 绑定银行卡,单纯绑定不签约快捷支付
- * @param {*}
- * @return {*}
- */
- public function actionBindCard()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $post = \Yii::$app->request->post();
- $rules = [
- [['bank_card_no', 'payer_name', 'bank_name', 'bank_code', 'card_type', 'card_type_name'], 'required'],
- ['bank_card_no', 'string', 'max' => 64],
- [['payer_name', 'bank_name'], 'string', 'max' => 255],
- [['bank_code', 'card_type', 'card_type_name'], 'string', 'max' => 32],
- ];
- $res = \Yii::$app->services->validate->validate($post, $rules);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $bank_card_info = UserBankCard::findONe(['mall_id'=>$this->mall_id,'bank_card_no' => $post['bank_card_no'], 'status' => StatusEnum::ENABLED]);
- if (!empty($bank_card_info)) {
- return $this->error('银行卡已经被绑定');
- }
- $user_id = $this->userwages['id'];
- $post['user_id'] = $user_id;
- $post['card_type'] = strtolower($post['card_type']);
- $model = UserBankCard::setData($this->mall_id, $post, 'bind');
- if (false === $model) return $this->error(UserBankCard::getStaticError());
- return $this->success('操作成功', ['card_id' => $model->id]);
- }
- /**
- * 请求成功统消息格式化处理
- * @Author bing
- * @DateTime 2020-10-27 15:43:16
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param string $msg
- * @param array $data
- * @param [type] $code
- * @return void|mixed
- */
- private function success_str($msg = '操作成功', $data = [], $code = ApiStatusEnum::CODE_SUCCESS)
- {
- if (is_object($data)) $data = ArrayHelper::toArray($data);
- \Yii::$app->controller->asJson(json_decode(json_encode(array(
- 'code' => $code,
- 'msg' => $msg,
- 'data' => $data
- )), true))->send();
- }
- }
|