Class: NextSges::CollectionNoteItem

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

Constant Summary

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

#before_create_initialize_itemObject

BEGIN Validation methods and events copies information from collection_note and colection_type to collection_note_item



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/next_sges/collection_note_item.rb', line 112

def before_create_initialize_item
  return if collection_note_id.nil? if collection_type_id.nil? if student_id.nil? if school_id.nil?
  # student.parent.students_number
  ob_coll_note = collection_note
  ob_coll_type = collection_type
  ob_student = student
  ob_school = school
  students_number = 0#student.number #parent.students.size
  discount = 0
  desc = ''

  if students_number >= 2 && ob_coll_type.second_child_discount > discount
    discount = ob_coll_type.second_child_discount
    desc = "Desconto pelo segundo Educando inscrito"
  end
  if students_number >= 3 && ob_coll_type.third_child_discount > discount
    discount = ob_coll_type.third_child_discount
    desc = "Desconto pelo segundo Educando inscrito"
  end
  if ob_coll_type.annual? && ob_coll_type.annual_payment_discount > discount
    discount = ob_coll_type.annual_payment_discount
    desc = "Desconto pelo segundo Educando inscrito"
  end

  self.discount_percentage = discount
  self.discount_description = desc

  self.base_value = ob_coll_type.value
  self.fine_value = (fine_percentage/100) * (base_value * quantity)
  self.discount_value = (discount_percentage/100) * (base_value * quantity)
  self.value = (base_value * quantity) + fine_value - discount_value
  self.name = ob_coll_type.name
  self.collection_type_category = ob_coll_type.category
  self.collection_type_frequency = ob_coll_type.frequency
end

#can_create_fine?Boolean

checks if a fine can be created for this item

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/next_sges/collection_note_item.rb', line 24

def can_create_fine?
  ob_coll_note = collection_note
  ob_coll_type = collection_type
  return false unless id.present?
  return false if collection_note.paid?
  return false unless (ob_coll_type.first_fine_after > 0 && ob_coll_type.first_fine_percentage > 0) ||
      (ob_coll_type.second_fine_after > 0 && ob_coll_type.second_fine_percentage > 0)


  if ob_coll_type.no_frequency?
    date_to_compare = updated_at
  else
    return false unless date.present?
    date_to_compare = date
  end
  if date_to_compare.to_date + ob_coll_type.second_fine_after.days <= DateTime.now.to_date && ob_coll_type.second_fine_percentage > fine_percentage
    return true
  end
  if date_to_compare.to_date + ob_coll_type.first_fine_after.days <= DateTime.now.to_date && ob_coll_type.first_fine_percentage > fine_percentage
    true
  else
    false
  end
end

#can_destroy_fine?Boolean

check if there is a fine to destroy

Returns:

  • (Boolean)


50
51
52
# File 'app/models/next_sges/collection_note_item.rb', line 50

def can_destroy_fine?
  fine_percentage > 0 || fine_value > 0
end

#create_fineObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/next_sges/collection_note_item.rb', line 54

def create_fine
  return unless can_create_fine?
  ob_coll_note = collection_note
  ob_coll_type = collection_type

  return false unless date.present?

  if ob_coll_type.no_frequency?
    date_to_compare = updated_at
  else
    date_to_compare = date
  end

  if date_to_compare.to_date + ob_coll_type.second_fine_after.days <= DateTime.now.to_date && ob_coll_type.second_fine_percentage > fine_percentage
    self.fine_description = "Multa de Atrazo de #{ob_coll_type.second_fine_after} dias"
    self.fine_percentage = ob_coll_type.second_fine_percentage
  end
  if date_to_compare.to_date + ob_coll_type.first_fine_after.days <= DateTime.now.to_date && ob_coll_type.first_fine_percentage > fine_percentage
    self.fine_description = "Multa de Atrazo de #{ob_coll_type.first_fine_after} dias"
    self.fine_percentage = ob_coll_type.first_fine_percentage
  end
end

#create_fine!Object



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

def create_fine!
  create_fine
  save
end

#destroy_fineObject

remove fine assossiated values



83
84
85
86
87
# File 'app/models/next_sges/collection_note_item.rb', line 83

def destroy_fine
  self.fine_description = nil
  self.fine_percentage = 0
  self.fine_value = 0
end

#destroy_fine!Object

remove fine values and saves item



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

def destroy_fine!
  destroy_fine
  save
end

#map_for_filter(method_name = :name) ⇒ Object



19
20
21
# File 'app/models/next_sges/collection_note_item.rb', line 19

def map_for_filter(method_name = :name)
  map_for_select(:name)
end

#map_for_select(method_name = :name) ⇒ Object

BEGIN instance methods



15
16
17
# File 'app/models/next_sges/collection_note_item.rb', line 15

def map_for_select(method_name = :name)
  super(:name)
end

#update_collection_noteObject

update collection note value



149
150
151
152
# File 'app/models/next_sges/collection_note_item.rb', line 149

def update_collection_note
  ob_coll_note = collection_note
  ob_coll_note.update_columns(value: ob_coll_note.collection_note_items.sum(:value))
end

#validate_dateObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/next_sges/collection_note_item.rb', line 154

def validate_date
  return unless date.present?
  ob_coll_note = collection_note
  ob_coll_type = collection_type
  ob_student = student
  ob_school = school

  if ob_coll_type.daily? && NextSges::CollectionNoteItem.where.not(id: id).where(student_id: student_id, collection_type_id: collection_type_id, collection_type_frequency: collection_type_frequency).where("extract(day from date) = ? AND extract(month from date) = ? AND extract(year from date) = ?", date.day, date.month, date.year).exists?
    errors.add(:date, "Já tem um pagamento nesta data para - #{ob_coll_type.name}")
  elsif ob_coll_type.monthly? && NextSges::CollectionNoteItem.where.not(id: id).where(student_id: student_id, collection_type_id: collection_type_id, collection_type_frequency: collection_type_frequency).where("extract(month from date) = ? AND extract(year from date) = ?", date.month, date.year).exists?
    errors.add(:date, "Já tem um pagamento nesta data para - #{ob_coll_type.name}")
  elsif ob_coll_type.annual? && NextSges::CollectionNoteItem.where.not(id: id).where(student_id: student_id, collection_type_id: collection_type_id, collection_type_frequency: collection_type_frequency).where("extract(year from date) = ?", date.year).exists?
    errors.add(:date, "Já tem um pagamento nesta data para - #{ob_coll_type.name}")
  end

end