input('insured_id'); $name = $request->input('name'); $phone = $request->input('phone'); $network_id = $request->input('network_id'); $network = Network::find($network_id); if (!isset($network)) { return $this->errorResponse(trans('errors.network_not_found')); } $currency_code = $network->country->currency_code; $datetime = $this->getCurrentTimeByCountryCode($network->country->code_country); $q = NhInsurance::with(['user:id,firstname,lastname,phone,email', 'network:id,name', 'beneficiaries', 'nhNetworkConfig', 'monthsGrid'])->where('network_id', $network_id) ->whereIn('state', [InsuranceState::PAID, InsuranceState::PARTIALLY_PAID])->where('start_at', '<=', $datetime); if (!empty($insured_id)) { $q = $q->where('insured_id', $insured_id); } if (!empty($name)) { $q = $q->whereHas('user', function ($query) use ($name) { return $query->where('lastname', 'like', '%' . $name . '%')->orWhere('firstname', 'like', '%' . $name . '%'); }); } if (!empty($phone)) { $q = $q->whereHas('user', function ($query) use ($phone) { return $query->where('phone', 'like', '%' . $phone . '%'); }); } $insured = $q->get(); foreach ($insured as $i) { $monthPrice = $i->monthsGrid; $i->insurance_consumed_amount = $this->toMoneyWithCurrencyCode($i->insurance_coverage_amount, $currency_code); $i->insurance_remaining_amount = $this->toMoneyWithCurrencyCode(($monthPrice->max_insurance_coverage_amount ?? 0) - $i->insurance_coverage_amount, $currency_code); foreach ($i->beneficiaries as $b) { $b->insurance_consumed_amount = $this->toMoneyWithCurrencyCode($b->insurance_coverage_amount, $currency_code); $b->insurance_remaining_amount = $this->toMoneyWithCurrencyCode(($monthPrice->max_insurance_coverage_amount ?? 0) - $b->insurance_coverage_amount, $currency_code); } unset($i->nhNetworkConfig); } return $this->successResponse($insured); } }