CashForm.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\admin;
  8. use app\models\Cash;
  9. use app\models\Share;
  10. use app\models\User;
  11. use app\utils\Export;
  12. use app\utils\ExportList;
  13. use yii\base\Model;
  14. /**
  15. * Undocumented class
  16. *
  17. * @Author LGL 24963@qq.com
  18. * @DateTime 2021-01-14
  19. * @desc: 提现表操作
  20. */
  21. class CashForm extends Model
  22. {
  23. public $id;
  24. public $store_id;
  25. public $user_id;
  26. public $price;
  27. public $status;
  28. public $is_delete;
  29. public $pay_time;
  30. public $type;
  31. public $mobile;
  32. public $name;
  33. public $bank_name;
  34. public $pay_type;
  35. public $order_no;
  36. public $service_charge;
  37. public $cash_type;
  38. //搜索
  39. public $search_key;
  40. public $search_is_show;
  41. public $key;
  42. public $action;//操作状态:1、微信打款,2、线下打款,3、驳回,4、确认申请
  43. public $dateStart;
  44. public $dateEnd;
  45. public $keyword;
  46. public $flag;
  47. public $fields;
  48. const SCENARIO_ADD = 'add';
  49. const SCENARIO_EDIT = 'edit';
  50. const SCENARIO_DEL = 'del';
  51. const SCENARIO_LIST = 'list';
  52. const SCENARIO_ACTION = 'action';
  53. const STATUS_TEXT = [
  54. Cash::STATUS_APPLY => '待审核',
  55. Cash::STATUS_CONFIRM => '待打款',
  56. Cash::STATUS_GIVEN => '已打款',
  57. Cash::STATUS_REFUSE => '驳回',
  58. Cash::STATUS_RECHARGE => '余额通过',
  59. ];
  60. const TYPE_TEXT = [
  61. Cash::TYPE_WX => '微信',
  62. Cash::TYPE_ALIPAY => '支付宝',
  63. Cash::TYPE_BANK => '银行卡',
  64. Cash::TYPE_RECHARGE => '余额'
  65. ];
  66. const PAY_TYPE_TEXT = [
  67. Cash::PAY_TYPE_NO => '未统计',
  68. Cash::PAY_TYPE_WX => '微信自动打款',
  69. Cash::PAY_TYPE_HAND => '手动拨款'
  70. ];
  71. const CAHS_TYPE = [
  72. Cash::IS_CASH_TYPE_SHARE => '非自提点',
  73. Cash::IS_CASH_TYPE_SHOP => '自提点'
  74. ];
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function rules()
  79. {
  80. return [
  81. [['id', 'user_id', 'store_id', 'status', 'is_delete', 'pay_time', 'type', 'pay_type', 'cash_type'], 'integer'],
  82. [['price', 'service_charge'], 'number'],
  83. [['keyword', 'flag'], 'trim'],
  84. [['mobile', 'name', 'order_no', 'keyword', 'dateStart', 'dateEnd'], 'string', 'max' => 255],
  85. [['bank_name', 'flag'], 'string', 'max' => 30],
  86. ['is_delete', 'default', 'value'=> Cash::IS_DELETE_NO],
  87. ['type', 'default', 'value'=> Cash::TYPE_WX],
  88. ['status', 'default', 'value'=> Cash::STATUS_APPLY],
  89. ['pay_type', 'default', 'value'=> Cash::PAY_TYPE_NO],
  90. ['cash_type', 'default', 'value'=> Cash::IS_CASH_TYPE_SHARE],
  91. [['fields'], 'safe'],
  92. // 操作
  93. [['name', 'pic_url', 'total_num', 'expire_day', 'money', 'price', 'sort'],'required', 'on'=> [self::SCENARIO_ADD, self::SCENARIO_EDIT]],
  94. // [['id'],'required', 'on'=> [self::SCENARIO_DEL, self::SCENARIO_EDIT]],
  95. [['key', 'action'], 'required', 'on'=> self::SCENARIO_ACTION],
  96. ['search_key', 'string', 'on' => self::SCENARIO_LIST]
  97. ];
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function attributeLabels()
  103. {
  104. return [
  105. 'id' => 'ID',
  106. 'user_id' => '用户id',
  107. 'store_id' => '商城id',
  108. 'price' => '提现金额',
  109. 'status' => '申请状态 0--申请中 1--确认申请 2--已打款 3--驳回 5--余额通过',
  110. 'is_delete' => '是否删除',
  111. 'pay_time' => '付款',
  112. 'type' => '支付方式 0--微信支付 1--支付宝 2--银行卡 3--余额',
  113. 'mobile' => '支付宝账号',
  114. 'name' => '支付宝姓名',
  115. 'bank_name' => '开户行名称',
  116. 'pay_type' => '打款方式 0--之前未统计的 1--微信自动打款 2--手动打款',
  117. 'order_no' => '微信自动打款订单号',
  118. 'service_charge' => '提现手续费',
  119. 'cash_type' => '0、正常,1、自提点提现',
  120. ];
  121. }
  122. public function scenarios()
  123. {
  124. $scenarios = parent::scenarios();
  125. return $scenarios;
  126. }
  127. // 获取分销商提现
  128. public function searchShopCash()
  129. {
  130. $query = Cash::find()->alias('c')
  131. ->leftJoin(['u' => User::tableName()], "u.id=c.user_id")
  132. ->select("c.*, u.nickname user_name, u.avatar_url user_head_url");
  133. $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");
  134. // 搜索
  135. if ($this->search_key) {
  136. $query->andWhere([
  137. 'or',
  138. ['like', 'u.nickname', $this->search_key],
  139. ['like', 'c.name', $this->search_key]
  140. ]);
  141. }
  142. if ($this->status == 0 and $this->status != '') {//待审核
  143. $query->andWhere(['c.status' => 0]);
  144. }
  145. if ($this->status == 1) {//待打款
  146. $query->andWhere(['c.status' => 1]);
  147. }
  148. if ($this->status == 2) {//已打款
  149. $query->andWhere(['in', 'c.status', [2, 5]]);
  150. }
  151. if ($this->status == 3) {//无效
  152. $query->andWhere(['c.status' => 3]);
  153. }
  154. if ($this->flag == Export::EXPORT) {
  155. $export = new ExportList();
  156. $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();
  157. $export->shareExportData($data, $this->fields);
  158. }
  159. $list = pagination_make($query);
  160. foreach ($list['list'] as &$val) {
  161. $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
  162. $val['status_text'] = self::STATUS_TEXT[$val['status']];
  163. $val['type_text'] = self::TYPE_TEXT[$val['type']];
  164. $val['pay_type_text'] = self::PAY_TYPE_TEXT[$val['pay_type']];
  165. // $val['is_shop_text'] = self::CAHS_TYPE[$val['is_shop']];
  166. }
  167. return [
  168. 'code' => 0,
  169. 'msg' => 'success',
  170. 'data' => [
  171. 'export_list' => $this->getCustomField(),
  172. 'data' => $list['list'],
  173. 'pageNo' => $list['pageNo'],
  174. 'totalCount' => $list['totalCount']
  175. ]
  176. ];
  177. }
  178. public function delCash()
  179. {
  180. if ($this->validate()) {
  181. $t = \Yii::$app->db->beginTransaction();
  182. if ($this->scenario !== self::SCENARIO_DEL) {
  183. return [
  184. 'code' => 1,
  185. 'msg' => '删除失败'
  186. ];
  187. }
  188. $model = Cash::findOne(['id' => $this->key]);
  189. $model->is_delete = Cash::IS_DELETE_YES;
  190. if (!$model || !$model->save()) {
  191. $t->rollBack();
  192. return [
  193. 'code' => 1,
  194. 'msg' => $model->getErrorSummary(false)[0]
  195. ];
  196. }
  197. $t->commit();
  198. return [
  199. 'code' => 0,
  200. 'msg' => '删除成功'
  201. ];
  202. // 所有输入数据都有效 all inputs are valid
  203. } else {
  204. // 验证失败:$errors 是一个包含错误信息的数组
  205. return [
  206. 'code' => 1,
  207. "msg" => $this->getErrorSummary(false)[0]
  208. ];
  209. }
  210. }
  211. // 获取所有提现列表
  212. public static function getCashList()
  213. {
  214. return Cash::find()->where(['is_delete' => Cash::IS_DELETE_NO, 'store_id' => get_store_id()])->orderBy(['sort desc, id desc'])->select('*')->asArray()->all();
  215. }
  216. /**
  217. * 获取分销提现列表
  218. * @return array
  219. */
  220. public function getShareCashList()
  221. {
  222. $query = Cash::find()->alias('c')
  223. ->where(['c.is_delete' => Cash::IS_DELETE_NO, 'c.store_id' => $this->store_id])
  224. ->leftJoin('{{%user}} u', 'u.id=c.user_id')
  225. ->leftJoin('{{%share}} s', 's.user_id=c.user_id')
  226. ->andWhere(['s.is_delete' => Share::SHARE_NOT_DELETE, 'u.is_distributor' => User::IS_DISTRIBUTOR,
  227. 'c.cash_type' => Cash::IS_CASH_TYPE_SHARE]);
  228. if ($this->keyword) {
  229. $query->andWhere([
  230. 'or',
  231. ['like', 'u.nickname', $this->keyword],
  232. ['like', 's.name', $this->keyword]
  233. ]);
  234. }
  235. if ($this->dateStart) {
  236. $query->andWhere(['>=', 'c.created_at', strtotime($this->dateStart)]);
  237. }
  238. if ($this->dateEnd) {
  239. $query->andWhere(['<=', 'c.created_at', strtotime($this->dateEnd)]);
  240. }
  241. if ($this->status == Cash::STATUS_APPLY and $this->status != '') {//待审核
  242. $query->andWhere(['c.status' => Cash::STATUS_APPLY]);
  243. }
  244. if ($this->status == Cash::STATUS_CONFIRM) {//待打款
  245. $query->andWhere(['c.status' => Cash::STATUS_CONFIRM]);
  246. }
  247. if ($this->status == Cash::STATUS_GIVEN) {//已打款
  248. $query->andWhere(['in', 'c.status', [Cash::STATUS_GIVEN, Cash::STATUS_RECHARGE]]);
  249. }
  250. if ($this->status == Cash::STATUS_REFUSE) {//无效
  251. $query->andWhere(['c.status' => Cash::STATUS_REFUSE]);
  252. }
  253. if ($this->id) {
  254. $query->andWhere(['s.id' => $this->id]);
  255. }
  256. if ($this->flag == Export::EXPORT) {
  257. $export = new ExportList();
  258. $data = $query->orderBy('c.status ASC,c.created_at DESC')
  259. ->select(['c.*', 'u.nickname', 'u.avatar_url','u.platform', 'u.id user_id'])->asArray()->all();
  260. $export->shareExportData($data, $this->fields);
  261. }
  262. $query->distinct()->orderBy('c.status ASC,c.created_at DESC')->select([
  263. 'c.*', 'u.nickname','u.platform', 'u.avatar_url'
  264. ]);
  265. $pagination = pagination_make($query);
  266. $list = $pagination['list'];
  267. foreach($list as &$value){
  268. $value['service_money'] = $value['service_charge'] * $value['price'] / 100;
  269. $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  270. $value['updated_at'] = !empty($value['updated_at']) ? date('Y-m-d H:i:s', $value['updated_at']) : '';
  271. $value['money'] = Cash::getServiceMoney($value);
  272. }
  273. return [
  274. 'code' => 0,
  275. 'msg' => 'success',
  276. 'data' => [
  277. 'export_list' => $this->getCustomField(),
  278. 'data' => $list,
  279. 'pageNo' => $pagination['pageNo'],
  280. 'totalCount' => $pagination['totalCount']
  281. ],
  282. ];
  283. }
  284. public function getCustomField()
  285. {
  286. return [
  287. [
  288. 'key' => 'order_no',
  289. 'value' => '订单号'
  290. ],
  291. [
  292. 'key' => 'nickname',
  293. 'value' => '姓名',
  294. ],
  295. [
  296. 'key' => 'price',
  297. 'value' => '提现金额',
  298. ],
  299. [
  300. 'key' => 'addtime',
  301. 'value' => '申请日期',
  302. ],
  303. [
  304. 'key' => 'bank_name',
  305. 'value' => '银行名称',
  306. ],
  307. [
  308. 'key' => 'bank_card',
  309. 'value' => '打款账号',
  310. ],
  311. [
  312. 'key' => 'name',
  313. 'value' => '真实姓名',
  314. ],
  315. [
  316. 'key' => 'type',
  317. 'value' => '类型',
  318. ],
  319. [
  320. 'key' => 'pay_type',
  321. 'value' => '打款方式',
  322. ],
  323. [
  324. 'key' => 'pay_time',
  325. 'value' => '付款时间',
  326. ],
  327. ];
  328. }
  329. public function confirm()
  330. {
  331. $id = post_params('id');
  332. $status = post_params('status');
  333. $store_id = get_store_id();
  334. $cash = Cash::findOne(['id' => $id, 'is_delete' => Cash::IS_DELETE_NO,
  335. 'store_id' => $store_id]);
  336. if (!$cash) {
  337. return [
  338. 'code' => 1,
  339. 'msg' => '提现记录不存在,请刷新重试'
  340. ];
  341. }
  342. if (!$cash->order_no) {
  343. $order_no = null;
  344. while (true) {
  345. $order_no = date('YmdHis') . mt_rand(100000, 999999);
  346. $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
  347. if (!$exist_order_no) {
  348. break;
  349. }
  350. }
  351. $cash->order_no = $order_no;
  352. $cash->save();
  353. }
  354. if ($cash->status != 1) {
  355. return [
  356. 'code' => 1,
  357. 'msg' => '操作错误,请刷新重试'
  358. ];
  359. }
  360. $res = [];
  361. $price = Cash::getServiceMoney($cash);
  362. if ($status == Cash::STATUS_GIVEN) { //微信自动打款
  363. $cash->status = Cash::STATUS_GIVEN;
  364. $cash->pay_time = time();
  365. $cash->pay_type = Cash::PAY_TYPE_WX;
  366. $user = User::findOne(['id' => $cash->user_id]);
  367. $data = [
  368. 'partner_trade_no' => $cash->order_no,
  369. 'openid' => $user->wechat_open_id,
  370. 'check_name' => 'NO_CHECK',
  371. 'amount' => $price * 100,
  372. 'desc' => '转账'
  373. ];
  374. $wechat = \Yii::$app->controller->wechatPay;
  375. $res = $wechat->transfer->toBalance($data);
  376. } elseif ($status == Cash::STATUS_HAND) { //手动打款
  377. $cash->status = Cash::STATUS_GIVEN;
  378. $cash->pay_time = time();
  379. $cash->pay_type = Cash::PAY_TYPE_HAND;
  380. if ($cash->type == Cash::TYPE_RECHARGE) {
  381. $user = User::findOne(['id' => $cash->user_id]);
  382. $user->money += doubleval($price);
  383. if (!$user->save()) {
  384. foreach ($user->errors as $error) {
  385. return [
  386. 'code' => 1,
  387. 'msg' => $error
  388. ];
  389. }
  390. }
  391. }
  392. $res['result_code'] = "SUCCESS";
  393. }
  394. if (isset($res['result_code']) && $res['result_code'] == 'SUCCESS') {
  395. $cash->save();
  396. return [
  397. 'code' => 0,
  398. 'msg' => '成功'
  399. ];
  400. } else {
  401. return [
  402. 'code' => 1,
  403. 'msg' => !empty($res['err_code_des']) ? $res['err_code_des'] : '请稍后重试',
  404. 'data' => $res
  405. ];
  406. }
  407. }
  408. }