Skip to content

Commit

Permalink
Datatable for transactions on session detail pages
Browse files Browse the repository at this point in the history
  • Loading branch information
sde1000 committed Jan 18, 2024
1 parent 230abb1 commit 68e9eb3
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 52 deletions.
75 changes: 75 additions & 0 deletions quicktill/tillweb/datatable.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,81 @@ def sessions(request, info):
})


@tillweb_view
def transactions(request, info):
columns = {
'id': Transaction.id,
'sessionid': Transaction.sessionid,
'total': Transaction.total,
'discount_total': Transaction.discount_total,
'notes': Transaction.notes,
'closed': Transaction.closed,
# There is a "discount policy" column that only applies to
# open transactions; it doesn't seem particularly useful to
# show it at the moment.
}

search_value = request.GET.get("search[value]")
q = td.s.query(Transaction)\
.join(Transaction.session, isouter=True)\
.options(undefer('total'),
undefer('discount_total'),
contains_eager(Transaction.session))

# Apply filters from parameters. The 'unfiltered' item count for
# this table is after this filtering step.
try:
sessionid = int(request.GET.get('sessionid'))
q = q.filter(Session.id == sessionid)
except (ValueError, TypeError):
pass

# Apply filters from search value and other parameters. The
# 'filtered' item count is after this filtering step.
fq = q
state = request.GET.get('state', 'any')
if state == "closed":
q = q.filter(Transaction.closed == True)
elif state == "open":
q = q.filter(Transaction.closed == False)
elif state == "deferred":
q = q.filter(Transaction.sessionid == None)
if search_value:
try:
intsearch = int(search_value)
except ValueError:
intsearch = None
try:
decsearch = Decimal(search_value)
except Exception:
decsearch = None
qs = [
columns['notes'].ilike(f'%{search_value}%'),
]
if intsearch:
qs.append(columns['id'] == intsearch)
qs.append(columns['sessionid'] == intsearch)
if decsearch is not None:
qs.append(columns['total'] == decsearch)
qs.append(columns['discount_total'] == decsearch)
fq = q.filter(or_(*qs))

return _datatables_json(
request, q, fq, columns, lambda t: {
'id': t.id,
'url': t.get_absolute_url(),
'sessionid': t.session.id,
'session_url': t.session.get_absolute_url(),
'session_date': t.session.date,
'total': t.total,
'discount_total': t.discount_total,
'notes': t.notes,
'closed': t.closed,
'DT_RowClass': "table-warning" if t.sessionid and not t.closed
else None,
})


@tillweb_view
def translines(request, info):
columns = {
Expand Down
31 changes: 0 additions & 31 deletions quicktill/tillweb/templates/tillweb/session-transactions.ajax

This file was deleted.

68 changes: 62 additions & 6 deletions quicktill/tillweb/templates/tillweb/session.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="transactionsTab" href="{% url "tillweb-session-transactions" pubname=pubname sessionid=session.id %}" data-toggle="tabajax" data-target="#transactions" role="tab" aria-controls="transactions" aria-selected="false">
<a class="nav-link" id="transactionsTab" href="#transactions" data-toggle="tab" data-target="#transactions" role="tab" aria-controls="transactions" aria-selected="false">
Transactions
</a>
</li>
Expand All @@ -90,9 +90,11 @@
</li>
</ul>

<div class="mb-1"></div>

<div class="tab-content">
<div class="tab-pane fade" id="deptTakings" aria-labelledby="deptTakingsTab">
<div class="row mt-2">
<div class="row">
<div class="col-lg-5 col">
<p class="d-none" id="session_open_warning">
This session is still open, and includes transactions that
Expand Down Expand Up @@ -130,7 +132,7 @@
</div>
</div>
<div class="tab-pane fade" id="userTakings" aria-labelledby="userTakingsTab">
<div class="row mt-2">
<div class="row">
<div class="col-md-5">
<table class="table table-striped table-sm" id="usertakings">
<thead class="thead-light">
Expand All @@ -153,9 +155,20 @@
<div class="tab-pane fade" id="stockSold" aria-labelledby="stockSoldTab">
</div>
<div class="tab-pane fade" id="transactions" aria-labelledby="transactionsTab">
<table class="table table-sm table-striped" id="transactionsTable">
<thead class="thead-light">
<tr>
<th scope="col">Transaction</th>
<th scope="col">Amount</th>
<th scope="col">Discount</th>
<th scope="col">Note</th>
<th scope="col">State</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<div class="tab-pane fade" id="translines" aria-labelledby="translinesTab">
<div class="mb-1"></div>
<table class="table table-sm table-striped" id="translinesTable">
<thead class="thead-light">
<tr>
Expand All @@ -177,7 +190,6 @@
</table>
</div>
<div class="tab-pane fade" id="payments" aria-labelledby="paymentsTab">
<div class="mb-1"></div>
<table class="table table-sm table-striped" id="paymentsTable">
<thead class="thead-light">
<tr>
Expand All @@ -196,7 +208,7 @@
</table>
</div>
<div class="tab-pane fade" id="info" aria-labelledby="infoTab">
<a class="btn btn-secondary mt-3 mb-3" href="{% url "tillweb-session-spreadsheet" pubname=pubname sessionid=session.id %}">Download spreadsheet</a>
<a class="btn btn-secondary mb-3" href="{% url "tillweb-session-spreadsheet" pubname=pubname sessionid=session.id %}">Download spreadsheet</a>
{% with logs=session.logs %}
{% include "tillweb/loglist.html" %}
{% endwith %}
Expand Down Expand Up @@ -244,6 +256,7 @@

let depttotals_table = null;
let usertotals_table = null;
let transactions_table = null;
let translines_table = null;
let payments_table = null;

Expand Down Expand Up @@ -377,6 +390,39 @@
});
};

