spree_core 3.0.10 → 3.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (289) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/spree.js.coffee.erb +12 -3
  3. data/app/helpers/spree/base_helper.rb +4 -1
  4. data/app/helpers/spree/products_helper.rb +37 -6
  5. data/app/mailers/spree/base_mailer.rb +11 -2
  6. data/app/models/concerns/spree/adjustment_source.rb +3 -10
  7. data/app/models/concerns/spree/default_price.rb +7 -1
  8. data/app/models/concerns/spree/named_type.rb +1 -1
  9. data/app/models/concerns/spree/user_api_authentication.rb +7 -1
  10. data/app/models/concerns/spree/user_methods.rb +47 -0
  11. data/app/models/concerns/spree/user_reporting.rb +2 -2
  12. data/app/models/concerns/spree/vat_price_calculation.rb +47 -0
  13. data/app/models/spree/address.rb +8 -7
  14. data/app/models/spree/adjustable/adjuster/base.rb +25 -0
  15. data/app/models/spree/adjustable/adjuster/promotion.rb +41 -0
  16. data/app/models/spree/adjustable/adjuster/tax.rb +26 -0
  17. data/app/models/spree/adjustable/adjustments_updater.rb +22 -45
  18. data/app/models/spree/adjustment.rb +8 -10
  19. data/app/models/spree/app_configuration.rb +5 -2
  20. data/app/models/spree/base.rb +4 -0
  21. data/app/models/spree/calculator.rb +0 -5
  22. data/app/models/spree/calculator/default_tax.rb +2 -10
  23. data/app/models/spree/classification.rb +7 -3
  24. data/app/models/spree/country.rb +14 -3
  25. data/app/models/spree/credit_card.rb +21 -31
  26. data/app/models/spree/customer_return.rb +7 -15
  27. data/app/models/spree/gateway.rb +7 -6
  28. data/app/models/spree/image.rb +1 -1
  29. data/app/models/spree/inventory_unit.rb +9 -6
  30. data/app/models/spree/legacy_user.rb +1 -6
  31. data/app/models/spree/line_item.rb +69 -68
  32. data/app/models/spree/log_entry.rb +1 -4
  33. data/app/models/spree/option_type.rb +15 -6
  34. data/app/models/spree/option_type_prototype.rb +9 -0
  35. data/app/models/spree/option_value.rb +11 -3
  36. data/app/models/spree/option_value_variant.rb +6 -0
  37. data/app/models/spree/order.rb +113 -64
  38. data/app/models/spree/order/checkout.rb +8 -11
  39. data/app/models/spree/order/currency_updater.rb +1 -1
  40. data/app/models/spree/order/store_credit.rb +96 -0
  41. data/app/models/spree/order_contents.rb +6 -1
  42. data/app/models/spree/order_inventory.rb +4 -8
  43. data/app/models/spree/order_promotion.rb +6 -0
  44. data/app/models/spree/order_updater.rb +2 -7
  45. data/app/models/spree/payment.rb +46 -19
  46. data/app/models/spree/payment/gateway_options.rb +8 -4
  47. data/app/models/spree/payment_method.rb +12 -13
  48. data/app/models/spree/payment_method/store_credit.rb +130 -0
  49. data/app/models/spree/preference.rb +1 -1
  50. data/app/models/spree/price.rb +16 -6
  51. data/app/models/spree/product.rb +52 -49
  52. data/app/models/spree/product/scopes.rb +7 -2
  53. data/app/models/spree/product_option_type.rb +7 -2
  54. data/app/models/spree/product_promotion_rule.rb +9 -0
  55. data/app/models/spree/product_property.rb +8 -10
  56. data/app/models/spree/promotion.rb +19 -19
  57. data/app/models/spree/promotion/rules/product.rb +3 -1
  58. data/app/models/spree/promotion/rules/taxon.rb +2 -1
  59. data/app/models/spree/promotion/rules/user.rb +4 -4
  60. data/app/models/spree/promotion_action.rb +3 -3
  61. data/app/models/spree/promotion_category.rb +1 -1
  62. data/app/models/spree/promotion_rule_taxon.rb +9 -0
  63. data/app/models/spree/promotion_rule_user.rb +9 -0
  64. data/app/models/spree/property.rb +2 -1
  65. data/app/models/spree/property_prototype.rb +9 -0
  66. data/app/models/spree/prototype.rb +8 -3
  67. data/app/models/spree/prototype_taxon.rb +9 -0
  68. data/app/models/spree/refund.rb +10 -7
  69. data/app/models/spree/refund_reason.rb +1 -1
  70. data/app/models/spree/reimbursement.rb +12 -16
  71. data/app/models/spree/reimbursement_type/reimbursement_helpers.rb +23 -6
  72. data/app/models/spree/reimbursement_type/store_credit.rb +28 -0
  73. data/app/models/spree/return_authorization.rb +8 -13
  74. data/app/models/spree/return_authorization_reason.rb +1 -1
  75. data/app/models/spree/return_item.rb +13 -12
  76. data/app/models/spree/return_item/eligibility_validator/time_since_purchase.rb +1 -1
  77. data/app/models/spree/role.rb +3 -2
  78. data/app/models/spree/role_user.rb +6 -0
  79. data/app/models/spree/shipment.rb +18 -23
  80. data/app/models/spree/shipment_handler.rb +2 -2
  81. data/app/models/spree/shipping_category.rb +6 -3
  82. data/app/models/spree/shipping_method.rb +11 -10
  83. data/app/models/spree/shipping_method_zone.rb +6 -0
  84. data/app/models/spree/shipping_rate.rb +16 -29
  85. data/app/models/spree/state.rb +3 -2
  86. data/app/models/spree/state_change.rb +1 -1
  87. data/app/models/spree/stock/content_item.rb +10 -12
  88. data/app/models/spree/stock/coordinator.rb +13 -14
  89. data/app/models/spree/stock/estimator.rb +28 -30
  90. data/app/models/spree/stock/inventory_unit_builder.rb +1 -9
  91. data/app/models/spree/stock/packer.rb +1 -1
  92. data/app/models/spree/stock/quantifier.rb +5 -5
  93. data/app/models/spree/stock/splitter/backordered.rb +2 -2
  94. data/app/models/spree/stock_item.rb +12 -18
  95. data/app/models/spree/stock_location.rb +4 -7
  96. data/app/models/spree/stock_movement.rb +11 -9
  97. data/app/models/spree/stock_transfer.rb +11 -12
  98. data/app/models/spree/store.rb +14 -6
  99. data/app/models/spree/store_credit.rb +252 -0
  100. data/app/models/spree/store_credit_category.rb +22 -0
  101. data/app/models/spree/store_credit_event.rb +38 -0
  102. data/app/models/spree/store_credit_type.rb +6 -0
  103. data/app/models/spree/tax_category.rb +3 -8
  104. data/app/models/spree/tax_rate.rb +56 -122
  105. data/app/models/spree/taxon.rb +11 -5
  106. data/app/models/spree/tracker.rb +12 -1
  107. data/app/models/spree/validations/db_maximum_length_validator.rb +2 -1
  108. data/app/models/spree/variant.rb +82 -50
  109. data/app/models/spree/zone.rb +21 -17
  110. data/app/models/spree/zone_member.rb +6 -0
  111. data/app/validators/db_maximum_length_validator.rb +11 -0
  112. data/app/views/layouts/spree/base_mailer.html.erb +38 -781
  113. data/app/views/spree/order_mailer/_adjustment.html.erb +8 -0
  114. data/app/views/spree/order_mailer/_subtotal.html.erb +8 -0
  115. data/app/views/spree/order_mailer/_total.html.erb +8 -0
  116. data/app/views/spree/order_mailer/cancel_email.html.erb +13 -28
  117. data/app/views/spree/order_mailer/cancel_email.text.erb +1 -1
  118. data/app/views/spree/order_mailer/confirm_email.html.erb +49 -63
  119. data/app/views/spree/order_mailer/confirm_email.text.erb +1 -1
  120. data/app/views/spree/shared/_base_mailer_header.html.erb +5 -7
  121. data/app/views/spree/shared/_base_mailer_stylesheets.html.erb +777 -0
  122. data/app/views/spree/shared/_error_messages.html.erb +1 -1
  123. data/app/views/spree/shared/_mailer_line_item.html.erb +12 -0
  124. data/app/views/spree/shipment_mailer/shipped_email.html.erb +21 -14
  125. data/app/views/spree/shipment_mailer/shipped_email.text.erb +3 -3
  126. data/config/initializers/user_class_extensions.rb +7 -38
  127. data/config/locales/en.yml +113 -13
  128. data/config/routes.rb +7 -0
  129. data/db/default/spree/default_reimbursement_type.rb +1 -1
  130. data/db/default/spree/zones.rb +4 -5
  131. data/db/migrate/20150118210639_create_spree_store_credits.rb +24 -0
  132. data/db/migrate/20150118211500_create_spree_store_credit_categories.rb +8 -0
  133. data/db/migrate/20150118212051_create_spree_store_credit_events.rb +17 -0
  134. data/db/migrate/20150118212101_create_spree_store_credit_types.rb +10 -0
  135. data/db/migrate/20150314013438_add_missing_indexes.rb +25 -0
  136. data/db/migrate/20150317174308_remove_duplicated_indexes_from_multi_columns.rb +18 -0
  137. data/db/migrate/20150324104002_remove_user_index_from_spree_state_changes.rb +14 -0
  138. data/db/migrate/20150522071831_add_position_to_spree_payment_methods.rb +5 -0
  139. data/db/migrate/20150626181949_add_taxable_adjustment_total_to_line_item.rb +19 -0
  140. data/db/migrate/20150627090949_migrate_payment_methods_display.rb +12 -0
  141. data/db/migrate/20150714154102_spree_payment_method_store_credits.rb +12 -0
  142. data/db/migrate/20150726141425_rename_has_and_belongs_to_associations_to_model_names.rb +18 -0
  143. data/db/migrate/20150727191614_spree_store_credit_types.rb +11 -0
  144. data/db/migrate/20150819154308_add_discontinued_to_products_and_variants.rb +68 -0
  145. data/db/migrate/20151220072838_remove_shipping_method_id_from_spree_orders.rb +13 -0
  146. data/db/migrate/20160207191757_add_id_column_to_earlier_habtm_tables.rb +16 -0
  147. data/db/migrate/20160219165458_add_indexes.rb +14 -0
  148. data/lib/generators/spree/dummy/templates/rails/database.yml +31 -24
  149. data/lib/generators/spree/dummy/templates/rails/test.rb +2 -1
  150. data/lib/spree/core.rb +16 -0
  151. data/lib/spree/core/controller_helpers/auth.rb +1 -1
  152. data/lib/spree/core/controller_helpers/common.rb +3 -3
  153. data/lib/spree/core/controller_helpers/order.rb +6 -5
  154. data/lib/spree/core/controller_helpers/search.rb +1 -1
  155. data/lib/spree/core/controller_helpers/store.rb +29 -0
  156. data/lib/spree/core/delegate_belongs_to.rb +2 -2
  157. data/lib/spree/core/engine.rb +30 -25
  158. data/lib/spree/core/environment.rb +1 -1
  159. data/lib/spree/core/importer/order.rb +37 -40
  160. data/lib/spree/core/number_generator.rb +52 -0
  161. data/lib/spree/core/product_filters.rb +1 -1
  162. data/lib/spree/core/search/base.rb +4 -3
  163. data/lib/spree/core/version.rb +1 -1
  164. data/lib/spree/localized_number.rb +3 -1
  165. data/lib/spree/permitted_attributes.rb +5 -2
  166. data/lib/spree/testing_support/common_rake.rb +3 -3
  167. data/lib/spree/testing_support/factories.rb +3 -3
  168. data/lib/spree/testing_support/factories/address_factory.rb +1 -1
  169. data/lib/spree/testing_support/factories/country_factory.rb +2 -2
  170. data/lib/spree/testing_support/factories/order_factory.rb +2 -2
  171. data/lib/spree/testing_support/factories/payment_factory.rb +5 -0
  172. data/lib/spree/testing_support/factories/payment_method_factory.rb +8 -0
  173. data/lib/spree/testing_support/factories/promotion_rule_factory.rb +5 -0
  174. data/lib/spree/testing_support/factories/refund_factory.rb +9 -1
  175. data/lib/spree/testing_support/factories/return_authorization_factory.rb +2 -0
  176. data/lib/spree/testing_support/factories/state_factory.rb +2 -2
  177. data/lib/spree/testing_support/factories/store_credit_category_factory.rb +9 -0
  178. data/lib/spree/testing_support/factories/store_credit_event_factory.rb +8 -0
  179. data/lib/spree/testing_support/factories/store_credit_factory.rb +17 -0
  180. data/lib/spree/testing_support/factories/store_credit_type_factory.rb +11 -0
  181. data/lib/spree/testing_support/factories/user_factory.rb +1 -1
  182. data/lib/spree/testing_support/factories/zone_member_factory.rb +6 -0
  183. data/lib/spree/testing_support/microdata.rb +189 -0
  184. data/lib/tasks/core.rake +68 -0
  185. data/lib/tasks/exchanges.rake +2 -2
  186. data/spec/fixtures/microdata.html +22 -0
  187. data/spec/fixtures/microdata_itemref.html +15 -0
  188. data/spec/fixtures/microdata_no_itemscope.html +20 -0
  189. data/spec/helpers/base_helper_spec.rb +64 -1
  190. data/spec/helpers/products_helper_spec.rb +75 -3
  191. data/spec/lib/i18n_spec.rb +2 -2
  192. data/spec/lib/search/base_spec.rb +2 -2
  193. data/spec/lib/spree/core/controller_helpers/auth_spec.rb +4 -2
  194. data/spec/lib/spree/core/controller_helpers/store_spec.rb +56 -0
  195. data/spec/lib/spree/core/importer/order_spec.rb +226 -123
  196. data/spec/lib/spree/core/number_generator_spec.rb +175 -0
  197. data/spec/lib/spree/core_spec.rb +23 -0
  198. data/spec/lib/spree/localized_number_spec.rb +10 -0
  199. data/spec/mailers/order_mailer_spec.rb +11 -13
  200. data/spec/mailers/shipment_mailer_spec.rb +26 -8
  201. data/spec/mailers/test_mailer_spec.rb +15 -1
  202. data/spec/models/option_type_prototype_spec.rb +9 -0
  203. data/spec/models/spree/ability_spec.rb +6 -13
  204. data/spec/models/spree/address_spec.rb +1 -1
  205. data/spec/models/spree/adjustable/adjuster/base_spec.rb +10 -0
  206. data/spec/models/spree/adjustable/adjuster/promotion_spec.rb +211 -0
  207. data/spec/models/spree/adjustable/adjuster/tax_spec.rb +86 -0
  208. data/spec/models/spree/adjustable/adjustments_updater_spec.rb +2 -262
  209. data/spec/models/spree/adjustment_spec.rb +27 -1
  210. data/spec/models/spree/app_configuration_spec.rb +5 -2
  211. data/spec/models/spree/calculator/default_tax_spec.rb +39 -14
  212. data/spec/models/spree/concerns/user_methods_spec.rb +55 -0
  213. data/spec/models/spree/concerns/vat_price_calculation_spec.rb +66 -0
  214. data/spec/models/spree/country_spec.rb +45 -8
  215. data/spec/models/spree/credit_card_spec.rb +8 -8
  216. data/spec/models/spree/customer_return_spec.rb +4 -26
  217. data/spec/models/spree/gateway_spec.rb +7 -0
  218. data/spec/models/spree/image_spec.rb +3 -0
  219. data/spec/models/spree/inventory_unit_spec.rb +1 -18
  220. data/spec/models/spree/line_item_spec.rb +78 -18
  221. data/spec/models/spree/option_type_spec.rb +2 -2
  222. data/spec/models/spree/option_value_spec.rb +8 -3
  223. data/spec/models/spree/order/checkout_spec.rb +49 -39
  224. data/spec/models/spree/order/currency_updater_spec.rb +3 -3
  225. data/spec/models/spree/order/finalizing_spec.rb +0 -3
  226. data/spec/models/spree/order/payment_spec.rb +1 -1
  227. data/spec/models/spree/order/state_machine_spec.rb +1 -6
  228. data/spec/models/spree/order/store_credit_spec.rb +423 -0
  229. data/spec/models/spree/order/updating_spec.rb +2 -2
  230. data/spec/models/spree/order_contents_spec.rb +42 -1
  231. data/spec/models/spree/order_inventory_spec.rb +27 -17
  232. data/spec/models/spree/order_spec.rb +65 -52
  233. data/spec/models/spree/payment/gateway_options_spec.rb +10 -2
  234. data/spec/models/spree/payment/store_credit_spec.rb +60 -0
  235. data/spec/models/spree/payment_method/store_credit_spec.rb +291 -0
  236. data/spec/models/spree/payment_method_spec.rb +22 -14
  237. data/spec/models/spree/payment_spec.rb +37 -44
  238. data/spec/models/spree/price_spec.rb +86 -0
  239. data/spec/models/spree/product/scopes_spec.rb +35 -0
  240. data/spec/models/spree/product_option_type_spec.rb +6 -2
  241. data/spec/models/spree/product_promotion_rule_spec.rb +9 -0
  242. data/spec/models/spree/product_property_spec.rb +11 -0
  243. data/spec/models/spree/product_spec.rb +82 -15
  244. data/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb +1 -1
  245. data/spec/models/spree/promotion/rules/user_spec.rb +8 -0
  246. data/spec/models/spree/promotion_action_spec.rb +1 -1
  247. data/spec/models/spree/promotion_rule_spec.rb +1 -1
  248. data/spec/models/spree/promotion_rule_taxon_spec.rb +9 -0
  249. data/spec/models/spree/promotion_rule_user_spec.rb +9 -0
  250. data/spec/models/spree/promotion_spec.rb +57 -36
  251. data/spec/models/spree/property_prototype_spec.rb +9 -0
  252. data/spec/models/spree/prototype_taxon_spec.rb +9 -0
  253. data/spec/models/spree/refund_reason_spec.rb +7 -0
  254. data/spec/models/spree/reimbursement_spec.rb +3 -30
  255. data/spec/models/spree/reimbursement_tax_calculator_spec.rb +17 -5
  256. data/spec/models/spree/reimbursement_type/store_credit_spec.rb +101 -0
  257. data/spec/models/spree/return_authorization_reason_spec.rb +7 -0
  258. data/spec/models/spree/return_authorization_spec.rb +2 -22
  259. data/spec/models/spree/return_item_spec.rb +50 -1
  260. data/spec/models/spree/returns_calculator_spec.rb +1 -1
  261. data/spec/models/spree/role_spec.rb +7 -0
  262. data/spec/models/spree/shipment_spec.rb +17 -17
  263. data/spec/models/spree/shipping_calculator_spec.rb +2 -2
  264. data/spec/models/spree/shipping_category_spec.rb +14 -0
  265. data/spec/models/spree/shipping_method_spec.rb +9 -2
  266. data/spec/models/spree/shipping_rate_spec.rb +40 -41
  267. data/spec/models/spree/state_spec.rb +12 -1
  268. data/spec/models/spree/stock/content_item_spec.rb +9 -0
  269. data/spec/models/spree/stock/estimator_spec.rb +56 -8
  270. data/spec/models/spree/stock/quantifier_spec.rb +61 -32
  271. data/spec/models/spree/stock_item_spec.rb +19 -1
  272. data/spec/models/spree/store_credit_event_spec.rb +101 -0
  273. data/spec/models/spree/store_credit_spec.rb +786 -0
  274. data/spec/models/spree/store_spec.rb +39 -11
  275. data/spec/models/spree/tax_category_spec.rb +6 -1
  276. data/spec/models/spree/tax_rate_spec.rb +204 -44
  277. data/spec/models/spree/user_spec.rb +105 -38
  278. data/spec/models/spree/variant_spec.rb +281 -9
  279. data/spec/models/spree/zone_member_spec.rb +38 -0
  280. data/spec/models/spree/zone_spec.rb +32 -8
  281. data/spec/spec_helper.rb +3 -0
  282. data/spec/support/concerns/{adjustment_source_spec.rb → adjustment_source.rb} +0 -0
  283. data/spec/support/concerns/{default_price_spec.rb → default_price.rb} +9 -0
  284. data/spec/validators/db_maximum_length_validator_spec.rb +22 -0
  285. data/spree_core.gemspec +5 -6
  286. metadata +99 -36
  287. data/CHANGELOG.md +0 -4
  288. data/app/models/concerns/spree/number_generator.rb +0 -39
  289. data/spec/models/spree/validations/db_maximum_length_validator_spec.rb +0 -24
