feat: add DTOs and services for tax switch integration
- Created SendBulkSaleInvoicesDto for handling bulk sale invoice requests. - Implemented TaxSwitchSendPayloadDto and related DTOs for tax switch item payloads and results. - Developed SalesInvoiceTaxSwitchService to manage tax switch operations, including sending and retrieving tax information. - Added SalesInvoiceTaxService for handling sales invoice tax logic, including bulk sending and persistence of results. - Introduced NamaTaxSwitchAdapter to interact with the tax switch service, simulating external API responses. - Created SendBulkSalesInvoicesDto for POS module to handle bulk sales invoice requests.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import {
|
||||
ArrayMinSize,
|
||||
IsBoolean,
|
||||
IsDateString,
|
||||
IsEnum,
|
||||
IsNumber,
|
||||
@@ -42,6 +43,11 @@ export class CreateSalesInvoiceDto {
|
||||
@IsString()
|
||||
notes?: string
|
||||
|
||||
@ApiProperty({ required: false, default: false })
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
send_to_tax?: boolean
|
||||
|
||||
@ApiProperty({ required: true, default: CustomerType.UNKNOWN })
|
||||
@IsEnum(CustomerType)
|
||||
customer_type: CustomerType
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { ArrayMinSize, IsArray, IsString } from 'class-validator'
|
||||
|
||||
export class SendBulkSalesInvoicesDto {
|
||||
@ApiProperty({ type: [String], minItems: 1 })
|
||||
@IsArray()
|
||||
@ArrayMinSize(1)
|
||||
@IsString({ each: true })
|
||||
invoice_ids: string[]
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
|
||||
import { PosInfo } from '@/common/decorators/posInfo.decorator'
|
||||
import type { IPosPayload } from '@/common/models/posPayload.model'
|
||||
import { CreateSalesInvoiceDto } from './dto/create-sales-invoice.dto'
|
||||
import { SalesInvoicesService } from './sales-invoices.service'
|
||||
import type { IPosPayload } from '@/common/models/posPayload.model'
|
||||
|
||||
@Controller('pos/sales_invoices')
|
||||
export class SalesInvoicesController {
|
||||
@@ -23,4 +23,14 @@ export class SalesInvoicesController {
|
||||
create(@Body() data: CreateSalesInvoiceDto, @PosInfo() posInfo: IPosPayload) {
|
||||
return this.salesInvoicesService.create(data, posInfo)
|
||||
}
|
||||
|
||||
// @Post(':id/send')
|
||||
// send(@Param('id') id: string, @PosInfo() posInfo: IPosPayload) {
|
||||
// return this.salesInvoicesService.send(id, posInfo)
|
||||
// }
|
||||
|
||||
// @Post('send/bulk')
|
||||
// sendBulk(@Body() data: SendBulkSalesInvoicesDto, @PosInfo() posInfo: IPosPayload) {
|
||||
// return this.salesInvoicesService.sendBulk(data.invoice_ids, posInfo)
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { SalesInvoiceTaxSwitchService } from '../../consumer/saleInvoices/tax/sales-invoice-tax-switch.service'
|
||||
import { SalesInvoiceTaxService } from '../../consumer/saleInvoices/tax/sales-invoice-tax.service'
|
||||
import { NamaTaxSwitchAdapter } from '../../consumer/saleInvoices/tax/switch/nama-tax-switch.adapter'
|
||||
import { SalesInvoicesController } from './sales-invoices.controller'
|
||||
import { SalesInvoicesService } from './sales-invoices.service'
|
||||
|
||||
@Module({
|
||||
controllers: [SalesInvoicesController],
|
||||
providers: [SalesInvoicesService],
|
||||
providers: [
|
||||
SalesInvoicesService,
|
||||
SalesInvoiceTaxService,
|
||||
SalesInvoiceTaxSwitchService,
|
||||
NamaTaxSwitchAdapter,
|
||||
],
|
||||
})
|
||||
export class PosSalesInvoicesModule {}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { IPosPayload } from '@/common/models/posPayload.model'
|
||||
import { SalesInvoiceCreateInput } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { BadRequestException, Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import type { CustomerIndividual, CustomerLegal } from 'generated/prisma/client'
|
||||
import { CustomerType, PaymentMethodType } from 'generated/prisma/enums'
|
||||
import { SalesInvoiceTaxService } from '../../consumer/saleInvoices/tax/sales-invoice-tax.service'
|
||||
import { CreateSalesInvoiceDto } from './dto/create-sales-invoice.dto'
|
||||
|
||||
// Define type guard for CustomerIndividual
|
||||
@@ -27,7 +27,10 @@ function isCustomerLegal(customer: any): customer is CustomerLegal {
|
||||
|
||||
@Injectable()
|
||||
export class SalesInvoicesService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private salesInvoiceTaxService: SalesInvoiceTaxService,
|
||||
) {}
|
||||
|
||||
findAll() {
|
||||
// TODO: Implement fetching all sales invoices
|
||||
@@ -39,6 +42,28 @@ export class SalesInvoicesService {
|
||||
return {}
|
||||
}
|
||||
|
||||
// async send(invoiceId: string, posInfo: IPosPayload) {
|
||||
// await this.salesInvoiceTaxService.send(invoiceId, posInfo.pos_id)
|
||||
|
||||
// return ResponseMapper.single({
|
||||
// invoice_id: invoiceId,
|
||||
// sent_to_tax: true,
|
||||
// })
|
||||
// }
|
||||
|
||||
// async sendBulk(invoiceIds: string[], posInfo: IPosPayload) {
|
||||
// if (!invoiceIds.length) {
|
||||
// throw new BadRequestException('invoice_ids is required and cannot be empty.')
|
||||
// }
|
||||
|
||||
// await this.salesInvoiceTaxService.sendBulk(invoiceIds, posInfo.pos_id)
|
||||
|
||||
// return ResponseMapper.single({
|
||||
// invoice_ids: invoiceIds,
|
||||
// sent_to_tax: true,
|
||||
// })
|
||||
// }
|
||||
|
||||
async create(data: CreateSalesInvoiceDto, posInfo: IPosPayload) {
|
||||
data.invoice_date = new Date(data.invoice_date).toISOString() as any
|
||||
|
||||
@@ -135,7 +160,40 @@ export class SalesInvoicesService {
|
||||
} else if (data.customer_type === CustomerType.UNKNOWN) {
|
||||
}
|
||||
|
||||
const salesInvoiceData: SalesInvoiceCreateInput = {
|
||||
const itemGoodIds = data.items
|
||||
.map(item => item.good_id)
|
||||
.filter((goodId): goodId is string => Boolean(goodId))
|
||||
|
||||
const goods = itemGoodIds.length
|
||||
? await $tx.good.findMany({
|
||||
where: {
|
||||
id: {
|
||||
in: itemGoodIds,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
sku: true,
|
||||
local_sku: true,
|
||||
barcode: true,
|
||||
pricing_model: true,
|
||||
unit_type: true,
|
||||
base_sale_price: true,
|
||||
image_url: true,
|
||||
category: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
: []
|
||||
|
||||
const goodsById = new Map(goods.map(good => [good.id, good]))
|
||||
|
||||
const salesInvoiceData: any = {
|
||||
...invoiceData,
|
||||
total_amount: data.total_amount,
|
||||
code: 'INV-' + Date.now(),
|
||||
@@ -150,6 +208,13 @@ export class SalesInvoicesService {
|
||||
payload: item.payload
|
||||
? JSON.parse(JSON.stringify(item.payload))
|
||||
: undefined,
|
||||
good_snapshot: item.good_id
|
||||
? JSON.parse(
|
||||
JSON.stringify({
|
||||
good: goodsById.get(item.good_id) || null,
|
||||
}),
|
||||
)
|
||||
: undefined,
|
||||
unit_type: item.unit_type,
|
||||
// pricing_model: item.pricingModel,
|
||||
})),
|
||||
@@ -192,6 +257,10 @@ export class SalesInvoicesService {
|
||||
|
||||
return salesInvoice
|
||||
})
|
||||
if (data.send_to_tax) {
|
||||
await this.salesInvoiceTaxService.send(salesInvoice.id, pos_id)
|
||||
}
|
||||
|
||||
return ResponseMapper.create(salesInvoice)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user