@lynsoluciones/medusa-docs
Version:
Medusa plugins to generate docs
54 lines (53 loc) • 2.87 kB
JavaScript
;
/*
*
*
* MIT License
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateInvoiceTable = void 0;
const hr_1 = require("./hr");
const currency_1 = require("../../../../utils/currency");
const i18next_1 = require("i18next");
function amountToDisplay(amount, currencyCode) {
const decimalDigits = (0, currency_1.getDecimalDigits)(currencyCode);
return `${(amount / Math.pow(10, decimalDigits)).toFixed(decimalDigits)} ${currencyCode.toUpperCase()}`;
}
function generateTableRow(doc, y, item, description, unitCost, quantity, lineTotal) {
doc
.fontSize(10)
.text(item, 50, y)
.text(description, 150, y)
.text(unitCost, 280, y, { width: 90, align: "right" })
.text(quantity, 370, y, { width: 90, align: "right" })
.text(lineTotal, 0, y, { align: "right" });
}
function generateInvoiceTable(doc, y, order, items) {
let i;
const invoiceTableTop = y + 35;
doc.font("Bold");
generateTableRow(doc, invoiceTableTop, (0, i18next_1.t)("invoice-table-header-item", "Item"), (0, i18next_1.t)("invoice-table-header-description", "Description"), (0, i18next_1.t)("invoice-table-header-unit-cost", "Unit Cost"), (0, i18next_1.t)("invoice-table-header-quantity", "Quantity"), (0, i18next_1.t)("invoice-table-header-line-total", "Line Total"));
(0, hr_1.generateHr)(doc, invoiceTableTop + 20);
doc.font("Regular");
for (i = 0; i < items.length; i++) {
const item = items[i];
const position = invoiceTableTop + (i + 1) * 30;
generateTableRow(doc, position, item.title, item.description, amountToDisplay(item.total / item.quantity, order.currency_code), item.quantity, amountToDisplay(item.total, order.currency_code));
(0, hr_1.generateHr)(doc, position + 20);
}
const subtotalPosition = invoiceTableTop + (i + 1) * 30;
generateTableRow(doc, subtotalPosition, "", "", (0, i18next_1.t)("invoice-table-shipping", "Shipping"), "", amountToDisplay(order.shipping_total, order.currency_code));
const taxPosition = subtotalPosition + 30;
generateTableRow(doc, taxPosition, "", "", (0, i18next_1.t)("invoice-table-tax", "Tax"), "", amountToDisplay(order.tax_total, order.currency_code));
const duePosition = taxPosition + 45;
doc.font("Bold");
generateTableRow(doc, duePosition, "", "", (0, i18next_1.t)("invoice-table-total", "Total"), "", amountToDisplay(order.total, order.currency_code));
doc.font("Regular");
}
exports.generateInvoiceTable = generateInvoiceTable;