class Object

Public Instance Methods

load_matkahuolto_delivery() click to toggle source
# File lib/controllers/spree/admin/checkout_controller_decorator.rb, line 5
def load_matkahuolto_delivery
  return unless ["delivery"].include? @order.state

  api = SpreeMatkahuolto::API::OfficeFinder.new(Rails.configuration.x.matkahuolto_username)
  @matkahuolto_destinations = api.find(@order.ship_address.address1, @order.ship_address.zipcode, @order.ship_address.city)
  @matkahuolto_selected_destination_code = Spree::MatkahuoltoShipment.where(order_id: @order.id).first.try(:destination_code)
end
matkahuolto_labels() click to toggle source
# File lib/controllers/spree/admin/orders_controller_decorator.rb, line 3
def matkahuolto_labels
  @order = Spree::Order.find_by(number:params[:id])
  return unless @order

  label_api = SpreeMatkahuolto::API::ShippingLabel.new(
    Rails.configuration.x.matkahuolto_username,
    Rails.configuration.x.matkahuolto_password,
    Rails.configuration.x.matkahuolto_test_mode,
    # ENV["#{Rails.env.upcase}_MATKAHUOLTO_USERNAME"],
    # ENV["#{Rails.env.upcase}_MATKAHUOLTO_PASSWORD"],
    # ENV["#{Rails.env.upcase}_MATKAHUOLTO_TEST_MODE"]
  )

  shipments = []

  @order.shipments.each do |s|
    # TODO: Change API Login
    m_shipment = SpreeMatkahuolto::Shipment.new(Rails.configuration.x.matkahuolto_username)

    if s.shipping_method.admin_name.include? "matkahuolto_lahella"
      destination_code = Spree::MatkahuoltoShipment.where(order_id: @order.id).first.try(:destination_code)

      if destination_code
        m_shipment.destination_place_code = destination_code
        m_shipment.product_code = "80"
      end
    end

    m_shipment.sender_reference = "Order: #{@order.number}"
    m_shipment.receiver_name = "#{s.address.first_name} #{s.address.last_name}"
    m_shipment.receiver_address = "#{s.address.address1} #{s.address.address2}"
    m_shipment.receiver_postal = "#{s.address.zipcode}"
    m_shipment.receiver_city = "#{s.address.city}"
    m_shipment.receiver_contact_name = "#{s.address.first_name} #{s.address.last_name}"
    m_shipment.receiver_contact_number = "#{s.address.phone}"
    m_shipment.receiver_email = "#{@order.email}"
    total_weight = 0.0
    s.line_items.each do |item|
      total_weight += item.try(:product).try(:weight)
    end
    if total_weight == 0.0
      total_weight = 1.0
    end
    m_shipment.weight = total_weight.to_s

    
    custom_s = SpreeMatkahuoltoCustomShipment.find_by(shipment_id:s.id)
    if custom_s and custom_s.label_file_name
      # m_shipment.message_type = "C"
      return send_file custom_s.label.path, type: "application/pdf"
    end

    shipments.push m_shipment
  end
  
  label = label_api.get_labels(shipments)

  @order.shipments.each do |s|
    custom_s = SpreeMatkahuoltoCustomShipment.find_by(shipment_id:s.id)
    if not custom_s
      custom_s = SpreeMatkahuoltoCustomShipment.create(shipment_id:s.id)
    end
    label_path = label[:path]
    label_file = File.open(label_path, 'r')
    custom_s.label = label_file
    custom_s.save
  end

  send_file label[:path], filename: label[:filename], type: "application/pdf"
end
save_matkahuolto_destination_shipment() click to toggle source
# File lib/controllers/spree/admin/checkout_controller_decorator.rb, line 13
def save_matkahuolto_destination_shipment
  return unless "update" == params[:action] && "delivery" == params[:state]

  destination_code = params[:matkahuolto_selected_destination_id]

  matkahuolto_shipment = Spree::MatkahuoltoShipment.where(order_id: @order.id).first
  # Delete record for order if not using matkahuolto lahella
  if matkahuolto_shipment && !destination_code
    matkahuolto_shipment.destroy
    return
  end

  unless matkahuolto_shipment
    matkahuolto_shipment = Spree::MatkahuoltoShipment.new
  end

  matkahuolto_shipment.destination_code = destination_code
  matkahuolto_shipment.order = @order
  matkahuolto_shipment.save
end