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

Closes #1189, #1188
  • Loading branch information
Artur Beljajev committed May 10, 2019
1 parent bc4634c commit 68dd660
Show file tree
Hide file tree
Showing 54 changed files with 913 additions and 408 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
end
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
59 changes: 38 additions & 21 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 Down Expand Up @@ -34,7 +41,7 @@ class Invoice < ActiveRecord::Base
before_create :set_invoice_number
before_create :apply_default_vat_rate, unless: :vat_rate?
before_create :calculate_total, unless: :total?
before_create :apply_default_buyer_vat_no, unless: :buyer_vat_no?
before_create :apply_default_vat_no, unless: :buyer_vat_no?

attribute :vat_rate, ::Type::VATRate.new
attr_readonly :vat_rate
Expand All @@ -59,22 +66,6 @@ def to_s
I18n.t('invoice_no', no: number)
end

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

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

def seller_country
Country.new(seller_country_code)
end

def buyer_country
Country.new(buyer_country_code)
end

# order is used for directo/banklink description
def order
"Order nr. #{number}"
Expand Down Expand Up @@ -103,14 +94,40 @@ def as_pdf
generator.as_pdf
end

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 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 apply_default_vat_rate
self.vat_rate = buyer.effective_vat_rate
self.vat_rate = registrar.effective_vat_rate
end

def apply_default_buyer_vat_no
self.buyer_vat_no = buyer.vat_no
def apply_default_vat_no
self.buyer_vat_no = registrar.vat_no
end

def calculate_total
Expand Down
37 changes: 16 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 @@ -61,23 +61,14 @@ 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.instance.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: country_code,
buyer_state: state,
buyer_street: street,
buyer_city: city,
buyer_zip: zip,
buyer_address: billing_address,
buyer_phone: phone,
buyer_url: website,
buyer_email: email,
Expand All @@ -103,18 +94,10 @@ def debit!(args)
cash_account.account_activities.create!(args)
end

def address
[street, city, state, zip].reject(&:blank?).compact.join(', ')
end

def to_s
name
end

def country
Country.new(country_code)
end

def code=(code)
self[:code] = code.gsub(/[ :]/, '').upcase if new_record? && code.present?
end
Expand Down Expand Up @@ -157,6 +140,18 @@ def notify(action)
notifications.create!(text: text)
end

def address
Address.new(street: street,
zip: zip,
city: city,
state: state,
country: Country.new(country_code).to_s)
end

def billing_address
address
end

private

def set_defaults
Expand All @@ -168,7 +163,7 @@ def forbid_special_code
end

def home_vat_payer?
country == Registry.instance.legal_address_country
Country.new(country_code) == Registry.instance.legal_address_country
end

def foreign_vat_payer?
Expand Down
14 changes: 14 additions & 0 deletions app/models/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,18 @@ def vat_rate
def legal_address_country
Country.new(Setting.registry_country_code)
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>
36 changes: 0 additions & 36 deletions app/views/admin/invoices/index.haml

This file was deleted.

Loading

0 comments on commit 68dd660

Please sign in to comment.