diff --git a/app/Http/Controllers/HealthCareSheetController.php b/app/Http/Controllers/HealthCareSheetController.php index 91047dd..91421f6 100755 --- a/app/Http/Controllers/HealthCareSheetController.php +++ b/app/Http/Controllers/HealthCareSheetController.php @@ -839,6 +839,10 @@ class HealthCareSheetController extends Controller if (!in_array($i->id, $prescriptionsIds)) { continue; } + if ($i->billed) { + return $this->errorResponse(__('errors.prescription_already_invoiced', ['id' => array_search($i->id, $prescriptionsIds) + 1])); + } + $itemId = $i->id; $item->unit_price = array_filter($prescriptions, function ($r) use ($itemId) { return $r['id'] == $itemId; @@ -850,6 +854,9 @@ class HealthCareSheetController extends Controller if (!in_array($i->id, $examsIds)) { continue; } + if ($i->billed) { + return $this->errorResponse(__('errors.exam_already_invoiced', ['id' => array_search($i->id, $examsIds) + 1])); + } $itemId = $i->id; $item->unit_price = array_filter($exams, function ($r) use ($itemId) { return $r['id'] == $itemId; @@ -862,8 +869,12 @@ class HealthCareSheetController extends Controller $item->insured_paid_amount = $parts->insured_part * $item->unit_price * ($item->quantity ?? 1); $item->insurer_paid_amount = $parts->insurer_part * $item->unit_price * ($item->quantity ?? 1); $item->created_at = $item->updated_at = $datetime; + $item->billed = true; $item->push(); + $i->billed = true; + $i->save(); + if ($relation == 'prescriptions') { NhHealthCareSheetsPrescription::create([ @@ -884,12 +895,12 @@ class HealthCareSheetController extends Controller } #Clone pivots - $performances = NhHealthCareSheetsPerformance::where('sheet_id', $sheet->id)->get(); - foreach ($performances as $pf) { - $p = $pf->replicate(); - $p->sheet_id = $healthCareSheet->id; - $p->push(); - } +// $performances = NhHealthCareSheetsPerformance::where('sheet_id', $sheet->id)->get(); +// foreach ($performances as $pf) { +// $p = $pf->replicate(); +// $p->sheet_id = $healthCareSheet->id; +// $p->push(); +// } NhHealthCareSheetsHistory::create([ 'action' => 'ADD', @@ -948,7 +959,7 @@ class HealthCareSheetController extends Controller * description="Status des feuilles de soins", * @OA\Schema( * type="string", - * enum = {"UNTREATED","TREATED","ACCEPTED"}, + * enum = {"UNTREATED","TREATED","ACCEPTED","TO_BILL"}, * default = "UNTREATED" * ), * in="query", @@ -979,13 +990,22 @@ class HealthCareSheetController extends Controller $this->validate($request, [ 'user_id' => 'required|integer|exists:users,id', 'type' => 'nullable|in:CONSULTATION,EXECUTION', - 'state' => 'nullable|in:UNTREATED,TREATED,ACCEPTED' + 'state' => 'nullable|in:UNTREATED,TREATED,ACCEPTED,TO_BILL' ]); $type = $request->input('type'); $state = $request->input('state'); $query = NhInfosHealthCareSheets::with(['performances.act:id,code,name', 'exams.act:id,code,name', 'prescriptions.drug_or_device:id,name'])->where('user_id', $request->input('user_id')); + if (!empty($state) && $state == 'TO_BILL') { + // Liste des feuilles de soins a afficher pour l'execution + $query = NhInfosHealthCareSheets::with(['performances.act:id,code,name', 'exams' => function ($q) { + return $q->with(['act:id,code,name'])->where('billed', 0); + }, 'prescriptions' => function ($q) { + return $q->with(['drug_or_device:id,name'])->where('billed', 0); + }])->where('user_id', $request->input('user_id')); + } + if (!empty($type)) { $query = $query->where('type', $type); } @@ -1004,7 +1024,20 @@ class HealthCareSheetController extends Controller } } - $sheets = $query->get(); + $sheets = $query->orderBy('created_at', 'DESC')->get(); + if (!empty($state) && $state == 'TO_BILL') { + // Liste des feuilles de soins a afficher pour l'execution , + // Retirer les feuilles de soins qui n'ont ni exams ou prescriptions non soldes + $notEmptySheets = []; + foreach ($sheets as $s) { + if (sizeof($s->exams) == 0 && sizeof($s->prescriptions) == 0) { + continue; + } + $notEmptySheets[] = $s; + } + $sheets = $notEmptySheets; + } + foreach ($sheets as $sheet) { $this->formalizeHealthCareSheet($sheet); } diff --git a/app/Models/NhExam.php b/app/Models/NhExam.php index 0ac9e61..0d3008f 100644 --- a/app/Models/NhExam.php +++ b/app/Models/NhExam.php @@ -19,6 +19,7 @@ use Illuminate\Database\Eloquent\Model; * @property float $unit_price * @property float $insured_paid_amount * @property float $insurer_paid_amount + * @property int $billed * @property Carbon $created_at * @property Carbon $updated_at * @@ -31,9 +32,10 @@ class NhExam extends Model protected $casts = [ 'act_id' => 'int', 'quantity' => 'int', - 'unit_price' => 'float', - 'insured_paid_amount' => 'float', - 'insurer_paid_amount' => 'float' +// 'unit_price' => 'float', +// 'insured_paid_amount' => 'float', +// 'insurer_paid_amount' => 'float', + 'billed' => 'boolean' ]; protected $fillable = [ @@ -42,7 +44,8 @@ class NhExam extends Model 'quantity', 'unit_price', 'insured_paid_amount', - 'insurer_paid_amount' + 'insurer_paid_amount', + 'billed' ]; public function act() diff --git a/app/Models/NhMedicalPrescription.php b/app/Models/NhMedicalPrescription.php index 0e4504e..49e394b 100644 --- a/app/Models/NhMedicalPrescription.php +++ b/app/Models/NhMedicalPrescription.php @@ -33,7 +33,8 @@ class NhMedicalPrescription extends Model 'quantity' => 'int', // 'unit_price' => 'float', // 'insured_paid_amount' => 'float', -// 'insurer_paid_amount' => 'float' +// 'insurer_paid_amount' => 'float', + 'billed' => 'boolean' ]; protected $fillable = [ @@ -43,6 +44,7 @@ class NhMedicalPrescription extends Model 'unit_price', 'insured_paid_amount', 'insurer_paid_amount', + 'billed', 'created_at', 'updated_at' ]; diff --git a/database/migrations/2021_12_20_094246_add_billed_to_nh_medical_prescriptions_and_nh_exams.php b/database/migrations/2021_12_20_094246_add_billed_to_nh_medical_prescriptions_and_nh_exams.php new file mode 100644 index 0000000..4433554 --- /dev/null +++ b/database/migrations/2021_12_20_094246_add_billed_to_nh_medical_prescriptions_and_nh_exams.php @@ -0,0 +1,42 @@ +boolean('billed')->default(0)->after('insurer_paid_amount') + ->comment("Pour savoir si cela a deja ete facturé dans une execution"); + }); + + Schema::table('nh_exams', function (Blueprint $table) { + $table->boolean('billed')->default(0)->after('insurer_paid_amount') + ->comment("Pour savoir si cela a deja ete facturé dans une execution"); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('nh_medical_prescriptions', function (Blueprint $table) { + $table->dropColumn('billed'); + }); + + Schema::table('nh_exams', function (Blueprint $table) { + $table->dropColumn('billed'); + }); + } +} diff --git a/resources/lang/en/errors.php b/resources/lang/en/errors.php index 62c2775..14b4100 100755 --- a/resources/lang/en/errors.php +++ b/resources/lang/en/errors.php @@ -35,5 +35,7 @@ return [ 'drug_device_already_exists' => "This drug / device code already exists", "beneficiary_not_found" => "This beneficiary does not exist", "care_sheet_already_been_processed" => "This care sheet has already been processed", - "unauthorized" => "You are not authorized to perform this operation" + "unauthorized" => "You are not authorized to perform this operation", + "prescription_already_invoiced" => "The prescription :id has already been invoiced", + "exam_already_invoiced" => "The exam:id has already been invoiced" ]; diff --git a/resources/lang/fr/errors.php b/resources/lang/fr/errors.php index 8881348..6ca9884 100755 --- a/resources/lang/fr/errors.php +++ b/resources/lang/fr/errors.php @@ -35,5 +35,7 @@ return [ 'drug_device_already_exists' => "Ce code médicament / appareillage existe deja", "beneficiary_not_found" => "Ce bénéficiaire n'existe pas", "care_sheet_already_been_processed" => "Cette feuille de soins a deja été traitée", - "unauthorized" => "Vous n'etes pas autorisé à effectuer cette operation" + "unauthorized" => "Vous n'etes pas autorisé à effectuer cette operation", + "prescription_already_invoiced" => "La prescription :id a deja été facturée", + "exam_already_invoiced" => "L'examen :id a deja été facturé" ];