@@ -20,26 +20,25 @@ module Spree
20
20
  packages = estimate_packages(packages)
21
21
  end
22
22
 
23
- # Build packages as per stock location
24
- #
25
- # It needs to check whether each stock location holds at least one stock
26
- # item for the order. In case none is found it wouldn't make any sense
27
- # to build a package because it would be empty. Plus we avoid errors down
28
- # the stack because it would assume the stock location has stock items
29
- # for the given order
30
- #
31
- # Returns an array of Package instances
32
23
  def build_packages(packages = Array.new)
33
- StockLocation.active.each do |stock_location|
34
- next unless stock_location.stock_items.where(:variant_id => inventory_units.map(&:variant_id).uniq).exists?
35
-
36
- packer = build_packer(stock_location, inventory_units)
37
- packages += packer.packages
24
+ stock_locations_with_requested_variants.each do |stock_location|
25
+ packages += build_packer(stock_location, inventory_units).packages
38
26
  end
27
+
39
28
  packages
40
29
  end
41
30
 
42
31
  private
32
+
33
+ def stock_locations_with_requested_variants
34
+ Spree::StockLocation.active.joins(:stock_items).
35
+ where(spree_stock_items: { variant_id: requested_variant_ids })
36
+ end
37
+
38
+ def requested_variant_ids
39
+ inventory_units.map(&:variant_id).uniq
40
+ end
41
+
43
42
  def prioritize_packages(packages)
