fix: bill
This commit is contained in:
@@ -2,10 +2,13 @@ import { EntityManager } from "@mikro-orm/postgresql";
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import dayjs from "dayjs";
|
||||
import Decimal from "decimal.js";
|
||||
import { Workbook } from "exceljs";
|
||||
|
||||
import { IFile } from "../utils/interfaces/IFile";
|
||||
|
||||
import { BillListQueryDto } from "./dto/bill-list-query.dto";
|
||||
import { CreateChargeBillDto } from "./dto/create-charge-bill.dto";
|
||||
import { CreateWaterBillDto } from "./dto/create-water-bill.dto";
|
||||
import { CreateWaterBillDto, CreateWaterBillFromExcelDto } from "./dto/create-water-bill.dto";
|
||||
import { Bill } from "./entities/bill.entity";
|
||||
import { BillType } from "./enums/bill-type.enum";
|
||||
import { BillsRepository } from "./repositories/bill.repository";
|
||||
@@ -81,6 +84,35 @@ export class BillsService {
|
||||
}
|
||||
}
|
||||
|
||||
async createWaterBillByExcel(file: IFile, dto: CreateWaterBillFromExcelDto, business: Business) {
|
||||
const values = await this.parseWaterBillExcel(file.buffer);
|
||||
if (values.length === 0) {
|
||||
throw new BadRequestException("Excel file contains no valid data rows");
|
||||
}
|
||||
return this.createWaterBill({ ...dto, values }, business);
|
||||
}
|
||||
|
||||
private async parseWaterBillExcel(buffer: Buffer): Promise<{ companyId: string; waterUsage: number }[]> {
|
||||
const workbook = new Workbook();
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
await workbook.xlsx.load(buffer as any);
|
||||
const worksheet = workbook.worksheets[0];
|
||||
if (!worksheet) {
|
||||
return [];
|
||||
}
|
||||
const values: { companyId: string; waterUsage: number }[] = [];
|
||||
worksheet.eachRow((row) => {
|
||||
const companyIdRaw = row.getCell(1).value;
|
||||
const waterUsageRaw = row.getCell(3).value;
|
||||
const companyId = companyIdRaw != null ? String(companyIdRaw).trim() : "";
|
||||
const waterUsage = typeof waterUsageRaw === "number" ? waterUsageRaw : Number(waterUsageRaw);
|
||||
if (companyId && !Number.isNaN(waterUsage) && waterUsage >= 0) {
|
||||
values.push({ companyId, waterUsage });
|
||||
}
|
||||
});
|
||||
return values;
|
||||
}
|
||||
|
||||
async createChargeBill(dto: CreateChargeBillDto, business: Business) {
|
||||
const { chargeRate, dueDays } = dto;
|
||||
const em = this.em.fork();
|
||||
|
||||
Reference in New Issue
Block a user