hasOne('App\User','id','user_id'); } /** * Writes a change log row for the per-charge collection methods on * `pro_forma_invoice`: processing_fee_collection_method, * cersai_collection_method and legal_charges_collection_method. * * logger() cannot be reused for these. It diffs a model against the request by * matching column names to request keys, and these do not line up: the processing * fee arrives as `processing_fee`, the stored values are 0/1/2 codes that mean * nothing to a reader, and a value can be refused by * ProFormaInvoice::collectionMethodForPersistence() so the request value is not * what ends up in the column. The caller therefore passes what was actually stored. * * Only methods that really changed are logged, and both sides are written as * labels so the change log reads "Collected by CreditFair" to "Add in loan * amount" rather than "0" to "2". * * @param int $applicationId * @param array $before column => method value before the save (null when unset) * @param array $after column => method value after the save * @param string $status application state when the change was made * @return void */ public function logChargeCollectionMethods($applicationId, array $before, array $after, $status = '') { if (empty($applicationId)) { return; } $changes = []; foreach ($after as $column => $newMethod) { $oldMethod = array_key_exists($column, $before) ? $before[$column] : null; // Normalise first so '0' and 0 are the same method, and a column left // untouched (null on both sides) is not reported as a change. $old = (is_null($oldMethod) || $oldMethod === '') ? null : (int) $oldMethod; $new = (is_null($newMethod) || $newMethod === '') ? null : (int) $newMethod; if ($old === $new) { continue; } $changes[$column] = [ 'old' => ProFormaInvoice::collectionMethodLabel($old), 'new' => ProFormaInvoice::collectionMethodLabel($new), ]; } if (!$changes) { return; } $this->insert([ 'application_id' => $applicationId, 'data' => json_encode($changes), 'status' => $status ?: '', 'user_id' => Auth::user() ? Auth::user()->id : (request()->has('user_id') ? request()->get('user_id') : 107), 'created_at' => Carbon::now()->toDateTimeString(), 'model' => ProFormaInvoice::$model, ]); } /** * @param mixed $component * @param string $model * @param bool $is_new * @param int|null $is_co_applicant_added Pass 1 when a co-applicant is being added by this * request. When null, it is derived from the component * state vs the incoming request. */ public function logger($component, $model, $is_new = false, $is_co_applicant_added = null){ $id = $component->id ?? 0; if ($model == ConsumerApplication::$model) { $beforeData = [ 'lender_id' => $component->lender_id ?? '', 'monthly_income' => $component->monthly_income ?? '', 'name' => $component->name ?? '', 'loan_amount' => $component->loan_amount ?? '', 'tenure' => $component->tenure ?? '', 'dob' => $component->dob ?? '', 'pan_card' => $component->pan_card ?? '', 'current_address_line_1' => $component->current_address_line_1 ?? '', 'current_address_pincode' => $component->current_address_pincode ?? '', 'current_address_city' => $component->current_address_city ?? '', 'current_address_state' => $component->current_address_state ?? '', 'current_address_landmark' => $component->current_address_landmark ?? '', 'current_address_status' => $component->current_address_status ?? '', 'permanent_address_line_1' => $component->permanent_address_line_1 ?? '', 'permanent_address_pincode' => $component->permanent_address_pincode ?? '', 'permanent_address_city' => $component->permanent_address_city ?? '', 'permanent_address_state' => $component->permanent_address_state ?? '', 'permanent_address_landmark' => $component->permanent_address_landmark ?? '', 'permanent_address_status' => $component->permanent_address_status ?? '', 'email' => $component->email ?? '', 'mobile_number' => $component->mobile_number ?? '', 'employee_status' => $component->employee_status ?? '', 'merchant_id' => $component->merchant_id ?? '', 'offer_id' => $component->offer_id ?? '', 'approved_amount' => $component->approved_amount ?? '', 'power_savings' => $component->application_element->inverter_power_savings ?? '', 'co_applicant_name' => $component->co_applicant_name ?? '', 'co_applicant_email' => $component->co_applicant_email ?? '', 'co_applicant_mobile_number' => $component->co_applicant_mobile_number ?? '', 'co_applicant_address_line_1' => $component->co_applicant_address_line_1 ?? '', 'co_applicant_address_pincode' => $component->co_applicant_address_pincode ?? '', 'co_applicant_address_city' => $component->co_applicant_address_city ?? '', 'co_applicant_address_state' => $component->co_applicant_address_state ?? '', 'co_applicant_address_landmark' => $component->co_applicant_address_landmark ?? '', 'co_applicant_address_status' => $component->co_applicant_address_status ?? '', 'co_applicant_pan_card' => $component->co_applicant_pan_card ?? '', 'co_applicant_monthly_income' => $component->co_applicant_monthly_income ?? '', 'co_applicant_employee_status' => $component->co_applicant_employee_status ?? '', 'is_co_applicant' => $component->co_applicant_name ? 1 : 0]; }else if ($model == Bank::$model) { $id = $component->application_id; $beforeData = [ 'name' => $component->name ?? '', 'account_type' => $component->account_type ?? '', 'bank_name' => $component->bank_name ?? '', 'account_number' => $component->account_number ?? '', 'branch_name' => $component->branch_name ?? '', 'ifsc_code' => $component->ifsc_code ?? '', 'karza_response' => $component->karza_response ?? '' ]; } $user_id = Auth::user() ? Auth::user()->id : (request()->has('user_id') ? request()->get('user_id') : 107); if (is_null($is_co_applicant_added)) { $is_co_applicant_added = ($model == ConsumerApplication::$model && empty($component->co_applicant_name) && trim((string) request()->get('co_applicant_name')) !== '') ? 1 : 0; } $is_co_applicant_added = $is_co_applicant_added ? 1 : 0; $date = Carbon::now()->toDateTimeString(); $afterDataItems = []; if (isset($beforeData) && $beforeData) { foreach ($beforeData as $key => $item) { if (request()->has($key) && ($is_new || (trim(request()->get($key)) != trim($item)))) { $new = trim(request()->get($key)); if ($key == 'is_co_applicant') { $new = $item; if ($item && !trim(request()->get('co_applicant_name'))) { $new = 0; } else if (!$item && trim(request()->get('co_applicant_name'))) { $new = 1; } if ($item == $new) { continue; } } if(($is_new && $new) || (trim($item) != trim($new))) { if($is_new){ $item = ''; } $afterDataItems[$key] = ['old' => trim($item), 'new' => $new]; } } else { unset($beforeData[$key]); } } if ($afterDataItems) { $this->insert(['application_id' => $id, 'data' => json_encode($afterDataItems), 'status' => $component->state ?? '', 'user_id' => $user_id, 'created_at' => $date, 'model' => $model, 'is_co_applicant_added' => $is_co_applicant_added]); } } } }