44
43
  prioritizer = Prioritizer.new(inventory_units, packages)
45
44
  prioritizer.prioritized_packages
@@ -1,6 +1,8 @@
1
1
  module Spree
2
2
  module Stock
3
3
  class Estimator
4
+ include VatPriceCalculation
5
+
4
6
  attr_reader :order, :currency
5
7
 
6
8
  def initialize(order)
@@ -28,44 +30,40 @@ module Spree
28
30
  def calculate_shipping_rates(package, ui_filter)
29
31
  shipping_methods(package, ui_filter).map do |shipping_method|
30
32
  cost = shipping_method.calculator.compute(package)
31
- tax_category = shipping_method.tax_category
32
- if tax_category
33
- tax_rate = tax_category.tax_rates.detect do |rate|
34
- # If the rate's zone matches the order's zone, a positive adjustment will be applied.
35
- # If the rate is from the default tax zone, then a negative adjustment will be applied.
36
- # See the tests in shipping_rate_spec.rb for an example of this.d
37
- rate.zone == order.tax_zone || rate.zone.default_tax?
38
- end
39
- end
40
-
41
- if cost
42
- rate = shipping_method.shipping_rates.new(cost: cost)
43
- rate.tax_rate = tax_rate if tax_rate
44
- end
45
33
 
