| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1\admin;
- use app\models\Cash;
- use app\models\Share;
- use app\models\User;
- use app\utils\Export;
- use app\utils\ExportList;
- use yii\base\Model;
- /**
- * Undocumented class
- *
- * @Author LGL 24963@qq.com
- * @DateTime 2021-01-14
- * @desc: 提现表操作
- */
- class CashForm extends Model
- {
- public $id;
- public $store_id;
- public $user_id;
- public $price;
- public $status;
- public $is_delete;
- public $pay_time;
- public $type;
- public $mobile;
- public $name;
- public $bank_name;
- public $pay_type;
- public $order_no;
- public $service_charge;
- public $cash_type;
- //搜索
- public $search_key;
- public $search_is_show;
- public $key;
- public $action;//操作状态:1、微信打款,2、线下打款,3、驳回,4、确认申请
- public $dateStart;
- public $dateEnd;
- public $keyword;
- public $flag;
- public $fields;
- const SCENARIO_ADD = 'add';
- const SCENARIO_EDIT = 'edit';
- const SCENARIO_DEL = 'del';
- const SCENARIO_LIST = 'list';
- const SCENARIO_ACTION = 'action';
- const STATUS_TEXT = [
- Cash::STATUS_APPLY => '待审核',
- Cash::STATUS_CONFIRM => '待打款',
- Cash::STATUS_GIVEN => '已打款',
- Cash::STATUS_REFUSE => '驳回',
- Cash::STATUS_RECHARGE => '余额通过',
- ];
- const TYPE_TEXT = [
- Cash::TYPE_WX => '微信',
- Cash::TYPE_ALIPAY => '支付宝',
- Cash::TYPE_BANK => '银行卡',
- Cash::TYPE_RECHARGE => '余额'
- ];
- const PAY_TYPE_TEXT = [
- Cash::PAY_TYPE_NO => '未统计',
- Cash::PAY_TYPE_WX => '微信自动打款',
- Cash::PAY_TYPE_HAND => '手动拨款'
- ];
- const CAHS_TYPE = [
- Cash::IS_CASH_TYPE_SHARE => '非自提点',
- Cash::IS_CASH_TYPE_SHOP => '自提点'
- ];
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['id', 'user_id', 'store_id', 'status', 'is_delete', 'pay_time', 'type', 'pay_type', 'cash_type'], 'integer'],
- [['price', 'service_charge'], 'number'],
- [['keyword', 'flag'], 'trim'],
- [['mobile', 'name', 'order_no', 'keyword', 'dateStart', 'dateEnd'], 'string', 'max' => 255],
- [['bank_name', 'flag'], 'string', 'max' => 30],
- ['is_delete', 'default', 'value'=> Cash::IS_DELETE_NO],
- ['type', 'default', 'value'=> Cash::TYPE_WX],
- ['status', 'default', 'value'=> Cash::STATUS_APPLY],
- ['pay_type', 'default', 'value'=> Cash::PAY_TYPE_NO],
- ['cash_type', 'default', 'value'=> Cash::IS_CASH_TYPE_SHARE],
- [['fields'], 'safe'],
- // 操作
- [['name', 'pic_url', 'total_num', 'expire_day', 'money', 'price', 'sort'],'required', 'on'=> [self::SCENARIO_ADD, self::SCENARIO_EDIT]],
- // [['id'],'required', 'on'=> [self::SCENARIO_DEL, self::SCENARIO_EDIT]],
- [['key', 'action'], 'required', 'on'=> self::SCENARIO_ACTION],
- ['search_key', 'string', 'on' => self::SCENARIO_LIST]
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'user_id' => '用户id',
- 'store_id' => '商城id',
- 'price' => '提现金额',
- 'status' => '申请状态 0--申请中 1--确认申请 2--已打款 3--驳回 5--余额通过',
- 'is_delete' => '是否删除',
- 'pay_time' => '付款',
- 'type' => '支付方式 0--微信支付 1--支付宝 2--银行卡 3--余额',
- 'mobile' => '支付宝账号',
- 'name' => '支付宝姓名',
- 'bank_name' => '开户行名称',
- 'pay_type' => '打款方式 0--之前未统计的 1--微信自动打款 2--手动打款',
- 'order_no' => '微信自动打款订单号',
- 'service_charge' => '提现手续费',
- 'cash_type' => '0、正常,1、自提点提现',
- ];
- }
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- return $scenarios;
- }
- // 获取分销商提现
- public function searchShopCash()
- {
- $query = Cash::find()->alias('c')
- ->leftJoin(['u' => User::tableName()], "u.id=c.user_id")
- ->select("c.*, u.nickname user_name, u.avatar_url user_head_url");
- $query->where(['c.is_delete' => Cash::IS_DELETE_NO, 'c.cash_type' => Cash::IS_CASH_TYPE_SHOP, 'c.store_id' => get_store_id()])->orderBy("id desc");
- // 搜索
- if ($this->search_key) {
- $query->andWhere([
- 'or',
- ['like', 'u.nickname', $this->search_key],
- ['like', 'c.name', $this->search_key]
- ]);
- }
- if ($this->status == 0 and $this->status != '') {//待审核
- $query->andWhere(['c.status' => 0]);
- }
- if ($this->status == 1) {//待打款
- $query->andWhere(['c.status' => 1]);
- }
- if ($this->status == 2) {//已打款
- $query->andWhere(['in', 'c.status', [2, 5]]);
- }
- if ($this->status == 3) {//无效
- $query->andWhere(['c.status' => 3]);
- }
- if ($this->flag == Export::EXPORT) {
- $export = new ExportList();
- $data = $query->orderBy('c.status ASC,c.created_at DESC')->select(['c.*', 'u.nickname', 'u.avatar_url','u.platform', 'u.id user_id'])->asArray()->all();
- $export->shareExportData($data, $this->fields);
- }
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- $val['status_text'] = self::STATUS_TEXT[$val['status']];
- $val['type_text'] = self::TYPE_TEXT[$val['type']];
- $val['pay_type_text'] = self::PAY_TYPE_TEXT[$val['pay_type']];
- // $val['is_shop_text'] = self::CAHS_TYPE[$val['is_shop']];
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'export_list' => $this->getCustomField(),
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ];
- }
- public function delCash()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario !== self::SCENARIO_DEL) {
- return [
- 'code' => 1,
- 'msg' => '删除失败'
- ];
- }
- $model = Cash::findOne(['id' => $this->key]);
- $model->is_delete = Cash::IS_DELETE_YES;
- if (!$model || !$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $model->getErrorSummary(false)[0]
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '删除成功'
- ];
- // 所有输入数据都有效 all inputs are valid
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
-
- // 获取所有提现列表
- public static function getCashList()
- {
- return Cash::find()->where(['is_delete' => Cash::IS_DELETE_NO, 'store_id' => get_store_id()])->orderBy(['sort desc, id desc'])->select('*')->asArray()->all();
- }
- /**
- * 获取分销提现列表
- * @return array
- */
- public function getShareCashList()
- {
- $query = Cash::find()->alias('c')
- ->where(['c.is_delete' => Cash::IS_DELETE_NO, 'c.store_id' => $this->store_id])
- ->leftJoin('{{%user}} u', 'u.id=c.user_id')
- ->leftJoin('{{%share}} s', 's.user_id=c.user_id')
- ->andWhere(['s.is_delete' => Share::SHARE_NOT_DELETE, 'u.is_distributor' => User::IS_DISTRIBUTOR,
- 'c.cash_type' => Cash::IS_CASH_TYPE_SHARE]);
- if ($this->keyword) {
- $query->andWhere([
- 'or',
- ['like', 'u.nickname', $this->keyword],
- ['like', 's.name', $this->keyword]
- ]);
- }
- if ($this->dateStart) {
- $query->andWhere(['>=', 'c.created_at', strtotime($this->dateStart)]);
- }
- if ($this->dateEnd) {
- $query->andWhere(['<=', 'c.created_at', strtotime($this->dateEnd)]);
- }
- if ($this->status == Cash::STATUS_APPLY and $this->status != '') {//待审核
- $query->andWhere(['c.status' => Cash::STATUS_APPLY]);
- }
- if ($this->status == Cash::STATUS_CONFIRM) {//待打款
- $query->andWhere(['c.status' => Cash::STATUS_CONFIRM]);
- }
- if ($this->status == Cash::STATUS_GIVEN) {//已打款
- $query->andWhere(['in', 'c.status', [Cash::STATUS_GIVEN, Cash::STATUS_RECHARGE]]);
- }
- if ($this->status == Cash::STATUS_REFUSE) {//无效
- $query->andWhere(['c.status' => Cash::STATUS_REFUSE]);
- }
- if ($this->id) {
- $query->andWhere(['s.id' => $this->id]);
- }
- if ($this->flag == Export::EXPORT) {
- $export = new ExportList();
- $data = $query->orderBy('c.status ASC,c.created_at DESC')
- ->select(['c.*', 'u.nickname', 'u.avatar_url','u.platform', 'u.id user_id'])->asArray()->all();
- $export->shareExportData($data, $this->fields);
- }
- $query->distinct()->orderBy('c.status ASC,c.created_at DESC')->select([
- 'c.*', 'u.nickname','u.platform', 'u.avatar_url'
- ]);
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach($list as &$value){
- $value['service_money'] = $value['service_charge'] * $value['price'] / 100;
- $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
- $value['updated_at'] = !empty($value['updated_at']) ? date('Y-m-d H:i:s', $value['updated_at']) : '';
- $value['money'] = Cash::getServiceMoney($value);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'export_list' => $this->getCustomField(),
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ],
- ];
- }
- public function getCustomField()
- {
- return [
- [
- 'key' => 'order_no',
- 'value' => '订单号'
- ],
- [
- 'key' => 'nickname',
- 'value' => '姓名',
- ],
- [
- 'key' => 'price',
- 'value' => '提现金额',
- ],
- [
- 'key' => 'addtime',
- 'value' => '申请日期',
- ],
- [
- 'key' => 'bank_name',
- 'value' => '银行名称',
- ],
- [
- 'key' => 'bank_card',
- 'value' => '打款账号',
- ],
- [
- 'key' => 'name',
- 'value' => '真实姓名',
- ],
- [
- 'key' => 'type',
- 'value' => '类型',
- ],
- [
- 'key' => 'pay_type',
- 'value' => '打款方式',
- ],
- [
- 'key' => 'pay_time',
- 'value' => '付款时间',
- ],
- ];
- }
- public function confirm()
- {
- $id = post_params('id');
- $status = post_params('status');
- $store_id = get_store_id();
- $cash = Cash::findOne(['id' => $id, 'is_delete' => Cash::IS_DELETE_NO,
- 'store_id' => $store_id]);
- if (!$cash) {
- return [
- 'code' => 1,
- 'msg' => '提现记录不存在,请刷新重试'
- ];
- }
- if (!$cash->order_no) {
- $order_no = null;
- while (true) {
- $order_no = date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
- if (!$exist_order_no) {
- break;
- }
- }
- $cash->order_no = $order_no;
- $cash->save();
- }
- if ($cash->status != 1) {
- return [
- 'code' => 1,
- 'msg' => '操作错误,请刷新重试'
- ];
- }
- $res = [];
- $price = Cash::getServiceMoney($cash);
- if ($status == Cash::STATUS_GIVEN) { //微信自动打款
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_WX;
- $user = User::findOne(['id' => $cash->user_id]);
- $data = [
- 'partner_trade_no' => $cash->order_no,
- 'openid' => $user->wechat_open_id,
- 'check_name' => 'NO_CHECK',
- 'amount' => $price * 100,
- 'desc' => '转账'
- ];
- $wechat = \Yii::$app->controller->wechatPay;
- $res = $wechat->transfer->toBalance($data);
- } elseif ($status == Cash::STATUS_HAND) { //手动打款
- $cash->status = Cash::STATUS_GIVEN;
- $cash->pay_time = time();
- $cash->pay_type = Cash::PAY_TYPE_HAND;
- if ($cash->type == Cash::TYPE_RECHARGE) {
- $user = User::findOne(['id' => $cash->user_id]);
- $user->money += doubleval($price);
- if (!$user->save()) {
- foreach ($user->errors as $error) {
- return [
- 'code' => 1,
- 'msg' => $error
- ];
- }
- }
- }
- $res['result_code'] = "SUCCESS";
- }
- if (isset($res['result_code']) && $res['result_code'] == 'SUCCESS') {
- $cash->save();
- return [
- 'code' => 0,
- 'msg' => '成功'
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => !empty($res['err_code_des']) ? $res['err_code_des'] : '请稍后重试',
- 'data' => $res
- ];
- }
- }
- }
|