#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <time.h>
#define NONBLOC 35
#include "Controle.h"

/* Variables globales au module de controle */
//short cpterrs=0;        /* Compteur des erreurs */
//short pervec=0 ;        /* position dans le vecteur d'erreur */
//short persc=0;          /* position du nombre d'erreur dans le RSS */
//short flag=0;           /* Indique le depassement de MAXVEC */

/* short contfac[NONBLOC]={1,4,5,6,7,17,18,21,22,23,24,25,26,27,28,29,30,31,33,34,35,36,37,38,39,40,41,44,64,69,70,71,72,73,83}; */
short contfac[NONBLOC]={1,4,5,6,7,17,18,21,22,23,24,25,26,27,28,29,30,31,33,34,35,36,37,38,39,40,41,44,64,69,70,71,72,73,82};


/*CD 30/06/2008 : controle du format de diagnostic*/
int controle_diag(char *zone)
{
    if(zone==0)
        return 0;
    if (strncmp(zone,"ZAIGU ",6)==0)
        return 0;
    return contfmt(zone,"X99++Z",0,3);
}

void testcpt(short *flag)
{
    if (cpterrs >= MAXVEC)*flag=1;
	else *flag=0;
}

//CD 02/01/07
void InitErreur(short vecerr[])
{   
    int i;
    for(i=0;i<MAXVEC;i++)
        vecerr[i]=0;
    cpterrs=0;
    flag=0;
}

/******************************************************************************
Fonction controle
Description:
Fonction contôlant la réalisation du controle d’un RSS :
	- Appel de la date système (fonction calldate)
	- Lancement de la fonction de contrôle des champs des RUMs (ctlsej)
	- Mise à jour de cmc - ghj - Code Retour .
******************************************************************************/
short controle(short vecerr[],short *cret,short *cmc,short *ghj)
{

	short i,j,f;
	short ret;

	/* PHASE D'INITIALISATION */
    *cmc=90;
	*ghj=900;
	for(i=0;i<MAXVEC;i++)vecerr[i]=0;
    /*CD 02/01/07 fait dans InitErreur
    cpterrs=0;
    flag=0;*/
    calldate(dateptr);
	/* TRAITEMENT DE CONTROLE DU SEJOUR HOPITAL */
	ctlsej(vecerr);

	/* Test des erreurs obligatoires non fait */
	ret=0;
    for(i=0;i<MAXVEC;i++)
	{
      f=0;
	  for(j=0;j<NONBLOC;j++)
		  if (vecerr[i]==contfac[j])
		  {
              f=1;break;
          };
	  if (f==0)
		  if((ret=vecerr[i])!=0) break;
    }
    *cret=ret;

	return(ret) ;

} /* FIN DE LA FONCTION CONTROLE */


