/* AUTEUR: Albyor */
/* DATE DE CREATION: 19/10/2005 */
/* -------------------------------- */
/* Initialisation de l'objet taxe   */
/* -------------------------------- */
function TaxeInitialise()
{
  this.effectif = 0 ;
  this.effectif_jeunes = 0 ;
  this.jours_OQ = 0 ;
  this.jours_CM = 0 ;
  this.jours_CS = 0 ;
  this.type_dossier=0 ;
  this.MS=0 ;
  this.TB=0 ;
  this.CSA=0 ;
  this.app=false ;
  this.nbapp=0 ;
  this.nbapp_annee=0 ;
  this.FNDMA=0 ;
  this.CDA = 0 ;
  this.solde_quota = 0 ;
  this.quota = 0 ;
  this.bareme = 0 ;
  this.message='' ;
  this.Frais_Stage = 0 ;
  this.Deja_Verse = 0 ;
  this.taxe_nette = 0 ;
  this.taxe_nette_reelle = 0 ; // Après déduction de l'acompte
  this.taxe_nette_ttc = 0 ;
  this.centimes_totaux=0 ;
  this.total_deductions = 0 ;
  this.FS_OQ = 0 ;
  this.FS_CM = 0 ;
  this.FS_CS = 0 ;
  this.DV_Quota_obl = 0 ;
  this.DV_Quota_nonobl = 0 ;
  this.DV_OQ = 0 ;
  this.DV_CM = 0 ;
  this.DV_CS = 0 ;
  this.acompte = 0 ;
  this.exoneration_forcee = 'non' ;
  this.plafond_ac = 0 ;

  this.est_soumis_bareme = function() {
    return(this.TB>plafond_soumission_bareme) ;
  }


  this.calcul=function() {

    this.message='';

    //--------------------------------------------------------------------------
    // on vérifie si l'entreprise est éxonérée
    //--------------------------------------------------------------------------
    if (this.nbapp_annee !=0 && this.MS<=plafond_masse_salariale && this.MS != 0) {
      this.exonere = true;
      this.message = this.message+"Vous êtes exonéré de déclaration et de versement de taxe d\'apprentissage ..." + '\n';
    }
    else if(this.MS==0) {
      this.exonere = true;
    }
    else if(this.exoneration_forcee=='oui') {
      this.exonere = true;
    }
    else {
      this.exonere = false;
    }

    //--------------------------------------------------------------------------
    // on calcule la taxe brute
    //--------------------------------------------------------------------------
    this.TB = taxe_brute(this.MS, this.type_dossier, this.effectif, this.effectif_jeunes);
    this.CSA = calcul_CSA(this.MS, this.type_dossier, this.effectif, this.effectif_jeunes);
    this.CDA = Calcul_CDA(this.MS);
    this.FNDMA = FondsNationalDevApp(this.TB, this.type_dossier);
    this.hors_quota = CalculBareme(this.TB, this.type_dossier,this.FNDMA);
    this.solde_quota = Calcul_solde_quota(this.TB, this.FNDMA, this.hors_quota);
    this.quota =  new Calcul_Quota(this.nbapp, this.solde_quota);
    this.bareme = new calcul_plafond(this.hors_quota, this.nbapp, this.type_dossier,this.solde_quota);
    this.Frais_Stage = new Calcul_Frais_Stage(this.TB,this.jours_OQ,this.jours_CM,this.jours_CS,this.FS_OQ,this.FS_CM,this.FS_CS,this.type_dossier);
    this.Deja_Verse = new Calcul_DV(this.DV_Quota_obl,this.DV_Quota_nonobl,this.DV_OQ,this.DV_CM,this.DV_CS,this.bareme,this.quota,this.nbapp);
    this.Solde = new Calcul_Solde(this.type_dossier, this.bareme, this.quota, this.Deja_Verse, this.Frais_Stage, this.exonere);
	
    // Correctif final du à la majoration qui part au FNDMA 
    //this.FNDMA = FondsNationalDevAppComplet(this.FNDMA,this.TB,this.TB_majoree) ; 
    //this.TB = this.TB_majoree ;
	// Fin correctif
    
    this.taxe_nette = Calcul_taxe_nette2(this.CDA,this.FNDMA,this.solde_quota,this.Solde.OQ,this.Solde.CM,this.Solde.CS,this.CSA) ;
    this.taxe_nette_reelle = Calcul_taxe_nette_reelle(this.taxe_nette,this.acompte);
    this.taxe_nette_ttc = Calcul_taxe_nette_ttc(this.taxe_nette);
    this.TVA = Calcul_tva(this.taxe_nette,this.taxe_nette_ttc);
    this.plafond_ac = Math.floor(0.2*this.hors_quota) ;
    
 	if(has_majoration(this.effectif,this.effectif_jeunes)) this.message = this.message+"Vous êtes soumis à majoration en raison d\'un nombre insuffisant de contrats d'apprentissage ou de contrats de professionnalisation..." + '\n';
    
    //--------------------------------------------------------------------------
    // Si l'entreprise est exonérée, on remet tout à 0
    //--------------------------------------------------------------------------
    if (this.exonere == true)
    {
      this.taxe_nette = 0;
      this.TB = 0;
      this.CDA = 0;
      this.FNDMA = 0;
      this.CSA = 0;
      this.solde_quota = 0 ;
      this.hors_quota = 0;
      this.bareme.OQ = 0;
      this.bareme.CM = 0;
      this.bareme.CS = 0;
      this.quota.quota_oblig = 0;
      this.quota.quota_non_oblig = 0;
      this.quota.part_apprenti = 0;
      this.quota.diff_forf_app = 0;
      this.CDA = 0;
	  this.taxe_nette_reelle = 0 ;
	  this.taxe_nette_ttc = 0 ;
	  this.TVA = 0 ;
    }
  }
}

