Files

306 lines
14 KiB
JavaScript

let gi = (n, c) => $$.sc('glyphicon glyphicon-' + n).aC(c);
let $inv = {
init2: function (type, options) {
type = type || 'inv';
options = options || {};
$ocms.getScript([
//{ script: 'web/jstree.min.js', css: 'web/jstree.min.css', condition: typeof $.fn.jstree !== 'function' }
//, { script: 'web/jquery.qtip.min.js', css: 'web/jquery.qtip.min.css', condition: typeof $.fn.qtip !== 'function' }
//, { script: 'web/typeahead.min.js', css: '', condition: typeof $.fn.typeahead !== 'function' }
//, { script: 'web/fullcalendar.min.js', module: 'FullCalendar', css: 'web/fullcalendar.min.css', condition: typeof FullCalendar !== 'object' }
], function () {
//FullCalendar = $vm.FullCalendar;
$inv.init3(type, options);
});
}, init3: async function (type, options) {
$fis.cf(true);
let lf = $fis.lf(true);
$('#topbar').ocmsmenu([]); //{ lbl: 'home', fnc: $inv.init3 }
$('#activemodule').text($ict.mdl);
let pa = [(async () => {
if (await $fis.getAuth('fds_inv') > 0) {
$inv.prepLst('');
lf.aC('fix'); //initially should be shown - for convenience
}
})(), new Promise((resolve, reject) => {
$fis.prepAuth(['fds_reminder']);
})];
await Promise.all(pa);
}, prepLst: function (includes) {
let td = new Date();
let lf = $fis.lf(true).ldng(1), sd = new Date('2021-01-01');
let frm = $fis.frm_list();
frm.IN(function () { });
let mitm = []
$.each($ict.iov, (ic, il) => {
mitm.push({ lbl: il, fnc: () => { $inv.prepLst(ic); lf.aC('fix'); } });
});
$fis.lfm().ocmsmenu([{ lbl: 'Filter', itm: mitm }]);
let osb = $$.i({ placeholder: $ict.in }).appendTo($$.dc('mth ivn', lf)).enterKey(function (ev) {
let mth = $(this), v = mth.val() || '';
ev.stopPropagation();
mth.parent().siblings().rC('selected');
if (v.length > 3) {
mth.parent().aC('selected');
$inv.renderinv('i:' + v, 's', 'all');
mth.val('');
}
});
let osb2 = $$.i({ placeholder: $ict.cc }).appendTo($$.dc('mth ivc', lf)).enterKey(function (ev) {
let mth = $(this), v = mth.val() || '';
ev.stopPropagation();
mth.parent().siblings().rC('selected');
if (v.length >= 3) {
mth.parent().aC('selected');
$inv.renderinv('c:' + v, 's', 'all');
mth.val('');
}
});
if (includes.substr(0,1) === '#') {
$$.dc('mth extra', lf).text($ict.iov[includes].replace(')',$ict.uba)).click(function (ev) {
let mth = $(this);
ev.stopPropagation();
mth.siblings().rC('selected');
if (mth.is('.selected') === true) {
mth.toggleClass('selected');
let tgt = fdt(new Date(), 'yy-MM-dd');
$inv.renderinv(tgt, 'a', includes);
}
mth.aC('selected');
});
}
lf.append('<hr />');
let mthl = $$.dc('mthl', lf), thisyear = td.getFullYear(), thismonth = td.getMonth() + 1;
for (let tyr = sd.getFullYear(); tyr <= thisyear; tyr++) {
let yr = $$.dc('yr').prependTo(mthl).text($ict.iov[includes] + ' - ' + tyr.toString()).toggleClass('selected', tyr === thisyear);
yr.click({
yr: tyr
}, function (ev) {
ev.stopPropagation();
yr.siblings().rC('selected');
yr.aC('selected');
});
let mfrm = $$.dc('mfrm', yr);
for (let tmt = 0; tmt < (tyr !== thisyear ? 12 : thismonth); tmt++) {
sd = new Date(tyr, tmt, 1);
let mth = $$.dc('mth').prependTo(mfrm).text($ict.iov[includes] + ' - ' + fdt(sd, 'MMM yyyy'));
mth.click({
yr: tyr, mt: tmt
}, function (ev) {
ev.stopPropagation();
mth.siblings().rC('selected');
if (mth.is('.selected') === true) {
mth.toggleClass('selected');
let tgt = fdt(new Date(ev.data.yr, ev.data.mt, 1), 'yy-MM-dd');
$inv.renderinv(tgt, 'm', includes);
}
mth.aC('selected');
});
if (includes === '') { // only for combi of final invoices
let mthdl = $$.dc('mthdl', mth).append(gi('compressed', 'ico'));
mthdl.click({
yr: tyr, mt: tmt
}, function (ev) {
ev.stopPropagation();
let tgt = fdt(new Date(ev.data.yr, ev.data.mt, 1), 'yy-MM-dd');
$inv.downloadzip(tgt, 'm');
});
}
let fwf = getMonday(sd), rd = fwf, lwf = new Date(sd);
lwf.setMonth(lwf.getMonth() + 1);
lwf.setDate(0);
lwf = getMonday(lwf);
let wfrm = $$.dc('wfrm', mth);
while (rd <= lwf) {
let wk = $$.dc('wk', wfrm).text(($ict.wk || 'W') + ' ' + fdt(rd, 'dd.MM.yy'));
wk.click({
rd: new Date(rd)
}, function (ev) {
ev.stopPropagation();
let tgt = fdt(ev.data.rd, 'yy-MM-dd');
$inv.renderinv(tgt, 'w', includes);
mth.siblings().rC('selected').find('.wk').rC('selected');
mth.aC('selected').find('.wk').rC('selected');
wk.aC('selected');
});
let wkdl = $$.dc('wkdl', wk).append(gi('compressed', 'ico'));
wkdl.click({
rd: new Date(rd)
}, function (ev) {
ev.stopPropagation();
let tgt = fdt(ev.data.rd, 'yy-MM-dd');
$inv.downloadzip(tgt, 'w');
});
rd.setDate(rd.getDate() + 7);
}
}
}
lf.ldng(0);
}, rerenderinv: function () {
let invfrm = $('#contentframe .invfrm:first');
if (invfrm.length > 0) {
let sets = invfrm.data('sets') || {};
if (sets.mode) {
$inv.renderinv(sets.tgt, sets.mode, sets.includes);
}
}
}, renderinv: function (tgt, mode, includes) {
let invlst = $fis.frm_list(true, true).ldng(1), invfrm = $$.dc('invfrm', invlst).aC('md' + mode).data('sets', $.extend({}, { tgt: tgt, mode: mode, includes: includes }));
let lf = $fis.lf();
$ocms.postXT({
url: $ocms.url('inv/invl'), data: { mode: mode, tgt: tgt, includes: includes }, success: (response) => {
lf.rC('fix').aC('hd');
$$.dc('ovhd', invfrm).text(response.admin.title);
let ts = $$.tblset({}, invfrm), fd = $invcol.inv;
let thr = $$.tr(ts.hd);
let haux = $$.th(thr);
$.each(fd.fields || [], (ci, cx) => {
$$.th(thr).text(cx.label);
if (cx.name === 'vat') {
$$.th(thr);
}
});
$.each(response.invoices || [], (ri, rw) => {
let tr = $$.tr(ts.bdy);
tr.click(function () {
lf.rC('fix').aC('hd');
tr.toggleClass('selected').siblings().rC('selected').find('td.av').rC('av');
tr.find('td.av').rC('av');
if (tr.is('.selected') === true) {
$inv.iMn(rw);
} else {
$inv.eM();
}
});
let raux = $$.td(tr, { class: 'raux' });
if (rw.hasFile) {
$$.dc('idl ilbtn', raux, { title: $ict.dl + '\n' + rw.DocumentName }).append(gi('save-file', 'ico')).click({ id: rw.Id }, $inv.downloadinv);
$$.dc('idl ilbtn', raux, { title: $ict.dsp + '\n' + rw.DocumentName }).append(gi('eye-open', 'ico')).click({ id: rw.Id, typ: 'inv' }, $inv.jdisp);
} else if (rw.isFinal === false && $fis.isAuth('fds_inv',2) === true) {
$$.dc('idl ilbtn', raux, { title: $ict.ed }).append(gi('edit', 'ico')).click({ id: rw.Id }, $inv.doContInv);
}
$$.dc('iitm ilbtn', raux, { title: $ict.sItm }).append(gi('list', 'ico')).click({ id: rw.Id }, $inv.showitm);
$$.dc('iitm ilbtn', raux, { title: $ict.sPay }).append(gi('euro', 'ico')).click({ id: rw.Id }, $inv.showpay);
$.each(fd.fields || [], (ci, cx) => {
let td = $$.td(tr).aC(cx.dtype), sel, o;
if ((cx.type || '') === 'select') {
td.text((cx.url || {})[rw[cx.name]] || '');
} else {
td.text(rw[cx.name])
}
switch (cx.name || '') {
case 'vat':
sel = $$.sel().appendTo($$.td(tr, { class: 'vsel' }));
o = (response.admin.ust_options || '19,0%;16,0%;0,0%').split(';');
$.each(o, (oi, oo) => { $$.opt(oo, oo).appendTo(sel); });
sel.click(function (ev) { ev.stopPropagation(); }).val(rw[cx.name]).change().change({ frm: invfrm, tgt: tgt, mode: mode, id: rw.Id, td: td, includes: includes }, $inv.setvat);
td.toggleClass('hl', rw[cx.name].substr(0, 2) !== o[0].substr(0, 2)).click(function (ev) {
ev.stopPropagation();
$(this).toggleClass('av');
});
break;
case 'balance':
td.aC('sh_' + (rw.SollHaben || '').toLowerCase());
break;
case 'invstatus': //drop through by intention
case 'reminderstatus':
td.aC((cx.name === 'invstatus' ? 'is_' : 'rs_') + rw[cx.name]);
break;
}
});
});
}, complete: () => { invlst.ldng(0); }
});
}, setvat: function (ev) {
let sel = $(this), data = ev.data || {};
$ocms.postXT({
url: $ocms.url('inv/setvat'), data: { id: data.id, val: sel.val() }, success: (response) => {
data.td.rC('av');
$inv.renderinv(data.tgt, data.mode, data.includes);
}
});
}, downloadzip: function (tgt, mode) {
let frm = $(this).empty();
window.open($ocms.url('inv/datevzip?mode=' + mode + '&tgt=' + encodeURIComponent(tgt)), '_blank');
}, showitm: function (ev) {
let tr = $(this).closest('tr');
ev.stopPropagation();
if (tr.is('.selected') === false) {
return;
}
$ocms.postXT({
url: $ocms.url('inv/rqi'), data: { id: ev.data.id }, success: (response) => {
let fr = $$.dc('rfrm');
if ((response.requests || []).length < 1) {
fr.text($ict.nd);
} else {
$.each(response.requests || [], function (ri, rx) {
let rq = $$.dc('srq', fr);
$$.dc('nme', rq).text(rx.name);
let rif = $$.tblset({ class: 'if' }, rq);
$.each(rx.items || [], (ii, ix) => {
let itr = $$.tr({ id: 'itm' + ix.Id }).appendTo(rif.bdy);
$$.td(itr).text(ix.NameOrNumber);
$$.td(itr).text(ix.Type);
$$.td(itr).aC('currency').text(ix.net_pos);
$$.td(itr).aC('currency').text(ix.bo_pos);
$$.td(itr).aC('num').text(ix.vat);
});
});
}
$ocms.dlg(fr, { width: 1000 });
}
});
}, showpay: function (ev) {
let tr = $(this).closest('tr');
ev.stopPropagation();
if (tr.is('.selected') === false) {
return;
}
$ocms.postXT({
url: $ocms.url('inv/pyi'), data: { id: ev.data.id }, success: (response) => {
let fr = $$.dc('rfrm');
if ((response.payments || []).length < 1) {
fr.text($ict.nd);
} else {
let rif = $$.tblset({ class: 'if' }, fr);
let hr = $$.tr(rif.hd);
$.each(['date','account', 'name', 'text', 'InvoiceID', 'amount', 'manual'], (hi, hx) => {
$$.th(hr, $ict.payi[hx]);
});
$.each(response.payments, (ii, ix) => {
let itr = $$.tr({ id: 'itm' + ix.banking_uid }).appendTo(rif.bdy);
$$.td(itr).aC('date').text(ix.date);
$$.td(itr).text(ix.account);
$$.td(itr).text(ix.name);
$$.td(itr).text(ix.text);
$$.td(itr).text(ix.InvoiceID);
$$.td(itr).aC('currency').text(ix.amount);
$$.td(itr).text(ix.manual);
});
}
$ocms.dlg(fr, { width: 1000, title: 'Übersicht der Zahlungen' });
}
});
}, downloadinv: function (ev) {
let tr = $(this).closest('tr');
ev.stopPropagation();
if (tr.is('.selected') === false) {
return;
}
window.open($ocms.url('inv/rdoc?id=' + ev.data.id), '_blank');
}, doContInv: function(ev) {
let tr = $(this).closest('tr');
ev.stopPropagation();
if (tr.is('.selected') === false) {
return;
}
$inv.cntInv({ id: ev.data.id });
}
}
let $$inv = { init2: $inv.init2, auth: {} };
export default $$inv;