short ctlsej(short vecerr[])
{
  short i,ret,ret1,ret2,ret3;
  /*CD 05/10/07 char zone[48], dumdiag[7];*/
  char zone[NBDAS*6+1];
  //char datlun[9];
  char dumdiag[7];
  //dlundi_s(ris.nosem,datlun);
  
    if((ret=fcnf(ris.finess))!=0)majvecerr(vecerr,ret);
    if((ret=fcnr(ris.nossr ))!=0)majvecerr(vecerr,ret);
/*
    if((ret=fcass(ris.insee))!=0)majvecerr(vecerr,ret);
    if((ret=fcrgb(ris.rbenf))!=0)majvecerr(vecerr,ret);
*/
    if((ret=fcth(ris.typhosp))!=0)majvecerr(vecerr,ret);
    if((ret2=ret=fcde(ris.datent,ris.typhosp))!=0)majvecerr(vecerr,ret);
    if((ret1=ret=fcdn(ris.naiss,ris.nosem))!=0)majvecerr(vecerr,ret);
    if((ret=fcsx(ris.sexe))!=0)majvecerr(vecerr,ret);
    if((ret=fcme(ris.modent,ris.typhosp))!=0)majvecerr(vecerr,ret);
    if((ret=fcpr(ris.prov,ris.modent,ris.typhosp))!=0)majvecerr(vecerr,ret);
    if((ret3=ret=fcds(ris.datesor,ret2,ris.datent))!=0)majvecerr(vecerr,ret);
    if((ret=fcms(ris.modsor,ris.datesor))!=0)majvecerr(vecerr,ret);
    if((ret=fcdst(ris.dest,ris.modsor,ris.typhosp))!=0)majvecerr(vecerr,ret);
    if((ret=fccp(ris.codepost))!=0)majvecerr(vecerr,ret);
    if((ret=fcnosm(ris.nosem,ret2,ris.datent,ret3,ris.datesor,ris.typhosp))!=0)majvecerr(vecerr,ret);
    strncpy(zone,ris.nbjhwe,7);
    if((ret=fcjpr(zone))!=0)majvecerr(vecerr,ret);

    if ((ret=fcus(ris.um))!=0)majvecerr(vecerr,ret);
    if ((ret=fcchi(ris.datchir,ret2,ris.nosem))!=0)majvecerr(vecerr,ret);
    if ((ret=fcdpc(ris.diagpc,prof_diags,profctl[0]))!=0)majvecerr(vecerr,ret);
    if ((ret=fcdp(ris.diagp,ris.diagpc,prof_diags+_dxmsize,profctl[1]))!=0)majvecerr(vecerr,ret);
    if ((ret=fcdge(ris.diage,ris.diagpc,ris.diagp,prof_diags+2*_dxmsize,profctl[2]))!=0)majvecerr(vecerr,ret);

    /*CD 05/01/07
    strncpy(zone,ris.diags1,30);*/
    strncpy(zone,ris.diags,nbdiagas*6);
    //for(i=0;i<5;i++){
    for(i=0;i<nbdiagas;i++)
    {
       strncpy(dumdiag,zone+i*6,6);dumdiag[6]=0;
       if((ret=fcdgs(dumdiag,prof_diags+(i+3)*_dxmsize,profctl[i+3]))!=0)
           majvecerr(vecerr,ret);
    }
    strncpy(zone,&ris.dephab,6);
    for(i=0;i<6;i++){
       if((ret=fcdep(zone[i]))!=0) majvecerr(vecerr,ret);
    }
    if((ret=fcfrou(ris.fautrou))!=0) majvecerr(vecerr,ret);
    for(i=0;i<12;i++){
       if((ret=fcreed(ris.procreed[i]))!=0)
           majvecerr(vecerr,ret);
    }
    if((ret=fcnba(ris.nbactes))!=0)majvecerr(vecerr,ret);

    for(i=0;i<nbactes;i++)
	{
	   if (lgacte==4)
           if((ret=fcac(tab_actes[i].arr,prof_actes[i]))!=0) majvecerr(vecerr,ret);
	   if (lgacte==8)
           if((ret=fcaccam(tab_actes[i].arr,prof_actes[i]))!=0) majvecerr(vecerr,ret);
    }
    return(0);

} /* FIN DE LA FONCTION CTLSEJ */


void majvecerr(short vecerr[],short ret)
{
    testcpt(&flag);
    if(!flag)
    {
     vecerr[cpterrs++]=ret;
    }
}

/********************************************/
/* FONCTION DE CONTROLE DU NUMERO DE FINESS */
/********************************************/
short fcnf (char finess[])
{
	short ret;

	if((ret=contfmt(finess,"9F9999999",0,9))!=0)
         {
          switch(ret) {
             case 1 :
             case 2 : return(1);
             case 3 : return(1);
             }
         }

return(0);
}

/*****************************************/
/* FONCTION DE CONTROLE DU NUMERO DE RIS */
/*****************************************/
short	fcnr (char nossr[])
{
	short ret;

	if((ret=contfmt(nossr,"9999999",1,0))!=0)
          switch(ret) {
             case 1 : return(2);
             case 2 :
             case 3 : return(3);
             }

return(0);
} /* fin de la fonction fcnr */

/****************************/
/* CONTROLE DU NUMERO INSEE */
/****************************/
short fcass(char zone[])
{
	short ret;

	if((ret=contfmt(zone,"9999999999999",0,13))!=0)
         {
          switch(ret) {
             case 1 :
             case 2 : return(4);
             case 3 : return(4);
             }
         }
    if(zone[0]!='1' && zone[0]!='2') return(5);

return(0);
}

/************************************/
/* CONTROLE DU RANG DU BENEFICIAIRE */
/************************************/
short fcrgb(char zone[])
{

   short ret;

	if((ret=contfmt(zone,"999",0,3))!=0)
    {
          switch(ret) {
             case 1 :
             case 2 : return(6);
             case 3 : return(6);
             }
    }

    if(contval(zone,"010203040506070809",2)==0)return(7);
    if (zone[1]-'0' < 4 || zone[1]=='9')
         if(zone[2]!='0')return(7);
    return(0);
}