//------------------------------------------------------------------------------
// Calcul de la taxe brute
//------------------------------------------------------------------------------
function taxe_brute(masse_salariale, typedossier) {
  if (typedossier==5) { // est
		return Math.round(masse_salariale * taux_taxe_brute_est);
  }
   else if (typedossier==3) { // outremer
		return Math.round(masse_salariale * taux_taxe_brute_outremer);
  } 
  else { // régime général
		return Math.round(masse_salariale * taux_taxe_brute);
  }
}


//------------------------------------------------------------------------------
//Calcul CSA
//------------------------------------------------------------------------------
function calcul_CSA(masse_salariale, typedossier, my_effectif, my_effectif_jeunes) {
	if((my_effectif >= seuil_gr_ent) && (my_effectif_jeunes<Math.floor(my_effectif*seuil_jeunes_gr_ent))) {
		if (typedossier==5) { // est
			return Math.round(masse_salariale * taux_csa_est);
		} else if (typedossier==3) { // outremer
			return Math.round(masse_salariale * taux_csa_outremer);
		} else { // régime général
			return Math.round(masse_salariale * taux_csa);
		}
	} else {
		return 0;
	}
}


//------------------------------------------------------------------------------
// Calcul CDA
//------------------------------------------------------------------------------
function Calcul_CDA(MS) {
  return Math.round(MS * taux_CDA);
}

//------------------------------------------------------------------------------
// Calcul FNDMA
//------------------------------------------------------------------------------
function FondsNationalDevApp(TB, typedossier) {
  var fndma = 0;
  if (typedossier == 5) { // est
    fndma = Math.round(TB * taux_FNDMA_est );
  }
  else if (typedossier == 3) { // outremer
    fndma = Math.round(TB * taux_FNDMA_outremer );
  }
  else { // complet
    fndma = Math.round(TB * taux_FNDMA );
  }  
  return fndma;
}


//------------------------------------------------------------------------------
// Calcul BAREME
//------------------------------------------------------------------------------
function CalculBareme(TB, typedossier) {
  if (typedossier == 5) { // est
    return Math.round(TB * taux_Bareme_est );
  }
  else if (typedossier == 3) { // outremer
    return Math.round(TB * taux_Bareme_outremer );
  }
  else { // régime général
    return Math.round(TB * taux_Bareme );
  }
}

//------------------------------------------------------------------------------
// Calcul SOLDE QUOTA
//------------------------------------------------------------------------------
function Calcul_solde_quota(mytaxebrute, FNDMA, Bareme) {
  return(mytaxebrute - Bareme - FNDMA);
}

//------------------------------------------------------------------------------
// Calcul du bareme
//------------------------------------------------------------------------------
function calcul_plafond(Bareme, nbapp, typedossier, solde_quota) {
  this.part_apprenti = 0;
  if (typedossier==5)  { // est
    this.OQ = 0;
    this.CM = 0;
    this.CS = 0;
  }
  else if (typedossier==3)  { // outremer
    this.OQ = parseInt(Math.round((parseInt(Bareme) * 40) / 100)) ;
    this.CM = parseInt(Math.round((parseInt(Bareme) * 40) / 100)) ;
    this.CS = parseInt(Bareme) - parseInt(this.OQ) - parseInt(this.CM);
  }
  else { // complet
    this.OQ = parseInt(Math.round((parseInt(Bareme) * 40) / 100)) ;
    this.CM = parseInt(Math.round((parseInt(Bareme) * 40) / 100)) ;
    this.CS = parseInt(Bareme) - parseInt(this.OQ) - parseInt(this.CM);
  }
}

