update pos consumer module
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { IPosPayload } from '@/common/models/posPayload.model'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
@@ -7,12 +8,6 @@ import { CreateSalesInvoiceDto } from './dto/create-sales-invoice.dto'
|
||||
|
||||
// Define type guard for CustomerIndividual
|
||||
function isCustomerIndividual(customer: any): customer is CustomerIndividual {
|
||||
console.log(
|
||||
customer &&
|
||||
typeof customer.first_name === 'string' &&
|
||||
typeof customer.last_name === 'string',
|
||||
)
|
||||
|
||||
return (
|
||||
customer &&
|
||||
typeof customer.first_name === 'string' &&
|
||||
@@ -43,9 +38,11 @@ export class SalesInvoicesService {
|
||||
return {}
|
||||
}
|
||||
|
||||
async create(data: CreateSalesInvoiceDto, account) {
|
||||
async create(data: CreateSalesInvoiceDto, posInfo: IPosPayload) {
|
||||
data.invoice_date = new Date(data.invoice_date).toISOString() as any
|
||||
|
||||
const { complex_id, pos_id, consumer_account_id } = posInfo
|
||||
|
||||
const salesInvoice = await this.prisma.$transaction(async $tx => {
|
||||
const payments = Object.entries(data.payments)
|
||||
.filter(([_, value]) => value > 0 && PaymentMethodType[_.toUpperCase()])
|
||||
@@ -58,6 +55,7 @@ export class SalesInvoicesService {
|
||||
})
|
||||
|
||||
const totalPayments = payments.reduce((sum, payment) => sum + payment.amount, 0)
|
||||
|
||||
if (totalPayments !== data.total_amount) {
|
||||
throw new Error('مبلغ پرداختی باید برابر با مبلغ کل فاکتور باشد.')
|
||||
}
|
||||
@@ -72,7 +70,7 @@ export class SalesInvoicesService {
|
||||
let customerIndividual = await $tx.customerIndividual.findUnique({
|
||||
where: {
|
||||
complex_id_national_id: {
|
||||
complex_id: account.complex_id,
|
||||
complex_id,
|
||||
national_id: customer.national_id,
|
||||
},
|
||||
},
|
||||
@@ -82,7 +80,7 @@ export class SalesInvoicesService {
|
||||
await $tx.customerIndividual.update({
|
||||
where: {
|
||||
complex_id_national_id: {
|
||||
complex_id: account.complex_id,
|
||||
complex_id,
|
||||
national_id: customer.national_id,
|
||||
},
|
||||
},
|
||||
@@ -99,8 +97,7 @@ export class SalesInvoicesService {
|
||||
const createdCustomer = await $tx.customer.create({
|
||||
data: {
|
||||
type: CustomerType.INDIVIDUAL,
|
||||
complex_id: account.complex_id,
|
||||
customerIndividuals: {
|
||||
customer_individuals: {
|
||||
connect: {
|
||||
customer_id: customerIndividual.customer_id,
|
||||
},
|
||||
@@ -116,7 +113,7 @@ export class SalesInvoicesService {
|
||||
let customerLegal = await $tx.customerLegal.findUnique({
|
||||
where: {
|
||||
complex_id_registration_number: {
|
||||
complex_id: account.complex_id,
|
||||
complex_id,
|
||||
registration_number: customer.registration_number,
|
||||
},
|
||||
},
|
||||
@@ -126,7 +123,7 @@ export class SalesInvoicesService {
|
||||
await $tx.customerLegal.update({
|
||||
where: {
|
||||
complex_id_registration_number: {
|
||||
complex_id: account.complex_id,
|
||||
complex_id,
|
||||
registration_number: customer.registration_number,
|
||||
},
|
||||
},
|
||||
@@ -143,8 +140,8 @@ export class SalesInvoicesService {
|
||||
const createdCustomer = await $tx.customer.create({
|
||||
data: {
|
||||
type: CustomerType.LEGAL,
|
||||
complex_id: account.complex_id,
|
||||
customerIndividuals: {
|
||||
|
||||
customer_individuals: {
|
||||
connect: {
|
||||
customer_id: customerLegal.customer_id,
|
||||
},
|
||||
@@ -156,16 +153,15 @@ export class SalesInvoicesService {
|
||||
newCustomerId = createdCustomer.id
|
||||
}
|
||||
}
|
||||
} else if (data.customer_type === CustomerType.UNKNOWN) {
|
||||
}
|
||||
|
||||
const salesInvoice = await $tx.salesInvoice.create({
|
||||
data: {
|
||||
...invoiceData,
|
||||
account_id: account.account_id,
|
||||
customer_id: newCustomerId,
|
||||
total_amount: data.total_amount,
|
||||
code: 'INV-' + Date.now(),
|
||||
complex_id: account.complex_id,
|
||||
|
||||
items: {
|
||||
createMany: {
|
||||
data: data.items.map(item => ({
|
||||
@@ -173,17 +169,33 @@ export class SalesInvoicesService {
|
||||
quantity: item.quantity,
|
||||
unit_price: item.unit_price,
|
||||
total_amount: item.total_amount,
|
||||
payload: item.payload,
|
||||
payload: JSON.parse(JSON.stringify(item.payload)),
|
||||
unit_type: item.unit_type,
|
||||
// pricing_model: item.pricingModel,
|
||||
})),
|
||||
},
|
||||
},
|
||||
unknown_customer: {},
|
||||
payments: {
|
||||
createMany: {
|
||||
data: payments,
|
||||
},
|
||||
},
|
||||
// customer: {
|
||||
// connect: {
|
||||
// id: newCustomerId,
|
||||
// },
|
||||
// },
|
||||
account: {
|
||||
connect: {
|
||||
id: consumer_account_id,
|
||||
},
|
||||
},
|
||||
pos: {
|
||||
connect: {
|
||||
id: pos_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user