Class: Osm::Invoice::Item

Inherits:
Model
  • Object
show all
Defined in:
lib/osm/invoice.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Constructor Details

#initializeObject

Initialize a new Budget

Parameters:

  • attributes (Hash)

    The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)



# File 'lib/osm/invoice.rb', line 313

Instance Attribute Details

#amountFixnum

Returns The amount of the transaction.

Returns:

  • (Fixnum)

    The amount of the transaction



288
# File 'lib/osm/invoice.rb', line 288

attribute :id, :type => Integer

#budget_nameFixnum

Returns The name of the budget this item is assigned to.

Returns:

  • (Fixnum)

    The name of the budget this item is assigned to



288
# File 'lib/osm/invoice.rb', line 288

attribute :id, :type => Integer

#dateFixnum

Returns The date the item was paid/received.

Returns:

  • (Fixnum)

    The date the item was paid/received



288
# File 'lib/osm/invoice.rb', line 288

attribute :id, :type => Integer

#descriptionFixnum

Returns A description for the transaction.

Returns:

  • (Fixnum)

    A description for the transaction



288
# File 'lib/osm/invoice.rb', line 288

attribute :id, :type => Integer

#idFixnum

Returns The OSM ID for the invoice item.

Returns:

  • (Fixnum)

    The OSM ID for the invoice item



288
# File 'lib/osm/invoice.rb', line 288

attribute :id, :type => Integer

#invoiceOsm::Invoice

Returns The Osm::Invoice the item belongs to.

Returns:



288
# File 'lib/osm/invoice.rb', line 288

attribute :id, :type => Integer

#paytoFixnum

Returns Who paid/reimbursed.

Returns:

  • (Fixnum)

    Who paid/reimbursed



288
# File 'lib/osm/invoice.rb', line 288

attribute :id, :type => Integer

#record_idFixnum

Returns The id of the item within the invoice.

Returns:

  • (Fixnum)

    The id of the item within the invoice



288
# File 'lib/osm/invoice.rb', line 288

attribute :id, :type => Integer

#typeFixnum

Returns The type of transaction (:expense or :income).

Returns:

  • (Fixnum)

    The type of transaction (:expense or :income)



288
# File 'lib/osm/invoice.rb', line 288

attribute :id, :type => Integer

Instance Method Details

#<=>(another) ⇒ Object

Compare Invoice Item based on invoice then date



419
420
421
422
423
# File 'lib/osm/invoice.rb', line 419

def <=>(another)
  result = self.invoice <=> another.try(:invoice)
  result = self.date <=> another.try(:date) if result == 0
  return result
end

#create(api) ⇒ Boolean

Create the item in OSM

Returns:

  • (Boolean)

    Whether the item was created in OSM

Raises:



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/osm/invoice.rb', line 322

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) ⇒ Boolean

Delete invoice item from OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the delete succedded



395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/osm/invoice.rb', line 395

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) ⇒ Boolean

Update invoice item in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the update succedded

Raises:



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/osm/invoice.rb', line 358

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

#valueFloat

Get value of this item for easy totaling

Returns:

  • (Float)


412
413
414
415
416
# File 'lib/osm/invoice.rb', line 412

def value
  return amount.to_f if type.eql?(:income)
  return -amount.to_f if type.eql?(:expense)
  return 0.0
end