Script Example: Printing Invoices in Sorted Sequence


 

The following script can be placed in a MAC file as a task or in the Preface section of a Visual Builder report. It will prompt the operator to supply an invoice number range, several different sort choices and the name of the form to print. Invoices may be sorted by account code, transaction name, postcode and state. The form will add each invoice to the Visual Builder print queue.

* Set-up a form to prompt users for the transaction

* number range, type of sorting and form to print

Declare aSortList Type Array

aSortList := { "Account Code", "Invoice Name", "Postcode", "State" }

FormCreate()

FormAdd("First Transaction", "", 9, "First transaction number.")

FormAdd("Last Transaction", "", 9, "Last transaction number.")

FormAdd("Sort By", "Account Code", aSortList)

FormAdd("Form", "Invoice", 9, "Enter the form to print.")

 

* End script if user pressesn Cancel

If ( .Not. FormShow("Invoice Print") ) Goto End

* User must specify first transaction number

If IsEmpty(FormResult(1))

   Echo("Sorry, you must specify the first transaction number.")

   Return FALSE

Endif

* User must also specify second transaction number

If IsEmpty(FormResult(2))

   Echo("Sorry, you must specify the last transaction number.")

   Return FALSE

Endif

 

OpenTable("Invoice")

* Find first transaction user specified

If ! Invoice->(Find(AsTran(FormResult(1))))

   Echo("Sorry, first transaction number not found.")

   CloseTable()

   Return FALSE

Endif

 

* Create a list of invoices to sort

Declare MyTranList Type Array

Declare nSort Type Number

Declare nCount Type Number

Declare cScript Type Character

:Loop

If ( Invoice->Invoiceno > AsTran(FormResult(2)) ) Goto Next

If ( Len(MyTranList) > 4000 ) Goto Next

AAdd(MyTranList, { Invoice->Invoiceno, Invoice->Acccode, Invoice->Name, Invoice->Postcode, Invoice->State } )

Invoice->(Skip())

Goto Loop

 

* Determine the sort method

:Next

If FormResult(3) == "Account Code"

   nSort := 1

Endif

If FormResult(3) == "Invoice Name"

   nSort := 2

Endif

If FormResult(3) == "Postcode"

   nSort := 3

Endif

If FormResult(3) == "State"

   nSort := 4

Endif

* Sort the list

* Note: The first element is the invoice number

* so we want to sort the next column past this number

ArraySort(MyTranList, nSort + 1)

 

* Now create a separate print job for every entry in the list

:Loop2

nCount := nCount + 1

If ( nCount > Len(MyTranList) ) Goto End

 

* Must build a script that a standard invoice form will

* understand

cScript := ""

cScript := cScript + "Declare FirstTran Type Transaction" + CHR(13) + CHR(10)

cScript := cScript + "FirstTran := '" + AsTran(MyTranList[nCount,1]) + "'" + CHR(13) + CHR(10)

cScript := cScript + "Declare LastTran Type Transaction" + CHR(13) + CHR(10)

cScript := cScript + "LastTran := '" + AsTran(MyTranList[nCount,1]) + "'" + CHR(13) + CHR(10)

cScript := cScript + "Declare FirstDate Type Date" + CHR(13) + CHR(10)

cScript := cScript + "FirstDate := ' / / '" + CHR(13) + CHR(10)

cScript := cScript + "Declare LastDate Type Date" + CHR(13) + CHR(10)

cScript := cScript + "LastDate := ' / / '" + CHR(13) + CHR(10)

cScript := cScript + "Declare AccountCode Type String" + CHR(13) + CHR(10)

cScript := cScript + "AccountCode := SPACE(08)" + CHR(13) + CHR(10)

cScript := cScript + "Declare AccountCat Type String" + CHR(13) + CHR(10)

cScript := cScript + "AccountCat := SPACE(10)"

* Add a print job to the Visual Builder print Queue

CreatePrintJob(FormResult(4), cScript)

Goto Loop2

* Finished

:End

CloseTable()

____________________________

Related Topics:

Script Programming

Script Samples



Help Topic Map