/***********************************************/
/*        CONTROLE DU TYPE D'HOSPITAL          */
/***********************************************/
short fcth (char zone)
{

	/* TYPHOSP ABSENT */
	if(contfmt(&zone,"9",0,1)!=0) return(8);

	/* VALEUR ERRONEE DU TYPE HOSPI */
	/* CD 08/01/07 ajout de type d'activité
    if(contval(&zone,"1234",1)==0)*/
    if(contval(&zone,"12345678",1)==0)
    return(9);

return(0);
} /* fin de la fonction fcth */

/************************************************/
/* FONCTION DE CONTROLE DE LA DATE DE NAISSANCE */
/************************************************/
short fcdn (char dn[],char nosem[])
{
	short ret;
    //char datlun[9];
	long dnbj;

	if((ret=contfmt(dn,"Z9Z99999",1,7))!=0)
	{
          switch(ret) {
             case 1 : return(10);
             case 2 : return(11);
             case 3 : return(11);
             }
	}

	if(contdate(dn)!=0) return(12);

    //dlundi_s(nosem,datlun);
    /*CD 08/01/07 PB avec les naissances dans la semaine indiquée
    if((dnbj=nombre_jour_1860(datlun)-nombre_jour_1860(dn))<0 )*/
    if((dnbj=nombre_jour_1860(datlun)-nombre_jour_1860(dn))<-6 )
        return(13);
    if ( (long)(dnbj/365) > 120L)
        return(14);

   return(0);

} /* fin de la fonction fcdn */

/*************************************/
/* FONCTION DE CONTROLE DU CODE SEXE */
/*************************************/
short fcsx (char zone)
{
        short ret;

	/* CODE SEXE ABSENT */
	if((ret=contfmt(&zone,"9",0,1))!=0) return(15);

	/* VALEUR ERRONEE DU CODE SEXE */
	if(contval(&zone,"12",1)==0) return(16);

return(0);
} /* fin de la fonction fcsx */

/***********************************************/
/* FONCTION DE CONTROLE DU CODE UNITE DE SOINS */
/***********************************************/
short fcus (char zone[])
{

    short ret;

	if((ret=contfmt(zone,"9999",3,0))!=0)
          switch(ret) {
             case 1 :
             case 2 : return(17);
             case 3 : return(0); /* Suppression du code erreur 18 */
             }

return(0);
} /* fin de la fonction fcus */

/********************************************/
/* FONCTION DE CONTROLE DE LA DATE D'ENTREE */
/********************************************/
short fcde (char zone[],char typh)
{

    short off;

    off=0;
    /*CD 18/06/2008
    if(typh!='1' || typh!='5')*/
    if(typh!='1' && typh!='5')
       if (strncmp(zone,"        ",8)==0) return(0);

	if(contfmt(zone,"Z9Z99999",1,7)!=0) return(19);

	if(contdate(zone)!=0) return(20);

    if(nombre_jour_1860(dateptr) - nombre_jour_1860(zone) < 0L) return(21);

    if(strncmp(zone+4,"1990",4)<=0) return(22);

return(0);
} /* fin de la fonction fcde */

/*****************************************/
/* FONCTION DE CONTROLE DU MODE D'ENTREE */
/*****************************************/
short fcme (char zone,char typh)
{
    short ret;

	/* MODE D'ENTREE ABSENT */
	/*CD 18/06/2008
    if(typh!='1' || typh!='5')*/
    if(typh!='1' && typh!='5')
       if (zone==' ') return(0);

	if((ret=contfmt(&zone,"9",3,1))!=0)
	{
          switch(ret) {
             case 1 :
             case 2 : return(23);
             case 3 : return(23);
             }
	}

	if(contval(&zone,"0678",1)==0) return(24);

return(0);
} /* fin de la fonction fcme */

/*********************************************/
/* FONCTION DE CONTROLE DE LA PROVENANCE     */
/*********************************************/
short fcpr (char prov,char modent,char typhosp)
{
  switch (modent)
  {
    case '0':
    case '6':
    case '7':
      if(prov==' ') return(25);
      if(contval(&prov,"1234",1)==0)return(26);
      break;
    case '8' :
      if(contval(&prov," 567",1)==0)return(26);
	  break;
    case ' ' :
      /*CD 18/06/2008
      if(typhosp!='1' || typhosp!='5')if (prov!=' ')return(27);*/
      if(typhosp!='1' && typhosp!='5')if (prov!=' ')return(27);
      break;
    default :
       if(contval(&prov," 1234567",1)==0)return(27);
  }
  return(0);
 }
