//-------------------------------------------------------//
// Author:Rodolfo Machon
// Company:WebSoft, SA de CV 
// Nombre del archivo: contribuyente.solvencia.get.js
// Created: 2017-10-01
// Updates: 2017-10-01
// Copyright - All Rights Reserved
//-------------------------------------------------------//
//-------------------------------------------------------//
var OpenWindows = {};
//-------------------------------------------------------//
window.onload = function(){
    window.resizeTo(1100,765);
};
//-------------------------------------------------------//
// LEVEL 1
function OpenBalance(id,gen){
    var clase="";
    if (gen=="inmueble"){clase="inmueble";}
    if (gen=="empresa"){clase="empresa";}
    if (gen=="agua"){clase="agua";}
    if (gen=="puesto"){clase="puesto";}     
    let winame 	= "ExpedienteEstados";
	if (!OpenWindows[winame] || OpenWindows[winame].closed){
		OpenWindows[winame]= window.open("../pl/../bin/BSWS.pl?clase=="+clase+""+"&id="+id+"&fields="+fields+"&values="+values,"ExpedienteBalanceBSWS","left=0,top=0,width=100,height=100,resizable=yes,scrollbars=yes");
	}else{OpenWindows[winame].focus();}
}
async function generarEstados(){
    document.getElementById("guardar").style.display="none";
    let todate = new Date().toLocaleDateString("fr-CA",{year:"numeric", month:"2-digit", day:"2-digit"}).split("-");
    let daysInMonth = getDaysInMonth(todate[0],todate[1]*1).toString();
    if(daysInMonth.length<2){daysInMonth= "0"+daysInMonth;}
    let dt = todate[0]+"-"+todate[1]+"-"+daysInMonth;
    let dr="YES";
	var exps = document.getElementById("expedientes").value;
    var gens = exps.split("~"); //INM|1163322,1163387~
    for (let i=0;i<gens.length;i++){
        if(gens[i]){
            let gen = gens[i].split("|");
            let clase = "";
            if (gen[0]=="INM"){clase="inmueble";}
            if (gen[0]=="EMP"){clase="empresa";}
            if (gen[0]=="AGU"){clase="agua";}
            if (gen[0]=="PUE"){clase="puesto";} 
            await fetchData(clase,dt,dr,gen[1]);
        }
        if(i==gens.length-1){
            openExpedienteEstado();
            document.getElementById("guardar").style.display="block";
        }
    }
}
function openExpedienteEstado(){
    let cid = document.getElementById('i8_contribuyente_id').value;
    let winame 	= "ExpedienteEstados";
	if (!OpenWindows[winame] || OpenWindows[winame].closed){
		OpenWindows[winame]= window.open("../pl/contribuyente.expediente.estado.rept.pl?i8_contribuyente_id="+cid,"ExpedienteEstadoRept","left=150,top=100,width=900,height=600,scrollbars=1,toolbar=0,menubar=0,resizable=1,location=0,status=0");
	}else{OpenWindows[winame].focus();}
}
//--------------------------------------------------------//
// LEVEL 2

//-------------------------------------------------------//
//-------------------------------------------------------//
// AUXILIAR
function getDaysInMonth(year, month) {
  return new Date(year, month, 0).getDate();
}
function fetchData(clase,dt,dr,id){
    return new Promise(resolve => {
		var r = new XMLHttpRequest();
		r.open("GET","../bin/gmswsp.pl?clase="+clase+"&dt_vencimiento="+dt+"&dr="+dr+"&id="+id, true);
		r.setRequestHeader("Content-Type", "application/json");
		r.send();	
		r.onreadystatechange = function () {
			if (r.readyState != 4 || r.status != 200) return;
			//console.log(endPoint);			
			console.log(r.responseText);
			resolve(r.responseText);
		};		
    }); 
}
