Skip to content

Commit

Permalink
add spent/earning counters (#1391)
Browse files Browse the repository at this point in the history
* add spent/earning counters
* change invoice placeholders
  • Loading branch information
dmitry-sinina committed Dec 16, 2023
1 parent 199658a commit 92055b3
Show file tree
Hide file tree
Showing 21 changed files with 505 additions and 190 deletions.
7 changes: 3 additions & 4 deletions app/admin/billing/invoice_templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ def find_resource
f.input :name
f.input :template_file, as: :file
end
panel 'test' do
'You can use next placeholders:'
f.actions
panel 'Scalar placeholders' do
text_node 'This list of placeholders you can use anywhere in ODT template'.html_safe
table_for BillingInvoice::GenerateDocument.replaces_list.each do |_x|
column :placeholder do |c|
strong do
Expand All @@ -66,8 +67,6 @@ def find_resource
end
end
end

f.actions
end

show do |_t|
Expand Down
135 changes: 119 additions & 16 deletions app/admin/billing/invoices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,69 @@ def scoped_collection
column :type
column :start_date
column :end_date
column :originated_amount, footer: lambda {
column :amount_total, footer: lambda {
strong do
@footer_data.money_format :total_originated_amount
@footer_data.money_format :total_amount_total
end
} do |c|
strong do
c.decorated_originated_amount
c.decorated_amount_total
end
end
column :terminated_amount, footer: lambda {
column :amount_spent, footer: lambda {
strong do
@footer_data.money_format :total_terminated_amount
@footer_data.money_format :total_amount_spent
end
} do |c|
strong do
c.decorated_terminated_amount
c.decorated_amount_spent
end
end
column :amount_earned, footer: lambda {
strong do
@footer_data.money_format :total_amount_earned
end
} do |c|
strong do
c.decorated_amount_earned
end
end

column :originated_amount_spent, footer: lambda {
strong do
@footer_data.money_format :total_originated_amount_spent
end
} do |c|
strong do
c.decorated_originated_amount_spent
end
end
column :originated_amount_earned, footer: lambda {
strong do
@footer_data.money_format :total_originated_amount_earned
end
} do |c|
strong do
c.decorated_originated_amount_earned
end
end

column :terminated_amount_spent, footer: lambda {
strong do
@footer_data.money_format :total_terminated_amount_spent
end
} do |c|
strong do
c.decorated_terminated_amount_spent
end
end
column :terminated_amount_earned, footer: lambda {
strong do
@footer_data.money_format :total_terminated_amount_earned
end
} do |c|
strong do
c.decorated_terminated_amount_earned
end
end

Expand Down Expand Up @@ -173,11 +220,19 @@ def scoped_collection
filter :type
filter :start_date, as: :date_time_range
filter :end_date, as: :date_time_range
filter :originated_amount

filter :amount_total
filter :amount_spent
filter :amount_earned

filter :originated_amount_spent
filter :originated_amount_earned
filter :terminated_amount_spent
filter :terminated_amount_earned

filter :originated_billing_duration
filter :originated_calls_count
filter :originated_calls_duration
filter :terminated_amount
filter :terminated_billing_duration
filter :terminated_calls_count
filter :terminated_calls_duration
Expand All @@ -197,19 +252,45 @@ def scoped_collection
row :start_date
row :end_date
row :created_at

row :amount_total do
strong do
s.decorated_amount_total
end
end
row :amount_spent do
strong do
s.decorated_amount_spent
end
end
row :amount_earned do
strong do
s.decorated_amount_earned
end
end
end
end
panel 'Traffic summary' do
attributes_table_for s do
row :originated_amount do
row :originated_amount_spent do
strong do
s.decorated_originated_amount_spent
end
end
row :originated_amount_earned do
strong do
s.decorated_originated_amount
s.decorated_originated_amount_earned
end
end
row :originated_calls_count
row :terminated_amount do
row :terminated_amount_spent do
strong do
s.decorated_terminated_amount_spent
end
end
row :terminated_amount_earned do
strong do
s.decorated_terminated_amount
s.decorated_terminated_amount_earned
end
end
row :terminated_calls_count
Expand All @@ -219,9 +300,14 @@ def scoped_collection
tab 'Originated traffic' do
panel 'Summary' do
attributes_table_for s do
row :originated_amount do
row :originated_amount_spent do
strong do
s.decorated_originated_amount_spent
end
end
row :originated_amount_earned do
strong do
s.decorated_originated_amount
s.decorated_originated_amount_earned
end
end
row :originated_calls_count
Expand All @@ -238,6 +324,9 @@ def scoped_collection
end
panel 'By destination(destination prefix)' do
table_for resource.originated_destinations do
column 'type' do |s|
s.spent ? status_tag('spent', class: :blue) : status_tag('earned', class: :green)
end
column :dst_prefix
column :country
column :network
Expand All @@ -259,6 +348,9 @@ def scoped_collection
end
panel 'By destination number country/network' do
table_for resource.originated_networks do
column 'type' do |s|
s.spent ? status_tag('spent', class: :blue) : status_tag('earned', class: :green)
end
column :country
column :network
column :rate
Expand All @@ -284,9 +376,14 @@ def scoped_collection
tab 'Terminated traffic' do
panel 'Summary' do
attributes_table_for s do
row :terminated_amount do
row :terminated_amount_spent do
strong do
s.decorated_terminated_amount
s.decorated_terminated_amount_spent
end
end
row :terminated_amount_earned do
strong do
s.decorated_terminated_amount_earned
end
end
row :terminated_calls_count
Expand All @@ -303,6 +400,9 @@ def scoped_collection
end
panel 'By destination(dialpeer prefix)' do
table_for resource.terminated_destinations do
column 'type' do |s|
s.spent ? status_tag('spent', class: :blue) : status_tag('earned', class: :green)
end
column :dst_prefix
column :country
column :network
Expand All @@ -323,6 +423,9 @@ def scoped_collection
end
panel 'By destination number country/network' do
table_for resource.terminated_networks do
column 'type' do |s|
s.spent ? status_tag('spent', class: :blue) : status_tag('earned', class: :green)
end
column :country
column :network
column :rate
Expand Down
36 changes: 28 additions & 8 deletions app/decorators/invoice_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,37 @@ class InvoiceDecorator < BillingDecorator
delegate_all
decorates Billing::Invoice

decorates_association :originated_destinations, with: InvoiceOriginatedDestinationDecorator
decorates_association :terminated_destinations, with: InvoiceTerminatedDestinationDecorator
decorates_association :originated_networks, with: InvoiceOriginatedNetworkDecorator
decorates_association :terminated_networks, with: InvoiceTerminatedNetworkDecorator
decorates_association :originated_destinations, with: InvoiceOriginatedDestinationDecorator, scope: :for_invoice
decorates_association :terminated_destinations, with: InvoiceTerminatedDestinationDecorator, scope: :for_invoice
decorates_association :originated_networks, with: InvoiceOriginatedNetworkDecorator, scope: :for_invoice
decorates_association :terminated_networks, with: InvoiceTerminatedNetworkDecorator, scope: :for_invoice

def decorated_originated_amount
money_format :originated_amount
def decorated_amount_total
money_format :amount_total
end

def decorated_terminated_amount
money_format :terminated_amount
def decorated_amount_spent
money_format :amount_spent
end

def decorated_amount_earned
money_format :amount_earned
end

def decorated_originated_amount_spent
money_format :originated_amount_spent
end

def decorated_originated_amount_earned
money_format :originated_amount_earned
end

def decorated_terminated_amount_spent
money_format :terminated_amount_spent
end

def decorated_terminated_amount_earned
money_format :terminated_amount_earned
end

def decorated_originated_calls_duration
Expand Down
31 changes: 24 additions & 7 deletions app/models/billing/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
# Table name: billing.invoices
#
# id :integer(4) not null, primary key
# amount_earned :decimal(, ) default(0.0), not null
# amount_spent :decimal(, ) default(0.0), not null
# amount_total :decimal(, ) default(0.0), not null
# end_date :timestamptz not null
# first_originated_call_at :timestamptz
# first_terminated_call_at :timestamptz
# last_originated_call_at :timestamptz
# last_terminated_call_at :timestamptz
# originated_amount :decimal(, ) default(0.0), not null
# originated_amount_earned :decimal(, ) default(0.0), not null
# originated_amount_spent :decimal(, ) default(0.0), not null
# originated_billing_duration :bigint(8) default(0), not null
# originated_calls_count :bigint(8) default(0), not null
# originated_calls_duration :bigint(8) default(0), not null
# originated_successful_calls_count :bigint(8) default(0), not null
# reference :string
# start_date :timestamptz not null
# terminated_amount :decimal(, ) default(0.0), not null
# terminated_amount_earned :decimal(, ) default(0.0), not null
# terminated_amount_spent :decimal(, ) default(0.0), not null
# terminated_billing_duration :integer(4) default(0), not null
# terminated_calls_count :integer(4) default(0), not null
# terminated_calls_duration :integer(4) default(0), not null
Expand All @@ -41,11 +46,16 @@ class Billing::Invoice < Cdr::Base
include WithPaperTrail

Totals = Struct.new(
:total_originated_amount,
:total_amount_total,
:total_amount_spent,
:total_amount_earned,
:total_originated_amount_spent,
:total_originated_amount_earned,
:total_originated_calls_count,
:total_originated_calls_duration,
:total_originated_billing_duration,
:total_terminated_amount,
:total_terminated_amount_spent,
:total_terminated_amount_earned,
:total_terminated_calls_count,
:total_terminated_calls_duration,
:total_terminated_billing_duration
Expand All @@ -54,11 +64,16 @@ class Billing::Invoice < Cdr::Base
class << self
def totals
row = extending(ActsAsTotalsRelation).totals_row_by(
'sum(originated_amount) as total_originated_amount',
'sum(amount_total) as total_amount_total',
'sum(amount_spent) as total_amount_spent',
'sum(amount_earned) as total_amount_earned',
'sum(originated_amount_spent) as total_originated_amount_spent',
'sum(originated_amount_earned) as total_originated_amount_earned',
'sum(originated_calls_count) as total_originated_calls_count',
'sum(originated_calls_duration) as total_originated_calls_duration',
'sum(originated_billing_duration) as total_originated_billing_duration',
'sum(terminated_amount) as total_terminated_amount',
'sum(terminated_amount_spent) as total_terminated_amount_spent',
'sum(terminated_amount_earned) as total_terminated_amount_earned',
'sum(terminated_calls_count) as total_terminated_calls_count',
'sum(terminated_calls_duration) as total_terminated_calls_duration',
'sum(terminated_billing_duration) as total_terminated_billing_duration'
Expand Down Expand Up @@ -109,7 +124,9 @@ def last_end_date(account_id:)
validates :type_id, inclusion: { in: Billing::InvoiceType.ids }, allow_nil: true

validate :validate_dates
validates :originated_amount, :terminated_amount, numericality: { greater_than_or_equal_to: 0 }
validates :amount_spent, :amount_earned,
:originated_amount_spent, :originated_amount_earned,
:terminated_amount_spent, :terminated_amount_earned, numericality: { greater_than_or_equal_to: 0 }

validates :originated_billing_duration,
:originated_calls_count,
Expand Down
7 changes: 4 additions & 3 deletions app/models/billing/invoice_originated_destination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# first_call_at :timestamptz
# last_call_at :timestamptz
# rate :decimal(, )
# spent :boolean default(TRUE), not null
# successful_calls_count :bigint(8)
# country_id :integer(4)
# invoice_id :integer(4) not null
Expand All @@ -34,8 +35,7 @@ class Billing::InvoiceOriginatedDestination < Cdr::Base
belongs_to :country, class_name: 'System::Country', foreign_key: :country_id, optional: true
belongs_to :network, class_name: 'System::Network', foreign_key: :network_id, optional: true

scope :for_invoice, -> { preload(:country, :network) }
scope :successful_calls, -> { where('successful_calls_count>0') }
scope :for_invoice, -> { preload(:country, :network).order(:spent, :dst_prefix) }

def self.to_csv
csv_string = CSV.generate do |csv|
Expand Down Expand Up @@ -64,7 +64,8 @@ def self.summary
coalesce(sum(successful_calls_count),0) as successful_calls_count,
coalesce(sum(calls_duration),0) as calls_duration,
coalesce(sum(billing_duration),0) as billing_duration,
COALESCE(sum(amount),0) as amount,
COALESCE(sum(amount) FILTER ( WHERE spent ), 0) as amount_spent,
COALESCE(sum(amount) FILTER ( WHERE NOT spent ),0) as amount_earned,
min(first_call_at) as first_call_at,
max(last_call_at) as last_call_at
").to_a[0]
Expand Down
Loading

0 comments on commit 92055b3

Please sign in to comment.