Class: YDIM::Html::State::Invoice

Inherits:
Global
  • Object
show all
Includes:
InvoiceKeys
Defined in:
lib/ydim/html/state/invoice.rb

Direct Known Subclasses

AutoInvoice

Defined Under Namespace

Classes: SortableInvoice

Constant Summary collapse

VIEW =
Html::View::Invoice

Constants inherited from Global

Global::EVENT_MAP

Instance Attribute Summary collapse

Attributes inherited from Global

#sort_reverse, #sortby

Instance Method Summary collapse

Methods included from InvoiceKeys

#invoice_key, #invoice_keys, #invoice_mandatory

Methods inherited from Global

#_create_invoice, #autoinvoice, #create_autoinvoice, #create_debitor, #create_invoice, #debitor, #invoice, #logout

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



80
81
82
# File 'lib/ydim/html/state/invoice.rb', line 80

def model
  @model
end

Instance Method Details

#_do_updateObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ydim/html/state/invoice.rb', line 142

def _do_update
	if((id = @session.user_input(:unique_id)) \
      && @model.unique_id == id.to_i)
		## update items
		keys = [:text, :quantity, :unit, :price]
		data = {}
		user_input(keys).each { |key, hash|
			hash.each { |idx, value|
				(data[idx] ||= {}).store(key, value)
			} unless hash.nil?
		}
		target = origin = nil
		converter = if((target = @session.user_input(:currency)) \
			 && (origin = @model.currency) \
			 && origin != target)
			@session.currency_converter
		end
		data.each { |idx, item|
			if(converter)
				item[:price] = converter.convert(item[:price], origin, target)
			end
			@session.update_item(id.to_i, idx.to_i, item, invoice_key)
		}

		_do_update_invoice(user_input(invoice_keys, invoice_mandatory))
     @model.update! ## @model is a SortableInvoice
		@model.odba_store
	end
end

#_do_update_invoice(input) ⇒ Object



171
172
173
174
175
176
# File 'lib/ydim/html/state/invoice.rb', line 171

def _do_update_invoice(input)
  input[:precision] = (input[:precision] || 2).to_i
  input.each { |key, val|
    @model.send("#{key}=", val)
  }
end

#ajax_create_itemObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/ydim/html/state/invoice.rb', line 85

def ajax_create_item
	if(id = @session.user_input(:unique_id))
		begin
			@session.add_items(id.to_i, [{:time => Time.now}], invoice_key)
		rescue IndexError
		end
     @model.update! ## @model is a SortableInvoice
	end
	AjaxItems.new(@session, @model.items)
end

#ajax_delete_itemObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ydim/html/state/invoice.rb', line 95

def ajax_delete_item
	if((id = @session.user_input(:unique_id)) \
		&& (idx = @session.user_input(:index)))
		begin
			@session.delete_item(id.to_i, idx.to_i, invoice_key)
		rescue IndexError
		end
     @model.update! ## @model is a SortableInvoice
	end
	AjaxItems.new(@session, @model.items)
end

#ajax_invoiceObject



125
126
127
128
# File 'lib/ydim/html/state/invoice.rb', line 125

def ajax_invoice
	_do_update
	AjaxInvoice.new(@session, @model)
end

#ajax_itemObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ydim/html/state/invoice.rb', line 106

def ajax_item
	data = {}
	if((id = @session.user_input(:unique_id)) \
		&& (idx = @session.user_input(:index)))
		begin
			keys = [:text, :quantity, :unit, :price]
			input = user_input(keys).delete_if { |key, val| val.nil?  }
			item = @session.update_item(id.to_i, idx.to_i, input, 
                                   invoice_key)
			input.each { |key, val| data.store("#{key}[#{item.index}]", val) }
			data.store("total_netto#{item.index}", item.total_netto)
		rescue IndexError
		end
	end
	[:total_netto, :vat, :total_brutto].each { |key|
		data.store(key, @model.send(key))
	}
	AjaxValues.new(@session, data)
end

#initObject



81
82
83
84
# File 'lib/ydim/html/state/invoice.rb', line 81

def init
  @model = SortableInvoice.new @model
  super
end

#pdfObject



129
130
131
132
133
# File 'lib/ydim/html/state/invoice.rb', line 129

def pdf
  newstate = super
  newstate.sortby, newstate.sort_reverse = @sortby, @sort_reverse
  newstate
end

#send_invoiceObject



134
135
136
137
# File 'lib/ydim/html/state/invoice.rb', line 134

def send_invoice
  _do_update
  super
end

#updateObject



138
139
140
141
# File 'lib/ydim/html/state/invoice.rb', line 138

def update
	_do_update
	self
end