next_sges 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +36 -0
- data/app/assets/config/next_sges_manifest.js +2 -0
- data/app/assets/javascripts/next_sges/administrators.js +2 -0
- data/app/assets/javascripts/next_sges/application.js +13 -0
- data/app/assets/javascripts/next_sges/collection_note_items.js +2 -0
- data/app/assets/javascripts/next_sges/collection_notes.js +2 -0
- data/app/assets/javascripts/next_sges/collection_types.js +2 -0
- data/app/assets/javascripts/next_sges/deposits.js +2 -0
- data/app/assets/javascripts/next_sges/inscriptions.js +2 -0
- data/app/assets/javascripts/next_sges/parents.js +2 -0
- data/app/assets/javascripts/next_sges/responsible_others.js +2 -0
- data/app/assets/javascripts/next_sges/school_admins.js +2 -0
- data/app/assets/javascripts/next_sges/schools.js +2 -0
- data/app/assets/javascripts/next_sges/students.js +2 -0
- data/app/assets/stylesheets/next_sges/administrators.css +4 -0
- data/app/assets/stylesheets/next_sges/application.css +15 -0
- data/app/assets/stylesheets/next_sges/collection_note_items.css +4 -0
- data/app/assets/stylesheets/next_sges/collection_notes.css +4 -0
- data/app/assets/stylesheets/next_sges/collection_types.css +4 -0
- data/app/assets/stylesheets/next_sges/deposits.css +4 -0
- data/app/assets/stylesheets/next_sges/inscriptions.css +4 -0
- data/app/assets/stylesheets/next_sges/parents.css +4 -0
- data/app/assets/stylesheets/next_sges/responsible_others.css +4 -0
- data/app/assets/stylesheets/next_sges/school_admins.css +4 -0
- data/app/assets/stylesheets/next_sges/schools.css +4 -0
- data/app/assets/stylesheets/next_sges/students.css +4 -0
- data/app/assets/stylesheets/scaffold.css +80 -0
- data/app/controllers/next_sges/administrators_controller.rb +62 -0
- data/app/controllers/next_sges/application_controller.rb +5 -0
- data/app/controllers/next_sges/collection_note_items_controller.rb +62 -0
- data/app/controllers/next_sges/collection_notes_controller.rb +62 -0
- data/app/controllers/next_sges/collection_types_controller.rb +62 -0
- data/app/controllers/next_sges/deposits_controller.rb +62 -0
- data/app/controllers/next_sges/inscriptions_controller.rb +62 -0
- data/app/controllers/next_sges/parents_controller.rb +62 -0
- data/app/controllers/next_sges/responsible_others_controller.rb +62 -0
- data/app/controllers/next_sges/school_admins_controller.rb +62 -0
- data/app/controllers/next_sges/schools_controller.rb +62 -0
- data/app/controllers/next_sges/students_controller.rb +62 -0
- data/app/helpers/next_sges/administrators_helper.rb +4 -0
- data/app/helpers/next_sges/application_helper.rb +4 -0
- data/app/helpers/next_sges/collection_note_items_helper.rb +4 -0
- data/app/helpers/next_sges/collection_notes_helper.rb +4 -0
- data/app/helpers/next_sges/collection_types_helper.rb +4 -0
- data/app/helpers/next_sges/deposits_helper.rb +4 -0
- data/app/helpers/next_sges/inscriptions_helper.rb +4 -0
- data/app/helpers/next_sges/parents_helper.rb +4 -0
- data/app/helpers/next_sges/responsible_others_helper.rb +4 -0
- data/app/helpers/next_sges/school_admins_helper.rb +4 -0
- data/app/helpers/next_sges/schools_helper.rb +4 -0
- data/app/helpers/next_sges/students_helper.rb +4 -0
- data/app/jobs/next_sges/application_job.rb +4 -0
- data/app/mailers/next_sges/application_mailer.rb +6 -0
- data/app/models/next_sges/administrator.rb +19 -0
- data/app/models/next_sges/application_record.rb +33 -0
- data/app/models/next_sges/collection_note.rb +68 -0
- data/app/models/next_sges/collection_note_item.rb +173 -0
- data/app/models/next_sges/collection_type.rb +36 -0
- data/app/models/next_sges/deposit.rb +31 -0
- data/app/models/next_sges/inscription.rb +5 -0
- data/app/models/next_sges/parent.rb +37 -0
- data/app/models/next_sges/responsible_other.rb +20 -0
- data/app/models/next_sges/school.rb +21 -0
- data/app/models/next_sges/school_admin.rb +10 -0
- data/app/models/next_sges/student.rb +39 -0
- data/app/views/layouts/next_sges/application.html.erb +14 -0
- data/app/views/next_sges/administrators/_form.html.erb +27 -0
- data/app/views/next_sges/administrators/edit.html.erb +6 -0
- data/app/views/next_sges/administrators/index.html.erb +29 -0
- data/app/views/next_sges/administrators/new.html.erb +5 -0
- data/app/views/next_sges/administrators/show.html.erb +14 -0
- data/app/views/next_sges/collection_note_items/_form.html.erb +107 -0
- data/app/views/next_sges/collection_note_items/edit.html.erb +6 -0
- data/app/views/next_sges/collection_note_items/index.html.erb +61 -0
- data/app/views/next_sges/collection_note_items/new.html.erb +5 -0
- data/app/views/next_sges/collection_note_items/show.html.erb +94 -0
- data/app/views/next_sges/collection_notes/_form.html.erb +37 -0
- data/app/views/next_sges/collection_notes/edit.html.erb +6 -0
- data/app/views/next_sges/collection_notes/index.html.erb +33 -0
- data/app/views/next_sges/collection_notes/new.html.erb +5 -0
- data/app/views/next_sges/collection_notes/show.html.erb +24 -0
- data/app/views/next_sges/collection_types/_form.html.erb +37 -0
- data/app/views/next_sges/collection_types/edit.html.erb +6 -0
- data/app/views/next_sges/collection_types/index.html.erb +33 -0
- data/app/views/next_sges/collection_types/new.html.erb +5 -0
- data/app/views/next_sges/collection_types/show.html.erb +24 -0
- data/app/views/next_sges/deposits/_form.html.erb +37 -0
- data/app/views/next_sges/deposits/edit.html.erb +6 -0
- data/app/views/next_sges/deposits/index.html.erb +33 -0
- data/app/views/next_sges/deposits/new.html.erb +5 -0
- data/app/views/next_sges/deposits/show.html.erb +24 -0
- data/app/views/next_sges/inscriptions/_form.html.erb +102 -0
- data/app/views/next_sges/inscriptions/edit.html.erb +6 -0
- data/app/views/next_sges/inscriptions/index.html.erb +59 -0
- data/app/views/next_sges/inscriptions/new.html.erb +5 -0
- data/app/views/next_sges/inscriptions/show.html.erb +89 -0
- data/app/views/next_sges/parents/_form.html.erb +42 -0
- data/app/views/next_sges/parents/edit.html.erb +6 -0
- data/app/views/next_sges/parents/index.html.erb +35 -0
- data/app/views/next_sges/parents/new.html.erb +5 -0
- data/app/views/next_sges/parents/show.html.erb +29 -0
- data/app/views/next_sges/responsible_others/_form.html.erb +97 -0
- data/app/views/next_sges/responsible_others/edit.html.erb +6 -0
- data/app/views/next_sges/responsible_others/index.html.erb +57 -0
- data/app/views/next_sges/responsible_others/new.html.erb +5 -0
- data/app/views/next_sges/responsible_others/show.html.erb +84 -0
- data/app/views/next_sges/school_admins/_form.html.erb +32 -0
- data/app/views/next_sges/school_admins/edit.html.erb +6 -0
- data/app/views/next_sges/school_admins/index.html.erb +31 -0
- data/app/views/next_sges/school_admins/new.html.erb +5 -0
- data/app/views/next_sges/school_admins/show.html.erb +19 -0
- data/app/views/next_sges/schools/_form.html.erb +62 -0
- data/app/views/next_sges/schools/edit.html.erb +6 -0
- data/app/views/next_sges/schools/index.html.erb +43 -0
- data/app/views/next_sges/schools/new.html.erb +5 -0
- data/app/views/next_sges/schools/show.html.erb +49 -0
- data/app/views/next_sges/students/_form.html.erb +42 -0
- data/app/views/next_sges/students/edit.html.erb +6 -0
- data/app/views/next_sges/students/index.html.erb +35 -0
- data/app/views/next_sges/students/new.html.erb +5 -0
- data/app/views/next_sges/students/show.html.erb +29 -0
- data/config/routes.rb +13 -0
- data/db/migrate/20171209090042_create_next_sges_schools.rb +17 -0
- data/db/migrate/20171209093554_create_next_sges_parents.rb +17 -0
- data/db/migrate/20171209093716_create_next_sges_collection_types.rb +13 -0
- data/db/migrate/20171209095546_create_next_sges_students.rb +17 -0
- data/db/migrate/20171209100114_create_next_sges_collection_notes.rb +14 -0
- data/db/migrate/20171209104046_create_next_sges_deposits.rb +13 -0
- data/db/migrate/20171209110844_create_next_sges_administrators.rb +10 -0
- data/db/migrate/20171209111036_create_next_sges_school_admins.rb +12 -0
- data/db/migrate/20171209213002_create_next_sges_inscriptions.rb +25 -0
- data/db/migrate/20171209213302_create_join_table_inscription_collection_type.rb +8 -0
- data/db/migrate/20171211132659_create_join_table_student_collection_type.rb +8 -0
- data/db/migrate/20171211140033_add_value_to_collection_types.rb +5 -0
- data/db/migrate/20171213141321_create_next_sges_collection_note_items.rb +26 -0
- data/db/migrate/20171213160012_add_discount_ad_fine_to_collection_type.rb +13 -0
- data/db/migrate/20171214154917_rename_anual_payment_discount_from_next_sges_collection_types.rb +5 -0
- data/db/migrate/20171214204406_add_account_balance_to_next_sges_parents.rb +17 -0
- data/db/migrate/20171215120306_remove_student_from_deposit.rb +6 -0
- data/db/migrate/20171215133634_add_type_to_next_sges_deposits.rb +5 -0
- data/db/migrate/20171215144509_add_documents_to_next_sgad_parents.rb +6 -0
- data/db/migrate/20171215175025_add_avatar_to_next_sges_schools.rb +5 -0
- data/db/migrate/20171218082424_add_nif_to_next_sges_schools.rb +11 -0
- data/db/migrate/20171221224739_create_next_sges_responsible_others.rb +24 -0
- data/db/migrate/20171227221731_add_birthday_to_next_sgad_students.rb +7 -0
- data/db/migrate/20180216140129_add_collection_note_to_deposits.rb +5 -0
- data/lib/next_sges.rb +5 -0
- data/lib/next_sges/engine.rb +5 -0
- data/lib/next_sges/version.rb +3 -0
- data/lib/tasks/next_sges_tasks.rake +4 -0
- metadata +224 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require_dependency "next_sges/application_controller"
|
|
2
|
+
|
|
3
|
+
module NextSges
|
|
4
|
+
class StudentsController < ::ApplicationController
|
|
5
|
+
before_action :set_student, only: [:show, :edit, :update, :destroy]
|
|
6
|
+
|
|
7
|
+
# GET /students
|
|
8
|
+
def index
|
|
9
|
+
@students = Student.all
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# GET /students/1
|
|
13
|
+
def show
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# GET /students/new
|
|
17
|
+
def new
|
|
18
|
+
@student = Student.new
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# GET /students/1/edit
|
|
22
|
+
def edit
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# POST /students
|
|
26
|
+
def create
|
|
27
|
+
@student = Student.new(student_params)
|
|
28
|
+
|
|
29
|
+
if @student.save
|
|
30
|
+
redirect_to @student, notice: 'Student was successfully created.'
|
|
31
|
+
else
|
|
32
|
+
render :new
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# PATCH/PUT /students/1
|
|
37
|
+
def update
|
|
38
|
+
if @student.update(student_params)
|
|
39
|
+
redirect_to @student, notice: 'Student was successfully updated.'
|
|
40
|
+
else
|
|
41
|
+
render :edit
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# DELETE /students/1
|
|
46
|
+
def destroy
|
|
47
|
+
@student.destroy
|
|
48
|
+
redirect_to students_url, notice: 'Student was successfully destroyed.'
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
# Use callbacks to share common setup or constraints between actions.
|
|
53
|
+
def set_student
|
|
54
|
+
@student = Student.find(params[:id])
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Only allow a trusted parameter "white list" through.
|
|
58
|
+
def student_params
|
|
59
|
+
params.require(:student).permit(:name, :number, :status, :parent_id, :account_balance)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module NextSges
|
|
2
|
+
class Administrator < NextSges::ApplicationRecord
|
|
3
|
+
belongs_to :user, polymorphic: true
|
|
4
|
+
INITIAL_LETTER = "A"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def map_for_select(map_method = :name)
|
|
8
|
+
all.map {|f| [f.send(map_method), f.id]}
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def map_for_filter(map_method = :name)
|
|
12
|
+
map_for_select(map_method)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
validates_presence_of :user_id
|
|
16
|
+
validates_uniqueness_of :user_id, scope: [:user_type]
|
|
17
|
+
before_create :create_number
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module NextSges
|
|
2
|
+
class ApplicationRecord < ::ApplicationRecord
|
|
3
|
+
self.abstract_class = true
|
|
4
|
+
MAIL_REGEX = /\A[a-z0-9._%+-]+@[a-z0-9._%+-].[a-z0-9._%+-]/i
|
|
5
|
+
CELL_REGEX = /d\{9}/
|
|
6
|
+
DEFAULT_STATUS_ENUM = {active: 0, inactive: 1}
|
|
7
|
+
DEFAULT_IDENTIFICATION_ENUM = {identification: 0, passport: 2}
|
|
8
|
+
|
|
9
|
+
# creates a sequencial number
|
|
10
|
+
#
|
|
11
|
+
def create_number
|
|
12
|
+
allowed_classess = [NextSges::Parent, NextSges::Student, NextSges::School, NextSges::Deposit, NextSges::Administrator, NextSges::SchoolAdmin, NextSges::CollectionNote, NextSges::ResponsibleOther].map(&:name)
|
|
13
|
+
this_class = self.class.name
|
|
14
|
+
return nil unless allowed_classess.include?(this_class)
|
|
15
|
+
i = self.class.all.map{|i| i.number.to_s.gsub(/\D/, '').to_i}.compact.sort.last || 0
|
|
16
|
+
i = i + 1
|
|
17
|
+
self.number = "#{self.class::INITIAL_LETTER}#{i.to_s.rjust(8, '0')}"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# creates a number and safes object after
|
|
21
|
+
def create_number!
|
|
22
|
+
self.save unless self.create_number.nil?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.map_for_select(map_method = :number)
|
|
26
|
+
all.map{|f| [f.send(map_method), f.id]}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.map_for_filter(map_method = :number)
|
|
30
|
+
map_for_select(map_method)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
module NextSges
|
|
2
|
+
class CollectionNote < NextSges::ApplicationRecord
|
|
3
|
+
belongs_to :student
|
|
4
|
+
belongs_to :school
|
|
5
|
+
has_many :collection_note_items, dependent: :destroy
|
|
6
|
+
has_many :deposits
|
|
7
|
+
|
|
8
|
+
enum status: {inactive: 0, active: 1, paid: 2}
|
|
9
|
+
INITIAL_LETTER = "F"
|
|
10
|
+
|
|
11
|
+
# Initialize item for a collection note with a given type
|
|
12
|
+
def new_item(collection_type)
|
|
13
|
+
NextSges::CollectionNoteItem.new(collection_note_id: id,
|
|
14
|
+
collection_type_id: collection_type.id,
|
|
15
|
+
collection_type_frequency: collection_type.frequency,
|
|
16
|
+
collection_type_category: collection_type.category,
|
|
17
|
+
school_id: school_id,
|
|
18
|
+
student_id: student_id)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Set Collection note to active
|
|
22
|
+
def activate
|
|
23
|
+
return unless can_activate?
|
|
24
|
+
if inactive?
|
|
25
|
+
active!
|
|
26
|
+
elsif paid?
|
|
27
|
+
if active!
|
|
28
|
+
encharged = student.parent
|
|
29
|
+
encharged.update_columns(account_balance: encharged.account_balance + value)
|
|
30
|
+
end
|
|
31
|
+
# TODO, Make code to return money address
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# set collection note to inactive
|
|
36
|
+
def deactivate
|
|
37
|
+
return unless can_deactivate?
|
|
38
|
+
inactive!
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Mark collection note to paid
|
|
42
|
+
def pay
|
|
43
|
+
return unless can_pay?
|
|
44
|
+
if paid!
|
|
45
|
+
encharged = student.parent
|
|
46
|
+
encharged.update_columns(account_balance: encharged.account_balance - value)
|
|
47
|
+
end
|
|
48
|
+
# Make Code to move money
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Set Collection note to active
|
|
52
|
+
def can_activate?
|
|
53
|
+
inactive? || paid?
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# set Collection note to inactive
|
|
57
|
+
def can_deactivate?
|
|
58
|
+
active?
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Mark Collection note to paid
|
|
62
|
+
def can_pay?
|
|
63
|
+
active? && student.parent.account_balance >= value
|
|
64
|
+
end
|
|
65
|
+
validates_presence_of :student_id, :school_id
|
|
66
|
+
before_create :create_number
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
module NextSges
|
|
2
|
+
class CollectionNoteItem < NextSges::ApplicationRecord
|
|
3
|
+
# BEGIN relations enumns and scopes do
|
|
4
|
+
belongs_to :collection_note
|
|
5
|
+
belongs_to :collection_type
|
|
6
|
+
belongs_to :student
|
|
7
|
+
belongs_to :school
|
|
8
|
+
|
|
9
|
+
enum collection_type_frequency: CollectionType.frequencies
|
|
10
|
+
enum collection_type_category: CollectionType.categories
|
|
11
|
+
|
|
12
|
+
# END
|
|
13
|
+
|
|
14
|
+
# BEGIN instance methods
|
|
15
|
+
def map_for_select(method_name = :name)
|
|
16
|
+
super(:name)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def map_for_filter(method_name = :name)
|
|
20
|
+
map_for_select(:name)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# checks if a fine can be created for this item
|
|
24
|
+
def can_create_fine?
|
|
25
|
+
ob_coll_note = collection_note
|
|
26
|
+
ob_coll_type = collection_type
|
|
27
|
+
return false unless id.present?
|
|
28
|
+
return false if collection_note.paid?
|
|
29
|
+
return false unless (ob_coll_type.first_fine_after > 0 && ob_coll_type.first_fine_percentage > 0) ||
|
|
30
|
+
(ob_coll_type.second_fine_after > 0 && ob_coll_type.second_fine_percentage > 0)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
if ob_coll_type.no_frequency?
|
|
34
|
+
date_to_compare = updated_at
|
|
35
|
+
else
|
|
36
|
+
return false unless date.present?
|
|
37
|
+
date_to_compare = date
|
|
38
|
+
end
|
|
39
|
+
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
|
|
40
|
+
return true
|
|
41
|
+
end
|
|
42
|
+
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
|
|
43
|
+
true
|
|
44
|
+
else
|
|
45
|
+
false
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# check if there is a fine to destroy
|
|
50
|
+
def can_destroy_fine?
|
|
51
|
+
fine_percentage > 0 || fine_value > 0
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def create_fine
|
|
55
|
+
return unless can_create_fine?
|
|
56
|
+
ob_coll_note = collection_note
|
|
57
|
+
ob_coll_type = collection_type
|
|
58
|
+
|
|
59
|
+
return false unless date.present?
|
|
60
|
+
|
|
61
|
+
if ob_coll_type.no_frequency?
|
|
62
|
+
date_to_compare = updated_at
|
|
63
|
+
else
|
|
64
|
+
date_to_compare = date
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
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
|
|
68
|
+
self.fine_description = "Multa de Atrazo de #{ob_coll_type.second_fine_after} dias"
|
|
69
|
+
self.fine_percentage = ob_coll_type.second_fine_percentage
|
|
70
|
+
end
|
|
71
|
+
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
|
|
72
|
+
self.fine_description = "Multa de Atrazo de #{ob_coll_type.first_fine_after} dias"
|
|
73
|
+
self.fine_percentage = ob_coll_type.first_fine_percentage
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def create_fine!
|
|
78
|
+
create_fine
|
|
79
|
+
save
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# remove fine assossiated values
|
|
83
|
+
def destroy_fine
|
|
84
|
+
self.fine_description = nil
|
|
85
|
+
self.fine_percentage = 0
|
|
86
|
+
self.fine_value = 0
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# remove fine values and saves item
|
|
90
|
+
def destroy_fine!
|
|
91
|
+
destroy_fine
|
|
92
|
+
save
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# END
|
|
96
|
+
|
|
97
|
+
# BEGIN class methods
|
|
98
|
+
# END
|
|
99
|
+
|
|
100
|
+
# BEGIN Validations and events
|
|
101
|
+
validates_presence_of :collection_note_id, :collection_type_id, :student_id, :school_id
|
|
102
|
+
validates_presence_of :date, unless: -> (item) {item&.collection_type&.no_frequency?}
|
|
103
|
+
validate :validate_date, unless: -> (item) {item&.collection_type&.no_frequency?}
|
|
104
|
+
|
|
105
|
+
before_save :before_create_initialize_item
|
|
106
|
+
after_save :update_collection_note
|
|
107
|
+
after_destroy :update_collection_note
|
|
108
|
+
# END
|
|
109
|
+
|
|
110
|
+
# BEGIN Validation methods and events
|
|
111
|
+
# copies information from collection_note and colection_type to collection_note_item
|
|
112
|
+
def before_create_initialize_item
|
|
113
|
+
return if collection_note_id.nil? if collection_type_id.nil? if student_id.nil? if school_id.nil?
|
|
114
|
+
# student.parent.students_number
|
|
115
|
+
ob_coll_note = collection_note
|
|
116
|
+
ob_coll_type = collection_type
|
|
117
|
+
ob_student = student
|
|
118
|
+
ob_school = school
|
|
119
|
+
students_number = student.parent.students.size
|
|
120
|
+
discount = 0
|
|
121
|
+
desc = ''
|
|
122
|
+
|
|
123
|
+
if students_number >= 2 && ob_coll_type.second_child_discount > discount
|
|
124
|
+
discount = ob_coll_type.second_child_discount
|
|
125
|
+
desc = "Desconto pelo segundo Educando inscrito"
|
|
126
|
+
end
|
|
127
|
+
if students_number >= 3 && ob_coll_type.third_child_discount > discount
|
|
128
|
+
discount = ob_coll_type.third_child_discount
|
|
129
|
+
desc = "Desconto pelo segundo Educando inscrito"
|
|
130
|
+
end
|
|
131
|
+
if ob_coll_type.annual? && ob_coll_type.annual_payment_discount > discount
|
|
132
|
+
discount = ob_coll_type.annual_payment_discount
|
|
133
|
+
desc = "Desconto pelo segundo Educando inscrito"
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
self.discount_percentage = discount
|
|
137
|
+
self.discount_description = desc
|
|
138
|
+
|
|
139
|
+
self.base_value = ob_coll_type.value
|
|
140
|
+
self.fine_value = (fine_percentage/100) * (base_value * quantity)
|
|
141
|
+
self.discount_value = (discount_percentage/100) * (base_value * quantity)
|
|
142
|
+
self.value = (base_value * quantity) + fine_value - discount_value
|
|
143
|
+
self.name = ob_coll_type.name
|
|
144
|
+
self.collection_type_category = ob_coll_type.category
|
|
145
|
+
self.collection_type_frequency = ob_coll_type.frequency
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# update collection note value
|
|
149
|
+
def update_collection_note
|
|
150
|
+
ob_coll_note = collection_note
|
|
151
|
+
ob_coll_note.update_columns(value: ob_coll_note.collection_note_items.sum(:value))
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def validate_date
|
|
155
|
+
return unless date.present?
|
|
156
|
+
ob_coll_note = collection_note
|
|
157
|
+
ob_coll_type = collection_type
|
|
158
|
+
ob_student = student
|
|
159
|
+
ob_school = school
|
|
160
|
+
|
|
161
|
+
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?
|
|
162
|
+
errors.add(:date, "Já tem um pagamento nesta data para - #{ob_coll_type.name}")
|
|
163
|
+
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?
|
|
164
|
+
errors.add(:date, "Já tem um pagamento nesta data para - #{ob_coll_type.name}")
|
|
165
|
+
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?
|
|
166
|
+
errors.add(:date, "Já tem um pagamento nesta data para - #{ob_coll_type.name}")
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
end
|
|
170
|
+
# END
|
|
171
|
+
|
|
172
|
+
end
|
|
173
|
+
end
|