CashForm.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\constants\OptionSetting;
  9. use app\models\Cash;
  10. use app\models\CashExt;
  11. use app\models\IntegralAppreciationCashLog;
  12. use app\models\Mch;
  13. use app\models\Option;
  14. use app\models\Share;
  15. use app\models\User;
  16. use app\models\SaasUser;
  17. use app\utils\Export;
  18. use app\utils\ExportList;
  19. use yii\base\Model;
  20. /**
  21. * Undocumented class
  22. *
  23. * @Author LGL 24963@qq.com
  24. * @DateTime 2021-01-14
  25. * @desc: 提现表操作
  26. */
  27. class CashForm extends Model
  28. {
  29. public $export;
  30. public $id;
  31. public $store_id;
  32. public $user_id;
  33. public $saas_id;
  34. public $price;
  35. public $status;
  36. public $is_delete;
  37. public $pay_time;
  38. public $type;
  39. public $mobile;
  40. public $name;
  41. public $bank_name;
  42. public $pay_type;
  43. public $order_no;
  44. public $service_charge;
  45. public $cash_type;
  46. //搜索
  47. public $search_key;
  48. public $search_is_show;
  49. public $key;
  50. public $action;//操作状态:1、微信打款,2、线下打款,3、驳回,4、确认申请
  51. public $dateStart;
  52. public $dateEnd;
  53. public $keyword;
  54. public $mch;
  55. public $mch_id;
  56. public $mch_common_cat_id;
  57. public $flag;
  58. public $fields;
  59. public $brands_id = 0;
  60. const SCENARIO_ADD = 'add';
  61. const SCENARIO_EDIT = 'edit';
  62. const SCENARIO_DEL = 'del';
  63. const SCENARIO_LIST = 'list';
  64. const SCENARIO_ACTION = 'action';
  65. const STATUS_TEXT = [
  66. Cash::STATUS_APPLY => '待审核',
  67. Cash::STATUS_CONFIRM => '待打款',
  68. Cash::STATUS_GIVEN => '已打款',
  69. Cash::STATUS_REFUSE => '驳回',
  70. Cash::STATUS_RECHARGE => '余额通过',
  71. ];
  72. const TYPE_TEXT = [
  73. Cash::TYPE_WX => '微信',
  74. Cash::TYPE_ALIPAY => '支付宝',
  75. Cash::TYPE_BANK => '银行卡',
  76. Cash::TYPE_RECHARGE => '余额'
  77. ];
  78. const PAY_TYPE_TEXT = [
  79. Cash::PAY_TYPE_NO => '未统计',
  80. Cash::PAY_TYPE_WX => '微信自动打款',
  81. Cash::PAY_TYPE_HAND => '手动拨款'
  82. ];
  83. const CAHS_TYPE = [
  84. Cash::IS_CASH_TYPE_SHARE => '非自提点',
  85. Cash::IS_CASH_TYPE_SHOP => '自提点'
  86. ];
  87. public $pay_wechat;
  88. public $pay_alipay;
  89. public $bank;
  90. public $lg;
  91. public $remaining_sum;
  92. public $cash_service_charge;
  93. public $min_money;
  94. public $cash_max_single_day;
  95. public $cash_max_day;
  96. public $cash_price_type;
  97. public $cash_price_amount;
  98. public $cash_price_balance;
  99. public $cash_price_integral;
  100. public $auto_money;
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function rules()
  105. {
  106. return [
  107. [['id', 'user_id', 'saas_id','store_id', 'status', 'is_delete', 'pay_time', 'type', 'pay_type', 'cash_type', 'brands_id'], 'integer'],
  108. [['price', 'service_charge'], 'number'],
  109. [['keyword', 'flag'], 'trim'],
  110. [['mobile', 'name', 'order_no', 'keyword', 'dateStart', 'dateEnd', 'cash_price_type'], 'string', 'max' => 255],
  111. [['bank_name', 'flag'], 'string', 'max' => 30],
  112. ['is_delete', 'default', 'value'=> Cash::IS_DELETE_NO],
  113. ['type', 'default', 'value'=> Cash::TYPE_WX],
  114. ['status', 'default', 'value'=> Cash::STATUS_APPLY],
  115. ['pay_type', 'default', 'value'=> Cash::PAY_TYPE_NO],
  116. ['cash_type', 'default', 'value'=> Cash::IS_CASH_TYPE_SHARE],
  117. [['fields'], 'safe'],
  118. // 操作
  119. [['name', 'pic_url', 'total_num', 'expire_day', 'money', 'price', 'sort'],'required', 'on'=> [self::SCENARIO_ADD, self::SCENARIO_EDIT]],
  120. // [['id'],'required', 'on'=> [self::SCENARIO_DEL, self::SCENARIO_EDIT]],
  121. [['key', 'action'], 'required', 'on'=> self::SCENARIO_ACTION],
  122. ['search_key', 'string', 'on' => self::SCENARIO_LIST],
  123. [['mch', 'mch_id', 'mch_common_cat_id'], 'safe'],
  124. [['export'], 'safe'],
  125. [['pay_alipay', 'pay_wechat', 'bank', 'remaining_sum', 'lg'], 'in', 'range' => [0, 1]],
  126. [['cash_service_charge', 'min_money', 'cash_max_single_day', 'cash_max_day', 'cash_price_amount', 'cash_price_balance', 'cash_price_integral', 'auto_money'], 'number'],
  127. ];
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function attributeLabels()
  133. {
  134. return [
  135. 'id' => 'ID',
  136. 'user_id' => '用户id',
  137. 'store_id' => '商城id',
  138. 'price' => '提现金额',
  139. 'status' => '申请状态 0--申请中 1--确认申请 2--已打款 3--驳回 5--余额通过',
  140. 'is_delete' => '是否删除',
  141. 'pay_time' => '付款',
  142. 'type' => '支付方式 0--微信支付 1--支付宝 2--银行卡 3--余额',
  143. 'mobile' => '支付宝账号',
  144. 'name' => '支付宝姓名',
  145. 'bank_name' => '开户行名称',
  146. 'pay_type' => '打款方式 0--之前未统计的 1--微信自动打款 2--手动打款',
  147. 'order_no' => '微信自动打款订单号',
  148. 'service_charge' => '提现手续费',
  149. 'cash_type' => '0、正常,1、自提点提现',
  150. ];
  151. }
  152. public function scenarios()
  153. {
  154. $scenarios = parent::scenarios();
  155. return $scenarios;
  156. }
  157. public function saveCash()
  158. {
  159. if ($this->validate()) {
  160. $t = \Yii::$app->db->beginTransaction();
  161. if ($this->scenario == self::SCENARIO_EDIT) {
  162. $model = Cash::findOne(['id' => $this->id]);
  163. } else {
  164. $model = new Cash();
  165. }
  166. $model->attributes = $this->attributes;
  167. if (!$model->save()) {
  168. $t->rollBack();
  169. return [
  170. 'code' => 1,
  171. 'msg' => $model->getErrorSummary(false)[0]
  172. ];
  173. }
  174. $t->commit();
  175. return [
  176. 'code' => 0,
  177. 'msg' => '保存成功'
  178. ];
  179. } else {
  180. // 验证失败:$errors 是一个包含错误信息的数组
  181. return [
  182. 'code' => 1,
  183. "msg" => $this->getErrorSummary(false)[0]
  184. ];
  185. }
  186. }
  187. public function searchShopCash()
  188. {
  189. $query = Cash::find()->alias('c')
  190. ->leftJoin(['u' => User::tableName()], "u.id=c.user_id")
  191. ->select("c.*, u.nickname user_name, u.avatar_url user_head_url");
  192. $query->where(['c.is_delete' => Cash::IS_DELETE_NO, 'cash_type' => Cash::IS_CASH_TYPE_SHOP])->orderBy("id desc");
  193. // 搜索
  194. if ($this->search_key) {
  195. $query->andWhere([
  196. 'or',
  197. ['like', 'u.nickname', $this->search_key],
  198. ['like', 'c.name', $this->search_key]
  199. ]);
  200. }
  201. if ($this->status == 0 and $this->status != '') {//待审核
  202. $query->andWhere(['c.status' => 0]);
  203. }
  204. if ($this->status == 1) {//待打款
  205. $query->andWhere(['c.status' => 1]);
  206. }
  207. if ($this->status == 2) {//已打款
  208. $query->andWhere(['in', 'c.status', [2, 5]]);
  209. }
  210. if ($this->status == 3) {//无效
  211. $query->andWhere(['c.status' => 3]);
  212. }
  213. if ($this->flag == Export::EXPORT) {
  214. $export = new ExportList();
  215. $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();
  216. $export->shareExportData($data, $this->fields);
  217. }
  218. $list = pagination_make($query);
  219. foreach ($list['list'] as &$val) {
  220. $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
  221. $val['status_text'] = self::STATUS_TEXT[$val['status']];
  222. $val['type_text'] = self::TYPE_TEXT[$val['type']];
  223. $val['pay_type_text'] = self::PAY_TYPE_TEXT[$val['pay_type']];
  224. // $val['is_shop_text'] = self::CAHS_TYPE[$val['is_shop']];
  225. }
  226. return [
  227. 'code' => 0,
  228. 'msg' => 'success',
  229. 'data' => [
  230. 'export_list' => $this->getCustomField(),
  231. 'data' => $list['list'],
  232. 'pageNo' => $list['pageNo'],
  233. 'totalCount' => $list['totalCount']
  234. ]
  235. ];
  236. }
  237. public function delCash()
  238. {
  239. if ($this->validate()) {
  240. $t = \Yii::$app->db->beginTransaction();
  241. if ($this->scenario !== self::SCENARIO_DEL) {
  242. return [
  243. 'code' => 1,
  244. 'msg' => '删除失败'
  245. ];
  246. }
  247. $model = Cash::findOne(['id' => $this->key]);
  248. $model->is_delete = Cash::IS_DELETE_YES;
  249. if (!$model || !$model->save()) {
  250. $t->rollBack();
  251. return [
  252. 'code' => 1,
  253. 'msg' => $model->getErrorSummary(false)[0]
  254. ];
  255. }
  256. $t->commit();
  257. return [
  258. 'code' => 0,
  259. 'msg' => '删除成功'
  260. ];
  261. // 所有输入数据都有效 all inputs are valid
  262. } else {
  263. // 验证失败:$errors 是一个包含错误信息的数组
  264. return [
  265. 'code' => 1,
  266. "msg" => $this->getErrorSummary(false)[0]
  267. ];
  268. }
  269. }
  270. /**
  271. * 获取分销提现列表
  272. * @return array
  273. */
  274. public function getShareCashList()
  275. {
  276. $query = Cash::find()->alias('c')
  277. ->where(['c.is_delete' => Cash::IS_DELETE_NO, 'c.store_id' => $this->store_id])
  278. ->leftJoin('{{%user}} u', 'u.id=c.user_id');
  279. if ($this->cash_type == Cash::IS_CASH_TYPE_SHARE) {
  280. $query->leftJoin('{{%share}} s', 's.user_id=c.user_id');
  281. $query->andWhere(['s.is_delete' => Share::SHARE_NOT_DELETE, 'u.is_distributor' => User::IS_DISTRIBUTOR]);
  282. }
  283. $query->andWhere(['c.cash_type' => $this->cash_type ? $this->cash_type : Cash::IS_CASH_TYPE_SHARE]);
  284. if ($this->keyword) {
  285. if ($this->cash_type == Cash::IS_CASH_TYPE_SHARE) {
  286. $query->andWhere([
  287. 'or',
  288. ['like', 'u.nickname', $this->keyword],
  289. ['like', 's.name', $this->keyword]
  290. ]);
  291. } else {
  292. $query->andWhere(['like', 'u.nickname', $this->keyword]);
  293. }
  294. }
  295. if (isset($this->mch) && $this->mch == 1) {
  296. if ($this->mch_id > 0) {
  297. $query->andWhere(['c.mch_id' => $this->mch_id]);
  298. }else{
  299. $query->andWhere(['>', 'c.mch_id', 0]);
  300. }
  301. if($this->brands_id > 0){
  302. $mchList = Mch::find()->where(['is_delete' => 0, 'store_id' => get_store_id(), 'brands_id' => $this->brands_id])->select('id')->column();
  303. $query->andWhere(['c.mch_id' => $mchList]);
  304. }
  305. if (isset($this->mch_common_cat_id) && $this->mch_common_cat_id > 0) {
  306. $query->andWhere(['c.mch_id' => \app\models\Mch::find()->select('id')->where(['store_id' => $this->store_id, 'mch_common_cat_id' => $this->mch_common_cat_id])]);
  307. }
  308. }else{
  309. $query->andWhere(['c.mch_id' => 0]);
  310. }
  311. if ($this->dateStart) {
  312. $query->andWhere(['>=', 'c.created_at', strtotime($this->dateStart)]);
  313. }
  314. if ($this->dateEnd) {
  315. $query->andWhere(['<=', 'c.created_at', strtotime($this->dateEnd)]);
  316. }
  317. if ($this->status == Cash::STATUS_APPLY and $this->status != '') {//待审核
  318. $query->andWhere(['c.status' => Cash::STATUS_APPLY]);
  319. }
  320. if ($this->status == Cash::STATUS_CONFIRM) {//待打款
  321. // $query->andWhere(['c.status' => Cash::STATUS_CONFIRM]);
  322. $query->andWhere(['in', 'c.status', [Cash::STATUS_CONFIRM, Cash::STATUS_LG_CONFIRM]]);
  323. }
  324. if ($this->status == Cash::STATUS_GIVEN) {//已打款
  325. $query->andWhere(['in', 'c.status', [Cash::STATUS_GIVEN, Cash::STATUS_HAND, Cash::STATUS_RECHARGE]]);
  326. }
  327. if ($this->status == Cash::STATUS_REFUSE) {//无效
  328. $query->andWhere(['c.status' => Cash::STATUS_REFUSE]);
  329. }
  330. if ($this->id) {
  331. $query->andWhere(['s.id' => $this->id]);
  332. }
  333. if (isset($this->type) && $this->type > -1) {
  334. $query->andWhere(['c.type' => $this->type]);
  335. }
  336. if ($this->flag == Export::EXPORT) {
  337. $export = new ExportList();
  338. $data = $query->orderBy('id DESC')
  339. ->select(['c.*', 'u.nickname', 'u.avatar_url','u.platform', 'u.id user_id'])->asArray()->all();
  340. $export->shareExportData($data, $this->fields);
  341. }
  342. $query->distinct()->orderBy('id DESC')->select([
  343. 'c.*', 'u.nickname','u.platform', 'u.avatar_url','u.binding'
  344. ]);
  345. $pagination = pagination_make($query);
  346. $list = $pagination['list'];
  347. if($this->cash_type == Cash::IS_CASH_TYPE_MCH){
  348. $mchList = (new MchForm(['store_id' => $this->store_id]))->mchSelectList();
  349. $mchCommonCatSelectList = (new MchForm(['store_id' => $this->store_id]))->mchCommonCatSelectList();
  350. }
  351. $wechat = \Yii::$app->controller->wechatPay;
  352. try {
  353. $Wxv3 = (new \app\utils\WechatMerchant\WxV3($wechat));
  354. } catch (\Exception $e) {
  355. $Wxv3 = null;
  356. }
  357. foreach($list as &$value){
  358. // if (intval($value['pay_type']) === 1) {
  359. // \app\events\OrderEvent::handleWechatCashFail($Wxv3, $this->store_id, $value['order_no']);
  360. // }
  361. $value['wx_cash_status'] = intval($value['wx_cash_status']);
  362. $value['type_name'] = Cash::getTypeName($value);
  363. $value['status_name'] = Cash::getCashStatusName($value);
  364. $value['service_money'] = $value['service_charge'] * $value['price'] / 100;
  365. $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  366. $value['updated_at'] = !empty($value['updated_at']) ? date('Y-m-d H:i:s', $value['updated_at']) : '';
  367. $value['money'] = Cash::getServiceMoney($value);
  368. $saas_user = SaasUser::findOne(['mobile' => $value['binding']]);
  369. $value['avatar_url'] = !empty($saas_user['avatar']) ? $saas_user['avatar'] : $value['avatar_url'];
  370. $value['card_no'] = '';
  371. if (intval($value['type']) === Cash::TYPE_BANK) {
  372. if ($saas_user->withdraw_method) {
  373. $withdraw_method = json_decode($saas_user->withdraw_method, true);
  374. if (!empty($withdraw_method)) {
  375. foreach ($withdraw_method as $method_item) {
  376. if ($method_item['type'] === 'bank_card') {
  377. $value['card_no'] = $method_item['card_no'];
  378. }
  379. }
  380. }
  381. }
  382. }
  383. if($this->cash_type == Cash::IS_CASH_TYPE_MCH){
  384. $value['mch'] = $mchList['data'][$value['mch_id']];
  385. $value['mch_common_cat'] = $mchCommonCatSelectList['data'][$value['mch']['mch_common_cat_id']];
  386. }
  387. $cashExtArr = CashExt::find()->where(['cash_id' => $value['id']])->select('cash_price_type, real_price')->asArray()->all();
  388. $value['cash_ext_text'] = null;
  389. if ($cashExtArr) {
  390. $value['cash_ext_text'] = '提现明细:';
  391. foreach ($cashExtArr as $cashExtItem) {
  392. $value['cash_ext_text'] .= CashExt::$cashPriceTypeMap[$cashExtItem['cash_price_type']] . ':' . $cashExtItem['real_price'] . ';';
  393. }
  394. }
  395. $value['integral_appreciation_num'] = 0;
  396. $value['integral_appreciation_price'] = 0;
  397. if (intval($value['cash_type']) === Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
  398. $integralCashLog = IntegralAppreciationCashLog::findOne(['cash_id' => $value['id']]);
  399. $value['integral_appreciation_num'] = $integralCashLog->integral ?: 0;
  400. $value['integral_appreciation_price'] = $integralCashLog->integral_price ?: 0;
  401. }
  402. }
  403. if($this->export){
  404. return $this->export($list);
  405. }
  406. return [
  407. 'code' => 0,
  408. 'msg' => 'success',
  409. 'data' => [
  410. 'q' => $query->createCommand()->getRawSql(),
  411. 'export_list' => $this->getCustomField(),
  412. 'data' => $list,
  413. 'pageNo' => $pagination['pageNo'],
  414. 'totalCount' => $pagination['totalCount']
  415. ],
  416. ];
  417. }
  418. private function export($list) {
  419. $rows = [[
  420. 'ID',
  421. '入驻商',
  422. '会员信息',
  423. '手机号',
  424. '申请提现金额(元)',
  425. '手续费(元)',
  426. '实际打款(元)',
  427. '姓名',
  428. '提现到',
  429. '账户',
  430. '开户行',
  431. '状态',
  432. '申请时间',
  433. '审核时间',
  434. ]];
  435. foreach($list as $item){
  436. $r = [
  437. $item['id'],
  438. $item['mch'] ? $item['mch']['name'] : '',
  439. $item['nickname'],
  440. $item['binding'],
  441. $item['price'],
  442. $item['service_money'],
  443. $item['money'],
  444. $item['name'],
  445. Cash::$type[$item['type']] ?? '--',
  446. $item['mobile'],
  447. $item['bank_name'],
  448. Cash::$status[$item['status']] ?? '',
  449. $item['created_at'],
  450. $item['updated_at'],
  451. ];
  452. $rows[] = $r;
  453. }
  454. $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
  455. ->addRows($rows)->toBrowser();
  456. }
  457. /**
  458. * 用户分销提现列表
  459. */
  460. public function shareCashUserList() {
  461. if ($this->user_id) {
  462. $query = Cash::find()->alias('c')
  463. ->where(['c.is_delete' => Cash::IS_DELETE_NO, 'c.store_id' => $this->store_id, 'c.user_id' => $this->user_id])
  464. ->leftJoin('{{%user}} u', 'u.id=c.user_id')
  465. ->leftJoin('{{%share}} s', 's.user_id=c.user_id')
  466. ->andWhere(['s.is_delete' => Share::SHARE_NOT_DELETE, 'u.is_distributor' => User::IS_DISTRIBUTOR,
  467. 'c.cash_type' => Cash::IS_CASH_TYPE_SHARE]);
  468. $query->distinct()->orderBy('c.status ASC,c.created_at DESC')->select([
  469. 'c.*', 'u.nickname','u.platform', 'u.avatar_url','u.binding'
  470. ]);
  471. $pagination = pagination_make($query);
  472. $list = $pagination['list'];
  473. foreach($list as &$value){
  474. $value['service_money'] = $value['service_charge'] * $value['price'] / 100;
  475. $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  476. $value['updated_at'] = !empty($value['updated_at']) ? date('Y-m-d H:i:s', $value['updated_at']) : '';
  477. $value['money'] = Cash::getServiceMoney($value);
  478. $sassuser = SaasUser::findOne(['mobile' => $value['binding']]);
  479. $value['avatar_url'] = !empty($sassuser['avatar']) ? $sassuser['avatar'] : $value['avatar_url'];
  480. }
  481. return [
  482. 'code' => 0,
  483. 'msg' => 'success',
  484. 'data' => [
  485. 'export_list' => $this->getCustomField(),
  486. 'data' => $list,
  487. 'pageNo' => $pagination['pageNo'],
  488. 'totalCount' => $pagination['totalCount']
  489. ],
  490. ];
  491. } else {
  492. return [
  493. 'code' => 0,
  494. 'msg' => '缺少参数',
  495. ];
  496. }
  497. }
  498. /**
  499. * saas用户分销提现列表
  500. */
  501. public function shareCashSaasList() {
  502. $query = Cash::find()->alias('c')
  503. ->where(['c.is_delete' => Cash::IS_DELETE_NO, 'c.store_id' => $this->store_id])
  504. ->leftJoin('{{%saas_user}} s', 's.id=c.saas_id')
  505. ->andWhere(['c.cash_type' => Cash::IS_CASH_TYPE_SHARE]);
  506. $query->distinct()->orderBy('c.status ASC,c.created_at DESC')->select([
  507. 'c.*','s.name saas_name','s.avatar avatar_url','s.mobile telphone'
  508. ]);
  509. if ($this->keyword) {
  510. $query->andWhere([
  511. 'or',
  512. ['like', 's.name', $this->keyword]
  513. ]);
  514. }
  515. if ($this->dateStart) {
  516. $query->andWhere(['>=', 'c.created_at', strtotime($this->dateStart)]);
  517. }
  518. if ($this->dateEnd) {
  519. $query->andWhere(['<=', 'c.created_at', strtotime($this->dateEnd)]);
  520. }
  521. if ($this->status == Cash::STATUS_APPLY and $this->status != '') {//待审核
  522. $query->andWhere(['c.status' => Cash::STATUS_APPLY]);
  523. }
  524. if ($this->status == Cash::STATUS_CONFIRM) {//待打款
  525. $query->andWhere(['c.status' => Cash::STATUS_CONFIRM]);
  526. }
  527. if ($this->status == Cash::STATUS_GIVEN) {//已打款
  528. $query->andWhere(['in', 'c.status', [Cash::STATUS_GIVEN, Cash::STATUS_RECHARGE]]);
  529. }
  530. if ($this->status == Cash::STATUS_REFUSE) {//无效
  531. $query->andWhere(['c.status' => Cash::STATUS_REFUSE]);
  532. }
  533. $pagination = pagination_make($query);
  534. $list = $pagination['list'];
  535. foreach($list as &$value){
  536. $value['service_money'] = $value['service_charge'] * $value['price'] / 100;
  537. $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  538. $value['updated_at'] = !empty($value['updated_at']) ? date('Y-m-d H:i:s', $value['updated_at']) : '';
  539. $value['money'] = Cash::getServiceMoney($value);
  540. //$sassuser = SaasUser::findOne(['mobile' => $value['binding']]);
  541. //$value['avatar_url'] = !empty($sassuser['avatar']) ? $sassuser['avatar'] : $value['avatar_url'];
  542. }
  543. return [
  544. 'code' => 0,
  545. 'msg' => 'success',
  546. 'data' => [
  547. 'export_list' => $this->getCustomField(),
  548. 'data' => $list,
  549. 'pageNo' => $pagination['pageNo'],
  550. 'totalCount' => $pagination['totalCount']
  551. ],
  552. ];
  553. }
  554. public function getCustomField()
  555. {
  556. return [
  557. [
  558. 'key' => 'order_no',
  559. 'value' => '订单号'
  560. ],
  561. [
  562. 'key' => 'nickname',
  563. 'value' => '姓名',
  564. ],
  565. [
  566. 'key' => 'price',
  567. 'value' => '提现金额',
  568. ],
  569. [
  570. 'key' => 'addtime',
  571. 'value' => '申请日期',
  572. ],
  573. [
  574. 'key' => 'bank_name',
  575. 'value' => '银行名称',
  576. ],
  577. [
  578. 'key' => 'bank_card',
  579. 'value' => '打款账号',
  580. ],
  581. [
  582. 'key' => 'name',
  583. 'value' => '真实姓名',
  584. ],
  585. [
  586. 'key' => 'type',
  587. 'value' => '类型',
  588. ],
  589. [
  590. 'key' => 'pay_type',
  591. 'value' => '打款方式',
  592. ],
  593. [
  594. 'key' => 'pay_time',
  595. 'value' => '付款时间',
  596. ],
  597. ];
  598. }
  599. public function getCashSetting()
  600. {
  601. $cash = Option::get(OptionSetting::CASH_SETTING, get_store_id(), 'store')['value'];
  602. if ($cash) {
  603. $data = json_decode($cash, true);
  604. } else {
  605. $data = [
  606. 'pay_wechat' => 0,
  607. 'pay_alipay' => 0,
  608. 'bank' => 0,
  609. 'remaining_sum' => 0,
  610. 'lg' => 0,
  611. 'min_money' => 0, //最小提现额度
  612. 'cash_max_day' => 0, //每天最大提现额度
  613. 'cash_max_single_day' => 0, //每人每天提现额度
  614. 'cash_service_charge' => floatval(0), //提现手续费
  615. 'cash_price_type' => '',
  616. 'cash_price_amount' => floatval(0),
  617. 'cash_price_balance' => floatval(0),
  618. 'cash_price_integral' => floatval(0),
  619. 'auto_money' => 0
  620. ];
  621. }
  622. $data['auto_money'] = $data['auto_money'] ?? 0;
  623. return ['code' => 0, 'msg' => 'success', 'data' => $data];
  624. }
  625. public function editCashSetting()
  626. {
  627. if (isset($this->cash_price_type)) {
  628. $total_profit = 0;
  629. $cash_price_type = explode(',', $this->cash_price_type);
  630. if (in_array(CashExt::CASH_PRICE_TYPE_AMOUNT, $cash_price_type)) {
  631. $total_profit = bcadd($total_profit, $this->cash_price_amount, 2);
  632. }
  633. if (in_array(CashExt::CASH_PRICE_TYPE_INTEGRAL, $cash_price_type)) {
  634. $total_profit = bcadd($total_profit, $this->cash_price_integral, 2);
  635. }
  636. if (in_array(CashExt::CASH_PRICE_TYPE_BALANCE, $cash_price_type)) {
  637. $total_profit = bcadd($total_profit, $this->cash_price_balance, 2);
  638. }
  639. if ($total_profit != 100) {
  640. return [
  641. 'code' => 1,
  642. 'msg' => '佣金/积分/余额比例设置错误'
  643. ];
  644. }
  645. }
  646. $data = [
  647. 'pay_wechat' => $this->pay_wechat,
  648. 'pay_alipay' => $this->pay_alipay,
  649. 'bank' => $this->bank,
  650. 'remaining_sum' => $this->remaining_sum,
  651. 'lg' => $this->lg,
  652. 'min_money' => $this->min_money, //最小提现额度
  653. 'cash_max_day' => $this->cash_max_day, //每天最大提现额度
  654. 'cash_max_single_day' => $this->cash_max_single_day, //每人每天提现额度
  655. 'cash_service_charge' => floatval($this->cash_service_charge), //提现手续费
  656. 'cash_price_type' => $this->cash_price_type,
  657. 'cash_price_amount' => floatval($this->cash_price_amount),
  658. 'cash_price_balance' => floatval($this->cash_price_balance),
  659. 'cash_price_integral' => floatval($this->cash_price_integral),
  660. 'auto_money' => floatval($this->auto_money),
  661. ];
  662. Option::set(OptionSetting::CASH_SETTING, json_encode($data), get_store_id(), 'store');
  663. return ['code' => 0, 'msg' => 'success', 'data' => $data];
  664. }
  665. public function getCashList()
  666. {
  667. $query = Cash::find()->alias('c')
  668. ->where(['c.is_delete' => Cash::IS_DELETE_NO, 'c.store_id' => $this->store_id])
  669. ->leftJoin(['u' => User::tableName()], 'u.id=c.user_id');
  670. if ($this->keyword) {
  671. $query->andWhere(['like', 'u.nickname', $this->keyword]);
  672. }
  673. if ($this->dateStart) {
  674. $query->andWhere(['>=', 'c.created_at', strtotime($this->dateStart)]);
  675. }
  676. if ($this->dateEnd) {
  677. $query->andWhere(['<=', 'c.created_at', strtotime($this->dateEnd)]);
  678. }
  679. if ($this->status == Cash::STATUS_APPLY and $this->status !== '') {//待审核
  680. $query->andWhere(['c.status' => Cash::STATUS_APPLY]);
  681. }
  682. if ($this->status == Cash::STATUS_CONFIRM) {//待打款
  683. $query->andWhere(['in', 'c.status', [Cash::STATUS_CONFIRM, Cash::STATUS_LG_CONFIRM]]);
  684. }
  685. if ($this->status == Cash::STATUS_GIVEN) {//已打款
  686. $query->andWhere(['in', 'c.status', [Cash::STATUS_GIVEN, Cash::STATUS_HAND, Cash::STATUS_RECHARGE]]);
  687. }
  688. if ($this->status == Cash::STATUS_REFUSE) {//无效
  689. $query->andWhere(['c.status' => Cash::STATUS_REFUSE]);
  690. }
  691. if ($this->id) {
  692. $query->andWhere(['s.id' => $this->id]);
  693. }
  694. if (isset($this->type) && $this->type > -1) {
  695. $query->andWhere(['c.type' => $this->type]);
  696. }
  697. if ($this->flag == Export::EXPORT) {
  698. $export = new ExportList();
  699. $data = $query->orderBy('id DESC')
  700. ->select(['c.*', 'u.nickname', 'u.avatar_url','u.platform', 'u.id user_id'])->asArray()->all();
  701. $export->shareExportData($data, $this->fields);
  702. }
  703. $query->distinct()->orderBy('id DESC')->select([
  704. 'c.*', 'u.nickname','u.platform', 'u.avatar_url','u.binding'
  705. ]);
  706. $pagination = pagination_make($query);
  707. $list = $pagination['list'];
  708. foreach($list as &$value){
  709. $value['wx_cash_status'] = intval($value['wx_cash_status']);
  710. $value['type_name'] = Cash::getTypeName($value);
  711. $value['status_name'] = Cash::getCashStatusName($value);
  712. $value['service_money'] = $value['service_charge'] * $value['price'] / 100;
  713. $value['created_at'] = date('Y-m-d H:i:s', $value['created_at']);
  714. $value['updated_at'] = !empty($value['updated_at']) ? date('Y-m-d H:i:s', $value['updated_at']) : '';
  715. $value['money'] = Cash::getServiceMoney($value);
  716. $saas_user = SaasUser::findOne(['mobile' => $value['binding']]);
  717. $value['avatar_url'] = !empty($saas_user['avatar']) ? $saas_user['avatar'] : $value['avatar_url'];
  718. $value['card_no'] = '';
  719. if ($saas_user->withdraw_method) {
  720. $withdraw_method = json_decode($saas_user->withdraw_method, true);
  721. if (!empty($withdraw_method)) {
  722. foreach ($withdraw_method as $method_item) {
  723. if (intval($value['type']) === Cash::TYPE_ALIPAY && $method_item['type'] === 'alipay') {
  724. $value['cash_img'] = $method_item['cash_img'];
  725. } else if (intval($value['type']) === Cash::TYPE_WX && $method_item['type'] === 'wechat'){
  726. $value['cash_img'] = $method_item['cash_img'];
  727. } else if (intval($value['type']) === Cash::TYPE_BANK && $method_item['type'] === 'bank_card') {
  728. $value['card_no'] = $method_item['card_no'];
  729. }
  730. }
  731. }
  732. }
  733. $value['real_price'] = $value['money'];;
  734. $cashExtArr = CashExt::find()->where(['cash_id' => $value['id']])->select('cash_price_type, real_price')->asArray()->all();
  735. $value['cash_ext_text'] = null;
  736. if ($cashExtArr) {
  737. $value['cash_ext_text'] = '提现明细:';
  738. foreach ($cashExtArr as $cashExtItem) {
  739. if ($cashExtItem['cash_price_type'] == CashExt::CASH_PRICE_TYPE_AMOUNT) {
  740. $value['real_price'] = $cashExtItem['real_price'];
  741. }
  742. $value['cash_ext_text'] .= CashExt::$cashPriceTypeMap[$cashExtItem['cash_price_type']] . ':' . $cashExtItem['real_price'] . ';';
  743. }
  744. }
  745. $value['integral_appreciation_num'] = 0;
  746. $value['integral_appreciation_price'] = 0;
  747. }
  748. if($this->export){
  749. return $this->export($list);
  750. }
  751. return [
  752. 'code' => 0,
  753. 'msg' => 'success',
  754. 'data' => [
  755. 'q' => $query->createCommand()->getRawSql(),
  756. 'export_list' => $this->getCustomField(),
  757. 'data' => $list,
  758. 'pageNo' => $pagination['pageNo'],
  759. 'totalCount' => $pagination['totalCount']
  760. ],
  761. ];
  762. }
  763. }