//------------------------------------------------------------------------------
// Calcul des soldes
//------------------------------------------------------------------------------
function Calcul_Solde(type_dossier, plaf_bareme, plaf_quota, DV, FS, exonere) {
  if (exonere == true) {
    this.quota_oblig = 0 ;
    this.quota = 0 ;
    this.OQ = 0 ;
    this.CM = 0 ;
    this.CS = 0 ;
  }
  else {
    this.quota_oblig = Number(plaf_quota.quota_oblig) - Number(DV.Quota_Obl) ;
    this.quota = Number(plaf_quota.quota_non_oblig) - Number(DV.Quota_NonObl) ;
	if(Number(plaf_bareme.OQ) - Number(DV.OQ) - Number(FS.OQ)>=0) {
    	this.OQ = Number(plaf_bareme.OQ) - Number(DV.OQ) - Number(FS.OQ) ;
    }
    else {
    	this.OQ = 0 ;
    }
	if(Number(plaf_bareme.CM) - Number(DV.CM) - Number(FS.CM)>=0) {
    	this.CM = Number(plaf_bareme.CM) - Number(DV.CM) - Number(FS.CM) ;
    }
    else {
    	this.CM = 0 ;
    }
	if(Number(plaf_bareme.CS) - Number(DV.CS) - Number(FS.CS)>=0) {
    	this.CS = Number(plaf_bareme.CS) - Number(DV.CS) - Number(FS.CS) ;
    }
    else {
    	this.CS = 0 ;
    }
  }
}

//------------------------------------------------------------------------------
// Calcul du Quota
//------------------------------------------------------------------------------
function Calcul_Quota(nbapp, solde_quota) {
  if (nbapp==0) {
    this.quota_oblig = 0 ;
    this.quota_non_oblig = solde_quota ;
    this.diff_forf_app = 0 ;
    this.part_apprenti = 0 ;
  }
  else {
    if ((nbapp * forfait_app) > solde_quota) {
      this.quota_oblig = solde_quota;
      this.quota_non_oblig = 0;
      this.part_apprenti = parseInt(solde_quota / nbapp);
      this.diff_forf_app = solde_quota - (this.part_apprenti * nbapp);
    }
    else {
      this.quota_oblig = (nbapp * forfait_app) ;
      this.quota_non_oblig = solde_quota - this.quota_oblig;
      this.part_apprenti = forfait_app;
      this.diff_forf_app = 0;
    }
  }
  this.total = Number(this.quota_oblig) + Number(this.quota_non_oblig);
}

//------------------------------------------------------------------------------
// Calcul Frais de stage
//------------------------------------------------------------------------------
function Calcul_Frais_Stage(TB,jours_OQ,jours_CM,jours_CS,montant_OQ,montant_CM,montant_CS,type_dossier) {

  this.max = Math.round(TB * 0.04) ;
  if(type_dossier==5) { // est
    this.max = 0 ;
  }
  if (jours_OQ>0) {
    this.OQ_norm = jours_OQ * montant_jour_stag_OQ ;
    this.OQ = jours_OQ * montant_jour_stag_OQ ;
  }
  else {
    this.OQ_norm = montant_OQ ;
    this.OQ = montant_OQ ;
  }
  if (jours_CM>0) {
    this.CM_norm = jours_CM * montant_jour_stag_CM;
    this.CM = jours_CM * montant_jour_stag_CM;
  }
  else {
    this.CM_norm = montant_CM;
    this.CM = montant_CM;
  }
  if (jours_CS>0) {
    this.CS_norm = jours_CS * montant_jour_stag_CS;
    this.CS = jours_CS * montant_jour_stag_CS;
  }
  else {
    this.CS_norm = montant_CS;
    this.CS = montant_CS;
  }

  this.total = Number(val(this.OQ)) + Number(val(this.CM)) + Number(val(this.CS)) ;
  if (this.total > this.max) {
    this.pourcentage_OQ = (this.OQ / this.total) * 100 ;
    this.pourcentage_CM = (this.CM / this.total) * 100 ;
    this.pourcentage_CS = (this.CS / this.total) * 100 ;
    this.total = this.max;
    this.OQ = parseInt(Math.round((this.total * this.pourcentage_OQ) / 100)) ;
    this.CM = parseInt(Math.round((this.total * this.pourcentage_CM) / 100)) ;
    this.CS = parseInt(Math.round((this.total * this.pourcentage_CS) / 100)) ;

    if (this.pourcentage_CS > 0) {
      this.CS = this.CS + (this.max - this.OQ - this.CM - this.CS) ;
    }
    else {
      this.OQ = this.OQ + (this.max - this.OQ - this.CM);
    }
  }

}