/*********************************************/
/* FONCTION DE CONTROLE DE LA DATE DE SORTIE */
/*********************************************/
short fcds (char zone[],short ret2,char datent[])
{
  short ret;

    if (strncmp(zone,"        ",8)==0) return(0);

	if((ret=contfmt(zone,"Z9Z99999",1,7))!=0)
	{
          switch(ret) {
             case 1 : return(29); /* remplacement du code 28 par 29 */
             case 2 : return(29);
             case 3 : return(30);
             }
	}
	if(contdate(zone)!=0) return(31);

/* Si la date d'entrée est bonne */
    if(!ret2)
    {
       if (strncmp(datent,"        ",8)==0)return(0);
       if(nombre_jour_1860(zone) - nombre_jour_1860(datent) < 0L) return(32);
    }

    if(nombre_jour_1860(dateptr) - nombre_jour_1860(zone) < 0L) return(33);

return(0);
} /* fin de la fonction fcds */

/******************************************/
/* FONCTION DE CONTROLE DU MODE DE SORTIE */
/******************************************/
short fcms (char zone,char datesor[])
{
    short ret;

	if(strncmp(datesor,"        ",8)==0) if (zone==' ') return(0);

	/* MODE DE SORTIE ABSENT */
	if((ret=contfmt(&zone,"9",3,1))!=0)
	{
          switch(ret) {
             case 1 : return(34);
             case 2 : return(34);
             case 3 : return(35);
             }
	}

	if(contval(&zone,"06789",1)==0) return(36);

return(0);

} /* fin de la fonction fcms */

/*********************************************/
/* FONCTION DE CONTROLE DE LA DESTINATION    */
/*********************************************/
short fcdst (char dest,char modsor,char typhosp)
{
  switch (modsor){
    case '0':
    case '6':
    case '7':
      if(dest==' ') return(37);
      if(contval(&dest,"1234",1)==0)return(38);
      break;
    case '8' :
		if(contval(&dest," 567",1)==0)return(39);
		break;
    case '9' :
      if (dest!=' ')return(39);
	  break;
    case ' ' :
    /*CD 18/06/2008
      if(typhosp!='1' || typhosp!='5') if (dest!=' ')return(39);*/
      if(typhosp!='1' && typhosp!='5') if (dest!=' ')return(39);
      break;
    default :
      if(contval(&dest," 1234",1)==0)return(39);
  }
/*
  if(modsor=='6' || modsor=='7'){
       if(dest==' ') return(37);
       if(contval(&dest,"1234",1)==0)return(38);
  }
  if(modsor=='8' || modsor=='9') if(dest!=' ')return(39);
*/

  return (0);
}

/*********************************************/
/* FONCTION DE CONTROLE CODE POSTAL          */
/*********************************************/
short fccp(char codep[])
{
    short ret;
	if((ret=contfmt(codep,"99999",0,5))!=0)
	{
          switch(ret) {
             case 1 : return(40);
             case 2 : return(40);
             case 3 : return(41);
             }
	}
return(0);
}

/*********************************************/
/* FONCTION CONTROLE NUMERO DE SEMAINE       */
/*********************************************/
// CG 31/01/2007
short contnumsem(char nosem[])
{
	char ns[3];
	char ans[5],ancour[5];

	strncpy(ns,nosem,2);ns[2]=0;
	strncpy(ans,nosem+2,4);ans[4]=0;
	if (atoi(ns)<1 || atoi(ns)>53) return(42);
	strncpy(ancour,dateptr+4,4);ancour[4]=0;
	if(atoi(ans) < 1990) return(44);
	return (0);
}

