WithdrawCashService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/12/2
  6. * Time: 下午3:34
  7. */
  8. namespace App\Modules\Finance\Services;
  9. use App\Modules\Channel\Services\ChannelService;
  10. use App\Modules\Finance\Models\FinancialStat;
  11. use App\Modules\Finance\Models\WithdrawCash;
  12. use App\Modules\Manage\Services\ManageService;
  13. use App\Modules\User\Services\UserService;
  14. class WithdrawCashService
  15. {
  16. /**
  17. * 自动提现
  18. * @param unknown_type $request
  19. */
  20. public static function auto_add_withdrawCash($distribution_channel_id,$amount,$remark) {
  21. $distribution_channel_name = '';
  22. if(!is_numeric($amount)) {
  23. return response()->error("PARAM_ERROR");
  24. }
  25. if($amount < 100) {
  26. //TODO 渠道14,取消100 金额的判断 大于1一块钱就可以
  27. if($distribution_channel_id == 14 || $distribution_channel_id = "14" || $distribution_channel_id = '14') {
  28. if($amount < 1) {
  29. return response()->error("WITHDRAW_CASH_AMOUNT");
  30. }
  31. } else {
  32. return response()->error("WITHDRAW_CASH_AMOUNT");
  33. }
  34. }
  35. if($amount >= (20 * 10000)) {
  36. //TODO 取消单笔提现额度限制
  37. //return response()->error("WITHDRAW_CASH_AMOUNT_MORE");
  38. }
  39. $financialStat = FinancialStatService::getFinancialStatSingle($distribution_channel_id);
  40. if(empty($financialStat) || $financialStat['enable_withdrawal_amount'] < $amount) {
  41. //可提现金额不够
  42. return response()->error("WITHDRAW_CASH_AMOUNT_INSUFFICIEN");
  43. }
  44. if(FinancialConfigService::isFrozenDistributionChannel($distribution_channel_id)) {
  45. //渠道被冻结
  46. return response()->error("WITHDRAW_CASH_AMOUNT_FROZEN");
  47. }
  48. //判断账户是否设置
  49. if(!CashAccountService::isCashAccountExits($distribution_channel_id)) {
  50. return response()->error("WITHDRAW_CASH_AMOUNT_ACCOUNT");
  51. }
  52. //判断今天是否已经提现
  53. if(WithdrawCashService::isWithdrawCashChannelToToday($distribution_channel_id)) {
  54. return response()->error("WITHDRAW_CASH_TODAY_USE");
  55. }
  56. WithdrawCashService::addWithdrawCash($distribution_channel_id, $amount, $remark);
  57. $financialStatUp = FinancialStatService::updateFinancialStatByWithdraw($distribution_channel_id, $amount);
  58. //修改可提现总额
  59. $enable_amount = $financialStatUp['enable_withdrawal_amount'];
  60. //修改提现中金额
  61. $withdraw_pending_amount = $financialStatUp['withdraw_pending_amount'];
  62. return response()->success(compact('enable_amount', 'withdraw_pending_amount'));
  63. }
  64. /**
  65. * 保存提现信息
  66. * @param $channelId 渠道ID
  67. * @param $amount 提现金额
  68. * @param $remark 备注
  69. * @return mixed
  70. */
  71. public static function addWithdrawCash($channelId, $amount, $remark) {
  72. $tallage = FinanceService::getWithdrawCashTallage($channelId, $amount);
  73. $is_company = 0;
  74. $channelName = ChannelService::getChannelNicknameById($channelId);
  75. $cashAccount = CashAccountService::getCashAccountSingle($channelId);
  76. if(!empty($cashAccount)) {
  77. $is_company = $cashAccount['is_company'];
  78. }
  79. $dataWithdrawCash['distribution_channel_id'] = $channelId;
  80. $dataWithdrawCash['distribution_channel_name'] = $channelName;
  81. $dataWithdrawCash['amount'] = $amount;
  82. $dataWithdrawCash['tallage'] = $tallage;
  83. $dataWithdrawCash['status'] = self::getWithdrawCashStatusStr(0);
  84. $dataWithdrawCash['remark'] = $remark;
  85. $dataWithdrawCash['is_company'] = $is_company;
  86. //$dataWithdrawCash['serial_number'] = "";
  87. //$dataWithdrawCash['check_user_id'] = "";
  88. //$dataWithdrawCash['check_user_name'] = "";
  89. $withdrawCash = WithdrawCash::create($dataWithdrawCash);
  90. return $withdrawCash;
  91. }
  92. /**
  93. * 更新提现信息状态
  94. * @param $id
  95. * @param $userId
  96. * @param $statusIn 状态 查看接口 WithdrawCashService::getWithdrawCashStatusList
  97. * @param $remark
  98. * @param string $serialNumber
  99. * @return mixed
  100. */
  101. public static function updateWithdrawCashStatus($id, $userId = '', $statusIn, $remark='', $serialNumber='') {
  102. //TODO 对于一个提现记录只有一条打款是没有Bug的。
  103. //TODO 一个提现记录对应多个支付通道打款,存在逻辑bug
  104. $withdrawCash = WithdrawCash::where('id', $id)->first();
  105. if(!empty($withdrawCash) && is_numeric($statusIn)) {
  106. if(WithdrawCashService::isWithdrawCashStatusPaymentIngStr($withdrawCash['status'])) {
  107. //当前状态是 打款中 可以修改状态
  108. } else {
  109. if(WithdrawCashService::isEditWithdrawCashStatus($id)) {
  110. return $withdrawCash;
  111. }
  112. }
  113. $userName = "";
  114. if($userId) {
  115. $manage = ManageService::getById($userId);
  116. if(!empty($manage)) {
  117. $userName = $manage['nickname'];
  118. }
  119. }
  120. $statusStr = WithdrawCashService::getWithdrawCashStatusStr($statusIn);
  121. if(empty($statusStr)) {
  122. } else {
  123. if($statusStr) {
  124. $withdrawCash['status'] = $statusStr;
  125. }
  126. }
  127. if(WithdrawCashService::isWithdrawCashStatusCodeSuccess($statusIn)) {
  128. //成功提现
  129. $financialStat = FinancialStat::getByDistributionChannel($withdrawCash['distribution_channel_id']);
  130. //修改累计提现金额
  131. $financialStat['accumulative_withdrawal_amount'] = (float)$financialStat['accumulative_withdrawal_amount'] + (float)$withdrawCash['amount'];
  132. //修改提现中金额
  133. $financialStat['withdraw_pending_amount'] = (float)$financialStat['withdraw_pending_amount'] - (float)$withdrawCash['amount'];
  134. $financialStat->save();
  135. }
  136. if($userId) {
  137. $withdrawCash['check_user_id'] = $userId;
  138. }
  139. if($userName) {
  140. $withdrawCash['check_user_name'] = $userName;
  141. }
  142. if($remark) {
  143. $withdrawCash['remark'] = $remark;
  144. }
  145. if($serialNumber) {
  146. if(strpos($withdrawCash['serial_number'], $serialNumber) !== false) {
  147. //包含
  148. } else {
  149. $serialNumber = $withdrawCash['serial_number'].",".$serialNumber;
  150. }
  151. $withdrawCash['serial_number'] = $serialNumber;
  152. }
  153. $withdrawCash->save();
  154. }
  155. return $withdrawCash;
  156. }
  157. /**
  158. * 获取一个提现信息
  159. * @param $id
  160. * @return mixed
  161. */
  162. public static function getWithdrawCash($id) {
  163. $withdrawCash = WithdrawCash::where('id', $id)->first();
  164. return $withdrawCash;
  165. }
  166. /**
  167. * 获取提现列表
  168. * @param string $channel_id 可不传
  169. * @param string $channel_name 可不传
  170. * @param string $start_date 可不传
  171. * @param string $end_date 可不传
  172. * @param string $statusIn 状态 查看接口 WithdrawCashService::getWithdrawCashStatusList 可不传
  173. * @param bool $is_all 可不传
  174. * @return mixed
  175. */
  176. public static function getList($channel_id='', $channel_name='', $start_date='',$end_date='', $statusIn = '', $is_all=false) {
  177. $status = false;
  178. if(is_numeric($statusIn)) {
  179. $status = self::getWithdrawCashStatusStrList($statusIn);
  180. }
  181. return WithdrawCash::getListByDistributionChannel($channel_id, $channel_name, $start_date, $end_date, $status, $is_all);
  182. }
  183. /**
  184. * 财务对账
  185. * @param $params[]
  186. * channel_id:渠道ID 可选
  187. * channel_name:渠道名称 可选
  188. * account_name: 打款人姓名
  189. * search_name: 搜索名称
  190. * start_date:开始时间 可选
  191. * end_date:结束时间 可选
  192. * is_frozen:冻结 0:正常状态; -1:冻结状态 可选
  193. * is_company: 0:私人; 1:公司
  194. * @param string $statusIn 状态 查看接口 WithdrawCashService::getWithdrawCashStatusList 可不传
  195. * @param $isAll
  196. * @return mixed
  197. */
  198. public static function getFinancialCounting($params=[], $statusIn = '', $isAll = '') {
  199. if(is_numeric($statusIn)) {
  200. $params['status'] = self::getWithdrawCashStatusStrList($statusIn);
  201. }
  202. return WithdrawCash::getFinancialCountingParam($params, $isAll);
  203. }
  204. /**
  205. * 财务审核
  206. * @param $params[]
  207. * channel_id:渠道ID 可选
  208. * channel_name:渠道名称 可选
  209. * account_name: 打款人姓名
  210. * search_name: 搜索名称 可选
  211. * start_date:开始时间 可选
  212. * end_date:结束时间 可选
  213. * is_frozen:冻结 0:正常状态; -1:冻结状态 可选
  214. * is_company: 0:私人; 1:公司
  215. * @param string $statusIn 状态 查看接口 WithdrawCashService::getWithdrawCashStatusList 可不传
  216. * @param $isAll
  217. * @return mixed
  218. */
  219. public static function getFinancialAudit($params=[], $statusIn = '', $isAll = false) {
  220. if(is_numeric($statusIn)) {
  221. $params['status'] = self::getWithdrawCashStatusStrList($statusIn);
  222. }
  223. return WithdrawCash::getFinancialAuditParam($params, $isAll);
  224. }
  225. /**
  226. * 判断当前渠道今天有没有提现
  227. * @param $channelId
  228. * @return bool true:今天已经提现
  229. */
  230. public static function isWithdrawCashChannelToToday($channelId) {
  231. $withdrawCash = WithdrawCash::getWithdrawCashLastRecord($channelId);
  232. if(empty($withdrawCash)) {
  233. return false;
  234. }
  235. $todayStart = strtotime(date('Y-m-d', time()).' 00:00:00');
  236. $createTime = strtotime($withdrawCash['created_at']);
  237. if($createTime > $todayStart) {
  238. return true;
  239. }
  240. return false;
  241. }
  242. /**
  243. * 是否能修改提现状态
  244. * @param $withdrawCashId
  245. * @return bool true:不能修改
  246. */
  247. public static function isEditWithdrawCashStatus($withdrawCashId) {
  248. $withdrawCash = WithdrawCash::where('id', $withdrawCashId)->first();
  249. if(empty($withdrawCash)) {
  250. return false;
  251. }
  252. if($withdrawCash['status'] == "打款中"
  253. || $withdrawCash['status'] == "自动打款中"
  254. || $withdrawCash['status'] == "人工打款中"
  255. || $withdrawCash['status'] == "打款成功"
  256. || $withdrawCash['status'] == "自动打款成功"
  257. || $withdrawCash['status'] == "人工打款成功" ) {
  258. return true;
  259. }
  260. return false;
  261. }
  262. /**
  263. * 判断当前提现记录是否 已打款 打款成功 状态
  264. * @param $withdrawCashId
  265. * @return bool true:已经打款 打款成功
  266. */
  267. public static function isWithdrawCashStatusSuccess($withdrawCashId) {
  268. $withdrawCash = WithdrawCash::where('id', $withdrawCashId)->first();
  269. if(empty($withdrawCash)) {
  270. return false;
  271. }
  272. if($withdrawCash['status'] == "打款成功"
  273. || $withdrawCash['status'] == "自动打款成功"
  274. || $withdrawCash['status'] == "人工打款成功" ) {
  275. return true;
  276. }
  277. return false;
  278. }
  279. /**
  280. * 待打款状态
  281. * @param string $statusStr
  282. * @return bool
  283. */
  284. public static function isWithdrawCashStatusPaymentWaitStr($statusStr = '') {
  285. if($statusStr == "待打款"
  286. || $statusStr == "自动待打款"
  287. || $statusStr == "人工待打款") {
  288. return true;
  289. }
  290. return false;
  291. }
  292. /**
  293. * 打款中状态
  294. * @param string $statusStr
  295. * @return bool
  296. */
  297. public static function isWithdrawCashStatusPaymentIngStr($statusStr = '') {
  298. if($statusStr == "打款中"
  299. || $statusStr == "自动打款中"
  300. || $statusStr == "人工打款中") {
  301. return true;
  302. }
  303. return false;
  304. }
  305. /**
  306. * 成功打款Code
  307. * @param string $statusIn
  308. * @return bool
  309. */
  310. public static function isWithdrawCashStatusCodeSuccess($statusIn = '') {
  311. if($statusIn == 30 || $statusIn == 31 || $statusIn == 32) {
  312. return true;
  313. }
  314. return false;
  315. }
  316. /**
  317. * 成功打款
  318. * @param string $statusStr
  319. * @return bool
  320. */
  321. public static function isWithdrawCashStatusStrSuccess($statusStr = '') {
  322. if($statusStr == "打款成功"
  323. || $statusStr == "自动打款成功"
  324. || $statusStr == "人工打款成功") {
  325. return true;
  326. }
  327. return false;
  328. }
  329. /**
  330. * 获取成功和失败的状态
  331. * @return array
  332. */
  333. public static function getWithdrawCashStatusStrSuccessAndFailList() {
  334. $status = [];
  335. $status[] = "打款成功";
  336. $status[] = "自动打款成功";
  337. $status[] = "人工打款成功";
  338. $status[] = "打款失败";
  339. $status[] = "自动打款失败";
  340. $status[] = "人工打款失败";
  341. return $status;
  342. }
  343. /**
  344. * 获取 审核失败 打款成功 打款失败 状态
  345. * @return array
  346. */
  347. public static function getWithdrawCashStatusStrSuccessFailCheckedSuccessList() {
  348. $status = [];
  349. $status[] = "审核通过";
  350. $status[] = "审核不通过";
  351. $status[] = "待打款";
  352. $status[] = "自动待打款";
  353. $status[] = "人工待打款";
  354. $status[] = "打款中";
  355. $status[] = "自动打款中";
  356. $status[] = "人工打款中";
  357. $status[] = "打款成功";
  358. $status[] = "自动打款成功";
  359. $status[] = "人工打款成功";
  360. $status[] = "打款失败";
  361. $status[] = "自动打款失败";
  362. $status[] = "人工打款失败";
  363. return $status;
  364. }
  365. public static function getWithdrawCashStatusList($type) {
  366. if($type == 10) {
  367. //channel
  368. $params = [
  369. ['code' => 0 ,'name' => "待审核", 'show' => 1],
  370. // ['code' => 1 ,'name' => "审核中", 'show' => 0],
  371. // ['code' => 2 ,'name' => "审核通过", 'show' => 0],
  372. ['code' => 9 ,'name' => "审核不通过", 'show' => 1],
  373. ['code' => 10,'name' => "待打款", 'show' => 1],
  374. // ['code' => 11,'name' => "自动待打款", 'show' => 0],
  375. // ['code' => 13,'name' => "人工待打款", 'show' => 0],
  376. ['code' => 20,'name' => "打款中", 'show' => 1],
  377. // ['code' => 21,'name' => "自动打款中", 'show' => 0],
  378. // ['code' => 22,'name' => "人工打款中", 'show' => 0],
  379. ['code' => 30,'name' => "打款成功", 'show' => 1],
  380. // ['code' => 31,'name' => "自动打款成功", 'show' => 0],
  381. // ['code' => 32,'name' => "人工打款成功", 'show' => 0],
  382. ['code' => 40,'name' => "打款失败", 'show' => 1],
  383. // ['code' => 41,'name' => "自动打款失败", 'show' => 0],
  384. // ['code' => 42,'name' => "人工打款失败", 'show' => 0],
  385. // ['code' => -1,'name' => "其他错误", 'show' => 0],
  386. ];
  387. return $params;
  388. }
  389. if($type == 20) {
  390. //manage
  391. $params = [
  392. ['code' => 0 ,'name' => "待审核", 'show' => 1],
  393. ['code' => 1 ,'name' => "审核中", 'show' => 0],
  394. ['code' => 2 ,'name' => "审核通过", 'show' => 0],
  395. ['code' => 9 ,'name' => "审核不通过", 'show' => 1],
  396. ['code' => 10,'name' => "待打款", 'show' => 1],
  397. ['code' => 11,'name' => "自动待打款", 'show' => 0],
  398. ['code' => 13,'name' => "人工待打款", 'show' => 0],
  399. ['code' => 20,'name' => "打款中", 'show' => 1],
  400. ['code' => 21,'name' => "自动打款中", 'show' => 0],
  401. ['code' => 22,'name' => "人工打款中", 'show' => 0],
  402. ['code' => 30,'name' => "打款成功", 'show' => 1],
  403. ['code' => 31,'name' => "自动打款成功", 'show' => 0],
  404. ['code' => 32,'name' => "人工打款成功", 'show' => 0],
  405. ['code' => 40,'name' => "打款失败", 'show' => 1],
  406. ['code' => 41,'name' => "自动打款失败", 'show' => 0],
  407. ['code' => 42,'name' => "人工打款失败", 'show' => 0],
  408. ['code' => -1,'name' => "其他错误", 'show' => 0],
  409. ];
  410. return $params;
  411. }
  412. }
  413. public static function getWithdrawCashStatusStr($statusIn = '') {
  414. $status = '';
  415. if(is_numeric($statusIn)) {
  416. if($statusIn == 0) {
  417. $status = "待审核";
  418. } else if($statusIn == 1) {
  419. $status = "审核中";
  420. } else if($statusIn == 2) {
  421. $status = "审核通过";
  422. } else if($statusIn == 9) {
  423. $status = "审核不通过";
  424. } else if($statusIn == 10) {
  425. $status = "待打款";
  426. } else if($statusIn == 11) {
  427. $status = "自动待打款";
  428. } else if($statusIn == 12) {
  429. $status = "人工待打款";
  430. } else if($statusIn == 20) {
  431. $status = "打款中";
  432. } else if($statusIn == 21) {
  433. $status = "自动打款中";
  434. } else if($statusIn == 22) {
  435. $status = "人工打款中";
  436. } else if($statusIn == 30) {
  437. $status = "打款成功";
  438. } else if($statusIn == 31) {
  439. $status = "自动打款成功";
  440. } else if($statusIn == 32) {
  441. $status = "人工打款成功";
  442. } else if($statusIn == 40) {
  443. $status = "打款失败";
  444. } else if($statusIn == 41) {
  445. $status = "自动打款失败";
  446. } else if($statusIn == 42) {
  447. $status = "人工打款失败";
  448. } else if($statusIn == -1) {
  449. $status = "其他错误";
  450. } else {
  451. $status = "";
  452. }
  453. }
  454. return $status;
  455. }
  456. public static function getWithdrawCashStatusStrList($statusIn = '') {
  457. $status = '';
  458. if(is_numeric($statusIn)) {
  459. $status = [];
  460. if($statusIn == 0) {
  461. $status[] = "待审核";
  462. } else if($statusIn == 1) {
  463. $status[] = "审核中";
  464. } else if($statusIn == 2) {
  465. $status[] = "审核通过";
  466. } else if($statusIn == 9) {
  467. $status[] = "审核不通过";
  468. } else if($statusIn == 10) {
  469. $status[] = "待打款";
  470. $status[] = "自动待打款";
  471. $status[] = "人工待打款";
  472. } else if($statusIn == 11) {
  473. $status[] = "自动待打款";
  474. } else if($statusIn == 12) {
  475. $status[] = "人工待打款";
  476. } else if($statusIn == 20) {
  477. $status[] = "打款中";
  478. $status[] = "自动打款中";
  479. $status[] = "人工打款中";
  480. } else if($statusIn == 21) {
  481. $status[] = "自动打款中";
  482. } else if($statusIn == 22) {
  483. $status[] = "人工打款中";
  484. } else if($statusIn == 30) {
  485. $status[] = "打款成功";
  486. $status[] = "自动打款成功";
  487. $status[] = "人工打款成功";
  488. } else if($statusIn == 31) {
  489. $status[] = "自动打款成功";
  490. } else if($statusIn == 32) {
  491. $status[] = "人工打款成功";
  492. } else if($statusIn == 40) {
  493. $status[] = "打款失败";
  494. $status[] = "自动打款失败";
  495. $status[] = "人工打款失败";
  496. } else if($statusIn == 41) {
  497. $status[] = "自动打款失败";
  498. } else if($statusIn == 42) {
  499. $status[] = "人工打款失败";
  500. } else if($statusIn == -1) {
  501. $status[] = "其他错误";
  502. } else {
  503. $status[] = "未知错误码";
  504. }
  505. }
  506. return $status;
  507. }
  508. public static function updateWithdrawCashType($id,$param) {
  509. return WithdrawCash::where('id',$id)->update($param);
  510. }
  511. }