//------------------------------------------------------------------------------
// Vérification des Déjà versés
//------------------------------------------------------------------------------
function Calcul_DV(DV_Quota_obl,DV_Quota_nonobl,DV_OQ,DV_CM,DV_CS,bareme,quota,nb_app) {
  if(Number(val(DV_Quota_obl)) + Number(val(DV_Quota_nonobl)) > Number(quota.total)) {
    if (val(nb_app)>0) {
      this.Quota_Obl = quota.total;
    }
    else {
      this.Quota_Obl = 0;
    }
    if (Number(quota.total)> Number(this.Quota_Obl)) {
      this.Quota_NonObl = quota.total - this.Quota_Obl;
    }
    else {
      this.Quota_NonObl = 0 ;
    }
  }
  else {
    if (val(nb_app)>0) {
      if (val(DV_Quota_nonobl) >0 && val(DV_Quota_obl)==0)
      {
        this.Quota_NonObl = DV_Quota_nonobl;
        this.Quota_Obl = 0;
      }
      else {
        this.Quota_Obl = DV_Quota_obl;
        this.Quota_NonObl = DV_Quota_nonobl ;
      }
    }
    else {
      if (val(DV_Quota_obl) >0 && val(DV_Quota_nonobl)==0) {
        this.Quota_Obl = 0;
        this.Quota_NonObl = DV_Quota_obl;
      }
      else {
        this.Quota_Obl = DV_Quota_obl;
        this.Quota_NonObl = DV_Quota_nonobl ;
      }
    }
  }

  if(Number(DV_OQ)>bareme.OQ) {
    this.OQ = bareme.OQ;
  }
  else {
    this.OQ = DV_OQ;
  }
  if(Number(DV_CM)>bareme.CM) {
    this.CM = bareme.CM;
  }
  else {
    this.CM = DV_CM;
  }
  if(Number(DV_CS)>bareme.CS) {
    this.CS = bareme.CS ;
  }
  else
  {
    this.CS = DV_CS ;
  }
  this.total = Number(this.Quota_Obl) + Number(this.Quota_NonObl) + Number(this.OQ) + Number(this.CM) + Number(this.CS);
}
//------------------------------------------------------------------------------
// Calcul de la taxe nette
//------------------------------------------------------------------------------
/*
function Calcul_taxe_nette(TB,CDA,FS_TOTAL,DV_TOTAL) {
  return this.taxe_nette = TB + CDA - FS_TOTAL - DV_TOTAL;
}
*/
//------------------------------------------------------------------------------
function Calcul_taxe_nette2(CDA,FNDMA,SOLDE_QUOTA,SOLDE_A,SOLDE_B,SOLDE_C,CSA) {
  return this.taxe_nette = CSA + CDA + FNDMA + SOLDE_QUOTA + SOLDE_A + SOLDE_B + SOLDE_C ;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Calcul de la taxe nette relle
//------------------------------------------------------------------------------
function Calcul_taxe_nette_reelle(TN,AC) {
  if(TN >= AC) {
    return this.taxe_nette_reelle = TN - AC
  }
  else {
    return this.taxe_nette_reelle = 0 ;
  }
}
//------------------------------------------------------------------------------


function Calcul_taxe_nette_ttc(TN) {
  return this.taxe_nette_ttc = Math.round(TN*100*1.196)/100 ;
}

function Calcul_tva(TN,TNTTC) {
  return this.TVA = Math.round((TNTTC - TN)*100)/100 ;
}


function has_majoration(my_effectif,my_effectif_jeunes) {
	return((my_effectif >= seuil_gr_ent) && (my_effectif_jeunes<Math.floor(my_effectif*seuil_jeunes_gr_ent))) ;
}

//------------------------------------------------------------------------------
function val(expression) {
   if (isNaN(expression) || expression=='') {
      return 0;
   }else{
      return expression;
   }
}
