Sha256: 6d5af44bba0a54ce689b3f2719ccd26b83d09cd843378382bd13a2d5edff051b

Contents?: true

Size: 1.78 KB

Versions: 17

Compression:

Stored size: 1.78 KB

Contents

module Comee
  module Core
    class CreditNote < ApplicationRecord
      before_validation :generate_credit_note_no, if: proc { |note| note.credit_note_no.nil? }
      after_save :update_amount_credited, if: proc { |note| note.approved? }

      belongs_to :invoice
      has_many :credit_note_items
      has_many :credit_note_additional_items

      enum :status, {draft: 0, approved: 1, issued: 2}

      validates :credit_note_no, presence: true, uniqueness: true
      validates :status, presence: true
      validates :total_price, numericality: {greater_than_or_equal_to: 0, allow_nil: true}
      validate :validate_total_price

      def calculate_total_price
        self.total_price = calculate_total
      end

      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

      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

      def update_amount_credited
        invoice.amount_credited = invoice.calculate_amount_credited
        invoice.save!
      end

      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

      def self.ransackable_attributes(_auth_object = nil)
        %w[
          id
          invoice_id
          credit_note_no
          created_at
          status
        ]
      end

      def self.ransackable_associations(_auth_object = nil)
        ["invoice"]
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
comee_core-0.3.62 app/models/comee/core/credit_note.rb
comee_core-0.3.61 app/models/comee/core/credit_note.rb
comee_core-0.3.60 app/models/comee/core/credit_note.rb
comee_core-0.3.59 app/models/comee/core/credit_note.rb
comee_core-0.3.58 app/models/comee/core/credit_note.rb
comee_core-0.3.57 app/models/comee/core/credit_note.rb
comee_core-0.3.56 app/models/comee/core/credit_note.rb
comee_core-0.3.55 app/models/comee/core/credit_note.rb
comee_core-0.3.54 app/models/comee/core/credit_note.rb
comee_core-0.3.53 app/models/comee/core/credit_note.rb
comee_core-0.3.51 app/models/comee/core/credit_note.rb
comee_core-0.3.50 app/models/comee/core/credit_note.rb
comee_core-0.3.49 app/models/comee/core/credit_note.rb
comee_core-0.3.48 app/models/comee/core/credit_note.rb
comee_core-0.3.47 app/models/comee/core/credit_note.rb
comee_core-0.3.46 app/models/comee/core/credit_note.rb
comee_core-0.3.45 app/models/comee/core/credit_note.rb