Skip to content

Commit

Permalink
Merge pull request 3scale#3840 from 3scale/THREESCALE-9824_invoices_t…
Browse files Browse the repository at this point in the history
…oolbar

🦋Update provider invoices page
  • Loading branch information
josemigallas authored Jul 3, 2024
2 parents c364c6f + b602f19 commit 3898a60
Show file tree
Hide file tree
Showing 37 changed files with 549 additions and 454 deletions.
23 changes: 10 additions & 13 deletions app/controllers/finance/provider/invoices_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
# frozen_string_literal: true

class Finance::Provider::InvoicesController < Finance::Provider::BaseController
include ThreeScale::Search::Helpers

activate_menu :audience, :finance, :invoices

helper Finance::InvoicesHelper
helper ColumnSortingHelper
include ThreeScale::Search::Helpers
helper_method :allow_edit?, :empty_state?
helper_method :allow_edit?, :presenter

before_action :find_buyer, only: [ :create ]
before_action :find_invoice, except: [ :index, :create ]

attr_reader :presenter

def index
@search = ThreeScale::Search.new(params[:search] || params)
@invoices = collection.scope_search(@search).order_by(params[:sort], params[:direction]).paginate(paginate_params).decorate
@years = Invoice.years_by_provider(current_account.id).presence || [(ActiveSupport::TimeZone.new(current_account.timezone) || Time.zone).now.year]
@presenter = Finance::Provider::InvoicesIndexPresenter.new(provider: current_account,
params: params,
user: current_user)
end

def create
Expand Down Expand Up @@ -106,10 +111,6 @@ def allow_edit?
!@invoice.buyer_account.nil?
end

def paginate_params
{ :page => params[:page] || 1, :per_page => 20 }
end

def collection
@collection ||= if params[:account_id]
find_buyer.invoices
Expand All @@ -125,8 +126,4 @@ def find_buyer(options = {})
def find_invoice
@invoice = collection.find(params[:id])
end

