/** * @param Model|RetailerProduct $retailerProduct * @param Model|Header $header * * @return bool */ public function setHeaderResult($retailerProduct, $header) { if ($retailerProduct->rates->count() > 0) { $rate_final = 0; if ($header->creditProduct->slug === 'PMO') { $rate = $retailerProduct->rates()->whereHas('creditProduct', function ($q) { $q->where('slug', 'PMO'); })->first(); if ($rate instanceof Rate) { $rate_final = $rate->rate_final; if ($header->coverage->slug === 'MC') { $Fd = [1 => 0, 2 => 0.1, 3 => 0.12, 4 => 0.17]; $TR = $rate->rate_final; $n = $header->details->count(); $Fn = $n > 3 ? $Fd[4] : $Fd[$n]; $rate_final = $TR * $n * (1 - $Fn); } } } else { $rates = $retailerProduct->rates()->doesntHave('creditProduct')->get(); if ($rates->count() === 1) { $rate = $rates->first(); $rate_final = $rate->rate_final; } } if ($rate_final > 0) { $header->ad_retailer_product_id = $retailerProduct->id; $header->total_rate = $rate_final; $header->total_premium = $header->amount_requested * $rate_final / 100; return $this->saveModel(); } } return false; }
/** * @param Model|RetailerProduct $retailerProduct * @param Model|Header $header * * @return array */ public function setVehicleResult($retailerProduct = null, $header) { $premium_total = 0; if ($retailerProduct instanceof RetailerProduct) { $max_year = $retailerProduct->rates()->max('year'); foreach ($retailerProduct->rates as $rate) { if ($header->full_year == $rate->year || $header->full_year > $max_year && $rate->year == $max_year) { /** * @var Detail $detail */ foreach ($header->details as $detail) { foreach ($rate->increments as $increment) { if ($increment->category->category == $detail->category->category) { $rate_vh = $rate->rate_final + $increment->increment; $premium_vh = $rate_vh * $detail->insured_value / 100; if ($header->full_year > $max_year) { $rate_annual = $rate_vh / $max_year; $rate_vh = $rate_annual * $header->full_year; $premium_vh = $rate_vh * $detail->insured_value / 100; if ($header->payment_method === 'PT') { $premium_diff = $premium_vh * 10 / 100; $premium_vh = $premium_vh - $premium_diff; } } $premium_total += $premium_vh; try { $detail->update(['rate' => $rate_vh, 'premium' => $premium_vh]); } catch (QueryException $e) { $this->errors = $e->getMessage(); return false; } } } } } } } else { foreach ($header->details as $detail) { $premium_total += $detail->premium; } } if ($premium_total > 0) { $share = []; $full_year = $header->full_year; if ($header->payment_method === 'PT') { $full_year = 1; } $date = Carbon::createFromDate(null, null, 15)->addMonth(1)->subYear(); $percentage = number_format(100 / $full_year, 2, '.', ','); for ($i = 1; $i <= $full_year; $i++) { array_push($share, ['number' => $i, 'date' => $date->addYear()->toDateString(), 'percentage' => $percentage, 'share' => number_format($premium_total * $percentage / 100, 2)]); } try { $header->update(['ad_retailer_product_id' => $retailerProduct->id, 'total_premium' => $premium_total, 'share' => json_encode($share)]); return true; } catch (QueryException $e) { $this->errors = $e->getMessage(); } } return false; }