convert actions
This commit is contained in:
@@ -329,3 +329,61 @@ export async function updateSalesDocument(type: SalesDocumentType, documentId: s
|
||||
const detail = await getSalesDocumentById(type, documentId);
|
||||
return detail ? { ok: true as const, document: detail } : { ok: false as const, reason: "Unable to load saved document." };
|
||||
}
|
||||
|
||||
export async function updateSalesDocumentStatus(type: SalesDocumentType, documentId: string, status: SalesDocumentStatus) {
|
||||
const existing = await documentConfig[type].findUnique({
|
||||
where: { id: documentId },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return { ok: false as const, reason: "Sales document was not found." };
|
||||
}
|
||||
|
||||
await documentConfig[type].update({
|
||||
where: { id: documentId },
|
||||
data: { status },
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
const detail = await getSalesDocumentById(type, documentId);
|
||||
return detail ? { ok: true as const, document: detail } : { ok: false as const, reason: "Unable to load updated document." };
|
||||
}
|
||||
|
||||
export async function convertQuoteToSalesOrder(quoteId: string) {
|
||||
const quote = await documentConfig.QUOTE.findUnique({
|
||||
where: { id: quoteId },
|
||||
include: buildInclude("QUOTE"),
|
||||
});
|
||||
|
||||
if (!quote) {
|
||||
return { ok: false as const, reason: "Quote was not found." };
|
||||
}
|
||||
|
||||
const mappedQuote = mapDocument(quote as SalesDocumentRecord);
|
||||
const nextOrderNumber = await nextDocumentNumber("ORDER");
|
||||
|
||||
const created = await documentConfig.ORDER.create({
|
||||
data: {
|
||||
documentNumber: nextOrderNumber,
|
||||
customerId: mappedQuote.customerId,
|
||||
status: "DRAFT",
|
||||
issueDate: new Date(),
|
||||
notes: mappedQuote.notes ? `Converted from ${mappedQuote.documentNumber}\n\n${mappedQuote.notes}` : `Converted from ${mappedQuote.documentNumber}`,
|
||||
lines: {
|
||||
create: mappedQuote.lines.map((line) => ({
|
||||
itemId: line.itemId,
|
||||
description: line.description,
|
||||
quantity: line.quantity,
|
||||
unitOfMeasure: line.unitOfMeasure,
|
||||
unitPrice: line.unitPrice,
|
||||
position: line.position,
|
||||
})),
|
||||
},
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
const order = await getSalesDocumentById("ORDER", created.id);
|
||||
return order ? { ok: true as const, document: order } : { ok: false as const, reason: "Unable to load converted sales order." };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user