/*********************************************/
/* FONCTION DU NUMERO DE SEMAINE             */
/*********************************************/
short fcnosm(char nosem[],short ret2,char datent[],short ret3,char datsor[],char typh)
{
   long nsris;
   char ns[3];
   char ans[5],ancour[5];

  strncpy(ns,nosem,2);ns[2]=0;
  strncpy(ans,nosem+2,4);ans[4]=0;
 if (atoi(ns)<1 || atoi(ns)>53) return(42);
  strncpy(ancour,dateptr+4,4);ancour[4]=0;
  if(atoi(ans) > atoi(ancour)) return(43);
  if(atoi(ans) < 1990) return(44);
 
  if(typh=='1' || typh=='5')
  {
     nsris=atoi(ans)*100L + atoi(ns);
     if (ret2!=0)
        if(nsris < no_semaine(datent)) return(45);
     if(ret3!=0)
        if(nsris > no_semaine(datsor)) return(46);
  }
  return(0);
}
/***********************************************/
/* FONCTION DE CONTROLE INDICATEUR DE PRESENCE */
/***********************************************/
short fcjpr(char zone[])
{
int nbjpres=0,i;

  for(i=0;i<7;i++){
    if(contval(zone+i," 01",1)==0)return(47);
    if(zone[i]=='1')  nbjpres ++;
  }
  if(nbjpres==0) return(47);
  return(0);
}
/***********************************************/
/*  FONCTION DE CONTROLE DATE CHIR             */
/***********************************************/
short fcchi (char zone[],short ret2,char nosem[])
{
   char ans[5],ns[3];
   long nsris;

   if (strncmp(zone,"        ",8)==0)return(0);
   if(contdate(zone)!=0) return(48);
   strncpy(ns,nosem,2);ns[2]=0;
   strncpy(ans,nosem+2,4);ans[4]=0;
   nsris=atoi(ans)*100L + atoi(ns);
   if (nsris < no_semaine(zone)) return(49);

   return(0);
}

/***********************************************/
/* FONCTION DE CONTROLE FINALITE PRINC. DE PC  */
/***********************************************/
short fcdpc (char zone[],unsigned char *masque,unsigned char prfct)
{
    unsigned char dz[3];
    short i;

	 if(strncmp(zone,"      ",6)==0) return(50);
     
     /*CD : controle du format du diag*/
     if(controle_diag(zone))
         return 50;

     if(toupper(zone[0])!= 'Z') return(51);
     if (strncmp(zone,"ZAIGU ",6)==0) return(0);
     
     if (*masque==0) return(50);
     if (*masque==99) {
           i=0;
           dz[0]=9900/256; dz[1]=9900%255;
           /*CD 16/01/07
           while(memcmp(masque+i+1,dz,2)!=0 && i<_dxmsize)*/
           while((i<_dxmsize) && (memcmp(masque+i+1,dz,2)!=0))
               i+=2;
           if(i<_dxmsize){
              return(51);
           }
           i=0;
           dz[0]=9902/256; dz[1]=9902%255;
           /*CD 16/01/07
           while(memcmp(masque+i+1,dz,2)!=0 && i<_dxmsize)*/
           while((i<_dxmsize) && (memcmp(masque+i+1,dz,2)!=0))
               i+=2;
           if(i<_dxmsize){
              return(52);
           }
      }
      if (prfct!= 255)
         if ((prfct & 192)>>6 ==0) return(53);

     return(0);

}
/***********************************************/
/* FONCTION DE CONTROLE MANIF MORBIDE PRINC    */
/***********************************************/
short fcdp (char zone[],char diagpc[],unsigned char *masque,unsigned char prfct)
{
    unsigned char dz[3];
    short i;

	 if(strncmp(zone,"      ",6)==0) return(54);

    /*CD : controle du format du diag*/
     if(controle_diag(zone))
         return 58;

     if(toupper(zone[0])== 'U' || toupper(zone[0])=='V' || toupper(zone[0])== 'W' || toupper(zone[0])== 'X'|| toupper(zone[0])== 'Y') return(55);
     if (strncmp(diagpc,"ZAIGU ",6)==0)
        if (toupper(zone[0])=='Z') return(56) ;
     if (strncmp(zone,diagpc,6)==0)return(57);
     if (*masque==0) return(58);
     if (*masque==99){
           i=0;
           dz[0]=9900/256; dz[1]=9900%255;
           /*CD 16/01/07
           while(memcmp(masque+i+1,dz,2)!=0 && i<_dxmsize)*/
           while((i<_dxmsize) && (memcmp(masque+i+1,dz,2)!=0))
               i+=2;
           if(i<_dxmsize){
              return(59);
           }
           i=0;
           dz[0]=9902/256; dz[1]=9902%255;
           /*CD 16/01/07
           while(memcmp(masque+i+1,dz,2)!=0 && i<_dxmsize)i+=2;*/
           while((i<_dxmsize) && (memcmp(masque+i+1,dz,2)!=0))
               i+=2;
           if(i<_dxmsize)
           {
              return(60);
           }
      }
      if (prfct!= 255)
         if ((prfct & 48)>>4 ==0) return(61);

     return(0);

}

