Class: SolidusBactracs::Api::ShipmentSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/solidus_bactracs/api/shipment_serializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(shipment:) ⇒ ShipmentSerializer

Returns a new instance of ShipmentSerializer.



7
8
9
10
11
# File 'lib/solidus_bactracs/api/shipment_serializer.rb', line 7

def initialize(shipment:)
  @shipment = shipment
  @config = SolidusBactracs.config
  @property_id = ::Spree::Property.find_by(name: SolidusBactracs.config.default_property_name)&.id
end

Instance Method Details

#call(sguid: nil) ⇒ Object



13
14
15
16
17
18
19
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/solidus_bactracs/api/shipment_serializer.rb', line 13

def call(sguid: nil)
  order = @shipment.order
  user = @shipment.user

  xml = Builder::XmlMarkup.new
  xml.instruct!(:xml, :encoding => "UTF-8")

  xml.soap(:Envelope, {"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema", "xmlns:soap" => "http://schemas.xmlsoap.org/soap/envelope/"}) do
    xml.soap :Body do
      xml.CreateNew({"xmlns" => "http://bactracs.andlor.com/rmaservice"}) do
        xml.sGuid                       sguid
        xml.NewRMA {
          xml.RMANumber                 @shipment.number
          xml.RMATypeName               safe_rma_type
          xml.RMASubTypeName
          xml.CustomerRef
          xml.InboundShippingPriority
          xml.InboundTrackingNumber     @shipment.tracking

          xml.Ship {
            xml.Carrier                 @config.default_carrier
            xml.ShipMethod              @config.default_ship_method
            xml.ShipDate                @shipment.created_at.strftime(SolidusBactracs::ExportHelper::BACTRACS_DATE_FORMAT)
            xml.TrackingNumber          @shipment.tracking
            xml.SerialNumber            @shipment.number
            xml.Ud1
          }
          xml.Customer {
            SolidusBactracs::ExportHelper.bactracs_address(xml, order, :ship)
            SolidusBactracs::ExportHelper.bactracs_address(xml, order, :bill)
          }
          xml.Rep {
            xml.Code
            xml.Name                    user.full_name
            xml.Email                   user.email
          }

          xml.RMALines {
            @shipment.line_items.each do |line|
              product = line.product
              if product.respond_to?(:assembly?) && product.assembly?
                product.parts.each do |part|
                  next unless part.product.product_properties.where(property_id: @property_id).present?
                  line_items_xml(xml: xml, line_item: line, variant: part, order: order)
                end
              else
                line_items_xml(xml: xml, line_item: line, variant: line.variant, order: order)
              end
            end
          }
          xml.OrderDate     order.completed_at.strftime(SolidusBactracs::ExportHelper::BACTRACS_DATE_FORMAT)
          xml.CreateDate    @shipment.created_at.strftime(SolidusBactracs::ExportHelper::BACTRACS_DATE_FORMAT)
          xml.Status        @config.default_status
          xml.RMAId         @shipment.id
          xml.ClientGuid
        }
      end
    end
  end
  # Rails.logger.info(xml.to_s)
  @xml = xml.to_xml.sub(/<to_xml\/>$/,'')
  return @xml
end

#find_sku_variant(variant) ⇒ Object



113
114
115
# File 'lib/solidus_bactracs/api/shipment_serializer.rb', line 113

def find_sku_variant(variant)
  @config.sku_map[variant.sku].present? ? @config.sku_map[variant.sku] : variant.sku
end

#get_df_partObject



117
118
119
120
121
122
123
124
# File 'lib/solidus_bactracs/api/shipment_serializer.rb', line 117

def get_df_part
  df_part =
    if safe_rma_type == "4"
      @shipment.number
    else
      nil
    end rescue nil
end

#get_rp_locationObject



106
107
108
109
110
111
# File 'lib/solidus_bactracs/api/shipment_serializer.rb', line 106

def get_rp_location
  #Double verifing
  return nil if safe_rma_type == "4"

  @config.default_rp_location.call(@shipment)
end

#line_items_xml(xml: nil, line_item: nil, variant: nil, order: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/solidus_bactracs/api/shipment_serializer.rb', line 77

def line_items_xml(xml: nil, line_item: nil, variant: nil, order: nil)
  shipment_notice = @shipment.shipment_notice
  xml.RMALine {
    xml.DFItem                  find_sku_variant(variant)
    xml.DFModelNum              find_sku_variant(variant)
    xml.DFCategory
    xml.DFCategoryDescription
    xml.DFQuantity              line_item.quantity
    xml.DFUnitPrice             line_item.price
    xml.DFSerialNumbers
    xml.Ud1s
    xml.CurrentWarranties
    xml.DFComments
    xml.DFStatus                @shipment.state
    xml.PurchaseDate            order.completed_at.strftime(SolidusBactracs::ExportHelper::BACTRACS_DATE_FORMAT)
    xml.ServiceProvider         shipment_notice&.service
    xml.WarrantyRepair
    xml.RMALineTest
    xml.InboundShipWeight       variant.weight.to_f
    xml.RPLocation              get_rp_location
    xml.DFPart                  get_df_part
  }
end

#safe_rma_typeObject



101
102
103
104
# File 'lib/solidus_bactracs/api/shipment_serializer.rb', line 101

def safe_rma_type
  rma_type = @shipment.rma_type if (@shipment.respond_to?(:rma_type) && @shipment.rma_type.present?)
  rma_type ||= @config.evaluate_rma_type.call(@shipment)
end