Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use gross amount in return items #706

Merged
merged 1 commit into from Feb 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def load_return_items
unassociated_inventory_units = all_inventory_units - associated_inventory_units

new_return_items = unassociated_inventory_units.map do |new_unit|
Spree::ReturnItem.new(inventory_unit: new_unit).tap(&:set_default_pre_tax_amount)
Spree::ReturnItem.new(inventory_unit: new_unit).tap(&:set_default_amount)
end
@form_return_items = (@return_authorization.return_items + new_return_items).sort_by(&:inventory_unit_id)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<tr>
<th><%= Spree::Product.model_name.human %></th>
<th><%= Spree::Variant.human_attribute_name(:sku) %></th>
<th><%= Spree::ReturnItem.human_attribute_name(:pre_tax_amount) %></th>
<th><%= Spree::ReturnItem.human_attribute_name(:amount) %></th>
<th><%= Spree::ReturnItem.human_attribute_name(:preferred_reimbursement_type) %></th>
<th><%= Spree::ReturnItem.human_attribute_name(:exchange_variant) %></th>
<th><%= Spree::ReturnItem.human_attribute_name(:acceptance_status_errors) %></th>
Expand All @@ -27,7 +27,7 @@
<%= return_item.inventory_unit.variant.sku %>
</td>
<td class="align-center">
<%= return_item.display_pre_tax_amount %>
<%= return_item.display_amount %>
</td>
<td class="align-center">
<%= reimbursement_type_name(return_item.preferred_reimbursement_type) %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</th>
<th><%= Spree::Product.model_name.human %></th>
<th><%= Spree::Variant.human_attribute_name(:sku) %></th>
<th><%= Spree::ReturnItem.human_attribute_name(:pre_tax_amount) %></th>
<th><%= Spree::ReturnItem.human_attribute_name(:amount) %></th>
<th><%= Spree::ReturnItem.human_attribute_name(:inventory_unit_state) %></th>
<th><%= Spree::ReturnItem.human_attribute_name(:exchange_variant) %></th>
<th><%= Spree::ReturnItem.human_attribute_name(:resellable) %></th>
Expand All @@ -23,9 +23,9 @@
<div style="display:none">
<%= item_fields.hidden_field :inventory_unit_id %>
<%= item_fields.hidden_field :return_authorization_id %>
<%= item_fields.hidden_field :pre_tax_amount %>
<%= item_fields.hidden_field :amount %>
</div>
<%= item_fields.check_box :returned, {checked: false, class: 'add-item', "data-price" => return_item.pre_tax_amount}, '1', '0' %>
<%= item_fields.check_box :returned, {checked: false, class: 'add-item', "data-price" => return_item.amount}, '1', '0' %>
</td>
<td>
<div class="variant-name"><%= return_item.inventory_unit.variant.name %></div>
Expand All @@ -35,7 +35,7 @@
<%= return_item.inventory_unit.variant.sku %>
</td>
<td class="align-center">
<%= return_item.display_pre_tax_amount %>
<%= return_item.display_amount %>
</td>
<td class="align-center">
<%= return_item.inventory_unit.state %>
Expand Down
2 changes: 1 addition & 1 deletion backend/app/views/spree/admin/reimbursements/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
) %>
</td>
<td class="align-center">
<%= item_fields.text_field :pre_tax_amount, { class: 'refund-amount-input' } %>
<%= item_fields.text_field :amount, { class: 'refund-amount-input' } %>
</td>
<td class="align-center">
<%= return_item.display_total %>
Expand Down
4 changes: 2 additions & 2 deletions backend/app/views/spree/admin/reimbursements/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<th><%= Spree.t(:preferred_reimbursement_type) %></th>
<th><%= Spree.t(:reimbursement_type_override) %></th>
<th><%= Spree.t(:exchange_for) %></th>
<th><%= Spree.t(:pre_tax_amount) %></th>
<th><%= Spree.t(:amount) %></th>
<th><%= Spree.t(:total) %></th>
</tr>
</thead>
Expand All @@ -41,7 +41,7 @@
<%= return_item.exchange_variant.try(:exchange_name) %>
</td>
<td class="align-center">
<%= return_item.display_pre_tax_amount %>
<%= return_item.display_amount %>
</td>
<td class="align-center">
<%= return_item.display_total %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<td class="return-item-checkbox align-center inventory-unit-checkbox">
<% if editable %>
<%= item_fields.hidden_field :inventory_unit_id %>
<%= item_fields.check_box :_destroy, {checked: return_item.persisted?, class: 'add-item', "data-price" => return_item.pre_tax_amount}, '0', '1' %>
<%= item_fields.check_box :_destroy, {checked: return_item.persisted?, class: 'add-item', "data-price" => return_item.amount}, '0', '1' %>
<% end %>
</td>
<td class="return-item-product">
Expand All @@ -36,13 +36,13 @@
</td>
<td class="return-item-state align-center"><%= inventory_unit.state.humanize %></td>
<td class="return-item-charged align-center">
<%= return_item.display_pre_tax_amount %>
<%= return_item.display_amount %>
</td>
<td class="return-item-pre-tax-refund-amount align-center">
<% if editable %>
<%= item_fields.text_field :pre_tax_amount, { class: 'refund-amount-input' } %>
<%= item_fields.text_field :amount, { class: 'refund-amount-input' } %>
<% else %>
<%= return_item.display_pre_tax_amount %>
<%= return_item.display_amount %>
<% end %>
</td>
<td class="return-item-reimbursement-type">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ module Admin
return_items_attributes: {
"0" => {
returned: "1",
"pre_tax_amount" => "15.99",
amount: "15.99",
inventory_unit_id: order.inventory_units.shipped.last.id,
reception_status_event: reception_status_event
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.description

def compute(return_item)
return 0.0.to_d if return_item.part_of_exchange?
weighted_order_adjustment_amount(return_item.inventory_unit) + weighted_line_item_pre_tax_amount(return_item.inventory_unit)
weighted_order_adjustment_amount(return_item.inventory_unit) + weighted_line_item_amount(return_item.inventory_unit)
end

private
Expand All @@ -18,18 +18,18 @@ def weighted_order_adjustment_amount(inventory_unit)
inventory_unit.order.adjustments.eligible.non_tax.sum(:amount) * percentage_of_order_total(inventory_unit)
end

def weighted_line_item_pre_tax_amount(inventory_unit)
inventory_unit.line_item.pre_tax_amount * percentage_of_line_item(inventory_unit)
def weighted_line_item_amount(inventory_unit)
inventory_unit.line_item.discounted_amount * percentage_of_line_item(inventory_unit)
end

def percentage_of_order_total(inventory_unit)
return 0.0 if inventory_unit.order.pre_tax_item_amount.zero?
weighted_line_item_pre_tax_amount(inventory_unit) / inventory_unit.order.pre_tax_item_amount
return 0.0 if inventory_unit.order.discounted_item_amount.zero?
weighted_line_item_amount(inventory_unit) / inventory_unit.order.discounted_item_amount
end

def percentage_of_line_item(inventory_unit)
1 / BigDecimal.new(inventory_unit.line_item.quantity)
end
end
end
end
end
end
9 changes: 7 additions & 2 deletions core/app/models/spree/customer_return.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class CustomerReturn < Spree::Base

extend DisplayMoney
money_methods pre_tax_total: { currency: Spree::Config[:currency] },
total: { currency: Spree::Config[:currency] }
total: { currency: Spree::Config[:currency] },
amount: { currency: Spree::Config[:currency] }

delegate :id, to: :order, prefix: true, allow_nil: true

Expand All @@ -26,7 +27,11 @@ def total
end

def pre_tax_total
return_items.sum(:pre_tax_amount)
return_items.map(&:pre_tax_amount).sum
end

def amount
return_items.sum(:amount)
end

# Temporarily tie a customer_return to one order
Expand Down
5 changes: 5 additions & 0 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ def pre_tax_item_amount
line_items.to_a.sum(&:pre_tax_amount)
end

# Sum of all line item amounts after promotions, before added tax
def discounted_item_amount
line_items.to_a.sum(&:discounted_amount)
end

def currency
self[:currency] || Spree::Config[:currency]
end
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/reimbursement_tax_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def call(reimbursement)
private

def set_tax!(return_item)
percent_of_tax = (return_item.pre_tax_amount <= 0) ? 0 : return_item.pre_tax_amount / Spree::ReturnItem.refund_amount_calculator.new.compute(return_item)
percent_of_tax = (return_item.amount <= 0) ? 0 : return_item.amount / Spree::ReturnItem.refund_amount_calculator.new.compute(return_item)

additional_tax_total = percent_of_tax * return_item.inventory_unit.additional_tax_total
included_tax_total = percent_of_tax * return_item.inventory_unit.included_tax_total
Expand Down
10 changes: 7 additions & 3 deletions core/app/models/spree/return_authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,24 @@ class ReturnAuthorization < Spree::Base
end

extend DisplayMoney
money_methods :pre_tax_total
money_methods :pre_tax_total, :amount

self.whitelisted_ransackable_attributes = ['memo']

def pre_tax_total
return_items.sum(:pre_tax_amount)
return_items.map(&:pre_tax_amount).sum
end

def amount
return_item.sum(:amount)
end

def currency
order.nil? ? Spree::Config[:currency] : order.currency
end

def refundable_amount
order.pre_tax_item_amount + order.promo_total
order.discounted_item_amount + order.promo_total
end

def customer_returned_items?
Expand Down
30 changes: 17 additions & 13 deletions core/app/models/spree/return_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class ReturnItem < Spree::Base
delegate :variant, to: :inventory_unit
delegate :shipment, to: :inventory_unit

before_create :set_default_pre_tax_amount, unless: :pre_tax_amount_changed?
before_save :set_exchange_pre_tax_amount
before_create :set_default_amount, unless: :amount_changed?
before_save :set_exchange_amount

state_machine :reception_status, initial: :awaiting do
after_transition to: COMPLETED_RECEPTION_STATUSES, do: :attempt_accept
Expand All @@ -93,7 +93,7 @@ class ReturnItem < Spree::Base
end

extend DisplayMoney
money_methods :pre_tax_amount, :total
money_methods :pre_tax_amount, :amount, :total

# @return [Boolean] true when this retur item is in a complete reception
# state
Expand Down Expand Up @@ -133,7 +133,7 @@ def reception_completed?
# unit if one exists, or a new one if one does not
def self.from_inventory_unit(inventory_unit)
valid.find_by(inventory_unit: inventory_unit) ||
new(inventory_unit: inventory_unit).tap(&:set_default_pre_tax_amount)
new(inventory_unit: inventory_unit).tap(&:set_default_amount)
end

# @return [Boolean] true when an exchange has been requested on this return
Expand All @@ -154,10 +154,14 @@ def exchange_required?
exchange_requested? && !exchange_processed?
end

# @return [BigDecimal] the cost of the item before tax, plus the included
# and additional taxes
# @return [BigDecimal] the cost of the item after tax
def total
pre_tax_amount + included_tax_total + additional_tax_total
amount + additional_tax_total
end

# @return [BigDecimal] the cost of the item before tax
def pre_tax_amount
amount - included_tax_total
end

# @note This uses the exchange_variant_engine configured on the class.
Expand Down Expand Up @@ -185,12 +189,12 @@ def exchange_shipment
exchange_inventory_unit.try(:shipment)
end

# Calculates and sets the default pre-tax amount to be refunded.
# Calculates and sets the default amount to be refunded.
#
# @note This uses the configured refund_amount_calculator configured on the
# class.
def set_default_pre_tax_amount
self.pre_tax_amount = refund_amount_calculator.new.compute(self)
def set_default_amount
self.amount = refund_amount_calculator.new.compute(self)
end

def potential_reception_transitions
Expand Down Expand Up @@ -240,7 +244,7 @@ def check_unexchange
original_ri = sibling_intended_for_exchange('awaiting')
if original_ri
original_ri.unexchange!
set_default_pre_tax_amount
set_default_amount
save!
end
end
Expand Down Expand Up @@ -276,8 +280,8 @@ def validate_acceptance_status_for_reimbursement
end
end

def set_exchange_pre_tax_amount
self.pre_tax_amount = 0.0.to_d if exchange_requested?
def set_exchange_amount
self.amount = 0.0.to_d if exchange_requested?
end

def validate_no_other_completed_return_items
Expand Down
2 changes: 1 addition & 1 deletion core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ en:
exchange_variant: Exchange for
inventory_unit_state: State
item_received?: "Item Received?"
pre_tax_amount: Pre-Tax Amount
pre_tax_amount: Amount before sales tax
preferred_reimbursement_type_id: Preferred Reimbursement Type
reception_status: Reception Status
resellable: "Resellable?"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class ChangeReturnItemPreTaxAmountToAmount < ActiveRecord::Migration
def up
execute(<<-SQL)
UPDATE spree_return_items
SET included_tax_total = 0
WHERE included_tax_total IS NULL;

UPDATE spree_return_items
SET pre_tax_amount = pre_tax_amount + included_tax_total;
SQL

rename_column :spree_return_items, :pre_tax_amount, :amount
end

def down
execute(<<-SQL)
UPDATE spree_return_items
SET included_tax_total = 0
WHERE included_tax_total IS NULL;

UPDATE spree_return_items
SET amount = amount - included_tax_total;
SQL

rename_column :spree_return_items, :amount, :pre_tax_amount
end
end
2 changes: 1 addition & 1 deletion core/lib/spree/permitted_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module PermittedAttributes
:month, :year, :expiry, :first_name, :last_name, :name
]

@@customer_return_attributes = [:stock_location_id, return_items_attributes: [:id, :inventory_unit_id, :return_authorization_id, :returned, :pre_tax_amount, :reception_status_event, :acceptance_status, :exchange_variant_id, :resellable]]
@@customer_return_attributes = [:stock_location_id, return_items_attributes: [:id, :inventory_unit_id, :return_authorization_id, :returned, :amount, :reception_status_event, :acceptance_status, :exchange_variant_id, :resellable]]

@@image_attributes = [:alt, :attachment, :position, :viewable_type, :viewable_id]

Expand Down
Loading