diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index e37950a00..765df6b8b 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -30,6 +30,8 @@ class BankTransaction < ActiveRecord::Base invoice = registrar.invoices.find_by(id: invoice_id) return unless invoice + return if invoice.binded? + return if invoice.sum != sum create_account_activity( account: registrar.cash_account, diff --git a/app/models/invoice.rb b/app/models/invoice.rb index b269b786d..a08ea88e5 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -2,11 +2,17 @@ class Invoice < ActiveRecord::Base belongs_to :seller, class_name: 'Registrar' belongs_to :buyer, class_name: 'Registrar' has_many :invoice_items + has_one :account_activity + 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 binded? + account_activity.present? + end + def to_s I18n.t('invoice_no', no: id) end diff --git a/spec/models/bank_statement_spec.rb b/spec/models/bank_statement_spec.rb index 050895eec..9fbedd86e 100644 --- a/spec/models/bank_statement_spec.rb +++ b/spec/models/bank_statement_spec.rb @@ -50,7 +50,7 @@ describe BankStatement do description: 'Invoice no. 1' }), Fabricate(:bank_transaction, { - sum: 120.0, + sum: 240.0, reference_no: 'RF7086666663', description: 'Invoice no. 1' }) @@ -66,6 +66,7 @@ describe BankStatement do r.cash_account.balance.should == 240.0 bs.bank_transactions.unbinded.count.should == 1 + bs.partially_binded?.should == true end # it 'should have one version' do