From 1431860f9f30e2d2d3f0de65d22c95fa3cce554e Mon Sep 17 00:00:00 2001 From: macniel Date: Sat, 2 May 2026 09:08:47 +0200 Subject: [PATCH] index.js aktualisiert --- index.js | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 138 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 61d5de5..cc0e35a 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ var gui = require('gui'); var fs = require('fs'); +var ui = require('intuition'); var rh = 14; var sp = 4; @@ -40,6 +41,114 @@ function toDateByComponents(s) { return new Date(year, month, day).getTime(); } +function closeAccount(data, selectedAccount) { + let gadgets = []; + + gadgets.push({ kind: 'text', id: 301, label: "Account:", left: sp+128, top: y, width: 200, height: rh, value: selectedAccount.name}); + const openStanding = getSummation(selectedAccount.entries); + gadgets.push({ kind: 'text', id: 302, label: "Remaining " + (openStanding>0? "funds": "debt"), left: sp+128, top: y+rh+sp, width: 100, height: rh}) + gadgets.push({ kind: 'checkbox', id: 303, label: "Transfer to:", left: sp, top: y+rh+sp+rh+sp, height: rh, value: 0}); + let accounts = []; + for (let i = 0; i < data.length; ++i) { + if (data[i].name != selectedAccount.name) { + accounts.push(data[i].name); + } + } + gadgets.push({ kind: 'cycle', id: 304, left: sp+128, top: y+rh+sp+rh+sp, width: 120, height: rh, items: accounts, value: 0 }); + gadgets.push({ kind: 'button', id: 305, left: sp+128, top: y+rh+sp+rh+sp+rh+sp, height: rh, width: 120, label: "Close Account"}) + + let win = gui.createWindow({ + title: "Close Account '" + selectedAccount.name + "'", + width: sp+128+240+sp, + height: y+rh+sp+rh+sp+rh+sp+rh+sp, + left: 30, + top: 30, + gadgets: gadgets + }); + + gui.setDisabled(win, 304, true); + gui.set(win, 302, openStanding.toFixed(2)); + + while (true) { + var evt = gui.waitEvent(win); + if (!evt) continue; + + if (evt.type === 'close') { + gui.closeWindow(win); + return null; + } + if (evt.type === 'gadgetup') { + if (evt.id === 303) { + gui.setDisabled(win, 304, !gui.get(win, 303)); + } + if (evt.id === 305) { + if (gui.get(win, 303)) { + let otherAccount; + let target = accounts[gui.get(win, 304)]; + for (let i = 0; data.length;++i) { + if (data[i].name === target) { + otherAccount = data[i]; + break; + } + } + if (otherAccount) { + otherAccount.entries.push({ + date: Date.now(), + subject: '('+ selectedAccount.name + ') Closing Statement', + amount: -openStanding, + targetAccount: null + }) + } + gui.closeWindow(win); + return -1; + } + } + } + } +} + +function addAccount(data) { + let gadgets = []; + + gadgets.push({ kind: 'string', id: 201, label: "Name:", left: sp + 80, top: y, width: 300, height: rh}); + gadgets.push({ kind: 'button', id: 202, label: "Open Account", left: sp+80, top: y + rh + sp, width: 100, height: rh}); + + let win = gui.createWindow({ + title: "New Account", + width: sp+80+300+sp, + height: y + rh+ sp+ rh + sp, + left: 30, + top: 30, + gadgets: gadgets + }); + + while (true) { + var evt = gui.waitEvent(win); + if (!evt) continue; + + if (evt.type === 'close') { + gui.closeWindow(win); + return null; + } + if (evt.type === 'gadgetup') { + if (evt.id === 202) { + const newAccountName = gui.get(win, 201); + + if (newAccountName.trim() != '') { + data.push( + { + name: newAccountName, + entries: [] + } + ) + gui.closeWindow(win); + break; + } + } + } + } +} + function subWindow(data) { let gadgets = []; let removable = false; @@ -197,13 +306,20 @@ function mainWindow(projectData) { { title: 'Project', items: [ - { label: 'New...', id: 101, key: 'N' }, + { label: 'New', id: 101, key: 'N' }, { label: 'Open...', id: 102, key: 'O' }, { label: 'Save...', id: 103, key: 'S' }, { label: '---' }, { label: 'Quit', id: 199, key: 'Q' } ] }, + { + title: 'Accounts', + items: [ + { label: "Add...", id:104, key: 'A'}, + { label: "Close...", id:105, key: 'C'} + ] + } ]); let currentAccountIndex = 0; @@ -231,10 +347,10 @@ function mainWindow(projectData) { invalidate = false; } - function doSave() { - var r = gui.fileRequest({ title: 'Save File', pattern: '#?.json', save: true }); + let doSave = (g, data) => { + var r = g.fileRequest({ title: 'Save File', pattern: '#?.json', save: true }); if (r) { - fs.writeFileSync(r.file, JSON.stringify(internalProjectData)); + fs.writeFileSync(r.file, JSON.stringify(data)); } } @@ -258,7 +374,24 @@ function mainWindow(projectData) { internalProjectData = doOpen(); break; } - else if (evt.id === 103) doSave(); + else if (evt.id === 103) { + doSave(gui, internalProjectData); + } + else if (evt.id === 104) { + addAccount(internalProjectData); + invalidate = true; + } + else if (evt.id === 105) { + if (internalProjectData.length > 1) { + const rValue = closeAccount(internalProjectData, internalProjectData[currentAccountIndex]); + if (rValue === -1) { + internalProjectData.splice(currentAccountIndex, 1); + invalidate = true; + } + } else { + ui.alert("Cannot close last remaining Account"); + } + } else if (evt.id === 199) { invalidate = true; internalProjectData = null;