Skip to content

Instantly share code, notes, and snippets.

@tavlima
Last active September 18, 2018 03:10
Show Gist options
  • Save tavlima/8450fbef051de98296abd4db92b8b804 to your computer and use it in GitHub Desktop.
Save tavlima/8450fbef051de98296abd4db92b8b804 to your computer and use it in GitHub Desktop.
boleto
import Ember from 'ember';
import DocCaixa from '../models/docCaixa';
export default Ember.Controller.extend({
appName: 'Boletos',
model: new DocCaixa(),
linhaInput: '',
actions: {
parseLinha: function() {
this.get('model').parseLinha(this.get('linhaInput'));
}
}
});
import EmberObject, { computed } from '@ember/object';
import Documento from './documento';
function replaceAt(string, index, replace) {
return string.substring(0, index) + replace + string.substring(index + 1);
}
export default Documento.extend({
registrado: Ember.computed('livre', {
get(key) {
return this.get('livre').slice(10, 11) == '1';
},
set(key, value) {
let livre = this.get('livre'),
atualizado = replaceAt(livre, 10, value ? 1 : 2);
this.set('livre', atualizado);
return value;
}
})
});
import EmberObject, { computed } from '@ember/object';
import { alias } from '@ember/object/computed';
import DV from '../util/dv';
export default EmberObject.extend({
banco: '', // 3
moeda: '', // 1
vencimento: '', // 4
valor: '', // 10
livre: '', // 24
dvLivre: computed('livre', function() {
return DV.mod11(this.get('livre'));
}),
dvGeral: computed('banco', 'moeda', 'vencimento', 'valor', 'livre', 'dvLivre', function() {
let data = [
this.get('banco'),
this.get('moeda'),
this.get('vencimento'),
this.get('valor'),
this.get('livre'),
this.get('dvLivre')
].join(''),
dv = DV.mod11(data);
return dv ? dv : 1;
}),
campo1: computed('banco', 'moeda', 'livre', function() {
return `${this.get('banco')}${this.get('moeda')}${this.get('livre').slice(0, 5)}`;
}),
campo2: computed('livre', function() {
return `${this.get('livre').slice(5, 15)}`;
}),
campo3: computed('livre', function() {
return `${this.get('livre').slice(15, 25)}`;
}),
campo4: alias('dvGeral'),
campo5: computed('vencimento', 'valor', function() {
return `${this.get('vencimento')}${this.get('valor')}`;
}),
dvCampo1: computed('campo1', function() {
return DV.mod10(this.get('campo1'));
}),
dvCampo2: computed('campo2', function() {
return DV.mod10(this.get('campo2'));
}),
dvCampo3: computed('campo3', function() {
return DV.mod10(this.get('campo3') + this.get('dvLivre'));
}),
parseLinha: function(linha) {
let raw = linha.replace(/[. -]+/g, ''),
banco = raw.slice(0, 3),
moeda = raw.slice(3, 4),
livre1 = raw.slice(4, 9),
livre2 = raw.slice(10, 20),
livre3 = raw.slice(21, 30),
vencimento = raw.slice(33, 37),
valor = raw.slice(37, 47);
this.set('banco', banco);
this.set('moeda', moeda);
this.set('livre', `${livre1}${livre2}${livre3}`);
this.set('vencimento', vencimento);
this.set('valor', valor);
},
codigoDeBarras: computed('banco', 'moeda', 'dvGeral', 'vencimento', 'valor', 'livre', 'dvLivre', function() {
return `${this.get('banco')}-${this.get('moeda')} ${this.get('dvGeral')} ${this.get('vencimento')} ${this.get('valor')} ${this.get('livre')}-${this.get('dvLivre')}`;
}),
digitavel: computed('campo1', 'dvCampo1', 'campo2', 'dvCampo2', 'campo3', 'dvCampo3', 'campo4', 'campo5', function() {
let c1 = `${this.get('campo1').slice(0, 5)}.${this.get('campo1').slice(5, 9)}${this.get('dvCampo1')}`,
c2 = `${this.get('campo2').slice(0, 5)}.${this.get('campo2').slice(5, 10)}${this.get('dvCampo2')}`,
c3 = `${this.get('campo3').slice(0, 5)}.${this.get('campo3').slice(5, 10)}${this.get('dvLivre')}${this.get('dvCampo3')}`;
return `${c1} ${c2} ${c3} ${this.get('campo4')} ${this.get('campo5')}`;
})
});
import Ember from 'ember';
export default Ember.Service.extend({
});
<h1>{{appName}}</h1>
{{input type="text" size="50" value="linhaInput" placeholder="Linha digitável" key-up="parseLinha"}}
<button {{action "parseLinha" linhaInput}}>Parse</button>
<h3>Campos</h3>
{{input type="text" value=model.banco size="3" placeholder="banco"}}
{{input type="text" value=model.moeda size="1" placeholder="moeda"}}
{{input type="text" value=model.dvGeral size="1" placeholder="dv" readonly=true}}
{{input type="text" value=model.vencimento size="4" placeholder="vencimento"}}
{{input type="text" value=model.valor size="10" placeholder="valor"}}
{{input type="text" value=model.livre size="24" placeholder="livre"}}
{{input type="text" value=model.dvLivre size="1" placeholder="dvLivre"}}
{{input type="checkbox" checked=model.registrado}}
<pre>{{ model.codigoDeBarras }}</pre>
<pre>{{ model.digitavel }}</pre>
import { run } from '@ember/runloop';
export default function destroyApp(application) {
run(application, 'destroy');
}
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
const { run } = Ember;
const assign = Ember.assign || Ember.merge;
export default function startApp(attrs) {
let application;
let attributes = assign({rootElement: "#test-root"}, config.APP);
attributes = assign(attributes, attrs); // use defaults, but you can override;
run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
import { start } from 'ember-cli-qunit';
setResolver(resolver);
start();
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { run } from "@ember/runloop";
import Documento from "../../../models/docCaixa";
test('parser', function(assert) {
const doc = new Documento();
run(() => doc.parseLinha('10498.24186 56990.100044 00001.114156 9 76630000070600'));
assert.equal(doc.get('campo1'), '104982418', 'Campo 1 is parsed');
assert.equal(doc.get('campo2'), '5699010004', 'Campo 2 is parsed');
assert.equal(doc.get('campo3'), '000011141', 'Campo 3 is parsed');
assert.equal(doc.get('campo5'), '76630000070600', 'Campo 5 is parsed');
run(() => doc.parseLinha('10490.05505 77222.133348 77777.777713 4 32420000032112'));
assert.equal(doc.get('campo1'), '104900550', 'Campo 1 is parsed');
assert.equal(doc.get('campo2'), '7722213334', 'Campo 2 is parsed');
assert.equal(doc.get('campo3'), '777777777', 'Campo 3 is parsed');
assert.equal(doc.get('campo5'), '32420000032112', 'Campo 5 is parsed');
assert.equal(doc.get('livre'), '005507722213334777777777', 'Livre is parsed');
});
test('DVs linha', function(assert) {
const doc = new Documento();
run(() => doc.parseLinha('10498.24186 56990.100044 00001.114156 9 76630000070600'));
assert.equal(doc.get('dvCampo1'), 6, 'DV c1 is calculated');
assert.equal(doc.get('dvCampo2'), 4, 'DV c2 is calculated');
assert.equal(doc.get('dvCampo3'), 6, 'DV c3 is calculated');
assert.equal(doc.get('dvGeral'), 9, 'DV geral is calculated');
assert.equal(doc.get('registrado'), true, 'Is registrado');
run(() => doc.parseLinha('10490.05505 77222.133348 77777.777713 4 32420000032112'));
assert.equal(doc.get('dvCampo1'), 5, 'DV c1 is calculated');
assert.equal(doc.get('dvCampo2'), 8, 'DV c2 is calculated');
assert.equal(doc.get('dvCampo3'), 3, 'DV c3 is calculated');
assert.equal(doc.get('dvGeral'), 4, 'DV geral is calculated');
assert.equal(doc.get('dvLivre'), 1, 'DV livre is calculated');
});
test('DVs geral', function(assert) {
const doc = new Documento();
run(() => doc.parseLinha('10498.24186 56990.100044 00001.114156 9 76630000070600'));
assert.equal(doc.get('dvGeral'), 9, 'DV geral is calculated');
run(() => doc.parseLinha('10490.05505 77222.133348 77777.777713 4 32420000032112'));
assert.equal(doc.get('dvGeral'), 4, 'DV geral is calculated');
});
test('DVs livre', function(assert) {
const doc = new Documento();
run(() => {
doc.set('banco', '104');
doc.set('moeda', '9');
doc.set('vencimento', '3242');
doc.set('valor', '0000032112');
doc.set('livre', '005507722213334777777777');
});
assert.equal(doc.get('dvLivre'), 1, 'DV livre is calculated');
assert.equal(doc.get('dvGeral'), 4, 'DV geral is calculated');
});
{
"version": "0.15.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0"
}
}
let calcDv = function(campo, pesos, mod, fn = null) {
let fnDigitos = fn ? fn : function (x) {
return x > 9 ? parseInt(`${x}`[0]) + parseInt(`${x}`[1]) : x;
};
let arrCampo = campo.split(''),
indices = [...Array(arrCampo.length).keys()],
coeficientes = indices.reverse().map(x => pesos[x % pesos.length]),
produtos = arrCampo.map((x, i) => x * coeficientes[i]),
digitos = produtos.map(fnDigitos),
soma = digitos.reduce((acc, x) => acc + x, 0),
resto = soma % mod,
dv = mod - resto;
return dv > 9 ? 0 : dv;
};
export default {
mod10: function(campo) {
return calcDv(campo, [2, 1], 10);
},
mod11: function(campo) {
return calcDv(campo, [2, 3, 4, 5, 6, 7, 8, 9], 11, (x) => x);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment