mindee
Version:
Mindee Client Library for Node.js
213 lines (212 loc) • 9.14 kB
JavaScript
import { cleanOutString, lineSeparator, } from "../../../v1/parsing/common/index.js";
import { FinancialDocumentV1LineItem } from "./financialDocumentV1LineItem.js";
import { AddressField, AmountField, ClassificationField, CompanyRegistrationField, DateField, LocaleField, PaymentDetailsField, StringField, Taxes, } from "../../../v1/parsing/standard/index.js";
/**
* Financial Document API version 1.14 document data.
*/
export class FinancialDocumentV1Document {
constructor(rawPrediction, pageId) {
/** List of company registration numbers associated to the customer. */
this.customerCompanyRegistrations = [];
/** List of line item present on the document. */
this.lineItems = [];
/** List of Reference numbers, including PO number, only if the document is an invoice. */
this.referenceNumbers = [];
/** List of company registration numbers associated to the supplier. */
this.supplierCompanyRegistrations = [];
/** List of payment details associated to the supplier (only for invoices). */
this.supplierPaymentDetails = [];
this.billingAddress = new AddressField({
prediction: rawPrediction["billing_address"],
pageId: pageId,
});
this.category = new ClassificationField({
prediction: rawPrediction["category"],
});
this.customerAddress = new AddressField({
prediction: rawPrediction["customer_address"],
pageId: pageId,
});
if (rawPrediction["customer_company_registrations"]) {
rawPrediction["customer_company_registrations"].map((itemPrediction) => this.customerCompanyRegistrations.push(new CompanyRegistrationField({
prediction: itemPrediction,
pageId: pageId,
})));
}
this.customerId = new StringField({
prediction: rawPrediction["customer_id"],
pageId: pageId,
});
this.customerName = new StringField({
prediction: rawPrediction["customer_name"],
pageId: pageId,
});
this.date = new DateField({
prediction: rawPrediction["date"],
pageId: pageId,
});
this.documentNumber = new StringField({
prediction: rawPrediction["document_number"],
pageId: pageId,
});
this.documentType = new ClassificationField({
prediction: rawPrediction["document_type"],
});
this.documentTypeExtended = new ClassificationField({
prediction: rawPrediction["document_type_extended"],
});
this.dueDate = new DateField({
prediction: rawPrediction["due_date"],
pageId: pageId,
});
this.invoiceNumber = new StringField({
prediction: rawPrediction["invoice_number"],
pageId: pageId,
});
if (rawPrediction["line_items"]) {
rawPrediction["line_items"].map((itemPrediction) => this.lineItems.push(new FinancialDocumentV1LineItem({
prediction: itemPrediction,
pageId: pageId,
})));
}
this.locale = new LocaleField({
prediction: rawPrediction["locale"],
});
this.paymentDate = new DateField({
prediction: rawPrediction["payment_date"],
pageId: pageId,
});
this.poNumber = new StringField({
prediction: rawPrediction["po_number"],
pageId: pageId,
});
this.receiptNumber = new StringField({
prediction: rawPrediction["receipt_number"],
pageId: pageId,
});
if (rawPrediction["reference_numbers"]) {
rawPrediction["reference_numbers"].map((itemPrediction) => this.referenceNumbers.push(new StringField({
prediction: itemPrediction,
pageId: pageId,
})));
}
this.shippingAddress = new AddressField({
prediction: rawPrediction["shipping_address"],
pageId: pageId,
});
this.subcategory = new ClassificationField({
prediction: rawPrediction["subcategory"],
});
this.supplierAddress = new AddressField({
prediction: rawPrediction["supplier_address"],
pageId: pageId,
});
if (rawPrediction["supplier_company_registrations"]) {
rawPrediction["supplier_company_registrations"].map((itemPrediction) => this.supplierCompanyRegistrations.push(new CompanyRegistrationField({
prediction: itemPrediction,
pageId: pageId,
})));
}
this.supplierEmail = new StringField({
prediction: rawPrediction["supplier_email"],
pageId: pageId,
});
this.supplierName = new StringField({
prediction: rawPrediction["supplier_name"],
pageId: pageId,
});
if (rawPrediction["supplier_payment_details"]) {
rawPrediction["supplier_payment_details"].map((itemPrediction) => this.supplierPaymentDetails.push(new PaymentDetailsField({
prediction: itemPrediction,
pageId: pageId,
})));
}
this.supplierPhoneNumber = new StringField({
prediction: rawPrediction["supplier_phone_number"],
pageId: pageId,
});
this.supplierWebsite = new StringField({
prediction: rawPrediction["supplier_website"],
pageId: pageId,
});
this.taxes = new Taxes().init(rawPrediction["taxes"], pageId);
this.time = new StringField({
prediction: rawPrediction["time"],
pageId: pageId,
});
this.tip = new AmountField({
prediction: rawPrediction["tip"],
pageId: pageId,
});
this.totalAmount = new AmountField({
prediction: rawPrediction["total_amount"],
pageId: pageId,
});
this.totalNet = new AmountField({
prediction: rawPrediction["total_net"],
pageId: pageId,
});
this.totalTax = new AmountField({
prediction: rawPrediction["total_tax"],
pageId: pageId,
});
}
/**
* Default string representation.
*/
toString() {
const referenceNumbers = this.referenceNumbers.join("\n ");
const supplierPaymentDetails = this.supplierPaymentDetails.join("\n ");
const supplierCompanyRegistrations = this.supplierCompanyRegistrations.join("\n ");
const customerCompanyRegistrations = this.customerCompanyRegistrations.join("\n ");
let lineItemsSummary = "";
if (this.lineItems && this.lineItems.length > 0) {
const lineItemsColSizes = [38, 14, 10, 12, 14, 14, 17, 12];
lineItemsSummary += "\n" + lineSeparator(lineItemsColSizes, "-") + "\n ";
lineItemsSummary += "| Description ";
lineItemsSummary += "| Product code ";
lineItemsSummary += "| Quantity ";
lineItemsSummary += "| Tax Amount ";
lineItemsSummary += "| Tax Rate (%) ";
lineItemsSummary += "| Total Amount ";
lineItemsSummary += "| Unit of measure ";
lineItemsSummary += "| Unit Price ";
lineItemsSummary += "|\n" + lineSeparator(lineItemsColSizes, "=");
lineItemsSummary += this.lineItems.map((item) => "\n " + item.toTableLine() + "\n" + lineSeparator(lineItemsColSizes, "-")).join("");
}
const outStr = `:Locale: ${this.locale}
:Invoice Number: ${this.invoiceNumber}
:Purchase Order Number: ${this.poNumber}
:Receipt Number: ${this.receiptNumber}
:Document Number: ${this.documentNumber}
:Reference Numbers: ${referenceNumbers}
:Purchase Date: ${this.date}
:Due Date: ${this.dueDate}
:Payment Date: ${this.paymentDate}
:Total Net: ${this.totalNet}
:Total Amount: ${this.totalAmount}
:Taxes: ${this.taxes}
:Supplier Payment Details: ${supplierPaymentDetails}
:Supplier Name: ${this.supplierName}
:Supplier Company Registrations: ${supplierCompanyRegistrations}
:Supplier Address: ${this.supplierAddress}
:Supplier Phone Number: ${this.supplierPhoneNumber}
:Customer Name: ${this.customerName}
:Supplier Website: ${this.supplierWebsite}
:Supplier Email: ${this.supplierEmail}
:Customer Company Registrations: ${customerCompanyRegistrations}
:Customer Address: ${this.customerAddress}
:Customer ID: ${this.customerId}
:Shipping Address: ${this.shippingAddress}
:Billing Address: ${this.billingAddress}
:Document Type: ${this.documentType}
:Document Type Extended: ${this.documentTypeExtended}
:Purchase Subcategory: ${this.subcategory}
:Purchase Category: ${this.category}
:Total Tax: ${this.totalTax}
:Tip and Gratuity: ${this.tip}
:Purchase Time: ${this.time}
:Line Items: ${lineItemsSummary}`.trimEnd();
return cleanOutString(outStr);
}
}