From 74bc38c2b757ead7b0716772627519bc9b2adb26 Mon Sep 17 00:00:00 2001 From: Thea Choem <29684683+theachoem@users.noreply.github.com> Date: Thu, 23 May 2024 18:09:07 +0700 Subject: [PATCH] close #162 allow payout to be optional for product --- .../payout_profile_products_controller.rb | 10 +++++ app/models/spree/payout_profile.rb | 19 +++++++++ app/models/spree/payout_profiles/payway_v2.rb | 5 +++ app/models/vpago/line_item_decorator.rb | 5 ++- app/models/vpago/product_decorator.rb | 9 ++++- .../payout_profile_products.html.erb.deface | 4 +- .../payout_profile_products/_form.html.erb | 27 ++++++++++++- .../_payout_profile_info_card.html.erb | 24 +++++++++++ .../_payout_profile_status.html.erb | 11 +++++ .../payout_profile_products/edit.html.erb | 18 +++++++++ .../payout_profile_products/new.html.erb | 17 +++++++- .../admin/payout_profiles/_filter.html.erb | 19 +++++++++ .../admin/payout_profiles/_form.html.erb | 2 +- .../admin/payout_profiles/index.html.erb | 8 +++- ..._optional_spere_payout_profile_products.rb | 5 +++ spec/models/spree/line_item_spec.rb | 40 ++++++------------- 16 files changed, 185 insertions(+), 38 deletions(-) create mode 100644 app/views/spree/admin/payout_profile_products/_payout_profile_info_card.html.erb create mode 100644 app/views/spree/admin/payout_profile_products/_payout_profile_status.html.erb create mode 100644 app/views/spree/admin/payout_profile_products/edit.html.erb create mode 100644 app/views/spree/admin/payout_profiles/_filter.html.erb create mode 100644 db/migrate/20240523070757_add_optional_spere_payout_profile_products.rb diff --git a/app/controllers/spree/admin/payout_profile_products_controller.rb b/app/controllers/spree/admin/payout_profile_products_controller.rb index 901c7046..8878b67b 100644 --- a/app/controllers/spree/admin/payout_profile_products_controller.rb +++ b/app/controllers/spree/admin/payout_profile_products_controller.rb @@ -1,8 +1,18 @@ module Spree module Admin class PayoutProfileProductsController < Spree::Admin::ResourceController + before_action :redirect_to_object, only: [:index] + belongs_to 'spree/product', find_by: :slug + def redirect_to_object + if parent.payout_profile_products.any? + redirect_to edit_object_url(parent.payout_profile_products.first) + else + redirect_to new_object_url + end + end + def model_class Spree::PayoutProfileProduct end diff --git a/app/models/spree/payout_profile.rb b/app/models/spree/payout_profile.rb index ac3e1756..e2bbd1fb 100644 --- a/app/models/spree/payout_profile.rb +++ b/app/models/spree/payout_profile.rb @@ -19,16 +19,35 @@ class PayoutProfile < Base before_save :ensure_default_exists_and_clear_vendor before_destroy :confirm_destroyable + self.whitelisted_ransackable_attributes = %w[name vendor_id] + def self.default Rails.cache.fetch("default_payout_account/#{self.name.underscore}") do find_by(type: self.name, default: true) end end + def bank_name + 'None' + end + + def display_name + display_name = name + + bank_info = [bank_name, bank_account_number].compact.join(" - ") + display_name += " (#{bank_info})" unless bank_info.empty? + + display_name + end + def verified? verified_at.present? end + def receivable? + verified? && active? + end + def registered_in_bank? true end diff --git a/app/models/spree/payout_profiles/payway_v2.rb b/app/models/spree/payout_profiles/payway_v2.rb index e241ccc0..708fea6a 100644 --- a/app/models/spree/payout_profiles/payway_v2.rb +++ b/app/models/spree/payout_profiles/payway_v2.rb @@ -30,6 +30,11 @@ def allow_to_verify_with_bank? preferred_api_key.present? && preferred_rsa_public_key.present? end + + # override + def bank_name + 'ABA' + end end end end diff --git a/app/models/vpago/line_item_decorator.rb b/app/models/vpago/line_item_decorator.rb index 1610d79f..a01dbf4c 100644 --- a/app/models/vpago/line_item_decorator.rb +++ b/app/models/vpago/line_item_decorator.rb @@ -4,10 +4,13 @@ def self.prepended(base) base.has_many :payout_profiles, class_name: 'Spree::PayoutProfile', through: :product base.has_many :active_payout_profiles, class_name: 'Spree::PayoutProfile', through: :product base.has_many :active_payway_payout_profiles, class_name: 'Spree::PayoutProfile', through: :product + + base.has_many :required_active_payout_profiles, class_name: 'Spree::PayoutProfile', through: :product end + # considred required when there are any required profiles. def required_payway_payout? - active_payway_payout_profiles.any? + required_active_payout_profiles.payway.exists? end end end diff --git a/app/models/vpago/product_decorator.rb b/app/models/vpago/product_decorator.rb index 287d8666..f6a5d6a8 100644 --- a/app/models/vpago/product_decorator.rb +++ b/app/models/vpago/product_decorator.rb @@ -1,11 +1,18 @@ module Vpago module ProductDecorator def self.prepended(base) + base.scope :required, -> { where(optional: false) } + base.scope :optional, -> { where(optional: true) } + base.has_many :payout_profile_products, class_name: 'Spree::PayoutProfileProduct', inverse_of: :product base.has_many :payout_profiles, class_name: 'Spree::PayoutProfile', through: :payout_profile_products, source: :payout_profile - + base.has_many :active_payout_profiles, -> { verified.active }, class_name: 'Spree::PayoutProfile', through: :payout_profile_products, source: :payout_profile base.has_many :active_payway_payout_profiles, -> { payway.verified.active }, class_name: 'Spree::PayoutProfile', through: :payout_profile_products, source: :payout_profile + + # required + base.has_many :required_payout_profile_products, -> { required }, class_name: 'Spree::PayoutProfileProduct', inverse_of: :product + base.has_many :required_active_payout_profiles, -> { verified.active }, class_name: 'Spree::PayoutProfile', through: :payout_profile_products, source: :payout_profile end end end diff --git a/app/overrides/spree/admin/shared/_product_tabs/payout_profile_products.html.erb.deface b/app/overrides/spree/admin/shared/_product_tabs/payout_profile_products.html.erb.deface index 43606497..b7aef11a 100644 --- a/app/overrides/spree/admin/shared/_product_tabs/payout_profile_products.html.erb.deface +++ b/app/overrides/spree/admin/shared/_product_tabs/payout_profile_products.html.erb.deface @@ -2,7 +2,7 @@ <%= content_tag :li, class: 'nav-item' do %> <%= link_to_with_icon 'wallet2.svg', - Spree.t(:payout_profile_products), + Spree.t(:payout), admin_product_payout_profile_products_url(@product), class: "nav-link #{'active' if current == :payout_profile_products}" %> -<% end if can?(:admin, Spree::PayoutProfileProduct) %> \ No newline at end of file +<% end if can?(:admin, Spree::PayoutProfileProduct) %> diff --git a/app/views/spree/admin/payout_profile_products/_form.html.erb b/app/views/spree/admin/payout_profile_products/_form.html.erb index f45ec26a..90d99702 100644 --- a/app/views/spree/admin/payout_profile_products/_form.html.erb +++ b/app/views/spree/admin/payout_profile_products/_form.html.erb @@ -1,5 +1,28 @@ <%= f.field_container :payout_profile_id do %> <%= f.label :payout_profile_id, Spree.t(:payout_profile) %> - <%= f.collection_select :payout_profile_id, @product.vendor.payout_profiles, :id, :name, { prompt: Spree.t('match_choices.none') }, class: 'form-control select2' %> + <%= f.collection_select :payout_profile_id, @product.vendor.payout_profiles, :id, :display_name, { prompt: Spree.t('match_choices.none') }, class: 'form-control select2' %> <%= f.error_message_on :payout_profile_id %> -<% end %> \ No newline at end of file +<% end %> + +<%= render partial: 'payout_profile_info_card', locals: { payout_profile: f.object.payout_profile } if f.object.payout_profile.present? %> + +<%= f.field_container :optional, class: ["form-group card card-body"] do %> + <% default = object.class.columns_hash['optional'].default %> + + <%= Spree.t(:optional) %> +