function add(array $request) { DB::beginTransaction(); try { if ($request['ex_clients_id'] == 0) { $client = Client::create(['preferred_name' => $request['preferred_name'], 'title' => $request['title'], 'given_name' => $request['given_name'], 'surname' => $request['surname'], 'added_by_users_id' => \Auth::user()->id]); $phone_number = $request['phone']; $phone = Phone::create(['number' => $phone_number, 'type' => 'home']); ClientPhone::create(['phones_id' => $phone->id, 'ex_clients_id' => $client->id, 'is_current' => 1]); $client_id = $client->id; } else { $client_id = $request['ex_clients_id']; } $lead = Lead::create(['referral_notes' => $request['referral_notes'], 'status' => 0, 'ex_clients_id' => $client_id, 'added_by_users_id' => \Auth::user()->id]); $loan = Loan::create(['loan_purpose' => $request['loan_purpose'], 'loan_type' => $request['loan_type'], 'ex_leads_id' => $lead->id]); //add logs $log = Log::create(['added_by' => \Auth::user()->id, 'comment' => 'Lead Created']); LeadLog::create(['ex_lead_id' => $lead->id, 'log_id' => $log->id]); DB::commit(); return $lead->id; } catch (\Exception $e) { DB::rollback(); dd($e); return false; } }
function addPhoneDetails($request, $key, $applicant_id) { //if(isset($request['mobile'][$key]) && $request['mobile'][$key] != null) { we need it for later in edit box $mobile_phone = Phone::create(['number' => $request['mobile'][$key], 'type' => 'mobile']); ApplicantPhone::create(['phones_id' => $mobile_phone->id, 'applicants_id' => $applicant_id]); //} //if(isset($request['home'][$key]) && $request['home'][$key] != null) { $home_phone = Phone::create(['number' => $request['home'][$key], 'type' => 'home']); ApplicantPhone::create(['phones_id' => $home_phone->id, 'applicants_id' => $applicant_id]); //} //if(isset($request['work'][$key]) && $request['work'][$key] != null) { $work_phone = Phone::create(['number' => $request['work'][$key], 'type' => 'work']); ApplicantPhone::create(['phones_id' => $work_phone->id, 'applicants_id' => $applicant_id]); //} }
function edit(array $request, $client_id) { DB::beginTransaction(); try { $client = Client::find($client_id); $client->preferred_name = $request['preferred_name']; $client->given_name = $request['given_name']; $client->surname = $request['surname']; $client->email = $request['email']; $client->salary = $request['salary']; $client->occupation = $request['occupation']; $client->salary_type = $request['salary_type']; $client->introducer = $request['introducer']; $client->title = $request['title']; $client->save(); /* Delete associated addresses and phone numbers... change this later for code optimization */ Client::find($client_id)->client_addresses()->with('address')->delete(); Client::find($client_id)->client_phones()->with('phone')->delete(); //change this later for multiple addresses $address = Addresses::create(['line1' => $request['line1'], 'line2' => $request['line2'], 'suburb' => $request['suburb'], 'state' => $request['state'], 'postcode' => $request['postcode'], 'country' => $request['country']]); ClientAddress::create(['ex_clients_id' => $client->id, 'address_type_id' => 1, 'is_current' => 1, 'address_id' => $address->id]); /* Add Phone numbers*/ $phone_numbers = $request['phone']; foreach ($phone_numbers as $key => $phone_num) { if ($phone_num != '') { $phone = Phone::create(['number' => $phone_num, 'type' => $request['phonetype'][$key]]); ClientPhone::create(['phones_id' => $phone->id, 'ex_clients_id' => $client->id]); } } DB::commit(); return $client->id; // all good } catch (\Exception $e) { DB::rollback(); dd($e); // something went wrong } if ($client) { return true; } else { return false; } }