-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #2197 Add currency parameter support to Spree::Money. Spree::Money now allows an optional currency parameter to be passed in, which will be used if specified. In the case where it is not specified, the global spree configuration value will be used instead. The foundation of multi-currency support is going to be modifying the code to support storing the currency in the various objects used by Spree. The goal is to modify the code to allow: - adjustments - line items - orders - payments - return authorizations - shipments - shipping rates - variants to eventually store and track their own currency. Currently these objects mirror the global currency value. As code support improves, this will be stored in the database for these models, or inferred from dependent models (for example, determining the line item currency from the order currency). Flagged money helper as deprecated The money helper is based around having a single site wide currency. This is instead being moved to a series of display_* calls on the models which have both the monetary amount and currency to produce its own Spree::Money object, which then allows us to display a properly formatted currency on the output. Split prices from variants & store currency in DB. This will allow a variant to have many prices in many different currencies. Store currency in database. Add currency to line items. Line items now support currency, which can be tracked on a line item basis. Add currency support to orders. Infer payment currency from order. Support currency on shipping. Base currencies off of order currency. Adjustment, Return Auth, and Shipment now infer their currency from the currency of their order.
- Loading branch information
Showing
80 changed files
with
910 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module Spree | ||
class Price < ActiveRecord::Base | ||
belongs_to :variant, :class_name => 'Spree::Variant' | ||
|
||
validate :check_price | ||
validates :amount, :numericality => { :greater_than_or_equal_to => 0 }, :allow_nil => true | ||
|
||
attr_accessible :variant_id, :currency, :amount | ||
|
||
def display_amount | ||
return nil if amount.nil? | ||
money.to_s | ||
end | ||
alias :display_price :display_amount | ||
|
||
def money | ||
Spree::Money.new(amount, { :currency => currency }) | ||
end | ||
|
||
def price | ||
amount | ||
end | ||
|
||
def price=(price) | ||
self[:amount] = parse_price(price) if price.present? | ||
end | ||
|
||
private | ||
def check_price | ||
raise "Price must belong to a variant" if variant.nil? | ||
if amount.nil? | ||
if variant.is_master? || variant.product.master.nil? || variant.product.master.default_price.nil? | ||
self.amount = nil | ||
else | ||
self.amount = variant.product.master.default_price.amount | ||
end | ||
end | ||
if currency.nil? | ||
if variant.product.master.default_price.nil? | ||
self.currency = Spree::Config[:currency] | ||
else | ||
self.currency = variant.product.master.default_price.currency | ||
end | ||
end | ||
end | ||
|
||
# strips all non-price-like characters from the price, taking into account locale settings | ||
def parse_price(price) | ||
return price unless price.is_a?(String) | ||
|
||
separator, delimiter = I18n.t([:'number.currency.format.separator', :'number.currency.format.delimiter']) | ||
non_price_characters = /[^0-9\-#{separator}]/ | ||
price.gsub!(non_price_characters, '') # strip everything else first | ||
price.gsub!(separator, '.') unless separator == '.' # then replace the locale-specific decimal separator with the standard separator if necessary | ||
|
||
price.to_d | ||
end | ||
|
||
end | ||
end | ||
|
Oops, something went wrong.