46
- rate
34
+ shipping_method.shipping_rates.new(
35
+ cost: gross_amount(cost, taxation_options_for(shipping_method)),
36
+ tax_rate: first_tax_rate_for(shipping_method.tax_category)
37
+ ) if cost
47
38
  end.compact
48
39
  end
49
40
 
41
+ # Override this if you need the prices for shipping methods to be handled just like the
42
+ # prices for products in terms of included tax manipulation.
43
+ #
44
+ def taxation_options_for(shipping_method)
45
+ {
46
+ tax_category: shipping_method.tax_category,
47
+ tax_zone: @order.tax_zone
48
+ }
49
+ end
50
+
51
+ def first_tax_rate_for(tax_category)
52
+ return unless @order.tax_zone && tax_category
53
+ Spree::TaxRate.for_tax_category(tax_category).
54
+ potential_rates_for_zone(@order.tax_zone).first
55
+ end
56
+
50
57
  def shipping_methods(package, display_filter)
51
58
  package.shipping_methods.select do |ship_method|
52
59
  calculator = ship_method.calculator
53
- begin
54
- ship_method.available_to_display(display_filter) &&
55
- ship_method.include?(order.ship_address) &&
56
- calculator.available?(package) &&
57
- (calculator.preferences[:currency].blank? ||
58
- calculator.preferences[:currency] == currency)
59
- rescue Exception => exception
60
- log_calculator_exception(ship_method, exception)
61
- end
62
- end
63
- end
64
60
 
