relaciones.js
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module.exports = function(planex, wsServer) {
return new Promise(function(resolve, reject) {
var promesasArticulo = [];
var tipoCuit = planex.identificaxEmisor.cuit.slice(0, 2);
var digitoVerificador = planex.identificaxEmisor.cuit[planex.identificaxEmisor.cuit.length - 1];
var cuitToDebo = tipoCuit + '-' + planex.identificaxEmisor.cuit.slice(2, 10) + '-' + digitoVerificador;
var promiseProveedores = wsServer.getEntidad(planex.identificaxSucursalReceptorFactura.glnSucursal, 'PROVEED', {CUIT: cuitToDebo});
var promiseEmp = wsServer.getEntidad(planex.identificaxSucursalReceptorFactura.glnSucursal, 'APAREMP');
planex.itemsFactura.forEach(item => {
var queryString = 'select cb.*, art.CodRub, art.DET_LAR from CODBAR cb ' +
'join ARTICULOS art on cb.CodArt = art.CodArt and cb.CodSec = art.CodSec where cb.CodBar = \'' +
item.codigoUPCEAN13 + '\'';
promesasArticulo.push(
wsServer.getEntidad(planex.identificaxSucursalReceptorFactura.glnSucursal, null, {}, queryString)
);
});
promesas = promesasArticulo.concat([promiseProveedores, promiseEmp]);
Promise.all(promesas).then(function(values) {
planex.itemsFactura.forEach((item, index) => {
if (!values[index]) {
console.error('No se encontró el articulo Código de barras = ' + item.codigoUPCEAN13);
}
item.CodArt = values[index] ? values[index].CodArt : 0;
item.CodSec = values[index] ? values[index].CodSec : 0;
item.CodRub = values[index] ? values[index].CodRub : 0;
item.DET_LAR = values[index] ? values[index].DET_LAR : item.descripcion;
delete values[index];
});
values = values.filter(function(p) {
return p;
});
resolve({planex, values});
}).catch(reject);
});
}