/*
DjRece es una interfase web para utilizar PyRece desde una app de Django
DjRece: Copyright 2010 Alan Etkin, hexa662@gmail.com.
Este programa se distribuye bajo los términos de la licencia AGPLv3.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License, or any later
version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
PyRece - PyAfipWS es copyright de M. Reingart y M. Alániz
http://code.google.com/p/pyafipws/
*/
// objeto para pagos
var pagos = new Array();
var item = new Object();
item.id = new Array();
item.precio = new Array();
item.iva = new Array();
item.cantidad = new Array();
item.confirmado = new Array();
var totalpagos = 0;
var imptotal = 0;
var impneto = 0;
var impliq = 0;
var cantidad_pagos = 0;
var cantidad_item = 0;
function tipoDeCbte(numero, nombres_cbte){
// window.alert("tipoDeCbte: " + numero + " " + String(nombres_cbte));
var resultado = null;
var elnumero = Number(numero);
for(var x = 0; x < nombres_cbte.nombres.length; x++){
if(nombres_cbte.numeros[x] == elnumero){
resultado = nombres_cbte.nombres[x];
} // fin de if
} // fin de for
return resultado;
} // fin de function
function agregarPago(tipo, valor, banco, codigo){
actualizarPagos();
if(Number(valor) <= 0 || isNaN(Number(valor))){
window.alert("El valor ingresado no es válido.");
return;
} // fin de if
pagos.push([tipo, valor, banco, codigo, ""]);
generarTablaPagos();
} // fin de function
function actualizarPagos(){
$("#pagos input[type='checkbox']").each(function(m){
if($(this).attr("checked")){
pagos[m][4] = "checked";
} // fin de if
} // fin de function
);
} // fin de function
function borrarPagos(){
var diferencia = 0;
$("#pagos input[type=checkbox]").each(function(m){
// si está tildado, borrar item i de la lista de pagos
if($(this).attr("checked")){
// sumar 1 a diferencia
// eliminar el item de pagos
pagos.splice((m - diferencia), 1);
diferencia +=1;
} // fin de function
});
generarTablaPagos();
} // fin de function
function generarTablaPagos(){
$("#pagos tbody").html("");
var parimpar = null;
for(var x = 0; x < pagos.length; x++){
if((x % 2) == 0){
parimpar = "par";
} // fin de if
else{
parimpar = "impar"
} // fin de else
$("#pagos tbody").append("<tr class=" + parimpar + " >" +
"<td>" + pagos[x][0] + "</td>" +
"<td>" + pagos[x][1] + "</td>" +
"<td>" + pagos[x][2] + "</td>" +
"<td>" + pagos[x][3] + "</td>" +
"<td class='checkbutton'>" + "<input type='checkbox' " + pagos[x][4] + " />" + "</td>" +
"</tr>");
} // fin de for
} // fin de function
function calcularCbte(){
item.id = new Array();
item.precio = new Array();
item.iva = new Array();
item.cantidad = new Array();
item.confirmado = new Array();
$("#productos tbody .id").each(function(m){
item.id.push($(this).text());
} // fin de function
);
$("#productos .precio").each(function(m){
item.precio.push(Number($(this).text()));
} // fin de function
);
$("#productos .iva").each(function(m){
item.iva.push(Number($(this).text()));
} // fin de function
);
$("#productos .cantidad").each(function(m){
item.cantidad.push(Number($(this).val()));
} // fin de function
);
$("#productos input[type='checkbox']").each(function(m){
item.confirmado.push($(this).attr("checked"));
} // fin de function
);
cantidad_pagos = 0;
cantidad_item = 0;
totalpagos = 0;
imptotal = 0;
impneto = 0;
impliq = 0;
for(var x = 0; x < item.id.length; x++){
if(item.confirmado[x] && (!isNaN(item.cantidad[x])) && (item.cantidad[x] > 0)){
cantidad_item += 1;
imptotal += (item.cantidad[x] * item.precio[x]) + (item.precio[x]*item.cantidad[x]*item.iva[x]/100);
impneto += (item.cantidad[x] * item.precio[x]);
impliq += (item.precio[x]*item.cantidad[x]*item.iva[x]/100);
} // fin de if
} // fin de for
for(var x = 0; x < pagos.length; x++){
cantidad_pagos += 1;
totalpagos += Number(pagos[x][1]);
} // fin de for
$("#total_pagos").val(totalpagos);
} // fin de function
function actualizarCbte(){
calcularCbte();
if($("#presta_serv").val() == "1"){
var netotmp = 0;
var liqtmp = 0;
var totaltmp = 0;
netotmp = Number($("#serv_importe").val());
// si es un recibo, neto = total, liq = 0
if($("#tipo_cbte").val() == "04" || $("#tipo_cbte").val() == "09"){
$("#imp_total").val(totalpagos);
$("#imp_neto").val(totalpagos);
$("#imp_liq").val("0");
} // fin de if
else{
if(isNaN(netotmp) || netotmp <= 0){
window.alert("Importe de servicios inválido.");
return;
} // fin de if
liqtmp = netotmp * 21 / 100;
totaltmp = netotmp + liqtmp;
$("#imp_total").val(totaltmp);
$("#imp_neto").val(netotmp);
$("#imp_liq").val(liqtmp);
} // fin de else
} // fin de if
else{
// borrar y generar los campos para el formulario
$("#campos").html("");
// total
// si es un recibo, neto = total, liq = 0
if($("#tipo_cbte").val() == "04" || $("#tipo_cbte").val() == "09"){
$("#imp_total").val(totalpagos);
$("#imp_neto").val(totalpagos);
$("#imp_liq").val("0");
} // fin de if
else{
$("#imp_total").val(imptotal);
$("#imp_neto").val(impneto);
$("#imp_liq").val(impliq);
} // fin de else
} // fin de else
$("#cantidad_item").val(cantidad_item);
$("#cantidad_pagos").val(cantidad_pagos);
// completar los campos
var contadortmp = 0;
for(var x = 0; x < item.id.length; x++){
if(item.confirmado[x] && (!isNaN(item.cantidad[x]))){
$("#campos").append("<input name='i_" + String(contadortmp) + "_id' type='text' value='" + String(item.id[x]) + "' />");
$("#campos").append("<input name='i_" + String(contadortmp) + "_cantidad' type='text' value='" + String(item.cantidad[x]) + "' />");
// window.alert("Se agregó el ítem: " + String(contadortmp));
contadortmp +=1;
} // fin de if
} // fin de for
for(var x = 0; x < pagos.length; x++){
$("#campos").append("<input name='p_" + String(x) + "_tipo' type='text' value='" + String(pagos[x][0]) + "' />");
$("#campos").append("<input name='p_" + String(x) + "_codigo' type='text' value='" + String(pagos[x][3]) + "' />");
$("#campos").append("<input name='p_" + String(x) + "_valor' type='text' value='" + String(pagos[x][1]) + "' />");
$("#campos").append("<input name='p_" + String(x) + "_banco' type='text' value='" + String(pagos[x][2]) + "' />");
} // fin de for
// window.alert("total: " + $("#imp_total").val() + " " + "neto: " + $("#imp_neto").val() + " " + "liq: " + $("#imp_liq").val());
} // fin de function
function enviarCbte(){
actualizarCbte();
if(cantidad_pagos > 5 || cantidad_item > 20){
window.alert("Se superó la cantidad máxima de ítem/pagos");
return;
} // fin de if
if(Number($("#imp_total").val()) <= 0){
window.alert("El importe de la operación es inválido.");
return;
} // fin de if
if($("#tipo_cbte").val() != "04" && $("#tipo_cbte").val() != "09" && $("#presta_serv").val() == "0"){
if(cantidad_item <= 0){
window.alert("El cbte. no tiene ítem.");
return;
} // fin de if
} // fin de if
// window.alert("Enviando cbte.");
$("#enviar_formulario").click();
} // fin de function
function parcialCbte(){
actualizarCbte();
if(cantidad_pagos > 5 || cantidad_item > 20){
window.alert("Se superó la cantidad máxima de ítem/pagos");
return;
} // fin de if
if(Number($("#imp_total").val()) <= 0){
window.alert("El importe de la operación es inválido.");
return;
} // fin de if
if($("#tipo_cbte").val() != "04" && $("#tipo_cbte").val() != "09" && $("#presta_serv").val() == "0"){
if(cantidad_item <= 0){
window.alert("El cbte. no tiene ítem.");
return;
} // fin de if
} // fin de if
} // fin de function
function stripping(){
$("tr").each(function(m){
if((m % 2) == 0){
// aplicar class "par"
$(this).attr("class", "par");
} // fin de if
else{
// aplicar class "impar"
$(this).attr("class", "impar");
} // fin de else
});
} // fin de function