Class: FriendlyShipping::Services::UpsJson::GenerateLabelsPayload

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/services/ups_json/generate_labels_payload.rb

Class Method Summary collapse

Class Method Details

.add_label_specification(payload, options) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 188

def add_label_specification(payload, options)
  payload[:ShipmentRequest][:LabelSpecification] = {
    LabelImageFormat: {
      Code: options.label_format
    },
    LabelStockSize: {
      Width: options.label_size[0].to_s, # "Valid value is 4."
      Height: options.label_size[1].to_s # "Only valid values are 6 or 8."
    }
  }
end

.add_packages(payload, shipment, options) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 172

def add_packages(payload, shipment, options)
  payload[:ShipmentRequest][:Shipment][:Package] = shipment.packages.map do |package|
    package_options = options.options_for_package(package)
    delivery_confirmation_code = package_level_delivery_confirmation?(shipment) ? package_options.delivery_confirmation_code : nil

    GeneratePackageHash.call(
      package: package,
      delivery_confirmation_code: delivery_confirmation_code,
      shipper_release: package_options.shipper_release,
      declared_value: package_options.declared_value,
      package_flavor: 'labels',
      reference_numbers: package_options.reference_numbers
    )
  end
end

.apply_international_forms_options(payload, shipment, options) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 72

def apply_international_forms_options(payload, shipment, options)
  if options.terms_of_shipment_code == "DDP"
    payload[:ShipmentRequest][:Shipment][:PaymentInformation][:ShipmentCharge].append(build_ddp_billing_info(options))
  end

  if options.paperless_invoice
    payload[:ShipmentRequest][:Shipment][:ShipmentServiceOptions][:InternationalForms] = international_forms(shipment, options)
  end

  sold_to_location = options.sold_to || shipment.destination

  international_forms_additions = {}
  international_forms_additions[:Contacts] = { SoldTo: GenerateAddressHash.call(location: sold_to_location, international: true) }
  international_forms_additions[:Contacts][:SoldTo].merge(
    {
      Phone: { Number: sold_to_location.phone },
      TaxIdentificationNumber: sold_to_location.try(:tax_id_number),
      EmailAddress: sold_to_location.email
    }
  ).compact
  payload[:ShipmentRequest][:Shipment][:ShipmentServiceOptions][:InternationalForms].merge!(international_forms_additions)

  return unless shipment.origin.country.code == "US" && ["CA", "PR"].include?(shipment.destination.country.code)

  # Required for shipments from the US to Canada or Puerto Rico
  # We'll assume USD as the origin country is the United States here.
  total_value = shipment.packages.inject(Money.new(0, "USD")) do |shipment_sum, package|
    shipment_sum + package.items.inject(Money.new(0, "USD")) do |package_sum, item|
      package_sum + (item.cost || Money.new(0, "USD"))
    end
  end
  payload[:ShipmentRequest][:Shipment][:InvoiceLineTotal] = {
    CurrencyCode: total_value.currency.iso_code,
    MonetaryValue: total_value.to_f.to_s
  }
end

