/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(StoreTicketRequest $request) { //var_dump($request->all()); $event = Event::find($request['event_id']); $zone = Zone::find($request['zone_id']); $nTickets = $request['quantity']; if ($event->place->rows != null) { //Es numerado $seats = $request['seats']; $seats = $this->getSelectedSlots($seats, $zone->id); foreach ($seats as $seat_id) { $slot = DB::table('slot_presentation')->where('slot_id', $seat_id)->where('presentation_id', $request['presentation_id'])->first(); if ($slot->status != config('constants.seat_available')) { return back()->withInput($request->except('seats'))->withErrors(['El asiento ' . $seat_id . ' no esta libre']); } } } else { //No es numerado $zoneXpres = DB::table('zone_presentation')->where('zone_id', $request['zone_id'])->where('presentation_id', $request['presentation_id'])->first(); if ($zoneXpres->slots_availables - $nTickets < 0) { return back()->withInput($request->except('seats'))->withErrors(['La zona esta llena']); } } try { DB::beginTransaction(); $tickets = array(); for ($i = 0; $i < $nTickets; $i++) { if ($event->place->rows != null) { //Cambiar estado de asiento $seat = DB::table('slot_presentation')->where('slot_id', $seats[$i])->where('presentation_id', $request['presentation_id'])->sharedLock(); //Revisa de nuevo $seat = DB::table('slot_presentation')->where('slot_id', $seats[$i])->where('presentation_id', $request['presentation_id'])->first(); if ($seat->status != config('constants.seat_available')) { return back()->withInput($request->except('seats'))->withErrors(['El asiento ' . $seat_id . ' no esta libre']); } DB::table('slot_presentation')->where('slot_id', $seats[$i])->where('presentation_id', $request['presentation_id'])->update(['status' => config('constants.seat_taken')]); } else { //Disminuir capacidad en la zona de esa presentacion DB::table('zone_presentation')->where('zone_id', $request['zone_id'])->where('presentation_id', $request['presentation_id'])->sharedLock(); $zoneXpres = DB::table('zone_presentation')->where('zone_id', $request['zone_id'])->where('presentation_id', $request['presentation_id'])->first(); if ($zoneXpres->slots_availables - $nTickets < 0) { return back()->withInput($request->except('seats'))->withErrors(['La zona esta llena']); } DB::table('zone_presentation')->where('zone_id', $request['zone_id'])->where('presentation_id', $request['presentation_id'])->decrement('slots_availables'); } } //Crear ticket $id = DB::table('tickets')->insertGetId(['payment_date' => new Carbon(), 'reserve' => null, 'cancelled' => 0, 'owner_id' => null, 'event_id' => $request['event_id'], 'price' => $zone->price, 'presentation_id' => $request['presentation_id'], 'zone_id' => $request['zone_id'], 'promo_id' => null, 'quantity' => $nTickets, 'salesman_id' => null, 'picked_up' => false, 'discount' => null, 'designee' => null, 'cash_amount' => null, 'credit_amount' => null, 'total_price' => $zone->price * $nTickets, 'created_at' => new Carbon(), 'updated_at' => new Carbon()]); if (\Auth::user()->role_id == config('constants.salesman')) { DB::table('tickets')->where('id', $id)->update(['salesman_id' => \Auth::user()->id]); DB::table('tickets')->where('id', $id)->update(['picked_up' => true]); DB::table('tickets')->where('id', $id)->update(['designee' => null]); } if ($request['designee'] != null) { DB::table('tickets')->where('id', $id)->update(['designee' => $request['designee']]); } if ($request['promotion_id'] != "") { $promo = Promotions::find($request['promotion_id']); if ($promo->desc != null) { DB::table('tickets')->where('id', $id)->update(['discount' => $promo->desc]); DB::table('tickets')->where('id', $id)->decrement('total_price', $promo->desc / 100 * ($nTickets * $zone->price)); } else { $pu = Zone::find($request['zone_id'])->price; $quantity = $request['quantity']; $pt = $pu * $quantity; $discTickets = $quantity / $promo->carry; $discTickets = floor($discTickets); $pd = $pt - $discTickets * $pu * ($promo->carry - $promo->pay); $desc = 100 - $pd / $pt * 100; DB::table('tickets')->where('id', $id)->update(['discount' => $desc]); DB::table('tickets')->where('id', $id)->update(['total_price' => $pd]); } DB::table('tickets')->where('id', $id)->update(['promo_id' => $promo->id]); } //Si existe cliente if ($request['user_id'] != "") { //Asignar cliente DB::table('tickets')->where('id', $id)->update(['owner_id' => $request['user_id']]); //Aumentar puntos de cliente DB::table('users')->where('id', $request['user_id'])->increment('points', $nTickets); } if ($event->place->rows != null) { //Asignar id en caso sea numerado for ($i = 0; $i < $nTickets; $i++) { DB::table('slot_presentation')->where('slot_id', $seats[$i])->where('presentation_id', $request['presentation_id'])->update(['sale_id' => $id]); } } $user = \Auth::user(); //Distincion de tarjeta o efectivo $price = Ticket::find($id)->total_price; if ($request['payMode'] == config('constants.credit')) { DB::table('tickets')->where('id', $id)->update(['credit_amount' => $price]); } else { if ($request['payMode'] == config('constants.cash')) { DB::table('tickets')->where('id', $id)->update(['cash_amount' => $price]); if ($user->role_id == config('constants.salesman')) { DB::table('modules')->where('id', $user->module_id)->increment('actual_cash', $price); } } else { if ($request['payMode'] == config('constants.mix')) { DB::table('tickets')->where('id', $id)->update(['cash_amount' => $request['paymentMix']]); DB::table('tickets')->where('id', $id)->update(['credit_amount' => $price - $request['paymentMix']]); if ($user->role_id == config('constants.salesman')) { DB::table('modules')->where('id', $user->module_id)->increment('actual_cash', $request['paymentMix']); } } } } array_push($tickets, $id); //var_dump('llego'); DB::commit(); } catch (\Exception $e) { //var_dump($e); //dd('rollback'); DB::rollback(); return back()->withInput($request->except('seats'))->withErrors(['Por favor intentelo nuevamente']); } session(['tickets' => $tickets]); if (\Auth::user()->role_id == config('constants.salesman')) { return redirect()->route('ticket.success.salesman'); } else { if (\Auth::user()->role_id == config('constants.client')) { return redirect()->route('ticket.success.client'); } } }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $promotion = Promotions::find($id); if ($promotion != NULL) { $promotion->delete(); } return redirect('promoter/promotion'); }