From e477e2202755b099ff0f5e2aa20c8fe233508177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Szymkiewicz?= Date: Mon, 2 Dec 2024 22:06:02 +0100 Subject: [PATCH] Billing or shipping address can be empty --- package.json | 2 +- .../invoices/basic/parts/customer-info.ts | 77 ++++++++++++------- .../basic/parts/customer-info-small.ts | 63 +++++++++------ .../basic/parts/customer-info.ts | 74 +++++++++++------- 4 files changed, 136 insertions(+), 80 deletions(-) diff --git a/package.json b/package.json index 8bc3630..7a3467a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rsc-labs/medusa-documents", - "version": "0.8.4", + "version": "0.8.5", "description": "Generate documents from Medusa", "main": "dist/index.js", "author": "RSC Labs (https://rsoftcon.com)", diff --git a/src/services/templates/invoices/basic/parts/customer-info.ts b/src/services/templates/invoices/basic/parts/customer-info.ts index 4568753..d407bcd 100644 --- a/src/services/templates/invoices/basic/parts/customer-info.ts +++ b/src/services/templates/invoices/basic/parts/customer-info.ts @@ -24,35 +24,54 @@ export function generateCustomerInformation(doc, y, order: Order) { const customerInformationTop = y + 70; - doc - .fontSize(10) - .font("Bold") - - .text(`${t("invoice-bill-to", "Bill to")}:`, 50, customerInformationTop, {align: 'left'}) - .font("Regular") - .text(`${order.billing_address.first_name} ${order.billing_address.last_name}`, 50, customerInformationTop + 15, {align: 'left'}) - .text(`${order.billing_address.city} ${order.billing_address.postal_code}`, 50, customerInformationTop + 30, {align: 'left'}) - const billAddress = order.billing_address.address_1; - const heightOfBillToAddress = doc.heightOfString(billAddress, { width: 150 }) - doc.text(billAddress, 50, customerInformationTop + 45 , {align: 'left', width: 150 }) - .moveDown(); - - doc - .fontSize(10) - .font("Bold") - .text(`${t("invoice-ship-to", "Ship to")}:`, 50, customerInformationTop, {align: 'right'}) - .font("Regular") - .text(`${order.shipping_address.first_name} ${order.shipping_address.last_name}`, 50, customerInformationTop + 15, {align: 'right'}) - .text(`${order.shipping_address.city} ${order.shipping_address.postal_code}`, 50, customerInformationTop + 30, {align: 'right'}) - .moveDown(); - const shipAddress = order.shipping_address.address_1; - const heightOfShipToAddress = doc.heightOfString(shipAddress, { width: 150 }) - doc.text(shipAddress, 360, customerInformationTop + 45 , {align: 'right', widdth: 150 }) - .moveDown(); - - if (heightOfShipToAddress > heightOfBillToAddress) { - return customerInformationTop + 45 + heightOfShipToAddress; - } else { + let heightOfBillToAddress: number | undefined; + + if (order.billing_address) { + doc + .fontSize(10) + .font("Bold") + + .text(`${t("invoice-bill-to", "Bill to")}:`, 50, customerInformationTop, {align: 'left'}) + .font("Regular") + .text(`${order.billing_address.first_name} ${order.billing_address.last_name}`, 50, customerInformationTop + 15, {align: 'left'}) + .text(`${order.billing_address.city} ${order.billing_address.postal_code}`, 50, customerInformationTop + 30, {align: 'left'}) + const billAddress = order.billing_address.address_1; + heightOfBillToAddress = doc.heightOfString(billAddress, { width: 150 }) + doc.text(billAddress, 50, customerInformationTop + 45 , {align: 'left', width: 150 }) + .moveDown(); + } + + let heightOfShipToAddress: number | undefined; + + if (order.shipping_address) { + doc + .fontSize(10) + .font("Bold") + .text(`${t("invoice-ship-to", "Ship to")}:`, 50, customerInformationTop, {align: 'right'}) + .font("Regular") + .text(`${order.shipping_address.first_name} ${order.shipping_address.last_name}`, 50, customerInformationTop + 15, {align: 'right'}) + .text(`${order.shipping_address.city} ${order.shipping_address.postal_code}`, 50, customerInformationTop + 30, {align: 'right'}) + .moveDown(); + const shipAddress = order.shipping_address.address_1; + heightOfShipToAddress = doc.heightOfString(shipAddress, { width: 150 }) + doc.text(shipAddress, 360, customerInformationTop + 45 , {align: 'right', widdth: 150 }) + .moveDown(); + } + + + if (heightOfBillToAddress && heightOfShipToAddress) { + if (heightOfShipToAddress > heightOfBillToAddress) { + return customerInformationTop + 45 + heightOfShipToAddress; + } else { + return customerInformationTop + 45 + heightOfBillToAddress; + } + } + if (heightOfBillToAddress) { return customerInformationTop + 45 + heightOfBillToAddress; } + if (heightOfShipToAddress) { + return customerInformationTop + 45 + heightOfShipToAddress; + } + + return customerInformationTop; } \ No newline at end of file diff --git a/src/services/templates/packing-slips/basic/parts/customer-info-small.ts b/src/services/templates/packing-slips/basic/parts/customer-info-small.ts index 7df13b7..e82d5ed 100644 --- a/src/services/templates/packing-slips/basic/parts/customer-info-small.ts +++ b/src/services/templates/packing-slips/basic/parts/customer-info-small.ts @@ -20,7 +20,10 @@ export function generateCustomerInformation(doc, y, order: Order) { const customerInformationTop = y + 25; - doc + let heightOfBillToAddress: number | undefined; + + if (order.billing_address) { + doc .fontSize(6) .font("Bold") .text(`${t("packing-slip-bill-to", "Bill to")}:`, 25, customerInformationTop, {align: 'left'}) @@ -28,32 +31,48 @@ export function generateCustomerInformation(doc, y, order: Order) { .text(`${order.billing_address.first_name} ${order.billing_address.last_name}`, 25, customerInformationTop + 10, {align: 'left'}) .text(`${order.billing_address.city} ${order.billing_address.postal_code}`, 25, customerInformationTop + 20, {align: 'left'}) const billAddress = order.billing_address.address_1; - const heightOfBillToAddress = doc.heightOfString(billAddress) + heightOfBillToAddress = doc.heightOfString(billAddress) doc.text(billAddress, 25, customerInformationTop + 30 , {align: 'left'}) .moveDown(); - - const RIGHT_MARGIN = 90; - const RIGHT_WIDTH = 100; - doc - .fontSize(6) - .font("Bold") + } - doc - .text(`${t("packing-slip-ship-to", "Ship to")}:`, RIGHT_MARGIN, customerInformationTop, {align: 'right', width: RIGHT_WIDTH}) - .font("Regular") + let heightOfShipToAddress: number | undefined; - doc - .text(`${order.shipping_address.first_name} ${order.shipping_address.last_name}`, RIGHT_MARGIN, customerInformationTop + 10, {align: 'right', width: RIGHT_WIDTH}) - .text(`${order.shipping_address.city} ${order.shipping_address.postal_code}`, RIGHT_MARGIN, customerInformationTop + 20, {align: 'right', width: RIGHT_WIDTH}) - .moveDown(); - const shipAddress = order.shipping_address.address_1; - const heightOfShipToAddress = doc.heightOfString(shipAddress, { width: RIGHT_WIDTH }) - doc.text(shipAddress, RIGHT_MARGIN, customerInformationTop + 30 , {align: 'right', width: RIGHT_WIDTH }) - .moveDown(); + if (order.shipping_address) { + const RIGHT_MARGIN = 90; + const RIGHT_WIDTH = 100; + doc + .fontSize(6) + .font("Bold") + + doc + .text(`${t("packing-slip-ship-to", "Ship to")}:`, RIGHT_MARGIN, customerInformationTop, {align: 'right', width: RIGHT_WIDTH}) + .font("Regular") + + doc + .text(`${order.shipping_address.first_name} ${order.shipping_address.last_name}`, RIGHT_MARGIN, customerInformationTop + 10, {align: 'right', width: RIGHT_WIDTH}) + .text(`${order.shipping_address.city} ${order.shipping_address.postal_code}`, RIGHT_MARGIN, customerInformationTop + 20, {align: 'right', width: RIGHT_WIDTH}) + .moveDown(); + const shipAddress = order.shipping_address.address_1; + heightOfShipToAddress = doc.heightOfString(shipAddress, { width: RIGHT_WIDTH }) + doc.text(shipAddress, RIGHT_MARGIN, customerInformationTop + 30 , {align: 'right', width: RIGHT_WIDTH }) + .moveDown(); + } - if (heightOfShipToAddress > heightOfBillToAddress) { - return customerInformationTop + 30 + heightOfShipToAddress; - } else { + if (heightOfBillToAddress && heightOfShipToAddress) { + if (heightOfShipToAddress > heightOfBillToAddress) { + return customerInformationTop + 30 + heightOfShipToAddress; + } else { + return customerInformationTop + 30 + heightOfBillToAddress; + } + } + if (heightOfBillToAddress) { return customerInformationTop + 30 + heightOfBillToAddress; } + if (heightOfShipToAddress) { + return customerInformationTop + 30 + heightOfShipToAddress; + } + + return customerInformationTop; + } \ No newline at end of file diff --git a/src/services/templates/packing-slips/basic/parts/customer-info.ts b/src/services/templates/packing-slips/basic/parts/customer-info.ts index 912e039..e5bf238 100644 --- a/src/services/templates/packing-slips/basic/parts/customer-info.ts +++ b/src/services/templates/packing-slips/basic/parts/customer-info.ts @@ -20,34 +20,52 @@ export function generateCustomerInformation(doc, y, order: Order) { const customerInformationTop = y + 50; - doc - .fontSize(10) - .font("Bold") - .text(`${t("packing-slip-bill-to", "Bill to")}:`, 50, customerInformationTop, {align: 'left'}) - .font("Regular") - .text(`${order.billing_address.first_name} ${order.billing_address.last_name}`, 50, customerInformationTop + 15, {align: 'left'}) - .text(`${order.billing_address.city} ${order.billing_address.postal_code}`, 50, customerInformationTop + 30, {align: 'left'}) - const billAddress = order.billing_address.address_1; - const heightOfBillToAddress = doc.heightOfString(billAddress, { width: 150 }) - doc.text(billAddress, 50, customerInformationTop + 45 , {align: 'left', width: 150 }) - .moveDown(); - - doc - .fontSize(10) - .font("Bold") - .text(`${t("packing-slip-ship-to", "Ship to")}:`, 50, customerInformationTop, {align: 'right'}) - .font("Regular") - .text(`${order.shipping_address.first_name} ${order.shipping_address.last_name}`, 50, customerInformationTop + 15, {align: 'right'}) - .text(`${order.shipping_address.city} ${order.shipping_address.postal_code}`, 50, customerInformationTop + 30, {align: 'right'}) - .moveDown(); - const shipAddress = order.shipping_address.address_1; - const heightOfShipToAddress = doc.heightOfString(shipAddress, { width: 150 }) - doc.text(shipAddress, 360, customerInformationTop + 45 , {align: 'right', widdth: 150 }) - .moveDown(); - - if (heightOfShipToAddress > heightOfBillToAddress) { - return customerInformationTop + 45 + heightOfShipToAddress; - } else { + let heightOfBillToAddress: number | undefined; + + if (order.billing_address) { + doc + .fontSize(10) + .font("Bold") + .text(`${t("packing-slip-bill-to", "Bill to")}:`, 50, customerInformationTop, {align: 'left'}) + .font("Regular") + .text(`${order.billing_address.first_name} ${order.billing_address.last_name}`, 50, customerInformationTop + 15, {align: 'left'}) + .text(`${order.billing_address.city} ${order.billing_address.postal_code}`, 50, customerInformationTop + 30, {align: 'left'}) + const billAddress = order.billing_address.address_1; + heightOfBillToAddress = doc.heightOfString(billAddress, { width: 150 }) + doc.text(billAddress, 50, customerInformationTop + 45 , {align: 'left', width: 150 }) + .moveDown(); + } + + let heightOfShipToAddress: number | undefined; + + if (order.shipping_address) { + doc + .fontSize(10) + .font("Bold") + .text(`${t("packing-slip-ship-to", "Ship to")}:`, 50, customerInformationTop, {align: 'right'}) + .font("Regular") + .text(`${order.shipping_address.first_name} ${order.shipping_address.last_name}`, 50, customerInformationTop + 15, {align: 'right'}) + .text(`${order.shipping_address.city} ${order.shipping_address.postal_code}`, 50, customerInformationTop + 30, {align: 'right'}) + .moveDown(); + const shipAddress = order.shipping_address.address_1; + heightOfShipToAddress = doc.heightOfString(shipAddress, { width: 150 }) + doc.text(shipAddress, 360, customerInformationTop + 45 , {align: 'right', widdth: 150 }) + .moveDown(); + } + + if (heightOfBillToAddress && heightOfShipToAddress) { + if (heightOfShipToAddress > heightOfBillToAddress) { + return customerInformationTop + 45 + heightOfShipToAddress; + } else { + return customerInformationTop + 45 + heightOfBillToAddress; + } + } + if (heightOfBillToAddress) { return customerInformationTop + 45 + heightOfBillToAddress; } + if (heightOfShipToAddress) { + return customerInformationTop + 45 + heightOfShipToAddress; + } + + return customerInformationTop; } \ No newline at end of file