spree_core 5.4.0.beta2 → 5.4.0.beta4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/app/finders/spree/products/find.rb +1 -14
  3. data/app/jobs/spree/exports/generate_job.rb +1 -1
  4. data/app/jobs/spree/reports/generate_job.rb +1 -1
  5. data/app/models/concerns/spree/product_scopes.rb +32 -4
  6. data/app/models/spree/address.rb +0 -14
  7. data/app/models/spree/api_key.rb +8 -5
  8. data/app/models/spree/country.rb +2 -23
  9. data/app/models/spree/coupon_code.rb +6 -1
  10. data/app/models/spree/current.rb +3 -2
  11. data/app/models/spree/exports/coupon_codes.rb +18 -0
  12. data/app/models/spree/market.rb +9 -2
  13. data/app/models/spree/market_country.rb +17 -0
  14. data/app/models/spree/newsletter_subscriber.rb +0 -3
  15. data/app/models/spree/order.rb +2 -5
  16. data/app/models/spree/product.rb +3 -13
  17. data/app/models/spree/reimbursement.rb +0 -2
  18. data/app/models/spree/shipment.rb +0 -1
  19. data/app/models/spree/shipment_handler.rb +0 -2
  20. data/app/models/spree/taxon.rb +10 -10
  21. data/app/models/spree/variant.rb +1 -1
  22. data/app/models/spree/zone.rb +21 -9
  23. data/app/presenters/spree/csv/coupon_code_presenter.rb +31 -0
  24. data/app/services/spree/newsletter/subscribe.rb +2 -2
  25. data/app/subscribers/spree/invitation_email_subscriber.rb +1 -1
  26. data/config/locales/en.yml +1 -0
  27. data/lib/spree/core/engine.rb +2 -1
  28. data/lib/spree/core/version.rb +1 -1
  29. data/lib/spree/core.rb +0 -1
  30. data/lib/spree_core.rb +0 -1
  31. data/lib/tasks/cli.rake +50 -0
  32. metadata +7 -29
  33. data/app/models/concerns/spree/filter_param.rb +0 -21
  34. data/app/models/spree/newsletter_subscriber/emails.rb +0 -12
  35. data/app/models/spree/order/emails.rb +0 -24
  36. data/app/models/spree/reimbursement/emails.rb +0 -11
  37. data/app/models/spree/shipment/emails.rb +0 -12
  38. data/lib/generators/spree/cursor_rules/cursor_rules_generator.rb +0 -19
  39. data/lib/generators/spree/cursor_rules/templates/spree_rules.mdc +0 -385
  40. data/lib/normalize_string.rb +0 -18
  41. data/lib/spree/core/importer/order.rb +0 -244
  42. data/lib/spree/core/importer/product.rb +0 -67
  43. data/lib/spree/core/importer.rb +0 -9
