Invoice migration improvements, invoice issuing

This commit is contained in:
Martin Lensment 2015-04-08 16:40:38 +03:00
parent 7fe851d480
commit 5c0e2be387
12 changed files with 187 additions and 34 deletions

View file

@ -1,2 +1,6 @@
class Invoice < ActiveRecord::Base class Invoice < ActiveRecord::Base
belongs_to :seller, class_name: 'Registrar'
belongs_to :buyer, class_name: 'Registrar'
has_many :invoice_items
accepts_nested_attributes_for :invoice_items
end end

View file

@ -0,0 +1,3 @@
class InvoiceItem < ActiveRecord::Base
belongs_to :invoice
end

View file

@ -6,6 +6,7 @@ class Registrar < ActiveRecord::Base
has_many :api_users, dependent: :restrict_with_error has_many :api_users, dependent: :restrict_with_error
has_many :messages has_many :messages
belongs_to :country_deprecated, foreign_key: :country_id belongs_to :country_deprecated, foreign_key: :country_id
has_many :invoices, foreign_key: 'buyer_id'
validates :name, :reg_no, :country_code, :email, presence: true validates :name, :reg_no, :country_code, :email, presence: true
validates :name, :reg_no, uniqueness: true validates :name, :reg_no, uniqueness: true
@ -19,6 +20,59 @@ class Registrar < ActiveRecord::Base
res = search(name_or_reg_no_cont: query).result res = search(name_or_reg_no_cont: query).result
res.reduce([]) { |o, v| o << { id: v[:id], display_key: "#{v[:name]} (#{v[:reg_no]})" } } res.reduce([]) { |o, v| o << { id: v[:id], display_key: "#{v[:name]} (#{v[:reg_no]})" } }
end end
def eis
find_by(reg_no: '90010019')
end
end
def issue_prepayment_invoice(amount, description = nil)
# Currently only EIS can issue invoices
eis = self.class.eis
invoices.create(
invoice_type: 'DEB',
due_date: Time.zone.now.to_date + 1.day,
payment_term: 'prepayment',
description: description,
currency: 'EUR',
vat_prc: 0.2,
seller_id: eis.id,
seller_name: eis.name,
seller_reg_no: eis.reg_no,
seller_iban: Setting.eis_iban,
seller_bank: Setting.eis_bank,
seller_swift: Setting.eis_swift,
seller_vat_no: eis.vat_no,
seller_country_code: eis.country_code,
seller_state: eis.state,
seller_street: eis.street,
seller_city: eis.city,
seller_zip: eis.zip,
seller_phone: eis.phone,
seller_url: eis.url,
seller_email: eis.email,
seller_contact_name: Setting.eis_invoice_contact,
buyer_id: id,
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_phone: phone,
buyer_url: url,
buyer_email: email,
invoice_items_attributes: [
{
description: 'prepayment',
item_unit: 'piece',
item_amount: 1,
item_price: amount
}
]
)
end end
def domain_transfers def domain_transfers

View file

@ -23,6 +23,11 @@ if con.present? && con.table_exists?('settings')
Setting.save_default(:transfer_wait_time, 0) Setting.save_default(:transfer_wait_time, 0)
Setting.save_default(:client_side_status_editing_enabled, false) Setting.save_default(:client_side_status_editing_enabled, false)
Setting.save_default(:eis_iban, 'EE557700771000598731')
Setting.save_default(:eis_bank, 'LHV Pank')
Setting.save_default(:eis_swift, 'LHVBEE22')
Setting.save_default(:eis_invoice_contact, 'Martti Õigus')
end end
# dev only setting # dev only setting

View file

