-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRestaurantsPayoutController.php
234 lines (199 loc) · 8.25 KB
/
RestaurantsPayoutController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
namespace App\Http\Controllers;
use App\Criteria\Earnings\EarningOfRestaurantCriteria;
use App\Criteria\Restaurants\RestaurantsOfManagerCriteria;
use App\DataTables\RestaurantsPayoutDataTable;
use App\Http\Requests\CreateRestaurantsPayoutRequest;
use App\Http\Requests\UpdateRestaurantsPayoutRequest;
use App\Repositories\CustomFieldRepository;
use App\Repositories\EarningRepository;
use App\Repositories\RestaurantRepository;
use App\Repositories\RestaurantsPayoutRepository;
use Carbon\Carbon;
use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Response;
use Prettus\Validator\Exceptions\ValidatorException;
class RestaurantsPayoutController extends Controller
{
/** @var RestaurantsPayoutRepository */
private $restaurantsPayoutRepository;
/**
* @var CustomFieldRepository
*/
private $customFieldRepository;
/**
* @var RestaurantRepository
*/
private $restaurantRepository;
/**
* @var EarningRepository
*/
private $earningRepository;
public function __construct(RestaurantsPayoutRepository $restaurantsPayoutRepo, CustomFieldRepository $customFieldRepo, RestaurantRepository $restaurantRepo, EarningRepository $earningRepository)
{
parent::__construct();
$this->restaurantsPayoutRepository = $restaurantsPayoutRepo;
$this->customFieldRepository = $customFieldRepo;
$this->restaurantRepository = $restaurantRepo;
$this->earningRepository = $earningRepository;
}
/**
* Display a listing of the RestaurantsPayout.
*
* @param RestaurantsPayoutDataTable $restaurantsPayoutDataTable
* @return Response
*/
public function index(RestaurantsPayoutDataTable $restaurantsPayoutDataTable)
{
return $restaurantsPayoutDataTable->render('restaurants_payouts.index');
}
/**
* Show the form for creating a new RestaurantsPayout.
*
* @return Response
*/
public function create()
{
if(auth()->user()->hasRole('manager')){
$this->restaurantRepository->pushCriteria(new RestaurantsOfManagerCriteria(auth()->id()));
}
$restaurant = $this->restaurantRepository->pluck('name', 'id');
$hasCustomField = in_array($this->restaurantsPayoutRepository->model(), setting('custom_field_models', []));
if ($hasCustomField) {
$customFields = $this->customFieldRepository->findByField('custom_field_model', $this->restaurantsPayoutRepository->model());
$html = generateCustomField($customFields);
}
return view('restaurants_payouts.create')->with("customFields", isset($html) ? $html : false)->with("restaurant", $restaurant);
}
/**
* Store a newly created RestaurantsPayout in storage.
*
* @param CreateRestaurantsPayoutRequest $request
*
* @return Response
*/
public function store(CreateRestaurantsPayoutRequest $request)
{
$input = $request->all();
$earning = $this->earningRepository->findByField('restaurant_id',$input['restaurant_id'])->first();
if($input['amount'] > $earning->restaurant_earning){
Flash::error('The payout amount must be less than restaurant earning');
return redirect(route('restaurantsPayouts.create'))->withInput($input);
}
$input['paid_date'] = Carbon::now();
$customFields = $this->customFieldRepository->findByField('custom_field_model', $this->restaurantsPayoutRepository->model());
try {
$this->earningRepository->update(['restaurant_earning'=>$earning->restaurant_earning - $input['amount']], $earning->id);
$restaurantsPayout = $this->restaurantsPayoutRepository->create($input);
$restaurantsPayout->customFieldsValues()->createMany(getCustomFieldsValues($customFields, $request));
} catch (ValidatorException $e) {
Flash::error($e->getMessage());
}
Flash::success(__('lang.saved_successfully', ['operator' => __('lang.restaurants_payout')]));
return redirect(route('restaurantsPayouts.index'));
}
/**
* Display the specified RestaurantsPayout.
*
* @param int $id
*
* @return Response
*/
public function show($id)
{
$restaurantsPayout = $this->restaurantsPayoutRepository->findWithoutFail($id);
if (empty($restaurantsPayout)) {
Flash::error('Restaurants Payout not found');
return redirect(route('restaurantsPayouts.index'));
}
return view('restaurants_payouts.show')->with('restaurantsPayout', $restaurantsPayout);
}
/**
* Show the form for editing the specified RestaurantsPayout.
*
* @param int $id
*
* @return Response
*/
public function edit($id)
{
$restaurantsPayout = $this->restaurantsPayoutRepository->findWithoutFail($id);
$restaurant = $this->restaurantRepository->pluck('name', 'id');
if (empty($restaurantsPayout)) {
Flash::error(__('lang.not_found', ['operator' => __('lang.restaurants_payout')]));
return redirect(route('restaurantsPayouts.index'));
}
$customFieldsValues = $restaurantsPayout->customFieldsValues()->with('customField')->get();
$customFields = $this->customFieldRepository->findByField('custom_field_model', $this->restaurantsPayoutRepository->model());
$hasCustomField = in_array($this->restaurantsPayoutRepository->model(), setting('custom_field_models', []));
if ($hasCustomField) {
$html = generateCustomField($customFields, $customFieldsValues);
}
return view('restaurants_payouts.edit')->with('restaurantsPayout', $restaurantsPayout)->with("customFields", isset($html) ? $html : false)->with("restaurant", $restaurant);
}
/**
* Update the specified RestaurantsPayout in storage.
*
* @param int $id
* @param UpdateRestaurantsPayoutRequest $request
*
* @return Response
*/
public function update($id, UpdateRestaurantsPayoutRequest $request)
{
$restaurantsPayout = $this->restaurantsPayoutRepository->findWithoutFail($id);
if (empty($restaurantsPayout)) {
Flash::error('Restaurants Payout not found');
return redirect(route('restaurantsPayouts.index'));
}
$input = $request->all();
$customFields = $this->customFieldRepository->findByField('custom_field_model', $this->restaurantsPayoutRepository->model());
try {
$restaurantsPayout = $this->restaurantsPayoutRepository->update($input, $id);
foreach (getCustomFieldsValues($customFields, $request) as $value) {
$restaurantsPayout->customFieldsValues()
->updateOrCreate(['custom_field_id' => $value['custom_field_id']], $value);
}
} catch (ValidatorException $e) {
Flash::error($e->getMessage());
}
Flash::success(__('lang.updated_successfully', ['operator' => __('lang.restaurants_payout')]));
return redirect(route('restaurantsPayouts.index'));
}
/**
* Remove the specified RestaurantsPayout from storage.
*
* @param int $id
*
* @return Response
*/
public function destroy($id)
{
$restaurantsPayout = $this->restaurantsPayoutRepository->findWithoutFail($id);
if (empty($restaurantsPayout)) {
Flash::error('Restaurants Payout not found');
return redirect(route('restaurantsPayouts.index'));
}
$this->restaurantsPayoutRepository->delete($id);
Flash::success(__('lang.deleted_successfully', ['operator' => __('lang.restaurants_payout')]));
return redirect(route('restaurantsPayouts.index'));
}
/**
* Remove Media of RestaurantsPayout
* @param Request $request
*/
public function removeMedia(Request $request)
{
$input = $request->all();
$restaurantsPayout = $this->restaurantsPayoutRepository->findWithoutFail($input['id']);
try {
if ($restaurantsPayout->hasMedia($input['collection'])) {
$restaurantsPayout->getFirstMedia($input['collection'])->delete();
}
} catch (\Exception $e) {
Log::error($e->getMessage());
}
}
}