	REMARK	*************************************************************
	REMARK	*	ACCOUNTS PAYABLE TRANSACTION SORT	(A/P02A)    *
	REMARK	*		VERS. OF 2.00 PM     6/12/79		    *
	REMARK	*  =======================================================  *
	REMARK	*  THIS PROGRAM USES THE SHELL-METZNER SORTING ALGORITHM    *
	REMARK	*  TO SORT A TRANSACTION FILE IN DECREASING INCREMENTS AND  *
	REMARK	*  WRITE THE SORTED RECORDS OUT TO A WORKFILE.		    *
	REMARK	*  ONCE THE WORKFILE IS COMPLETELY WRITTEN, IT REPLACES THE *
	REMARK	*  FILE USED AS INPUT.					    *
	REMARK	*************************************************************

	ZERO$="000000"
	DIM DATA$(1000),C(27),D(27),P(5)
%INCLUDE CURSOR
	GOTO 6000
%INCLUDE READINV
%INCLUDE WRITEINV
%INCLUDE A/P-INFO




6000	CONSOLE
	PRINT CLEAR.SCREEN$;"A/P TRANSACTION SORT/PRINT"
	PRINT "KEY RETURN TO BEGIN; ENTER 'END' TO EXIT"
	INPUT LINE A$
	IF A$="END" THEN \
		PRINT CLEAR.SCREEN$;"A/P SORT/PRINT LOADING MENU":\
		CHAIN "A/P000"
	PRINT CLEAR.SCREEN$;"SORT PHASE"
	PRINT "WORKING...DO NOT INTERRUPT"
	PRINT
	INPUT.FILE$="A/P0F020.DAT"
	OUTPUT.FILE$="WORKFILE.DAT"
	RECLENGTH=580
	OPEN INPUT.FILE$ RECL RECLENGTH AS 1
	OPEN "A/P0F130.DAT" AS 5
	X0=5:GOSUB 3310
	IF TRANSACTION.RCD.COUNT%=0 THEN 8000
	IF END #1 THEN 7000						REMARK	SET END-OF-FILE BRANCH

	REMARK		**********     READ DATA RECORDS AND STRIP OFF KEY ELEMENTS FOR USE IN THE SORT     **********


6050	READ #1; VAR$,VAR1
	RECORD.COUNT%=RECORD.COUNT% + 1
	DATA$(RECORD.COUNT%)=VAR$
	PRINT CURSOR.HOME$:PRINT:PRINT "RECORD #";RECORD.COUNT%

	X0$=ZERO$+STR$(VAR1)
	X0$=RIGHT$(X0$,6)						REMARK	CONVERT INVOICE # TO ALPHANUMERIC
	FOR I%=1 TO 6 STEP 2
	DATA$(RECORD.COUNT%)=DATA$(RECORD.COUNT%)+CHR$(VAL(MID$(X0$,I%,2)))
	NEXT I%								REMARK	ROUTINE TO PACK INVOICE NUMBER
	X0$=ZERO$+STR$(RECORD.COUNT%)
	X0$=RIGHT$(X0$,4)
	X0%=VAL(LEFT$(X0$,2))
	X1%=VAL(RIGHT$(X0$,2))
	DATA$(RECORD.COUNT%)=DATA$(RECORD.COUNT%)+CHR$(X0%)+CHR$(X1%)
	GOTO 6050							REMARK	GET THE NEXT RECORD
7000	CLOSE 1
	OPEN INPUT.FILE$ RECL RECLENGTH AS 1
	PRINT "NUMBER OF RECORDS IN FILE =";RECORD.COUNT%
	M%=RECORD.COUNT%
7010	M%=M%/2								REMARK	DIVIDE THE SORT INTERVAL IN HALF
	IF M%=0 THEN GOTO 7150						REMARK	IF SORT INTERVAL IS EXHAUSTED,\
										RE-WRITE THE ORDERED FILE.
	K%=RECORD.COUNT%-M%
	J%=1
7040	I%=J%
7050	L%=I% + M%

	IF DATA$(I%) <= DATA$(L%) THEN GOTO 7120
	TEMP$=DATA$(I%)
	DATA$(I%)=DATA$(L%)
	DATA$(L%)=TEMP$
	I%=I% - M%
	IF I% > 0 THEN GOTO 7050
7120	J%=J%+1
	IF J% > K% THEN GOTO 7010 ELSE GOTO 7040
7150	PRINT CLEAR.SCREEN$;
	PRINT "SORT PHASE ENDED"
	PRINT "WRITING SORTED RECORDS TO WORKFILE"
	PRINT "WORKING... DO NOT INTERRUPT"
	CREATE OUTPUT.FILE$ RECL RECLENGTH AS 2
	FOR OUTPUT.COUNT%=1 TO RECORD.COUNT%
	X0$=RIGHT$(DATA$(OUTPUT.COUNT%),2)
	X0%=ASC(LEFT$(X0$,1))
	POINTER%=X0% * 100
	X0%=ASC(RIGHT$(X0$,1))
	POINTER%=POINTER% + X0%
	FILE.NO=1:X0%=POINTER%:GOSUB 3000
	FILE.NO=2:X0%=OUTPUT.COUNT%:GOSUB 3050
	NEXT OUTPUT.COUNT%
	DELETE 1
	CLOSE 2
	A%=RENAME(INPUT.FILE$,OUTPUT.FILE$)
	CLOSE 5
	OPEN "A/P0F130.DAT" AS 5
	SORT%=1
	X0=5:GOSUB 3350
8000	CHAIN "A/P02B"