@ -3,16 +3,23 @@ class AddInvoiceColumns < ActiveRecord::Migration
# invoice info # invoice info
# invoice number comes from id # invoice number comes from id
add_column :invoices, :invoice_type, :string, null: false # CRE / DEB add_column :invoices, :invoice_type, :string, null: false # CRE / DEB
add_column :invoices, :document_name, :string, null: false # Arve # add_column :invoices, :document_name, :string, null: false # Invoice / credit invoice ...
add_column :invoices, :due_date, :datetime, null: false add_column :invoices, :due_date, :datetime, null: false
add_column :invoices, :payment_term, :string # maksetingimus (free text) add_column :invoices, :payment_term, :string # free text
add_column :invoices, :currency, :string, null: false add_column :invoices, :currency, :string, null: false
add_column :invoices, :description, :string, null: false # Selgitus add_column :invoices, :description, :string
add_column :invoices, :reference_no, :string # Viitenumber
add_column :invoices, :total_sum, :decimal add_column :invoices, :reference_no, :string
add_column :invoices, :vat_prc, :decimal
#add_column :invoices, :total_sum, :decimal # calculate on the fly
add_column :invoices, :paid_at, :datetime
# seller info # seller info
# add_column :invoices, :sellable_id, :integer # EIS is actually a registrar itself and invoice can belong to EIS
# add_column :invoices, :sellable_type, :string
add_column :invoices, :seller_id, :integer
add_column :invoices, :seller_name, :string, null: false add_column :invoices, :seller_name, :string, null: false
add_column :invoices, :seller_reg_no, :string add_column :invoices, :seller_reg_no, :string
add_column :invoices, :seller_iban, :string, null: false add_column :invoices, :seller_iban, :string, null: false
@ -20,6 +27,8 @@ class AddInvoiceColumns < ActiveRecord::Migration
add_column :invoices, :seller_swift, :string, null: false add_column :invoices, :seller_swift, :string, null: false
add_column :invoices, :seller_vat_no, :string add_column :invoices, :seller_vat_no, :string
add_column :invoices, :seller_country_code, :string
add_column :invoices, :seller_state, :string
add_column :invoices, :seller_street, :string add_column :invoices, :seller_street, :string
add_column :invoices, :seller_city, :string add_column :invoices, :seller_city, :string
add_column :invoices, :seller_zip, :string add_column :invoices, :seller_zip, :string
@ -29,35 +38,24 @@ class AddInvoiceColumns < ActiveRecord::Migration
add_column :invoices, :seller_contact_name, :string add_column :invoices, :seller_contact_name, :string
# payer info # buyer info
add_column :invoices, :payer_name, :string, null: false # add_column :invoices, :payable_id, :integer
add_column :invoices, :payer_reg_no, :string, null: false # add_column :invoices, :payable_type, :string
add_column :invoices, :buyer_id, :integer
add_column :invoices, :payer_street, :string add_column :invoices, :buyer_name, :string, null: false
add_column :invoices, :payer_city, :string add_column :invoices, :buyer_reg_no, :string, null: false
add_column :invoices, :payer_zip, :string
add_column :invoices, :payer_phone, :string
add_column :invoices, :payer_url, :string
add_column :invoices, :payer_email, :string
# MIGRATION TO invoice_rows / invoice_items add_column :invoices, :buyer_country_code, :string
# add_column :invoices, :serial_number, :string # kauba seeria kood add_column :invoices, :buyer_state, :string
add_column :invoices, :product_code, :string # teenuse kood müüja süsteemis (sellerProductId) add_column :invoices, :buyer_street, :string
add_column :invoices, :description, :string, null: false add_column :invoices, :buyer_city, :string
add_column :invoices, :item_unit, :string add_column :invoices, :buyer_zip, :string
add_column :invoices, :item_amount, :integer add_column :invoices, :buyer_phone, :string
add_column :invoices, :item_price, :decimal # without taxes and discounts add_column :invoices, :buyer_url, :string
add_column :invoices, :item_sum, :decimal # could calculate on the fly (amount * price) (without taxes and discounts) add_column :invoices, :buyer_email, :string
add_column :invoices, :vat_sum, :decimal # could calculate on the fly
add_column :invoices, :item_total_sum, :decimal # could calculate on the fly (row's total sum with taxes)
add_column :invoices, :buyer_contact_name, :string
###
add_column :invoices, :seller_address, :string
add_column :invoices, :seller_name, :string
add_column :invoices, :buyer_name, :string
add_column :invoices, :buyer_reg_no, :string
end end
end end

