海南旅游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.

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