mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
93 lines
4.1 KiB
PHP
93 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace Streamline\Services\WardManagement;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Streamline\Exceptions\ModelNotFoundException;
|
|
use Streamline\Models\Drug;
|
|
use Streamline\Models\WardTreatmentDispensation;
|
|
|
|
class WardTreatmentDispensationService
|
|
{
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @throws ModelNotFoundException
|
|
*/
|
|
public function saveDispensation(int $patient_id, int $episode_id, int $drug_id, int $quantity_given,
|
|
int $ward_treatment_id, int $ward_id, $batch_id = null): bool|int
|
|
{
|
|
$drug_details = Drug::find($drug_id);
|
|
|
|
if (!$drug_details) {
|
|
throw new ModelNotFoundException("Drug not found: ${$drug_id}");
|
|
}
|
|
|
|
$drug_exists = WardTreatmentDispensation::where('episode_id', $episode_id)
|
|
->where('drug_id', $drug_id)->first();
|
|
|
|
if (!$drug_exists) {
|
|
$ward_treatments_dispensation = new WardTreatmentDispensation;
|
|
$ward_treatments_dispensation->patient_id = $patient_id;
|
|
$ward_treatments_dispensation->episode_id = $episode_id;
|
|
$ward_treatments_dispensation->drug_id = $drug_id;
|
|
$ward_treatments_dispensation->quantity_given = $quantity_given;
|
|
$ward_treatments_dispensation->ward_treatment_id = $ward_treatment_id;
|
|
$ward_treatments_dispensation->ward_id = $ward_id;
|
|
$ward_treatments_dispensation->current_chart_of_account = $drug_details->account_id;
|
|
$ward_treatments_dispensation->cost_price = $drug_details->cost_price;
|
|
$ward_treatments_dispensation->inventory_account = $drug_details->inventory_account;
|
|
$ward_treatments_dispensation->cost_of_goods_account = $drug_details->cost_of_goods_account;
|
|
$ward_treatments_dispensation->batch_id = $batch_id;
|
|
|
|
if(patient_insurance_status($patient_id) == 1){
|
|
$drug_insurance_details = get_item_insurance_pricing($patient_id, $drug_id, 4, true, $ward_id);
|
|
$drug_selling_price = $drug_insurance_details[2];
|
|
$ward_treatments_dispensation->chi_price = $drug_insurance_details[1];
|
|
} else {
|
|
$price_list_id = is_patient_category_attached_to_price_list($patient_id);
|
|
if ($price_list_id) {
|
|
$drug_selling_price = get_price_list_category_price($price_list_id, 3, $drug_id);
|
|
} else {
|
|
$drug_selling_price = $drug_details->non_insured_price;
|
|
}
|
|
}
|
|
|
|
$ward_treatments_dispensation->price = $drug_selling_price;
|
|
$ward_treatments_dispensation->created_by = Auth::id();
|
|
$dispensation_history = [[
|
|
'entered_at' => Carbon::now()->toDateTimeString(),
|
|
'entered_by' => Auth::id()
|
|
]];
|
|
$ward_treatments_dispensation->dispensation_history = json_encode($dispensation_history);
|
|
} else {
|
|
$ward_treatments_dispensation = $drug_exists;
|
|
$ward_treatments_dispensation->quantity_given = $ward_treatments_dispensation->quantity_given + $quantity_given;
|
|
$ward_treatments_dispensation->updated_by = Auth::id();
|
|
|
|
$dispensation_history = json_decode($ward_treatments_dispensation->dispensation_history, true);
|
|
|
|
if (is_null($ward_treatments_dispensation->dispensation_history)) {
|
|
$dispensation_history[] = [
|
|
'entered_at' => $ward_treatments_dispensation->created_at->toDateTimeString(),
|
|
'entered_by' => $ward_treatments_dispensation->created_by,
|
|
];
|
|
}
|
|
|
|
$dispensation_history[] = [
|
|
'entered_at' => Carbon::now()->toDateTimeString(),
|
|
'entered_by' => Auth::id(),
|
|
];
|
|
$ward_treatments_dispensation->dispensation_history = json_encode($dispensation_history);
|
|
}
|
|
|
|
if ($ward_treatments_dispensation->save()) {
|
|
return $ward_treatments_dispensation->id;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
} |