where('id', 1)->first(); $hospital_name = $hospital_info->name; $hospital_prefix = $hospital_info->patient_number_abbr; $today = now(); try { echo "Creating new plan... \n"; $plan_id = DB::table('community_health_insurance_plans') ->insertGetId(['name' => $hospital_name . ' Plan', 'plan_fee' => 0, 'plan_start_date' => $today, 'plan_end_date' => now()->addYear(), 'member_annual_limit' => 0, 'family_annual_limit' => 0, 'is_authorized_required' => 0, 'membership_card_color' => 'blue', 'created_by' => 1, 'created_at' => $today, 'updated_at' => $today]); echo "Creating new benefit... \n"; $benefit_id = DB::table('insurance_benefits') ->insertGetId(['name' => $hospital_name . ' Benefit', 'waiting_period' => 0, 'fee' => 0, 'annual_member_limit' => 0, 'annual_family_limit' => 0, 'created_by' => 1, 'created_at' => $today, 'updated_at' => $today, 'plan_id' => $plan_id]); // array to hold insurance benefit items $insurance_benefit_items = []; echo "Fetching insured drugs \n"; $drugs = DB::table('drugs')->where('insurance_coverage', 1)->whereNull('deleted_at') ->get(['id', 'insured_price', 'non_insured_price']); echo "Creating drug benefit items \n"; foreach ($drugs as $drug) { $insurance_benefit_items[4][] = $drug->id; $json_array = [$benefit_id => [ "co_payment" => $drug->insured_price, "ipd_co_payment" => $drug->insured_price, "capitation_fee" => 0, "annual_member_limit" => 0, "annual_family_limit" => 0, "authorisation" => 0, "apply_opd_percentage" => 0, "apply_ipd_percentage" => 0, ]]; DB::table('drugs')->where('id', $drug->id)->update(['insurance_benefit_details' => json_encode($json_array)]); } echo "Fetching insured sundries \n"; $sundries = DB::table('sundries')->where('insurance', 1)->whereNull('deleted_at') ->get(['id', 'insured_price', 'non_insured_price']); echo "Creating sundries benefit items \n"; foreach ($sundries as $sundry) { $insurance_benefit_items[5][] = $sundry->id; $json_array = [$benefit_id => [ "co_payment" => $sundry->insured_price, "ipd_co_payment" => $sundry->insured_price, "capitation_fee" => 0, "annual_member_limit" => 0, "annual_family_limit" => 0, "authorisation" => 0, "apply_opd_percentage" => 0, "apply_ipd_percentage" => 0, ]]; DB::table('sundries')->where('id', $sundry->id)->update(['insurance_benefit_details' => json_encode($json_array)]); } echo "Fetching insured investigations \n"; $investigations = DB::table('investigations')->where('insurance_coverage', 1)->whereNull('deleted_at') ->get(['id', 'insured_price', 'non_insured_price']); echo "Creating investigation benefit items \n"; foreach ($investigations as $investigation) { $insurance_benefit_items[3][] = $investigation->id; $json_array = [$benefit_id => [ "co_payment" => $investigation->insured_price, "ipd_co_payment" => $investigation->insured_price, "capitation_fee" => 0, "annual_member_limit" => 0, "annual_family_limit" => 0, "authorisation" => 0, "apply_opd_percentage" => 0, "apply_ipd_percentage" => 0, ]]; DB::table('investigations')->where('id', $investigation->id)->update(['insurance_benefit_details' => json_encode($json_array)]); } echo "Fetching insured procedures \n"; $procedures = DB::table('procedures')->where('insurance', 1)->whereNull('deleted_at') ->get(['id', 'insured_price', 'non_insured_price']); echo "Creating procedures benefit items \n"; foreach ($procedures as $procedure) { $insurance_benefit_items[2][] = $procedure->id; $json_array = [$benefit_id => [ "co_payment" => $procedure->insured_price, "ipd_co_payment" => $procedure->insured_price, "capitation_fee" => 0, "annual_member_limit" => 0, "annual_family_limit" => 0, "authorisation" => 0, "apply_opd_percentage" => 0, "apply_ipd_percentage" => 0, ]]; DB::table('procedures')->where('id', $procedure->id)->update(['insurance_benefit_details' => json_encode($json_array)]); } echo "Fetching insured services \n"; $services = DB::table('services')->where('insurance_coverage', 1)->whereNull('deleted_at') ->get(['id', 'insured_price', 'non_insured_price']); echo "Creating services benefit items \n"; foreach ($services as $service) { $insurance_benefit_items[1][] = $service->id; $json_array = [$benefit_id => [ "co_payment" => $service->insured_price, "ipd_co_payment" => $service->insured_price, "capitation_fee" => 0, "annual_member_limit" => 0, "annual_family_limit" => 0, "authorisation" => 0, "apply_opd_percentage" => 0, "apply_ipd_percentage" => 0, ]]; DB::table('services')->where('id', $service->id)->update(['insurance_benefit_details' => json_encode($json_array)]); } echo "Creating new benefit items... \n"; foreach ($insurance_benefit_items as $item_type => $benefit_items) { DB::table('insurance_benefit_items') ->insert(['plan_id' => $plan_id, 'benefit_id' => $benefit_id, 'item_type' => $item_type, 'item_id' => implode(",", $benefit_items), 'created_by' => 1]); } // begin shifting the patients to new CHI model echo "Fetching heads of family and updating... \n"; $heads = DB::table('insurance_heads_of_family')->whereNull('deleted_at')->get(); foreach ($heads as $head) { // fetch the family members ignoring those who have been deleted $family_members_arr = []; $family_members_amount_arr = []; $family_subscription_id = false; $family_members = DB::table('insurance_members')->join('patients', 'patients.id', '=', 'insurance_members.patient_id') ->whereNull('insurance_members.deleted_at') ->whereNull('patients.deleted_at') ->where('insurance_members.family_id', $head->id) ->get(['insurance_members.patient_id', 'insurance_members.group_id', 'insurance_members.id', 'patients.gender']); $family_subs = DB::table('insurance_subscriptions')->whereNull('deleted_at') ->whereRaw('FIND_IN_SET(' . $head->id . ',family_heads)')->orderBy('end_date', 'desc') ->first(['id', 'family_heads', 'family_amount', 'start_date', 'end_date', 'payment_date', 'covered_member_ids', 'covered_member_premiums_paid']); if ($family_subs) { $temp_family_heads = explode(",", $family_subs->family_heads); $temp_family_amounts = explode(",", $family_subs->family_amount); $family_premium = $temp_family_amounts[array_search($head->id, $temp_family_heads)] ?? 0; $family_start_date = $family_subs->start_date; $family_end_date = $family_subs->end_date; $family_payment_date = $family_subs->payment_date; $family_subscription_id = $family_subs->id; } else { $family_premium = 0; $family_start_date = NULL; $family_end_date = NULL; $family_payment_date = NULL; } $number_of_family_members = count($family_members); $last_member_premium = 0; // avoid 0 division if ($family_premium != 0 && $number_of_family_members != 0) { // check if money can be shared equally, else force it if ($family_premium % $number_of_family_members == 0) { $individual_premium = $family_premium / $number_of_family_members; } else { $individual_premium = (int)floor($family_premium / $number_of_family_members);; // get any remainder and give it to the last member $last_member_premium = $family_premium - ($individual_premium * ($number_of_family_members - 1)); // confirm the amount not negative else just return 0 $last_member_premium = ($last_member_premium > 0) ? $last_member_premium : 0; } } else { $individual_premium = 0; } // loop through the family members to get valid ones foreach ($family_members as $family_member) { $title = (empty($family_member->gender) || $family_member->gender == 1) ? 1 : 2; $member_account_number = $hospital_prefix . "/" . date('Y') . "/" . $family_member->group_id. "/" . sprintf("%04d", $family_member->id); // check if this is the last person if ((count($family_members_arr) + 1) == $number_of_family_members && $number_of_family_members != 1) { $individual_premium = $last_member_premium; } $family_members_arr[] = $family_member->id; $family_members_amount_arr[] = $individual_premium; DB::table('insurance_members')->where('id', $family_member->id) ->update(['title' => $title, 'premium' => $individual_premium, 'chi_plan' => $plan_id, 'membership_start_date' => $family_start_date, 'member_account_number' => $member_account_number, 'membership_registration_date' => $family_payment_date, 'existing_premium_start_date' => $family_start_date, 'existing_premium_end_date' => $family_end_date]); } // if the subscription table is available then update if ($family_subscription_id && $family_subs) { $exploded_covered_member_ids = is_null($family_subs->covered_member_ids) ? [] : explode(",", $family_subs->covered_member_ids); $exploded_covered_member_premiums = is_null($family_subs->covered_member_premiums_paid) ? [] : explode(",", $family_subs->covered_member_premiums_paid); $exploded_covered_member_ids = array_merge($exploded_covered_member_ids, $family_members_arr); $exploded_covered_member_premiums = array_merge($exploded_covered_member_premiums, $family_members_amount_arr); DB::table('insurance_subscriptions')->where('id', $family_subscription_id) ->update(['covered_member_ids' => implode(",", $exploded_covered_member_ids), 'covered_member_premiums_paid' => implode(",", $exploded_covered_member_premiums)]); } // update the family listing DB::table('insurance_heads_of_family')->where('id', $head->id) ->update(['family_members_ids' => implode(",", $family_members_arr), 'family_member_premiums' => implode(",", $family_members_amount_arr)]); } // fetch number of insurance members with null $member_count = DB::table('insurance_members')->whereNull(['deleted_at', 'chi_plan'])->count(); echo "Insurance members with null are " . $member_count . "\n"; // fetch number of insurance heads with null $heads_count = DB::table('insurance_heads_of_family')->whereNull(['deleted_at', 'family_members_ids'])->count(); echo "Insurance heads of family with null are " . $heads_count . "\n"; // fetch number of insurance subscriptions with null $subs_count = DB::table('insurance_subscriptions')->whereNull(['deleted_at', 'covered_member_ids'])->count(); echo "Insurance subscriptions with null are " . $subs_count . "\n"; echo "Migration complete \n"; DB::commit(); } catch (\Exception $e) { echo $e->getMessage() . "\n"; DB::rollback(); } return 0; } }