Listing 3. The Complete AWK Program

#!/bin/awk -f
# The MkInvoice.sh script to print invoices for AP Building Supplies
#

function Usage()
     # Tell the user how to run the program
{
 print "Usage: MkInvoice <SalesFile> <ClientFile> <InvoiceFile>";
 exit(1);
}

function PrintHeader()
     # Print the business address
{
 printf "\t\t\t\t\t\tA P Building Supplies\n"  > InvFile;
 printf "\t\t\t\t\t\t59 Hardware Avenue\n"  > InvFile;
 printf "\t\t\t\t\t\tHammerville\n"  > InvFile;
 printf "\t\t\t\t\t\t2439\n\n"  > InvFile;
}

function PrintClient()
     # Print the Customer details
{
 print CName  > InvFile;
 print CAdr1  > InvFile;
 print CAdr2  > InvFile;
 print CAdr3  > InvFile;
 print CZip  > InvFile;
 print CTel  > InvFile;
}

function PrintInvHead()
     # Print the invoice item headings
{
 printf "\n"  > InvFile;
 printf "Stock Code\tItem Bought\t\tQty\tUnit Price\tTotal\n"  > InvFile;
 printf "----------\t-----------\t\t---\t----------\t-----\n"  > InvFile;
}

function PrintInvLine()
     # Print the invoice item details
{
 STotal = 0;
 STotal = SPrice * SQty;
 RTotal = RTotal + STotal;
 printf
i"%10s\t%-19s\t%d\t%1.2f\t\t%5.2f\n",SCode,SDesc,SQty,SPrice,STotal > InvFile; 
}

function PrintInvTot()
     # Print the total owing for all items on the invoice
{
 printf "\t\t\t\t\t\t\t\t------\n" > InvFile;
 printf "\t\t\t\t\t\t\t\t%5.2f\n\n",RTotal> InvFile;
 printf "\f"  > InvFile;
 RTotal = 0;
 InvTotFlag = 0;
 HeaderFlag = 0;
}

function ReadSales()
     # Read a record from the Sales file
     # If the end of file is reached or
     # the file is not found, set EndSales to 1
{
 FS=",";
 SaleStat = getline < SalesFile;
 if (SaleStat > 0)
    {
     SCode = $1;
     SCId = $2;
     SQty = $3;  # Assign Sales item details to
     SDesc = $4; # variables
     SPrice = $5;
    }
 else
    {
     EndSales = 1;
    }
}

function ReadClient()
     # Read a record from the Client file
     # If the end of file is reached or
     # the file is not found, set EndClient to 1
{
 FS="~";
 ClientStat = getline < ClientFile;
 if (ClientStat > 0)
    {
     CName = $1;
     CId = $2;
     CAdr1 = $3;
     CAdr2 = $4; # Assign Customer details
     CAdr3 = $5; # to variables
     CZip = $6;
     CTel = $7;
    }
 else
    {
     EndClient = 1;
    }
}

BEGIN {
 InvTotFlag = 0;
 HeaderFlag = 0;
 EndClient = 0;   # Initialise the various "flags"
 EndSales = 0;

 numparm = (ARGC -1);
 if (numparm !=3) # Check the number of parameters
    Usage();      # passed to the script
 else
    {   # Assign parameters to variables
     SalesFile = ARGV[1];
     ClientFile = ARGV[2];
     InvFile = ARGV[3];
    }

       # Read a record from each of the Sales
       # and Client files
 ReadClient();
 ReadSales();

 if (EndSales == 1) # Exit the script if file not found
   {
    print "Error! - Sales file NOT found!!";
    exit(1);
   }

 if (EndClient == 1) # Exit the script if file not found
   {
    print "Error! - Client file NOT found!!";
    exit(1);
   }

 while (EndSales == 0)  # While not EOF
      {

       while (( SCId == CId ) && (EndSales == 0))
             {
              if (HeaderFlag == 0)
                 {
                  PrintHeader();
                  PrintClient(); # Print Inv Headers
                  PrintInvHead();
                  HeaderFlag = 1;
                  InvTotFlag = 1;
                 }
              else
                 {
                  PrintInvLine();
                  ReadSales();
                 }
             }

       if (InvTotFlag == 1)
          PrintInvTot();    # Print the invoice total

       ReadClient();   # Read the next Client file record

       if (EndClient == 1)
          break;

       }

}