feat(api)!: add room-based stock to doctor packages
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import { DoctorItemPackage, ItemMaster, ItemPrice, SellingPricePercentage, User } from "entity";
|
||||
import { DoctorItemPackage, ItemMaster, ItemPrice, SellingPricePercentage, User, PharmacyInfo, RoomToPharmacy } from "entity";
|
||||
import { NextFunction, Response } from "express";
|
||||
import { Request } from "express-jwt";
|
||||
import Joi from "joi";
|
||||
@ -20,6 +20,7 @@ export class DoctorItemPackageController {
|
||||
#swagger.tags = ['Doctor Item Package']
|
||||
#swagger.security = [{ "bearerAuth": [] }]
|
||||
#swagger.parameters['doctor_id'] = { in: 'query', required: true, type: 'string' }
|
||||
#swagger.parameters['room_id'] = { in: 'query', required: true, type: 'string' }
|
||||
#swagger.parameters['filter'] = { description: '', in: 'query', type: 'string' }
|
||||
#swagger.parameters['page'] = { in: 'query', required: true, type: 'number' }
|
||||
#swagger.parameters['limit'] = { in: 'query', required: true, type: 'number' }
|
||||
@ -29,6 +30,7 @@ export class DoctorItemPackageController {
|
||||
try {
|
||||
const schema = Joi.object().keys({
|
||||
doctor_id: Joi.string().uuid().required().label("Doctor ID"),
|
||||
room_id: Joi.string().uuid().required().label("Room ID"),
|
||||
filter: Joi.string().allow("").optional().label("Filter"),
|
||||
page: Joi.number().required().min(1).label("Page"),
|
||||
limit: Joi.number().required().min(1).label("Limit"),
|
||||
@ -52,6 +54,29 @@ export class DoctorItemPackageController {
|
||||
|
||||
const offset = (param.page - 1) * param.limit;
|
||||
|
||||
const pharmacyInfoRow = await OrmHelper.DB
|
||||
.getRepository(PharmacyInfo)
|
||||
.createQueryBuilder("pharmacy_info")
|
||||
.select("pharmacy_info.default_room_id", "default_room_id")
|
||||
.where("pharmacy_info.deleted_at IS NULL")
|
||||
.orderBy("pharmacy_info.created_at", "DESC")
|
||||
.limit(1)
|
||||
.getRawOne<{ default_room_id: string | null }>();
|
||||
|
||||
const defaultRoomId = pharmacyInfoRow?.default_room_id ?? null;
|
||||
|
||||
const roomToPharmacyRow = await OrmHelper.DB
|
||||
.getRepository(RoomToPharmacy)
|
||||
.createQueryBuilder("room_to_pharmacy")
|
||||
.select("room_to_pharmacy.pharmacy_room_id", "pharmacy_room_id")
|
||||
.where("room_to_pharmacy.deleted_at IS NULL")
|
||||
.andWhere("room_to_pharmacy.room_id = :roomId", { roomId: param.room_id })
|
||||
.orderBy("room_to_pharmacy.created_at", "DESC")
|
||||
.limit(1)
|
||||
.getRawOne<{ pharmacy_room_id: string | null }>();
|
||||
|
||||
const pharmacyRoomId = roomToPharmacyRow?.pharmacy_room_id ?? null;
|
||||
|
||||
const current_page = param.page;
|
||||
const total_count_data = await query.getCount();
|
||||
|
||||
@ -75,7 +100,7 @@ export class DoctorItemPackageController {
|
||||
const packageIds = packages.map((p: any) => p.id);
|
||||
|
||||
// Query 2: all items for these packages (build items[] per package)
|
||||
const itemsRaw = await OrmHelper.DB.getRepository(DoctorItemPackage)
|
||||
let itemsQuery = OrmHelper.DB.getRepository(DoctorItemPackage)
|
||||
.createQueryBuilder("DoctorItemPackage")
|
||||
.select([
|
||||
"DoctorItemPackage.id as package_id",
|
||||
@ -83,6 +108,12 @@ export class DoctorItemPackageController {
|
||||
"ItemMaster.item_name as item_name",
|
||||
"GenericName.generic_name as item_generic_name",
|
||||
"item_type.type_detail_name as item_type",
|
||||
(pharmacyRoomId
|
||||
? `COALESCE((SELECT SUM(rs.total_stock)::text FROM pharmacy_logistic.room_stock rs WHERE rs."roomId" = :pharmacyRoomId AND rs."itemmasterId" = ItemMaster.id AND rs.deleted_at IS NULL), '0') as pharmacy_stock`
|
||||
: "'0' as pharmacy_stock"),
|
||||
(defaultRoomId
|
||||
? `COALESCE((SELECT SUM(rs.total_stock)::text FROM pharmacy_logistic.room_stock rs WHERE rs."roomId" = :defaultRoomId AND rs."itemmasterId" = ItemMaster.id AND rs.deleted_at IS NULL), '0') as storage_stock`
|
||||
: "'0' as storage_stock"),
|
||||
"ROUND(CASE WHEN selling_price_percentage.percentage IS NULL " +
|
||||
"THEN COALESCE(item_price.selling_price, 0) " +
|
||||
"ELSE COALESCE(item_price.selling_price, 0) + (COALESCE(item_price.selling_price, 0) * COALESCE(selling_price_percentage.percentage, 0) / 100) END, 2) as price",
|
||||
@ -92,8 +123,16 @@ export class DoctorItemPackageController {
|
||||
.leftJoin("ItemMaster.type_detail", "item_type")
|
||||
.leftJoin(ItemPrice, "item_price", "item_price.item_master_id = ItemMaster.id")
|
||||
.leftJoin(SellingPricePercentage, "selling_price_percentage", "selling_price_percentage.item_master_id = ItemMaster.id")
|
||||
.where("DoctorItemPackage.id IN (:...ids)", { ids: packageIds })
|
||||
.getRawMany();
|
||||
.where("DoctorItemPackage.id IN (:...ids)", { ids: packageIds });
|
||||
|
||||
if (defaultRoomId) {
|
||||
itemsQuery = itemsQuery.setParameter("defaultRoomId", defaultRoomId);
|
||||
}
|
||||
if (pharmacyRoomId) {
|
||||
itemsQuery = itemsQuery.setParameter("pharmacyRoomId", pharmacyRoomId);
|
||||
}
|
||||
|
||||
const itemsRaw = await itemsQuery.getRawMany();
|
||||
|
||||
const itemsByPackageId = new Map<string, any[]>();
|
||||
for (const row of itemsRaw) {
|
||||
@ -105,6 +144,8 @@ export class DoctorItemPackageController {
|
||||
item_name: row["item_name"],
|
||||
item_generic_name: row["item_generic_name"],
|
||||
item_type: row["item_type"],
|
||||
pharmacy_stock: row["pharmacy_stock"],
|
||||
storage_stock: row["storage_stock"],
|
||||
price: row["price"],
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user