/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     //
     $validator = Validator::make($request->all(), ["terminal_id" => "required", "ticket_type" => "required"]);
     if ($validator->fails()) {
         if ($request->ajax()) {
             return response()->json($validator->messages());
         } else {
             return \Redirect::back()->withErrors($validator)->withInput();
         }
     }
     $input = $request->all();
     $stack = new Ticketstack();
     $stack->batch_code = Ticket::stackCode();
     $stack->created_at = date("Y-m-d H:i:s");
     $stack->save();
     $ticket = new Ticket();
     $terminal = Busstop::where("short_name", "=", trim($input['terminal_id']))->first();
     $count = $input['qty'];
     for ($k = 1; $k <= $count; $k++) {
         $time = time();
         DB::table('ticketseed')->insert(['code' => $time]);
         $mid = DB::table("ticketseed")->max("id");
         $amount = $input['ticket_type'] == 1 ? $terminal->one_way_to_fare : $terminal->one_way_from_fare;
         Ticket::insert(array("code" => $ticket->ticketCode($mid), "serial_no" => $ticket->uniqueID($mid), "terminal_id" => $terminal->id, "stack_id" => $stack->id, "route_id" => $terminal->id, "amount" => $amount, "ticket_type" => $input['ticket_type']));
     }
 }
 public function addBusStop(Request $request, $id)
 {
     $input = $request->all();
     $agent = Merchant::find($id);
     if (isset($input['type']) && $input['type'] == 'genticket') {
         $validator = Validator::make($request->all(), ["terminal_id" => "required", "ticket_type" => "required"]);
         if ($validator->fails()) {
             if ($request->ajax()) {
                 return response()->json($validator->messages());
             } else {
                 return \Redirect::back()->withErrors($validator)->withInput();
             }
         }
         //$input = $request->all();
         $stack = new Ticketstack();
         $stack->batch_code = Ticket::stackCode();
         $stack->created_at = date("Y-m-d H:i:s");
         $stack->save();
         $ticket = new Ticket();
         $terminal = Busstop::where("id", "=", trim($input['terminal_id']))->first();
         $count = $input['qty'];
         $account = Account::where("app_id", "=", $input['app_id'])->first();
         for ($k = 1; $k <= $count; $k++) {
             $time = time();
             DB::table('ticketseed')->insert(['code' => $time]);
             $mid = DB::table("ticketseed")->max("id");
             //$amount   =  ($input['ticket_type'] == 1) ? $terminal->one_way_to_fare : $terminal->one_way_from_fare ;
             $amount = 1000;
             Ticket::insert(array("code" => $ticket->ticketCode($mid), "account_id" => $account->id, "app_id" => $input['app_id'], "agent_id" => $id, "serial_no" => $ticket->uniqueID($mid), "terminal_id" => $terminal->id, "stack_id" => $stack->id, "route_id" => $input['route_id'], "amount" => $amount, "ticket_type" => $input['ticket_type']));
         }
         $account->balance += $amount * $input['qty'];
         $agent->balance += $amount * $input['qty'];
         $account->update();
         $agent->update();
     } else {
         $account = new Account();
         $mid = Account::uniqueID() + 1;
         $terminal_id = DB::table("busstops")->where("short_name", "=", $input['short_name'])->pluck("id");
         $account->merchant_id = $input['merchant_id'];
         $account->busstop_id = $terminal_id;
         $m = $account->merchant_id + $mid;
         $realid = uniqid($m);
         $realid = substr($realid, -1, 5);
         $realid = $realid . str_pad($mid, 5, 0, STR_PAD_LEFT);
         $account->account_id = uniqid($m);
         if ($account->save()) {
             return response()->json("record saved successfully");
         }
     }
 }