| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Http\Controllers\Retail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Http\Controllers\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Purchase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Facades\Log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Facades\Auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Support\Facades\DB; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | class SoldTicketsController extends Controller | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public function getPurchases() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         // not the nicest way to do it, but it works | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         // Get a list of not already archived purchases by getting the archived projects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $preFilteredPurchaseIds = DB::table('projects') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             ->join('events', 'projects.id', '=', 'events.project_id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             ->join('tickets', 'events.id', '=', 'tickets.event_id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             ->where('projects.is_archived', 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             ->distinct('tickets.purchase_id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             ->pluck('tickets.purchase_id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         // Use the previously fetched array of not archived purchase ids and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         // get the purchase models with additional filters applied | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $purchases = Purchase::where('vendor_id', auth()->user()->id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             ->whereIn('state', ['paid', 'free', 'reserved']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             ->whereIn('id', $preFilteredPurchaseIds) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             ->orderBy('state_updated', 'DESC') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             ->get(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |         return view('retail.purchases', ['purchases' => $purchases]); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     public function setToPaid(Purchase $purchase) | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         // First check if the soon-to-be-deleted purchase is | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         // owned by the currently logged in user as vendor | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         if (auth()->user()->id != $purchase->vendor_id) { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             return redirect()->route('retail.sold.tickets')->with('status', 'Error: You are not authorized!'); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         Log::info('[Retail user#' . Auth::user()->id . '] Setting state of purchase#' . $purchase->id . ' to "paid"'); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $purchase->state = 'paid'; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $purchase->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         return redirect()->route('ticket.purchase', $purchase)->with('status', 'Purchase successfully deleted!'); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function deletePurchase(Purchase $purchase) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         // First check if the soon-to-be-deleted purchase is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         // owned by the currently logged in user as vendor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         if (auth()->user()->id != $purchase->vendor_id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             return redirect()->route('retail.sold.tickets')->with('status', 'Error: You are not authorized!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         Log::info('[Retail user#' . Auth::user()->id . '] Deleting purchase#' . $purchase->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $purchase->deleteWithAllData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |         return redirect()->route('retail.sold.tickets')->with('status', 'Purchase successfully deleted!'); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  |