Budget/waitForCloseAccountWindow.js

123 lines
3.4 KiB
JavaScript

var gui = require('gui');
const { getSummation } = require('./utils');
function waitForCloseAccountWindow(data, selectedAccount) {
var rh = 14;
var sp = 4;
var y = sp;
let gadgets = [];
const accountNameId = 301;
const remainingId = 302;
const transferCheckboxId = 303;
const targetAccountId = 304;
const executeButtonId = 305;
const openStanding = getSummation(selectedAccount.entries);
let accounts = [];
for (let i = 0; i < data.length; ++i) {
if (data[i].name != selectedAccount.name) {
accounts.push(data[i].name);
}
}
gadgets.push({
kind: 'text', id: accountNameId,
label: "Account:",
left: sp + 128,
top: y,
width: 200,
height: rh,
value: selectedAccount.name
});
gadgets.push({
kind: 'text', id: remainingId,
label: "Remaining " + (openStanding > 0 ? "funds" : "debt"),
left: sp + 128,
top: y + rh + sp,
width: 100,
height: rh
});
gadgets.push({
kind: 'checkbox', id: transferCheckboxId,
label: "Transfer to:",
left: sp,
top: y + rh + sp + rh + sp,
height: rh,
value: 0
});
gadgets.push({
kind: 'cycle', id: targetAccountId,
left: sp + 128,
top: y + rh + sp + rh + sp,
width: 120,
height: rh,
items: accounts,
value: 0
});
gadgets.push({
kind: 'button', id: executeButtonId,
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, targetAccountId, true);
gui.set(win, remainingId, 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 === transferCheckboxId) {
gui.setDisabled(win, targetAccountId, !gui.get(win, transferCheckboxId));
}
if (evt.id === executeButtonId) {
if (gui.get(win, transferCheckboxId)) {
let otherAccount;
let target = accounts[gui.get(win, targetAccountId)];
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;
}
}
}
}
}
exports.waitForCloseAccountWindow = waitForCloseAccountWindow;