/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $this->validate($request, Enterprise::$rules);
     $this->validate($request, Representative::$rules_rl);
     $this->validate($request, Representative::$rules_ct);
     $empresa = Enterprise::create($request->all());
     $rep_legal = new Representative();
     $rep_legal->tipo = 'legal';
     $rep_legal->nombre = $request->input('nombre_rl');
     $rep_legal->apellido = $request->input('apellido_rl');
     $rep_legal->ci = $request->input('ci_rl');
     $rep_legal->rif = $request->input('rif_rl');
     $rep_legal->email = $request->input('email_rl');
     $rep_legal->telefono = $request->input('telefono_rl');
     $rep_legal->direccion = $request->input('direccion_rl');
     $rep_legal->save();
     $empresa->representatives()->attach($rep_legal->id);
     $rep_contact = new Representative();
     $rep_contact->tipo = 'contacto';
     $rep_contact->nombre = $request->input('nombre_ct');
     $rep_contact->apellido = $request->input('apellido_ct');
     $rep_contact->ci = $request->input('ci_ct');
     $rep_contact->rif = $request->input('rif_ct');
     $rep_contact->email = $request->input('email_ct');
     $rep_contact->telefono = $request->input('telefono_ct');
     $rep_contact->direccion = $request->input('direccion_ct');
     $rep_contact->save();
     $empresa->representatives()->attach($rep_contact->id);
     return redirect()->route('admin.empresa.index')->with('message', '<div class="alert alert-success" style="margin-top:15px">Empresa creada con Éxito</div>');
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $representante = Representative::findOrFail($id);
     $data = $request->all();
     $this->validate($request, Representative::$rules);
     $representante->update($data);
     return redirect()->route('admin.empresa.index')->with('message', '<div class="alert alert-success" style="margin-top:15px">Datos actualizados con Éxito</div>');
 }
Example #3
0
 public function openStates()
 {
     $reps = [];
     $count = 0;
     foreach (Location::states as $state => $state_name) {
         if ($count < 20) {
             $count++;
             continue;
         }
         echo "{$state} \n";
         $request = StateAPI::state($state)->then(function ($data) use(&$reps, &$count) {
             foreach ($data as $d) {
                 if (!isset($d['state']) || !isset($d['chamber']) || !isset($d['district'])) {
                     continue;
                 }
                 $division = 'ocd-division/country:us/state:' . $d['state'] . '/sld';
                 $division .= $d['chamber'] == 'upper' ? 'u' : 'l';
                 $division .= ':' . $d['district'];
                 $entry = [];
                 $rep = Representative::where('division', $division)->first();
                 $entry['old'] = $rep;
                 if (null === $rep) {
                     echo "!! new division: {$division} \n";
                     $rep = Representative::create(['division' => $division]);
                 }
                 $rep->load($d);
                 $entry['change'] = [];
                 foreach ($rep->toArray() as $k => $v) {
                     if (null === $entry['old'] || $entry['old']->{$k} !== $v) {
                         $entry['change'][$k] = $v;
                     }
                 }
                 if (count($entry['change']) > 0) {
                     $reps[] = $entry;
                 }
             }
         });
         $request->wait();
         $count++;
         if ($count > 30) {
             break;
         }
     }
     return response()->json($reps);
 }
