feat: add payment issue tax invoice flow account

This commit is contained in:
Methapon2001 2024-12-12 15:25:04 +07:00
parent 10419861a4
commit 4ef407c8b1

View file

@ -22,6 +22,9 @@ import {
createPermCheck,
createPermCondition,
} from "../services/permission";
import flowAccount from "../services/flowaccount";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
const MANAGE_ROLES = ["system", "head_of_admin", "admin", "head_of_accountant", "accountant"];
@ -304,3 +307,46 @@ export class PaymentFileController extends Controller {
return await deleteFile(fileLocation.quotation.payment(quotationId, paymentId, name));
}
}
@Route("api/v1/payment/{paymentId}/flow-account")
@Tags("Payment")
export class FlowAccountController extends Controller {
@Get()
async getDocument(@Path() paymentId: string) {
const payment = await prisma.payment.findFirst({
where: {
id: paymentId,
},
include: { invoice: true },
});
if (!payment) throw notFoundError("Payment");
if (payment.paymentStatus !== PaymentStatus.PaymentSuccess)
throw new HttpError(
HttpStatus.PRECONDITION_FAILED,
"Payment not success",
"paymentNotSuccess",
);
if (!payment.invoice.flowAccountRecordId) {
const result = await flowAccount.issueInvoice(payment.invoice.id);
const documentId = result?.body?.data?.documentId;
await prisma.invoice.update({
where: { id: payment.invoice.id },
data: { flowAccountRecordId: documentId },
});
if (documentId) {
return flowAccount.getInvoiceDocument(documentId);
} else {
throw new HttpError(
HttpStatus.INTERNAL_SERVER_ERROR,
"Failed to issue invoice/receipt document.",
"InvoiceReceiptIssueFailed",
);
}
} else {
return flowAccount.getInvoiceDocument(payment.invoice.flowAccountRecordId);
}
}
}