Class: YDIM::RootSession
- Inherits:
-
Object
- Object
- YDIM::RootSession
- Defined in:
- lib/ydim/root_session.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#serv ⇒ Object
Returns the value of attribute serv.
Instance Method Summary collapse
- #add_items(invoice_id, items, invoice_key = :invoice) ⇒ Object
- #autoinvoice(invoice_id) ⇒ Object
- #autoinvoices ⇒ Object
- #collect_garbage(debitor_id = nil) ⇒ Object
- #create_autoinvoice(debitor_id) ⇒ Object
- #create_debitor ⇒ Object
- #create_invoice(debitor_id) ⇒ Object
- #currency_converter ⇒ Object
- #debitor(debitor_id) ⇒ Object
- #debitors ⇒ Object
- #delete_autoinvoice(invoice_id) ⇒ Object
- #delete_item(invoice_id, index, invoice_key = :invoice) ⇒ Object
- #generate_invoice(invoice_id) ⇒ Object
-
#initialize(user) ⇒ RootSession
constructor
A new instance of RootSession.
- #invoice(invoice_id) ⇒ Object
- #invoice_infos(status = nil) ⇒ Object
- #invoices ⇒ Object
-
#mark_paid(invoice_id, date = Date.today) ⇒ Object
Marks an invoice as paid.
-
#open_invoice_infos ⇒ Object
The invoices a payment could still be settling.
-
#reconcile_camt(entries, opts = {}) ⇒ Object
Matches booked bank credits against the invoice book.
- #search_debitors(email_or_name) ⇒ Object
- #send_invoice(invoice_id, sort_args = {}) ⇒ Object
- #update_item(invoice_id, index, data, invoice_key = :invoice) ⇒ Object
- #whoami ⇒ Object
Constructor Details
#initialize(user) ⇒ RootSession
Returns a new instance of RootSession.
16 17 18 |
# File 'lib/ydim/root_session.rb', line 16 def initialize(user) @user = user end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
15 16 17 |
# File 'lib/ydim/root_session.rb', line 15 def client @client end |
#serv ⇒ Object
Returns the value of attribute serv.
15 16 17 |
# File 'lib/ydim/root_session.rb', line 15 def serv @serv end |
Instance Method Details
#add_items(invoice_id, items, invoice_key = :invoice) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ydim/root_session.rb', line 19 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
32 33 34 35 36 37 38 39 40 |
# File 'lib/ydim/root_session.rb', line 32 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 |
#autoinvoices ⇒ Object
92 93 94 95 |
# File 'lib/ydim/root_session.rb', line 92 def autoinvoices @serv.logger.debug(whoami) { "autoinvoices" } if @serv && @serv.respond_to?(:logger) AutoInvoice.odba_extent end |
#collect_garbage(debitor_id = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ydim/root_session.rb', line 41 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
52 53 54 55 56 57 |
# File 'lib/ydim/root_session.rb', line 52 def create_autoinvoice(debitor_id) @serv.logger.debug(whoami) { "create_autoinvoice(#{debitor_id})" } ODBA.transaction { @serv.factory.create_autoinvoice(debitor(debitor_id)) } end |
#create_debitor ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/ydim/root_session.rb', line 58 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
65 66 67 68 69 70 |
# File 'lib/ydim/root_session.rb', line 65 def create_invoice(debitor_id) @serv.logger.debug(whoami) { "create_invoice(#{debitor_id})" } ODBA.transaction { @serv.factory.create_invoice(debitor(debitor_id)) } end |
#currency_converter ⇒ Object
71 72 73 74 |
# File 'lib/ydim/root_session.rb', line 71 def currency_converter @serv.logger.debug(whoami) { "currency_converter" } @serv.currency_converter.drb_dup end |
#debitor(debitor_id) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/ydim/root_session.rb', line 75 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 |
#debitors ⇒ Object
84 85 86 87 |
# File 'lib/ydim/root_session.rb', line 84 def debitors @serv.logger.debug(whoami) { "debitors" } if @serv && @serv.respond_to?(:logger) Debitor.odba_extent end |
#delete_autoinvoice(invoice_id) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/ydim/root_session.rb', line 96 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
103 104 105 106 107 108 109 110 |
# File 'lib/ydim/root_session.rb', line 103 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
111 112 113 114 115 |
# File 'lib/ydim/root_session.rb', line 111 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
116 117 118 119 120 121 122 123 124 |
# File 'lib/ydim/root_session.rb', line 116 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
125 126 127 128 |
# File 'lib/ydim/root_session.rb', line 125 def invoice_infos(status=nil) @serv.logger.debug(whoami) { "invoice_infos(#{status})" } Invoice.search_by_status(status).collect { |inv| inv.info } end |
#invoices ⇒ Object
88 89 90 91 |
# File 'lib/ydim/root_session.rb', line 88 def invoices @serv.logger.debug(whoami) { "invoices" } if @serv && @serv.respond_to?(:logger) Invoice.odba_extent end |
#mark_paid(invoice_id, date = Date.today) ⇒ Object
Marks an invoice as paid. Stores the date the money was booked rather than a plain true, so that a later reconciliation run can tell an already-booked payment from a fresh one.
140 141 142 143 144 145 146 147 148 |
# File 'lib/ydim/root_session.rb', line 140 def mark_paid(invoice_id, date=Date.today) @serv.logger.info(whoami) { "mark_paid(#{invoice_id}, #{date})" } ODBA.transaction { inv = invoice(invoice_id) inv.payment_received = date inv.odba_store inv.info } end |
#open_invoice_infos ⇒ Object
The invoices a payment could still be settling. Both statuses are asked for because the status index is only refreshed once a day, so an invoice that has just fallen due may still be indexed as open.
132 133 134 135 136 |
# File 'lib/ydim/root_session.rb', line 132 def open_invoice_infos @serv.logger.debug(whoami) { "open_invoice_infos" } (Invoice.search_by_status('is_open') \ | Invoice.search_by_status('is_due')).collect { |inv| inv.info } end |
#reconcile_camt(entries, opts = {}) ⇒ Object
Matches booked bank credits against the invoice book. entries are
YDIM::Camt::Entry objects, parsed client-side -- the daemon never needs
to see the statement files. Read-only unless :apply is given, in which
case only unambiguous matches (invoice number named and amount to the
cent) are booked; everything else is left for a human.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/ydim/root_session.rb', line 154 def reconcile_camt(entries, opts={}) @serv.logger.info(whoami) { "reconcile_camt(#{entries.size} entries, #{opts.inspect})" } accounts = opts[:accounts] || @serv.config.camt_accounts resolver = lambda { |id| inv = Invoice.find_by_unique_id(id.to_s) inv && inv.info } result = Reconciler.new(open_invoice_infos, accounts, resolver)\ .reconcile(entries) if opts[:apply] result.applicable.each { |match| match.invoices.each { |info| mark_paid(info.unique_id, match.entry.booking_date) } match.applied = true } end result end |
#search_debitors(email_or_name) ⇒ Object
174 175 176 177 178 |
# File 'lib/ydim/root_session.rb', line 174 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
179 180 181 182 |
# File 'lib/ydim/root_session.rb', line 179 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
183 184 185 186 187 188 189 190 191 |
# File 'lib/ydim/root_session.rb', line 183 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 |
#whoami ⇒ Object
192 193 194 |
# File 'lib/ydim/root_session.rb', line 192 def whoami @user.unique_id.to_s end |