Commit a6e87fc5 authored by o@immerda.ch's avatar o@immerda.ch
Browse files

allow users to create bills

parent a947b23e
This diff is collapsed.
class BillsController < ApplicationController
include ApiBackend
def to_creditor_reference(key)
str = ""
key.split("").each do |d|
d = d.downcase.chr
if d >= '0' && d <= '9'
str << d
elsif d >= 'a' && d <= 'z'
str << (d.bytes.first-'a'.bytes.first+10).to_s
else
throw d
throw :invalid_char
end
end
str2 = "#{str}2715"
rem = str2.to_i % 97
diff = (rem-1) % 97
"RF#{'%02d' % diff}#{key}"
end
def show
begin
@year = (params[:year] || DateTime.now.year).to_i
res = api.get_user_transactions('bill', ["#{@year}-01-01", "#{@year+1}-01-01"])
res.each do |r|
paid = api.get_global_transaction('bill', r.first[:reference])
if paid
r.first[:paid] = paid
end
r.first[:reference] = to_creditor_reference(r.first[:reference])
end
rescue ApiBackend::ApiError
flash[:danger] = :failed
redirect_to root_path
return
end
@bills = res
end
def create
ttl = 6*365
amount = params[:amount].to_f
reference = api.get_fresh_global_transaction_key('bill', 20, ttl)
reference = reference['key']
if reference.length == 20
api.add_user_transaction('bill',
{reference: reference, amount: amount, currency: 'CHF'}, ttl)
end
redirect_to action: :show
rescue ApiError => e
flash[:danger] = e.api_msg
redirect_to bills_path
end
end
......@@ -399,6 +399,35 @@ module ApiBackend
def release_reserved_identifier(kind, id)
post(['users_admin', 'release_reserved_identifier'], {release: {kind => id}})
end
def get_user_transactions(scope, timeframe = nil)
res = if timeframe
get(['user_transaction', 'get'], {scope: scope, from: timeframe.first, to: timeframe.second})
else
get(['user_transaction', 'get'], {scope: scope})
end
res['values'].map{|e| [YAML.load(e.first), DateTime.parse(e.second)] }
end
def add_user_transaction(scope, value, ttl)
post(['user_transaction', 'add'], {scope: scope, value: value.to_yaml, ttl: ttl})
end
def get_global_transaction(scope, key)
res = get(['global_transaction', 'get'], {scope: scope, key: key})
if res['notfound']
return nil
end
YAML.load(res['value'])
end
def get_fresh_global_transaction_key(scope, length, ttl)
post(['global_transaction', 'get_fresh_key'], {scope: scope, lenght: length, ttl: ttl})
end
def add_global_transaction(scope, key, value, ttl)
post(['global_transaction', 'add'], {scope: scope, key: key, value: value.to_yaml, ttl: ttl})
end
end
def api
......
......@@ -18,6 +18,7 @@
mail_crypt: {nudge: !mail_crypt_enabled?},
app_passwords: {},
pgpkeys: {},
bills: ({} if feature_toggle?('bills')),
invites: ({} if allow_invites?),
delete_account: {},
services: ({divider: true} if (any_resource_enabled? || any_resource_exists?)),
......
<%= form_tag do %>
<div class="form-group">
<%= label_tag "CHF" %>
<%= text_field_tag('amount', "", class: 'form-control') %>
</div>
<%= submit_tag t(:create), name: 'create', class: "btn btn-primary" %>
<% end %>
<hr />
<%= form_tag "", method: :get do %>
<div class="form-group">
<%= select_tag(:year,
options_for_select([['----',nil]]+(0..5).map{|d| DateTime.now.year - d },
@year || '----'),
onchange: 'submit()') %>
</div>
<% end %>
<% id = 0; sum = {} %>
<% for e in @bills %>
<% bill, date = e
id = id+1
if bill[:paid]
p = bill[:paid]
sum[p[:currency]] ||= 0.0
sum[p[:currency]] += (p[:amount] || 0)
end %>
<%= "#{date.day}.#{date.month}.#{date.year}" %>:
<%= bill[:currency] %>
<% if bill[:amount] > 0.0 %>
<%= bill[:amount] %>
<% end %>
<% if bill[:paid] %>
<b><%= bill[:paid][:status] %></b>
<% if bill[:paid][:amount] != bill[:amount] ||
bill[:paid][:currency] != bill[:currency] %>
(<%= bill[:paid][:currency] %> <%= bill[:paid][:amount] %>)
<% end %>
<% end %>
<a href="#" onclick="this.style.display = 'none'; qrbill.createQRBill(
'<%= bill[:currency].upcase %>',
<%= if bill[:amount] > 0.0 then bill[:amount] else 'undefined' end %>,
'<%= bill[:reference] %>',
function(url){
var f = document.getElementById('bill-<%= id %>');
f.src = url;
f.style.display = 'block';
});" />
show</a> <br />
<iframe id="bill-<%= id %>" style="width:100%; height: 450px; display: none;">
</iframe>
<% end %>
<% unless sum.empty? %>
<hr />
Thank you very much for donating a total of <%= sum.to_a.map{|e| e.join(" ")}.join(", ") %>
to immerda in the year <%= @year %>.
<% end %>
......@@ -69,6 +69,9 @@ Rails.application.routes.draw do
post '/resources/:kind', to: 'resources#update'
get '/new_resource', to: 'resources#create'
post '/new_resource', to: 'resources#do_create'
get '/bills', to: 'bills#show'
post '/bills', to: 'bills#create'
else
root to: 'admin#index'
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment