refactor: update flow-account api function
This commit is contained in:
parent
eb8e311d3d
commit
4245bcaca8
1 changed files with 83 additions and 55 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import prisma from "../db";
|
import prisma from "../db";
|
||||||
import config from "../config.json";
|
import config from "../config.json";
|
||||||
|
import { CustomerType } from "@prisma/client";
|
||||||
|
|
||||||
if (!process.env.FLOW_ACCOUNT_URL) throw new Error("Require FLOW_ACCOUNT_URL");
|
if (!process.env.FLOW_ACCOUNT_URL) throw new Error("Require FLOW_ACCOUNT_URL");
|
||||||
if (!process.env.FLOW_ACCOUNT_CLIENT_ID) throw new Error("Require FLOW_ACCOUNT_CLIENT_ID");
|
if (!process.env.FLOW_ACCOUNT_CLIENT_ID) throw new Error("Require FLOW_ACCOUNT_CLIENT_ID");
|
||||||
|
|
@ -50,6 +51,13 @@ enum ProductAndServiceType {
|
||||||
ProductInv = 5,
|
ProductInv = 5,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum PaymentDeductionType {
|
||||||
|
SpecialDiscount = 1,
|
||||||
|
Commission = 3,
|
||||||
|
Process = 5,
|
||||||
|
Round = 7,
|
||||||
|
}
|
||||||
|
|
||||||
type ProductAndService = {
|
type ProductAndService = {
|
||||||
type?: ProductAndServiceType;
|
type?: ProductAndServiceType;
|
||||||
name: string;
|
name: string;
|
||||||
|
|
@ -144,6 +152,10 @@ const flowAccountAPI = {
|
||||||
documentStructureType?: "SimpleDocument" | null;
|
documentStructureType?: "SimpleDocument" | null;
|
||||||
saleAndPurchaseChannel?: SaleAndPurchaseChannel;
|
saleAndPurchaseChannel?: SaleAndPurchaseChannel;
|
||||||
items: ProductAndService[];
|
items: ProductAndService[];
|
||||||
|
/** This must be in yyyy-MM-dd format if pass as string */
|
||||||
|
paymentDate: Date | string;
|
||||||
|
paymentDeductionType?: PaymentDeductionType;
|
||||||
|
collected: number;
|
||||||
},
|
},
|
||||||
withPayment?: boolean,
|
withPayment?: boolean,
|
||||||
) {
|
) {
|
||||||
|
|
@ -163,7 +175,14 @@ const flowAccountAPI = {
|
||||||
data.dueDate = `${year}-${String(month).padStart(2, "0")}-${String(date).padStart(2, "0")}`;
|
data.dueDate = `${year}-${String(month).padStart(2, "0")}-${String(date).padStart(2, "0")}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await fetch(api + "/tax-invoices/inline" + withPayment ? "/with-payment" : "", {
|
if (data.paymentDate instanceof Date) {
|
||||||
|
let date = data.paymentDate.getDate();
|
||||||
|
let month = data.paymentDate.getMonth() + 1;
|
||||||
|
let year = data.paymentDate.getFullYear();
|
||||||
|
data.paymentDate = `${year}-${String(month).padStart(2, "0")}-${String(date).padStart(2, "0")}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = await fetch(api + "/tax-invoices/inline" + (withPayment ? "/with-payment" : ""), {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
["Content-Type"]: `application/json`,
|
["Content-Type"]: `application/json`,
|
||||||
|
|
@ -180,14 +199,16 @@ const flowAccountAPI = {
|
||||||
},
|
},
|
||||||
|
|
||||||
async getInvoiceDocument(recordId: string) {
|
async getInvoiceDocument(recordId: string) {
|
||||||
const res = await fetch(api + "tax-invoices/shareddocument", {
|
const { token } = await flowAccountAPI.auth();
|
||||||
|
|
||||||
|
const res = await fetch(api + "/tax-invoices/sharedocument", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
["Content-Type"]: `application/json`,
|
["Content-Type"]: `application/json`,
|
||||||
["Authorization"]: `Bearer ${token}`,
|
["Authorization"]: `Bearer ${token}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
recordId,
|
documentId: recordId,
|
||||||
culture: "th",
|
culture: "th",
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
@ -195,13 +216,13 @@ const flowAccountAPI = {
|
||||||
return {
|
return {
|
||||||
ok: res.ok,
|
ok: res.ok,
|
||||||
status: res.status,
|
status: res.status,
|
||||||
body: await res.json(),
|
body: await res.text(),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const flowAccount = {
|
const flowAccount = {
|
||||||
issueInvoiceWithPayment: async (invoiceId: string) => {
|
issueInvoice: async (invoiceId: string) => {
|
||||||
const data = await prisma.invoice.findFirst({
|
const data = await prisma.invoice.findFirst({
|
||||||
where: { id: invoiceId },
|
where: { id: invoiceId },
|
||||||
include: {
|
include: {
|
||||||
|
|
@ -240,67 +261,74 @@ const flowAccount = {
|
||||||
const branch = quotation.registeredBranch;
|
const branch = quotation.registeredBranch;
|
||||||
const product = quotation.productServiceList;
|
const product = quotation.productServiceList;
|
||||||
|
|
||||||
return await flowAccountAPI.createInvoice(
|
const payload = {
|
||||||
{
|
contactCode: customer.code,
|
||||||
contactCode: customer.code,
|
contactName:
|
||||||
contactName: [customer.firstName, customer.lastName].join(" "),
|
(customer.customer.customerType === CustomerType.PERS
|
||||||
contactAddress: [
|
? [customer.firstName, customer.lastName].join(" ").trim()
|
||||||
customer.address,
|
: customer.registerName) || "-",
|
||||||
!!customer.moo ? "หมู่" + customer.moo : null,
|
contactAddress: [
|
||||||
!!customer.soi ? "ซอย" + customer.soi : null,
|
customer.address,
|
||||||
!!customer.street ? "ถนน" + customer.street : null,
|
!!customer.moo ? "หมู่" + customer.moo : null,
|
||||||
(customer.province?.id === "10" ? "แขวง" : "อำเภอ") + customer.subDistrict?.name,
|
!!customer.soi ? "ซอย" + customer.soi : null,
|
||||||
(customer.province?.id === "10" ? "เขต" : "ตำบล") + customer.district?.name,
|
!!customer.street ? "ถนน" + customer.street : null,
|
||||||
customer.province?.name,
|
(customer.province?.id === "10" ? "แขวง" : "อำเภอ") + customer.subDistrict?.name,
|
||||||
customer.subDistrict?.zipCode,
|
(customer.province?.id === "10" ? "เขต" : "ตำบล") + customer.district?.name,
|
||||||
]
|
customer.province?.name,
|
||||||
.filter(Boolean)
|
customer.subDistrict?.zipCode,
|
||||||
.join(" "),
|
]
|
||||||
contactTaxId: customer.citizenId || customer.code,
|
.filter(Boolean)
|
||||||
contactBranch: customer.authorizedName ?? undefined,
|
.join(" "),
|
||||||
contactPerson: branch.contactName ?? undefined,
|
contactTaxId: customer.citizenId || customer.code,
|
||||||
contactEmail: branch.email,
|
contactBranch:
|
||||||
contactNumber: branch.telephoneNo,
|
(customer.customer.customerType === CustomerType.PERS
|
||||||
contactZipCode: branch.subDistrict?.zipCode,
|
? [customer.firstName, customer.lastName].join(" ").trim()
|
||||||
contactGroup:
|
: customer.registerName) || "-",
|
||||||
customer.customer.customerType === "PERS" ? ContactGroup.PERS : ContactGroup.CORP,
|
contactPerson: customer.contactName ?? undefined,
|
||||||
dueDate: quotation.dueDate,
|
contactEmail: customer.email,
|
||||||
salesName: [quotation.createdBy?.firstName, quotation.createdBy?.lastName].join(" "),
|
contactNumber: customer.telephoneNo,
|
||||||
|
contactZipCode: customer.subDistrict?.zipCode,
|
||||||
|
contactGroup:
|
||||||
|
customer.customer.customerType === "PERS" ? ContactGroup.PERS : ContactGroup.CORP,
|
||||||
|
dueDate: quotation.dueDate,
|
||||||
|
salesName: [quotation.createdBy?.firstName, quotation.createdBy?.lastName].join(" "),
|
||||||
|
|
||||||
isVatInclusive: true,
|
isVatInclusive: true,
|
||||||
isVat: true,
|
isVat: true,
|
||||||
|
|
||||||
useReceiptDeduction: true,
|
useReceiptDeduction: true,
|
||||||
|
|
||||||
discounPercentage: 0,
|
discounPercentage: 0,
|
||||||
discountAmount: quotation.totalDiscount,
|
discountAmount: quotation.totalDiscount,
|
||||||
|
|
||||||
subTotal: quotation.totalPrice,
|
subTotal: quotation.totalPrice,
|
||||||
totalAfterDiscount: quotation.finalPrice,
|
totalAfterDiscount: quotation.finalPrice,
|
||||||
vatAmount: quotation.vat,
|
vatAmount: quotation.vat,
|
||||||
grandTotal: quotation.finalPrice,
|
grandTotal: quotation.finalPrice,
|
||||||
|
|
||||||
items: product.map((v) => ({
|
paymentDate: data.payment?.createdAt ?? new Date(),
|
||||||
type: ProductAndServiceType.ProductNonInv,
|
collected: quotation.finalPrice,
|
||||||
name: v.product.name,
|
|
||||||
pricePerUnit: v.pricePerUnit,
|
items: product.map((v) => ({
|
||||||
quantity: v.amount,
|
type: ProductAndServiceType.ProductNonInv,
|
||||||
discountAmount: v.discount,
|
name: v.product.name,
|
||||||
total: (v.pricePerUnit - (v.discount || 0)) * v.amount + v.vat,
|
pricePerUnit: v.pricePerUnit,
|
||||||
vatRate: VAT_DEFAULT * 100,
|
quantity: v.amount,
|
||||||
})),
|
discountAmount: v.discount,
|
||||||
},
|
total: (v.pricePerUnit - (v.discount || 0)) * v.amount + v.vat,
|
||||||
true,
|
vatRate: Math.round(VAT_DEFAULT * 100),
|
||||||
);
|
})),
|
||||||
|
};
|
||||||
|
|
||||||
|
return await flowAccountAPI.createInvoice(payload, false);
|
||||||
},
|
},
|
||||||
|
|
||||||
getInvoiceDocument: async (recordId: string) => {
|
getInvoiceDocument: async (recordId: string) => {
|
||||||
const ret = await flowAccountAPI.getInvoiceDocument(recordId);
|
const ret = await flowAccountAPI.getInvoiceDocument(recordId);
|
||||||
|
console.log(ret);
|
||||||
if (ret && ret.ok) {
|
if (ret && ret.ok) {
|
||||||
return ret.body.data.link;
|
return ret.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue