Skip to content

Commit

Permalink
Refactor invoices
Browse files Browse the repository at this point in the history
- Combine invoice seller and buyer address parts
- Convert HAML to ERB
- Extract translations
- Extract partials
- Improve database structure
- Improve UI
- Remove unnecessary validations

Closes #1189, #1188
  • Loading branch information
Artur Beljajev committed Jun 21, 2019
1 parent 3b3c996 commit fa7e861
Show file tree
Hide file tree
Showing 53 changed files with 880 additions and 433 deletions.
2 changes: 1 addition & 1 deletion app/controllers/concerns/deliverable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Deliverable

def new
authorize! :manage, @invoice
@recipient = @invoice.buyer.billing_email
@recipient = @invoice.registrar.billing_email
end

def create
Expand Down
2 changes: 1 addition & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def epp # Registrar/api_user dynamic role
end

def billing # Registrar/api_user dynamic role
can(:manage, Invoice) { |i| i.buyer_id == @user.registrar_id }
can(:manage, Invoice) { |i| i.registrar == @user.registrar }
can :manage, :deposit
can :read, AccountActivity
can :manage, :balance_auto_reload
Expand Down
36 changes: 36 additions & 0 deletions app/models/address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Address
attr_reader :parts

def initialize(parts)
@parts = parts
end

def street
parts[:street]
end

def zip
parts[:zip]
end

def city
parts[:city]
end

def state
parts[:state]
end

def country
parts[:country]
end

def ==(other)
parts == other.parts
end

def to_s
ordered_parts = [street, city, state, zip, country]
ordered_parts.reject(&:blank?).compact.join(', ')
end
end
7 changes: 7 additions & 0 deletions app/models/bank_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class BankAccount
include ActiveModel::Model

attr_accessor :iban
attr_accessor :swift
attr_accessor :bank_name
end
4 changes: 2 additions & 2 deletions app/models/bank_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def invoice
end

def registrar
@registrar ||= Invoice.find_by(reference_no: reference_no)&.buyer
@registrar ||= Invoice.find_by(reference_no: reference_no)&.registrar
end


Expand Down Expand Up @@ -75,7 +75,7 @@ def bind_invoice(invoice_no)
return
end

create_activity(invoice.buyer, invoice)
create_activity(invoice.registrar, invoice)
end

def create_activity(registrar, invoice)
Expand Down
11 changes: 11 additions & 0 deletions app/models/company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Company
include ActiveModel::Model

attr_accessor :name
attr_accessor :registration_number
attr_accessor :vat_number
attr_accessor :address
attr_accessor :email
attr_accessor :phone
attr_accessor :website
end
2 changes: 1 addition & 1 deletion app/models/directo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.send_receipts
"InvoiceDate" => invoice.issue_date.strftime("%Y-%m-%d"),
"PaymentTerm" => Setting.directo_receipt_payment_term,
"Currency" => invoice.currency,
"CustomerCode"=> invoice.buyer.accounting_customer_code
"CustomerCode"=> invoice.registrar.accounting_customer_code
){
xml.line(
"ProductID" => Setting.directo_receipt_product_name,
Expand Down
49 changes: 38 additions & 11 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
class Invoice < ActiveRecord::Base
class Buyer < Company; end

class Seller < Company
attr_accessor :contact_person
attr_accessor :bank_account
end

include Versions
include Concerns::Invoice::Cancellable
include Concerns::Invoice::Payable

belongs_to :seller, class_name: 'Registrar'
belongs_to :buyer, class_name: 'Registrar'
belongs_to :registrar
has_one :account_activity
has_many :items, class_name: 'InvoiceItem', dependent: :destroy
has_many :directo_records, as: :item, class_name: 'Directo'
Expand All @@ -26,12 +33,10 @@ class Invoice < ActiveRecord::Base

scope :overdue, -> { unpaid.non_cancelled.where('due_date < ?', Time.zone.today) }

validates :due_date, :currency, :seller_name,
:seller_iban, :buyer_name, :items, presence: true
validates :items, presence: true

before_create :set_invoice_number
before_create :calculate_total, unless: :total?
before_create :apply_default_buyer_vat_no, unless: :buyer_vat_no?

attribute :vat_rate, ::Type::VATRate.new

Expand All @@ -56,19 +61,19 @@ def to_s
end

def seller_address
[seller_street, seller_city, seller_state, seller_zip].reject(&:blank?).compact.join(', ')
[seller_address_street, seller_address_city, seller_address_state, seller_address_zip].reject(&:blank?).compact.join(', ')
end

def buyer_address
[buyer_street, buyer_city, buyer_state, buyer_zip].reject(&:blank?).compact.join(', ')
[buyer_address_street, buyer_address_city, buyer_address_state, buyer_address_zip].reject(&:blank?).compact.join(', ')
end

def seller_country
Country.new(seller_country_code)
Country.new(seller_address_country_code)
end

def buyer_country
Country.new(buyer_country_code)
Country.new(buyer_address_country_code)
end

# order is used for directo/banklink description
Expand Down Expand Up @@ -103,12 +108,34 @@ def to_e_invoice
generator.generate
end

private
def seller
bank_account = BankAccount.new(iban: seller_iban,
swift: seller_swift,
bank_name: seller_bank)

Seller.new(name: seller_name,
registration_number: seller_reg_no,
vat_number: seller_vat_no,
address: seller_address,
email: seller_email,
phone: seller_phone,
website: seller_url,
contact_person: seller_contact_name,
bank_account: bank_account)
end

def apply_default_buyer_vat_no
self.buyer_vat_no = buyer.vat_no
def buyer
Buyer.new(name: buyer_name,
registration_number: buyer_reg_no,
vat_number: buyer_vat_no,
address: buyer_address,
email: buyer_email,
phone: buyer_phone,
website: buyer_url)
end

private

def calculate_total
self.total = subtotal + vat_amount
end
Expand Down
20 changes: 10 additions & 10 deletions app/models/invoice/e_invoice_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ def initialize(invoice)

def generate
seller = EInvoice::Seller.new
seller.name = invoice.seller_name
seller.registration_number = invoice.seller_reg_no
seller.vat_number = invoice.seller_vat_no
seller.name = invoice.seller.name
seller.registration_number = invoice.seller.registration_number
seller.vat_number = invoice.seller.vat_number

seller_legal_address = EInvoice::Address.new
seller_legal_address.line1 = invoice.seller_street
Expand All @@ -21,10 +21,10 @@ def generate
seller.legal_address = seller_legal_address

buyer = EInvoice::Buyer.new
buyer.name = invoice.buyer_name
buyer.registration_number = invoice.buyer_reg_no
buyer.vat_number = invoice.buyer_vat_no
buyer.email = invoice.buyer.billing_email
buyer.name = invoice.buyer.name
buyer.registration_number = invoice.buyer.registration_number
buyer.vat_number = invoice.buyer.vat_number
buyer.email = invoice.registrar.billing_email

buyer_bank_account = EInvoice::BankAccount.new
buyer_bank_account.number = invoice.buyer.e_invoice_iban
Expand Down Expand Up @@ -62,9 +62,9 @@ def generate
i.recipient_id_code = invoice.buyer_reg_no
i.reference_number = invoice.reference_no
i.due_date = invoice.due_date
i.beneficiary_name = invoice.seller_name
i.beneficiary_account_number = invoice.seller_iban
i.payer_name = invoice.buyer_name
i.beneficiary_name = invoice.seller.name
i.beneficiary_account_number = invoice.seller.bank_account.iban
i.payer_name = invoice.buyer.name
i.subtotal = invoice.subtotal
i.vat_amount = invoice.vat_amount
i.total = invoice.total
Expand Down
38 changes: 17 additions & 21 deletions app/models/registrar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Registrar < ActiveRecord::Base
has_many :contacts, dependent: :restrict_with_error
has_many :api_users, dependent: :restrict_with_error
has_many :notifications
has_many :invoices, foreign_key: 'buyer_id'
has_many :invoices
has_many :accounts, dependent: :destroy
has_many :nameservers, through: :domains
has_many :whois_records
Expand Down Expand Up @@ -64,23 +64,15 @@ def issue_prepayment_invoice(amount, description = nil)
seller_bank: Setting.registry_bank,
seller_swift: Setting.registry_swift,
seller_vat_no: Setting.registry_vat_no,
seller_country_code: Setting.registry_country_code,
seller_state: Setting.registry_state,
seller_street: Setting.registry_street,
seller_city: Setting.registry_city,
seller_zip: Setting.registry_zip,
seller_address: Registry.current.billing_address,
seller_phone: Setting.registry_phone,
seller_url: Setting.registry_url,
seller_email: Setting.registry_email,
seller_contact_name: Setting.registry_invoice_contact,
buyer: self,
buyer_name: name,
buyer_reg_no: reg_no,
buyer_country_code: address_country_code,
buyer_state: address_state,
buyer_street: address_street,
buyer_city: address_city,
buyer_zip: address_zip,
buyer_vat_no: vat_no,
buyer_address: billing_address,
buyer_phone: phone,
buyer_url: website,
buyer_email: email,
Expand All @@ -107,18 +99,10 @@ def debit!(args)
cash_account.account_activities.create!(args)
end

def address
[address_street, address_city, address_state, address_zip].reject(&:blank?).compact.join(', ')
end

def to_s
name
end

def country
Country.new(address_country_code)
end

def code=(code)
self[:code] = code.gsub(/[ :]/, '').upcase if new_record? && code.present?
end
Expand Down Expand Up @@ -153,7 +137,7 @@ def vat_country=(country)
end

def vat_country
country
Country.new(address_country_code)
end

def vat_liable_locally?(registry = Registry.current)
Expand All @@ -169,6 +153,18 @@ def e_invoice_iban
iban
end

def address
Address.new(street: address_street,
zip: address_zip,
city: address_city,
state: address_state,
country: Country.new(address_country_code).to_s)
end

def billing_address
address
end

private

def set_defaults
Expand Down
14 changes: 14 additions & 0 deletions app/models/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,18 @@ def self.current

new(vat_rate: vat_rate, vat_country: vat_country)
end

def billing_address
address
end

private

def address
Address.new(street: Setting.registry_street,
zip: Setting.registry_zip,
city: Setting.registry_city,
state: Setting.registry_state,
country: Country.new(Setting.registry_country_code))
end
end
2 changes: 1 addition & 1 deletion app/views/admin/base/_menu.haml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
- if can? :view, Billing::Price
%li= link_to t('.prices'), admin_prices_path
%li= link_to t(:bank_statements), admin_bank_statements_path
%li= link_to t(:invoices), admin_invoices_path
%li= link_to t('.invoices'), admin_invoices_path
%li= link_to t(:account_activities), admin_account_activities_path(created_after: 'today')
%li.divider
%li.dropdown-header= t('.archive')
Expand Down
28 changes: 28 additions & 0 deletions app/views/admin/invoices/_invoice.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<tr>
<td><%= link_to invoice, admin_invoice_path(invoice) %></td>
<td><%= link_to invoice.registrar, admin_registrar_path(invoice.registrar) %></td>

<% if invoice.cancelled? %>
<td class="text-grey">
<%= t(:cancelled) %>
</td>
<% else %>
<td>
<%= l invoice.due_date %>
</td>
<% end %>
<% if invoice.paid? %>
<td>
<%= l invoice.receipt_date %>
</td>
<% elsif invoice.cancelled? %>
<td class="text-grey">
<%= t(:cancelled) %>
</td>
<% else %>
<td class="text-danger">
<%= t(:unpaid) %>
</td>
<% end %>
</tr>
Loading

0 comments on commit fa7e861

Please sign in to comment.