Class: NextSges::CollectionNote

Inherits:
ApplicationRecord show all
Defined in:
app/models/next_sges/collection_note.rb

Constant Summary collapse

INITIAL_LETTER =
"F"

Constants inherited from ApplicationRecord

ApplicationRecord::CELL_REGEX, ApplicationRecord::DEFAULT_IDENTIFICATION_ENUM, ApplicationRecord::DEFAULT_STATUS_ENUM, ApplicationRecord::MAIL_REGEX

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

#create_number, #create_number!, map_for_filter, map_for_select

Class Method Details

.by_monthObject



21
22
23
# File 'app/models/next_sges/collection_note.rb', line 21

def self.by_month
  where('extract(month from updated_at) = ?', DateTime.now.month)
end

.by_yearObject



25
26
27
# File 'app/models/next_sges/collection_note.rb', line 25

def self.by_year
  where('extract(year from updated_at) = ?', DateTime.now.year)
end

Instance Method Details

#activateObject

Set Collection note to active



40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/next_sges/collection_note.rb', line 40

def activate
  return unless can_activate?
  if inactive?
    active!
  elsif paid?
    if active!
      collection_note_responsibles.each(&:destroy)
    end
    # TODO, Make code to return money address
  end
end

#can_activate?Boolean

Set Collection note to active

Returns:

  • (Boolean)


76
77
78
# File 'app/models/next_sges/collection_note.rb', line 76

def can_activate?
  inactive? || paid?
end

#can_deactivate?Boolean

set Collection note to inactive

Returns:

  • (Boolean)


81
82
83
# File 'app/models/next_sges/collection_note.rb', line 81

def can_deactivate?
  active?
end

#can_pay?Boolean

TODO, Remove## Mark Collection note to paid

Returns:

  • (Boolean)


86
87
88
# File 'app/models/next_sges/collection_note.rb', line 86

def can_pay?
  active? #&& student.parent.account_balance >= value
end

#can_responsible_pay?(responsible) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'app/models/next_sges/collection_note.rb', line 90

def can_responsible_pay?(responsible)
  active? && responsible. >= value
end

#deactivateObject

set collection note to inactive



53
54
55
56
# File 'app/models/next_sges/collection_note.rb', line 53

def deactivate
  return unless can_deactivate?
  inactive!
end

#new_item(collection_type) ⇒ Object

Initialize item for a collection note with a given type



30
31
32
33
34
35
36
37
# File 'app/models/next_sges/collection_note.rb', line 30

def new_item(collection_type)
  NextSges::CollectionNoteItem.new(collection_note_id: id,
                                   collection_type_id: collection_type.id,
                                   collection_type_frequency: collection_type.frequency,
                                   collection_type_category: collection_type.category,
                                   school_id: school_id,
                                   student_id: student_id)
end

#pay(responsible) ⇒ Object

Mark collection note to paid



60
61
62
63
64
65
# File 'app/models/next_sges/collection_note.rb', line 60

def pay(responsible)
  return unless can_pay?
  if paid!
    responsible.update_columns(account_balance: responsible. - value)
  end
end

#responsible_pay(responsible) ⇒ Object

pays the collection note with this responsible balance



68
69
70
71
72
73
# File 'app/models/next_sges/collection_note.rb', line 68

def responsible_pay(responsible)
  return unless can_responsible_pay?(responsible)
  if paid!
    CollectionNoteResponsible.create(responsible_id: responsible.id, collection_note_id: id, value: value)
  end
end

#responsibles_that_can_payObject



94
95
96
# File 'app/models/next_sges/collection_note.rb', line 94

def responsibles_that_can_pay
  responsibles.active.where("account_balance >= ?", value)
end