@@ -1,244 +0,0 @@
1
- module Spree
2
- module Core
3
- module Importer
4
- class Order
5
- def self.import(user, params)
6
- Spree::Deprecation.warn('Spree::Core::Importer::Order is deprecated and will be removed in Spree 5.5. Please use `Spree::Imports::Order` instead.')
7
-
8
- ensure_country_id_from_params params[:ship_address_attributes]
9
- ensure_state_id_from_params params[:ship_address_attributes]
10
- ensure_country_id_from_params params[:bill_address_attributes]
11
- ensure_state_id_from_params params[:bill_address_attributes]
12
-
13
- create_params = params.slice :currency
14
- order = Spree::Order.create! create_params
15
- order.associate_user!(user)
16
-
17
- shipments_attrs = params.delete(:shipments_attributes)
18
-
19
- create_line_items_from_params(params.delete(:line_items_attributes), order)
20
- create_shipments_from_params(shipments_attrs, order)
21
- create_adjustments_from_params(params.delete(:adjustments_attributes), order)
22
- create_payments_from_params(params.delete(:payments_attributes), order)
23
-
24
- if completed_at = params.delete(:completed_at)
25
- order.completed_at = completed_at
26
- order.state = 'complete'
27
- end
28
-
29
- params.delete(:user_id) unless user.try(:has_spree_role?, 'admin') && params.key?(:user_id)
30
-
31
- order.update!(params)
32
-
33
- order.create_proposed_shipments unless shipments_attrs.present?
34
-
35
- # Really ensure that the order totals & states are correct
36
- order.updater.update
37
- if shipments_attrs.present?
38
- order.shipments.each_with_index do |shipment, index|
39
- shipment.update_columns(cost: shipments_attrs[index][:cost].to_f) if shipments_attrs[index][:cost].present?
40
- end
41
- end
42
- order.reload
43
- rescue StandardError => e
44
- order.destroy if order&.persisted?
45
- raise e.message
46
- end
47
-
48
- def self.create_shipments_from_params(shipments_hash, order)
49
- return [] unless shipments_hash
50
-
51
- shipments_hash.each do |s|
52
- shipment = order.shipments.build
53
- shipment.tracking = s[:tracking]
54
- shipment.stock_location = Spree::StockLocation.find_by(admin_name: s[:stock_location]) ||
55
- Spree::StockLocation.find_by!(name: s[:stock_location])
56
- inventory_units = create_inventory_units_from_order_and_params(order, s[:inventory_units])
57
-
58
- inventory_units.each do |inventory_unit|
59
- inventory_unit.shipment = shipment
60
-
61
- if s[:shipped_at].present?
62
- inventory_unit.pending = false
63
- inventory_unit.state = 'shipped'
64
- end
65
-
66
- inventory_unit.save!
67
- end
68
-
69
- if s[:shipped_at].present?
70
- shipment.shipped_at = s[:shipped_at]
71
- shipment.state = 'shipped'
72
- end
73
-
74
- shipment.save!
75
-
76
- shipping_method = Spree::ShippingMethod.find_by(name: s[:shipping_method]) ||
77
- Spree::ShippingMethod.find_by!(admin_name: s[:shipping_method])
78
- rate = shipment.shipping_rates.create!(shipping_method: shipping_method, cost: s[:cost])
79
-
80
- shipment.selected_shipping_rate_id = rate.id
81
- shipment.update_amounts
82
-
83
- adjustments = s.delete(:adjustments_attributes)
84
- create_adjustments_from_params(adjustments, order, shipment)
85
- rescue StandardError => e
86
- raise "Order import shipments: #{e.message} #{s}"
87
- end
88
- end
89
-
90
- def self.create_inventory_units_from_order_and_params(order, inventory_unit_params)
91
- inventory_unit_params.each_with_object([]) do |inventory_unit_param, inventory_units|
92
- ensure_variant_id_from_params(inventory_unit_param)
93
- existing = inventory_units.detect { |unit| unit.variant_id == inventory_unit_param[:variant_id] }
94
- if existing
95
- existing.quantity += 1
96
- else
97
- line_item = order.line_items.detect { |ln| ln.variant_id == inventory_unit_param[:variant_id] }
98
- inventory_units << InventoryUnit.new(line_item: line_item, order_id: order.id, variant: line_item.variant, quantity: 1)
99
- end
100
- end
101
- end
102
-
103
- def self.create_line_items_from_params(line_items, order)
104
- return {} unless line_items
105
-
106
- line_items.each do |line_item|
107
- adjustments = line_item.delete(:adjustments_attributes)
108
- extra_params = line_item.except(:variant_id, :quantity, :sku)
109
- line_item = ensure_variant_id_from_params(line_item)
110
- variant = Spree::Variant.find(line_item[:variant_id])
111
- line_item = Cart::AddItem.call(order: order, variant: variant, quantity: line_item[:quantity]).value
112
- # Raise any errors with saving to prevent import succeeding with line items
113
- # failing silently.
114
- if extra_params.present?
115
- line_item.update!(extra_params)
116
- else
117
- line_item.save!
118
- end
119
- create_adjustments_from_params(adjustments, order, line_item)
120
- rescue StandardError => e
121
- raise "Order import line items: #{e.message} #{line_item}"
122
- end
123
- end
124
-
125
- def self.create_adjustments_from_params(adjustments, order, adjustable = nil)
126
- return [] unless adjustments
127
-
128
- adjustments.each do |a|
129
- adjustment = (adjustable || order).adjustments.build(
130
- order: order,
131
- amount: a[:amount].to_f,
132
- label: a[:label],
133
- source_type: source_type_from_adjustment(a)
134
- )
135
- adjustment.save!
136
- adjustment.close!
137
- rescue StandardError => e
138
- raise "Order import adjustments: #{e.message} #{a}"
139
- end
140
- end
141
-
142
- def self.create_payments_from_params(payments_hash, order)
143
- return [] unless payments_hash
144
-
145
- payments_hash.each do |p|
146
- payment = order.payments.build order: order
147
- payment.amount = p[:amount].to_f
148
- # Order API should be using state as that's the normal payment field.
149
- # spree_wombat serializes payment state as status so imported orders should fall back to status field.
150
- payment.state = p[:state] || p[:status] || 'completed'
151
- payment.created_at = p[:created_at] if p[:created_at]
152
- payment.payment_method = Spree::PaymentMethod.find_by!(name: p[:payment_method])
153
- payment.source = create_source_payment_from_params(p[:source], payment) if p[:source]
154
- payment.save!
155
- rescue StandardError => e
156
- raise "Order import payments: #{e.message} #{p}"
157
- end
158
- end
159
-
160
- def self.create_source_payment_from_params(source_hash, payment)
161
- Spree::CreditCard.create(
162
- month: source_hash[:month],
163
- year: source_hash[:year],
164
- cc_type: source_hash[:cc_type],
165
- last_digits: source_hash[:last_digits],
166
- name: source_hash[:name],
167
- payment_method: payment.payment_method,
168
- gateway_customer_profile_id: source_hash[:gateway_customer_profile_id],
169
- gateway_payment_profile_id: source_hash[:gateway_payment_profile_id],
170
- imported: true
171
- )
172
- rescue StandardError => e
173
- raise "Order import source payments: #{e.message} #{source_hash}"
174
- end
175
-
176
- def self.ensure_variant_id_from_params(hash)
177
- sku = hash.delete(:sku)
178
- unless hash[:variant_id].present?
179
- hash[:variant_id] = Spree::Variant.active.find_by!(sku: sku).id
180
- end
181
- hash
182
- rescue ActiveRecord::RecordNotFound => e
183
- raise "Ensure order import variant: Variant w/SKU #{sku} not found."
184
- rescue StandardError => e
185
- raise "Ensure order import variant: #{e.message} #{hash}"
186
- end
187
-
188
- def self.ensure_country_id_from_params(address)
189
- return if address.nil? || address[:country_id].present? || address[:country].nil?
190
-
191
- begin
192
- search = {}
193
- if name = address[:country]['name']
194
- search[:name] = name
195
- elsif iso_name = address[:country]['iso_name']
196
- search[:iso_name] = iso_name.upcase
197
- elsif iso = address[:country]['iso']
198
- search[:iso] = iso.upcase
199
- elsif iso3 = address[:country]['iso3']
200
- search[:iso3] = iso3.upcase
201
- end
202
-
203
- address.delete(:country)
204
- address[:country_id] = Spree::Country.where(search).first!.id
205
- rescue StandardError => e
206
- raise "Ensure order import address country: #{e.message} #{search}"
207
- end
208
- end
209
-
210
- def self.ensure_state_id_from_params(address)
211
- return if address.nil? || address[:state_id].present? || address[:state].nil?
212
-
213
- begin
214
- search = {}
215
- if name = address[:state]['name']
216
- search[:name] = name
217
- elsif abbr = address[:state]['abbr']
218
- search[:abbr] = abbr.upcase
219
- end
220
-
221
- address.delete(:state)
222
- search[:country_id] = address[:country_id]
223
-
224
- if state = Spree::State.where(search).first
225
- address[:state_id] = state.id
226
- else
227
- address[:state_name] = search[:name] || search[:abbr]
228
- end
229
- rescue StandardError => e
230
- raise "Ensure order import address state: #{e.message} #{search}"
231
- end
232
- end
233
-
234
- def self.source_type_from_adjustment(adjustment)
235
- if adjustment[:tax]
236
- 'Spree::TaxRate'
237
- elsif adjustment[:promotion]
238
- 'Spree::PromotionAction'
239
- end
240
- end
241
- end
242
- end
243
- end
244
- end
@@ -1,67 +0,0 @@
1
- module Spree
2
- module Core
3
- module Importer
4
- class Product
5
- attr_reader :product, :product_attrs, :variants_attrs, :options_attrs, :store
6
-
7
- def initialize(product, product_params, options = {})
8
- Spree::Deprecation.warn('Spree::Core::Importer::Product is deprecated and will be removed in Spree 5.5. Please use `Spree::Imports::Product` instead.')
9
-
10
- @store = options[:store] || Spree::Store.default
11
- @product = product || Spree::Product.new(product_params)
12
- @product.stores << @store if @product.stores.exclude?(@store)
13
-
14
- @product_attrs = product_params.to_h
15
- @variants_attrs = (options[:variants_attrs] || []).map(&:to_h)
16
- @options_attrs = options[:options_attrs] || []
17
- end
18
-
19
- def create
20
- if product.save
21
- variants_attrs.each do |variant_attribute|
22
- # make sure the product is assigned before the options=
23
- product.variants.create({ product: product }.merge(variant_attribute))
24
- end
25
-
26
- set_up_options
27
- end
28
-
29
- product
30
- end
31
-
32
- def update
33
- if product.update(product_attrs)
34
- variants_attrs.each do |variant_attribute|
35
- # update the variant if the id is present in the payload
36
- if variant_attribute['id'].present?
37
- product.variants.find(variant_attribute['id']).update(variant_attribute)
38
- else
39
- # make sure the product is assigned before the options=
40
- product.variants.create({ product: product }.merge(variant_attribute))
41
- end
42
- end
43
-
44
- set_up_options
45
- end
46
-
47
- product
48
- end
49
-
50
- private
51
-
52
- def set_up_options
53
- options_attrs.each do |name|
54
- option_type = Spree::OptionType.where(name: name).first_or_initialize do |option_type|
55
- option_type.presentation = name
56
- option_type.save!
57
- end
58
-
59
- unless product.option_types.include?(option_type)
60
- product.option_types << option_type
61
- end
62
- end
63
- end
64
- end
65
- end
66
- end
67
- end
@@ -1,9 +0,0 @@
1
- module Spree
2
- module Core
3
- module Importer
4
- end
5
- end
6
- end
7
-
8
- require 'spree/core/importer/order'
9
- require 'spree/core/importer/product'