430 lines
12 KiB
C
430 lines
12 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <conio.h>
|
|
#include <sys/stat.h>
|
|
|
|
|
|
#define __TVA__ 1.21
|
|
#define __REDUC__ 0.9
|
|
#define __MAXNBRART__ 3
|
|
|
|
#define __STRLEN__ 34
|
|
|
|
typedef enum{FALSE,TRUE} Bool;
|
|
typedef struct {
|
|
char deisgnation[__STRLEN__];
|
|
float prixHTVA;
|
|
int quant;
|
|
} Article;
|
|
typedef struct {
|
|
Article * article;
|
|
unsigned int nbrArt;
|
|
} Panier;
|
|
typedef struct{
|
|
int jour,moi,annee;
|
|
} Date;
|
|
typedef struct{
|
|
char rue[__STRLEN__],numero[__STRLEN__],localite[__STRLEN__];
|
|
int codePostal;
|
|
} Adresse;
|
|
typedef struct{
|
|
char telephone[13],email[__STRLEN__];
|
|
} Contact;
|
|
typedef struct{
|
|
char nom[__STRLEN__];
|
|
Adresse adresse;
|
|
Contact contact;
|
|
Date anniversaire;
|
|
Panier panier;
|
|
} Client;
|
|
|
|
|
|
|
|
|
|
void encoderDate(Date *);
|
|
Bool dateValide (Date *);
|
|
void encoderArticle(Article *);
|
|
void encoderClient(Client *);
|
|
void sauvegarderClient(Client *, FILE *);
|
|
void afficherClient(Client *);
|
|
void encoderFacture(Panier *);
|
|
FILE * creerFichier(Date *, Client *);
|
|
Bool fichierExistant(char *);
|
|
void sauvergarderFacture(FILE *, Date *, Client *);
|
|
void afficherFacture(Date *, Client *);
|
|
void cleanFgets(char*);
|
|
int itoaY(int i, char*);
|
|
int itoaDM(int i, char*);
|
|
|
|
|
|
int main()
|
|
{
|
|
Date date;
|
|
FILE * file;
|
|
Client client;
|
|
do
|
|
encoderDate(&date);
|
|
while(!dateValide(&date));
|
|
|
|
encoderClient(&client);
|
|
afficherClient(&client);
|
|
|
|
file = creerFichier(&date, &client);
|
|
|
|
client.panier.article = (Article *)malloc(sizeof(Article));
|
|
client.panier.nbrArt = 1;
|
|
if(client.panier.article == NULL)
|
|
{
|
|
printf("ERREUR ALLOCATION ARTICLE!");
|
|
free(client.panier.article);
|
|
return -1;
|
|
}
|
|
|
|
encoderFacture(&client.panier);
|
|
afficherFacture(&date, &client);
|
|
sauvergarderFacture(file, &date, &client);
|
|
getch();
|
|
fclose(file);
|
|
free(client.panier.article);
|
|
return 0;
|
|
}
|
|
void encoderDate(Date * date)
|
|
{
|
|
printf("Entrez la date sous cette forme : jj/mm/aaaa\n");
|
|
fflush(stdin); scanf("%d/%d/%d",&(date->jour),&(date->moi),&(date->annee));
|
|
}
|
|
|
|
Bool dateValide(Date * date)
|
|
{
|
|
int j = date->jour;
|
|
int m = date->moi;
|
|
int a = date->annee;
|
|
int bis;
|
|
if(m != 2)
|
|
{
|
|
if(m >= 1 && m <= 7)
|
|
{
|
|
if(m %2 == 0)
|
|
{
|
|
if(j >= 1 && j <= 30) return TRUE;
|
|
else return FALSE;
|
|
}
|
|
else
|
|
{
|
|
if(j >= 1 && j <= 31) return TRUE;
|
|
else return FALSE;
|
|
}
|
|
}
|
|
else if(m > 7 && m <= 12)
|
|
{
|
|
if(m %2 == 0)
|
|
{
|
|
if(j >= 1 && j <= 31) return TRUE;
|
|
else return FALSE;
|
|
}
|
|
else
|
|
{
|
|
if(j >= 1 && j <= 30) return TRUE;
|
|
else return FALSE;
|
|
}
|
|
}
|
|
else return FALSE;
|
|
}
|
|
else
|
|
{
|
|
if(a %100 == 0)
|
|
{
|
|
if(a %400 == 0) bis = 1;
|
|
else bis = FALSE;
|
|
}
|
|
else
|
|
{
|
|
if(a %4 == 0)bis = 1;
|
|
else bis = FALSE;
|
|
}
|
|
if(bis == 1)
|
|
{
|
|
if(j >= 1 && j <= 29) return TRUE;
|
|
else return FALSE;
|
|
}
|
|
else
|
|
{
|
|
if(j >= 1 && j <= 28) return TRUE;
|
|
else return FALSE;
|
|
}
|
|
}
|
|
}
|
|
|
|
void encoderArticle(Article * article)
|
|
{
|
|
printf("\nVeuillez entrer le nom : ");
|
|
scanf("%34s", article->deisgnation);
|
|
printf("\nVeuillez entrer le prix a l'unite hors TVA : ");
|
|
scanf("%f", &article->prixHTVA);
|
|
printf("\nVeuillez entrer la quantite desiree : ");
|
|
fflush(stdin);
|
|
scanf("%d", &article->quant);
|
|
}
|
|
|
|
void encoderClient(Client * client)
|
|
{
|
|
printf("Encoage Client\n");
|
|
fflush(stdin);
|
|
if(!(printf("Entrez la rue :") && !fflush(stdin) && fgets(client->adresse.rue,__STRLEN__,stdin) != NULL && \
|
|
printf("\nEntrez le numero :" ) && !fflush(stdin) && fgets(client->adresse.numero,__STRLEN__,stdin) != NULL && \
|
|
printf("\nEntrez la localité :") && !fflush(stdin) && fgets(client->adresse.localite,__STRLEN__,stdin) != NULL && \
|
|
printf("\nEntrez le code postal :") && !fflush(stdin) && scanf("%d", &(client->adresse.codePostal)) && \
|
|
printf("\nNom client :") && !fflush(stdin) && fgets(client->nom,__STRLEN__,stdin) != NULL))
|
|
{
|
|
printf("\nErreur lors de l'encodage de l'adresse");
|
|
exit(-1);
|
|
}
|
|
cleanFgets(client->adresse.rue);cleanFgets(client->adresse.numero);cleanFgets(client->adresse.localite);
|
|
cleanFgets(client->nom);
|
|
if(!(printf("\n Entrez le numéro de tél. :") && !fflush(stdin) &&fgets(client->contact.telephone,13,stdin) && \
|
|
printf("\nEntrez l'adresse email :") && !fflush(stdin) &&fgets(client->contact.email,__STRLEN__,stdin)))
|
|
{
|
|
printf("\nErreur lors de l'encodage des coo de contact");
|
|
exit(-1);
|
|
}
|
|
cleanFgets(client->contact.telephone);cleanFgets(client->contact.email);
|
|
printf("\n Anniversaire client :");
|
|
do
|
|
encoderDate(&(client->anniversaire));
|
|
while(!dateValide(&(client->anniversaire)));
|
|
}
|
|
void afficherClient(Client *client)
|
|
{
|
|
printf("Client : %s\n", client->nom);
|
|
printf("rue : %s",client->adresse.rue);
|
|
printf("numero : %s",client->adresse.numero);
|
|
printf("localité : %s",client->adresse.localite);
|
|
printf("code postal : %d",client->adresse.codePostal);
|
|
printf("numéro de tél. : %s",client->contact.telephone);
|
|
printf("adresse email : %s",client->contact.email);
|
|
}
|
|
void sauvegarderClient(Client *client, FILE * file)
|
|
{
|
|
fprintf(file, "Client : %s\n", client->nom);
|
|
fprintf(file, "rue : %s",client->adresse.rue);
|
|
fprintf(file, "numero : %s",client->adresse.numero);
|
|
fprintf(file, "localité : %s",client->adresse.localite);
|
|
fprintf(file, "code postal : %d",client->adresse.codePostal);
|
|
fprintf(file, "numéro de tél. : %s",client->contact.telephone);
|
|
fprintf(file, "adresse email : %s",client->contact.email);
|
|
}
|
|
void encoderFacture(Panier * panier)
|
|
{
|
|
char continuer = 'y';
|
|
do{
|
|
if(panier->nbrArt <= __MAXNBRART__)
|
|
{
|
|
encoderArticle(&(panier->article[panier->nbrArt-1]));
|
|
printf("Y-a-t-il encore un article ?(Y|n)");
|
|
fflush(stdin);
|
|
scanf("%c", &continuer);
|
|
if(panier->nbrArt < __MAXNBRART__ && (continuer|0b00100000) == 'y')
|
|
{
|
|
panier->nbrArt++;
|
|
Article * tempArticle;
|
|
tempArticle = (Article*)realloc(panier->article, sizeof(Article)*(panier->nbrArt));
|
|
if(panier->article == NULL)
|
|
{
|
|
printf("ERREUR ALLOCATION !");
|
|
free(tempArticle);
|
|
free(panier->article);
|
|
exit(-1);
|
|
}
|
|
else
|
|
{
|
|
panier->article = tempArticle;
|
|
}
|
|
}
|
|
else if((continuer|0b00100000) == 'y')
|
|
{
|
|
system("cls");
|
|
printf("Vous avez ateint le nombre d'article maximum !\n\n");
|
|
break;
|
|
}
|
|
}
|
|
}while((continuer|0b00100000) == 'y' && panier->nbrArt <= __MAXNBRART__);
|
|
}
|
|
FILE * creerFichier(Date * date, Client * client)
|
|
{
|
|
char fileName[__STRLEN__*+16];
|
|
fileName[0] = '\0';
|
|
int i;
|
|
char buf[5];
|
|
|
|
//Toute cette partie aurais pus être remplacé par un sprintf
|
|
strcat(fileName,client->nom);
|
|
itoaY(date->annee, buf);
|
|
strcat(fileName,buf);
|
|
itoaDM(date->moi, buf);
|
|
strcat(fileName, buf);
|
|
itoaDM(date->jour, buf);
|
|
strcat(fileName, buf);
|
|
strcat(fileName,"_");
|
|
|
|
|
|
for(i=1; i<= 99; i++)//Le caractère 10 n'existe pas, flem de coder pour avoir plus que 9 et aloc dynamique nécessaire
|
|
{
|
|
char fileNameTmp[__STRLEN__*+16];
|
|
sprintf(fileNameTmp,"%s%d",fileName,i);
|
|
if(!fichierExistant(fileNameTmp))
|
|
{
|
|
printf("%s\n", fileNameTmp);
|
|
FILE * f = fopen(fileNameTmp,"w");
|
|
if(f==NULL)
|
|
{
|
|
printf("Erreur lors de l'ouverture du fichier");
|
|
exit(-1);
|
|
}
|
|
else
|
|
return f;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
Bool fichierExistant(char * filename)
|
|
{
|
|
FILE * f = fopen(filename, "r");
|
|
if(f==NULL)
|
|
return FALSE;
|
|
else
|
|
return TRUE;
|
|
}
|
|
void sauvergarderFacture(FILE * file, Date * date, Client * client)
|
|
{
|
|
int i;
|
|
float prixTot = 0;
|
|
fprintf(file,"La Bulle Informatique\nRoute du Condroz 78, 4123 Neupre\n");
|
|
fprintf(file,"\nFacture a payer\n");
|
|
fprintf(file,"Date de facturation :");
|
|
fprintf(file,"%02d/%02d/%02d", date->jour, date->moi, date->annee);
|
|
fprintf(file, "\n Info client :");
|
|
sauvegarderClient(&client, file)
|
|
fprintf(file,"\n|---------------------------------------------------------------------------------------------|\
|
|
\n| Designation | prixHTVA | Quantite | 10%% | prixTVAC |\
|
|
\n|---------------------------------------------------------------------------------------------|");
|
|
|
|
for(i=0;client->panier.nbrArt>i;i++)
|
|
{
|
|
fprintf(file,"\n|%34s|%12.2f Eur |%14d| %c |%12.2f |",\
|
|
client->panier.article[i].deisgnation,client->panier.article[i].prixHTVA,client->panier.article[i].quant\
|
|
,(client->panier.article[i].quant>=20)?'x':' ',\
|
|
(client->panier.article[i].quant>=20)?client->panier.article[i].prixHTVA*__TVA__*__REDUC__*client->panier.article[i].quant:client->panier.article[i].prixHTVA*__TVA__*client->panier.article[i].quant);
|
|
prixTot += (client->panier.article[i].quant>=20)?client->panier.article[i].prixHTVA*__TVA__*__REDUC__*client->panier.article[i].quant:client->panier.article[i].prixHTVA*__TVA__*client->panier.article[i].quant;
|
|
}
|
|
fprintf(file,"\n|---------------------------------------------------------------------------------------------|");
|
|
fprintf(file,"\nPrix totale :%f", prixTot);
|
|
}
|
|
|
|
void afficherFacture(Date * date, Client * client)
|
|
{
|
|
int i;
|
|
float prixTot = 0;
|
|
printf("La Bulle Informatique\nRoute du Condroz 78, 4123 Neupre\n");
|
|
printf("\nFacture a payer\n");
|
|
printf("Date de facturation :");
|
|
printf("%02d/%02d/%02d", date->jour, date->moi, date->annee);
|
|
printf("\n Info client :");
|
|
afficherClient(client);
|
|
printf("\n|---------------------------------------------------------------------------------------------|\
|
|
\n| Designation | prixHTVA | Quantite | 10%% | prixTVAC |\
|
|
\n|---------------------------------------------------------------------------------------------|");
|
|
|
|
for(i=0;client->panier.nbrArt>i;i++)
|
|
{
|
|
printf("\n|%34s|%12.2f Eur |%14d| %c |%12.2f |",\
|
|
client->panier.article[i].deisgnation,client->panier.article[i].prixHTVA,client->panier.article[i].quant\
|
|
,(client->panier.article[i].quant>=20)?'x':' ',\
|
|
(client->panier.article[i].quant>=20)?client->panier.article[i].prixHTVA*__TVA__*__REDUC__*client->panier.article[i].quant:client->panier.article[i].prixHTVA*__TVA__*client->panier.article[i].quant);
|
|
prixTot += (client->panier.article[i].quant>=20)?client->panier.article[i].prixHTVA*__TVA__*__REDUC__*client->panier.article[i].quant:client->panier.article[i].prixHTVA*__TVA__*client->panier.article[i].quant;
|
|
}
|
|
printf("\n|---------------------------------------------------------------------------------------------|");
|
|
printf("\nPrix totale :%f", prixTot);
|
|
}
|
|
|
|
|
|
void cleanFgets(char * str)
|
|
{
|
|
char *p = strchr(str, '\n');
|
|
if(p!=NULL)
|
|
*p = '\0';
|
|
else
|
|
{
|
|
printf("Une erreur est arrivé lors du nettoygae \\0");
|
|
exit(-1);
|
|
}
|
|
}
|
|
int itoaY(int i, char * buf)
|
|
{
|
|
buf [0] = '0';
|
|
buf [1] = '0';
|
|
buf [2] = '0';
|
|
buf [3] = '0';
|
|
buf [4] = '\0';
|
|
int j;
|
|
if(!i)
|
|
return 1;
|
|
buf [3] = '1';
|
|
for(j=1; j<=i && j<=9999 ; j++)
|
|
{
|
|
if(!(j%1000))
|
|
{
|
|
buf[0]++;
|
|
buf[1]='0';
|
|
buf[2]='0';
|
|
buf[3]='0';
|
|
}
|
|
else if(!(j%100))
|
|
{
|
|
buf[1]++;
|
|
buf[2]='0';
|
|
buf[3]='0';
|
|
}
|
|
else if(!(j%10))
|
|
{
|
|
buf[2]++;
|
|
buf[3]='0';
|
|
}
|
|
else
|
|
{
|
|
buf[3]++;
|
|
}
|
|
}
|
|
if(j>9999)
|
|
return 0;
|
|
else
|
|
return 1;
|
|
}
|
|
int itoaDM(int i, char * buf)
|
|
{
|
|
buf[0] = '0';
|
|
buf[1] = '0';
|
|
buf[2] = '\0';
|
|
printf("%s\n", buf);
|
|
int j;
|
|
if(!i)
|
|
return 1;
|
|
buf[1]++;
|
|
for(j=1; j<=i && j<=99 ; j++)
|
|
{
|
|
if(!(j%10))
|
|
{
|
|
(buf[0])++;
|
|
(buf[1])='0';
|
|
}
|
|
else
|
|
{
|
|
(buf[1])++;
|
|
}
|
|
}
|
|
if(j>99)
|
|
return 0;
|
|
else
|
|
return 1;
|
|
}
|