Example #4
0
 /**
  * convert api data to contact my reps data format
  * @param  object $data google response data
  * @return object       contact my reps api response
  */
 public function validate($data)
 {
     $response = (object) ['reps' => [], 'divisions' => []];
     if (isset($data->normalizedInput)) {
         $l = $data->normalizedInput;
         $city = ucwords($l->city);
         $response->location = (object) array('city' => $city, 'state' => $l->state, 'zip' => $l->zip);
         if (!empty($city) && !empty($l->zip)) {
             $location = Location::where('zip', intval($l->zip))->first();
             if (null !== $location) {
                 $location->city = $city;
                 $location->save();
             }
         }
     }
     if (!isset($data->divisions)) {
         return $response;
     }
     $response->divisions = array_keys((array) $data->divisions);
     if (!isset($data->offices)) {
         return $response;
     }
     foreach ($data->offices as $office) {
         if (!Representative::isValidOffice($office->name)) {
             continue;
         }
         $divisionId = $office->divisionId;
         $l = divisions_split([$divisionId]);
         foreach ($office->officialIndices as $i) {
             $d = $data->officials[$i];
             $d->office = $office->name;
             $d->division = $divisionId;
             if (isset($l['state'])) {
                 $d->state = $l['state'];
             }
             $rep = Representative::find($d);
             // if (is_null($rep)) $rep = new Representative((array) $d);
             if (null === $rep) {
                 continue;
             }
             //for now no new reps
             if (in_array('google', $rep->sources)) {
                 $response->reps[] = $rep;
                 continue;
             }
             if (empty($rep->name) && isset($d->name)) {
                 $rep->name = $d->name;
             }
             if (empty($rep->party) && isset($d->party)) {
                 $rep->party = $d->party;
             }
             if (empty($rep->photo) && isset($d->photoUrl)) {
                 $rep->photo = $d->photoUrl;
             }
             if (empty($rep->address) && isset($d->address)) {
                 $new = [];
                 $a = $d->address;
                 if (is_array($a)) {
                     $a = $a[0];
                 }
                 if (!empty($a->line1)) {
                     $new[] = ucwords($a->line1);
                 }
                 if (!empty($a->line2)) {
                     $new[] = ucwords($a->line2);
                 }
                 if (!empty($a->line3)) {
                     $new[] = ucwords($a->line3);
                 }
                 if (!empty($a->city) && !empty($a->state) && !empty($a->zip)) {
                     $new[] = $a->city . ', ' . $a->state . ' ' . $a->zip;
                 }
                 $rep->address = $new;
             }
             if (isset($d->phones) && count($d->phones) > 0) {
                 $phones = $rep->phones ?? [];
                 foreach ($d->phones as &$p) {
                     $p = str_replace(['(', ') '], ['', '-'], $p);
                     if (!in_array($p, $phones)) {
                         array_push($phones, $p);
                     }
                 }
                 $rep->phones = $phones;
                 if (!isset($rep->phone)) {
                     $rep->phone = $phones[0];
                 }
             }
             if (isset($d->urls) && count($d->urls) > 0) {
                 $urls = $rep->urls ?? [];
                 foreach ($d->urls as $u) {
                     if (!in_array($u, $urls)) {
                         array_push($urls, $u);
                     }
                 }
                 $rep->urls = $urls;
                 if (!isset($rep->website)) {
                     $rep->website = $urls[0];
                 }
             }
             if (isset($d->emails) && count($d->emails) > 0) {
                 $emails = $rep->emails ?? [];
                 foreach ($d->emails as $e) {
                     if (!in_array($e, $emails)) {
                         array_push($emails, $e);
                     }
                 }
                 $rep->emails = $emails;
                 if (!isset($rep->email)) {
                     $rep->email = $emails[0];
                 }
             }
             if (isset($d->channels)) {
                 foreach ($d->channels as $c) {
                     $key = strtolower($c->type) . '_id';
                     if (!isset($rep->{$key})) {
                         $rep->{$key} = $c->id;
                     }
                 }
             }
             $rep->addSource('google');
             $rep->save();
             $response->reps[] = $rep;
         }
     }
     return $response;
 }
Example #5
0
 public static function find($query)
 {
     if (isset($query->first_name) && isset($query->last_name)) {
         $first = $query->first_name;
         if (stripos($first, " ") !== false) {
             $first = explode(" ", $first)[0];
         }
         $query->name = $first . ' ' . $query->last_name;
     }
     $reps = Representative::name($query->name)->get()->all();
     if (count($reps) == 1) {
         return $reps[0];
     } else {
         if (count($reps) > 1) {
             $reps = array_filter($reps, function ($r) use($query) {
                 if (isset($query->division)) {
                     return $query->division == $r->division;
                 }
                 if (isset($query->district) && isset($query->state)) {
                     return $query->district == $r->district && $query->state == $r->state;
                 }
             });
             if (count($reps) === 1) {
                 return array_pop($reps);
             }
         }
     }
     return null;
 }
 /**
  * GET /rep/{id}/flag
  */
 public function flag(Request $request, $id)
 {
     $rep = Representative::where('_id', $id)->first();
     if (null === $rep) {
         return $this->error("no representative with id: {$id}");
     }
     $rep->reports()->create(['text' => $request->input('text', '')]);
 }
Example #7
0
 public function congress()
 {
     $path = 'resources/assets/data/reps/congress.csv';
     $data = $this->readCSV($path);
     $data = CongressAPI::validate($data);
     foreach ($data as $d) {
         $d['source'] = ['opencongress'];
         $rep = Representative::fromData($d);
         //deprecated, use format() then new()
         if (!Representative::exists($rep)) {
             $rep->save();
         }
     }
 }
 public function reports()
 {
     $reps = Representative::has('reports')->get();
     return view('admin.reports', compact('reps'));
 }