.apply_options(payload, options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 54

def apply_options(payload, options)
  if options.customer_context.present?
    payload[:ShipmentRequest][:Request][:TransactionReference] = { CustomerContext: options.customer_context }
  end

  if options.negotiated_rates
    payload[:ShipmentRequest][:Shipment][:ShipmentRatingOptions] = { NegotiatedRatesIndicator: "X" }
  end

  payload[:ShipmentRequest][:Shipment][:ShipmentServiceOptions] = {
    UPScarbonneutralIndicator: options.carbon_neutral ? "X" : nil,
    SaturdayDeliveryIndicator: options.saturday_delivery ? "X" : nil,
    LabelDelivery: {
      LabelLinksIndicator: "X"
    }
  }.compact
end

.build_ddp_billing_info(options) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 113

def build_ddp_billing_info(options)
  billing_options = options.billing_options
  {
    Type: "02", # Duties and taxes
    BillThirdParty: {
      AccountNumber: billing_options.,
      Address: {
        PostalCode: billing_options.billing_zip,
        CountryCode: billing_options.billing_country
      }
    }
  }
end

.call(shipment:, options:) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 11

def call(shipment:, options:)
  payload = initialize_payload(shipment, options)
  apply_options(payload, options)
  apply_international_forms_options(payload, shipment, options) if international?(shipment)
  add_packages(payload, shipment, options)
  add_label_specification(payload, options)
  payload.compact
end

.initialize_payload(shipment, options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 20

def initialize_payload(shipment, options)
  contents_description = shipment.packages.flat_map do |package|
    package.items.map(&:description)
  end.compact.uniq.join(', ').slice(0, 50)

  {
    ShipmentRequest: {
      Request: {
        RequestOption: options.validate_address ? "validate" : "nonvalidate",
        SubVersion: options.sub_version
      },
      Shipment: {
        Description: contents_description,
        Service: {
          Code: options.shipping_method.service_code
        },
        Shipper: GenerateAddressHash.call(location: options.shipper || shipment.origin, international: international?(shipment), shipper_number: options.shipper_number),
        ShipTo: GenerateAddressHash.call(location: shipment.destination, international: international?(shipment)),
        ShipmentDate: Time.current.strftime("%Y%m%d"),
        PaymentInformation: {
          ShipmentCharge: [
            {
              Type: "01", # Transportation
              BillShipper: {
                AccountNumber: options.shipper_number
              }
            }
          ]
        }
      }
    }
  }
end

.international?(shipment) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 109

def international?(shipment)
  shipment.origin.country != shipment.destination.country
end

.international_forms(shipment, options) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 127

def international_forms(shipment, options)
  invoice_date = options.invoice_date || Date.current
  result = {
    FormType: "01", # 01 is "Invoice"
    InvoiceDate: invoice_date.strftime("%Y%m%d"),
    ReasonForExport: options.reason_for_export,
    CurrencyCode: options.billing_options.currency || "USD",
    DeclarationStatement: options.declaration_statement,
    TermsOfShipment: options.terms_of_shipment_code,
    Product: []
  }.compact

  all_items = shipment.packages.map(&:items).map(&:to_a).flatten
  all_item_options = shipment.packages.flat_map do |package|
    package_options = options.options_for_package(package)
    package.items.flat_map do |item|
      package_options.options_for_item(item)
    end
  end

  all_items.group_by(&:description).each do |description, items|
    # This is a group of identically described items
    reference_item = items.first
    cost = reference_item.cost || Money.new(0, "USD")
    # Get the options for this item
    item_options = all_item_options.detect { |o| o.item_id == reference_item.id } || LabelItemOptions.new(item_id: nil)

    product_hash = {
      Description: description&.slice(0, 35), # Description of the product. Applies to all International Forms. Optional for Partial Invoice. Must be present at least once and can occur for a maximum of 3 times.
      CommodityCode: item_options.commodity_code,
      OriginCountryCode: item_options.country_of_origin || shipment.origin.country.code,
      Unit: {
        Number: items.length.to_s,
        UnitOfMeasurement: {
          Code: item_options.product_unit_of_measure_code
        },
        Value: cost.to_d
      }
    }

    result[:Product] << product_hash
  end
  result
end

.package_level_delivery_confirmation?(shipment) ⇒ Boolean

For certain origin/destination pairs, UPS allows each package in a shipment to have a specified delivery_confirmation option otherwise the delivery_confirmation option must be specified on the entire shipment. See https://developer.ups.com/api/reference/rating/appendix?loc=en_US for details

Returns:

  • (Boolean)


203
204
205
206
# File 'lib/friendly_shipping/services/ups_json/generate_labels_payload.rb', line 203

def package_level_delivery_confirmation?(shipment)
  origin, destination = shipment.origin.country.code, shipment.destination.country.code
  origin == destination || [['US', 'PR'], ['PR', 'US']].include?([origin, destination])
end