postel-ita
Version:
Library to create files compatible with italian Poste Postel system
204 lines • 6.83 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Lang_1 = __importDefault(require("./Lang"));
class Form {
constructor(payment) {
this.pPayment = payment;
}
get parent() {
return this.pPayment;
}
header() {
const rows = [];
// I don't know why but this is no good
// rows.push('!NEW 2;INL 0;TEX 2;TOP');
// while this is good
rows.push('!NEW 2;INL 0;TEX 2;INL 5');
rows.push('!INL 0;TOP');
rows.push('@G01');
return rows.join(Lang_1.default.EOL);
}
ccLine() {
const PRow = Lang_1.default.PostelRow({ type: 'abs', value: 29 });
// ? Don't know why @-
return PRow.lineSpacing(5)
.font(5)
.prePostText('@-', '@+')
.appendText(this.parent.bank.cc)
.setAlignRight(28)
.setMaxWidth(12)
.back()
.appendText(this.parent.bank.cc)
.setMaxWidth(12)
.setAlignRight(46)
.back()
.result();
}
accountHolder() {
return (Lang_1.default.PostelRow({ type: 'abs', value: 33 })
.prePostText('@>')
.appendText(this.parent.bank.name)
.back()
.result() +
Lang_1.default.EOL +
Lang_1.default.PostelRow({ value: -1, type: 'rel' })
.prePostText(undefined, '@<')
.appendText(this.parent.bank.name)
.setAlignLeft(52)
.back()
.result());
}
amounts() {
return Lang_1.default.PostelRow({ type: 'abs', value: 29 })
.prePostText('@-', '@+')
.appendText(this.parent.comm.amount.toPostelString())
.setMaxWidth(11)
.setAlignLeft(34)
.back()
.appendText(this.parent.comm.amount.toPostelString())
.setAlignLeft(50)
.setMaxWidth(11)
.back()
.result();
}
customCodCli() {
// strutture: 6 cifre del codice cliente (strippato della prima lettera)
// + le 9 cifre della fattura + '0' finale inutilizzato per ora
return (this.parent.address.id.replace(/[^\d]/, '').padStart(6, '0') +
this.parent.comm.invoiceId.padStart(9, '0') +
'0');
}
checkSum(baseNum) {
return baseNum % 93;
}
customCodCliCheckSum() {
const ccc = this.customCodCli();
return (ccc +
this.checkSum(Number.parseInt(ccc)).toString().padStart(2, '0'));
}
customCodCliLine() {
return Lang_1.default.PostelRow({ type: 'abs', value: 38 })
.prePostText('@>@+', '@<@-')
.appendText(this.customCodCliCheckSum())
.setAlignLeft(52)
.back()
.result();
}
ibanSpaces() {
const iban = this.parent.bank.iban;
const ibanA = iban.split('');
return ibanA.join(' ');
}
iban() {
return Lang_1.default.PostelRow({ type: 'abs', value: 46 })
.font(4)
.appendText(this.ibanSpaces())
.setAlignLeft(34)
.back()
.appendText(this.ibanSpaces())
.setAlignLeft(49)
.back()
.result();
}
address() {
const address = this.parent.address.result({ where: 'form' });
const addressA = address.split(Lang_1.default.EOL);
const pr1 = Lang_1.default.PostelRow({ type: 'abs', value: 32 })
.font(2)
.appendText(address)
.setAlignLeft(8)
.back();
const pr2 = Lang_1.default.PostelRow({ type: 'rel', value: -4 });
addressA.forEach((item, idx) => {
pr2.appendText(item)
.setAlignLeft(idx == 0 ? 94 : 87)
.back()
.appendText(Lang_1.default.EOL);
});
return pr1.result() + Lang_1.default.EOL + pr2.result();
}
codeLineItems() {
const amountA = this.parent.comm.amount.toPostelString().split(',');
const cl = {
customCodCliChecksum: this.customCodCliCheckSum(),
docId: '896',
amount: [amountA[0].padStart(8, '0'), amountA[1]],
cc: this.parent.bank.cc.padStart(12, '0')
};
return cl;
}
codeLine() {
const cl = this.codeLineItems();
return Lang_1.default.PostelRow({ type: 'bot' })
.prePostText('@+', '@-')
.font(5)
.appendText('<' + cl.customCodCliChecksum + '>')
.setAlignLeft(51)
.back()
.appendText(cl.amount[0] + '+' + cl.amount[1] + '>')
.setAlignLeft(7)
.back()
.appendText(cl.cc + '<')
.setAlignLeft(2)
.back()
.appendText(cl.docId + '>')
.setAlignLeft(2)
.back()
.result();
}
barcode() {
const cl = this.codeLineItems();
return (
// Use manual code because Lang produce
// this !INL 144;TOP;TEX 4;SPA 64
'!TEX 4;TOP;INL 144;SPA 64' +
Lang_1.default.EOL +
Lang_1.default.PostelRow({ type: 'rel' })
.font(98)
.appendText(cl.customCodCliChecksum +
cl.cc +
cl.amount[0] +
cl.amount[1] +
cl.docId)
.back()
.result());
}
datamatrix() {
const cl = this.codeLineItems();
return (
// Manual code because have strange command order
// if it used Lang result was
// this !INL 0;TEX 2;INL 5;TEX 4;INL 59;TOP;SPA 70
'!INL 0;TEX 2;INL 5;TEX 4;TOP;INL 59;SPA 70' +
Lang_1.default.EOL +
Lang_1.default.PostelRow({ type: 'rel' })
.shebang(false)
.prePostText('@<@-@Z99', '@>@+')
.appendText(cl.customCodCliChecksum +
cl.cc +
cl.amount[0] +
cl.amount[1] +
cl.docId)
.back()
.result());
}
result() {
const rows = [];
rows.push(this.header());
rows.push(this.ccLine());
rows.push(this.accountHolder());
rows.push(this.amounts());
rows.push(this.customCodCliLine());
rows.push(this.iban());
rows.push(this.address());
rows.push(this.codeLine());
rows.push(this.barcode());
rows.push(this.datamatrix());
return rows.join(Lang_1.default.EOL);
}
}
exports.default = Form;
//# sourceMappingURL=Form.js.map