class Osm::Invoice::Item

Constants

SORT_BY

Public Instance Methods

create(api) click to toggle source

Create the item in OSM @return [Boolean] Whether the item was created in OSM @raise [Osm::ObjectIsInvalid] If the Item is invalid @raise [Osm::Error] If the invoice item already exists in OSM

# File lib/osm/invoice.rb, line 318
def create(api)
  raise Osm::Error, 'the invoice item already exists in OSM' unless id.nil?
  raise Osm::ObjectIsInvalid, 'invoice item is invalid' unless valid?
  Osm::Model.require_ability_to(api, :write, :finance, invoice.section_id)

  last_item = invoice.get_items(api, {:no_cache=>true}).sort{ |a,b| a.record_id <=> b.record_id }[-1]

  data = api.perform_query("finances.php?action=addRecord&invoiceid=#{invoice.id}&sectionid=#{invoice.section_id}")
  if data.is_a?(Hash) && data['ok'].eql?(true)
    new_item = invoice.get_items(api, {:no_cache => true}).sort{ |a,b| a.record_id <=> b.record_id }[-1]
    if !new_item.nil? && (last_item.try(:id) != new_item.try(:id))
      # The cached invoice items for the section will be out of date - remove them
      cache_delete(api, ['invoice_items', invoice.id])
      self.id = new_item.id
      self.record_id = new_item.record_id
      # Update attributes in OSM
      [['amount', amount], ['comments', description], ['type', type.to_s.titleize], ['payto_userid', payto], ['categoryid', budget_name], ['entrydate', date.strftime(Osm::OSM_DATE_FORMAT)]].each do |osm_name, value|
        api.perform_query("finances.php?action=updateRecord&sectionid=#{invoice.section_id}&dateFormat=generic", {
          'section_id' => invoice.section_id,
          'invoiceid' => invoice.id,
          'recordid' => record_id,
          'row' => 0,
          'column' => osm_name,
          'value' => value,
        })
      end
      return true
    end
  end
  return false
end
delete(api) click to toggle source

Delete invoice item from OSM @param [Osm::Api] api The api to use to make the request @return [Boolean] whether the delete succedded

# File lib/osm/invoice.rb, line 391
def delete(api)
  require_ability_to(api, :write, :finance, invoice.section_id)

  data = api.perform_query("finances.php?action=deleteEntry&sectionid=#{invoice.section_id}", {
    'id' => id,
  })

  if data.is_a?(Hash) && data['ok']
    # The cached invoice items for the section will be out of date - remove them
    cache_delete(api, ['invoice_items', invoice.id])
    return true
  end
  return false
end
update(api) click to toggle source

Update invoice item in OSM @param [Osm::Api] api The api to use to make the request @return [Boolean] whether the update succedded @raise [Osm::ObjectIsInvalid] If the Invoice is invalid

# File lib/osm/invoice.rb, line 354
def update(api)
  require_ability_to(api, :write, :finance, invoice.section_id)
  raise Osm::ObjectIsInvalid, 'invoice item is invalid' unless valid?

  updated = true
  to_update = Array.new
  to_update.push ['amount', amount] if changed_attributes.include?('amount')
  to_update.push ['comments', description] if changed_attributes.include?('description')
  to_update.push ['type', type.to_s.titleize] if changed_attributes.include?('type')
  to_update.push ['payto_userid', payto] if changed_attributes.include?('payto')
  to_update.push ['categoryid', budget_name] if changed_attributes.include?('budget_name')
  to_update.push ['entrydate', date.strftime(Osm::OSM_DATE_FORMAT)] if changed_attributes.include?('date')
  to_update.each do |osm_name, value|
    data = api.perform_query("finances.php?action=updateRecord&sectionid=#{invoice.section_id}&dateFormat=generic", {
      'section_id' => invoice.section_id,
      'invoiceid' => invoice.id,
      'recordid' => record_id,
      'row' => 0,
      'column' => osm_name,
      'value' => value,
    })
    updated &&= (data.is_a?(Hash) && data[osm_name].to_s.eql?(value.to_s))
  end

  if updated
    reset_changed_attributes
    # The cached items for the invoice will be out of date - remove them
    cache_delete(api, ['invoice_items', invoice.id])
    return true
  else
    return false
  end
end
value() click to toggle source

Get value of this item for easy totaling @return [Float]

# File lib/osm/invoice.rb, line 408
def value
  return amount.to_f if type.eql?(:income)
  return -amount.to_f if type.eql?(:expense)
  return 0.0
end