diff --git a/app/Traits/Helper.php b/app/Traits/Helper.php index 28d2d55..fd24a32 100644 --- a/app/Traits/Helper.php +++ b/app/Traits/Helper.php @@ -4,6 +4,7 @@ namespace App\Traits; +use App\HealthCareSheetType; use App\InsuranceSubscriptionAffiliation; use App\InsuranceSubscriptionState; use App\Models\CountriesCurrency; @@ -23,6 +24,7 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Http\Request; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; @@ -121,7 +123,7 @@ trait Helper public function storeBeneficiariesAndGetBonus(NhInsurancesSubscription $subscription, Request $request, - $networkConfig, NhMonthsPricesGrid $monthPrice, string $datetime) + $networkConfig, NhMonthsPricesGrid $monthPrice, string $datetime) { $subscription->state = InsuranceSubscriptionState::UNDER_VALIDATION; @@ -164,35 +166,33 @@ trait Helper return date('d') . '/' . date('m') . '/' . date('Y') . '/' . $agent_code; } - public function fetchHealthCareSheetAmounts($sheet): void + public function fetchHealthCareSheetAmounts($sheet) { $insurerAmount = 0; $insuredAmount = 0; - $sum = $sheet->performances()->selectRaw('SUM(moderator_ticket) as moderator_ticket , SUM(insurance_amount) as insurance_amount') - ->groupBy('nh_health_care_sheets_performances.id')->groupBy('nh_health_care_sheets_performances.sheet_id')->first(); - - if (isset($sum)) { - $insuredAmount += $sum->moderator_ticket; - $insurerAmount += $sum->insurance_amount; - } + if ($sheet->type == HealthCareSheetType::CONSULTATION) { + $sum = current(DB::select("SELECT SUM(moderator_ticket) as moderator_ticket , SUM(insurance_amount) as insurance_amount FROM nh_performances p INNER JOIN + nh_health_care_sheets_performances hp ON p.id = hp.performance_id WHERE hp.sheet_id = :sheet_id LIMIT 1", ['sheet_id' => $sheet->id])); + if (isset($sum)) { + $insuredAmount += $sum->moderator_ticket; + $insurerAmount += $sum->insurance_amount; + } + } else { + $sum = current(DB::select("SELECT SUM(insured_paid_amount) as insured_paid_amount , SUM(insurer_paid_amount) as insurer_paid_amount FROM nh_medical_prescriptions p INNER JOIN + nh_health_care_sheets_prescriptions hp ON p.id = hp.prescription_id WHERE hp.sheet_id = :sheet_id LIMIT 1", ['sheet_id' => $sheet->id])); + if (isset($sum)) { + $insuredAmount += $sum->insured_paid_amount; + $insurerAmount += $sum->insurer_paid_amount; + } - $sum = $sheet->prescriptions()->selectRaw('SUM(insured_paid_amount) as insured_paid_amount , SUM(insurer_paid_amount) as insurer_paid_amount') - ->groupBy('nh_health_care_sheets_prescriptions.id')->groupBy('nh_health_care_sheets_prescriptions.sheet_id')->first(); - - if (isset($sum)) { - $insuredAmount += $sum->insured_paid_amount; - $insurerAmount += $sum->insurer_paid_amount; - } - - $sum = $sheet->exams()->selectRaw('SUM(insured_paid_amount) as insured_paid_amount , SUM(insurer_paid_amount) as insurer_paid_amount') - ->groupBy('nh_health_care_sheets_exams.id')->groupBy('nh_health_care_sheets_exams.sheet_id')->first(); - - - if (isset($sum)) { - $insuredAmount += $sum->insured_paid_amount; - $insurerAmount += $sum->insurer_paid_amount; + $sum = current(DB::select("SELECT SUM(insured_paid_amount) as insured_paid_amount , SUM(insurer_paid_amount) as insurer_paid_amount FROM nh_exams e INNER JOIN + nh_health_care_sheets_exams he ON e.id = he.exam_id WHERE he.sheet_id = :sheet_id LIMIT 1", ['sheet_id' => $sheet->id])); + if (isset($sum)) { + $insuredAmount += $sum->insured_paid_amount; + $insurerAmount += $sum->insurer_paid_amount; + } } $sheet->insurer_amount = $insurerAmount;