lib/xero_gateway/invoice.rb in xero_gateway-2.0.13 vs lib/xero_gateway/invoice.rb in xero_gateway-2.0.14

- old
+ new

@@ -1,13 +1,12 @@ module XeroGateway class Invoice include Dates include Money + include LineItemCalculations - class Error < RuntimeError; end class NoGatewayError < Error; end - class InvalidLineItemError < Error; end INVOICE_TYPE = { 'ACCREC' => 'Accounts Receivable', 'ACCPAY' => 'Accounts Payable' } unless defined?(INVOICE_TYPE) @@ -37,11 +36,11 @@ # Represents whether the line_items have been downloaded when getting from GET /API.XRO/2.0/INVOICES attr_accessor :line_items_downloaded # All accessible fields - attr_accessor :invoice_id, :invoice_number, :invoice_type, :invoice_status, :date, :due_date, :reference, :line_amount_types, :currency_code, :line_items, :contact, :payments, :fully_paid_on, :amount_due, :amount_paid, :amount_credited, :sent_to_contact + attr_accessor :invoice_id, :invoice_number, :invoice_type, :invoice_status, :date, :due_date, :reference, :line_amount_types, :currency_code, :line_items, :contact, :payments, :fully_paid_on, :amount_due, :amount_paid, :amount_credited, :sent_to_contact, :url def initialize(params = {}) @errors ||= [] @payments ||= [] @@ -105,62 +104,10 @@ def contact @contact ||= build_contact end - # Helper method to create a new associated line_item. - # Usage: - # invoice.add_line_item({:description => "Bob's Widgets", :quantity => 1, :unit_amount => 120}) - 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 - - # Deprecated (but API for setter remains). - # - # As sub_total must equal SUM(line_item.line_amount) for the API call to pass, this is now - # automatically calculated in the sub_total method. - def sub_total=(value) - end - - # Calculate the sub_total as the SUM(line_item.line_amount). - def sub_total - line_items.inject(BigDecimal.new('0')) { | sum, line_item | sum + BigDecimal.new(line_item.line_amount.to_s) } - end - - # Deprecated (but API for setter remains). - # - # As total_tax must equal SUM(line_item.tax_amount) for the API call to pass, this is now - # automatically calculated in the total_tax method. - def total_tax=(value) - end - - # Calculate the total_tax as the SUM(line_item.tax_amount). - def total_tax - line_items.inject(BigDecimal.new('0')) { | sum, line_item | sum + BigDecimal.new(line_item.tax_amount.to_s) } - end - - # Deprecated (but API for setter remains). - # - # As total must equal sub_total + total_tax for the API call to pass, this is now - # automatically calculated in the total method. - def total=(value) - end - - # Calculate the toal as sub_total + total_tax. - def total - sub_total + total_tax - end - # Helper method to check if the invoice is accounts payable. def accounts_payable? invoice_type == 'ACCPAY' end @@ -176,16 +123,14 @@ # If line items are not downloaded, then attempt a download now (if this record was found to begin with). def line_items if line_items_downloaded? @line_items - - # There is an invoice_is so we can assume this record was loaded from Xero. - # attempt to download the line_item records. - elsif invoice_id =~ GUID_REGEX - raise NoGatewayError unless @gateway - + + elsif invoice_id =~ GUID_REGEX && @gateway + # There is an invoice_id so we can assume this record was loaded from Xero. + # Let's attempt to download the line_item records (if there is a gateway) response = @gateway.get_invoice(invoice_id) raise InvoiceNotFoundError, "Invoice with ID #{invoice_id} not found in Xero." unless response.success? && response.invoice.is_a?(XeroGateway::Invoice) @line_items = response.invoice.line_items @line_items_downloaded = true @@ -248,10 +193,11 @@ b.LineItems { self.line_items.each do |line_item| line_item.to_xml(b) end } + b.Url url if url } end #TODO UpdatedDateUTC def self.from_xml(invoice_element, gateway = nil, options = {}) @@ -277,9 +223,10 @@ when "Payments" then element.children.each { | payment | invoice.payments << Payment.from_xml(payment) } when "AmountDue" then invoice.amount_due = BigDecimal.new(element.text) when "AmountPaid" then invoice.amount_paid = BigDecimal.new(element.text) when "AmountCredited" then invoice.amount_credited = BigDecimal.new(element.text) when "SentToContact" then invoice.sent_to_contact = (element.text.strip.downcase == "true") + when "Url" then invoice.url = element.text end end invoice end end