diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 91948db03..c72982e59 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -4,6 +4,8 @@ class Invoice < ActiveRecord::Base has_many :invoice_items accepts_nested_attributes_for :invoice_items + validates :invoice_type, :due_date, :currency, :seller_name, :seller_iban, :buyer_name, :invoice_items, :vat_prc, presence: true + def seller_address [seller_street, seller_city, seller_state, seller_zip].reject(&:blank?).compact.join(', ') end diff --git a/db/migrate/20150407145943_add_invoice_columns.rb b/db/migrate/20150407145943_add_invoice_columns.rb index eb4dbd473..86772eae0 100644 --- a/db/migrate/20150407145943_add_invoice_columns.rb +++ b/db/migrate/20150407145943_add_invoice_columns.rb @@ -1,18 +1,18 @@ class AddInvoiceColumns < ActiveRecord::Migration def change # 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 # Invoice / credit invoice ... - add_column :invoices, :due_date, :datetime, null: false + # invoice number comes from id # e-invoice + add_column :invoices, :invoice_type, :string, null: false # CRE / DEB # e-invoice + # add_column :invoices, :document_name, :string, null: false # Invoice / credit invoice ... # e-invoice + add_column :invoices, :due_date, :datetime, null: false # e-invoice add_column :invoices, :payment_term, :string # free text - add_column :invoices, :currency, :string, null: false + add_column :invoices, :currency, :string, null: false # e-invoice 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, :vat_prc, :decimal, null: false + #add_column :invoices, :total_sum, :decimal # calculate on the fly # e-invoice add_column :invoices, :paid_at, :datetime # maybe figure this out from transactions # seller info @@ -20,11 +20,11 @@ class AddInvoiceColumns < ActiveRecord::Migration # 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 # e-invoice add_column :invoices, :seller_reg_no, :string - add_column :invoices, :seller_iban, :string, null: false - add_column :invoices, :seller_bank, :string, null: false - add_column :invoices, :seller_swift, :string, null: false + add_column :invoices, :seller_iban, :string, null: false # e-invoice + add_column :invoices, :seller_bank, :string + add_column :invoices, :seller_swift, :string add_column :invoices, :seller_vat_no, :string add_column :invoices, :seller_country_code, :string @@ -43,8 +43,8 @@ class AddInvoiceColumns < ActiveRecord::Migration # add_column :invoices, :payable_type, :string add_column :invoices, :buyer_id, :integer - add_column :invoices, :buyer_name, :string, null: false - add_column :invoices, :buyer_reg_no, :string, null: false + add_column :invoices, :buyer_name, :string, null: false # e-invoice + add_column :invoices, :buyer_reg_no, :string add_column :invoices, :buyer_country_code, :string add_column :invoices, :buyer_state, :string @@ -55,7 +55,7 @@ class AddInvoiceColumns < ActiveRecord::Migration add_column :invoices, :buyer_url, :string add_column :invoices, :buyer_email, :string - add_column :invoices, :buyer_contact_name, :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 index c884c3dda..30163d857 100644 --- a/db/migrate/20150408081917_create_invoice_items.rb +++ b/db/migrate/20150408081917_create_invoice_items.rb @@ -3,7 +3,7 @@ class CreateInvoiceItems < ActiveRecord::Migration create_table :invoice_items do |t| t.integer :invoice_id # t.string :product_code - t.string :description, null: false + t.string :description, null: false # e-invoice t.string :unit t.integer :amount t.decimal :price diff --git a/db/schema.rb b/db/schema.rb index 6546ff70f..2c707ef81 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -225,14 +225,14 @@ ActiveRecord::Schema.define(version: 20150413140933) do t.string "currency", null: false t.string "description" t.string "reference_no" - t.decimal "vat_prc" + t.decimal "vat_prc", null: false 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_bank" + t.string "seller_swift" t.string "seller_vat_no" t.string "seller_country_code" t.string "seller_state" @@ -245,7 +245,7 @@ ActiveRecord::Schema.define(version: 20150413140933) do 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_reg_no" t.string "buyer_country_code" t.string "buyer_state" t.string "buyer_street" @@ -254,7 +254,6 @@ ActiveRecord::Schema.define(version: 20150413140933) do 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/spec/fabricators/invoice_fabricator.rb b/spec/fabricators/invoice_fabricator.rb index 43c2f91e7..daf2997bf 100644 --- a/spec/fabricators/invoice_fabricator.rb +++ b/spec/fabricators/invoice_fabricator.rb @@ -1,9 +1,12 @@ Fabricator(:invoice) do - invoice_type 'DEB' - due_date { Time.zone.now.to_date + 1.day } - payment_term { 'Prepayment' } + buyer_name 'Registrar 1' currency { 'EUR' } - description { 'Invoice no. 1' } - description { 'Invoice no. 1' } - domain + due_date { Time.zone.now.to_date + 1.day } + invoice_type 'DEB' + seller_iban { '123' } + seller_name { 'EIS' } + seller_city { 'Tallinn' } + seller_street { 'Paldiski mnt. 123' } + invoice_items(count: 2) + vat_prc 0.2 end diff --git a/spec/fabricators/invoice_item_fabricator.rb b/spec/fabricators/invoice_item_fabricator.rb new file mode 100644 index 000000000..24d01676e --- /dev/null +++ b/spec/fabricators/invoice_item_fabricator.rb @@ -0,0 +1,6 @@ +Fabricator(:invoice_item) do + description { 'add money' } + unit 1 + amount 1 + price 150 +end diff --git a/spec/models/invoice_spec.rb b/spec/models/invoice_spec.rb new file mode 100644 index 000000000..098296a68 --- /dev/null +++ b/spec/models/invoice_spec.rb @@ -0,0 +1,83 @@ +require 'rails_helper' + +describe Invoice do + it { should belong_to(:seller) } + it { should belong_to(:buyer) } + it { should have_many(:invoice_items) } + + context 'with invalid attribute' do + before :all do + @invoice = Invoice.new + end + + it 'should not be valid' do + @invoice.valid? + @invoice.errors.full_messages.should match_array([ + "Buyer name is missing", + "Currency is missing", + "Due date is missing", + "Invoice items is missing", + "Invoice type is missing", + "Seller iban is missing", + "Seller name is missing", + "Vat prc is missing" + ]) + end + + # it 'should not have any versions' do + # @invoice.versions.should == [] + # end + end + + context 'with valid attributes' do + before :all do + @invoice = Fabricate(:invoice) + end + + it 'should be valid' do + @invoice.valid? + @invoice.errors.full_messages.should match_array([]) + end + + it 'should be valid twice' do + @invoice = Fabricate(:invoice) + @invoice.valid? + @invoice.errors.full_messages.should match_array([]) + end + + it 'should be valid twice' do + @invoice = Fabricate(:invoice) + @invoice.valid? + @invoice.errors.full_messages.should match_array([]) + end + + it 'should return correct addresses' do + @invoice = Fabricate(:invoice) + @invoice.seller_address.should == 'Paldiski mnt. 123, Tallinn' + end + + it 'should calculate totals correctly' do + @invoice = Fabricate(:invoice) + @invoice.vat_prc.should == BigDecimal.new('0.2') + @invoice.total_without_vat.should == BigDecimal.new('300.0') + @invoice.total_vat.should == BigDecimal.new('60.0') + @invoice.total.should == BigDecimal.new('360.0') + + ii = @invoice.items.first + ii.item_total_without_vat.should == BigDecimal.new('150.0') + + ii = @invoice.items.last + ii.item_total_without_vat.should == BigDecimal.new('150.0') + end + + # it 'should have one version' do + # with_versioning do + # @invoice.versions.should == [] + # @invoice.body = 'New body' + # @invoice.save + # @invoice.errors.full_messages.should match_array([]) + # @invoice.versions.size.should == 1 + # end + # end + end +end