Sha256: 33b6c66faf82ebbe86e86b9677f9a5953c2050a2f32ea892f36b926bb9143fc2

Contents?: true

Size: 1.83 KB

Versions: 16

Compression:

Stored size: 1.83 KB

Contents

module Comee
  module Core
    class Invoice < ApplicationRecord
      before_validation :generate_invoice_no, if: proc { |inv| inv.invoice_no.nil? }

      belongs_to :sales_order
      belongs_to :pod
      has_many :invoice_items
      has_many :additional_items
      has_many :payments

      has_noticed_notifications model_name: "Comee::Core::Notification"

      enum :status, {draft: 0, approved: 1, issued: 2}
      enum :payment_status, {not_paid: 0, partially_paid: 1, fully_paid: 2, overpaid: 3}

      validates :invoice_no, uniqueness: true
      validates :status, :payment_status, presence: true
      validates :total_price, numericality: {greater_than_or_equal_to: 0, allow_nil: true}
      validates :amount_paid, numericality: {greater_than_or_equal_to: 0, allow_nil: true}

      delegate(:order_number, to: :sales_order, prefix: false)
      delegate(:reference_no, to: :pod, prefix: true)

      def calculate_total_price
        self.total_price = calculate_total
      end

      def calculate_vat
        return unless sales_order.client.tax_code == "Inland"

        self.vat = (calculate_total * 0.19).round(2)
      end

      def calculate_total
        items = InvoiceItem.where(invoice_id: id)
        additionals = AdditionalItem.where(invoice_id: id)
        (items.sum(:total_price) + additionals.sum(:total_price)).round(2)
      end

      def self.ransackable_attributes(_auth_object = nil)
        %w[
          id
          sales_order_id
          invoice_no
          date_issued
          ship_name
          delivery_date
          voyage_no
          status
          payment_status
        ]
      end

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

      def generate_invoice_no
        self.invoice_no = Util.generate_number("Invoice", "invoice_no")
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
comee_core-0.3.33 app/models/comee/core/invoice.rb
comee_core-0.3.32 app/models/comee/core/invoice.rb
comee_core-0.3.31 app/models/comee/core/invoice.rb
comee_core-0.3.30 app/models/comee/core/invoice.rb
comee_core-0.3.29 app/models/comee/core/invoice.rb
comee_core-0.3.28 app/models/comee/core/invoice.rb
comee_core-0.3.27 app/models/comee/core/invoice.rb
comee_core-0.3.26 app/models/comee/core/invoice.rb
comee_core-0.3.25 app/models/comee/core/invoice.rb
comee_core-0.3.24 app/models/comee/core/invoice.rb
comee_core-0.3.23 app/models/comee/core/invoice.rb
comee_core-0.3.22 app/models/comee/core/invoice.rb
comee_core-0.3.21 app/models/comee/core/invoice.rb
comee_core-0.3.20 app/models/comee/core/invoice.rb
comee_core-0.3.19 app/models/comee/core/invoice.rb
comee_core-0.3.18 app/models/comee/core/invoice.rb