Class: BankStatement

Inherits:
Ekylibre::Record::Base show all
Includes:
Attachable, Customizable
Defined in:
app/models/bank_statement.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2019 Brice Texier, David Joulin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see www.gnu.org/licenses.

Table: bank_statements

accounted_at           :datetime
cash_id                :integer          not null
created_at             :datetime         not null
creator_id             :integer
credit                 :decimal(19, 4)   default(0.0), not null
currency               :string           not null
custom_fields          :jsonb
debit                  :decimal(19, 4)   default(0.0), not null
id                     :integer          not null, primary key
initial_balance_credit :decimal(19, 4)   default(0.0), not null
initial_balance_debit  :decimal(19, 4)   default(0.0), not null
journal_entry_id       :integer
lock_version           :integer          default(0), not null
number                 :string           not null
started_on             :date             not null
stopped_on             :date             not null
updated_at             :datetime         not null
updater_id             :integer

Instance Method Summary collapse

Methods included from Customizable

#custom_value, #set_custom_value, #validate_custom_fields

Methods inherited from Ekylibre::Record::Base

#already_updated?, #check_if_destroyable?, #check_if_updateable?, columns_definition, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, nomenclature_reflections, #old_record, refers_to, #unsuppress, #updateable?

Methods included from Userstamp::Stampable

included

Methods included from Userstamp::Stamper

included

Instance Method Details

#balance_creditObject


122
123
124
# File 'app/models/bank_statement.rb', line 122

def balance_credit
  (debit > credit ? 0.0 : credit - debit)
end

#balance_debitObject


126
127
128
# File 'app/models/bank_statement.rb', line 126

def balance_debit
  (debit > credit ? debit - credit : 0.0)
end

#eligible_entries_in(start, finish) ⇒ Object


174
175
176
177
178
# File 'app/models/bank_statement.rb', line 174

def eligible_entries_in(start, finish)
  unpointed = cash.unpointed_journal_entry_items.between(start, finish)
  pointed = JournalEntryItem.pointed_by(self).between(start, finish)
  JournalEntryItem.where(id: unpointed.pluck(:id) + pointed.pluck(:id))
end

#eligible_journal_entry_itemsObject


168
169
170
171
172
# File 'app/models/bank_statement.rb', line 168

def eligible_journal_entry_items
  unpointed = cash.unpointed_journal_entry_items
  pointed = JournalEntryItem.pointed_by(self)
  JournalEntryItem.where(id: unpointed.pluck(:id) + pointed.pluck(:id))
end

#letter_items(statement_items, journal_entry_items) ⇒ Object


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/bank_statement.rb', line 150

def letter_items(statement_items, journal_entry_items)
  new_letter = next_letter
  return false if (journal_entry_items + statement_items).length.zero?

  statement_entries = JournalEntryItem.where(resource: statement_items)
  to_letter = journal_entry_items + statement_entries
  cash..mark(to_letter) if cash.suspend_until_reconciliation

  saved = true
  saved &&= statement_items.update_all(letter: new_letter)
  saved &&= journal_entry_items.update_all(
    bank_statement_letter: new_letter,
    bank_statement_id: id
  )

  saved && new_letter
end

#nextObject


142
143
144
# File 'app/models/bank_statement.rb', line 142

def next
  self.class.where('started_on >= ?', stopped_on).reorder(started_on: :asc).first
end

#next_letterObject


146
147
148
# File 'app/models/bank_statement.rb', line 146

def next_letter
  cash.next_reconciliation_letter
end

#othersObject


134
135
136
# File 'app/models/bank_statement.rb', line 134

def others
  siblings.where.not(id: id || 0)
end

#previousObject


138
139
140
# File 'app/models/bank_statement.rb', line 138

def previous
  self.class.where('stopped_on <= ?', started_on).reorder(stopped_on: :desc).first
end

#save_with_items(statement_items) ⇒ Object


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/models/bank_statement.rb', line 180

def save_with_items(statement_items)
  ActiveRecord::Base.transaction do
    saved = save

    previous_journal_entry_item_ids_by_letter = items.each_with_object({}) do |item, hash|
      item.associated_journal_entry_items.each do |journal_entry_item|
        ids = (hash[journal_entry_item.bank_statement_letter] ||= [])
        ids << journal_entry_item.id
      end
    end

    items.clear

    statement_items.each_index do |index|
      statement_items[index] = items.build(statement_items[index])
      if started_on > statement_items[index].transfered_on
        statement_items[index].transfered_on = started_on
      end
      if statement_items[index].transfered_on > stopped_on
        statement_items[index].transfered_on = stopped_on
      end
      saved = false if saved && !statement_items[index].save
    end

    previous_journal_entry_item_ids_by_letter.each do |letter, journal_entry_item_ids|
      new_item_with_letter = items.detect { |item| item.letter == letter }
      if new_item_with_letter
        bank_statement_id = id
        bank_statement_letter = letter
      end
      JournalEntryItem.where(id: journal_entry_item_ids).update_all(
        bank_statement_id: bank_statement_id,
        bank_statement_letter: bank_statement_letter
      )
    end

    if saved && reload.save
      return true
    else
      raise ActiveRecord::Rollback
    end
  end
  false
end

#siblingsObject


130
131
132
# File 'app/models/bank_statement.rb', line 130

def siblings
  self.class.where(cash_id: cash_id)
end