feat(pos): rewire cashup.html to /api/v1/pos/sales

Method split now reads pos_sales.payment_method directly. The four
method rows cashup already renders (cash/card/eft/account) would have
been permanently R0 for eft and account under a cash/card-only backend.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Dave 2026-08-31 01:46:26 +02:00
parent fac417a697
commit 58f06175e1

View file

@ -246,7 +246,6 @@ button:focus-visible,input:focus-visible,select:focus-visible{outline:0;box-shad
<script>
(function(){
'use strict';
const cfg = window.PB_CONFIG;
const fmt = n => 'R ' + (Number(n)||0).toLocaleString('en-ZA',{minimumFractionDigits:2,maximumFractionDigits:2});
const esc = s => String(s||'').replace(/[&<>"']/g, c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[c]);
@ -347,21 +346,20 @@ async function load(){
document.getElementById('kCount').textContent = '—';
document.getElementById('kTotal').textContent = 'R 0';
try{
await window.pbAuth();
const r = await window.pbFetch(`/api/collections/${cfg.collections.orders}/records?perPage=500&filter=${encodeURIComponent(`created >= '${start}' && created <= '${end}'`)}`);
const r = await window.posFetch(`/sales?from=${encodeURIComponent(start)}&to=${encodeURIComponent(end)}`);
if(!r.ok) throw new Error('sales returned ' + r.status);
const j = await r.json();
totals = {cash:0, card:0, eft:0, account:0, count: j.items.length};
const sales = j.sales || [];
totals = {cash:0, card:0, eft:0, account:0, count: sales.length};
const counts = {cash:0, card:0, eft:0, account:0};
for(const o of j.items){
const li = await window.pbFetch(`/api/collections/${cfg.collections.orderItems}/records?perPage=200&filter=order='${o.id}'`);
const liJ = await li.json();
const total = (liJ.items||[]).reduce((s,it)=> s + (it.selling_price||0)*(it.quantity||0), 0);
const isAccount = /ACCOUNT/.test(o.notes||'');
const method = isAccount ? 'account' : (/EFT/.test(o.notes||'')?'eft':(/CARD/.test(o.notes||'')?'card':'cash'));
totals[method] += total;
for(const s of sales){
const method = totals[s.payment_method] !== undefined ? s.payment_method : 'cash';
totals[method] += Number(s.total)||0;
counts[method]++;
}
grand = totals.cash + totals.card + totals.eft + totals.account;
// Only cash sales put money in the drawer. Change is already netted
// out: for a cash sale, cash_given - change_given === total.
cashSales = totals.cash;
document.getElementById('kCount').textContent = totals.count;
document.getElementById('kTotal').textContent = fmt(grand);