Class: JournalEntryItem

Inherits:
Ekylibre::Record::Base show all
Defined in:
app/models/journal_entry_item.rb

Overview

What are the differents columns:

* (credit|debit|balance) are in currency of the journal
* real_(credit|debit|balance) are in currency of the financial year
* absolute_(credit|debit|balance) are in currency of the company

Class Method Summary collapse

Instance Method Summary collapse

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, #others, refers_to, #unsuppress, #updateable?

Methods included from Userstamp::Stampable

included

Methods included from Userstamp::Stamper

included

Class Method Details

.attributes_for(name, account, amount, options = {}) ⇒ Object

Computes attribute for adding an item


270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'app/models/journal_entry_item.rb', line 270

def self.attributes_for(name, , amount, options = {})
  if name.size > 255
    omission = (options.delete(:omission) || '...').to_s
    name = name[0..254 - omission.size] + omission
  end
  credit = options.delete(:credit) ? true : false
  credit = !credit if amount < 0
  attributes = options.merge(name: name)
  attributes[:account_id] = .is_a?(Integer) ?  : .id
  attributes[:activity_budget_id] = options[:activity_budget].id if options[:activity_budget]
  attributes[:team_id] = options[:team].id if options[:team]
  attributes[:equipment_id] = options[:equipment].id if options[:equipment]
  attributes[:project_budget_id] = options[:project_budget].id if options[:project_budget]
  attributes[:tax_id] = options[:tax].id if options[:tax]
  attributes[:real_pretax_amount] = attributes.delete(:pretax_amount) if attributes[:pretax_amount]
  attributes[:resource_prism] = attributes.delete(:as) if options[:as]
  attributes[:letter] = attributes.delete(:letter) if options[:letter]
  if credit
    attributes[:real_credit] = amount.abs
    attributes[:real_debit]  = 0.0
  else
    attributes[:real_credit] = 0.0
    attributes[:real_debit]  = amount.abs
  end
  attributes
end

.new_for(name, account, amount, options = {}) ⇒ Object


297
298
299
# File 'app/models/journal_entry_item.rb', line 297

def self.new_for(name, , amount, options = {})
  new(attributes_for(name, , amount, options))
end

Instance Method Details

#balanced_entryObject

this method:allows to fix a display color if the entry containing the entry_item is balanced or not.


405
406
407
# File 'app/models/journal_entry_item.rb', line 405

def balanced_entry
  (entry.balanced? ? 'balanced' : 'unbalanced')
end

#balanced_letter?Boolean

Check if the current letter is balanced with all entry items with the same letter

Returns:

  • (Boolean)

356
357
358
359
# File 'app/models/journal_entry_item.rb', line 356

def balanced_letter?
  return true if letter.blank?
  .balanced_letter?(letter)
end

#clear_bank_statement_reconciliationObject


264
265
266
267
# File 'app/models/journal_entry_item.rb', line 264

def clear_bank_statement_reconciliation
  return unless bank_statement && bank_statement_letter
  bank_statement.items.where(letter: bank_statement_letter).update_all(letter: nil)
end

#completely_lettered?Boolean

Returns:

  • (Boolean)

202
203
204
# File 'app/models/journal_entry_item.rb', line 202

def completely_lettered?
  lettered? && !partially_lettered?
end

#computeObject


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'app/models/journal_entry_item.rb', line 220

def compute
  self.debit       ||= 0
  self.credit      ||= 0
  self.real_debit  ||= 0
  self.real_credit ||= 0

  if entry
    self.entry_number = entry.number
    %i[financial_year_id printed_on journal_id currency
       absolute_currency real_currency real_currency_rate].each do |replicated|
      send("#{replicated}=", entry.send(replicated))
    end
    unless closed?
      self.debit  = real_debit * real_currency_rate
      self.credit = real_credit * real_currency_rate
      self.pretax_amount = real_pretax_amount * real_currency_rate
      if currency && Nomen::Currency.find(currency)
        precision = Nomen::Currency.find(currency).precision
        self.debit  = debit.round(precision)
        self.credit = credit.round(precision)
        self.pretax_amount = pretax_amount.round(precision)
      end
    end
  end
  self.absolute_currency = Preference[:currency]
  if absolute_currency == currency
    self.absolute_debit = debit
    self.absolute_credit = credit
    self.absolute_pretax_amount = pretax_amount
  elsif absolute_currency == real_currency
    self.absolute_debit = real_debit
    self.absolute_credit = real_credit
    self.absolute_pretax_amount = real_pretax_amount
  else
    # FIXME: We need to do something better when currencies don't match
    if currency.present? && (absolute_currency.present? || real_currency.present?)
      raise JournalEntry::IncompatibleCurrencies, "You cannot create an entry where the absolute currency (#{absolute_currency.inspect}) is not the real (#{real_currency.inspect}) or current one (#{currency.inspect})"
    end
  end

  self.balance = debit - credit
  self.real_balance = real_debit - real_credit
end

#followingsObject

Returns following items


332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'app/models/journal_entry_item.rb', line 332

