controller.js
10.6 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
angular.module('focaModalDescarga')
.controller('focaModalDescargaCtrl', [
'$scope',
'focaModalDescargasService',
'$filter',
'focaModalService',
'$timeout',
'$uibModalInstance',
'$uibModal',
'remito',
'focaModalDetalleHojaRutaService',
'focaSeguimientoService',
'crearRemitoService',
function ($scope, focaModalDescargasService, $filter,
focaModalService, $timeout, $uibModalInstance, $uibModal, remito,
focaModalDetalleHojaRutaService, focaSeguimientoService, crearRemitoService) {
//#region variables
$scope.remito = {};
$scope.remito = remito;
$scope.nroRecibo = 0;
$scope.cisternas = remito.cisternas;
$scope.nroSucursal;
$scope.nroRemito;
$scope.articulos = [];
$scope.hasErrorSucursalRemito = false;
//#endregion
function init() {
crearRemitoService.getNumeroRemito().then(
function (res) {
$scope.nroSucursal = res.data.sucursal;
$scope.nroRemito = res.data.numeroRemito;
$scope.validateSucursalRemito($scope.nroSucursal, $scope.nroRemito);
},
function (err) {
focaModalService.alert('La terminal no esta configurada correctamente');
console.info(err);
}
);
};
init();
$scope.crearRemito = function () {
if (!$scope.remito.cliente) {
focaModalService.alert("No has seleccionado cliente");
return;
}
if (validarDescarga()) {
return;
}
//#region OBJETO REMITO
var remito = {
id: 0,
fechaRemito: new Date().toISOString().slice(0, 19).replace('T', ' '),
idCliente: $scope.remito.cliente.cod,
nombreCliente: $scope.remito.cliente.nom,
cuitCliente: $scope.remito.cliente.cuit,
total: $scope.getTotal() * $scope.remito.cotizacion.VENDEDOR,
numeroNotaPedido: $scope.remito.numeroNotaPedido,
idVendedor: $scope.remito.cliente.ven,
idProveedor: $scope.remito.proveedor.COD,
idDomicilio: 0,
idCotizacion: $scope.remito.cotizacion.ID,
idListaPrecio: parseInt($scope.remito.cliente.mod),
flete: $scope.remito.flete,
fob: $scope.remito.fob,
bomba: $scope.remito.bomba,
kilometros: $scope.remito.kilometros,
domicilioStamp: $scope.remito.cliente.DOM,
observaciones: $scope.remito.observaciones,
numeroRemito: parseInt($scope.nroRemito),
sucursal: parseInt($scope.nroSucursal),
responsabilidadIvaCliente: $scope.remito.cliente.iva,
descuento: 0,//TODO,
importeNeto: getImporte('netoUnitario'),
importeExento: getImporte('exentoUnitario'),
importeIva: getImporte('ivaUnitario'),
importeIvaServicios: 0,//TODO
importeImpuestoInterno: getImporte('impuestoInternoUnitario'),
importeImpuestoInterno1: getImporte('impuestoInterno1Unitario'),
importeImpuestoInterno2: getImporte('impuestoInterno2Unitario'),
percepcion: 0,//TODO
percepcionIva: 0,//TODO
redondeo: 0,//TODO
anulado: false,
planilla: $filter('date')($scope.now, 'ddMMyyyy'),
lugar: parseInt($scope.puntoVenta),
cuentaMadre: 0,//TODO
cuentaContable: 0,//TODO
asiento: 0,//TODO
e_hd: '',//TODO
c_hd: '',
numeroLiquidoProducto: 0,//TODO
estado: 0,
destinoVenta: 0,//TODO
operacionTipo: 0, //TODO
idHojaRuta: $scope.remito.idHojaRuta
}
//#endregion
focaModalDescargasService.crearRemito({ remito: remito, notaPedido: { id: 0 } })
.then(function (res) {
remitoId = res.data.id
descargar(remitoId);
focaModalDescargasService.
addArticulos($scope.remito.articulosRemito, $scope.articulos, remitoId, 1);
focaModalService.alert("Descarga realizada")
.then(function () {
$uibModalInstance.dismiss();
})
})
.catch(function (e) { console.log(e); })
};
$scope.getTotal = function () {
var total = 0;
$scope.cisternas.forEach(function (cisterna) {
total += cisterna.cisternaCarga.articulo.PreVen * cisterna.cisternaCarga.descargar;
cisterna.cisternaCarga.articulo.cantidad = cisterna.cisternaCarga.descargar;
$scope.articulos.push(cisterna.cisternaCarga.articulo);
});
return parseFloat(total.toFixed(2));
};
function descargar(idRemito) {
for (var i = 0; i < $scope.cisternas.length; i++) {
var descarga = $scope.cisternas[i].cisternaCarga.descargar;
var hojaRutaMovimientos = [];
var cisternaMovimientos = [];
var cisternaCargas = [];
var cisternaCarga = $scope.cisternas[i].cisternaCarga;
if (!descarga) continue;
cisternaCarga.cantidad -= descarga;
//Guardar
var now = new Date();
var cisternaMovimiento = {
fecha: now.toISOString().slice(0, 19).replace('T', ' '),
cantidad: descarga,
metodo: 'descarga',
idCisternaCarga: cisternaCarga.id,
idRemito: idRemito
};
var hojaRutaMovimiento = {
reciboDescarga: $scope.nroRecibo,
idRemito: idRemito
};
cisternaCarga = angular.copy($scope.cisternas[i].cisternaCarga);
delete cisternaCarga.articulo;
delete cisternaCarga.descargar;
cisternaCargas.push(cisternaCarga);
cisternaMovimientos.push(cisternaMovimiento);
hojaRutaMovimientos.push(hojaRutaMovimiento);
}
var save = {
cisternaCargas: cisternaCargas,
cisternaMovimientos: cisternaMovimientos,
hojaRutaMovimientos: hojaRutaMovimientos,
articulo: $scope.articuloSeleccionado
};
focaModalDetalleHojaRutaService
.postMovimientoHojaRuta(save)
.then(guardarSeguimiento)
.catch(function () { });
function guardarSeguimiento(res) {
focaSeguimientoService
.guardarPosicion(
'Entrega de producto',
res.data[0].id,
'');
$scope.aDescargar = [];
};
};
function getImporte(propiedad) {
var importe = 0;
$scope.remito.articulosRemito.forEach(function (articulo) {
if (articulo[propiedad]) {
importe += articulo[propiedad] * articulo.cantidad;
}
return;
});
return importe;
};
function validarDescarga() {
for (var i = 0; i < $scope.cisternas.length; i++) {
if (parseInt($scope.cisternas[i].cisternaCarga.descargar) >
$scope.cisternas[i].cisternaCarga.cantidad ||
parseInt($scope.cisternas[i].cisternaCarga.descargar) < 0) {
focaModalService.alert("Cantidad de descarga ingresada erronea");
return true;
}
}
return false;
};
$scope.seleccionarCliente = function () {
var modalInstance = $uibModal.open(
{
ariaLabelledBy: 'Busqueda de Cliente',
templateUrl: 'foca-busqueda-cliente-modal.html',
controller: 'focaBusquedaClienteModalController',
resolve: {
vendedor: function () { return null; },
cobrador: function () { return null; }
},
size: 'lg'
}
);
modalInstance.result.then(function (cliente) {
$scope.remito.cliente = cliente;
}, function () {
//funcion ejecutada al cancelar modal
});
};
$scope.validateSucursalRemito = function (sucursal, remito) {
if (!sucursal || !remito) {
$scope.hasErrorSucursalRemito = false;
return;
}
focaModalDescargasService.validarSucursalRemito(sucursal, remito)
.then(function (res) {
if (res.data.data === 1) {
$scope.hasErrorSucursalRemito = true;
} else {
$scope.hasErrorSucursalRemito = false;
}
})
.catch(function (e) { console.error(e); });
};
$scope.cancelar = function () {
$uibModalInstance.dismiss();
};
function rellenar(relleno, longitud) {
relleno = '' + relleno;
while (relleno.length < longitud) {
relleno = '0' + relleno;
}
return relleno;
};
}
]);