Class: YDIM::Invoice

Inherits:
Object
  • Object
show all
Includes:
ODBA::Persistable, ItemId
Defined in:
lib/ydim/odba.rb,
lib/ydim/invoice.rb

Direct Known Subclasses

AutoInvoice

Defined Under Namespace

Classes: Info

Constant Summary collapse

ODBA_SERIALIZABLE =
['@items']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ItemId

#next_item_id

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

#currencyObject

Returns the value of attribute currency.



25
26
27
# File 'lib/ydim/invoice.rb', line 25

def currency
  @currency
end

#dateObject

Returns the value of attribute date.



25
26
27
# File 'lib/ydim/invoice.rb', line 25

def date
  @date
end

#debitorObject

Returns the value of attribute debitor.



24
25
26
# File 'lib/ydim/invoice.rb', line 24

def debitor
  @debitor
end

#deletedObject

Returns the value of attribute deleted.



25
26
27
# File 'lib/ydim/invoice.rb', line 25

def deleted
  @deleted
end

#descriptionObject

Returns the value of attribute description.



25
26
27
# File 'lib/ydim/invoice.rb', line 25

def description
  @description
end

#itemsObject (readonly)

Returns the value of attribute items.



24
25
26
# File 'lib/ydim/invoice.rb', line 24

def items
  @items
end

#payment_periodObject

Returns the value of attribute payment_period.



25
26
27
# File 'lib/ydim/invoice.rb', line 25

def payment_period
  @payment_period
end

#payment_receivedObject

Returns the value of attribute payment_received.



25
26
27
# File 'lib/ydim/invoice.rb', line 25

def payment_received
  @payment_received
end

#precisionObject

Returns the value of attribute precision.



25
26
27
# File 'lib/ydim/invoice.rb', line 25

def precision
  @precision
end

#suppress_vatObject

Returns the value of attribute suppress_vat.



24
25
26
# File 'lib/ydim/invoice.rb', line 24

def suppress_vat
  @suppress_vat
end

#unique_idObject (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_emailObject



58
59
60
# File 'lib/ydim/invoice.rb', line 58

def debitor_email
	@debitor.email if(@debitor)
end

#debitor_idObject



61
62
63
# File 'lib/ydim/invoice.rb', line 61

def debitor_id
	@debitor.unique_id if(@debitor)
end

#debitor_nameObject



64
65
66
# File 'lib/ydim/invoice.rb', line 64

def debitor_name
	@debitor.name if(@debitor)
end

#due_dateObject



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

Returns:

  • (Boolean)


83
84
85
# File 'lib/ydim/invoice.rb', line 83

def empty?
  @items.empty?
end

#infoObject



86
87
88
# File 'lib/ydim/invoice.rb', line 86

def info
	Info.new(self)
end

#invoice_keyObject



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

#statusObject



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