feat: add stock keeping units management

- Created migration to drop `type` column from `stock_keeping_units` table.
- Added `Guild` model to Prisma schema.
- Implemented `BusinessActivitiesQueryService` for querying business activities.
- Developed `GoodsSharedService` for handling goods creation and updates.
- Introduced DTOs for creating and updating stock keeping units.
- Created controller and service for managing stock keeping units.
- Implemented response DTOs for stock keeping units service.
- Established module for stock keeping units management.
This commit is contained in:
2026-05-07 20:30:24 +03:30
parent 658496320b
commit fbe7230865
77 changed files with 2065 additions and 1846 deletions
@@ -1,3 +1,4 @@
import { TspProviderCustomerType } from '@/common/enums/enums'
import {
SharedCreateSalesInvoiceItemDto,
SharedCreateSalesInvoicePaymentsDto,
@@ -6,7 +7,6 @@ import {
CustomerType,
InvoiceTemplateType,
PaymentMethodType,
TspProviderCustomerType,
TspProviderRequestType,
TspProviderResponseStatus,
TspProviderType,
@@ -1,10 +1,10 @@
import { SharedSaleInvoiceCreateService } from '@/common/services/saleInvoices/sale-invoice-create.service'
import { PrismaService } from '@/prisma/prisma.service'
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'
import { TspProviderCustomerType } from 'common/enums/enums'
import {
CustomerType,
Prisma,
TspProviderCustomerType,
TspProviderRequestType,
TspProviderResponseStatus,
} from 'generated/prisma/client'
@@ -12,7 +12,6 @@ import {
TspProviderActionResponseDto,
TspProviderCorrectionInvoicePayloadDto,
TspProviderCorrectionSendPayloadDto,
TspProviderCorrectionSendResponseDto,
TspProviderOriginalSendPayloadDto,
TspProviderRevokePayloadDto,
TspProviderSendItemResultDto,
@@ -37,9 +36,12 @@ export class SalesInvoiceTspService {
invoice_id: string,
): Promise<TspProviderActionResponseDto> {
const payload = await this.buildPayload(invoice_id, posId)
const attemptNumber = await this.getOriginalResendAttemptNumber(invoice_id)
const attempt = await this.prisma.saleInvoiceTspAttempts.create({
data: {
attempt_no: 1,
attempt_no: attemptNumber,
invoice_id,
status: TspProviderResponseStatus.QUEUED,
request_payload: JSON.parse(JSON.stringify(payload)),
@@ -49,7 +51,7 @@ export class SalesInvoiceTspService {
const result = await this.trySend(payload)
return await this.onCreateCorrectionResult(result, attempt.id)
return await this.onResult(result, attempt.id)
}
async sendBulk(consumer_id: string, invoice_ids: string[]): Promise<void> {
@@ -259,7 +261,7 @@ export class SalesInvoiceTspService {
console.log('sendResult')
console.log(result)
return this.onCreateCorrectionResult(result, attempt.id)
return this.onResult(result, attempt.id)
// const countByGoodId = (goodIds: string[]): Map<string, number> => {
// const counts = new Map<string, number>()
@@ -444,83 +446,46 @@ export class SalesInvoiceTspService {
const result = await this.tspSwitchService.revoke(revokePayload)
return await this.onCreateCorrectionResult(result, attempt.id)
return await this.onResult(result, attempt.id)
}
private async getPosId(
consumer_account_id: string,
invoice_id: string,
): Promise<string> {
const now = new Date()
console.log(consumer_account_id, invoice_id)
const saleInvoice = await this.prisma.salesInvoice.findUnique({
private async getOriginalResendAttemptNumber(invoice_id: string): Promise<number> {
let attemptNumber = 1
const existingAttempt = await this.prisma.saleInvoiceTspAttempts.findFirst({
where: {
id: invoice_id,
pos: {
complex: {
business_activity: {
consumer: {
accounts: {
some: {
id: consumer_account_id,
},
},
},
},
},
},
invoice_id,
},
select: {
pos: {
include: {
invoice: {
select: {
id: true,
complex: {
select: {
business_activity: {
select: {
name: true,
license_activation: {
select: {
expires_at: true,
license_renews: {
select: {
expires_at: true,
},
},
},
},
},
},
},
},
type: true,
},
},
},
})
if (!saleInvoice) {
throw new NotFoundException('متاسفانه فاکتور مورد نظر یافت نشد.')
}
if (existingAttempt) {
attemptNumber = existingAttempt.attempt_no + 1
if (existingAttempt.invoice.type !== TspProviderRequestType.ORIGINAL) {
throw new BadRequestException(
'فقط فاکتورهای اصلی قابل ارسال مجدد به سامانه مالیاتی هستند.',
)
}
const { license_activation, name: businessName } =
saleInvoice.pos.complex.business_activity
let expired = !license_activation || false
if (license_activation) {
const { expires_at, license_renews } = license_activation
if (expires_at < now) {
for (const renew of license_renews) {
if (renew.expires_at > now) {
expired = false
break
}
}
if (existingAttempt.status === TspProviderResponseStatus.SUCCESS) {
throw new BadRequestException(
'فاکتور تایید شده از طرف سازمان مالیاتی قابل ارسال مجدد نیست.',
)
}
if (existingAttempt.status === TspProviderResponseStatus.QUEUED) {
throw new BadRequestException(
'در حال حاضر فاکتور شما در حال بررسی توسط سازمان مالیاتی است.',
)
}
}
if (expired) {
throw new NotFoundException(`متاسفانه مجوز ${businessName} منقضی شده است.`)
}
return saleInvoice.pos.id
return attemptNumber
}
private async buildRevokePayload(
@@ -732,8 +697,8 @@ export class SalesInvoiceTspService {
invoice_date: new Date(),
customer_type: invoice.customer?.type
? TspProviderCustomerType.Known
: TspProviderCustomerType.Unknown,
? TspProviderCustomerType.KNOWN
: TspProviderCustomerType.UNKNOWN,
customer: invoice.customer?.type
? {
type: invoice.customer.type,
@@ -881,8 +846,8 @@ export class SalesInvoiceTspService {
type: TspProviderRequestType.ORIGINAL,
tsp_provider: partner.tsp_provider!,
customer_type: invoice.customer?.type
? TspProviderCustomerType.Known
: TspProviderCustomerType.Unknown,
? TspProviderCustomerType.KNOWN
: TspProviderCustomerType.UNKNOWN,
customer: invoice.customer?.type
? {
type: invoice.customer.type,
@@ -907,14 +872,6 @@ export class SalesInvoiceTspService {
}
}
private async tryCorrection(
payload: TspProviderCorrectionSendPayloadDto,
): Promise<TspProviderCorrectionSendResponseDto | null> {
const taxResult = await this.tspSwitchService.correction(payload)
return taxResult || null
}
private async trySend(
payload: TspProviderOriginalSendPayloadDto,
): Promise<TspProviderSendItemResultDto | null> {
@@ -923,7 +880,7 @@ export class SalesInvoiceTspService {
return taxResult || null
}
private async onCreateCorrectionResult(
private async onResult(
result: any,
attempt_id: string,
): Promise<TspProviderActionResponseDto> {
@@ -960,7 +917,8 @@ export class SalesInvoiceTspService {
status: result.status,
sent_at: result.sent_at,
received_at: result.received_at,
message: 'ارسال با موفقیت انجام شد.',
message:
result.message?.toString() || 'فاکتور با موفقیت به سامانه مالیاتی ارسال شد.',
},
select: {
status: true,
@@ -981,5 +939,3 @@ export class SalesInvoiceTspService {
}
}
}
// {"type": "ORIGINAL", "items": [{"sku": "2720000044726", "good_id": "01KQHY48M299C9BG2D06DMWK4E", "payload": {"karat": "18", "wages": 40000000, "profit": 48000000, "commission": 40000000}, "sku_vat": "0", "discount": "0", "quantity": 2, "service_id": null, "unit_price": 200000000, "measure_unit": "63", "total_amount": 540800000, "good_snapshot": {"good": {"id": "01KQHY48M299C9BG2D06DMWK4E", "sku": {"id": "01KQHXE2D4XEFSDK4DSV2J6GT2", "VAT": "0", "code": "2720000044726", "name": "گوشواره طلا زیورالات ساخته شده از طلا"}, "name": "گوشواره", "barcode": null, "category": {"id": "01KQHY48CF6X2TK2AG09MKDVKQ", "name": "زیورآلات"}, "image_url": null, "local_sku": null, "measure_unit": {"id": "01KQHY489HE02787SE8TWDE18S", "code": "63", "name": "گرم"}, "pricing_model": "GOLD", "base_sale_price": "0"}}, "invoice_item_id": "01KQMZTMDC4M4T7VE9GFZJEW4E"}], "payments": [{"amount": 540800000, "paid_at": "2026-05-02T18:39:46.000Z", "payment_method": "CASH"}], "fiscal_id": "A296T3", "tsp_token": "pfFE6QxOJxUIhzkpUCPWYMR5", "invoice_id": "98c3377d-4e42-4703-97f0-38c3b3c8e87f", "invoice_date": "2026-05-02T18:39:46.000Z", "total_amount": 540800000, "tsp_provider": "NAMA", "customer_type": "Unknown", "economic_code": "14003579468", "invoice_number": 13, "invoice_template": "GOLD_JEWELRY"}
@@ -1,8 +1,8 @@
import { TspProviderCustomerType } from '@/common/enums/enums'
import {
CustomerType,
InvoiceTemplateType,
PaymentMethodType,
TspProviderCustomerType,
TspProviderRequestType,
TspProviderResponseStatus,
} from '@/generated/prisma/enums'
@@ -23,6 +23,8 @@ import {
export class NamaProviderUtils {
mapResponseStatus(status: NamaProviderResponseStatus): TspProviderResponseStatus {
console.log('NAMA status', status)
switch (status.toUpperCase()) {
case NamaProviderResponseStatus.SUCCESS:
case NamaProviderResponseStatus.POSTED:
@@ -161,9 +163,9 @@ export class NamaProviderUtils {
private mapTspProviderCustomerType(type?: TspProviderCustomerType) {
switch (type) {
case TspProviderCustomerType.Known:
case TspProviderCustomerType.KNOWN:
return '1'
case TspProviderCustomerType.Unknown:
case TspProviderCustomerType.UNKNOWN:
return '2'
default:
return '3'