65
- def log_calculator_exception(ship_method, exception)
66
- Rails.logger.info("Something went wrong calculating rates with the #{ship_method.name} (ID=#{ship_method.id}) shipping method.")
67
- Rails.logger.info("*" * 50)
68
- Rails.logger.info(exception.backtrace.join("\n"))
61
+ ship_method.available_to_display(display_filter) &&
62
+ ship_method.include?(order.ship_address) &&
63
+ calculator.available?(package) &&
64
+ (calculator.preferences[:currency].blank? ||
65
+ calculator.preferences[:currency] == currency)
66
+ end
69
67
  end
70
68
  end
71
69
  end
@@ -8,15 +8,7 @@ module Spree
8
8
  def units
9
9
  @order.line_items.flat_map do |line_item|
10
10
  line_item.quantity.times.map do |i|
11
- @order.inventory_units.includes(
12
- variant: {
13
- product: {
14
- shipping_category: {
15
- shipping_methods: [:calculator, { zones: :zone_members }]
16
- }
17
- }
18
- }
19
- ).build(
11
+ @order.inventory_units.build(
20
12
  pending: true,
21
13
  variant: line_item.variant,
22
14
  line_item: line_item,
@@ -24,7 +24,7 @@ module Spree
24
24
  if variant.should_track_inventory?
25
25
  next unless stock_location.stock_item(variant)
26
26
 
27
- on_hand, backordered = stock_location.fill_status(variant, units.count)
27
+ on_hand, backordered = stock_location.fill_status(variant, units.size)
28
28
  package.add_multiple units.slice!(0, on_hand), :on_hand if on_hand > 0
29
29
  package.add_multiple units.slice!(0, backordered), :backordered if backordered > 0
30
30
  else
@@ -1,15 +1,15 @@
1
1
  module Spree
2
2
  module Stock
3
3
  class Quantifier
4
- attr_reader :stock_items
4
+ attr_reader :stock_items, :variant
5
5
 
6
6
  def initialize(variant)
7
7
  @variant = variant
8
- @stock_items = Spree::StockItem.joins(:stock_location).where(variant_id: @variant, Spree::StockLocation.table_name => { active: true })
8
+ @stock_items = @variant.stock_items.with_active_stock_location
9
9
  end
10
10
 
11
11
  def total_on_hand
12
- if @variant.should_track_inventory?
12
+ if variant.should_track_inventory?
13
13
  stock_items.sum(:count_on_hand)
14
14
  else
15
15
  Float::INFINITY
@@ -20,8 +20,8 @@ module Spree
20
20
  stock_items.any?(&:backorderable)
21
21
  end
22
22
 
23
- def can_supply?(required)
24
- total_on_hand >= required || backorderable?
23
+ def can_supply?(required = 1)
24
+ variant.available? && (total_on_hand >= required || backorderable?)
25
25
  end
26
26
 
27
27
  end
@@ -6,11 +6,11 @@ module Spree
6
6
  def split(packages)
7
7
  split_packages = []
8
8
  packages.each do |package|
9
- if package.on_hand.count > 0
9
+ if package.on_hand.size > 0
10
10
  split_packages << build_package(package.on_hand)
11
11
  end
12
12
 
13
- if package.backordered.count > 0
13
+ if package.backordered.size > 0
14
14
  split_packages << build_package(package.backordered)
15
15
  end
16
16
  end
@@ -2,39 +2,37 @@ module Spree
2
2
  class StockItem < Spree::Base
3
3
  acts_as_paranoid
4
4
 
5
- belongs_to :stock_location, class_name: 'Spree::StockLocation', inverse_of: :stock_items
6
- belongs_to :variant, class_name: 'Spree::Variant', inverse_of: :stock_items, counter_cache: true
5
+ with_options inverse_of: :stock_items do
6
+ belongs_to :stock_location, class_name: 'Spree::StockLocation'
7
+ belongs_to :variant, class_name: 'Spree::Variant', counter_cache: true
8
+ end
7
9
  has_many :stock_movements, inverse_of: :stock_item
8
10
 
9
- validates_presence_of :stock_location, :variant
10
- validates_uniqueness_of :variant_id, scope: [:stock_location_id, :deleted_at]
11
+ validates :stock_location, :variant, presence: true
12
+ validates :variant_id, uniqueness: { scope: [:stock_location_id, :deleted_at] }, allow_blank: true
11
13
 
12
- validates_numericality_of :count_on_hand,
14
+ validates :count_on_hand, numericality: {
13
15
  greater_than_or_equal_to: 0,
14
16
  less_than_or_equal_to: 2**31 - 1,
15
- only_integer: true, if: :verify_count_on_hand?
17
+ only_integer: true }, if: :verify_count_on_hand?
16
18
 
17
19
  delegate :weight, :should_track_inventory?, to: :variant
20
+ delegate :name, to: :variant, prefix: true
18
21
 
19
22
  after_save :conditional_variant_touch, if: :changed?
20
23
  after_touch { variant.touch }
21
24
 
22
25
  self.whitelisted_ransackable_attributes = ['count_on_hand', 'stock_location_id']
23
26
 
27
+ scope :with_active_stock_location, -> { joins(:stock_location).merge(Spree::StockLocation.active) }
28
+
24
29
  def backordered_inventory_units
25
30
  Spree::InventoryUnit.backordered_for_stock_item(self)
26
31
  end
27
32
 
28
- def variant_name
29
- variant.name
30
- end
31
-
32
33
  def adjust_count_on_hand(value)
33
34
  self.with_lock do
34
- self.count_on_hand = self.count_on_hand + value
35
- process_backorders(count_on_hand - count_on_hand_was)
36
-
37
- self.save!
35
+ set_count_on_hand(count_on_hand + value)
38
36
  end
39
37
  end
40
38
 
@@ -67,10 +65,6 @@ module Spree
67
65
  count_on_hand_changed? && !backorderable? && (count_on_hand < count_on_hand_was) && (count_on_hand < 0)
68
66
  end
69
67
 
70
- def count_on_hand=(value)
71
- write_attribute(:count_on_hand, value)
72
- end
73
-
74
68
  # Process backorders based on amount of stock received
75
69
  # If stock was -20 and is now -15 (increase of 5 units), then we should process 5 inventory orders.
76
70
  # If stock was -20 but then was -25 (decrease of 5 units), do nothing.
@@ -7,12 +7,12 @@ module Spree
7
7
  belongs_to :state, class_name: 'Spree::State'
8
8
  belongs_to :country, class_name: 'Spree::Country'
9
9
 
10
- validates_presence_of :name
10
+ validates :name, presence: true
11
11
 
12
12
  scope :active, -> { where(active: true) }
13
13
  scope :order_default, -> { order(default: :desc, name: :asc) }
14
14
 
15
- after_create :create_stock_items, :if => "self.propagate_all_variants?"
15
+ after_create :create_stock_items, if: :propagate_all_variants?
16
16
  after_save :ensure_one_default
17
17
 
18
18
  def state_text
@@ -68,7 +68,7 @@ module Spree
68
68
  item = stock_item_or_create(variant)
69
69
  item.update_columns(
70
70
  count_on_hand: item.count_on_hand + quantity,
71
- updated_at: Time.now
71
+ updated_at: Time.current
72
72
  )
73
73
  end
74
74
 
@@ -108,10 +108,7 @@ module Spree
108
108
 
109
109
  def ensure_one_default
110
110
  if self.default
111
- StockLocation.where(default: true).where.not(id: self.id).each do |stock_location|
112
- stock_location.default = false
113
- stock_location.save!
114
- end
111
+ StockLocation.where(default: true).where.not(id: self.id).update_all(default: false)
115
112
  end
116
113
  end
117
114
  end
@@ -5,15 +5,17 @@ module Spree
5
5
 
6
6
  after_create :update_stock_item_quantity
7
7
 
8
- validates :stock_item, presence: true
9
- validates :quantity, presence: true, numericality: {
10
- greater_than_or_equal_to: -2**31,
11
- less_than_or_equal_to: 2**31-1,
12
- only_integer: true,
13
- allow_nil: true
14
- }
15
-
16
- scope :recent, -> { order('created_at DESC') }
8
+ with_options presence: true do
9
+ validates :stock_item
10
+ validates :quantity, numericality: {
11
+ greater_than_or_equal_to: -2**31,
12
+ less_than_or_equal_to: 2**31 - 1,
13
+ only_integer: true,
14
+ allow_nil: true
15
+ }
16
+ end
17
+
18
+ scope :recent, -> { order(created_at: :desc) }
17
19
 
18
20
  self.whitelisted_ransackable_attributes = ['quantity']
19
21
 
@@ -1,34 +1,27 @@
1
1
  module Spree
2
2
  class StockTransfer < Spree::Base
3
+ include Spree::Core::NumberGenerator.new(prefix: 'T')
4
+
3
5
  extend FriendlyId
4
6
  friendly_id :number, slug_column: :number, use: :slugged
5
7
 
6
- include Spree::NumberGenerator
7
-
8
- def generate_number(options = {})
9
- options[:prefix] ||= 'T'
10
- super(options)
11
- end
12
-
13
8
  has_many :stock_movements, as: :originator
14
9
 
15
10
  belongs_to :source_location, class_name: 'StockLocation'
16
11
  belongs_to :destination_location, class_name: 'StockLocation'
17
12
 
18
- self.whitelisted_ransackable_attributes = %w[reference source_location_id destination_location_id closed_at created_at number]
13
+ self.whitelisted_ransackable_attributes = %w[reference source_location_id destination_location_id created_at number]
19
14
 
20
15
  def to_param
21
16
  number
22
17
  end
23
18
 
24
19
  def source_movements
25
- stock_movements.joins(:stock_item)
26
- .where('spree_stock_items.stock_location_id' => source_location_id)
20
+ find_stock_location_with_location_id(source_location_id)
27
21
  end
28
22
 
29
23
  def destination_movements
30
- stock_movements.joins(:stock_item)
31
- .where('spree_stock_items.stock_location_id' => destination_location_id)
24
+ find_stock_location_with_location_id(destination_location_id)
32
25
  end
33
26
 
34
27
  def transfer(source_location, destination_location, variants)
@@ -47,5 +40,11 @@ module Spree
47
40
  def receive(destination_location, variants)
48
41
  transfer(nil, destination_location, variants)
49
42
  end
43
+
44
+ private
45
+
46
+ def find_stock_location_with_location_id(location_id)
47
+ stock_movements.joins(:stock_item).where('spree_stock_items.stock_location_id' => location_id)
48
+ end
50
49
  end
51
50
  end
@@ -2,23 +2,27 @@ module Spree
2
2
  class Store < Spree::Base
3
3
  has_many :orders, class_name: "Spree::Order"
4
4
 
5
- validates :code, presence: true, uniqueness: { allow_blank: true }
6
- validates :name, presence: true
7
- validates :url, presence: true
8
- validates :mail_from_address, presence: true
5
+ with_options presence: true do
6
+ validates :code, uniqueness: { allow_blank: true }
7
+ validates :name, :url, :mail_from_address
8
+ end
9
9
 
10
10
  before_save :ensure_default_exists_and_is_unique
11
11
  before_destroy :validate_not_default
12
12
 
13
13
  scope :by_url, lambda { |url| where("url like ?", "%#{url}%") }
14
14
 
15
+ after_commit :clear_cache
16
+
15
17
  def self.current(domain = nil)
16
18
  current_store = domain ? Store.by_url(domain).first : nil
17
19
  current_store || Store.default
18
20
  end
19
21
 
20
22
  def self.default
21
- where(default: true).first || new
23
+ Rails.cache.fetch("default_store") do
24
+ where(default: true).first_or_initialize
25
+ end
22
26
  end
23
27
 
24
28
  private
@@ -26,7 +30,7 @@ module Spree
26
30
  def ensure_default_exists_and_is_unique
27
31
  if default
28
32
  Store.where.not(id: id).update_all(default: false)
29
- elsif Store.where(default: true).count == 0
33
+ elsif Store.where(default: true).count.zero?
30
34
  self.default = true
31
35
  end
32
36
  end
@@ -36,5 +40,9 @@ module Spree
36
40
  errors.add(:base, :cannot_destroy_default_store)
37
41
  end
38
42
  end
43
+
44
+ def clear_cache
45
+ Rails.cache.delete("default_store")
46
+ end
39
47
  end
40
48
  end
@@ -0,0 +1,252 @@
1
+ module Spree
2
+ class StoreCredit < Spree::Base
3
+ acts_as_paranoid
4
+
5
+ VOID_ACTION = 'void'.freeze
6
+ CANCEL_ACTION = 'cancel'.freeze
7
+ CREDIT_ACTION = 'credit'.freeze
8
+ CAPTURE_ACTION = 'capture'.freeze
9
+ ELIGIBLE_ACTION = 'eligible'.freeze
10
+ AUTHORIZE_ACTION = 'authorize'.freeze
11
+ ALLOCATION_ACTION = 'allocation'.freeze
12
+
13
+ DEFAULT_CREATED_BY_EMAIL = 'spree@example.com'.freeze
14
+
15
+ belongs_to :user, class_name: Spree.user_class.to_s, foreign_key: 'user_id'
16
+ belongs_to :category, class_name: "Spree::StoreCreditCategory"
17
+ belongs_to :created_by, class_name: Spree.user_class.to_s, foreign_key: 'created_by_id'
18
+ belongs_to :credit_type, class_name: 'Spree::StoreCreditType', foreign_key: 'type_id'
19
+ has_many :store_credit_events
20
+
21
+ validates_presence_of :user, :category, :credit_type, :created_by, :currency
22
+ validates_numericality_of :amount, greater_than: 0
23
+ validates_numericality_of :amount_used, greater_than_or_equal_to: 0
24
+ validate :amount_used_less_than_or_equal_to_amount
25
+ validate :amount_authorized_less_than_or_equal_to_amount
26
+
27
+ delegate :name, to: :category, prefix: true
28
+ delegate :email, to: :created_by, prefix: true
29
+
30
+ scope :order_by_priority, -> { includes(:credit_type).order('spree_store_credit_types.priority ASC') }
31
+
32
+ before_validation :associate_credit_type
33
+ after_save :store_event
34
+ before_destroy :validate_no_amount_used
35
+
36
+ attr_accessor :action, :action_amount, :action_originator, :action_authorization_code
37
+
38
+ def display_amount
39
+ Spree::Money.new(amount)
40
+ end
41
+
42
+ def display_amount_used
43
+ Spree::Money.new(amount_used)
44
+ end
45
+
46
+ def amount_remaining
47
+ amount - amount_used - amount_authorized
48
+ end
49
+
50
+ def authorize(amount, order_currency, options = {})
51
+ authorization_code = options[:action_authorization_code]
52
+ if authorization_code
53
+ if store_credit_events.find_by(action: AUTHORIZE_ACTION, authorization_code: authorization_code)
54
+ # Don't authorize again on capture
55
+ return true
56
+ end
57
+ else
58
+ authorization_code = generate_authorization_code
59
+ end
60
+
61
+ if validate_authorization(amount, order_currency)
62
+ update_attributes!(
63
+ action: AUTHORIZE_ACTION,
64
+ action_amount: amount,
65
+ action_originator: options[:action_originator],
66
+ action_authorization_code: authorization_code,
67
+ amount_authorized: amount_authorized + amount
68
+ )
69
+ authorization_code
70
+ else
71
+ errors.add(:base, Spree.t('store_credit_payment_method.insufficient_authorized_amount'))
72
+ false
73
+ end
74
+ end
75
+
76
+ def validate_authorization(amount, order_currency)
77
+ if amount_remaining.to_d < amount.to_d
78
+ errors.add(:base, Spree.t('store_credit_payment_method.insufficient_funds'))
79
+ elsif currency != order_currency
80
+ errors.add(:base, Spree.t('store_credit_payment_method.currency_mismatch'))
81
+ end
82
+ errors.blank?
83
+ end
84
+
85
+ def capture(amount, authorization_code, order_currency, options = {})
86
+ return false unless authorize(amount, order_currency, action_authorization_code: authorization_code)
87
+
88
+ if amount <= amount_authorized
89
+ if currency != order_currency
90
+ errors.add(:base, Spree.t('store_credit_payment_method.currency_mismatch'))
91
+ false
92
+ else
93
+ update_attributes!(
94
+ action: CAPTURE_ACTION,
95
+ action_amount: amount,
96
+ action_originator: options[:action_originator],
97
+ action_authorization_code: authorization_code,
98
+ amount_used: amount_used + amount,
99
+ amount_authorized: amount_authorized - amount
100
+ )
101
+ authorization_code
102
+ end
103
+ else
104
+ errors.add(:base, Spree.t('store_credit_payment_method.insufficient_authorized_amount'))
105
+ false
106
+ end
107
+ end
108
+
109
+ def void(authorization_code, options = {})
110
+ if auth_event = store_credit_events.find_by(action: AUTHORIZE_ACTION, authorization_code: authorization_code)
111
+ update_attributes!(
112
+ action: VOID_ACTION,
113
+ action_amount: auth_event.amount,
114
+ action_authorization_code: authorization_code,
115
+ action_originator: options[:action_originator],
116
+ amount_authorized: amount_authorized - auth_event.amount
117
+ )
118
+ true
119
+ else
120
+ errors.add(:base, Spree.t('store_credit_payment_method.unable_to_void', auth_code: authorization_code))
121
+ false
122
+ end
123
+ end
124
+
125
+ def credit(amount, authorization_code, order_currency, options = {})
126
+ # Find the amount related to this authorization_code in order to add the store credit back
127
+ capture_event = store_credit_events.find_by(action: CAPTURE_ACTION, authorization_code: authorization_code)
128
+
129
+ if currency != order_currency # sanity check to make sure the order currency hasn't changed since the auth
130
+ errors.add(:base, Spree.t('store_credit_payment_method.currency_mismatch'))
131
+ false
132
+ elsif capture_event && amount <= capture_event.amount
133
+ action_attributes = {
134
+ action: CREDIT_ACTION,
135
+ action_amount: amount,
136
+ action_originator: options[:action_originator],
137
+ action_authorization_code: authorization_code
138
+ }
139
+ create_credit_record(amount, action_attributes)
140
+ true
141
+ else
142
+ errors.add(:base, Spree.t('store_credit_payment_method.unable_to_credit', auth_code: authorization_code))
143
+ false
144
+ end
145
+ end
146
+
147
+ def actions
148
+ [CAPTURE_ACTION, VOID_ACTION, CREDIT_ACTION]
149
+ end
150
+
151
+ def can_capture?(payment)
152
+ payment.pending? || payment.checkout?
153
+ end
154
+
155
+ def can_void?(payment)
156
+ payment.pending?
157
+ end
158
+
159
+ def can_credit?(payment)
160
+ payment.completed? && payment.credit_allowed > 0
161
+ end
162
+
163
+ def generate_authorization_code
164
+ "#{id}-SC-#{Time.now.utc.strftime('%Y%m%d%H%M%S%6N')}"
165
+ end
166
+
167
+ class << self
168
+ def default_created_by
169
+ Spree.user_class.find_by(email: DEFAULT_CREATED_BY_EMAIL)
170
+ end
171
+ end
172
+
173
+ private
174
+
175
+ def create_credit_record(amount, action_attributes = {})
176
+ # Setting credit_to_new_allocation to true will create a new allocation anytime #credit is called
177
+ # If it is not set, it will update the store credit's amount in place
178
+ credit = if Spree::Config.credit_to_new_allocation
179
+ Spree::StoreCredit.new(create_credit_record_params(amount))
180
+ else
181
+ self.amount_used = amount_used - amount
182
+ self
183
+ end
184
+
185
+ credit.assign_attributes(action_attributes)
186
+ credit.save!
187
+ end
188
+
189
+ def create_credit_record_params(amount)
190
+ {
191
+ amount: amount,
192
+ user_id: user_id,
193
+ category_id: category_id,
194
+ created_by_id: created_by_id,
195
+ currency: currency,
196
+ type_id: type_id,
197
+ memo: credit_allocation_memo
198
+ }
199
+ end
200
+
201
+ def credit_allocation_memo
202
+ "This is a credit from store credit ID #{id}"
203
+ end
204
+
205
+ def store_event
206
+ return unless amount_changed? || amount_used_changed? || amount_authorized_changed? || action == ELIGIBLE_ACTION
207
+
208
+ event = if action
209
+ store_credit_events.build(action: action)
210
+ else
211
+ store_credit_events.where(action: ALLOCATION_ACTION).first_or_initialize
212
+ end
213
+
214
+ event.update_attributes!(
215
+ amount: action_amount || amount,
216
+ authorization_code: action_authorization_code || event.authorization_code || generate_authorization_code,
217
+ user_total_amount: user.total_available_store_credit,
218
+ originator: action_originator
219
+ )
220
+ end
221
+
222
+ def amount_used_less_than_or_equal_to_amount
223
+ return true if amount_used.nil?
224
+
225
+ if amount_used > amount
226
+ errors.add(:amount_used, :cannot_be_greater_than_amount)
227
+ false
228
+ end
229
+ end
230
+
231
+ def amount_authorized_less_than_or_equal_to_amount
232
+ if (amount_used + amount_authorized) > amount
233
+ errors.add(:amount_authorized, :exceeds_total_credits)
234
+ false
235
+ end
236
+ end
237
+
238
+ def validate_no_amount_used
239
+ if amount_used > 0
240
+ errors.add(:amount_used, :greater_than_zero_restrict_delete)
241
+ false
242
+ end
243
+ end
244
+
245
+ def associate_credit_type
246
+ unless type_id
247
+ credit_type_name = category.try(:non_expiring?) ? 'Non-expiring' : 'Expiring'
248
+ self.credit_type = Spree::StoreCreditType.find_by_name(credit_type_name)
249
+ end
250
+ end
251
+ end
252
+ end