/***********************************************/
/* FONCTION DE CONTROLE DIAGNOSTIC ETIOLOG.    */
/***********************************************/
short fcdge  (char zone[],char diagpc[],char diagp[],unsigned char *masque,unsigned char prfctl)
{
    unsigned char dz[3];
    short i;
	 if (strncmp(zone,"      ",6)==0)
/*	     if(diagp[0]!='Z') return(62); else */
		 return(0);

     /*CD : controle du format du diag*/
     if(controle_diag(zone))
         return 65;

     
     if(toupper(zone[0])=='R' ||toupper(zone[0])=='U' ||toupper(zone[0])=='V' ||toupper(zone[0])=='W' ||toupper(zone[0])=='X' ||toupper(zone[0])=='Y' ||
        toupper(zone[0])=='Z' ) return(63);

     if (strncmp(zone,diagpc,6)==0 || strncmp(zone,diagp,6)==0) return(64);

     if (*masque==0) return(65);

     if (*masque==99){
           i=0;
           dz[0]=9900/256; dz[1]=9900%255;
           /*CD 16/01/07
           while(memcmp(masque+i+1,dz,2)!=0 && i<_dxmsize)i+=2;*/
           while((i<_dxmsize) && (memcmp(masque+i+1,dz,2)!=0))
               i+=2;
           if(i<_dxmsize)
           {
              return(66);
           }
           i=0;
           dz[0]=9902/256; dz[1]=9902%255;
           /*CD 16/01/07
           while(memcmp(masque+i+1,dz,2)!=0 && i<_dxmsize)i+=2;*/
           while((i<_dxmsize) && (memcmp(masque+i+1,dz,2)!=0))
               i+=2;
           if(i<_dxmsize)
           {
              return(67);
           }
      }

      if (prfctl!= 255)
         if ((prfctl & 12)>>2 ==0) return(68);

     return(0);

}

/***********************************************/
/* FONCTION DE CONTROLE DIAGNOSTIC ASSOCIE.    */
/***********************************************/
short fcdgs (char zone[],unsigned char *masque,unsigned char prfctl)
{
      unsigned char dz[3];
      short i;

     if (strncmp(zone,"      ",6)==0) return(0);

   /*  if(zone[0]=='V' || zone[0]== 'W' || zone[0]== 'X'|| zone[0]== 'Y') return(69);  */

     /*CD : controle du format du diag*/
     if(controle_diag(zone))
         return 70;
     
     if (*masque==0) return(70);
     if (*masque==99){
           i=0;
           dz[0]=9900/256; dz[1]=9900%255;
           /*CD 16/01/07
           while(memcmp(masque+i+1,dz,2)!=0 && i<_dxmsize)i+=2;*/
           while((i<_dxmsize) && (memcmp(masque+i+1,dz,2)!=0))
               i+=2;
           if(i<_dxmsize)
           {
              return(71);
           }
           i=0;
           dz[0]=9902/256; dz[1]=9902%255;
           /*CD 16/01/07
           while(memcmp(masque+i+1,dz,2)!=0 && i<_dxmsize)i+=2;*/
           while((i<_dxmsize) && (memcmp(masque+i+1,dz,2)!=0))
               i+=2;
           if(i<_dxmsize)
           {
              return(72);
           }
      }

      if (prfctl!= 255)
         if ((prfctl & 3) ==0) return(73);

   return(0);
}
/*********************************************/
/* FONCTION DE CONTROLE DU NOMBRE D' ACTES   */
/*********************************************/
short fcnba(char nba[])
{
 char dz[3];
  short ret;
    strncpy(dz,nba,2);dz[2]=0;
	if ((ret=contfmt(nba,"99",1,0))!=0)
	{
          switch(ret) {
             case 1 : return(74);
             case 3 : return(75);
             }
	}
   if (atoi(dz)<0 || atoi(dz)>10) return(76);

return(0);
}
/************************************************/
/* FONCTION DE CONTROLE DES VARIABLES DE DEP.   */
/************************************************/
short fcdep (char zone)
{
    if (contval(&zone,"1234",1)==0)return(77);
    return(0);
}
/************************************************/
/* FONCTION DE CONTROLE DU FAUTEUIL ROULANT     */
/************************************************/
short  fcfrou(char zone)
{
    if (contval(&zone,"12",1)==0) return(78);
    return(0);
}
/************************************************/
/* FONCTION DE CONTROLE DES VAR DE REEDUCATION  */
/************************************************/
short fcreed(char zone[])
{
  char dz[5];
  short ret;

   strncpy(dz,zone,4);dz[4]=0;
   if((ret=contfmt(zone,"9999",3,0))!=0){
          switch(ret) {
             case 1 : return(79);
             case 3 : return(80);
             }
   }

   return(0);
}
/************************************************/
/* FONCTION DE CONTROLE DES ACTES CDAM          */
/************************************************/
short fcac(char acte[],unsigned char profil)
{
   short ret;
	if((ret=contfmt(acte,"X999",0,4))!=0)
	{
		if(ret==1)return(0);
		return(81);
	}
    if (profil == 0)
        return(82);
/*    if (profil == 4) return(83);  */

return(0);
}

