/**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     //
     $this->validate($request, ['first_name' => 'required', 'last_name' => 'required', 'email' => 'email', 'dob' => 'date', 'url' => 'url', 'parish_id' => 'integer|min:0', 'gender' => 'in:Male,Female,Other,Unspecified']);
     $contact = \montserrat\Contact::with('addresses.location', 'emails.location', 'phones.location', 'websites', 'emergency_contact')->findOrFail($request->input('id'));
     $contact->title = $request->input('title');
     $contact->firstname = $request->input('first_name');
     $contact->middlename = $request->input('middle_name');
     $contact->lastname = $request->input('last_name');
     $contact->suffix = $request->input('suffix');
     $contact->nickname = $request->input('nick_name');
     $contact->display_name = $request->input('display_name');
     $contact->sort_name = $request->input('sort_name');
     //emergency contact info
     $contact->emergency_contact->name = $request->input('emergency_contact_name');
     $contact->emergency_contact->relationship = $request->input('emergency_contact_relationship');
     $contact->emergency_contact->phone = $request->input('emergency_contact_phone');
     $contact->emergency_contact->phone_alternate = $request->input('emergency_contact_phone_alternate');
     //demographic info
     $contact->gender = $request->input('gender');
     $contact->dob = $request->input('dob');
     $contact->ethnicity = $request->input('ethnicity');
     $contact->parish_id = $request->input('parish_id');
     $contact->languages = $request->input('languages');
     //health info
     $contact->medical = $request->input('medical');
     $contact->dietary = $request->input('dietary');
     //misc info
     $contact->notes = $request->input('notes');
     $contact->roompreference = $request->input('roompreference');
     //group or roles info
     $contact->is_donor = $request->input('is_donor');
     $contact->is_retreatant = $request->input('is_retreatant');
     $contact->is_director = $request->input('is_director');
     $contact->is_innkeeper = $request->input('is_innkeeper');
     $contact->is_assistant = $request->input('is_assistant');
     $contact->is_captain = $request->input('is_captain');
     $contact->is_staff = $request->input('is_staff');
     $contact->is_volunteer = $request->input('is_volunteer');
     $contact->is_pastor = $request->input('is_pastor');
     $contact->is_bishop = $request->input('is_bishop');
     $contact->is_catholic = $request->input('is_catholic');
     $contact->is_board = $request->input('is_board');
     $contact->is_formerboard = $request->input('is_formerboard');
     $contact->is_jesuit = $request->input('is_jesuit');
     $contact->is_deceased = $request->input('is_deceased');
     $contact->save();
     $home_address = \montserrat\Address::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_HOME]);
     //dd($home_address);
     $home_address->street_address = $request->input('address_home_address1');
     $home_address->supplemental_address_1 = $request->input('address_home_address2');
     $home_address->city = $request->input('address_home_city');
     $home_address->state_province_id = $request->input('address_home_state');
     $home_address->postal_code = $request->input('address_home_zip');
     $home_address->country_id = $request->input('address_home_country');
     $home_address->save();
     $work_address = \montserrat\Address::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_WORK]);
     $work_address->street_address = $request->input('address_work_address1');
     $work_address->supplemental_address_1 = $request->input('address_work_address2');
     $work_address->city = $request->input('address_work_city');
     $work_address->state_province_id = $request->input('address_work_state');
     $work_address->postal_code = $request->input('address_work_zip');
     $work_address->country_id = $request->input('address_work_country');
     $work_address->save();
     $other_address = \montserrat\Address::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_OTHER]);
     $other_address->street_address = $request->input('address_other_address1');
     $other_address->supplemental_address_1 = $request->input('address_other_address2');
     $other_address->city = $request->input('address_other_city');
     $other_address->state_province_id = $request->input('address_other_state');
     $other_address->postal_code = $request->input('address_other_zip');
     $other_address->country_id = $request->input('address_other_country');
     $other_address->save();
     $phone_home_phone = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_HOME, 'phone_type' => 'Phone']);
     $phone_home_phone->phone = $request->input('phone_home_phone');
     $phone_home_phone->save();
     $phone_home_mobile = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_HOME, 'phone_type' => 'Mobile']);
     $phone_home_mobile->phone = $request->input('phone_home_mobile');
     $phone_home_mobile->save();
     $phone_home_fax = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_HOME, 'phone_type' => 'Fax']);
     $phone_home_fax->phone = $request->input('phone_home_fax');
     $phone_home_fax->save();
     $phone_work_phone = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_WORK, 'phone_type' => 'Phone']);
     $phone_work_phone->phone = $request->input('phone_work_phone');
     $phone_work_phone->save();
     $phone_work_mobile = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_WORK, 'phone_type' => 'Mobile']);
     $phone_work_mobile->phone = $request->input('phone_work_mobile');
     $phone_work_mobile->save();
     $phone_work_fax = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_WORK, 'phone_type' => 'Fax']);
     $phone_work_fax->phone = $request->input('phone_work_fax');
     $phone_work_fax->save();
     $phone_other_phone = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_OTHER, 'phone_type' => 'Phone']);
     $phone_other_phone->phone = $request->input('phone_other_phone');
     $phone_other_phone->save();
     $phone_other_mobile = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_OTHER, 'phone_type' => 'Mobile']);
     $phone_other_mobile->phone = $request->input('phone_other_mobile');
     $phone_other_mobile->save();
     $phone_other_fax = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_OTHER, 'phone_type' => 'Fax']);
     $phone_other_fax->phone = $request->input('phone_other_fax');
     $phone_other_fax->save();
     $email_home = \montserrat\Email::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_HOME]);
     $email_home->email = $request->input('email_home');
     $email_home->save();
     $email_work = \montserrat\Email::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_WORK]);
     $email_work->email = $request->input('email_work');
     $email_work->save();
     $email_other = \montserrat\Email::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => LOCATION_TYPE_OTHER]);
     $email_other->email = $request->input('email_other');
     $email_other->save();
     $url_main = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'Main']);
     $url_main->url = $request->input('url_main');
     $url_main->save();
     $url_work = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'Work']);
     $url_work->url = $request->input('url_work');
     $url_work->save();
     $url_facebook = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'Facebook']);
     $url_facebook->url = $request->input('url_facebook');
     $url_facebook->save();
     $url_google = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'Google']);
     $url_google->url = $request->input('url_google');
     $url_google->save();
     $url_instagram = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'Instagram']);
     $url_instagram->url = $request->input('url_instagram');
     $url_instagram->save();
     $url_linkedin = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'LinkedIn']);
     $url_linkedin->url = $request->input('url_linkedin');
     $url_linkedin->save();
     $url_twitter = \montserrat\Website::firstOrNew(['contact_id' => $contact->id, 'website_type' => 'Twitter']);
     $url_twitter->url = $request->input('url_twitter');
     $url_twitter->save();
     return Redirect::action('ContactsController@index');
     //
 }
 public function merge($contact_id, $merge_id = NULL)
 {
     $contact = \montserrat\Contact::findOrFail($contact_id);
     $similar = \montserrat\Contact::whereSortName($contact->sort_name)->get();
     $duplicates = $similar->keyBy('id');
     $duplicates->forget($contact->id);
     if (!empty($merge_id)) {
         $merge = \montserrat\Contact::findOrFail($merge_id);
         //dd($merge);
         if (empty($contact->prefix_id) && !empty($merge->prefix_id)) {
             $contact->prefix_id = $merge->prefix_id;
         }
         if (empty($contact->first_name) && !empty($merge->first_name)) {
             $contact->first_name = $merge->first_name;
         }
         if (empty($contact->nick_name) && !empty($merge->nick_name)) {
             $contact->nick_name = $merge->nick_name;
         }
         if (empty($contact->middle_name) && !empty($merge->middle_name)) {
             $contact->middle_name = $merge->middle_name;
         }
         if (empty($contact->last_name) && !empty($merge->last_name)) {
             $contact->last_name = $merge->last_name;
         }
         if (empty($contact->organization_name) && !empty($merge->organization_name)) {
             $contact->organization_name = $merge->organization_name;
         }
         if (empty($contact->suffix_id) && !empty($merge->suffix_id)) {
             $contact->suffix_id = $merge->suffix_id;
         }
         if (empty($contact->gender_id) && !empty($merge->gender_id)) {
             $contact->gender_id = $merge->gender_id;
         }
         if (empty($contact->birth_date) && !empty($merge->birth_date)) {
             $contact->birth_date = $merge->birth_date;
         }
         if (empty($contact->religion_id) && !empty($merge->religion_id)) {
             $contact->religion_id = $merge->religion_id;
         }
         if (empty($contact->occupation_id) && !empty($merge->occupation_id)) {
             $contact->occupation_id = $merge->occupation_id;
         }
         if (empty($contact->ethnicity_id) && !empty($merge->ethnicity_id)) {
             $contact->ethnicity_id = $merge->ethnicity_id;
         }
         $contact->save();
         //addresses
         if (NULL === $contact->address_primary) {
             $contact->address_primary = new \montserrat\Address();
             $contact->address_primary->contact_id = $contact->id;
             $contact->address_primary->is_primary = 1;
         }
         if (empty($contact->address_primary->street_address) && !empty($merge->address_primary->street_address)) {
             $contact->address_primary->street_address = $merge->address_primary->street_address;
         }
         if (empty($contact->address_primary->supplemental_address) && !empty($merge->address_primary->supplemental_address)) {
             $contact->address_primary->supplemental_address = $merge->address_primary->supplemental_address;
         }
         if (empty($contact->address_primary->city) && !empty($merge->address_primary->city)) {
             $contact->address_primary->city = $merge->address_primary->city;
         }
         if (empty($contact->address_primary->state_province_id) && !empty($merge->address_primary->state_province_id)) {
             $contact->address_primary->state_province_id = $merge->address_primary->state_province_id;
         }
         if (empty($contact->address_primary->postal_code) && !empty($merge->address_primary->postal_code)) {
             $contact->address_primary->postal_code = $merge->address_primary->postal_code;
         }
         if (empty($contact->address_primary->country_code) && !empty($merge->address_primary->country_code)) {
             $contact->address_primary->country_code = $merge->address_primary->country_code;
         }
         $contact->address_primary->save();
         //emergency_contact_info
         if (NULL === $contact->emergency_contact) {
             $contact->emergency_contact = new \montserrat\EmergencyContact();
             $contact->emergency_contact->contact_id = $contact->id;
         }
         if (empty($contact->emergency_contact->name) && !empty($merge->emergency_contact->name)) {
             $contact->emergency_contact->name = $merge->emergency_contact->name;
         }
         if (empty($contact->emergency_contact->relationship) && !empty($merge->emergency_contact->relationship)) {
             $contact->emergency_contact->relationship = $merge->emergency_contact->relationship;
         }
         if (empty($contact->emergency_contact->phone) && !empty($merge->emergency_contact->phone)) {
             $contact->emergency_contact->phone = $merge->emergency_contact->phone;
         }
         if (empty($contact->emergency_contact->phone_alternate) && !empty($merge->emergency_contact->phone_alternate)) {
             $contact->emergency_contact->phone_alternate = $merge->emergency_contact->phone_alternate;
         }
         $contact->emergency_contact->save();
         //emails
         foreach ($merge->emails as $email) {
             $contact_email = \montserrat\Email::firstOrNew(['contact_id' => $contact->id, 'location_type_id' => $email->location_type_id]);
             $contact_email->contact_id = $contact->id;
             $contact_email->location_type_id = $email->location_type_id;
             $contact_email->is_primary = $email->is_primary;
             //only create or overwrite if the current email address for the location is empty
             if (empty($contact_email->email)) {
                 $contact_email->email = $email->email;
                 $contact_email->save();
             }
         }
         //phones
         foreach ($merge->phones as $phone) {
             $contact_phone = \montserrat\Phone::firstOrNew(['contact_id' => $contact->id, "location_type_id" => $phone->location_type_id, "phone_type" => $phone->phone_type]);
             $contact_phone->contact_id = $contact->id;
             $contact_phone->location_type_id = $phone->location_type_id;
             $contact_phone->phone_type = $phone->phone_type;
             $contact_phone->is_primary = $phone->is_primary;
             //only create or overwrite if the current email address for the location is empty
             if (empty($contact_phone->phone)) {
                 $contact_phone->phone = $phone->phone;
                 $contact_phone->save();
             }
         }
         //notes - move all from merge to contact
         foreach ($merge->notes as $note) {
             $note->entity_id = $contact_id;
             $note->save();
         }
         //groups - move all from merge to contact
         foreach ($merge->groups as $group) {
             $group_exist = \montserrat\GroupContact::whereContactId($contact_id)->whereGroupId($group->group_id)->first();
             if (!isset($group_exist)) {
                 $group->contact_id = $contact_id;
                 $group->save();
             }
         }
         //relationships
         foreach ($merge->a_relationships as $a_relationship) {
             $a_relationship_exist = \montserrat\Relationship::whereContactIdA($contact_id)->whereContactIdB($a_relationship->contact_id_b)->whereRelationshipTypeId($a_relationship->relationship_type_id)->first();
             if (!isset($a_relationship_exist)) {
                 $a_relationship->contact_id_a = $contact_id;
                 $a_relationship->save();
             }
         }
         foreach ($merge->b_relationships as $b_relationship) {
             $b_relationship_exist = \montserrat\Relationship::whereContactIdB($contact_id)->whereContactIdA($b_relationship->contact_id_a)->whereRelationshipTypeId($b_relationship->relationship_type_id)->first();
             if (!isset($b_relationship_exist)) {
                 $b_relationship->contact_id_b = $contact_id;
                 $b_relationship->save();
             }
         }
         //touchpoints
         foreach ($merge->touchpoints as $touchpoint) {
             $touchpoint->person_id = $contact_id;
             $touchpoint->save();
         }
         //attachments
         foreach ($merge->attachments as $attachment) {
             $path = 'contact/' . $merge_id . '/attachments/' . $attachment->uri;
             $newpath = 'contact/' . $contact_id . '/attachments/' . $attachment->uri;
             Storage::move($path, $newpath);
             $attachment->entity_id = $contact->id;
             $attachment->save();
         }
     }
     return view('persons.merge', compact('contact', 'duplicates'));
 }