-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close #166 support promotion, adjustments, tax in payouts
- Loading branch information
Showing
24 changed files
with
592 additions
and
192 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# This model is used to associate line items with payout profiles, even though we may not know in advance | ||
# if user select payment method that supports payouts. The benefit is that when a user selects and pays with | ||
# a payment method that does support payouts, we can use this association to tell the bank which payout profile to send to. | ||
# It also benefits financial reporting. | ||
module Spree | ||
class PayoutProfileLineItem < Base | ||
belongs_to :payout_profile, class_name: 'Spree::PayoutProfile', required: true, inverse_of: :payout_profile_line_items | ||
belongs_to :line_item, class_name: 'Spree::LineItem', required: true, inverse_of: :payout_profile_line_items | ||
|
||
has_one :order, through: :line_item | ||
|
||
validates :payout_profile_id, uniqueness: { scope: :line_item_id } | ||
|
||
extend DisplayMoney | ||
money_methods :amount, :commission_amount, :pre_commission_amount, :outstanding_amount | ||
|
||
before_save :set_amounts | ||
|
||
def available_amount_can_be_payout_to_vendor | ||
order_adjustment = order.line_item_count.zero? ? 0 : order.adjustments.eligible.credit.total / order.line_item_count | ||
available_amount = line_item.subtotal + line_item.adjustments.eligible.credit.total + order_adjustment | ||
expected_amount_by_vendor = line_item.pre_commission_amount | ||
|
||
# avoid more than expected. | ||
[available_amount, expected_amount_by_vendor].min | ||
end | ||
|
||
private | ||
|
||
def set_amounts | ||
self.amount = available_amount_can_be_payout_to_vendor | ||
|
||
self.commission_rate = line_item.commission_rate | ||
self.commission_amount = line_item.commission_amount | ||
self.pre_commission_amount = line_item.pre_commission_amount | ||
|
||
# amount that should be later sent to vendor | ||
self.outstanding_amount = self.pre_commission_amount - self.amount | ||
end | ||
end | ||
end |
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,26 @@ | ||
module Vpago | ||
module AdjustmentDecorator | ||
def self.prepended(base) | ||
base.enum run_by: { unspecified: 0, store: 1, vendor: 2 }, _prefix: true | ||
|
||
base.scope :promotion_run_by_vendor, -> { promotion.eligible.where(run_by: :vendor) } | ||
base.scope :promotion_run_by_store, -> { promotion.eligible.where(run_by: :store) } | ||
|
||
base.before_save :set_run_by | ||
|
||
def base.total | ||
sum(:amount) | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_run_by | ||
self.run_by = source.try(:run_by) if source.is_a?(::Spree::PromotionAction) | ||
end | ||
end | ||
end | ||
|
||
unless Spree::Adjustment.included_modules.include?(Vpago::AdjustmentDecorator) | ||
Spree::Adjustment.prepend(Vpago::AdjustmentDecorator) | ||
end |
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,29 @@ | ||
module Vpago | ||
class PayoutProfileLineItemGenerator | ||
attr_reader :order | ||
|
||
def initialize(order) | ||
@order = order | ||
end | ||
|
||
def call | ||
return if order.payout_profile_payments.any? | ||
|
||
prepare_payout_profile_line_items | ||
end | ||
|
||
# Associate line items with payout profiles, even though we may not know in advance | ||
# if user select payment method that supports payouts. The benefit is that when a user selects and pays with | ||
# a payment method that does support payouts, we can use this association to tell the bank which payout profile to send to. | ||
# It also benefits financial reporting. | ||
def prepare_payout_profile_line_items | ||
line_items = order.line_items.includes(:active_payway_payout_profiles) | ||
Spree::PayoutProfileLineItem.where(line_item_id: line_items.pluck(:id)).delete_all | ||
|
||
line_items.each do |line_item| | ||
payout_profile = line_item.active_payway_payout_profiles.first || Spree::PayoutProfiles::PaywayV2.default | ||
line_item.payout_profile_line_items.create(payout_profile: payout_profile) | ||
end | ||
end | ||
end | ||
end |
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,11 @@ | ||
module Vpago | ||
module PromotionActionDecorator | ||
def self.prepended(base) | ||
base.enum run_by: { unspecified: 0, store: 1, vendor: 2 }, _prefix: true | ||
end | ||
end | ||
end | ||
|
||
unless Spree::PromotionAction.included_modules.include?(Vpago::PromotionActionDecorator) | ||
Spree::PromotionAction.prepend(Vpago::PromotionActionDecorator) | ||
end |
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
16 changes: 16 additions & 0 deletions
16
db/migrate/20240610103708_create_spree_payout_profile_line_items.rb
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,16 @@ | ||
class CreateSpreePayoutProfileLineItems < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :spree_payout_profile_line_items, if_not_exists: true do |t| | ||
t.references :payout_profile, foreign_key: { to_table: :spree_payout_profiles } | ||
t.references :line_item, foreign_key: { to_table: :spree_line_items } | ||
|
||
t.decimal :amount, precision: 10, scale: 2, default: "0.0", null: false | ||
t.decimal :commission_rate, null: false | ||
t.decimal :commission_amount, precision: 10, scale: 2, default: "0.0", null: false | ||
t.decimal :pre_commission_amount, precision: 10, scale: 2, default: "0.0", null: false | ||
t.decimal :outstanding_amount, precision: 10, scale: 2, default: "0.0", null: false | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
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,5 @@ | ||
class AddRunByToSpreeAdjustments < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :spree_adjustments, :run_by, :integer, null: false, default: 0, if_not_exists: true | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
db/migrate/20240611093824_add_run_by_to_spree_promotion_actions.rb
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,5 @@ | ||
class AddRunByToSpreePromotionActions < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :spree_promotion_actions, :run_by, :integer, null: false, default: 0, if_not_exists: true | ||
end | ||
end |
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,6 @@ | ||
FactoryBot.define do | ||
factory :payout_profile_line_item, class: Spree::PayoutProfileLineItem do | ||
payout_profile {|p| p.association(:payway_payout_profile) } | ||
line_item {|p| p.association(:line_item) } | ||
end | ||
end |
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,6 @@ | ||
FactoryBot.define do | ||
factory :payout_profile_payment, class: Spree::PayoutProfilePayment do | ||
payout_profile {|p| p.association(:payway_payout_profile) } | ||
payment {|p| p.association(:payment) } | ||
end | ||
end |
Oops, something went wrong.