source: app/Http/Controllers/OfferController.php@ dfae77e

Last change on this file since dfae77e was dfae77e, checked in by Igor Danilovski <igor_danilovski@…>, 2 years ago
  • Initial commit;
  • Property mode set to 100644
File size: 9.8 KB
Line 
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Enum\OfferStatus;
6use App\Enum\UserType;
7use App\Http\Controllers\Utils\UtilController;
8use App\Models\Artist;
9use App\Models\Event;
10use App\Models\Offer;
11use App\Models\OfferComment;
12use App\Models\Organizer;
13use App\Models\User;
14use Carbon\Carbon;
15use Illuminate\Http\RedirectResponse;
16use Illuminate\Http\Request;
17use Illuminate\Support\Facades\Auth;
18use Illuminate\Support\Str;
19use Exception;
20
21class OfferController extends Controller
22{
23 //
24 public function __construct()
25 {
26 $this->middleware(['auth', 'verified', 'onboarding']);
27 $this->middleware(['role:artist,organizer,manager'])->only(['show', 'writeComment', 'declineOffer']);
28 $this->middleware(['role:artist,manager'])->only(['setPrice']);
29 $this->middleware(['role:artist,organizer'])->only('index');
30 $this->middleware(['role:organizer'])->only(['create', 'store']);
31 }
32
33 public function index()
34 {
35 $offers = Offer::select('offers.*')
36 ->join('events', 'events.id', '=', 'offers.event_id')
37 ->when(Auth::user()->type === UserType::ORGANIZER->value, function ($query) {
38 return $query->where('events.organizer_id', '=', Auth::id());
39 })
40 ->when(Auth::user()->type === UserType::ARTIST->value, function ($query) {
41 return $query->where('offers.artist_id', '=', Auth::id());
42 })
43 ->orderBy('offers.status', 'asc')
44 ->orderBy('events.event_date', 'desc')
45 ->paginate(15);
46
47 return view('web.offer.offers')
48 ->with('offers', $offers);
49 }
50
51 /**
52 * Show the form for creating a new resource.
53 *
54 * @return Response|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
55 */
56 public function create($username)
57 {
58 $organizer = Organizer::findOrFail(Auth::id());
59 $user_artist = User::where('username', $username)->firstOrFail();
60 $artist = Artist::where('user_id', $user_artist->id)->firstOrFail();
61
62 return view('web.offer.create')
63 ->with('events', $organizer->events)
64 ->with('artist', $artist);
65 }
66
67 /**
68 * Store a newly created resource in storage.
69 *
70 * @return Response|\Illuminate\Contracts\Foundation\Application|RedirectResponse|\Illuminate\Routing\Redirector
71 */
72 public function store(Request $request)
73 {
74 $artist = Artist::where('user_id', $request->artist_id)->firstOrFail();
75 $event = Event::where('id', $request->event_id)->firstOrFail();
76
77 $checkIfOffersExists = Offer::where(function ($query) use ($event, $artist) {
78 $query->where('event_id', $event->id);
79 $query->where('artist_id', $artist->user_id);
80 $query->where('status', OfferStatus::DECLINED->value);
81 $query->where('created_at', '>', Carbon::now()->subDays(7)->endOfDay());
82 })->orWhere(function ($query) use ($event, $artist) {
83 $query->where('event_id', $event->id);
84 $query->where('artist_id', $artist->user_id);
85 $query->where('status', '!=', OfferStatus::DECLINED->value);
86 })->exists();
87
88 if ($checkIfOffersExists) {
89 return redirect()->back()
90 ->with('warning', 'You cannot send request to the artist for the selected event');
91 }
92
93 $offer = new Offer();
94 $offer->slug = UtilController::generateSlugFromString('offer' . '-' . $event->title . Str::random(10));
95 $offer->status = OfferStatus::IN_PROGRESS->value;
96 $offer->payment_type = 1;
97
98 $artist_id = Artist::find($request->artist_id);
99 $offer->artist()->associate($artist_id);
100
101 $event_id = Event::find($request->event_id);
102 $offer->event()->associate($event_id);
103
104 try {
105 $offer->save();
106 } catch (Exception $e) {
107 return redirect()->back()
108 ->with('error', $e->getMessage());
109 }
110
111 return redirect('/offers/' . $offer->slug)
112 ->with('success', 'Request created and sent!');
113 }
114
115 /**
116 * Display the specified resource.
117 *
118 * @param $slug
119 * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\never
120 */
121 public function show($slug)
122 {
123 $offer = Offer::where('slug', $slug)->firstOrFail();
124 $logged_user = User::where('id', Auth::id())->firstOrFail();
125
126 if (
127 ($logged_user->type === UserType::ARTIST->value && $offer->artist->user_id == $logged_user->id) ||
128 ($logged_user->type === UserType::MANAGER->value && $offer->artist->manager->user_id == $logged_user->id) ||
129 ($logged_user->type === UserType::ORGANIZER->value && $logged_user->id == $offer->event->organizer->user_id)
130 ) {
131 if ($logged_user->type === UserType::ORGANIZER->value) {
132 if ($offer->status == OfferStatus::WAITING_FOR_PAYMENT->value) {
133 $intent = Auth::user()->role->createSetupIntent();
134 }
135 }
136 return view('web.offer.offer')
137 ->with('offer', $offer)
138 ->with('intent', $intent ?? null);
139 }
140
141 return abort(404);
142 }
143
144 public function writeComment(Request $request)
145 {
146 $offer = Offer::where('id', $request->offer_id)->firstOrFail();
147 $logged_user = User::where('id', Auth::id())->firstOrFail();
148
149 //ORGANIZER, ARTIST, MANAGER
150 /*
151 * ALLOWED TP REPLY IF:
152 * OFFER IS CREATED BY THE SIGNED ORGANIZER
153 * OFFER IS SENT TO SIGNED ARTIST
154 * OFFER IS SENT TO ARTIST WHO IS MANAGED BY SIGNED MANAGER
155 */
156 if (
157 ($logged_user->type === UserType::ORGANIZER->value && $logged_user->id == $offer->event->organizer->user_id) ||
158 ($logged_user->type === UserType::ARTIST->value && $offer->artist->user_id == $logged_user->id) ||
159 ($logged_user->type === UserType::MANAGER->value && $offer->artist->manager->user_id == $logged_user->id)
160 ) {
161 $offerComment = new OfferComment();
162 $offerComment->offer()->associate($offer);
163
164 $offerComment->author()->associate($logged_user);
165
166 $offerComment->content = $request->comment;
167 $offerComment->updated_at = null;
168
169 try {
170 $offerComment->save();
171 } catch (\Exception $e) {
172 dd($e);
173 }
174
175 return redirect()->back();
176 }
177 return abort(404);
178 }
179
180 public function setPrice(Request $request): RedirectResponse
181 {
182 $offer = Offer::where('id', $request->offer_id)->firstOrFail();
183 $logged_user = User::where('id', Auth::id())->firstOrFail();
184
185 if (
186 ($offer->status === OfferStatus::IN_PROGRESS->value) &&
187 (($logged_user->type === UserType::ARTIST->value && $offer->artist->user_id == $logged_user->id) ||
188 ($logged_user->type === UserType::MANAGER->value && $offer->artist->manager->user_id == $logged_user->id))
189 ) {
190 $offer->price = $request->price;
191
192 try {
193 $offer->update();
194 } catch (Exception $e) {
195 return redirect()->back()
196 ->with('error', $e->getMessage());
197 }
198
199 return redirect()->back();
200 } else {
201 return redirect()->back()
202 ->with('warning', 'You are not authorized for that action!');
203 }
204 }
205
206 public function declineOffer(Request $request): RedirectResponse
207 {
208 $offer = Offer::where('id', $request->offer_id)->firstOrFail();
209 $logged_user = User::where('id', Auth::id())->firstOrFail();
210
211 if (
212 ($offer->status === OfferStatus::IN_PROGRESS->value) &&
213 (($logged_user->type === UserType::ORGANIZER->value && $logged_user->id == $offer->event->organizer->user_id) ||
214 ($logged_user->type === UserType::ARTIST->value && $offer->artist->user_id == $logged_user->id) ||
215 ($logged_user->type === UserType::MANAGER->value && $offer->artist->manager->user_id == $logged_user->id))
216 ) {
217 $offer->status = OfferStatus::DECLINED->value;
218 $offer->completed_at = Carbon::now()->toDateTimeString();
219
220 try {
221 $offer->update();
222 } catch (Exception $e) {
223 return redirect()->back()
224 ->with('error', $e->getMessage());
225 }
226
227 return redirect()->back();
228 } else {
229 return redirect()->back()
230 ->with('warning', 'You are not authorized for that action!');
231 }
232 }
233
234 public function acceptOffer(Request $request): RedirectResponse
235 {
236 $offer = Offer::where('id', $request->offer_id)->firstOrFail();
237 $logged_user = User::where('id', Auth::id())->firstOrFail();
238
239 if (
240 ($offer->status === OfferStatus::IN_PROGRESS->value) &&
241 (($logged_user->type === UserType::ORGANIZER->value && $logged_user->id == $offer->event->organizer->user_id) ||
242 ($logged_user->type === UserType::ARTIST->value && $offer->artist->user_id == $logged_user->id) ||
243 ($logged_user->type === UserType::MANAGER->value && $offer->artist->manager->user_id == $logged_user->id))
244 ) {
245 $offer->status = OfferStatus::WAITING_FOR_PAYMENT->value;
246 $offer->completed_at = Carbon::now()->toDateTimeString();
247
248 try {
249 $offer->update();
250 } catch (Exception $e) {
251 return redirect()->back()
252 ->with('error', $e->getMessage());
253 }
254
255 return redirect()->back();
256 } else {
257 return redirect()->back()
258 ->with('warning', 'You are not authorized for that action!');
259 }
260 }
261}
Note: See TracBrowser for help on using the repository browser.