Skip to content

Commit

Permalink
fixed by total sort column
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Sep 15, 2023
1 parent 6920568 commit 53dc809
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
9 changes: 7 additions & 2 deletions app/controllers/admin/invoices_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# rubocop:disable Metrics/ClassLength
# rubocop:disable Metrics
# require 'invoice_already_paid'

module Admin
Expand All @@ -19,7 +19,12 @@ def index
.includes(:paid_with_payment_order)
.search(params)

@pagy, @invoices = pagy(invoices, items: params[:per_page] ||= 15)
if invoices.is_a?(Array)
@pagy, @invoices = pagy_array(invoices, items: params[:per_page] ||= 15)
else
@pagy, @invoices = pagy(invoices, items: params[:per_page] ||= 15)
end

end

# GET /admin/invoices/aa450f1a-45e2-4f22-b2c3-f5f46b5f906b/download
Expand Down
39 changes: 28 additions & 11 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Invoice < ApplicationRecord
delegate :enable_deposit?, to: :enable_deposit?
delegate :deposit, to: :deposit

attr_accessor :vat_rate

scope :with_search_scope, (lambda do |origin|
if origin.present?
if numeric?(origin)
Expand Down Expand Up @@ -61,15 +63,6 @@ class Invoice < ApplicationRecord
Time.zone.today + number_of_days, statuses[:issued])
}

def self.with_totals
select("invoices.*,
(CASE WHEN auctions.enable_deposit
THEN ROUND((invoices.cents / 100.0 * (1 + COALESCE(invoices.vat_rate, 0.0)) - auctions.requirement_deposit_in_cents / 100.0), 2)
ELSE ROUND((invoices.cents / 100.0 * (1 + COALESCE(invoices.vat_rate, 0.0))), 2)
END) AS total_value")
.joins(result: :auction)
end

def self.search(params = {})
sort_column = params[:sort].presence_in(%w[paid_through
paid_amount
Expand All @@ -86,13 +79,37 @@ def self.search(params = {})

case params[:sort]
when 'channel'
query.left_outer_joins(:payment_orders)
# invoices_array = query.to_a

# if sort_direction == 'asc'
# invoices_array.sort_by do |invoice|
# invoice.paid_with_payment_order&.channel || ''
# end
# else
# invoices_array.sort_by do |invoice|
# invoice.paid_with_payment_order&.channel || ''
# end.reverse
# end

query.left_outer_joins(:paid_with_payment_order)
.select("invoices.*, REPLACE(payment_orders.type, 'PaymentOrders::', '') AS payment_order_channel")
.order(Arel.sql("payment_order_channel #{sort_direction} NULLS LAST"))

# query.left_outer_joins(:paid_with_payment_order)
# .select("invoices.*, COALESCE(REPLACE(paid_with_payment_orders.type, 'PaymentOrders::', ''), '') AS payment_order_channel")
# .order(Arel.sql("payment_order_channel #{sort_direction} NULLS LAST"))


when 'billing_profile_name'
query.left_outer_joins(:billing_profile).order("billing_profiles.name #{sort_direction}")
when 'total'
query.with_totals.order("total_value #{sort_direction} NULLS LAST")
invoices_array = query.to_a

if sort_direction == 'asc'
invoices_array.sort_by(&:total)
else
invoices_array.sort_by(&:total).reverse
end
else
query.order("#{sort_column} #{sort_direction} NULLS LAST")
end
Expand Down
2 changes: 2 additions & 0 deletions config/initializers/pagy.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'pagy/extras/overflow'
require "pagy/extras/array"

Pagy::I18n.load({ locale: 'en',
filepath: "#{Rails.root}/config/locales/pagy.en.yml" },
{ locale: 'et',
Expand Down

0 comments on commit 53dc809

Please sign in to comment.