Sha256: 00daea035b68d67100c0f327c03dbebd0bfdc1c5dd20bbcda3f4d2e59d2cfec7

Contents?: true

Size: 1.82 KB

Versions: 7

Compression:

Stored size: 1.82 KB

Contents

module Dorsale
  module BillingMachine
    class Quotation < ActiveRecord::Base
      self.table_name = "dorsale_billing_machine_quotations"

      belongs_to :customer, polymorphic: true
      belongs_to :id_card
      belongs_to :payment_term
      has_many :lines,
        :inverse_of => :quotation,
        :dependent  => :destroy,
        :class_name => ::Dorsale::BillingMachine::QuotationLine

      has_many :attachments,
        :as         => :attachable,
        :dependent  => :destroy,
        :class_name => ::Dorsale::Alexandrie::Attachment

      accepts_nested_attributes_for :lines, allow_destroy: true

      polymorphic_id_for :customer

      validates :id_card, presence: true
      validates :date,    presence: true

      # simple_form
      validates :id_card_id, presence: true

      def initialize(*args)
        super
        self.date       = Date.today     if date.nil?
        self.expires_at = date + 1.month if expires_at.nil?
        self.vat_rate   = 20             if vat_rate.nil?
      end

      before_create :assign_unique_index
      before_create :assign_tracking_id

      def assign_unique_index
        if unique_index.nil?
          self.unique_index = self.class.all.pluck(:unique_index).max.to_i.next
        end
      end

      def assign_tracking_id
        self.tracking_id = date.year.to_s + "-" + unique_index.to_s.rjust(2, "0")
      end

      before_save :update_total

      def update_total
        self.vat_rate        = 0 if vat_rate.nil?
        self.total_duty      = lines.pluck(:total).sum
        self.vat_amount      = total_duty * vat_rate / 100.0
        self.total_all_taxes = total_duty + vat_amount
      end

      def pdf
        pdf = ::Dorsale::BillingMachine::QuotationPdf.new(self)
        pdf.build
        pdf
      end

    end # Quotation
  end # BillingMachine
end # Dorsale

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
dorsale-2.1.5 app/models/dorsale/billing_machine/quotation.rb
dorsale-2.1.4 app/models/dorsale/billing_machine/quotation.rb
dorsale-2.1.3 app/models/dorsale/billing_machine/quotation.rb
dorsale-2.1.2 app/models/dorsale/billing_machine/quotation.rb
dorsale-2.1.1 app/models/dorsale/billing_machine/quotation.rb
dorsale-2.1.0 app/models/dorsale/billing_machine/quotation.rb
dorsale-2.0.1 app/models/dorsale/billing_machine/quotation.rb