feat: add flowaccount issue invoice

This commit is contained in:
Methapon2001 2024-12-06 18:04:13 +07:00
parent 71721cb03c
commit cb9c9cef68

View file

@ -15,6 +15,47 @@ const clientScope = process.env.FLOW_ACCOUNT_CLIENT_SCOPE;
let token: string = "";
let tokenExpire: number = Date.now();
enum ContactGroup {
PERS = 1,
CORP = 3,
}
enum CreditType {
Cash = 3,
CreditDay = 1,
CreditNoLimit = 5,
}
enum DocumentDeductionType {
Special = 1,
Agent = 3,
Process = 5,
Round = 7,
}
enum SaleAndPurchaseChannel {
Lazada = "Lazada",
Shopee = "Shopee",
}
enum ProductAndServiceType {
Service = 1,
ProductNonInv = 3,
ProductInv = 5,
}
type ProductAndService = {
type?: ProductAndServiceType;
name: string;
description?: string;
quantity: number;
unitName?: string;
pricePerUnit: number;
total: number;
sellChartOfAccountCode?: string;
buyChartOfAccountcode?: string;
};
const flowaccount = {
auth: async () => {
// Get new token if it is expiring soon (30s).
@ -47,6 +88,87 @@ const flowaccount = {
return { token, tokenExpire };
},
createInvoice: async (
data: {
recordId?: string;
contactCode?: string;
contactName: string;
contactAddress: string;
contactTaxId?: string;
contactBranch?: string;
contactPerson?: string;
contactEmail?: string;
contactNumber?: string;
contactZipCode?: string;
contactGroup?: ContactGroup;
/** This must be in yyyy-MM-dd format if pass as string */
publishedOn?: Date | string;
creditType?: CreditType;
/** This is integer */
creditDays?: number;
/** This must be in yyyy-MM-dd format if pass as string */
dueDate?: Date | string;
salesName?: string;
projectName?: string;
reference?: string;
isVatInclusive: boolean;
useReceiptDeduction?: boolean;
subTotal: number;
discounPercentage?: number;
discountAmount?: number;
totalAfterDiscount: number;
isVat?: boolean;
vatAmount?: number;
/** VAT included */
grandTotal?: number;
/** แสดงหรือไม่แสดง หัก ณ ที่จ่ายท้ายเอกสาร */
documentShowWithholdingTax?: boolean;
/** ภาษีหัก ณ ที่จ่าย (%) */
documentWithholdingTaxPercentage?: number;
/** ภาษีหัก ณ ที่จ่าย */
documentWithholdingTaxAmount?: number;
documentDeductionType?: DocumentDeductionType;
documentDeductionAmount?: number;
remarks?: string;
internalNotes?: string;
showSignatureOrStamp?: boolean;
documentStructureType?: "SimpleDocument" | null;
saleAndPurchaseChannel?: SaleAndPurchaseChannel;
items: ProductAndService[];
},
withPayment?: boolean,
) => {
const { token } = await flowaccount.auth();
if (data.publishedOn instanceof Date) {
let date = data.publishedOn.getDate();
let month = data.publishedOn.getMonth() + 1;
let year = data.publishedOn.getFullYear();
data.publishedOn = `${year}-${String(month).padStart(2, "0")}-${String(date).padStart(2, "0")}`;
}
if (data.dueDate instanceof Date) {
let date = data.dueDate.getDate();
let month = data.dueDate.getMonth() + 1;
let year = data.dueDate.getFullYear();
data.dueDate = `${year}-${String(month).padStart(2, "0")}-${String(date).padStart(2, "0")}`;
}
const res = await fetch(api + "/tax-invoices" + withPayment ? "/with-payment" : "", {
method: "POST",
headers: {
["Content-Type"]: `application/json`,
["Authorization"]: `Bearer ${token}`,
},
body: JSON.stringify(data),
});
return {
status: res.status,
body: await res.json(),
};
},
};
export default flowaccount;