short fcaccam(char acte[],unsigned char profil)
{
   short ret;
	if((ret=contfmt(acte,"XXXX9999",0,8))!=0)
	{
		if(ret==1)
            return(0);
		return(81);
	}
    if (profil == 0)
        return(82);
return(0);
}

short contfmt (char *zone,char format[],short ft,short taille)
{
	short i,j,lg,ret,t,deb,fin;
	short pcnbg,pcnbd;
	char zone2[24],fmt2[24];

	lg=(short)(strlen(format));
	pcnbg= -1;
	pcnbd= lg+1;
	for(i=0,j=lg-1;i<lg;i++,j--)
	{
		if(*(zone+i)!=' ' && pcnbg== -1)pcnbg=i;
		if(*(zone+j)!=' ' && pcnbd== lg+1)pcnbd=j;
	}
	t=pcnbd-pcnbg+1;

	if(t>lg) return(1);

	deb=0;
	fin=lg-1;
	if(ft==1)deb=pcnbg;
	if(ft==2)fin=pcnbd;
	if(ft==3){
		deb=pcnbg;
		fin=pcnbd;
	}
	t=fin-deb+1;

	if(t<taille) return(2);

	strncpy(zone2,zone+deb,t);
	strncpy(fmt2,format+deb,t);

	ret=1;
	for (i=0;i<t;i++)
	{
		switch(*(fmt2+i)){
		case '9' :
			ret=isdigit(*(zone2+i));
			break;
		case 'Z' :
			ret=(isdigit(*(zone2+i)) || *(zone2+i)==' ');
			break;
		case 'Y' :
			ret=(isalnum(*(zone2+i)) || *(zone2+i)==' ');
			break;
		case 'A' :
			ret=isalnum(*(zone2+i));
			break;
		case 'X' :
			ret=isalpha(*(zone2+i));
			break;
        case '+' :
            ret=(isdigit(*(zone2+i)) || *(zone2+i)==' ' || *(zone2+i)=='+');
			break;
		case 'F' :
			ret=(isdigit(*(zone2+i)) || toupper(*(zone2+i))=='A' || toupper(*(zone2+i))=='B');
	        break;
		}
		if (!ret) return(3);
	}
	return(0);
}

short contvide(char *zone,short lg)
{
	short i;

	for(i=0;i<lg;i++)
		if(*(zone+i) !=' ')return(1);
	return(0);
}


short contval(char *zone,char *valeur,short lgt)
{
	short i;

	for(i=0;i<(short)(strlen(valeur));)
    {
		if(strncmp(zone,valeur+i,lgt)==0)
            return(1);
		i+=lgt ;
    }
	return(0);
}


short contdate (char *adate)
{
	short bis; /* variables de travail */
	static  short tabj[2][13] = {
				{0,31,28,31,30,31,30,31,31,30,31,30,31},
				{0,31,29,31,30,31,30,31,31,30,31,30,31}	};
	short jj,mm,aa;

	convdate(adate,&jj,&mm,&aa);
	bis= aa%4 == 0 && aa%100 !=0 || aa%400==0;
	/* controle mois   */
	if (mm < 1 || mm > 12) return(1);
	/* controle brut du jour */
	if (jj < 1 || jj > tabj[bis][mm]) return(1);
	return(0);
}

short mask_null (char *masque, short lg)
{
  short i;
  for(i=0;i<lg;i++)
    if(*(masque+i)!= 0)return(0);
  return(1);
}