def followings
  return self.class.none unless 

  if new_record?
    
      .journal_entry_items
      .where('printed_on > ?', printed_on)
  else
    
      .journal_entry_items
      .where('(printed_on = ? AND id > ?) OR printed_on > ?', printed_on, id, printed_on)
  end
end

#journal_nameObject

This method returns the name of journal which the entries are saved.


396
397
398
399
400
401
402
# File 'app/models/journal_entry_item.rb', line 396

def journal_name
  if entry
    entry.journal.name
  else
    :none.tl
  end
end

#letter_groupObject


215
216
217
218
# File 'app/models/journal_entry_item.rb', line 215

def letter_group
  return JournalEntryItem.none unless letter
  .journal_entry_items.where('letter = ? OR letter = ?', letter_radix, letter_radix + '*')
end

#letter_radixObject


210
211
212
213
# File 'app/models/journal_entry_item.rb', line 210

def letter_radix
  return nil unless letter
  letter.delete('*')
end

#lettered?Boolean

Returns:

  • (Boolean)

312
313
314
# File 'app/models/journal_entry_item.rb', line 312

def lettered?
  letter.present?
end

#modeObject

this method allows to fix a display color if the entry_item is in draft mode.


362
363
364
365
366
# File 'app/models/journal_entry_item.rb', line 362

def mode
  mode = ''
  mode += 'warning' if draft?
  mode
end

#next(balance) ⇒ Object

this method creates a next entry_item with an initialized value matching to the previous entry.


410
411
412
413
414
415
416
417
418
# File 'app/models/journal_entry_item.rb', line 410

def next(balance)
  entry_item = JournalEntryItem.new
  if balance > 0
    entry_item.real_credit = balance.abs
  elsif balance < 0
    entry_item.real_debit = balance.abs
  end
  entry_item
end

#partially_lettered?Boolean

Returns:

  • (Boolean)

198
199
200
# File 'app/models/journal_entry_item.rb', line 198

def partially_lettered?
  lettered? && letter.include?('*')
end

#previousObject

Returns the previous item


322
323
324
325
326
327
328
329
# File 'app/models/journal_entry_item.rb', line 322

def previous
  return nil unless 
  if new_record?
    .journal_entry_items.order(printed_on: :desc, id: :desc).where('printed_on <= ?', printed_on).limit(1).first
  else
    .journal_entry_items.order(printed_on: :desc, id: :desc).where('(printed_on = ? AND id < ?) OR printed_on < ?', printed_on, id, printed_on).limit(1).first
  end
end

#product_item_to_tax_labelObject

get link item corresponding to vat line from product item


387
388
389
390
391
392
393
# File 'app/models/journal_entry_item.rb', line 387

def product_item_to_tax_label
  vat_journal_entry_item = entry.items.find_by(resource_id: resource_id, resource_prism: 'item_tax') # where.not(id: self.id).
  if vat_journal_entry_item && vat_journal_entry_item.id != id
    t = Tax.find(vat_journal_entry_item.tax_id)
    return t.name if t
  end
end

#resourceObject


369
370
371
372
373
374
375
# File 'app/models/journal_entry_item.rb', line 369

def resource
  if entry
    entry.resource_type
  else
    :none.tl
  end
end

#semi_lettered?Boolean

Returns:

  • (Boolean)

206
207
208
# File 'app/models/journal_entry_item.rb', line 206

def semi_lettered?
  lettered? || !partially_lettered?
end

#state_labelObject

Prints human name of current state


302
303
304
# File 'app/models/journal_entry_item.rb', line 302

def state_label
  JournalEntry.tc("states.#{state}")
end

#third_partyObject


420
421
422
423
424
# File 'app/models/journal_entry_item.rb', line 420

def third_party
  return unless 
  third_parties = Entity.uniq.where('client_account_id = ? OR supplier_account_id = ? OR employee_account_id = ?', .id, .id, .id)
  third_parties.take if third_parties.count == 1
end

#unmarkObject

Unmark all the journal entry items with the same mark in the same account


317
318
319
# File 'app/models/journal_entry_item.rb', line 317

def unmark
  .unmark(letter) if letter.present?
end

#update_entryObject

Updates the amounts to the debit and the credit for the matching entry.


308
309
310
# File 'app/models/journal_entry_item.rb', line 308

def update_entry
  entry.refresh
end

#vat_accountObject

fixed_assets, expenses and revenues are used into tax declaration


427
428
429
430
431
# File 'app/models/journal_entry_item.rb', line 427

def 
 prefixes = Account.tax_declarations.pluck(:number).join
 return if prefixes.empty? || !( =~ /^[#{prefixes}].*/)
 entry.items.find_by(resource_prism: ["item_tax_reverse_charge", "item_tax"])&.
end

#vat_account_labelObject


433
434
435
# File 'app/models/journal_entry_item.rb', line 433

def 
  &.label
end

#vat_item_to_product_accountObject

get link item corresponding to charge or product line in purchase or sale from vat item


378
379
380
381
382
383
384
# File 'app/models/journal_entry_item.rb', line 378

def 
  product_journal_entry_item = entry.items.find_by(resource_id: resource_id, resource_prism: 'item_product')
  if product_journal_entry_item
    a = Account.where(id: product_journal_entry_item.).first
    return a.label if a
  end
end