function add_loan(array $request, $lead_id) { DB::beginTransaction(); try { /* Lead converted to application */ $lead = Lead::find($lead_id); /* If application is not created before and this is the first time */ if ($lead->status != 2) { $lead->status = 2; $lead->save(); /* Create application */ $application = Application::create(['date_created' => get_today_date(), 'ex_user_id' => \Auth::user()->id, 'ex_lead_id' => $lead_id, 'submitted' => 0]); $this->changeStatus($application->id, 1); //add logs for application addition $log = Log::create(['added_by' => \Auth::user()->id, 'comment' => 'Application Created']); ApplicationLog::create(['application_id' => $application->id, 'log_id' => $log->id]); } else { /* Delete removed loans */ $application = Application::where('ex_lead_id', $lead_id)->first(); if (isset($request['loan_id'])) { $loan_ids = NewApplicantLoan::where('application_id', $application->id)->lists('id')->toArray(); $removed_loans = array_diff($loan_ids, $request['loan_id']); NewApplicantLoan::whereIn('id', $removed_loans)->delete(); } } foreach ($request['loan_purpose'] as $key => $loan_purpose) { if (!isset($request['loan_id'][$key])) { NewApplicantLoan::create(['application_id' => $application->id, 'amount' => $request['amount'][$key], 'loan_purpose' => $loan_purpose, 'deposit_paid' => $request['deposit_paid'][$key], 'settlement_date' => $request['settlement_date'][$key], 'loan_usage' => $request['loan_usage'][$key], 'total_loan_term' => $request['total_loan_term'][$key], 'loan_type' => $request['loan_type'][$key], 'fixed_rate_term' => $request['fixed_rate_term'][$key], 'repayment_type' => $request['repayment_type'][$key], 'io_term' => $request['io_term'][$key], 'interest_rate' => $request['interest_rate'][$key]]); } else { $loan = NewApplicantLoan::find($request['loan_id'][$key]); $loan->application_id = $application->id; $loan->amount = $request['amount'][$key]; $loan->loan_purpose = $loan_purpose; $loan->deposit_paid = $request['deposit_paid'][$key]; $loan->settlement_date = $request['settlement_date'][$key]; $loan->loan_usage = $request['loan_usage'][$key]; $loan->total_loan_term = $request['total_loan_term'][$key]; $loan->loan_type = $request['loan_type'][$key]; $loan->fixed_rate_term = $request['fixed_rate_term'][$key]; $loan->repayment_type = $request['repayment_type'][$key]; $loan->io_term = $request['io_term'][$key]; $loan->interest_rate = $request['interest_rate'][$key]; $loan->save(); } } DB::commit(); return true; } catch (\Exception $e) { DB::rollback(); dd($e); return false; } }
function remove($lead_id) { DB::beginTransaction(); try { //get details $lead = Lead::find($lead_id); //loan details $loan_details = Loan::where('ex_leads_id', $lead_id)->first(); //lead assign $lead_assign = ClientLeadAssign::where('ex_leads_id', $lead_id); $lead_assign->delete(); $loan_details->delete(); //logs $lead_logs = LeadLog::where('ex_lead_id', $lead_id)->get(); $lead_logs->delete(); foreach ($lead_logs as $lead_log) { $log = Log::find($lead_log->log_id); $log->delete(); } $lead->delete(); DB::commit(); // all good } catch (\Exception $e) { DB::rollback(); dd($e); // something went wrong } }