Skip to content

Commit

Permalink
Billing or shipping address can be empty
Browse files Browse the repository at this point in the history
  • Loading branch information
radoslaw-sz committed Dec 2, 2024
1 parent 42fe950 commit e477e22
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 80 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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)",
Expand Down
77 changes: 48 additions & 29 deletions src/services/templates/invoices/basic/parts/customer-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,59 @@ 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'})
.font("Regular")
.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;

}
74 changes: 46 additions & 28 deletions src/services/templates/packing-slips/basic/parts/customer-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit e477e22

Please sign in to comment.