lib/zanders/order.rb
# Public: Create a new order
#
# items - Array of hashes containing item_number and quantity
# address - Hash of address
# purchase_order - internal identifier for the order
#
# Returns an order_number
Create a new order
items - Array of hashes containing item_number and quantity
address - Hash of address
purchase_order - internal identifier for the order


Create a new order items - Array of hashes containing item_number and quantity address - Hash of address purchase_order - internal identifier for the order
def create_order(items, address, purchase_order_number, details = {})
      order = build_order_data
      order_items   = Array.new
      order[:order] = Hash.new
      shipping_information = Array.new
      items.each do |item|
        order_items.push(item: [
          {
            key: 'itemNumber',
            value: item[:item_number],
            attributes!: { key: { 'xsi:type' => 'xsd:string' }, value: { 'xsi:type' => 'xsd:string' } }
          }, {
            key: 'quantity',
            value: item[:quantity],
            attributes!: { key: { 'xsi:type' => 'xsd:string' }, value: { 'xsi:type' => 'xsd:string' } }
          }, {
            key: 'allowBackOrder',
            value: false,
            attributes!: { key: { 'xsi:type' => 'xsd:string' }, value: { 'xsi:type' => 'xsd:boolean' } }
          }
        ])
      end
      shipping_code = (@options[:account] == :accessory ? 'UM' : 'UG')
      shipping_information.push(*[
        { key: 'shipDate', value: Time.now.strftime("%Y-%m-%d") },
        { key: 'shipViaCode', value: shipping_code },
        { key: 'purchaseOrderNumber', value: purchase_order_number }
      ])
      if address == nil
        shipping_information.push({key: 'shipToNo', value: '0001'})
      elsif address[:fflno].present?
        ship_to_number = Zanders::Address.ship_to_number(address, @options)
        if ship_to_number[:success]
          shipping_information.push({key: 'shipToNo', value: ship_to_number[:ship_to_number] })
        else
          return { success: false, error_code: ship_to_number[:error_code], error_message: ship_to_number[:error_message] }
        end
      else
        shipping_information.push(*[
          { key: 'shipToName',     value: address[:name]     },
          { key: 'shipToAddress1', value: address[:address1] },
          { key: 'shipToAddress2', value: address[:address2] },
          { key: 'shipToCity',     value: address[:city]     },
          { key: 'shipToState',    value: address[:state]    },
          { key: 'shipToZip',      value: address[:zip]      }
        ])
      end
      if details[:name]
        shipping_information.push(
          { key: 'shipInstructions', value: format_shipping_instructions(details[:name], details[:phone_number]) }
        )
      end
      order[:order][:item] = shipping_information
      order_items = {item: order_items, attributes!: { item: { "xsi:type" => "ns2:Map"} }}
      order[:order][:item].push({
        key: 'items',
        value: order_items,
        attributes!: {
          key: {
            "xsi:type" => "xsd:string"
          },
          value: {
            "enc:itemType"  => "ns2:Map[{order_items.count}]",
            "enc:arraySize" => order_items.count.to_s,
            "xsi:type"      => "enc:Array"
          }
        }
      })
      soap_response = soap_client(ORDER_API_URL).call(:create_order, message: order)
      hash = soap_response.body[:create_order_response][:return][:item]
      if hash.first[:value] == "0"
        {
          success:       true,
          order_number:  hash.find { |h| h[:key] == 'orderNumber' }&.dig(:value),
          removed_items: hash.find { |h| h[:key] == 'removedItems' }&.dig(:value, :item),
          raw_xml:       soap_response.to_xml
        }
      else
        {
          success:       false,
          error_code:    hash.first[:value],
          error_message: hash.last[:value]
        }
      end
    end
def create_order ( items , address , purchase_order_number , details = { } ) order = build_order_data order_items = Array . new order [ :order ] = Hash . new shipping_information = Array . new items . each do | item | order_items . push ( item : [ { key : 'itemNumber' , value : item [ :item_number ] , attributes! : { key : { 'xsi:type' => 'xsd:string' } , value : { 'xsi:type' => 'xsd:string' } } } , { key : 'quantity' , value : item [ :quantity ] , attributes! : { key : { 'xsi:type' => 'xsd:string' } , value : { 'xsi:type' => 'xsd:string' } } } , { key : 'allowBackOrder' , value : false , attributes! : { key : { 'xsi:type' => 'xsd:string' } , value : { 'xsi:type' => 'xsd:boolean' } } } ] ) end shipping_code = ( @options [ :account ] == :accessory ? 'UM' : 'UG' ) shipping_information . push ( * [ { key : 'shipDate' , value : Time . now . strftime ( "%Y-%m-%d" ) } , { key : 'shipViaCode' , value : shipping_code } , { key : 'purchaseOrderNumber' , value : purchase_order_number } ] ) if address == nil shipping_information . push ( { key : 'shipToNo' , value : '0001' } ) elsif address [ :fflno ] . present? ship_to_number = Zanders :: Address . ship_to_number ( address , @options ) if ship_to_number [ :success ] shipping_information . push ( { key : 'shipToNo' , value : ship_to_number [ :ship_to_number ] } ) else return { success : false , error_code : ship_to_number [ :error_code ] , error_message : ship_to_number [ :error_message ] } end else shipping_information . push ( * [ { key : 'shipToName' , value : address [ :name ] } , { key : 'shipToAddress1' , value : address [ :address1 ] } , { key : 'shipToAddress2' , value : address [ :address2 ] } , { key : 'shipToCity' , value : address [ :city ] } , { key : 'shipToState' , value : address [ :state ] } , { key : 'shipToZip' , value : address [ :zip ] } ] ) end if details [ :name ] shipping_information . push ( { key : 'shipInstructions' , value : format_shipping_instructions ( details [ :name ] , details [ :phone_number ] ) } ) end order [ :order ] [ :item ] = shipping_information order_items = { item : order_items , attributes! : { item : { "xsi:type" => "ns2:Map" } } } order [ :order ] [ :item ] . push ( { key : 'items' , value : order_items , attributes! : { key : { "xsi:type" => "xsd:string" } , value : { "enc:itemType" => "ns2:Map[#{order_items.count}]" , "enc:arraySize" => order_items . count . to_s , "xsi:type" => "enc:Array" } } } ) soap_response = soap_client ( ORDER_API_URL ) . call ( :create_order , message : order ) hash = soap_response . body [ :create_order_response ] [ :return ] [ :item ] if hash . first [ :value ] == "0" { success : true , order_number : hash . find { | h | h [ :key ] == 'orderNumber' } &. dig ( :value ) , removed_items : hash . find { | h | h [ :key ] == 'removedItems' } &. dig ( :value , :item ) , raw_xml : soap_response . to_xml } else { success : false , error_code : hash . first [ :value ] , error_message : hash . last [ :value ] } end end
Public: Create a new order
items - Array of hashes containing item_number and quantity
address - Hash of address
purchase_order - internal identifier for the order
Public : Create a new order items - Array of hashes containing item_number and quantity address - Hash of address purchase_order - internal identifier for the order
# NOTE-david # order(ns2 map) #   item #     key #     value(ns2map) #       item  - order item #       item  - order item #   item #     " #
items
<not_specific>
address
<not_specific>
purchase_order_number
<not_specific>
details
<not_specific>