/** * Copyright (c) 2026 Niolla * All rights reserved. * * This source code is proprietary and confidential. * Unauthorized copying, modification, distribution, or use * of this file, via any medium, is strictly prohibited. */ // app/templates/documents/creditNote/creditNote.template.js const { log } = require("../../../utils/consoleLog.utill"); module.exports = async (data) => { log("📋 Preparing Credit Note data for PDF..."); let payload = data; if (typeof payload === "string") { try { payload = JSON.parse(payload); } catch (error) { payload = {}; } } const source = payload.documentData || payload.document_data || payload.data || payload; const pick = (...keys) => { for (const key of keys) { const value = source?.[key]; if (value !== undefined && value !== null && value !== "") { return value; } } return ""; }; // Normalize incoming fields and provide defaults matching the screenshot const company = { name: pick("companyName", "company_name") || "GREEK-LANKA MARITIME SERVICES (PVT) LTD", addressLine1: pick("companyAddressLine1", "company_address_line1") || "No. 56/2, Dharmapala Mw, Kotte", addressLine2: pick("companyAddressLine2", "company_address_line2") || "Sri Jayewardenepura", addressLine3: pick("companyAddressLine3", "company_address_line3") || "Sri Lanka", postal: pick("companyPostal", "company_postal") || "Postal Code : 10100", contact: pick("companyContact", "company_contact") || "Tel:+94 11 2083206 / Mobile (24/7) : +94 777 232 271", br: pick("companyBR", "company_br") || "BR No : PV 0022630", license: pick("companyLicense", "company_license") || "License No : SA00317-2024", logoUrl: pick("logoUrl", "logo_url") || "https://res.cloudinary.com/dtthuvvir/image/upload/v1780748661/company_logo_transperent_bg_uullzw.png", }; const header = { title: pick("title") || "CREDIT NOTE", }; const billTo = { name: pick("billToName", "bill_to_name") || "Master & Owner of MV TEAM VENTURES", details: pick("billToDetails", "bill_to_details") || "VRIDHI MARITIME SHIP MANAGEMENT & OPERATION LLC\nOffice No: 2004, The Prism Tower, Dubai", }; const meta = { crnNo: pick("crnNo", "crn_no", "creditNoteNumber", "credit_note_number", "reference_no") || "GLMS/COLOMBO/474/CRN01", date: pick("date") || new Date().toLocaleDateString(), }; const vessel = { name: pick("vesselName", "vessel_name") || 'MV "TEAM VENTURES"', grt: pick("grt", "GRT") || "25,543 MT", port: pick("port", "port_name") || "REPAIRS AT COLOMBO DOCK YARD", imo: pick("imo", "imo_number", "imoNumber") || "9339765", agent: pick("nameOfAgent", "name_of_agent", "agent") || "", portCountry: pick("portCountry", "port_country") || "COLOMBO / SRI LANKA", }; const orderMeta = { orderDate: pick("orderDate", "order_date") || meta.date, orderNo: pick("orderNo", "order_no") || "", portReference: pick("portReference", "port_reference") || "", }; // Items: array of { description, amount } const items = (Array.isArray(source.items) ? source.items : []).map((it) => ({ description: it.description || it.desc || it.item_name || '', amount: Number(it.amount || it.value || it.total || it.totalAmount || 0), quantity: it.quantity ?? it.qty ?? '', note: it.note || it.notes || '', })); const computedTotal = items.reduce((s, i) => s + (i.amount || 0), 0); const templateData = { company, header, billTo, meta, vessel, orderMeta, items, totalAmount: Number(pick("totalAmount", "total") || computedTotal), approvedBy: pick("approvedBy", "approved_by") || 'GLMS Finance Dept.', approvedByLabel: pick("approvedByLabel", "approved_by_label") || 'Approved by GLMS Finance Dept.', departmentLabel: pick("departmentLabel", "department_label") || 'GLMS - Accounts Department', }; // Top-level conveniences for templates and pdfRenderer templateData.logoUrl = company.logoUrl; templateData.creditNoteNumber = templateData.meta.crnNo; templateData.date = templateData.meta.date; templateData.billToName = templateData.billTo.name; templateData.billToDetails = templateData.billTo.details; templateData.vessel = templateData.vessel; // Provide invoiceNumber if present templateData.invoiceNumber = pick("invoiceNumber", "invoice_number", "invoice_no") || ''; // Provide subtotal/tax/discount/total for EJS summary table compatibility templateData.subtotal = Number(pick("subtotal", "sub_total") || computedTotal); templateData.tax = Number(pick("tax") || 0); templateData.discount = Number(pick("discount") || 0); templateData.total = Number(pick("totalAmount", "total") || templateData.subtotal + templateData.tax - templateData.discount); // Provide reason / notes templateData.reason = pick("reason", "remarks", "notes") || ''; // Provide paymentTerms and notes for EJS templateData.paymentTerms = pick("paymentTerms", "payment_terms") || ''; templateData.notes = pick("notes", "note") || ''; log("✅ Credit Note data prepared", JSON.stringify(templateData)); return templateData; };