Class: YDIM::RootSession

Inherits:
Object
  • Object
show all
Defined in:
lib/ydim/root_session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ RootSession

Returns a new instance of RootSession.



15
16
17
# File 'lib/ydim/root_session.rb', line 15

def initialize(user)
	@user = user
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



14
15
16
# File 'lib/ydim/root_session.rb', line 14

def client
  @client
end

#servObject

Returns the value of attribute serv.



14
15
16
# File 'lib/ydim/root_session.rb', line 14

def serv
  @serv
end

Instance Method Details

#add_items(invoice_id, items, invoice_key = :invoice) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ydim/root_session.rb', line 18

def add_items(invoice_id, items, invoice_key=:invoice)
	@serv.logger.debug(whoami) { 
		size = (items.respond_to?(:size)) ? items.size : nil
		"add_items(#{invoice_id}, #{items.class}[#{size}], #{invoice_key})" }
	invoice = self.send(invoice_key, invoice_id)
    rate = invoice.suppress_vat ? 0 : @serv.config.vat_rate
	items.each { |data|
		item = Item.new({:vat_rate => rate}.update(data))
		invoice.add_item(item)
	}
	invoice.odba_store
	invoice.items
end

#autoinvoice(invoice_id) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/ydim/root_session.rb', line 31

def autoinvoice(invoice_id)
  @serv.logger.debug(whoami) { "autoinvoice #{invoice_id}" }
  AutoInvoice.find_by_unique_id(invoice_id.to_s) \
  or begin
    msg = "invalid invoice_id: #{invoice_id}"
    @serv.logger.error(whoami) { msg }
    raise IndexError, msg
  end
end

#autoinvoicesObject



91
92
93
94
# File 'lib/ydim/root_session.rb', line 91

def autoinvoices
  @serv.logger.debug(whoami) { "autoinvoices" } if @serv &&  @serv.respond_to?(:logger)
  AutoInvoice.odba_extent
end

#collect_garbage(debitor_id = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ydim/root_session.rb', line 40

def collect_garbage(debitor_id=nil)
	@serv.logger.info(whoami) { "collect_garbage" }
	deleted = []
    Invoice.odba_extent { |inv|
		if([nil, inv.debitor_id].include?(debitor_id) && inv.deleted)
			deleted.push(inv.info)
			inv.odba_delete
		end
	}
	deleted unless(deleted.empty?)
end

#create_autoinvoice(debitor_id) ⇒ Object



51
52
53
54
55
56
# File 'lib/ydim/root_session.rb', line 51

def create_autoinvoice(debitor_id)
  @serv.logger.debug(whoami) { "create_autoinvoice(#{debitor_id})" }
  ODBA.transaction {
    @serv.factory.create_autoinvoice(debitor(debitor_id))
  }
end

#create_debitorObject



57
58
59
60
61
62
63
# File 'lib/ydim/root_session.rb', line 57

def create_debitor
	@serv.logger.info(whoami) { "create_debitor" }
	ODBA.transaction {
		id = @serv.id_server.next_id(:debitor)
		Debitor.new(id).odba_store
	}
end

#create_invoice(debitor_id) ⇒ Object



64
65
66
67
68
69
# File 'lib/ydim/root_session.rb', line 64

def create_invoice(debitor_id)
	@serv.logger.debug(whoami) { "create_invoice(#{debitor_id})" }
	ODBA.transaction {
		@serv.factory.create_invoice(debitor(debitor_id))
	}
end

#currency_converterObject



70
71
72
73
# File 'lib/ydim/root_session.rb', line 70

def currency_converter
	@serv.logger.debug(whoami) { "currency_converter" }
	@serv.currency_converter.drb_dup
end

#debitor(debitor_id) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/ydim/root_session.rb', line 74

def debitor(debitor_id)
	@serv.logger.debug(whoami) { "debitor #{debitor_id}" }
    Debitor.find_by_unique_id(debitor_id.to_s)\
    or begin
      msg = "invalid debitor_id: #{debitor_id}"
      @serv.logger.error(whoami) { msg }
      raise IndexError, msg
    end
end

#debitorsObject



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

def debitors
  @serv.logger.debug(whoami) { "debitors" } if @serv &&  @serv.respond_to?(:logger)
  Debitor.odba_extent
end

#delete_autoinvoice(invoice_id) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/ydim/root_session.rb', line 95

def delete_autoinvoice(invoice_id)
			@serv.logger.debug(whoami) { 
"delete_autoinvoice(#{invoice_id})" }
  if(invoice = autoinvoice(invoice_id))
    invoice.odba_delete
  end
end

#delete_item(invoice_id, index, invoice_key = :invoice) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/ydim/root_session.rb', line 102

def delete_item(invoice_id, index, invoice_key=:invoice)
	@serv.logger.debug(whoami) { 
      "delete_item(#{invoice_id}, #{index}, #{invoice_key})" }
	invoice = self.send(invoice_key, invoice_id)
	invoice.items.delete_if { |item| item.index == index }
	invoice.odba_store
	invoice.items
end

#generate_invoice(invoice_id) ⇒ Object



110
111
112
113
114
# File 'lib/ydim/root_session.rb', line 110

def generate_invoice(invoice_id)
	@serv.logger.info(whoami) { "generate_invoice(#{invoice_id})" }
    invoice = autoinvoice(invoice_id)
	AutoInvoicer.new(@serv).generate(invoice)
end

#invoice(invoice_id) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/ydim/root_session.rb', line 115

def invoice(invoice_id)
  @serv.logger.debug(whoami) { "invoice #{invoice_id}" }
  Invoice.find_by_unique_id(invoice_id.to_s) \
  or begin
    msg = "invalid invoice_id: #{invoice_id}"
    @serv.logger.error(whoami) { msg }
    raise IndexError, msg
  end
end

#invoice_infos(status = nil) ⇒ Object



124
125
126
127
# File 'lib/ydim/root_session.rb', line 124

def invoice_infos(status=nil)
	@serv.logger.debug(whoami) { "invoice_infos(#{status})" }
	Invoice.search_by_status(status).collect { |inv| inv.info }
end

#invoicesObject



87
88
89
90
# File 'lib/ydim/root_session.rb', line 87

def invoices
  @serv.logger.debug(whoami) { "invoices" } if @serv &&  @serv.respond_to?(:logger)
  Invoice.odba_extent
end

#search_debitors(email_or_name) ⇒ Object



128
129
130
131
132
# File 'lib/ydim/root_session.rb', line 128

def search_debitors(email_or_name)
	@serv.logger.debug(whoami) { "search_debitors(#{email_or_name})" }
	Debitor.search_by_exact_email(email_or_name) |
		Debitor.search_by_exact_name(email_or_name)
end

#send_invoice(invoice_id, sort_args = {}) ⇒ Object



133
134
135
136
# File 'lib/ydim/root_session.rb', line 133

def send_invoice(invoice_id, sort_args={})
	@serv.logger.info(whoami) { "send_invoice(#{invoice_id})" }
	Mail.send_invoice(@serv.config, invoice(invoice_id), sort_args)
end

#update_item(invoice_id, index, data, invoice_key = :invoice) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/ydim/root_session.rb', line 137

def update_item(invoice_id, index, data, invoice_key=:invoice)
	@serv.logger.debug(whoami) { 
		"update_item(#{invoice_id}, #{index}, #{data.inspect})" }
	invoice = self.send(invoice_key, invoice_id)
	item = invoice.item(index)
	item.update(data)
	invoice.odba_store
	item
end

#whoamiObject



146
147
148
# File 'lib/ydim/root_session.rb', line 146

def whoami
	@user.unique_id.to_s
end