Class: Comee::Core::CreditNote
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Comee::Core::CreditNote
- Defined in:
- app/models/comee/core/credit_note.rb
Class Method Summary collapse
- .ransackable_associations(_auth_object = nil) ⇒ Object
- .ransackable_attributes(_auth_object = nil) ⇒ Object
Instance Method Summary collapse
- #calculate_total ⇒ Object
- #calculate_total_price ⇒ Object
- #generate_credit_note_no ⇒ Object
- #update_amount_credited ⇒ Object
- #validate_total_price ⇒ Object
Class Method Details
.ransackable_associations(_auth_object = nil) ⇒ Object
55 56 57 |
# File 'app/models/comee/core/credit_note.rb', line 55 def self.ransackable_associations(_auth_object = nil) ["invoice"] end |
.ransackable_attributes(_auth_object = nil) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'app/models/comee/core/credit_note.rb', line 45 def self.ransackable_attributes(_auth_object = nil) %w[ id invoice_id credit_note_no created_at status ] end |
Instance Method Details
#calculate_total ⇒ Object
22 23 24 25 26 |
# File 'app/models/comee/core/credit_note.rb', line 22 def calculate_total items = CreditNoteItem.where(credit_note_id: id) additionals = CreditNoteAdditionalItem.where(credit_note_id: id) (items.sum(:total_price) + additionals.sum(:total_price)).round(2) end |
#calculate_total_price ⇒ Object
18 19 20 |
# File 'app/models/comee/core/credit_note.rb', line 18 def calculate_total_price self.total_price = calculate_total end |
#generate_credit_note_no ⇒ Object
28 29 30 31 |
# File 'app/models/comee/core/credit_note.rb', line 28 def generate_credit_note_no year = Date.current.year.to_s self.credit_note_no = Util.generate_number("CreditNote", "credit_note_no", year, "-", 100001) end |
#update_amount_credited ⇒ Object
33 34 35 36 |
# File 'app/models/comee/core/credit_note.rb', line 33 def update_amount_credited invoice.amount_credited = invoice.calculate_amount_credited invoice.save! end |
#validate_total_price ⇒ Object
38 39 40 41 42 43 |
# File 'app/models/comee/core/credit_note.rb', line 38 def validate_total_price return unless total_price && invoice balance = invoice.total_price - invoice.amount_paid errors.add(:total_price, "exceeds invoice unpaid amount") if total_price > balance end |