erp-clinicas-backend-v2/src/modules/pharmacy/entities/dispensation.entity.ts
Adrian Flores Cortes 50a409bf17 [CL-007] feat: Implement pharmacy module
Add complete pharmacy module for erp-clinicas with:

Entities:
- Medication: catalog with controlled substance flags, categories, forms
- PharmacyInventory: stock management with batch/lot tracking, alerts
- PharmacyPrescription: linked to consultation and patient
- Dispensation: medication dispensing with partial fills support

Services:
- MedicationService: CRUD for medication catalog
- PharmacyInventoryService: stock management, low stock alerts, expiry tracking
- PrescriptionService: prescription workflow, refills, controlled substances
- DispensationService: dispensing workflow, verification, returns

Features:
- Multi-tenant support (tenantId in all queries)
- Controlled substance tracking and scheduling
- Inventory with low stock and expiration alerts
- Prescription linked to consultation and patient
- Dispensation tracking with partial fills support
- Stock reservation and dispensing workflow
- FIFO dispensing based on expiration dates

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 19:30:45 -06:00

140 lines
4.3 KiB
TypeScript

import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
Index,
ManyToOne,
JoinColumn,
} from 'typeorm';
import { PharmacyPrescription } from './prescription.entity';
export type DispensationStatus = 'pending' | 'processing' | 'completed' | 'cancelled' | 'returned';
export type DispensationType = 'full' | 'partial' | 'refill';
@Entity({ name: 'dispensations', schema: 'clinica' })
export class Dispensation {
@PrimaryGeneratedColumn('uuid')
id: string;
@Index()
@Column({ name: 'tenant_id', type: 'uuid' })
tenantId: string;
@Index()
@Column({ name: 'dispensation_number', type: 'varchar', length: 50 })
dispensationNumber: string;
@Index()
@Column({ name: 'prescription_id', type: 'uuid' })
prescriptionId: string;
@ManyToOne(() => PharmacyPrescription, (prescription) => prescription.dispensations)
@JoinColumn({ name: 'prescription_id' })
prescription: PharmacyPrescription;
@Index()
@Column({ name: 'patient_id', type: 'uuid' })
patientId: string;
@Column({ name: 'inventory_id', type: 'uuid', nullable: true })
inventoryId?: string;
@Column({ name: 'medication_id', type: 'uuid' })
medicationId: string;
@Column({ name: 'medication_name', type: 'varchar', length: 200 })
medicationName: string;
@Column({ name: 'batch_number', type: 'varchar', length: 100, nullable: true })
batchNumber?: string;
@Column({ name: 'lot_number', type: 'varchar', length: 100, nullable: true })
lotNumber?: string;
@Column({ name: 'quantity_requested', type: 'int' })
quantityRequested: number;
@Column({ name: 'quantity_dispensed', type: 'int' })
quantityDispensed: number;
@Column({ type: 'enum', enum: ['full', 'partial', 'refill'], default: 'full' })
type: DispensationType;
@Column({ type: 'enum', enum: ['pending', 'processing', 'completed', 'cancelled', 'returned'], default: 'pending' })
status: DispensationStatus;
@Column({ name: 'dispensed_by', type: 'uuid', nullable: true })
dispensedBy?: string;
@Column({ name: 'dispensed_at', type: 'timestamptz', nullable: true })
dispensedAt?: Date;
@Column({ name: 'verified_by', type: 'uuid', nullable: true })
verifiedBy?: string;
@Column({ name: 'verified_at', type: 'timestamptz', nullable: true })
verifiedAt?: Date;
@Column({ name: 'received_by', type: 'varchar', length: 200, nullable: true })
receivedBy?: string;
@Column({ name: 'receiver_relationship', type: 'varchar', length: 100, nullable: true })
receiverRelationship?: string;
@Column({ name: 'receiver_identification', type: 'varchar', length: 100, nullable: true })
receiverIdentification?: string;
@Column({ name: 'signature_captured', type: 'boolean', default: false })
signatureCaptured: boolean;
@Column({ name: 'unit_price', type: 'decimal', precision: 10, scale: 2, nullable: true })
unitPrice?: number;
@Column({ name: 'total_price', type: 'decimal', precision: 10, scale: 2, nullable: true })
totalPrice?: number;
@Column({ name: 'discount_amount', type: 'decimal', precision: 10, scale: 2, default: 0 })
discountAmount: number;
@Column({ name: 'insurance_covered', type: 'decimal', precision: 10, scale: 2, default: 0 })
insuranceCovered: number;
@Column({ name: 'patient_copay', type: 'decimal', precision: 10, scale: 2, nullable: true })
patientCopay?: number;
@Column({ type: 'text', nullable: true })
instructions?: string;
@Column({ name: 'counseling_provided', type: 'boolean', default: false })
counselingProvided: boolean;
@Column({ name: 'counseling_notes', type: 'text', nullable: true })
counselingNotes?: string;
@Column({ type: 'text', nullable: true })
notes?: string;
@Column({ name: 'cancelled_at', type: 'timestamptz', nullable: true })
cancelledAt?: Date;
@Column({ name: 'cancellation_reason', type: 'text', nullable: true })
cancellationReason?: string;
@Column({ name: 'returned_at', type: 'timestamptz', nullable: true })
returnedAt?: Date;
@Column({ name: 'return_reason', type: 'text', nullable: true })
returnReason?: string;
@Column({ name: 'return_quantity', type: 'int', nullable: true })
returnQuantity?: number;
@CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
createdAt: Date;
@UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
updatedAt: Date;
}