function init_transactions_table() {
transactions_table = $("#transactionsTable").DataTable({
ajax: {
url: '{% url "tillweb-datatable-transactions" pubname=pubname %}',
dataSrc: 'data',
data: function (d) {
d.sessionid = {{session.id}};
},
},
columns: [
{ data: 'id',
render: render_link('url', DataTable.render.text()),
},
{ data: 'total',
render: render_money(),
},
{ data: 'discount_total',
render: render_money(),
},
{ data: 'notes',
render: DataTable.render.text(),
},
{ data: 'closed',
render: render_closed(),
},
],
order: [ [0, 'desc'] ],
searching: true,
paging: true,
serverSide: true,
});
};

function init_translines_table() {
translines_table = $("#translinesTable").DataTable({
ajax: {
Expand Down Expand Up @@ -520,6 +566,16 @@
}
});

$('#transactionsTab').on("click", function (e) {
if (transactions_table === null) {
init_transactions_table();
} else {
{% if not session.endtime %}
transactions_table.ajax.reload();
{% endif %}
}
});

$('#translinesTab').on("click", function (e) {
if (translines_table === null) {
init_translines_table();
Expand Down
9 changes: 9 additions & 0 deletions quicktill/tillweb/templates/tillweb/tillweb.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@
return "No";
};
};

function render_closed() {
return function (data, type, row) {
if (data) {
return "Closed";
}
return "Open";
};
};
</script>
{% endblock %}
{% block breadcrumbs %}
Expand Down
4 changes: 2 additions & 2 deletions quicktill/tillweb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
name="tillweb-session-discounts"),
path('stock-sold.html', views.session_stock_sold,
name="tillweb-session-stock-sold"),
path('transactions.html', views.session_transactions,
name="tillweb-session-transactions"),
path('dept<int:dept>/', views.sessiondept,
name="tillweb-session-department"),
])),
Expand Down Expand Up @@ -132,6 +130,8 @@
name="tillweb-datatable-sessions"),
path('datatable/sessiontotals.json', datatable.sessiontotals,
name="tillweb-datatable-sessiontotals"),
path('datatable/transactions.json', datatable.transactions,
name="tillweb-datatable-transactions"),
path('datatable/translines.json', datatable.translines,
name="tillweb-datatable-translines"),
path('datatable/payments.json', datatable.payments,
Expand Down
13 changes: 0 additions & 13 deletions quicktill/tillweb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,19 +696,6 @@ def session_stock_sold(request, info, sessionid):
return ('session-stock-sold.ajax', {'session': s})


@tillweb_view
def session_transactions(request, info, sessionid):
s = td.s.query(Session)\
.options(undefer('transactions.total'),
undefer('transactions.discount_total'),
joinedload('transactions.payments'))\
.get(sessionid)
if not s:
raise Http404

return ('session-transactions.ajax', {'session': s})


@tillweb_view
def sessiondept(request, info, sessionid, dept):
s = td.s.query(Session).get(sessionid)
Expand Down

0 comments on commit 68e9eb3

Please sign in to comment.