Sha256: d5b785c1acf6366f8d486f718bb088f086c1c576c0c250758ea15a5296438b69

Contents?: true

Size: 897 Bytes

Versions: 41

Compression:

Stored size: 897 Bytes

Contents

module Dorsale
  module BillingMachine
    class InvoiceLine < ActiveRecord::Base
      self.table_name = "dorsale_billing_machine_invoice_lines"

      belongs_to :invoice, inverse_of: :lines

      validates :invoice, presence: true

      default_scope -> {
        order(created_at: :asc)
      }

      def initialize(*)
        super
        assign_default_values
      end

      before_validation :assign_default_values
      before_validation :update_total

      def assign_default_values
          self.quantity   ||= 0
          self.unit_price ||= 0
          self.vat_rate ||= ::Dorsale::BillingMachine::DEFAULT_VAT_RATE
      end

      def update_total
        assign_default_values
        self.total = self.quantity * self.unit_price
      end

      after_save :update_invoice_total

      def update_invoice_total
        self.invoice.reload.save!
      end

    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
dorsale-2.4.1 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.4.0 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.3.5 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.3.4 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.3.3 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.3.2 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.3.1 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.3.0 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.2.16 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.2.15 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.2.14 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.2.13 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.2.12 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.2.11 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.2.10 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.2.9 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.2.8 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.2.6 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.2.5 app/models/dorsale/billing_machine/invoice_line.rb
dorsale-2.2.4 app/models/dorsale/billing_machine/invoice_line.rb