1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use App\Lot; |
6
|
|
|
use App\Repositories\LotRepository; |
7
|
|
|
use Closure; |
8
|
|
|
use Illuminate\Contracts\Auth\Guard; |
9
|
|
|
|
10
|
|
|
class AddLotMiddleware |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Guard |
14
|
|
|
*/ |
15
|
|
|
protected $auth; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var LotRepository |
19
|
|
|
*/ |
20
|
|
|
protected $lots; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* AddLotMiddleware constructor. |
24
|
|
|
* @param Guard $auth |
25
|
|
|
* @param LotRepository $lotRepository |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Guard $auth, LotRepository $lotRepository) |
28
|
|
|
{ |
29
|
|
|
$this->auth = $auth; |
30
|
|
|
$this->lots = $lotRepository; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Handle an incoming request. |
35
|
|
|
* |
36
|
|
|
* @param \Illuminate\Http\Request $request |
37
|
|
|
* @param \Closure $next |
38
|
|
|
* @param string|null $guard |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
public function handle($request, Closure $next, $guard = null) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
if($lot = $request->route('lot')) |
44
|
|
|
{ |
45
|
|
|
if($lot->status == Lot::STATUS_DRAFTED) |
46
|
|
|
{ |
47
|
|
|
/*$category = $lot->category;*/ |
|
|
|
|
48
|
|
|
if($this->issetAmount($lot, $request->get('comision'))) |
49
|
|
|
{ |
50
|
|
|
$lot->status = Lot::STATUS_COMPLETE; |
51
|
|
|
$lot->save(); |
52
|
|
|
return $next($request); |
53
|
|
|
} else { |
54
|
|
|
//return redirect()->back()->withErrors('No amount'); |
|
|
|
|
55
|
|
|
return response('No amount',402); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return redirect()->back()->withSuccess('Something Wrong!'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function issetAmount($lot, $requestComision) |
64
|
|
|
{ |
65
|
|
|
$comision = $this->lots->userLotsPendingComision(\Auth::user(),$lot->id) + $requestComision; |
66
|
|
|
$wallet = $this->auth->user()->wallet; |
|
|
|
|
67
|
|
|
if ($wallet->amount >= $comision) { |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function blockAmount($lot, $tax) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$user = $this->auth->user(); |
|
|
|
|
76
|
|
|
$amount = $lot->yield_amount; |
77
|
|
|
return $this->blockSumm($tax, $amount); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function blockSumm($tax, $amount) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$wallet = $this->auth->user()->wallet; |
|
|
|
|
83
|
|
|
|
84
|
|
|
$summ = $this->calcEraseSumm($tax, $amount); |
85
|
|
|
|
86
|
|
|
$calc = $wallet->amount - $summ; |
87
|
|
|
if($calc > 0) |
88
|
|
|
{ |
89
|
|
|
return $wallet->fill([ |
90
|
|
|
//'amount' => $calc, |
|
|
|
|
91
|
|
|
'amount_block' => $summ |
92
|
|
|
])->save(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $lot |
101
|
|
|
* @param $tax |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
private function eraseFromWallet($lot, $tax) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$user = $this->auth->user(); |
107
|
|
|
$amount = $lot->yield_amount; |
108
|
|
|
|
109
|
|
|
if(! $user->isAdmin()) |
110
|
|
|
{ |
111
|
|
|
return $this->eraseSummFromWallet($tax, $amount); |
112
|
|
|
} else { |
113
|
|
|
$lot->status = Lot::STATUS_COMPLETE; |
114
|
|
|
$lot->save(); |
115
|
|
|
|
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
/** |
120
|
|
|
* @param $tax |
121
|
|
|
* @param $amount |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function eraseSummFromWallet($tax, $amount) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$wallet = $this->auth->user()->wallet; |
|
|
|
|
127
|
|
|
|
128
|
|
|
$summ = $this->calcEraseSumm($tax, $amount); |
129
|
|
|
|
130
|
|
|
$calc = $wallet->amount - $summ; |
131
|
|
|
if($calc > 0) |
132
|
|
|
{ |
133
|
|
|
return $wallet->fill([ |
134
|
|
|
'amount' => $calc |
135
|
|
|
])->save(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $tax |
143
|
|
|
* @param $amount |
144
|
|
|
* @return float|int |
145
|
|
|
*/ |
146
|
|
|
public function calcEraseSumm($tax, $amount) |
147
|
|
|
{ |
148
|
|
|
return ($amount / 100 * $tax); |
149
|
|
|
} |
150
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.