View file

@ -0,0 +1,14 @@
class CreateInvoiceItems < ActiveRecord::Migration
def change
create_table :invoice_items do |t|
t.integer :invoice_id
# t.string :product_code
t.string :description, null: false
t.string :item_unit
t.integer :item_amount
t.decimal :item_price
t.timestamps
end
end
end

View file

@ -206,9 +206,55 @@ ActiveRecord::Schema.define(version: 20150413140933) do
add_index "epp_sessions", ["session_id"], name: "index_epp_sessions_on_session_id", unique: true, using: :btree add_index "epp_sessions", ["session_id"], name: "index_epp_sessions_on_session_id", unique: true, using: :btree
add_index "epp_sessions", ["updated_at"], name: "index_epp_sessions_on_updated_at", using: :btree add_index "epp_sessions", ["updated_at"], name: "index_epp_sessions_on_updated_at", using: :btree
create_table "invoice_items", force: :cascade do |t|
t.integer "invoice_id"
t.string "description", null: false
t.string "item_unit"
t.integer "item_amount"
t.decimal "item_price"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "invoices", force: :cascade do |t| create_table "invoices", force: :cascade do |t|
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "invoice_type", null: false
t.datetime "due_date", null: false
t.string "payment_term"
t.string "currency", null: false
t.string "description"
t.string "reference_no"
t.decimal "vat_prc"
t.datetime "paid_at"
t.integer "seller_id"
t.string "seller_name", null: false
t.string "seller_reg_no"
t.string "seller_iban", null: false
t.string "seller_bank", null: false
t.string "seller_swift", null: false
t.string "seller_vat_no"
t.string "seller_country_code"
t.string "seller_state"
t.string "seller_street"
t.string "seller_city"
t.string "seller_zip"
t.string "seller_phone"
t.string "seller_url"
t.string "seller_email"
t.string "seller_contact_name"
t.integer "buyer_id"
t.string "buyer_name", null: false
t.string "buyer_reg_no", null: false
t.string "buyer_country_code"
t.string "buyer_state"
t.string "buyer_street"
t.string "buyer_city"
t.string "buyer_zip"
t.string "buyer_phone"
t.string "buyer_url"
t.string "buyer_email"
t.string "buyer_contact_name"
end end
create_table "keyrelays", force: :cascade do |t| create_table "keyrelays", force: :cascade do |t|

View file

@ -84,3 +84,17 @@ ZonefileSetting.where({
}).first_or_create! }).first_or_create!
AdminUser.update_all(roles: ['admin']) AdminUser.update_all(roles: ['admin'])
Registrar.where(
name: 'EIS',
reg_no: '90010019',
phone: '+372 727 1000',
country_code: 'EE',
vat_no: 'EE101286464',
email: 'info@internet.ee',
state: 'Harjumaa',
city: 'Tallinn',
street: 'Paldiski mnt 80',
zip: '10617',
url: 'www.internet.ee'
).first_or_create!

View file

@ -0,0 +1,6 @@
VV 000767EE3722002210548552131402031032
VV 220140120 M EE372200221054855213EUR Algsaldo C 0
VV 12014012100331249MK EE372200221054855213EUR767EE192200221051034998 SERGEI VOROTILENKO 205 NATALJA VOROTILENKO C 1000
VV 12014012700965291MK EE372200221054855213EUR767EE252200001102529776 INGRID HEINSOO 50,12 TIIT HEINSOO 14940 C 150023123993
VV 320140203 M EE372200221054855213EUR Lõppsaldo D 0
VV 999 2500

View file

@ -0,0 +1,9 @@
Fabricator(:invoice) do
invoice_type 'DEB'
due_date { Time.zone.now.to_date + 1.day }
payment_term { 'Prepayment' }
currency { 'EUR' }
description { 'Invoice no. 1' }
description { 'Invoice no. 1' }
domain
end