void calldate (char wdate[])
{
    int i;
    char ddate[33];
    char mois[13][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
        "Sep","Oct","Nov","Dec"};
    //CD 04/01/07
    //long clock;
    time_t clock;

    strcpy(wdate,"        ");
    clock = time(0);
    strcpy(ddate,ctime(&clock));
    for(i=0;i<12;i++)
    {
        if(strncmp(ddate+4,mois[i],3)==0)break;
    }
    strncpy(wdate,ddate+8,2);
    sprintf(wdate+2,"%02.2d",i+1);
    strncpy(wdate+4,ddate+20,4);
    if(wdate[0]==' ')wdate[0]='0';
    wdate[8]='\0';
}



long nombre_jour_1860(char *date)
{
	static short tab[2][12] = {
              {0,31,59,90,120,151,181,212,243,273,304,334},
              {0,31,60,91,121,152,182,213,244,274,305,335} };
	short 	jj, mm, aa;	/* date entree en entier */
    short bis;
    long nbj;
    static long leap=450;

        convdate (date,&jj,&mm,&aa);
        nbj = 0;
        nbj=(long)(aa -1860)*365L + (((long)(aa-1)/4L) - ((long)(aa-1)/100L) + ((long)(aa-1)/400L)) - leap ;
        bis=(aa%4 == 0 && aa%100 !=0 || aa%400==0) ;
        nbj += (long)tab[bis][mm-1];
        nbj += (long)jj;
        return(nbj);
}

void dlundi_s(char nosem[],char datlun[])
{
  	static int tab[2][13] = {
              {31,59,90,120,151,181,212,243,273,304,334,365},
              {31,60,91,121,152,182,213,244,274,305,335,366} };
    static int tab2[2][13] = {
              {0,31,28,31,30,31,30,31,31,30,31,30,31},
              {0,31,29,31,30,31,30,31,31,30,31,30,31} };

   int bis;
   char zone[6];
   int aa,nsem,jj,m,mm;
   int nbj,offset;
   char janv1[9];
   int off[7]={1,0,-1,-2,-3,3,2};

    strncpy(zone,nosem+2,4);zone[4]=0;aa=atoi(zone);
    bis=(aa%4 == 0 && aa%100 !=0 || aa%400==0) ;
    strncpy(zone,nosem,2);zone[2]=0;nsem=atoi(zone);

    nbj=(nsem-1)*7 +1;
    m=0;
	while(nbj>tab[bis][m])
        m++;

	mm=m+1;
	if (m>0)
        jj=nbj-tab[bis][m-1];
	else
        jj=nbj;

    sprintf(janv1,"0101%d",aa);
    offset=off[jour_semaine(janv1)];

	jj=jj+offset;
    if (jj<=0) {
		  if (mm==1) {
			       mm=12;
				   aa=aa-1;
		   }
		   else  mm=mm-1;
		  jj=tab2[bis][mm]+jj;
            }
	if (jj> tab2[bis][mm]){
		   jj=jj-tab2[bis][mm];
		   if(mm == 12) {
               mm=1;
			   aa=aa+1;
		   }
		   else mm=mm+1;
	}

    sprintf(datlun,"%02d%02d%04d",jj,mm,aa);
}

long no_semaine(char *date)
{
      short jj,mm,aa,jour1;
      char janv1[9],janvp1[9];
      long nosem;
      static short debut[]={6,7,8,9,10,4,5};

      convdate (date,&jj,&mm,&aa);
      sprintf(janv1,"0101%d",aa);
      jour1=jour_semaine(janv1);
      nosem=(long)((nombre_jour_1860(date) - nombre_jour_1860(janv1) + debut[jour1])/7);
      if(nosem==0) return((long)(aa-1)*100L + 53L);
      if(nosem==53){
         sprintf(janvp1,"0101%d",aa+1);
         jour1=jour_semaine(janvp1);
         if(jour1!=0 && jour1<5) return((long)(aa+1)*100L+1L);
         else return((long)aa*100+53L);
      }
      return((long)aa*100L+nosem);
}

short jour_semaine (char *date)
{
    static short t[]={0,3,2,5,0,3,5,1,4,6,2,4} ;
    short jj,mm,aa;

    convdate(date,&jj,&mm,&aa);
    aa -= mm<3;
    return(aa + aa/4 - aa/100 + aa/400 + t[mm-1] + jj) % 7;
}

