ploxion-finances/app.blade.php

55 lines
11 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{{-- PL0XI0N finances 💰 — L'ORGANISATION COMPLÈTE DES FINANCES de la zone (RS-7 11.09). Sur
jOSLedger (cat 'argent') : comptes (solde), flux récurrents (loyer, charges, assurance,
électricité, internet, abonnements, salaire) → échéances « à payer avant », budget net mensuel,
runway, mouvements (dépense/revenu en 2 taps), pack « appart suisse ». Bus ploxion.finances.cmd. --}}
<script>
(function () {
var ID = 'finances';
var PACK = [
{ nom: 'Loyer', montant: -0, periode: 'mois', jour: 1 }, { nom: 'Charges (chauffage/eau)', montant: -0, periode: 'mois', jour: 1 },
{ nom: 'Assurance ménage + RC', montant: -0, periode: 'mois', jour: 5 }, { nom: 'Électricité', montant: -0, periode: 'mois', jour: 15 },
{ nom: 'Internet', montant: -0, periode: 'mois', jour: 10 }, { nom: 'Téléphone', montant: -0, periode: 'mois', jour: 10 },
{ nom: 'Transports', montant: -0, periode: 'mois', jour: 1 }, { nom: 'Courses (budget)', montant: -0, periode: 'semaine' },
{ nom: 'Salaire / revenus', montant: 0, periode: 'mois', jour: 25 }
];
function mount(container, ctx) {
var C = window.jOSCiv, U = C.ui; U.style(); container.classList.add('cv'); container.style.position = 'relative';
var Z = (ctx && ctx.zone && jOSZone.get(ctx.zone.id)) || jOSZone.current(); if (!Z) { container.innerHTML = '<div class="cv__empty">aucune zone — ouvre 🫧 zone</div>'; return { unmount: function () { container.innerHTML = ''; } }; }
var L = C.ledger(Z), tab = 'apercu', offS = null;
function comptes() { return L.cat('argent'); }
function nb(n, unit) { return '<span class="nb ' + (n < 0 ? 'neg' : n > 0 ? 'pos' : '') + '">' + U.chf(n) + (unit ? ' ' + unit : '') + '</span>'; }
function render() {
var cs = comptes(), solde = L.total('argent'), netM = cs.reduce(function (s, c) { return s + L.net(c.id, 'mois'); }, 0), rw = netM < 0 ? solde / (-netM / 30.44) : Infinity;
var h = U.zoneHd(Z, 'Finances', '💰') + '<div class="cv__tabs">' + [['apercu', 'aperçu'], ['echeances', 'échéances'], ['flux', 'flux récurrents'], ['mouv', 'mouvements']].map(function (x) { return '<button data-tab="' + x[0] + '" class="' + (tab === x[0] ? 'on' : '') + '">' + x[1] + '</button>'; }).join('') + '</div><div class="cv__bd">';
if (!cs.length) { h += '<div class="cv__empty">Aucun compte. Commence par ton compte principal (le solde d\'aujourd\'hui), puis le pack « appart suisse » pose loyer, charges, assurance, électricité, internet, salaire — tu mets les montants.</div><div style="text-align:center"><button class="cv__btn ok" data-compte>+ mon compte</button></div>'; }
else if (tab === 'apercu') {
h += '<div class="cv__jg"><div><small>solde total</small><b>' + U.chf(solde) + ' CHF</b></div><div><small>net / mois</small><b>' + U.chf(netM) + '</b></div><div><small>runway</small><b>' + (rw === Infinity ? '∞' : Math.round(rw) + ' j') + '</b></div><div><small>7 prochains jours</small><b>' + U.chf(L.echeances(7).reduce(function (s, e) { return s + e.montant; }, 0)) + '</b></div></div>';
h += '<div class="cv__sec"><span>comptes</span><button class="cv__btn sm" data-compte>+</button></div>' + cs.map(function (c) { return '<div class="cv__row" data-compte-id="' + c.id + '"><span class="ic">🏦</span><div class="tx"><b>' + U.esc(c.nom) + '</b><small>' + (c.flux || []).length + ' flux · net ' + U.chf(L.net(c.id, 'mois')) + '/mois</small></div>' + nb(c.qte, c.unite) + '</div>'; }).join('');
var e7 = L.echeances(14); h += '<div class="cv__sec"><span>à régler (14 j)</span><span>' + e7.length + '</span></div>' + (e7.map(function (e) { return '<div class="cv__row ' + (e.dans <= 1 ? 'u3' : e.dans <= 3 ? 'u2' : '') + '"><span class="ic">🧾</span><div class="tx"><b>' + U.esc(e.flux.nom) + '</b><small>' + U.esc(U.dans(e.date) + ' · ' + U.dateFr(e.date) + ' · ' + e.res.nom) + '</small></div>' + nb(e.montant) + '<button class="cv__btn sm ok" data-regle="' + e.res.id + '" data-fid="' + e.flux.id + '">✓ réglé</button></div>'; }).join('') || '<div class="cv__empty">rien à régler sous 14 jours</div>');
if (cs.every(function (c) { return !(c.flux || []).length; })) h += '<div style="text-align:center;margin-top:10px"><button class="cv__btn" data-pack>🇨🇭 pack « appart suisse » (loyer, charges, assurance, élec, internet, salaire…)</button></div>';
} else if (tab === 'echeances') { var e30 = L.echeances(60); h += e30.map(function (e) { return '<div class="cv__row ' + (e.dans <= 1 ? 'u3' : e.dans <= 3 ? 'u2' : '') + '"><span class="ic">' + (e.montant >= 0 ? '💵' : '🧾') + '</span><div class="tx"><b>' + U.esc(e.flux.nom) + '</b><small>' + U.esc(U.dateFr(e.date) + ' · ' + U.dans(e.date) + ' · ' + e.res.nom) + '</small></div>' + nb(e.montant) + '<button class="cv__btn sm ok" data-regle="' + e.res.id + '" data-fid="' + e.flux.id + '">✓</button></div>'; }).join('') || '<div class="cv__empty">aucune échéance sous 60 jours</div>'; }
else if (tab === 'flux') { h += cs.map(function (c) { return '<div class="cv__sec"><span>' + U.esc(c.nom) + '</span><button class="cv__btn sm" data-flux-add="' + c.id + '">+ flux</button></div>' + ((c.flux || []).map(function (f) { return '<div class="cv__row" data-flux-edit="' + c.id + '" data-fid="' + f.id + '"><span class="ic">' + (f.montant >= 0 ? '💵' : '🔁') + '</span><div class="tx"><b>' + U.esc(f.nom) + '</b><small>' + U.esc(f.periode + (f.jour ? ' · le ' + f.jour : '') + (f.prochaine ? ' · prochaine ' + U.dateFr(f.prochaine) : '')) + '</small></div>' + nb(f.montant) + '</div>'; }).join('') || '<div class="cv__empty">aucun flux</div>'); }).join(''); }
else { var mv = []; cs.forEach(function (c) { (c.mouvements || []).forEach(function (m) { mv.push(Object.assign({ compte: c.nom }, m)); }); }); mv.sort(function (a, b) { return b.t - a.t; }); h += mv.slice(0, 100).map(function (m) { return '<div class="cv__row"><span class="ic">' + (m.delta >= 0 ? '💵' : '💸') + '</span><div class="tx"><b>' + U.esc(m.note || (m.delta >= 0 ? 'revenu' : 'dépense')) + '</b><small>' + U.esc(new Date(m.t).toLocaleString('fr-CH', { day: '2-digit', month: '2-digit', hour: '2-digit', minute: '2-digit' }) + ' · ' + m.compte) + '</small></div>' + nb(m.delta) + '</div>'; }).join('') || '<div class="cv__empty">aucun mouvement</div>'; }
h += '</div>' + (cs.length ? '<button class="cv__fab" data-mouv title="dépense / revenu">+</button>' : ''); container.innerHTML = h;
}
function formCompte(c) { U.form(container, c ? 'Compte' : 'Nouveau compte', [{ k: 'nom', label: 'nom', val: c ? c.nom : 'Compte principal' }, { k: 'qte', label: 'solde aujourd\'hui (CHF)', type: 'number', step: '0.05', val: c ? c.qte : '' }, { k: 'seuil', label: 'alerte sous (CHF)', type: 'number', val: c ? c.seuil : 500 }], function (v) { if (c) L.set(c.id, { nom: v.nom, qte: v.qte || 0, seuil: v.seuil || 0 }); else L.add({ nom: v.nom || 'Compte', cat: 'argent', unite: 'CHF', qte: v.qte || 0, seuil: v.seuil || 0 }); render(); }, c ? '<div style="margin-top:8px"><button type="button" class="cv__btn" data-del>🗑 supprimer le compte</button></div>' : ''); var d = container.querySelector('[data-del]'); if (d && c) d.addEventListener('click', function () { L.remove(c.id); container.querySelector('.cv__form').remove(); render(); }); }
function formFlux(cid, f) { var cs = comptes(); U.form(container, f ? 'Flux' : 'Nouveau flux récurrent', [{ k: 'nom', label: 'nom', val: f ? f.nom : '', ph: 'loyer, salaire, Netflix…' }, { k: 'montant', label: 'montant (− dépense, + revenu)', type: 'number', step: '0.05', val: f ? f.montant : '' }, { k: 'periode', label: 'période', type: 'chips', opts: [{ v: 'mois', l: 'mois' }, { v: 'semaine', l: 'semaine' }, { v: 'an', l: 'an' }, { v: 'jour', l: 'jour' }, { v: 'unique', l: 'une fois' }], val: f ? f.periode : 'mois' }, { k: 'jour', label: 'jour du mois (1-28)', type: 'number', val: f ? f.jour || '' : '' }, { k: 'prochaine', label: 'prochaine échéance', type: 'date', val: f ? f.prochaine || '' : '' }, { k: 'compte', label: 'compte', type: 'chips', opts: cs.map(function (c) { return { v: c.id, l: c.nom }; }), val: cid }], function (v) { var target = v.compte || cid; var patch = { nom: v.nom || '?', montant: v.montant || 0, periode: v.periode || 'mois', jour: v.jour || null, prochaine: v.prochaine || null }; if (f) { if (target !== cid) { L.fluxRemove(cid, f.id); L.fluxAdd(target, patch); } else L.fluxSet(cid, f.id, patch); } else L.fluxAdd(target, patch); render(); }, f ? '<div style="margin-top:8px"><button type="button" class="cv__btn" data-del>🗑 supprimer</button></div>' : ''); var d = container.querySelector('[data-del]'); if (d && f) d.addEventListener('click', function () { L.fluxRemove(cid, f.id); container.querySelector('.cv__form').remove(); render(); }); }
function formMouv() { var cs = comptes(); U.form(container, 'Dépense / revenu', [{ k: 'delta', label: 'montant (− dépense, + revenu)', type: 'number', step: '0.05' }, { k: 'note', label: 'quoi', ph: 'courses Migros, resto, remboursement…' }, { k: 'compte', label: 'compte', type: 'chips', opts: cs.map(function (c) { return { v: c.id, l: c.nom }; }), val: cs[0].id }], function (v) { if (v.delta) L.mouv(v.compte || cs[0].id, v.delta, v.note); render(); }); }
container.addEventListener('click', function (e) {
var b; if ((b = e.target.closest('[data-tab]'))) { tab = b.getAttribute('data-tab'); render(); return; }
if (e.target.closest('[data-compte]')) { formCompte(null); return; }
if ((b = e.target.closest('[data-compte-id]')) && !e.target.closest('button')) { formCompte(L.get(b.getAttribute('data-compte-id'))); return; }
if ((b = e.target.closest('[data-regle]'))) { L.fluxFait(b.getAttribute('data-regle'), b.getAttribute('data-fid')); render(); return; }
if ((b = e.target.closest('[data-flux-add]'))) { formFlux(b.getAttribute('data-flux-add'), null); return; }
if ((b = e.target.closest('[data-flux-edit]'))) { var c = L.get(b.getAttribute('data-flux-edit')); formFlux(c.id, (c.flux || []).filter(function (f) { return f.id === b.getAttribute('data-fid'); })[0]); return; }
if (e.target.closest('[data-pack]')) { var c0 = comptes()[0]; PACK.forEach(function (p) { L.fluxAdd(c0.id, p); }); tab = 'flux'; render(); return; }
if (e.target.closest('[data-mouv]')) { formMouv(); return; }
});
render(); offS = L.on(function () { render(); });
return { unmount: function () { try { offS(); } catch (e) {} container.classList.remove('cv'); container.innerHTML = ''; } };
}
function describe() { var Z = window.jOSZone && jOSZone.current(); if (!Z || !window.jOSCiv) return null; var L = jOSCiv.ledger(Z), cs = L.cat('argent'), solde = L.total('argent'), netM = cs.reduce(function (s, c) { return s + L.net(c.id, 'mois'); }, 0), e = L.echeances(7); return { v: 1, id: ID, title: 'Finances', icon: '💰', items: [{ p: 1, kind: 'stat', label: 'solde', value: Math.round(solde), unit: 'CHF' }, { p: 2, kind: 'stat', label: 'net/mois', value: Math.round(netM), unit: 'CHF' }, { p: 2, kind: 'list', label: 'à régler (7 j)', items: e.slice(0, 5).map(function (x) { return { label: x.flux.nom + ' ' + Math.abs(x.montant), value: jOSCiv.ui.dans(x.date) }; }) }] }; }
window.jOSApps = window.jOSApps || {}; window.jOSApps[ID] = { manifest: { id: ID, name: 'Finances', icon: '💰' }, mount: mount, describe: describe };
})();
</script>