1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\InvolveProductRequest; |
6
|
|
|
use App\Http\Requests\ExitProductRequest; |
7
|
|
|
use App\Repositories\InvolvedRepository; |
8
|
|
|
use App\Repositories\LotRepository; |
9
|
|
|
use App\Repositories\SpecPriceRepository; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
|
12
|
|
|
class UsersController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var InvolvedRepository |
16
|
|
|
*/ |
17
|
|
|
protected $involved; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var LotRepository |
21
|
|
|
*/ |
22
|
|
|
protected $lot; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var SpecPriceRepository |
26
|
|
|
*/ |
27
|
|
|
protected $specs; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* UsersController constructor. |
31
|
|
|
* @param InvolvedRepository $involvedRepository |
32
|
|
|
*/ |
33
|
|
|
public function __construct(InvolvedRepository $involvedRepository, |
34
|
|
|
LotRepository $lotRepository, |
35
|
|
|
SpecPriceRepository $specPriceRepository) |
36
|
|
|
{ |
37
|
|
|
$this->involved = $involvedRepository; |
38
|
|
|
$this->lot = $lotRepository; |
39
|
|
|
$this->specs = $specPriceRepository; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Involve the product method. |
44
|
|
|
* |
45
|
|
|
* @param InvolveProductRequest $request |
46
|
|
|
* @param $product |
47
|
|
|
* @return \Illuminate\Http\RedirectResponse |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
public function involveProductOffer(InvolveProductRequest $request, $product) |
51
|
|
|
{ |
52
|
|
|
|
53
|
|
|
$this->involved->create($request->all(), $product); |
54
|
|
|
|
55
|
|
|
$selledPrice = $this->countInvolvedLot($product); |
56
|
|
|
|
57
|
|
|
if ($selledPrice >= $product->lot->yield_amount) { |
58
|
|
|
|
59
|
|
|
$product->lot->update(['verify_status' => 'expired']); |
60
|
|
|
|
61
|
|
|
return redirect()->back()->with(['status' => 'Oferta este finisata!', 'color' => 'green']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return redirect()->back()->with(['status' => 'Success! You are involve the product offer.', 'color' => 'green']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Exit involved product. |
69
|
|
|
* |
70
|
|
|
* @param ExitProductRequest $request |
71
|
|
|
* @param $involve |
72
|
|
|
* @return \Illuminate\Http\RedirectResponse |
73
|
|
|
*/ |
74
|
|
|
public function exitProductOffer(ExitProductRequest $request, $involve, $product) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$selledPrice = $this->countInvolvedLot($product); |
77
|
|
|
|
78
|
|
|
$involve = $this->involved->update($involve, ['active' => 0]); |
79
|
|
|
|
80
|
|
|
$remaining = config('product.times') - $this->involved->getInvolveTimesProduct($involve->product); |
|
|
|
|
81
|
|
|
|
82
|
|
|
if ($selledPrice < $product->lot->yield_amount) { |
83
|
|
|
$product->lot->update(['verify_status' => 'verified']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
return redirect()->back()->with(['status'=>'Success! You are exit from product offer. Remaining attempts (' . $remaining . ')','color'=>'green']); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $product |
92
|
|
|
* @return number |
93
|
|
|
*/ |
94
|
|
|
public function countInvolvedLot($product) |
95
|
|
|
{ |
96
|
|
|
$count = $product->lot->involved; |
97
|
|
|
|
98
|
|
|
foreach ($count as $item) { |
99
|
|
|
$changes[] = $item->count * $this->specs->getPriceById($item->price_id); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
return array_sum($changes); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.