{ Utilisation de tableaux, nombre de clients inconnu et proc‚dures …
paramŠtres pour cette version du 25 janvier 2000 }

PROGRAM facturation;
uses crt;
VAR aff, nbrart, i, compteur, comptart: integer;
    nomcl, adrcl, cp, vilcl: string;
    tothtva, tothtvb, tothtvc, tottvaca, tottvacb,
    tottvacc, totalht, totaltva, facture: real;
    n: char;
    numart:array [1..50] of real;
    pua:array [1..50] of real;
    qteart:array [1..50] of real;
    pvhtva:array [1..50] of real;
    codtva:array [1..50] of char;

{--------------------------------------------------------------------------
Cette proc‚dure permet de remettre a z‚ro certaines variables du programme
comme par exemple les diff‚rents totaux de tva
--------------------------------------------------------------------------}

PROCEDURE REMISE_ZERO (var a,b,c,d,e,f:real);
BEGIN
    a:=0;b:=0;c:=0;d:=0;e:=0;f:=0
END;

{--------------------------------------------------------------------------
Saisie des donn‚es du client courant, avec en entr‚e puis en sortie les
variables nomcl, adrcl, cp et vilcl, repr‚sent‚es par a,b,c et d
--------------------------------------------------------------------------}

PROCEDURE DONNEES_CLIENTS (var a,b,c,d:string);
BEGIN
    WRITE ('Nom du client: '); READLN (a);
    WRITE ('Rue + num‚ro: '); READLN (b);
    WRITE ('Code postal: '); READLN (c);
    WRITE ('Ville: '); READLN (d)
END;

{--------------------------------------------------------------------------
Saisie des donn‚es de chaque articles avec les variables numart, pua, qteart
et codtva transmises par parametre
--------------------------------------------------------------------------}

PROCEDURE SAISIE_ARTICLE (var a,b,c,e:real; var d:char);
BEGIN
    WRITE ('Num‚ro de l''article: '); READLN (a);
    WRITE ('Prix unitaire: '); READLN (b);
    WRITE ('Quantit‚ command‚e: '); READLN (c);
    WRITE ('Code TVA (a b ou c): '); READLN (d);
    e := b * c
END;

{--------------------------------------------------------------------------
Test pour appliquer le taux de TVA correct en transmettant toutes les
donn‚es par parametre
--------------------------------------------------------------------------}

PROCEDURE TEST_TVA (a:char; var b,c,d,e:real);
BEGIN
   IF ((a = 'a') OR (a = 'A')) THEN b := b + c;
   IF ((a = 'b') OR (a = 'B')) THEN d := d + c;
   IF ((a = 'c') OR (a = 'C')) THEN e := e + c
END;

{--------------------------------------------------------------------------
Calcul des totaux: total tvac code A, code B, code C, totalht, total de la
TVA, total final, ...
--------------------------------------------------------------------------}

PROCEDURE CALCUL_TOTAUX (var a,b,c,d,e,f:real; g,h,i:real);
BEGIN
   a := g + (g * 6 / 100);
   b := h + (h * 16 / 100);
   c := i + (i * 25 / 100);
   d := g + h + i;
   e := (g * 6 / 100) + (h * 16 / 100) + (i * 25 / 100);
   f := d  + e
END;

{--------------------------------------------------------------------------
R‚alise l'affichage et le remplissage des totaux et montants de la facture
--------------------------------------------------------------------------}

PROCEDURE TOTAUX_FACTURE (var a,b,c,d,e,f,g,h,i:real);
VAR ii:integer;
BEGIN
    FOR ii:=1 to 50 do write ('-');
    WRITELN;
    WRITELN ('HTVA','CODE':12,'TAUX':13,'TVA':8,'TVAC':13);
    FOR ii:=1 to 50 do write ('-');
    WRITELN;
    WRITELN (a:6:1, 'A':10, '6':13, (a * 6 / 100):8:1, b:11:1);
    WRITELN (c:6:1, 'B':10, '16':13, (c * 16 / 100):8:1, d:11:1);
    WRITELN (e:6:1, 'C':10, '25':13, (e * 25 / 100):8:1, f:11:1);
    WRITELN;
    WRITELN ('HTVA':30, 'TVA':8, 'TOTAL':11);
    WRITELN ('------------------------------':49);
    WRITELN (g:30:1, h:8:1, i:11:1);
    READLN
END;

{--------------------------------------------------------------------------
Permet d'afficher le d‚tail des articles achet‚s sur la facture
--------------------------------------------------------------------------}

PROCEDURE AFFICHAGE;
BEGIN
   FOR i:=1 to 50 do WRITE ('-');
   WRITELN;
   WRITELN ('Num‚ro','Prix Unit.':12,'Quantit‚':13,'Code':8,'Prix':10);
   FOR i:=1 to 50 do WRITE ('-');
   WRITELN;
   for aff := 1 to nbrart do
   WRITELN (numart[aff]:6:0, pua[aff]:10:1, qteart[aff]:13:0, codtva[aff]:8,
   pvhtva[aff]:12:1)
END;

{--------------------------------------------------------------------------
Affiche l'entˆte de la facture avec les coordonn‚es du client
--------------------------------------------------------------------------}

PROCEDURE ENTETE (a,b,c,d:string);
BEGIN
    WRITELN ('Firme le bon petit marchand':50);
    WRITELN ('220 route d''Arlon':45);
    WRITELN ('STRASSEN':39);
    WRITELN; WRITELN;
    WRITELN ('Monsieur ',a);
    WRITELN (' ':9, b);
    WRITELN (' ':9, c,' ',d)
END;

{--------------------------------------------------------------------------
Commencement de la boucle principale du programme
---------------------------------------------------------------------------}

BEGIN
CLRSCR;
WRITE ('Commencer a encoder les factures ? (o/n): ');
READLN (n);
WHILE ((n = 'o') or (n = 'O')) DO

    BEGIN
    REMISE_ZERO (tothtva,tothtvb,tothtvc,tottvaca,tottvacb,tottvacc);
    DONNEES_CLIENTS (nomcl,adrcl,cp,vilcl);

    (* Saisie des articles command‚s *)

    WRITE ('Nombre d''articles de la commande: '); READLN (nbrart);
    FOR comptart := 1 to nbrart do

        BEGIN

        SAISIE_ARTICLE (numart[comptart],pua[comptart],qteart[comptart],
        pvhtva[comptart],codtva[comptart]);
        TEST_TVA (codtva[comptart],tothtva,pvhtva[comptart],tothtvb,tothtvc);

        END;

    CALCUL_TOTAUX (tottvaca,tottvacb,tottvacc,totalht,totaltva,facture,
    tothtva,tothtvb,tothtvc);

    (* Affichage de la facture *)

    clrscr;
    ENTETE (nomcl,adrcl,cp,vilcl);
    writeln;
    AFFICHAGE;
    TOTAUX_FACTURE (tothtva,tottvaca,tothtvb,tottvacb,tothtvc,tottvacc,
    totalht,totaltva,facture);
    WRITELN;
    clrscr;
    WRITE ('Continuer avec un autre client ? (o/n) ');
    READLN (n)
    END;  (* n != o *)

WRITELN;
WRITELN ('Fin des factures')

END.
