Class: Comee::Core::CreditNote

Inherits:
ApplicationRecord show all
Defined in:
app/models/comee/core/credit_note.rb

Class Method Summary collapse

Instance Method Summary collapse

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_totalObject



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_priceObject



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_noObject



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_creditedObject



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_priceObject



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