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-2016 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

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
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?, attr_readonly_with_conditions, #check_if_destroyable?, #check_if_updateable?, columns_definition, complex_scopes, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, human_attribute_name_with_id, nomenclature_reflections, #old_record, refers_to, scope_with_registration, simple_scopes, #updateable?

Instance Method Details

#balance_creditObject


100
101
102
# File 'app/models/bank_statement.rb', line 100

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

#balance_debitObject


104
105
106
# File 'app/models/bank_statement.rb', line 104

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

#eligible_journal_entry_itemsObject


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

def eligible_journal_entry_items
  margin = 20.days
  unpointed = JournalEntryItem.where(account_id: ).unpointed.between(started_on - margin, stopped_on + margin)
  pointed = JournalEntryItem.pointed_by(self)
  JournalEntryItem.where(id: unpointed.pluck(:id) + pointed.pluck(:id))
end

#nextObject


120
121
122
# File 'app/models/bank_statement.rb', line 120

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

#othersObject


112
113
114
# File 'app/models/bank_statement.rb', line 112

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

#previousObject


116
117
118
# File 'app/models/bank_statement.rb', line 116

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

#save_with_items(statement_items) ⇒ Object


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/models/bank_statement.rb', line 131

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])
      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


108
109
110
# File 'app/models/bank_statement.rb', line 108

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