Class: Spree::Core::Importer::Order
- Inherits:
-
Object
- Object
- Spree::Core::Importer::Order
- Defined in:
- lib/spree/core/importer/order.rb
Class Method Summary collapse
- .create_adjustments_from_params(adjustments, order) ⇒ Object
- .create_line_items_from_params(line_items_hash, order) ⇒ Object
- .create_payments_from_params(payments_hash, order) ⇒ Object
- .create_shipments_from_params(shipments_hash, order) ⇒ Object
- .create_source_payment_from_params(source_hash, payment) ⇒ Object
- .ensure_country_id_from_params(address) ⇒ Object
- .ensure_state_id_from_params(address) ⇒ Object
- .ensure_variant_id_from_params(hash) ⇒ Object
- .import(user, params) ⇒ Object
Class Method Details
.create_adjustments_from_params(adjustments, order) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/spree/core/importer/order.rb', line 105 def self.create_adjustments_from_params(adjustments, order) return [] unless adjustments adjustments.each do |a| begin adjustment = order.adjustments.build(:amount => a[:amount].to_f, :label => a[:label]) adjustment.save! adjustment.close! rescue Exception => e raise "Order import adjustments: #{e.} #{a}" end end end |
.create_line_items_from_params(line_items_hash, order) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/spree/core/importer/order.rb', line 89 def self.create_line_items_from_params(line_items_hash, order) return {} unless line_items_hash line_items_hash.each_key do |k| begin line_item = line_items_hash[k] ensure_variant_id_from_params(line_item) extra_params = line_item.except(:variant_id, :quantity) line_item = order.contents.add(Spree::Variant.find(line_item[:variant_id]), line_item[:quantity]) line_item.update_attributes(extra_params) unless extra_params.empty? rescue Exception => e raise "Order import line items: #{e.} #{line_item}" end end end |
.create_payments_from_params(payments_hash, order) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/spree/core/importer/order.rb', line 119 def self.create_payments_from_params(payments_hash, order) return [] unless payments_hash payments_hash.each do |p| begin payment = order.payments.build order: order payment.amount = p[:amount].to_f # Order API should be using state as that's the normal payment field. # spree_wombat serializes payment state as status so imported orders should fall back to status field. payment.state = p[:state] || p[:status] || 'completed' payment.payment_method = Spree::PaymentMethod.find_by_name!(p[:payment_method]) payment.source = create_source_payment_from_params(p[:source], payment) if p[:source] payment.save! rescue Exception => e raise "Order import payments: #{e.} #{p}" end end end |
.create_shipments_from_params(shipments_hash, order) ⇒ Object
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 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/spree/core/importer/order.rb', line 39 def self.create_shipments_from_params(shipments_hash, order) return [] unless shipments_hash line_items = order.line_items 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 unit.order = order # Spree expects a Inventory Unit to always reference a line # item and variant otherwise users might get exceptions when # trying to view these units. Note the Importer might not be # able to find the line item if line_item.variant_id |= iu.variant_id unit.variant_id = iu[:variant_id] unit.line_item_id = line_items.select do |l| l.variant_id.to_i == iu[:variant_id].to_i end.first.try(:id) end # Mark shipped if it should be. if s[:shipped_at].present? shipment.shipped_at = s[:shipped_at] shipment.state = 'shipped' shipment.inventory_units.each do |unit| unit.state = 'shipped' end end shipment.save! shipping_method = Spree::ShippingMethod.find_by_name(s[:shipping_method]) || Spree::ShippingMethod.find_by_admin_name!(s[:shipping_method]) rate = shipment.shipping_rates.create!(:shipping_method => shipping_method, :cost => s[:cost]) shipment.selected_shipping_rate_id = rate.id shipment.update_amounts rescue Exception => e raise "Order import shipments: #{e.} #{s}" end end end |
.create_source_payment_from_params(source_hash, payment) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/spree/core/importer/order.rb', line 137 def self.create_source_payment_from_params(source_hash, payment) begin Spree::CreditCard.create( month: source_hash[:month], year: source_hash[:year], cc_type: source_hash[:cc_type], last_digits: source_hash[:last_digits], name: source_hash[:name], payment_method: payment.payment_method, gateway_customer_profile_id: source_hash[:gateway_customer_profile_id], gateway_payment_profile_id: source_hash[:gateway_payment_profile_id], imported: true ) rescue Exception => e raise "Order import source payments: #{e.} #{source_hash}" end end |
.ensure_country_id_from_params(address) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/spree/core/importer/order.rb', line 166 def self.ensure_country_id_from_params(address) return if address.nil? or address[:country_id].present? or address[:country].nil? begin search = {} if name = address[:country]['name'] search[:name] = name elsif iso_name = address[:country]['iso_name'] search[:iso_name] = iso_name.upcase elsif iso = address[:country]['iso'] search[:iso] = iso.upcase elsif iso3 = address[:country]['iso3'] search[:iso3] = iso3.upcase end address.delete(:country) address[:country_id] = Spree::Country.where(search).first!.id rescue Exception => e raise "Ensure order import address country: #{e.} #{search}" end end |
.ensure_state_id_from_params(address) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/spree/core/importer/order.rb', line 189 def self.ensure_state_id_from_params(address) return if address.nil? or address[:state_id].present? or address[:state].nil? begin search = {} if name = address[:state]['name'] search[:name] = name elsif abbr = address[:state]['abbr'] search[:abbr] = abbr.upcase end address.delete(:state) search[:country_id] = address[:country_id] if state = Spree::State.where(search).first address[:state_id] = state.id else address[:state_name] = search[:name] || search[:abbr] end rescue Exception => e raise "Ensure order import address state: #{e.} #{search}" end end |
.ensure_variant_id_from_params(hash) ⇒ Object
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/spree/core/importer/order.rb', line 155 def self.ensure_variant_id_from_params(hash) begin unless hash[:variant_id].present? hash[:variant_id] = Spree::Variant.active.find_by_sku!(hash[:sku]).id hash.delete(:sku) end rescue Exception => e raise "Ensure order import variant: #{e.} #{hash}" end end |
.import(user, params) ⇒ Object
6 7 8 9 10 11 12 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 |
# File 'lib/spree/core/importer/order.rb', line 6 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_line_items_from_params(params.delete(:line_items_attributes),order) create_shipments_from_params(params.delete(:shipments_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 params.delete(:user_id) unless user.try(:has_spree_role?, "admin") && params.key?(:user_id) order.update_attributes!(params) # Really ensure that the order totals & states are correct order.updater.update order.reload rescue Exception => e order.destroy if order && order.persisted? raise e. end end |