Skip to content

Commit

Permalink
Close #2181 Billing export page insert new columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Regene27 committed Dec 24, 2024
1 parent 15a4239 commit 077db97
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
42 changes: 32 additions & 10 deletions app/controllers/spree/billing/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,44 @@ def customers_scope

def load_orders_by_month
@search = orders_scope.where(payment_state: %w[paid balance_due failed])
.joins(:invoice)
.where('EXTRACT(YEAR FROM cm_invoices.date) = ?', @year).ransack(params[:q]).result
.joins(:invoice)
.where('EXTRACT(YEAR FROM cm_invoices.date) = ?', @year).ransack(params[:q]).result

initialize_order_hashes

(1..12).each do |month|
load_monthly_orders(month)
end
end

def initialize_order_hashes
@orders = {}
@total = {}
@paid = {}
@balance_due = {}
@voided = {}

(1..12).each do |month|
@orders[month] = @search.joins(:invoice).where('EXTRACT(MONTH FROM cm_invoices.date) = ?', month)
@total[month] = Spree::Money.new(@orders[month].sum(:total)).to_s
@paid[month] = Spree::Money.new(@orders[month].where(payment_state: :paid).sum(:total)).to_s
@balance_due[month] = Spree::Money.new(@orders[month].where(payment_state: :balance_due).sum(:total)).to_s
@voided[month] = Spree::Money.new(@orders[month].where(payment_state: :failed).sum(:total)).to_s
end
@previous_month_carried_forward = {}
@current_month_total = {}
end

def load_monthly_orders(month)
@orders[month] = @search.joins(:invoice).where('EXTRACT(MONTH FROM cm_invoices.date) = ?', month)
@total[month] = Spree::Money.new(@orders[month].sum(:total)).to_s
@paid[month] = Spree::Money.new(@orders[month].where(payment_state: :paid).sum(:total)).to_s
@balance_due[month] = Spree::Money.new(@orders[month].where(payment_state: :balance_due).sum(:total)).to_s
@voided[month] = Spree::Money.new(@orders[month].where(payment_state: :failed).sum(:total)).to_s

previous_month_orders = if month == 1
@search.joins(:invoice).where(
'EXTRACT(YEAR FROM cm_invoices.date) = ? AND EXTRACT(MONTH FROM cm_invoices.date) = ?', @year.to_i - 1, 12
)
else
@orders[month - 1]
end

@previous_month_carried_forward[month] = Spree::Money.new(previous_month_orders.sum(&:outstanding_balance)).to_s
current_month_total = @orders[month].sum(:total) - previous_month_orders.sum(&:outstanding_balance)
@current_month_total[month] = Spree::Money.new([current_month_total, 0].max).to_s
end

def handle_exceeding_range
Expand Down
4 changes: 4 additions & 0 deletions app/views/spree/billing/reports/export.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
<tr data-hook="admin_subscriptions_index_headers">
<th class="text-center"><%= Spree.t('billing.month') %></th>
<th class="text-center"><%= Spree.t('billing.total_amount') %></th>
<th class="text-center"><%= Spree.t('billing.current_month_total') %></th>
<th class="text-center"><%= Spree.t('billing.previous_month_carried_forward') %></th>
<th class="text-center"><%= Spree.t('billing.paid') %></th>
<th class="text-center"><%= Spree.t('billing.balance_due') %></th>
<th class="text-center"><%= Spree.t('billing.voided') %></th>
Expand All @@ -50,6 +52,8 @@
<tr>
<td class="text-center"><%= Date::MONTHNAMES[month] %></td>
<td class="text-center"><%= @total[month] %></td>
<td class="text-center"><%= @current_month_total[month] %></td>
<td class="text-center"><%= @previous_month_carried_forward[month] %></td>
<td class="text-center"><%= @paid[month] %></td>
<td class="text-center"><%= @balance_due[month] %></td>
<td class="text-center"><%= @voided[month] %></td>
Expand Down

0 comments on commit 077db97

Please sign in to comment.