海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Repositories\WithdrawalBank;
  4. use App\Common\StatementType;
  5. use App\Models\Agent;
  6. use App\Models\SystemSetting;
  7. use App\Models\Withdrawal;
  8. use App\Service\WithdrawalService;
  9. use App\Traits\DemandTraits;
  10. use App\Traits\WithdrawalTraits;
  11. use Dcat\Admin\Admin;
  12. use Dcat\Admin\Form;
  13. use Dcat\Admin\Grid;
  14. use Dcat\Admin\Show;
  15. use Dcat\Admin\Http\Controllers\AdminController;
  16. use Illuminate\Database\Eloquent\Model;
  17. use Illuminate\Support\Facades\DB;
  18. class WithdrawalBankController extends AdminController
  19. {
  20. /**
  21. * Make a form builder.
  22. *
  23. * @return Form
  24. */
  25. protected function form()
  26. {
  27. return Form::make(new WithdrawalBank(), function (Form $form) {
  28. $auto = Withdrawal::query()
  29. ->with('pay')
  30. ->where([
  31. 'user_id' => Admin::user()->id,
  32. 'user_type' => DemandTraits::$col[0],
  33. 'status' => WithdrawalTraits::$state[3],
  34. 'pay_type' => WithdrawalTraits::$userType[1],
  35. ])
  36. ->orderByDesc('updated_at')
  37. ->first();
  38. $form->display('id');
  39. $min = SystemSetting::val('withdrawal', WithdrawalTraits::$adminType[3]) ?? 0;
  40. $form->decimal('price','提现金额')->rules('required|numeric|min:'.$min.'|not_in:0',[
  41. '*' => '提现金额为必填字段且必须大于'.$min,
  42. ])
  43. ->default($auto->price ?? 0)
  44. ->help('含手续费' . SystemSetting::val('withdrawal', WithdrawalTraits::$adminType[0]) . ' %,最小提现金额'.$min);
  45. $form->text('name')->required()->maxLength(50)->default($auto->pay->name ?? '');
  46. $form->text('card_number')->required()->maxLength(50)->type('number')->default($auto->pay->card_number ?? '');
  47. $form->text('account_name')->required()->maxLength(50)->default($auto->pay->account_name ?? '');
  48. $form->text('branch')->required()->maxLength(100)->default($auto->pay->branch ?? '');
  49. $form->hidden('withdrawal_id');
  50. $form->saving(function (Form $form) {
  51. DB::beginTransaction();
  52. try {
  53. $user = Agent::query()->where('id', Admin::user()->id)->lockForUpdate()->first();
  54. //手续费
  55. $cutPrice = bcmul($form->price, SystemSetting::val('withdrawal', WithdrawalTraits::$adminType[0]), 6);
  56. $cutPrice = $cutPrice > 0 ? bcdiv($cutPrice, 100, 6) : 0;
  57. //总花费
  58. $total = bcadd($form->price, $cutPrice,6);
  59. if ($total > $user->balance) {
  60. return $form->response()->error('余额不足,本次提现需花费'.bcadd($total,0,2).'(含手续费),当前可用余额为' . $user->balance);
  61. }
  62. //提现扣钱流水
  63. $service = new WithdrawalService();
  64. $service->create(
  65. bcmul($form->price, -1, 6),
  66. StatementType::WITHDRAWAL,
  67. Admin::user()->id,
  68. DemandTraits::$col[0]
  69. );
  70. //提现手续费流水
  71. $service->create(
  72. bcmul($cutPrice, -1, 6),
  73. StatementType::WITHDRAWAL_CAT,
  74. Admin::user()->id,
  75. DemandTraits::$col[0]
  76. );
  77. $user->balance = bcsub($user->balance, $total, 6);
  78. $user->save();
  79. $withdrawal = new Withdrawal();
  80. $withdrawal->user_id = Admin::user()->id;
  81. $withdrawal->user_type = DemandTraits::$col[0];
  82. $withdrawal->price = request('price', 0);
  83. $withdrawal->cut_price = $cutPrice;
  84. //$withdrawal->pay_id = $form->getKey();
  85. $withdrawal->pay_type = WithdrawalTraits::$userType[1];
  86. $withdrawal->save();
  87. $form->withdrawal_id = $withdrawal->id;
  88. $form->deleteInput('price');
  89. DB::commit();
  90. } catch (\Exception $e) {
  91. DB::rollBack();
  92. return $form->response()->error($e->getMessage());
  93. }
  94. });
  95. $form->saved(function (Form $form) {
  96. $withdrawal = Withdrawal::find($form->withdrawal_id);
  97. $withdrawal->pay_id = $form->getKey();
  98. $withdrawal->save();
  99. });
  100. });
  101. }
  102. }