Withdraw.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/4/7
  6. * Time: 下午3:08
  7. */
  8. namespace app\common\services\finance;
  9. use app\common\events\withdraw\BalanceWithdrawSuccessEvent;
  10. use app\common\events\withdraw\WithdrawPayedEvent;
  11. use app\common\models\Income;
  12. use app\common\models\Withdraw as WithdrawModel;
  13. use app\common\services\income\WithdrawIncomeService;
  14. class Withdraw
  15. {
  16. public static function getWithdrawServicetaxPercent($amount,$withdraw = null)
  17. {
  18. if ($withdraw && in_array($withdraw->type,\app\common\models\Withdraw::$noDeductionServicetax)) {//不扣除劳务税的提现类型
  19. return ['servicetax_amount' => 0, 'servicetax_percent' => 0];
  20. }
  21. $withdraw_set = \Setting::get('withdraw.income');
  22. $percent = 0;
  23. if (bccomp($withdraw_set['servicetax_rate'], 0, 2) == 1) {
  24. $percent = $withdraw_set['servicetax_rate'];
  25. }
  26. if ($servicetax = $withdraw_set['servicetax']) {
  27. $max_money = array_column($servicetax, 'servicetax_money');
  28. array_multisort($max_money, SORT_DESC, $servicetax);
  29. foreach ($servicetax as $value) {
  30. if ($amount >= $value['servicetax_money'] && !empty($value['servicetax_money'])) {
  31. $percent = $value['servicetax_rate'];
  32. break;
  33. }
  34. }
  35. }
  36. $servicetax_amount = bcmul($amount, bcdiv($percent, 100, 4), 2);
  37. if (bccomp($servicetax_amount, 0, 2) != 1) $servicetax_amount = 0;
  38. return ['servicetax_amount' => $servicetax_amount, 'servicetax_percent' => $percent];
  39. }
  40. public static function paySuccess($withdrawSN)
  41. {
  42. $withdrawModel = WithdrawModel::getWithdrawByWithdrawSN($withdrawSN);
  43. if ($withdrawModel && $withdrawModel->type == 'balance') {
  44. if ($withdrawModel->status != 2) {
  45. $withdrawModel->status = 2;
  46. $withdrawModel->arrival_at = time();
  47. $result = $withdrawModel->save();
  48. if ($result) {
  49. event(new BalanceWithdrawSuccessEvent($withdrawModel));
  50. BalanceNoticeService::withdrawSuccessNotice($withdrawModel);
  51. }
  52. }
  53. return true;
  54. }
  55. return static::otherWithdrawSuccess($withdrawSN);
  56. }
  57. public static function otherWithdrawSuccess($withdrawId)
  58. {
  59. $withdraw = WithdrawModel::getWithdrawById($withdrawId)->first();
  60. if ($withdraw->status != '1' && $withdraw->status != 4) {
  61. return false;
  62. }
  63. $withdraw->pay_status = 1;
  64. //提现打款到账事件
  65. event(new WithdrawPayedEvent($withdraw));
  66. //修改收入状态
  67. foreach ($withdraw['type_data']['incomes'] as $item) {
  68. if ($item['pay_status'] == '1') {
  69. Income::updatedIncomePayStatus($item['id'], ['pay_status' => '2']);
  70. }
  71. }
  72. //修改提现记录状态
  73. $updatedData = [
  74. 'status' => 2,
  75. 'arrival_at' => time(),
  76. ];
  77. \Log::debug('修改提现记录状态', [$withdrawId,$updatedData]);
  78. return WithdrawModel::updatedWithdrawStatus($withdrawId, $updatedData);
  79. }
  80. public static function payFail($withdrawSN)
  81. {
  82. $withdrawModel = WithdrawModel::getWithdrawByWithdrawSN($withdrawSN);
  83. if ($withdrawModel && $withdrawModel->type == 'balance') {
  84. if ($withdrawModel->status != 1 && $withdrawModel->status == 4) {
  85. $withdrawModel->status = 1;
  86. $withdrawModel->arrival_at = time();
  87. $withdrawModel->save();
  88. }
  89. }
  90. return static::otherWithdrawFail($withdrawSN);
  91. }
  92. public static function otherWithdrawFail($withdrawId)
  93. {
  94. $withdraw = WithdrawModel::getWithdrawById($withdrawId)->first();
  95. if ($withdraw->status != '1' && $withdraw->status == '4') {
  96. $updatedData = [
  97. 'status' => 1,
  98. 'arrival_at' => time(),
  99. ];
  100. WithdrawIncomeService::delete($withdraw);
  101. \Log::debug('修改提现记录状态', [$withdrawId,$updatedData]);
  102. return WithdrawModel::updatedWithdrawStatus($withdrawId, $updatedData);
  103. }
  104. }
  105. }