Sha256: b83f99b1b884aae302dc178d418d537e7f7ff93ea1a439b28a30725d5f4b0586

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

module XeroGateway
  module LineItemCalculations

    def add_line_item(params = {})
      line_item = nil
      case params
        when Hash then      line_item = LineItem.new(params)
        when LineItem then  line_item = params
        else                raise InvalidLineItemError
      end
      @line_items << line_item
      line_item
    end

    %w(sub_total total_tax total).each do |line_item_total_type|
      define_method("#{line_item_total_type}=") do |new_total|
        instance_variable_set("@#{line_item_total_type}", new_total) unless line_items_downloaded?
      end
    end

    # Calculate the sub_total as the SUM(line_item.line_amount).
    def sub_total
      total_cache(:sub_total) || sum_line_items(line_items, :line_amount)
    end

    # Calculate the total_tax as the SUM(line_item.tax_amount).
    def total_tax
      total_cache(:total_tax) || sum_line_items(line_items, :tax_amount)
    end

    # Calculate the toal as sub_total + total_tax.
    def total
      total_cache(:total) || (sub_total + total_tax)
    end

    private

      def total_cache(name)
        instance_variable_defined?("@#{name}") && instance_variable_get("@#{name}")
      end

      def sum_line_items(lines, sum_type = :line_amount)
        lines.inject(BigDecimal('0')) do |sum, line|
          sum + BigDecimal(line.send(sum_type).to_s)
        end
      end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xero_gateway-2.7.0 lib/xero_gateway/line_item_calculations.rb