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

Instance Method Summary collapse

Methods inherited from ApplicationRecord

#create_number, #create_number!, map_for_filter, map_for_select

Instance Method Details

#activateObject

Set Collection note to active



24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/next_sges/collection_note.rb', line 24

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)


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

def can_activate?
  inactive? || paid?
end

#can_deactivate?Boolean

set Collection note to inactive

Returns:

  • (Boolean)


65
66
67
# File 'app/models/next_sges/collection_note.rb', line 65

def can_deactivate?
  active?
end

#can_pay?Boolean

TODO, Remove## Mark Collection note to paid

Returns:

  • (Boolean)


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

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

#can_responsible_pay?(responsible) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/next_sges/collection_note.rb', line 74

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

#deactivateObject

set collection note to inactive



37
38
39
40
# File 'app/models/next_sges/collection_note.rb', line 37

def deactivate
  return unless can_deactivate?
  inactive!
end

#new_item(collection_type) ⇒ Object

Initialize item for a collection note with a given type



14
15
16
17
18
19
20
21
# File 'app/models/next_sges/collection_note.rb', line 14

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



44
45
46
47
48
49
# File 'app/models/next_sges/collection_note.rb', line 44

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



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

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



78
79
80
# File 'app/models/next_sges/collection_note.rb', line 78

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