From 5c0e2be3875d92a86954480acb7f7ad7ef15db58 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Wed, 8 Apr 2015 16:40:38 +0300 Subject: [PATCH] Invoice migration improvements, invoice issuing --- app/models/invoice.rb | 4 ++ app/models/invoice_item.rb | 3 + app/models/registrar.rb | 54 ++++++++++++++++ config/initializers/initial_settings.rb | 5 ++ .../20150407145943_add_invoice_columns.rb | 62 +++++++++---------- .../20150408081917_create_invoice_items.rb | 14 +++++ db/schema.rb | 50 ++++++++++++++- db/seeds.rb | 14 +++++ import/bank_statements/20150414074751.txt | 0 import/bank_statements/20150414074827.txt | 0 import/bank_statements/20150414074919.txt | 6 ++ spec/fabricators/invoice_fabricator.rb | 9 +++ 12 files changed, 187 insertions(+), 34 deletions(-) create mode 100644 app/models/invoice_item.rb create mode 100644 db/migrate/20150408081917_create_invoice_items.rb create mode 100644 import/bank_statements/20150414074751.txt create mode 100644 import/bank_statements/20150414074827.txt create mode 100644 import/bank_statements/20150414074919.txt create mode 100644 spec/fabricators/invoice_fabricator.rb diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 7bf0c96c5..4da323cde 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -1,2 +1,6 @@ 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 diff --git a/app/models/invoice_item.rb b/app/models/invoice_item.rb new file mode 100644 index 000000000..180f29522 --- /dev/null +++ b/app/models/invoice_item.rb @@ -0,0 +1,3 @@ +class InvoiceItem < ActiveRecord::Base + belongs_to :invoice +end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index e895273b3..2728642b7 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -6,6 +6,7 @@ class Registrar < ActiveRecord::Base has_many :api_users, dependent: :restrict_with_error has_many :messages 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, uniqueness: true @@ -19,6 +20,59 @@ class Registrar < ActiveRecord::Base res = search(name_or_reg_no_cont: query).result res.reduce([]) { |o, v| o << { id: v[:id], display_key: "#{v[:name]} (#{v[:reg_no]})" } } 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 def domain_transfers diff --git a/config/initializers/initial_settings.rb b/config/initializers/initial_settings.rb index 0e5da5f3b..a09c83424 100644 --- a/config/initializers/initial_settings.rb +++ b/config/initializers/initial_settings.rb @@ -23,6 +23,11 @@ if con.present? && con.table_exists?('settings') Setting.save_default(:transfer_wait_time, 0) 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 # dev only setting diff --git a/db/migrate/20150407145943_add_invoice_columns.rb b/db/migrate/20150407145943_add_invoice_columns.rb index e3c4e7afa..543fb0ac0 100644 --- a/db/migrate/20150407145943_add_invoice_columns.rb +++ b/db/migrate/20150407145943_add_invoice_columns.rb @@ -3,16 +3,23 @@ class AddInvoiceColumns < ActiveRecord::Migration # invoice info # invoice number comes from id 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, :payment_term, :string # maksetingimus (free text) + add_column :invoices, :payment_term, :string # free text add_column :invoices, :currency, :string, null: false - add_column :invoices, :description, :string, null: false # Selgitus - add_column :invoices, :reference_no, :string # Viitenumber - add_column :invoices, :total_sum, :decimal + add_column :invoices, :description, :string + + 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 + # 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_reg_no, :string 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_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_city, :string add_column :invoices, :seller_zip, :string @@ -29,35 +38,24 @@ class AddInvoiceColumns < ActiveRecord::Migration add_column :invoices, :seller_contact_name, :string - # payer info - add_column :invoices, :payer_name, :string, null: false - add_column :invoices, :payer_reg_no, :string, null: false + # buyer info + # add_column :invoices, :payable_id, :integer + # add_column :invoices, :payable_type, :string + add_column :invoices, :buyer_id, :integer - add_column :invoices, :payer_street, :string - add_column :invoices, :payer_city, :string - add_column :invoices, :payer_zip, :string - add_column :invoices, :payer_phone, :string - add_column :invoices, :payer_url, :string - add_column :invoices, :payer_email, :string + add_column :invoices, :buyer_name, :string, null: false + add_column :invoices, :buyer_reg_no, :string, null: false - # MIGRATION TO invoice_rows / invoice_items - # add_column :invoices, :serial_number, :string # kauba seeria kood - add_column :invoices, :product_code, :string # teenuse kood müüja süsteemis (sellerProductId) - add_column :invoices, :description, :string, null: false - add_column :invoices, :item_unit, :string - add_column :invoices, :item_amount, :integer - add_column :invoices, :item_price, :decimal # without taxes and discounts - add_column :invoices, :item_sum, :decimal # could calculate on the fly (amount * price) (without taxes and discounts) - 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_country_code, :string + add_column :invoices, :buyer_state, :string + add_column :invoices, :buyer_street, :string + add_column :invoices, :buyer_city, :string + add_column :invoices, :buyer_zip, :string + add_column :invoices, :buyer_phone, :string + add_column :invoices, :buyer_url, :string + add_column :invoices, :buyer_email, :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 + add_column :invoices, :buyer_contact_name, :string end end + diff --git a/db/migrate/20150408081917_create_invoice_items.rb b/db/migrate/20150408081917_create_invoice_items.rb new file mode 100644 index 000000000..e4025a8fc --- /dev/null +++ b/db/migrate/20150408081917_create_invoice_items.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 6dde21bac..7db3923fe 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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", ["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| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_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 create_table "keyrelays", force: :cascade do |t| diff --git a/db/seeds.rb b/db/seeds.rb index 975dfa9b3..d5b022514 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -84,3 +84,17 @@ ZonefileSetting.where({ }).first_or_create! 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! diff --git a/import/bank_statements/20150414074751.txt b/import/bank_statements/20150414074751.txt new file mode 100644 index 000000000..e69de29bb diff --git a/import/bank_statements/20150414074827.txt b/import/bank_statements/20150414074827.txt new file mode 100644 index 000000000..e69de29bb diff --git a/import/bank_statements/20150414074919.txt b/import/bank_statements/20150414074919.txt new file mode 100644 index 000000000..89171d9db --- /dev/null +++ b/import/bank_statements/20150414074919.txt @@ -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 diff --git a/spec/fabricators/invoice_fabricator.rb b/spec/fabricators/invoice_fabricator.rb new file mode 100644 index 000000000..43c2f91e7 --- /dev/null +++ b/spec/fabricators/invoice_fabricator.rb @@ -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