From 6920568f195ce4197d1d71b842e423f89eb3bc8d Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Fri, 15 Sep 2023 12:05:10 +0300 Subject: [PATCH] fixed invoice total sorting --- app/models/invoice.rb | 18 +++++++++++++++--- app/views/admin/invoices/index.html.erb | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 30f263b53..f41fb387f 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -61,6 +61,15 @@ 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 @@ -75,12 +84,15 @@ def self.search(params = {}) query = with_search_scope(params[:search_string]).with_statuses(params[:statuses_contains]) - if params[:sort] == 'channel' + case params[:sort] + when 'channel' query.left_outer_joins(:payment_orders) .select("invoices.*, REPLACE(payment_orders.type, 'PaymentOrders::', '') AS payment_order_channel") .order(Arel.sql("payment_order_channel #{sort_direction} NULLS LAST")) - elsif params[:sort] == 'billing_profile_name' - query.left_outer_joins(:billing_profile).order("billing_profiles.name #{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") else query.order("#{sort_column} #{sort_direction} NULLS LAST") end diff --git a/app/views/admin/invoices/index.html.erb b/app/views/admin/invoices/index.html.erb index 787bbd924..a1ebe0821 100644 --- a/app/views/admin/invoices/index.html.erb +++ b/app/views/admin/invoices/index.html.erb @@ -60,7 +60,7 @@ <%= t('invoices.total') %> - <%= sort_link_to "", "cents" %> + <%= sort_link_to "", "total" %> <%= t('invoices.vat_rate_on_payment') %>