/
%invoice_id.spt
121 lines (103 loc) · 3.74 KB
/
%invoice_id.spt
1
2
3
4
5
6
7
8
9
10
11
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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from liberapay.exceptions import LoginRequired
from liberapay.models.participant import Participant
from liberapay.utils import get_participant, markdown
[---]
i_id = request.path['invoice_id'].split('.html')[0]
try:
i_id = int(i_id)
except ValueError:
raise response.error(404)
participant = get_participant(state, restrict=False)
invoice = website.db.one("""
SELECT *
FROM invoices
WHERE id = %(i_id)s
AND (sender = %(p_id)s OR addressee = %(p_id)s)
AND status <> 'canceled'
""", dict(i_id=i_id, p_id=participant.id))
if invoice is None:
raise response.error(404)
if participant.id == invoice.sender:
addressee = user if user.id == invoice.addressee else Participant.from_id(invoice.addressee)
sender = participant
else:
addressee = participant
sender = user if user.id == invoice.sender else Participant.from_id(invoice.sender)
if invoice.status == 'pre' and user != sender and not user.is_admin:
raise response.error(404)
if request.method == 'POST':
action = request.body['action']
if action == 'send':
new_status = 'new'
elif action == 'cancel':
new_status = 'canceled'
else:
response.error(400, "bad `action` value")
if user.id != invoice.sender:
raise response.error(403)
r = sender.update_invoice_status(invoice.id, new_status)
if not r:
raise response.error(409, "This invoice has already been submitted or canceled.")
if action == 'send':
addressee.notify(
'new_invoice',
invoice=invoice,
sender_name=sender.username,
force_email=True,
)
response.redirect(request.line.uri)
invoice_events = website.db.all("""
SELECT *
FROM invoice_events
WHERE invoice = %s
ORDER BY ts DESC
""", (i_id,))
title = _("Invoice #{0}", i_id)
[---] text/html
% extends "templates/base.html"
{% block heading %}{% endblock %}
% block content
% if invoice.status == 'pre'
<p class="alert alert-info">{{ _("The invoice is ready, please check that everything is correct, then click the button below to send it.") }}</p>
% endif
<h1 class="text-center">{{ _(constants.INVOICE_NATURES[invoice.nature]) }}</h1>
<p>{{ _("From: {0}", sender.username) }}</p>
<p>{{ _("To: {0}", addressee.username) }}</p>
<p>{{ _("Date: {0}", invoice.ctime.date()) }}</p>
<p>{{ _("Amount: {0}", Money(invoice.amount, 'EUR')) }}</p>
<p>{{ _("Description: {0}", invoice.description) }}</p>
% if invoice.details
<p>{{ _("Details:") }}</p>
<section class="invoice-details">{{ markdown.render(invoice.details) }}</section>
% endif
% if invoice.documents
<p>{{ _("Documents:") }}</p>
<ul>
% for filename, attrs in invoice.documents.items()
<li>{{ filename.replace('%20', ' ') }}</li>
% endfor
</ul>
% endif
% if invoice.status == 'pre'
<br>
<form action="" method="POST">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}" />
<button class="btn btn-success btn-lg" name="action" value="send">{{ _("Send") }}</button>
<a class="btn btn-primary btn-lg" href="{{ participant.path('invoices/new?id=%s' % invoice.id) }}">{{ _("Modify") }}</a>
<button class="btn btn-danger btn-lg" name="action" value="cancel">{{ _("Delete") }}</button>
</form>
% elif invoice_events
<br>
<h3>{{ _("History") }}</h3>
% for e in invoice_events
<p>{{ format_datetime(e.ts) }} —
{{ _(constants.INVOICE_STATUSES[invoice.status]) }}
{% if e.message %} — {{ _("Message: {0}", e.message) }}{% endif %}</p>
% endfor
<p>{{ format_datetime(invoice.ctime) }} — {{ _("Created") }}</p>
% else
<p>{{ _("Status: {0}", _(constants.INVOICE_STATUSES[invoice.status])) }}</p>
% endif
% endblock