2026-03-07 11:25:11 +03:30
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
2026-01-07 15:25:59 +03:30
|
|
|
import { Injectable } from '@nestjs/common'
|
2026-03-07 11:25:11 +03:30
|
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
|
|
|
|
import type { CustomerIndividual, CustomerLegal } from 'generated/prisma/client'
|
|
|
|
|
import { CustomerType, PaymentMethodType } from 'generated/prisma/enums'
|
2026-02-24 12:42:10 +03:30
|
|
|
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' &&
|
|
|
|
|
typeof customer.last_name === 'string'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Define type guard for CustomerLegal
|
|
|
|
|
function isCustomerLegal(customer: any): customer is CustomerLegal {
|
|
|
|
|
return (
|
|
|
|
|
customer &&
|
|
|
|
|
typeof customer.company_name === 'string' &&
|
|
|
|
|
typeof customer.registration_number === 'string'
|
|
|
|
|
)
|
|
|
|
|
}
|
2026-01-07 15:25:59 +03:30
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class SalesInvoicesService {
|
2026-02-24 12:42:10 +03:30
|
|
|
constructor(private prisma: PrismaService) {}
|
|
|
|
|
|
2026-02-12 20:31:04 +03:30
|
|
|
findAll() {
|
|
|
|
|
// TODO: Implement fetching all sales invoices
|
|
|
|
|
return []
|
2026-01-07 15:25:59 +03:30
|
|
|
}
|
|
|
|
|
|
2026-02-12 20:31:04 +03:30
|
|
|
findOne(id: number) {
|
|
|
|
|
// TODO: Implement fetching a single sales invoice
|
|
|
|
|
return {}
|
2026-01-07 15:25:59 +03:30
|
|
|
}
|
|
|
|
|
|
2026-02-24 12:42:10 +03:30
|
|
|
async create(data: CreateSalesInvoiceDto, account) {
|
|
|
|
|
data.invoice_date = new Date(data.invoice_date).toISOString() as any
|
|
|
|
|
|
|
|
|
|
const salesInvoice = await this.prisma.$transaction(async $tx => {
|
|
|
|
|
const payments = Object.entries(data.payments)
|
|
|
|
|
.filter(([_, value]) => value > 0 && PaymentMethodType[_.toUpperCase()])
|
|
|
|
|
.map(([key, value]) => {
|
|
|
|
|
return {
|
|
|
|
|
amount: value,
|
|
|
|
|
payment_method: key.toLocaleUpperCase() as PaymentMethodType,
|
|
|
|
|
paid_at: data.invoice_date,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const totalPayments = payments.reduce((sum, payment) => sum + payment.amount, 0)
|
|
|
|
|
if (totalPayments !== data.total_amount) {
|
|
|
|
|
throw new Error('مبلغ پرداختی باید برابر با مبلغ کل فاکتور باشد.')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { customer_id, customer_type, customer, ...invoiceData } = data
|
|
|
|
|
let newCustomerId: string | null = customer_id || null
|
|
|
|
|
if (customer_id) {
|
|
|
|
|
} else if (
|
|
|
|
|
customer_type === CustomerType.INDIVIDUAL &&
|
|
|
|
|
isCustomerIndividual(customer)
|
|
|
|
|
) {
|
|
|
|
|
let customerIndividual = await $tx.customerIndividual.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
complex_id_national_id: {
|
|
|
|
|
complex_id: account.complex_id,
|
|
|
|
|
national_id: customer.national_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (customerIndividual) {
|
|
|
|
|
await $tx.customerIndividual.update({
|
|
|
|
|
where: {
|
|
|
|
|
complex_id_national_id: {
|
|
|
|
|
complex_id: account.complex_id,
|
|
|
|
|
national_id: customer.national_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
...customer,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
const customerIndividual = await $tx.customerIndividual.create({
|
|
|
|
|
data: {
|
|
|
|
|
...customer,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
const createdCustomer = await $tx.customer.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: CustomerType.INDIVIDUAL,
|
|
|
|
|
complex_id: account.complex_id,
|
|
|
|
|
customerIndividuals: {
|
|
|
|
|
connect: {
|
|
|
|
|
customer_id: customerIndividual.customer_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (createdCustomer && createdCustomer.id) {
|
|
|
|
|
newCustomerId = createdCustomer.id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (data.customer_type === CustomerType.LEGAL && isCustomerLegal(customer)) {
|
|
|
|
|
let customerLegal = await $tx.customerLegal.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
complex_id_registration_number: {
|
|
|
|
|
complex_id: account.complex_id,
|
|
|
|
|
registration_number: customer.registration_number,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (customerLegal) {
|
|
|
|
|
await $tx.customerLegal.update({
|
|
|
|
|
where: {
|
|
|
|
|
complex_id_registration_number: {
|
|
|
|
|
complex_id: account.complex_id,
|
|
|
|
|
registration_number: customer.registration_number,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
...customer,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
const customerLegal = await $tx.customerLegal.create({
|
|
|
|
|
data: {
|
|
|
|
|
...customer,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
const createdCustomer = await $tx.customer.create({
|
|
|
|
|
data: {
|
|
|
|
|
type: CustomerType.LEGAL,
|
|
|
|
|
complex_id: account.complex_id,
|
|
|
|
|
customerIndividuals: {
|
|
|
|
|
connect: {
|
|
|
|
|
customer_id: customerLegal.customer_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (createdCustomer && createdCustomer.id) {
|
|
|
|
|
newCustomerId = createdCustomer.id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 => ({
|
|
|
|
|
good_id: item.good_id,
|
|
|
|
|
quantity: item.quantity,
|
|
|
|
|
unit_price: item.unit_price,
|
|
|
|
|
total_amount: item.total_amount,
|
|
|
|
|
payload: item.payload,
|
|
|
|
|
unit_type: item.unit_type,
|
2026-03-07 11:25:11 +03:30
|
|
|
// pricing_model: item.pricingModel,
|
2026-02-24 12:42:10 +03:30
|
|
|
})),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
payments: {
|
|
|
|
|
createMany: {
|
|
|
|
|
data: payments,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return salesInvoice
|
|
|
|
|
})
|
|
|
|
|
return ResponseMapper.create(salesInvoice)
|
2026-01-07 15:25:59 +03:30
|
|
|
}
|
|
|
|
|
}
|