UNPKG

gcp-nrces-fhir

Version:

Google cloud healthcare api NRCES FHIR implimenataion

186 lines 9.06 kB
"use strict"; /** * FHIR Invoice resource class implementing the NRCES Invoice profile * https://nrces.in/ndhm/fhir/r4/StructureDefinition/Invoice */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Invoice = exports.PRICE_COMPONENT_CODES = exports.INVOICE_TYPE_CODES = void 0; const gcp_1 = __importDefault(require("../classess/gcp")); // ABDM Invoice type codes from ValueSet ndhm-invoice-types exports.INVOICE_TYPE_CODES = { CONSULTATION: { code: "00", display: "Consultation", system: "https://nrces.in/ndhm/fhir/r4/CodeSystem/ndhm-billing-codes" }, PHARMACY: { code: "01", display: "Pharmacy", system: "https://nrces.in/ndhm/fhir/r4/CodeSystem/ndhm-billing-codes" }, IPD: { code: "02", display: "IPD", system: "https://nrces.in/ndhm/fhir/r4/CodeSystem/ndhm-billing-codes" }, OPD: { code: "03", display: "OPD", system: "https://nrces.in/ndhm/fhir/r4/CodeSystem/ndhm-billing-codes" }, OTHERS: { code: "99", display: "Others", system: "https://nrces.in/ndhm/fhir/r4/CodeSystem/ndhm-billing-codes" }, }; // ABDM Price Component codes from ValueSet ndhm-price-components exports.PRICE_COMPONENT_CODES = { MRP: { code: "00", display: "MRP", system: "https://nrces.in/ndhm/fhir/r4/CodeSystem/ndhm-price-components" }, RATE: { code: "01", display: "Rate", system: "https://nrces.in/ndhm/fhir/r4/CodeSystem/ndhm-price-components" }, DISCOUNT: { code: "02", display: "Discount", system: "https://nrces.in/ndhm/fhir/r4/CodeSystem/ndhm-price-components" }, CGST: { code: "03", display: "CGST", system: "https://nrces.in/ndhm/fhir/r4/CodeSystem/ndhm-price-components" }, SGST: { code: "04", display: "SGST", system: "https://nrces.in/ndhm/fhir/r4/CodeSystem/ndhm-price-components" }, }; class Invoice { /** * Map internal voucher type to ABDM Invoice type coding */ static mapVoucherTypeToInvoiceType(voucherType) { if (voucherType.startsWith("INVOICE_P")) { return { coding: [exports.INVOICE_TYPE_CODES.PHARMACY], text: "Pharmacy" }; } if (voucherType.startsWith("INVOICE")) { return { coding: [exports.INVOICE_TYPE_CODES.OPD], text: "Consultation" }; } if (voucherType.startsWith("CREDIT_NOTE")) { return { coding: [exports.INVOICE_TYPE_CODES.OTHERS], text: "Credit Note" }; } if (voucherType === "RECEIPT") { return { coding: [exports.INVOICE_TYPE_CODES.OTHERS], text: "Payment Receipt" }; } if (voucherType === "PAYMENT") { return { coding: [exports.INVOICE_TYPE_CODES.OTHERS], text: "Payment Refund" }; } return { coding: [exports.INVOICE_TYPE_CODES.OTHERS], text: "Others" }; } /** * Build a price component for a line item */ static buildPriceComponent(type, code, amount, currency = "INR") { return { type, code: { coding: [{ system: code.system, code: code.code, display: code.display }] }, amount: { value: parseFloat(amount.toFixed(2)), currency }, }; } /** * Build price components array for a single line item from GST item data */ static buildLineItemPriceComponents(quantity, salePrice, discountPercent = 0, cgst = 0, sgst = 0) { const baseAmount = quantity * salePrice; const discountAmount = baseAmount * (discountPercent / 100); const components = []; components.push(Invoice.buildPriceComponent("base", exports.PRICE_COMPONENT_CODES.RATE, baseAmount)); if (discountAmount > 0) { components.push(Invoice.buildPriceComponent("discount", exports.PRICE_COMPONENT_CODES.DISCOUNT, -discountAmount)); } if (cgst > 0) { components.push(Invoice.buildPriceComponent("tax", exports.PRICE_COMPONENT_CODES.CGST, cgst)); } if (sgst > 0) { components.push(Invoice.buildPriceComponent("tax", exports.PRICE_COMPONENT_CODES.SGST, sgst)); } return components; } /** * Calculate net total from line items (base - discounts; taxes are added separately in totalGross) */ static calculateNetTotal(lineItems) { return lineItems.reduce((total, item) => { const itemTotal = item.priceComponent.reduce((sum, pc) => { if (pc.type === "base" || pc.type === "surcharge") return sum + pc.amount.value; if (pc.type === "discount" || pc.type === "deduction") return sum + pc.amount.value; return sum; }, 0); return total + itemTotal; }, 0); } /** * Calculate gross total from line items (base - discounts + taxes) */ static calculateGrossTotal(lineItems) { return lineItems.reduce((total, item) => { const itemTotal = item.priceComponent.reduce((sum, pc) => sum + pc.amount.value, 0); return total + itemTotal; }, 0); } /** * Generate the FHIR Invoice resource JSON */ static toFhir(options) { const body = { resourceType: "Invoice", id: options.id || undefined, meta: { profile: ["https://nrces.in/ndhm/fhir/r4/StructureDefinition/Invoice"], lastUpdated: new Date().toISOString(), }, identifier: [{ system: options.identifier.system || "https://www.nicehms.com/invoice", value: options.identifier.value, }], status: options.status, type: options.type, subject: options.subject, date: options.date, lineItem: options.lineItem.map((item, idx) => ({ sequence: item.sequence || idx + 1, chargeItemReference: { reference: item.chargeItemReference, type: "ChargeItem" }, priceComponent: item.priceComponent, })), totalNet: { value: options.totalNet.value, currency: options.totalNet.currency || "INR" }, totalGross: { value: options.totalGross.value, currency: options.totalGross.currency || "INR" }, }; if (options.recipient) body.recipient = options.recipient; if (options.issuer) body.issuer = options.issuer; if (options.participant) body.participant = options.participant; if (options.account) body.account = options.account; if (options.paymentTerms) body.paymentTerms = options.paymentTerms; if (options.note) body.note = options.note; if (options.cancelledReason) body.cancelledReason = options.cancelledReason; return body; } /** * Store an Invoice resource in GCP FHIR store */ static create(options, creds, dbPath) { return __awaiter(this, void 0, void 0, function* () { const fhirBody = Invoice.toFhir(options); const gcpFhirCrud = creds ? new gcp_1.default(creds, dbPath) : new gcp_1.default(); return yield gcpFhirCrud.createFhirResource(fhirBody, "Invoice"); }); } /** * Update an existing Invoice resource */ static update(id, options, creds, dbPath) { return __awaiter(this, void 0, void 0, function* () { const fhirBody = Invoice.toFhir(Object.assign(Object.assign({}, options), { id })); const gcpFhirCrud = creds ? new gcp_1.default(creds, dbPath) : new gcp_1.default(); return yield gcpFhirCrud.updateFhirResource(fhirBody, id, "Invoice"); }); } /** * Get Invoice resource from GCP FHIR store */ static get(id, creds, dbPath) { return __awaiter(this, void 0, void 0, function* () { const gcpFhirCrud = creds ? new gcp_1.default(creds, dbPath) : new gcp_1.default(); return yield gcpFhirCrud.getFhirResource(id, "Invoice"); }); } } exports.Invoice = Invoice; //# sourceMappingURL=Invoice.js.map