Class: Osm::Invoice::Item
Constant Summary collapse
- SORT_BY =
[:invoice, :date]
Instance Attribute Summary collapse
-
#amount ⇒ Fixnum
The amount of the transaction.
-
#budget_name ⇒ Fixnum
The name of the budget this item is assigned to.
-
#date ⇒ Fixnum
The date the item was paid/received.
-
#description ⇒ Fixnum
A description for the transaction.
-
#id ⇒ Fixnum
The OSM ID for the invoice item.
-
#invoice ⇒ Osm::Invoice
The Osm::Invoice the item belongs to.
-
#payto ⇒ Fixnum
Who paid/reimbursed.
-
#record_id ⇒ Fixnum
The id of the item within the invoice.
-
#type ⇒ Fixnum
The type of transaction (:expense or :income).
Instance Method Summary collapse
-
#create(api) ⇒ Boolean
Create the item in OSM.
-
#delete(api) ⇒ Boolean
Delete invoice item from OSM.
-
#initialize ⇒ Object
constructor
Initialize a new Budget.
-
#update(api) ⇒ Boolean
Update invoice item in OSM.
-
#value ⇒ Float
Get value of this item for easy totaling.
Methods inherited from Model
#<, #<=, #<=>, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i
Constructor Details
#initialize ⇒ Object
Initialize a new Budget
|
# File 'lib/osm/invoice.rb', line 309
|
Instance Attribute Details
#amount ⇒ Fixnum
Returns The amount of the transaction.
284 |
# File 'lib/osm/invoice.rb', line 284 attribute :id, :type => Integer |
#budget_name ⇒ Fixnum
Returns The name of the budget this item is assigned to.
284 |
# File 'lib/osm/invoice.rb', line 284 attribute :id, :type => Integer |
#date ⇒ Fixnum
Returns The date the item was paid/received.
284 |
# File 'lib/osm/invoice.rb', line 284 attribute :id, :type => Integer |
#description ⇒ Fixnum
Returns A description for the transaction.
284 |
# File 'lib/osm/invoice.rb', line 284 attribute :id, :type => Integer |
#id ⇒ Fixnum
Returns The OSM ID for the invoice item.
284 |
# File 'lib/osm/invoice.rb', line 284 attribute :id, :type => Integer |
#invoice ⇒ Osm::Invoice
Returns The Osm::Invoice the item belongs to.
284 |
# File 'lib/osm/invoice.rb', line 284 attribute :id, :type => Integer |
#payto ⇒ Fixnum
Returns Who paid/reimbursed.
284 |
# File 'lib/osm/invoice.rb', line 284 attribute :id, :type => Integer |
#record_id ⇒ Fixnum
Returns The id of the item within the invoice.
284 |
# File 'lib/osm/invoice.rb', line 284 attribute :id, :type => Integer |
#type ⇒ Fixnum
Returns The type of transaction (:expense or :income).
284 |
# File 'lib/osm/invoice.rb', line 284 attribute :id, :type => Integer |
Instance Method Details
#create(api) ⇒ Boolean
Create the item in OSM
318 319 320 321 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 |
# 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}§ionid=#{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§ionid=#{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
391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# 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§ionid=#{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
354 355 356 357 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 |
# 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§ionid=#{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 ⇒ Float
Get value of this item for easy totaling
408 409 410 411 412 |
# 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 |