Class: YDIM::Invoice
- Inherits:
-
Object
- Object
- YDIM::Invoice
- Includes:
- ODBA::Persistable, ItemId
- Defined in:
- lib/ydim/odba.rb,
lib/ydim/invoice.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Info
Constant Summary collapse
- ODBA_SERIALIZABLE =
['@items']
Instance Attribute Summary collapse
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#date ⇒ Object
Returns the value of attribute date.
-
#debitor ⇒ Object
Returns the value of attribute debitor.
-
#deleted ⇒ Object
Returns the value of attribute deleted.
-
#description ⇒ Object
Returns the value of attribute description.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#payment_period ⇒ Object
Returns the value of attribute payment_period.
-
#payment_received ⇒ Object
Returns the value of attribute payment_received.
-
#precision ⇒ Object
Returns the value of attribute precision.
-
#suppress_vat ⇒ Object
Returns the value of attribute suppress_vat.
-
#unique_id ⇒ Object
readonly
Returns the value of attribute unique_id.
Class Method Summary collapse
Instance Method Summary collapse
- #add_item(item) ⇒ Object
- #debitor_email ⇒ Object
- #debitor_id ⇒ Object
- #debitor_name ⇒ Object
- #due_date ⇒ Object
- #empty? ⇒ Boolean
- #info ⇒ Object
-
#initialize(unique_id) ⇒ Invoice
constructor
A new instance of Invoice.
- #invoice_key ⇒ Object
- #item(index) ⇒ Object
- #pdf_invoice(sort_args = {}) ⇒ Object
- #status ⇒ Object
- #to_pdf(sort_args = {}) ⇒ Object
Methods included from ItemId
Constructor Details
#initialize(unique_id) ⇒ Invoice
Returns a new instance of Invoice.
35 36 37 38 39 40 |
# File 'lib/ydim/invoice.rb', line 35 def initialize(unique_id) @unique_id = unique_id @items = [] @precision = 2 @payment_period = 10 end |
Instance Attribute Details
#currency ⇒ Object
Returns the value of attribute currency.
25 26 27 |
# File 'lib/ydim/invoice.rb', line 25 def currency @currency end |
#date ⇒ Object
Returns the value of attribute date.
25 26 27 |
# File 'lib/ydim/invoice.rb', line 25 def date @date end |
#debitor ⇒ Object
Returns the value of attribute debitor.
24 25 26 |
# File 'lib/ydim/invoice.rb', line 24 def debitor @debitor end |
#deleted ⇒ Object
Returns the value of attribute deleted.
25 26 27 |
# File 'lib/ydim/invoice.rb', line 25 def deleted @deleted end |
#description ⇒ Object
Returns the value of attribute description.
25 26 27 |
# File 'lib/ydim/invoice.rb', line 25 def description @description end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
24 25 26 |
# File 'lib/ydim/invoice.rb', line 24 def items @items end |
#payment_period ⇒ Object
Returns the value of attribute payment_period.
25 26 27 |
# File 'lib/ydim/invoice.rb', line 25 def payment_period @payment_period end |
#payment_received ⇒ Object
Returns the value of attribute payment_received.
25 26 27 |
# File 'lib/ydim/invoice.rb', line 25 def payment_received @payment_received end |
#precision ⇒ Object
Returns the value of attribute precision.
25 26 27 |
# File 'lib/ydim/invoice.rb', line 25 def precision @precision end |
#suppress_vat ⇒ Object
Returns the value of attribute suppress_vat.
24 25 26 |
# File 'lib/ydim/invoice.rb', line 24 def suppress_vat @suppress_vat end |
#unique_id ⇒ Object (readonly)
Returns the value of attribute unique_id.
24 25 26 |
# File 'lib/ydim/invoice.rb', line 24 def unique_id @unique_id end |
Class Method Details
.sum(key) ⇒ Object
27 28 29 30 31 |
# File 'lib/ydim/invoice.rb', line 27 def Invoice.sum(key) define_method(key) { @items.inject(0.0) { |value, item| value + item.send(key) } } end |
Instance Method Details
#add_item(item) ⇒ Object
44 45 46 47 48 |
# File 'lib/ydim/invoice.rb', line 44 def add_item(item) item.index = next_item_id @items.push(item) item end |
#debitor_email ⇒ Object
58 59 60 |
# File 'lib/ydim/invoice.rb', line 58 def debitor_email @debitor.email if(@debitor) end |
#debitor_id ⇒ Object
61 62 63 |
# File 'lib/ydim/invoice.rb', line 61 def debitor_id @debitor.unique_id if(@debitor) end |
#debitor_name ⇒ Object
64 65 66 |
# File 'lib/ydim/invoice.rb', line 64 def debitor_name @debitor.name if(@debitor) end |
#due_date ⇒ Object
67 68 69 70 71 |
# File 'lib/ydim/invoice.rb', line 67 def due_date if(@date && !@payment_received) @date + @payment_period.to_i end end |
#empty? ⇒ Boolean
83 84 85 |
# File 'lib/ydim/invoice.rb', line 83 def empty? @items.empty? end |
#invoice_key ⇒ Object
41 42 43 |
# File 'lib/ydim/invoice.rb', line 41 def invoice_key :invoice end |
#item(index) ⇒ Object
89 90 91 |
# File 'lib/ydim/invoice.rb', line 89 def item(index) @items.find { |item| item.index == index } end |
#pdf_invoice(sort_args = {}) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ydim/invoice.rb', line 104 def pdf_invoice(sort_args={}) config = PdfInvoice.config.dup config.formats['quantity'] = "%1.#{@precision}f" config.formats['total'] = "#{@currency} %1.2f" if(item = @items[0]) config.texts['tax'] = sprintf("MwSt %0.1f%%", item.vat_rate) end invoice = PdfInvoice::Invoice.new(config) invoice.date = @date invoice.invoice_number = @unique_id invoice.description = @description invoice.debitor_address = @debitor.address items = @items if sort_by = sort_args[:sortby] begin items = items.sort_by do |item| item.send(sort_by) end if sort_args[:sort_reverse] items.reverse! end rescue StandardError end end invoice.items = items.collect { |item| [ item.time, item.text, item.unit, item.quantity.to_f, item.price.to_f, item.vat.to_f ] } invoice end |
#status ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ydim/invoice.rb', line 92 def status if(@deleted) 'is_trash' elsif(@payment_received) 'is_paid' elsif(@date && @payment_period \ && ((@date + @payment_period) < Date.today)) 'is_due' else 'is_open' end end |
#to_pdf(sort_args = {}) ⇒ Object
139 140 141 |
# File 'lib/ydim/invoice.rb', line 139 def to_pdf(sort_args={}) pdf_invoice(sort_args).to_pdf end |