def empty_state?
collection.empty?
end
end
2 changes: 1 addition & 1 deletion app/javascript/src/Common/components/TableToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const TableToolbar: FunctionComponent<Props> = ({
))}
</ToolbarGroup>
)}
{overflow && (
{overflow && overflow.length > 0 && (
<ToolbarItem variant="overflow-menu">
<OverflowMenu breakpoint="xl" breakpointReference={toolbarRef}>
<OverflowMenuContent>
Expand Down
1 change: 1 addition & 0 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ def log_entry_created
LogEntry.log( :info, "Invoice created for #{buyer_account.org_name} for period #{period}", provider_account, buyer_account)
end

# TODO: move to decorator
def name
self.period.begin.strftime('%B, %Y')
end
Expand Down
108 changes: 108 additions & 0 deletions app/presenters/finance/provider/invoices_index_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# frozen_string_literal: true

class Finance::Provider::InvoicesIndexPresenter
include System::UrlHelpers.system_url_helpers

delegate :can?, to: :ability

def initialize(provider:, user:, params:)
@provider = provider
@search = ThreeScale::Search.new(params[:search] || params)
@sort_params = [params[:sort], params[:direction]]
@pagination_params = { page: params[:page] || 1, per_page: params[:per_page] || 20 }
@ability = Ability.new(user)
end

attr_reader :provider, :search, :sort_params, :pagination_params, :ability

def empty_state?
raw_invoices.empty?
end

def empty_search?
searched_invoices.empty?
end

def searched_invoices
@searched_invoices ||= raw_invoices.scope_search(search)
end

def invoices
@invoices ||= searched_invoices.order_by(*sort_params)
.paginate(pagination_params)
.decorate
end

def years
@years ||= Invoice.years_by_provider(provider.id).presence || [(ActiveSupport::TimeZone.new(provider.timezone) || Time.zone).now.year]
end

def toolbar_props
props = {
totalEntries: invoices.total_entries,
overflow: [],
attributeFilters: [{
name: 'search[number]',
title: 'Number',
placeholder: '2017-06-* / 2018-*',
chip: search.number
}, {
name: 'search[buyer_query]',
title: 'Account',
placeholder: '',
chip: search.buyer_query
}, {
name: 'search[month_number]',
title: 'Month',
collection: months_for_filter,
placeholder: 'Filter by month',
chip: I18n.t('date.month_names')[search.month_number.to_i]
}, {
name: 'search[year]',
title: 'Year',
collection: years_for_filter,
placeholder: 'Filter by year',
chip: search.year
}, {
name: 'search[state]',
title: 'State',
collection: states_for_filter,
placeholder: 'Filter by state',
chip: search.state&.capitalize
}]
}

if can?(:export, :data)
props[:overflow].append({ href: new_provider_admin_account_data_exports_path,
label: 'Export to CSV',
isShared: false,
variant: :secondary })
end

props
end

private

def raw_invoices
@raw_invoices ||= provider.buyer_invoices.includes(:provider_account)
end

def states_for_filter
Invoice.state_machine.states.keys.collect(&:to_s).sort.map do |state|
{ id: state, title: state.capitalize }
end
end

def months_for_filter
I18n.t('date.month_names').drop(1).map.with_index(1) do |month, i| # First item in array is nil
{ id: i, title: month }
end
end

def years_for_filter
years.map do |year|
{ id: year, title: year.to_s }
end
end
end
61 changes: 0 additions & 61 deletions app/views/finance/provider/invoices/index.html.erb

This file was deleted.

40 changes: 40 additions & 0 deletions app/views/finance/provider/invoices/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
- content_for :page_header_title, t('.page_header_title')

- if presenter.empty_state?
= render partial: 'shared/empty_state', locals: { title: t('.empty_state.title'),
body: t('.empty_state.body') }
- else
- content_for :javascripts do
= javascript_packs_with_chunks_tag 'table_toolbar'

table class="pf-c-table pf-m-grid-lg" role="grid" aria-label="Invoices table" data-toolbar-props=presenter.toolbar_props.to_json
thead
tr role="row"
= th_sortable('friendly_id', 'Number', :admin_finance_invoices_path)
= th_sortable('accounts.org_name', 'Account', :admin_finance_invoices_path)
= th_sortable('period', 'Month', :admin_finance_invoices_path)
th role="columnheader" scope="col" Cost
= th_sortable('state', 'State', :admin_finance_invoices_path)
td

tbody role="rowgroup" class="invoices"
- if presenter.empty_search?
= render partial: 'shared/empty_search_state', locals: { title: t('.empty_search.title'),
body: t('.empty_search.body') }
- else
- presenter.invoices.each do |invoice|
= content_tag_for(:tr, invoice, role: "row") do
td role="cell" data-label="Number"
= link_to(invoice.friendly_id, admin_finance_invoice_path(invoice))
td role="cell" data-label="Account"
= link_to_buyer_or_deleted(invoice.buyer, :admin_buyers_account_invoices_path)
td role="cell" data-label="Month"
= invoice.name
td role="cell" data-label="Cost"
= price_tag(invoice.cost)
td role="cell" data-label="State"
= invoice.state.capitalize
- if invoice.pdf.file?
td role="cell" data-label="Download"
= invoice_pdf_link(invoice)

1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ en:
provider:
invoices:
index:
page_header_title: Invoices
empty_state:
title: Nothing to see here
body: There are no invoices yet
Expand Down
21 changes: 21 additions & 0 deletions features/developer_portal/admin/account/invoices/pdf.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Feature: Dev Portal > Invoices > Download PDF

As a buyer, I want to have the PDF versions of my invoices

Background:
Given a provider on 1st January 2011
And the provider is charging its buyers in prepaid mode
And the provider has "finance" switch visible
And a buyer "Jane"
And the following invoices:
| Buyer | Month | Friendly ID | State |
| Jane | January, 2011 | 2011-01-00000001 | Paid |
And the buyer logs in

Scenario: Download PDF from the invoices table
When they go to the dev portal invoices page
Then there should be a secure link to download the PDF of invoice "2011-01-00000001"

Scenario: Download PDF from the invoice detail
When they go to the invoice "2011-01-00000001" dev portal page
Then there should be a secure link to download the PDF
6 changes: 3 additions & 3 deletions features/old/authorization/provider_finance.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Feature: Provider finance authorization
When I go to the provider dashboard
Then I should see "Billing" within the audience dashboard widget
And they should be able to go to the following pages:
| the finance page |
| the earnings by month page |
| the finance settings page |

Scenario: Members per default cannot access finance
Expand All @@ -26,7 +26,7 @@ Feature: Provider finance authorization
And I go to the provider dashboard
Then I should not see "Billing" within the audience dashboard widget
And they should see an error when going to the following pages:
| the finance page |
| the earnings by month page |
| the finance settings page |

Scenario: Members of finance group can access finance
Expand All @@ -37,5 +37,5 @@ Feature: Provider finance authorization
And I go to the provider dashboard
Then I should see "Billing" within the audience dashboard widget
And they should be able to go to the following pages:
| the finance page |
| the earnings by month page |
| the finance settings page |
2 changes: 1 addition & 1 deletion features/old/finance/change_plans/variable_costs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Feature: Change plan
When time flies to 16th May 2009
And current domain is the admin domain of provider "foo.3scale.localhost"
And I log in as provider "foo.3scale.localhost"
And I go to my earnings
And I go to the earnings by month page
And follow "May, 2009"
Then I should see "EUR 1.00"
Then buyer "stallman" makes 1 service transactions with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Feature: Change plan
| Metric | Value |
| hits | 1 |
And time flies to 3rd June 2009
And I go to my earnings
And I go to the earnings by month page
Then I should have an invoice of "11.0 EUR"

@javascript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Feature: Change plan
When time flies to 16th May 2009
And current domain is the admin domain of provider "foo.3scale.localhost"
And I log in as provider "foo.3scale.localhost"
And I go to my earnings
And I go to the earnings by month page
Then I should have an invoice of "1.0 EUR"
Then buyer "stallman" makes 1 service transactions with:
| Metric | Value |
Expand Down
2 changes: 1 addition & 1 deletion features/old/finance/invoices/create_invoice.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Feature: Create invoice

@javascript
Scenario: Create and view the invoice
And go to the invoices of account "zoidberg" page
And go to the invoices page of account "zoidberg"
Then I should not see "open"
When the date is 1st January 2009
And I follow "Create invoice"
Expand Down
2 changes: 1 addition & 1 deletion features/old/finance/invoices/deleted_account.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Feature: Invoices of deleted account
| Custom | 42 |
And I issue the invoice number "2011-01-00000001"
And account "bob" is deleted
And I go to all provider's invoices page
And I go to the admin portal invoices page
Then I should see 1 invoice
When I follow "2011-01-00000001"
Then I should see "2011-01-00000001"
Expand Down
Loading

0 comments on commit 3898a60

Please sign in to comment.