mindee
Version:
Mindee Client Library for Node.js
115 lines (114 loc) • 4.71 kB
JavaScript
import { cleanOutString, lineSeparator, } from "../../../v1/parsing/common/index.js";
import { ReceiptV5LineItem } from "./receiptV5LineItem.js";
import { AmountField, ClassificationField, CompanyRegistrationField, DateField, LocaleField, StringField, Taxes, } from "../../../v1/parsing/standard/index.js";
/**
* Receipt API version 5.4 document data.
*/
export class ReceiptV5Document {
constructor(rawPrediction, pageId) {
/** List of all line items on the receipt. */
this.lineItems = [];
/** List of company registration numbers associated to the supplier. */
this.supplierCompanyRegistrations = [];
this.category = new ClassificationField({
prediction: rawPrediction["category"],
});
this.date = new DateField({
prediction: rawPrediction["date"],
pageId: pageId,
});
this.documentType = new ClassificationField({
prediction: rawPrediction["document_type"],
});
if (rawPrediction["line_items"]) {
rawPrediction["line_items"].map((itemPrediction) => this.lineItems.push(new ReceiptV5LineItem({
prediction: itemPrediction,
pageId: pageId,
})));
}
this.locale = new LocaleField({
prediction: rawPrediction["locale"],
});
this.receiptNumber = new StringField({
prediction: rawPrediction["receipt_number"],
pageId: pageId,
});
this.subcategory = new ClassificationField({
prediction: rawPrediction["subcategory"],
});
this.supplierAddress = new StringField({
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.supplierName = new StringField({
prediction: rawPrediction["supplier_name"],
pageId: pageId,
});
this.supplierPhoneNumber = new StringField({
prediction: rawPrediction["supplier_phone_number"],
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 supplierCompanyRegistrations = this.supplierCompanyRegistrations.join("\n ");
let lineItemsSummary = "";
if (this.lineItems && this.lineItems.length > 0) {
const lineItemsColSizes = [38, 10, 14, 12];
lineItemsSummary += "\n" + lineSeparator(lineItemsColSizes, "-") + "\n ";
lineItemsSummary += "| Description ";
lineItemsSummary += "| Quantity ";
lineItemsSummary += "| Total Amount ";
lineItemsSummary += "| Unit Price ";
lineItemsSummary += "|\n" + lineSeparator(lineItemsColSizes, "=");
lineItemsSummary += this.lineItems.map((item) => "\n " + item.toTableLine() + "\n" + lineSeparator(lineItemsColSizes, "-")).join("");
}
const outStr = `:Expense Locale: ${this.locale}
:Purchase Category: ${this.category}
:Purchase Subcategory: ${this.subcategory}
:Document Type: ${this.documentType}
:Purchase Date: ${this.date}
:Purchase Time: ${this.time}
:Total Amount: ${this.totalAmount}
:Total Net: ${this.totalNet}
:Total Tax: ${this.totalTax}
:Tip and Gratuity: ${this.tip}
:Taxes: ${this.taxes}
:Supplier Name: ${this.supplierName}
:Supplier Company Registrations: ${supplierCompanyRegistrations}
:Supplier Address: ${this.supplierAddress}
:Supplier Phone Number: ${this.supplierPhoneNumber}
:Receipt Number: ${this.receiptNumber}
:Line Items: ${lineItemsSummary}`.trimEnd();
return cleanOutString(outStr);
}
}