public function hookInstall()
 {
     //set up my Collectors table
     $db = get_db();
     $sql = "\n        CREATE TABLE IF NOT EXISTS `{$db->Collector}` (\n          `id` int(10) unsigned NOT NULL AUTO_INCREMENT,\n          `collection_id` int(10) unsigned NOT NULL,\n          `name` TEXT NOT NULL,\n          `bio` TEXT NOT NULL,\n          `public` tinyint(1) default '0',\n          PRIMARY KEY (`id`)\n        ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
     $db->query($sql);
     //set up some fake data to work with
     //this is the kind of work that is usually done in a Controller when processing a form
     $collection = new Collection();
     $collection->name = "Fake Collection";
     $collection->owner_id = 1;
     $collection->save();
     $collector = new Collector();
     $collector->name = "Mr. Fake Collector";
     $collector->collection_id = $collection->id;
     $collector->bio = "I collected stuff for the Fake Collection";
     $collector->public = 1;
     $collector->save();
     $collector = new Collector();
     $collector->name = "Mysterious Ghost Collector";
     $collector->collection_id = $collection->id;
     $collector->bio = "I collected stuff for the Fake Collection, but in secret so I don't want the public to know";
     $collector->public = 0;
     $collector->save();
     $collection = new Collection();
     $collection->name = "Pseudo Collection";
     $collection->owner_id = 1;
     $collection->save();
     $collector = new Collector();
     $collector->name = "Mrs. Pseudo Collector";
     $collector->collection_id = $collection->id;
     $collector->bio = "I collected stuff for the Pseudo Collection";
     $collector->public = 1;
     $collector->save();
 }
 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 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));
 }