Created
January 3, 2018 19:28
-
-
Save cored/a308680a1019d546b653830b76b6adf7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Fulfillment | |
module BulkShipments | |
module Types | |
include Dry::Types.module | |
end | |
class PalletLabels < Dry::Struct | |
HEADER_PADDING = 20 | |
constructor_type :strict_with_defaults | |
class Address < Dry::Struct | |
def self.for(address) | |
new( | |
name: address.display_name, | |
address_1: address.address1, | |
city: address.city, | |
state: address.state, | |
zipcode: address.zipcode, | |
) | |
end | |
def to_s | |
<<-ADDRESS | |
#{name} | |
#{address_1} | |
#{city}, #{state}, #{zipcode} | |
ADDRESS | |
end | |
attribute :name, Types::String | |
attribute :address_1, Types::String | |
attribute :city, Types::String | |
attribute :state, Types::String | |
attribute :zipcode, Types::String | |
end | |
def self.for(bulk_shipment:, amount:) | |
delivery_window = "#{bulk_shipment | |
.ship_by_start.strftime('%B %dth, %Y')} - #{bulk_shipment | |
.ship_by_end.strftime('%B %dth, %Y')}" | |
purchase_order_number = bulk_shipment.order_number | |
new( | |
purchase_order_number: purchase_order_number, | |
shipping_address: Address.for(bulk_shipment.shipping_address), | |
order_placed: bulk_shipment.created_at.strftime("%m/%d/%y"), | |
delivery_window: delivery_window, | |
total_cartons: amount, | |
total_shipment_weight: bulk_shipment.total_weight, | |
filename: "bulk_shipment_#{purchase_order_number}_pallet_labels.pdf", | |
) | |
end | |
attribute :purchase_order_number, Types::String | |
attribute :order_placed, Types::String | |
attribute :delivery_window, Types::String | |
attribute :from, Types::String.default("Casper Sleep Inc.") | |
attribute :shipping_address, Address | |
attribute :total_cartons, Types::Int | |
attribute :total_shipment_weight, Types::Int | |
attribute :total_shipment_volume, Types::Float.default(0.0) | |
attribute :filename, Types::String | |
def to_h | |
super.except(:filename) | |
end | |
def to_pdf | |
top_header | |
order_details | |
address_details | |
total_details | |
pdf_document.render | |
end | |
private | |
def top_header | |
pdf_document.stroke_horizontal_rule | |
header_padding do | |
pdf_document.text "PO# #{purchase_order_number}" | |
end | |
end | |
def order_details | |
pdf_document.stroke_horizontal_rule | |
header_padding do | |
pdf_document.text "Order Pickup: #{order_placed}" | |
pdf_document.text "Delivery Window: #{delivery_window}" | |
end | |
pdf_document.stroke_horizontal_rule | |
end | |
def address_details | |
header_padding do | |
pdf_document.text "From: #{from}" | |
end | |
shipping_address_details | |
pdf_document.stroke_horizontal_rule | |
end | |
def shipping_address_details | |
header_padding do | |
pdf_document.text "Shipping Address:" | |
pdf_document.text shipping_address.to_s | |
end | |
pdf_document.stroke_horizontal_rule | |
end | |
def total_details | |
header_padding do | |
pdf_document.text "Total Cartons: #{total_cartons}" | |
pdf_document.text "Total shipment weight: #{total_shipment_weight} pounds" | |
pdf_document.text "Total shipment volume: #{total_shipment_volume} cuFt" | |
end | |
end | |
def header_padding | |
pdf_document.pad(HEADER_PADDING) { yield } | |
end | |
def pdf_document | |
@pdf_document ||= Prawn::Document.new | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment