Module: BBMB::Util::Invoicer
- Defined in:
- lib/bbmb/util/invoicer.rb
Class Method Summary collapse
- .create_invoice(time_range, owed, orders, date, currency = 'CHF') ⇒ Object
- .fiscal_year(range) ⇒ Object
- .number_format(string) ⇒ Object
- .orders(range) ⇒ Object
- .run(range, date = Date.today) ⇒ Object
- .send_invoice(invoice_id) ⇒ Object
- .ydim_connect(&block) ⇒ Object
Class Method Details
.create_invoice(time_range, owed, orders, date, currency = 'CHF') ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bbmb/util/invoicer.rb', line 38 def create_invoice(time_range, owed, orders, date, currency='CHF') time = Time.now baseline = Util::Money.new(BBMB.config.invoice_monthly_baseline) baseamount = Util::Money.new(BBMB.config.invoice_monthly_baseamount) ydim_connect { |client| ydim_inv = client.create_invoice(BBMB.config.ydim_id) ydim_inv.description = sprintf(BBMB.config.invoice_format, time_range.first.strftime("%d.%m.%Y"), (time_range.last - 1).strftime("%d.%m.%Y")) ydim_inv.date = date ydim_inv.currency = currency ydim_inv.payment_period = 30 items = [] item_format = BBMB.config.invoice_item_format item_args = [orders.size] time = Time.local(date.year, date.month, date.day) if baseamount > 0 item_format = BBMB.config.invoice_item_overrun_format basepart = [baseline, owed].min text = sprintf(BBMB.config.invoice_item_baseline_format, basepart, owed, *item_args) item_data = { :price => baseamount.to_f, :quantity => 1, :text => number_format(text), :time => time, :unit => BBMB.config.invoice_item_baseamount_unit, } item_args.unshift owed items.push item_data end if owed > baseline owed -= baseline text = sprintf(item_format, owed, *item_args) item_data = { :price => owed.to_f * BBMB.config.invoice_percentage / 100, :quantity => 1, :text => number_format(text), :time => Time.local(date.year, date.month, date.day), :unit => "%0.1f%" % BBMB.config.invoice_percentage, } items.push item_data end client.add_items(ydim_inv.unique_id, items) ydim_inv } end |
.fiscal_year(range) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bbmb/util/invoicer.rb', line 85 def fiscal_year(range) day, month, = BBMB.config.invoice_newyear.split('.', 3) time = range.first year = time.year first = Time.local(year, month, day) if(first > time) first = Time.local(year - 1, month, day) end first...time end |
.number_format(string) ⇒ Object
95 96 97 98 99 |
# File 'lib/bbmb/util/invoicer.rb', line 95 def number_format(string) string.reverse.gsub(/\d{3}(?=\d)(?!\d*\.)/) do |match| match << "'" end.reverse end |
.orders(range) ⇒ Object
100 101 102 103 104 |
# File 'lib/bbmb/util/invoicer.rb', line 100 def orders(range) BBMB.persistence.all(Model::Order).select { |order| order.commit_time && range.include?(order.commit_time) } end |
.run(range, date = Date.today) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/bbmb/util/invoicer.rb', line 12 def run(range, date=Date.today) orders = orders(range) turnover = deduction = Util::Money.new(0) baseline = Util::Money.new(BBMB.config.invoice_baseline) total = orders.inject(turnover) { |memo, order| order.total + memo } if(total == 0 || baseline > 0) all_orders = orders(fiscal_year(range)) turnover = all_orders.inject(turnover) { |memo, order| order.total + memo } deduction = [deduction, baseline - turnover].max end owed = total - deduction if(owed > 0) invoice = create_invoice(range, owed, orders, date) send_invoice(invoice.unique_id) else body = sprintf(<<-EOS, baseline, turnover + total, total, deduction - total) Baseline: %10.2f Turnover: %10.2f Current Month: %10.2f ------------------------- Outstanding: %10.2f EOS Util::Mail.notify_debug("No invoice necessary", body) end end |
.send_invoice(invoice_id) ⇒ Object
105 106 107 |
# File 'lib/bbmb/util/invoicer.rb', line 105 def send_invoice(invoice_id) ydim_connect { |client| client.send_invoice(invoice_id) } end |
.ydim_connect(&block) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/bbmb/util/invoicer.rb', line 108 def ydim_connect(&block) config = YDIM::Client::CONFIG if(path = BBMB.config.ydim_config) config.load(path) end server = DRbObject.new(nil, config.server_url) client = YDIM::Client.new(config) key = OpenSSL::PKey::DSA.new(File.read(config.private_key)) client.login(server, key) block.call(client) ensure client.logout if(client) end |