<commit_before>module Spree
  module Core
    module Importer
      class Order

        def self.import(user, params)
          begin
            ensure_country_id_from_params params[:ship_address_attributes]
            ensure_state_id_from_params params[:ship_address_attributes]
            ensure_country_id_from_params params[:bill_address_attributes]
            ensure_state_id_from_params params[:bill_address_attributes]

            order = Spree::Order.create!
            order.associate_user!(user)

            create_shipments_from_params(params.delete(:shipments_attributes), order)
            create_line_items_from_params(params.delete(:line_items_attributes),order)
            create_adjustments_from_params(params.delete(:adjustments_attributes), order)
            create_payments_from_params(params.delete(:payments_attributes), order)


            if(completed_at = params.delete(:completed_at))
              order.completed_at = completed_at
              order.state = 'complete'
            end

            user_id = params.delete(:user_id)
            if user.has_spree_role? :admin
              order.user_id = user_id
            end

            order.update_attributes!(params)
            # Really ensure that the order totals are correct
            order.update_totals
            order.persist_totals
            order.reload
          rescue Exception => e
            order.destroy if order && order.persisted?
            raise e.message
          end
        end

        def self.create_shipments_from_params(shipments_hash, order)
          return [] unless shipments_hash
          shipments_hash.each do |s|
            begin
              shipment = order.shipments.build
              shipment.tracking = s[:tracking]
              shipment.stock_location = Spree::StockLocation.find_by_name!(s[:stock_location])

              inventory_units = s[:inventory_units] || []
              inventory_units.each do |iu|
                ensure_variant_id_from_params(iu)

                unit = shipment.inventory_units.build<commit_msg>Check for has_spree_role? using a string 'admin'<commit_after>module Spree
  module Core
    module Importer
      class Order

        def self.import(user, params)
          begin
            ensure_country_id_from_params params[:ship_address_attributes]
            ensure_state_id_from_params params[:ship_address_attributes]
            ensure_country_id_from_params params[:bill_address_attributes]
            ensure_state_id_from_params params[:bill_address_attributes]

            order = Spree::Order.create!
            order.associate_user!(user)

            create_shipments_from_params(params.delete(:shipments_attributes), order)
            create_line_items_from_params(params.delete(:line_items_attributes),order)
            create_adjustments_from_params(params.delete(:adjustments_attributes), order)
            create_payments_from_params(params.delete(:payments_attributes), order)


            if(completed_at = params.delete(:completed_at))
              order.completed_at = completed_at
              order.state = 'complete'
            end

            user_id = params.delete(:user_id)
            if user.has_spree_role? "admin"
              order.user_id = user_id
            end

            order.update_attributes!(params)
            # Really ensure that the order totals are correct
            order.update_totals
            order.persist_totals
            order.reload
          rescue Exception => e
            order.destroy if order && order.persisted?
            raise e.message
          end
        end

        def self.create_shipments_from_params(shipments_hash, order)
          return [] unless shipments_hash
          shipments_hash.each do |s|
            begin
              shipment = order.shipments.build
              shipment.tracking = s[:tracking]
              shipment.stock_location = Spree::StockLocation.find_by_name!(s[:stock_location])

              inventory_units = s[:inventory_units] || []
              inventory_units.each do |iu|
                ensure_variant_id_from_params(iu)

                unit = shipment.inventory_units.build
{'fraction_non_alphanumeric': 0.06711089957163256, 'fraction_numerical': 0.0, 'mean_word_length': 2.1961977186311787, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4653126', 'n_tokens_mistral': 1114, 'n_tokens_neox': 1075, 'n_words': 189}