public function asignarRol()
 {
     $colonia = Session::get("colonia");
     $urbanism = Urbanism::where('colony_id', '=', $colonia)->first();
     $datos = (string) Input::get("rol");
     $array = explode(",", $datos);
     $num = count($array);
     for ($i = 0; $i < $num; $i++) {
         $roles = explode(".", $array[$i]);
         $id = (int) $roles[1];
         $rol = (int) $roles[0];
         $urb = (int) $roles[2];
         //$permisos = Permisos::all();
         //$permisos = Permisos::find($id);
         Permisos::where("user_id", "=", $id)->where("colony_id", "=", Session::get("colonia"))->update(array("role_id" => $rol));
         $BuscarCollector = Collector::where("user_id", "=", $id)->where('urbanism_id', '=', $urbanism->id)->orderBy('updated_at', 'desc')->first();
         if ($rol == 2 || $rol == 3 || $rol == 4 || $rol == 5) {
             if (!$BuscarCollector) {
                 $Collector = new Collector();
                 $Collector->user_id = $id;
                 $Collector->urbanism_id = $urb;
                 $Collector->save();
             }
         }
         if ($rol == 6) {
             if ($BuscarCollector) {
                 $cobrador = Collector::findOrFail($BuscarCollector->id);
                 $cobrador->delete(['id']);
             }
         }
         //$permisos->role_id = $rol;
         //$permisos->save();
     }
     return Response::json(array("estado" => 1));
 }
 public function config_store()
 {
     $user_id = Auth::user()->id;
     $num_street_id = NULL;
     $num_floor_id = NULL;
     $data = Input::all();
     $name_floor = Input::get('name_floor');
     $name_street = Input::get('name_street');
     //~====================SAVE STEP 1===============================
     $colony = new Colony();
     $colony->location_id = Input::get('city');
     $colony->name = Input::get('colony_name');
     if ($colony->save()) {
         $created_colony = date('Y-m-j', strtotime($colony->created_at));
         $newExpiration = strtotime('+1 month', strtotime($created_colony));
         $newExpiration = date('Y-m-j', $newExpiration);
         $expiration = new Expiration();
         $expiration->colony_id = $colony->id;
         $expiration->expiration = $newExpiration;
         $expiration->status = 1;
         $expiration->save();
         $expiration_lic = new LicenseExpiration();
         $expiration_lic->colony_id = $colony->id;
         $expiration_lic->expiration = null;
         $expiration_lic->save();
         $datetime2 = new DateTime($expiration->expiration);
         $datetime1 = new DateTime(date('Y-m-d'));
         $interval = $datetime1->diff($datetime2);
         $days_expiration = $interval->format('%a');
         Session::put('days_expiration', $days_expiration);
         $role = AssigmentRole::where('user_id', '=', $user_id)->where('role_id', '=', 2)->where('colony_id', '=', 0)->first();
         if ($role) {
             $rol = AssigmentRole::findOrFail($role->id);
             $rol->colony_id = $colony->id;
             $rol->update(['id']);
         } else {
             $rol = new AssigmentRole();
             $rol->user_id = $user_id;
             $rol->role_id = 2;
             $rol->colony_id = $colony->id;
             $rol->save();
         }
     }
     $last_colony_add = $colony->id;
     $urbanism = new Urbanism();
     $urbanism->colony_id = $last_colony_add;
     $urbanism->urbanism_type_id = Input::get('urbanism_type');
     //$urbanism->name				= Input::get('urbanism_name');
     $urbanism->save();
     $last_urbanism_add = $urbanism->id;
     $user_id = Auth::user()->id;
     $neighbor = Neighbors::where('user_id', '=', $user_id)->pluck('id');
     if (!$neighbor) {
         $admin_colonies = new Neighbors();
         $admin_colonies->user_id = $user_id;
         $admin_colonies->name = Input::get('admin_name');
         $admin_colonies->last_name = Input::get('admin_lname');
         $admin_colonies->phone = Input::get('admin_phone');
         $admin_colonies->save();
         $last_adminColonies_add = $admin_colonies->id;
     }
     if ($neighbor) {
         $last_adminColonies_add = $neighbor;
     }
     $streets = $data['streets'];
     if ($urbanism->urbanism_type_id == 3) {
         foreach ($streets as $Building_name) {
             $Building = new BuildingCatalog();
             $Building->urbanism_id = $last_urbanism_add;
             $Building->description = $Building_name;
             $Building->save();
             if ($Building_name == $name_floor) {
                 $num_floor_id = $Building->id;
             }
         }
     } else {
         foreach ($streets as $street_name) {
             $street = new StreetCatalog();
             $street->urbanism_id = $last_urbanism_add;
             $street->name = $street_name;
             $street->save();
             if ($street_name == $name_street) {
                 $num_street_id = $street->id;
             }
         }
     }
     $neighbor_properties = new NeighborProperty();
     $neighbor_properties->neighbors_id = $last_adminColonies_add;
     $neighbor_properties->urbanism_id = $last_urbanism_add;
     $neighbor_properties->num_street_id = $num_street_id;
     $neighbor_properties->num_floor_id = $num_floor_id;
     $neighbor_properties->num_house_or_apartment = Input::get('num_house_or_apartment');
     $neighbor_properties->status = 1;
     $neighbor_properties->save();
     //~====================SAVE STEP 2===============================
     $monthly_fee = new MonthlyFee();
     $monthly_fee->urbanism_id = $last_urbanism_add;
     $monthly_fee->amount = Input::get('monthly_fee');
     $monthly_fee->since = date("Y-m") . "-01";
     $monthly_fee->until = NULL;
     $monthly_fee->save();
     //~====================SAVE STEP 3===============================
     //~====================sub cuentas egreso e ingreso por defecto ===============================
     $subAccounts_ingreso = new SubAccount();
     $subAccounts_ingreso->account_id = 1;
     $subAccounts_ingreso->urbanism_id = $urbanism->id;
     $subAccounts_ingreso->description = 'Pago de cuota mensual';
     $subAccounts_ingreso->save();
     $subAccount_egreso = new SubAccount();
     $subAccount_egreso->account_id = 2;
     $subAccount_egreso->urbanism_id = $urbanism->id;
     $subAccount_egreso->description = 'NĂ³mina de guardias';
     $subAccount_egreso->save();
     $cobrador = Collector::where('user_id', '=', $user_id)->where('urbanism_id', '=', $urbanism->id)->pluck('id');
     if (!$cobrador) {
         $collector = new Collector();
         $collector->user_id = $user_id;
         $collector->urbanism_id = $urbanism->id;
         $collector->save();
     }
     $campoEmail = Input::get('email');
     if ($campoEmail) {
         $invited_neighbors = $data['mails'];
         $invited_id = md5(uniqid(mt_rand(), true));
         foreach ($invited_neighbors as $neighbor_email) {
             $encrypted = Crypt::encrypt(md5(uniqid(mt_rand(), true)));
             $invited = new InvitedNeighbors();
             $invited->neighbor_id = $last_adminColonies_add;
             $invited->invited_id = $invited_id;
             $invited->urbanism_id = $last_urbanism_add;
             $invited->email = $neighbor_email;
             $invited->confirmed = false;
             $invited->confirmation_code = $encrypted;
             $invited->save();
         }
         return Redirect::action('HomeController@sendMailInvitation', array('invited_id' => $invited_id, 'admin_colonia' => $last_adminColonies_add, 'urbanismo' => $last_urbanism_add));
     } else {
         return Redirect::action('NeighborController@admin_neighbor', array('admin_colonia' => $last_adminColonies_add, 'urbanismo' => $last_urbanism_add));
     }
 }
 public function record_index()
 {
     $user_id = Auth::user()->id;
     $colonia = Session::get("colonia");
     $AssigmentRole = Auth::user()->AssigmentRole[0]->role_id;
     $urbanism = Urbanism::where('colony_id', '=', $colonia)->first();
     $urb_name = $urbanism->Colony->name;
     $collector = Collector::where('user_id', '=', $user_id)->where('urbanism_id', '=', $urbanism->id)->first();
     $collector_neighbor = Neighbors::where('user_id', '=', $user_id)->first();
     $collector_properties = NeighborProperty::where('neighbors_id', '=', $collector_neighbor->id)->pluck('id');
     $neighbors_payments = Payment::join('neighbors_properties', 'payments.neighbor_property_id', '=', 'neighbors_properties.id')->join('neighbors', 'neighbors_properties.neighbors_id', '=', 'neighbors.id')->join('sub_accounts', 'payments.sub_account_id', '=', 'sub_accounts.id')->select('payments.id', 'payments.created_at', 'payments.amount', 'payments.coments', 'neighbors.name', 'neighbors.last_name', 'sub_accounts.description')->where('neighbors_properties.urbanism_id', '=', $urbanism->id)->orderBy('created_at')->get();
     $breadcrumbs = Neighbors::with('NeighborProperty')->where('user_id', '=', $user_id)->first();
     $breadcrumbs_data = $breadcrumbs->name . " " . $breadcrumbs->last_name . " [" . $urb_name . "]";
     return View::make('dashboard.incomes.record.index', ['incomes' => $neighbors_payments, 'breadcrumbs_data' => $breadcrumbs_data, 'AssigmentRole' => $AssigmentRole, 'total' => 0]);
 }
 public function asignarRol()
 {
     $datos = (string) Input::get("rol");
     $array = explode(",", $datos);
     $num = count($array);
     for ($i = 0; $i < $num; $i++) {
         $roles = explode(".", $array[$i]);
         $id = (int) $roles[1];
         $rol = (int) $roles[0];
         $urb = (int) $roles[2];
         //$permisos = Permisos::all();
         //$permisos = Permisos::find($id);
         Permisos::where("user_id", "=", $id)->update(array("role_id" => $rol));
         if ($rol == 4) {
             $BuscarCollector = Collector::where("user_id", "=", $id)->orderBy('updated_at', 'desc')->first();
             if (!$BuscarCollector) {
                 $Collector = new Collector();
                 $Collector->user_id = $id;
                 $Collector->urbanism_id = $urb;
                 $Collector->save();
             }
         }
         //$permisos->role_id = $rol;
         //$permisos->save();
     }
     return Response::json(array("estado" => 1));
 }