StoreWithdraw.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\store\model;
  11. use app\model\BaseModel;
  12. use app\model\store\Store;
  13. use app\model\system\Pay;
  14. use think\facade\Cache;
  15. class StoreWithdraw extends BaseModel
  16. {
  17. public $status = [
  18. 0 => '待审核',
  19. 1 => '待转账',
  20. 2 => '已转账',
  21. -1 => '已拒绝',
  22. ];
  23. public $settlement_type = array (
  24. 'apply' => '申请结算',
  25. 'day' => '每日结算',
  26. 'week' => '每周结算',
  27. 'month' => '每月结算',
  28. );
  29. /**
  30. * 门店账户提现
  31. * @param $params
  32. */
  33. public function apply($params)
  34. {
  35. $money = $params[ 'money' ] ?? 0;
  36. $site_id = $params[ 'site_id' ];
  37. $store_id = $params[ 'store_id' ];
  38. $settlement_type = $params[ 'settlement_type' ] ?? 'apply';
  39. $store_config_model = new Config();
  40. $config = $store_config_model->getStoreWithdrawConfig($site_id)[ 'data' ][ 'value' ] ?? [];
  41. $is_audit = $config[ 'is_audit' ];
  42. $is_settlement = $config[ 'is_settlement' ];
  43. if ($is_settlement == 0)
  44. return $this->error([], '门店结算未开启');
  45. $withdraw_least = $config[ 'withdraw_least' ];
  46. $store_model = new Store();
  47. $store_condition = array (
  48. [ 'site_id', '=', $site_id ],
  49. [ 'store_id', '=', $store_id ]
  50. );
  51. $store_info = $store_model->getStoreInfo($store_condition)[ 'data' ] ?? [];
  52. if (empty($store_info))
  53. return $this->error([], '找不到可结算的门店');
  54. if ($settlement_type == 'apply') {
  55. if ($money < $withdraw_least) {
  56. return $this->error([], '门店最低结算金额为' . $withdraw_least . '元');
  57. }
  58. }
  59. if ($money > $store_info[ 'account' ]) {
  60. return $this->error([], '申请结算金额不能大于门店最大可结算金额');
  61. }
  62. $bank_type = $store_info[ 'bank_type' ];
  63. if ($bank_type == 0)
  64. return $this->error([], '当前门店未配置结算账户,无法结算');
  65. $transfer_type = array ( 3 => 'bank', 2 => 'alipay', 1 => 'wechatpay' )[ $store_info[ 'bank_type' ] ];//转账方式
  66. $transfer_type_list = $this->getTransferType($site_id);
  67. $transfer_type_name = $transfer_type_list[ $transfer_type ] ?? '';
  68. switch ( $transfer_type ) {
  69. case 'bank':
  70. $bank_name = $store_info[ 'bank_type_name' ];
  71. $realname = $store_info[ 'bank_user_name' ];//户头
  72. $account_number = $store_info[ 'bank_type_account' ];
  73. break;
  74. case 'alipay':
  75. $realname = $store_info[ 'bank_user_name' ];
  76. $account_number = $store_info[ 'bank_type_account' ];
  77. break;
  78. case 'wechatpay':
  79. $account_number = $store_info[ 'bank_user_name' ];
  80. break;
  81. }
  82. $data = array (
  83. 'site_id' => $site_id,
  84. 'withdraw_no' => $this->createWithdrawNo(),
  85. 'store_name' => $store_info[ 'store_name' ],
  86. 'store_id' => $store_id,
  87. 'transfer_type' => $transfer_type,
  88. 'transfer_type_name' => $transfer_type_name,
  89. 'money' => $money,
  90. 'apply_time' => time(),
  91. 'status' => 0,
  92. 'status_name' => $this->status[ 0 ],
  93. 'realname' => $realname ?? '',
  94. 'bank_name' => $bank_name ?? '',
  95. 'account_number' => $account_number ?? '',
  96. 'settlement_type' => $settlement_type,
  97. 'settlement_type_name' => $this->settlement_type[ $settlement_type ]
  98. );
  99. model('store_withdraw')->startTrans();
  100. try {
  101. $withdraw_id = model('store_withdraw')->add($data);
  102. $store_account_model = new StoreAccount();
  103. $store_account_data = array (
  104. 'account_data' => -$money,
  105. 'site_id' => $site_id,
  106. 'store_id' => $store_id,
  107. 'from_type' => 'withdraw',
  108. 'remark' => '门店结算,金额' . $money,
  109. 'related_id' => $withdraw_id
  110. );
  111. $result = $store_account_model->addStoreAccount($store_account_data);
  112. if ($result[ 'code' ] < 0) {
  113. model('store_withdraw')->rollback();
  114. return $result;
  115. }
  116. //增加结算中余额
  117. model('store')->setInc([ [ 'store_id', '=', $store_id ] ], 'account_apply', $money);
  118. //结算无需审核的话,就直接结算
  119. if ($is_audit == 0) {
  120. $result = $this->agree([ 'withdraw_id' => $withdraw_id, 'site_id' => $site_id, 'store_id' => $store_id ]);
  121. if ($result[ 'code' ] < 0) {
  122. model('store_withdraw')->rollback();
  123. return $result;
  124. }
  125. }
  126. model('store_withdraw')->commit();
  127. return $this->success($withdraw_id);
  128. } catch (\Exception $e) {
  129. model('store_withdraw')->rollback();
  130. return $this->error('', $e->getMessage());
  131. }
  132. }
  133. /**
  134. * 同意结算申请
  135. * @param $condition
  136. */
  137. public function agree($params)
  138. {
  139. $site_id = $params[ 'site_id' ];
  140. $store_id = $params[ 'store_id' ] ?? 0;
  141. $withdraw_id = $params[ 'withdraw_id' ];
  142. $condition = array (
  143. [ 'site_id', '=', $site_id ],
  144. [ 'withdraw_id', '=', $withdraw_id ],
  145. [ 'status', '=', 0 ]
  146. );
  147. if ($store_id > 0) {
  148. $condition[] = [ 'store_id', '=', $store_id ];
  149. }
  150. $info = model('store_withdraw')->getInfo($condition);
  151. if (empty($info))
  152. return $this->error();
  153. $data = array (
  154. 'status' => 1,
  155. 'status_name' => $this->status[ 1 ],
  156. 'audit_time' => time(),
  157. );
  158. $result = model('store_withdraw')->update($data, $condition);
  159. return $this->success();
  160. }
  161. /**
  162. * 拒绝结算申请
  163. * @param $condition
  164. */
  165. public function refuse($params)
  166. {
  167. $site_id = $params[ 'site_id' ];
  168. $store_id = $params[ 'store_id' ] ?? 0;
  169. $withdraw_id = $params[ 'withdraw_id' ];
  170. $condition = array (
  171. [ 'site_id', '=', $site_id ],
  172. [ 'withdraw_id', '=', $withdraw_id ],
  173. [ 'status', '=', 0 ]
  174. );
  175. if ($store_id > 0) {
  176. $condition[] = [ 'store_id', '=', $store_id ];
  177. }
  178. $info = model('store_withdraw')->getInfo($condition);
  179. if (empty($info))
  180. return $this->error();
  181. model('store_withdraw')->startTrans();
  182. try {
  183. $data = array (
  184. 'status' => -1,
  185. 'status_name' => $this->status[ -1 ],
  186. 'refuse_reason' => $params[ 'refuse_reason' ],
  187. 'audit_time' => time(),
  188. );
  189. model('store_withdraw')->update($data, $condition);
  190. $money = $info[ 'money' ];
  191. //增加现金余额
  192. $store_id = $info[ 'store_id' ];
  193. $store_account_model = new StoreAccount();
  194. $store_account_data = array (
  195. 'account_data' => $money,
  196. 'site_id' => $site_id,
  197. 'store_id' => $store_id,
  198. 'from_type' => 'withdraw',
  199. 'remark' => '门店结算被拒绝,返还金额' . $money,
  200. 'related_id' => $withdraw_id
  201. );
  202. $result = $store_account_model->addStoreAccount($store_account_data);
  203. if ($result[ 'code' ] < 0) {
  204. model('store_withdraw')->rollback();
  205. return $result;
  206. }
  207. //减少结算中余额
  208. model('store')->setDec([ [ 'store_id', '=', $store_id ] ], 'account_apply', $money);
  209. model('store_withdraw')->commit();
  210. return $this->success();
  211. } catch (\Exception $e) {
  212. model('store_withdraw')->rollback();
  213. return $this->error('', $e->getMessage());
  214. }
  215. }
  216. /**
  217. * 结算转账完成
  218. * @param $id
  219. */
  220. public function transferFinish($params)
  221. {
  222. $site_id = $params[ 'site_id' ];
  223. $store_id = $params[ 'store_id' ] ?? 0;
  224. $withdraw_id = $params[ 'withdraw_id' ];
  225. $condition = array (
  226. [ 'site_id', '=', $site_id ],
  227. [ 'withdraw_id', '=', $withdraw_id ]
  228. );
  229. if ($store_id > 0) {
  230. $condition[] = [ 'store_id', '=', $store_id ];
  231. }
  232. $info = model('store_withdraw')->getInfo($condition);
  233. if (empty($info))
  234. return $this->error();
  235. $transfer_time = time();
  236. model('store_withdraw')->startTrans();
  237. try {
  238. $store_id = $info[ 'store_id' ];
  239. $data = [
  240. 'status' => 2,
  241. 'status_name' => $this->status[ 2 ],
  242. 'transfer_time' => $transfer_time,
  243. 'voucher_img' => $params[ 'voucher_img' ] ?? '',
  244. 'voucher_desc' => $params[ 'voucher_desc' ] ?? ''
  245. ];
  246. model('store_withdraw')->update($data, $condition);
  247. $store_condition = array (
  248. [ 'store_id', '=', $store_id ]
  249. );
  250. $money = $info[ 'money' ];
  251. //增加已结算余额
  252. model('store')->setInc($store_condition, 'account_withdraw', $money);
  253. //减少结算中余额
  254. model('store')->setDec($store_condition, 'account_apply', $money);
  255. model('store_withdraw')->commit();
  256. return $this->success();
  257. } catch (\Exception $e) {
  258. model('store_withdraw')->rollback();
  259. return $this->error('', $e->getMessage());
  260. }
  261. }
  262. /**
  263. * 转账方式
  264. */
  265. public function getTransferType($site_id = 0)
  266. {
  267. $pay_model = new Pay();
  268. $transfer_type_list = $pay_model->getTransferType($site_id);
  269. $data = [];
  270. foreach ($transfer_type_list as $k => $v) {
  271. $data[ $k ] = $v;
  272. }
  273. return $data;
  274. }
  275. /**
  276. * 结算
  277. * @param $condition
  278. */
  279. public function getStoreWithdrawInfo($condition, $field = '*')
  280. {
  281. $info = model('store_withdraw')->getInfo($condition, $field);
  282. return $this->success($info);
  283. }
  284. /**
  285. * 结算记录
  286. * @param $condition
  287. * @param string $field
  288. */
  289. public function getStoreWithdrawList($condition = [], $field = '*', $order = '', $limit = null)
  290. {
  291. $list = model('store_withdraw')->getList($condition, $field, $order, '', '', '', $limit);
  292. return $this->success($list);
  293. }
  294. /**
  295. * 获取账户分页列表
  296. * @param array $condition
  297. * @param int $page
  298. * @param int $page_size
  299. * @param string $order
  300. * @param string $field
  301. * @return array|\multitype
  302. */
  303. public function getStoreWithdrawPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'apply_time desc', $field = '*', $alias = 'a', $join = [])
  304. {
  305. $list = model('store_withdraw')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  306. return $this->success($list);
  307. }
  308. /**
  309. * 获取结算单数量
  310. * @param $condition
  311. * @return array
  312. */
  313. public function getStoreWithdrawCount($condition)
  314. {
  315. $data = model('store_withdraw')->getCount($condition);
  316. return $this->success($data);
  317. }
  318. /**
  319. * 获取结算单字段总和
  320. * @param $condition
  321. * @return array
  322. */
  323. public function getStoreWithdrawSum($condition, $field)
  324. {
  325. $data = model('store_withdraw')->getSum($condition, $field);
  326. return $this->success($data);
  327. }
  328. /**
  329. * 结算详情
  330. * @param $params
  331. * @return array
  332. */
  333. public function detail($params)
  334. {
  335. $withdraw_id = $params[ 'withdraw_id' ];
  336. $site_id = $params[ 'site_id' ] ?? 0;
  337. $store_id = $params[ 'store_id' ] ?? 0;
  338. $condition = array (
  339. [ 'withdraw_id', '=', $withdraw_id ]
  340. );
  341. if ($site_id > 0) {
  342. $condition[] = [ 'site_id', '=', $site_id ];
  343. }
  344. if ($store_id > 0) {
  345. $condition[] = [ 'store_id', '=', $store_id ];
  346. }
  347. $info = model('store_withdraw')->getInfo($condition);
  348. if (empty($info))
  349. return $this->error();
  350. $settlement_type = $info[ 'settlement_type' ];
  351. //非主动申请的结算,会有周期性结算信息
  352. if ($settlement_type != 'apply') {
  353. $settlement_model = new Settlement();
  354. $settlement_condition = array (
  355. [ 'withdraw_id', '=', $withdraw_id ]
  356. );
  357. $settlement_info = $settlement_model->getSettlementInfo($settlement_condition)[ 'data' ] ?? [];
  358. if (!empty($settlement_info))
  359. $info[ 'settlement_info' ] = $settlement_info;
  360. }
  361. return $this->success($info);
  362. }
  363. /**
  364. * 翻译
  365. * @param $data
  366. */
  367. // public function translate($data){
  368. // $settlement_type = $data['settlement_type'] ?? '';
  369. // if(!empty($settlement_type)){
  370. // $data['settlement_type_name'] = $settlement_type;
  371. // }
  372. //
  373. // return $data;
  374. // }
  375. /**
  376. * 结算流水号
  377. */
  378. private function createWithdrawNo()
  379. {
  380. $cache = Cache::get('store_withdraw_no' . time());
  381. if (empty($cache)) {
  382. Cache::set('niutk' . time(), 1000);
  383. $cache = Cache::get('store_withdraw_no' . time());
  384. } else {
  385. $cache = $cache + 1;
  386. Cache::set('store_withdraw_no' . time(), $cache);
  387. }
  388. $no = date('Ymdhis', time()) . rand(1000, 9999) . $cache;
  389. return $no;
  390. }
  391. }