AR2011 RPG36
The provided document is an RPG program named AR2011.rpg36.txt, written in RPG II or III for the IBM System/36 or AS/400 System/36 Environment. It is called by the OCL procedure AR2011.ocl36.txt, which itself is invoked by AR200.ocl36.txt as a post-processing step in the cash receipts posting process. The program calculates and allocates discount amounts from cash receipts (ARTRAN) across invoice line items by product, writing the results to a sales discount file (SA5DSC). This supports detailed reporting or analysis of discounts by product. Below, I explain the process steps, business rules, tables used, and external programs called.
Process Steps of the RPG Program¶
The AR2011 program processes transactions from ARTRAN (sorted cash receipts), matches them to invoice line items in SA5FIND, calculates discount percentages per product, and writes detailed discount records to SA5DSC. It uses arrays to track percentages and amounts, adjusting the final record to ensure totals match. The program includes modifications (MG01, 05/09/22) to handle false discounts from credit and rebill scenarios.
Here’s a detailed breakdown of the process steps:
- Program Setup and File Definitions:
- Header Specifications:
H P064: Program identifier (P064).- Comments: Creates sales/accounts receivable discount percentages from cash receipts, preprocesses invoice totals, and splits discounts by product. MG01 (05/09/22) added logic to eliminate false discounts from credit/rebill miscalculations.
- File Specifications (F Specs):
ARTRAN IP F 256 256 DISK: Primary input file, 256-byte records, contains cash receipt transactions.SA5FIND IF F 1024 1024L25AI EXTK DISK: Sales finder file, input, 1024-byte records, keyed by 25-byte alternate index (AI), externally described (EXTK).GSTABL IF F 256 256 12AI 2 DISK: General table file, input, 256-byte records, 12-byte key (MG01 addition).ARCUST IF F 384 384 8AI 2 DISK: Customer master file, input, 384-byte records, 8-byte key (MG01 addition).SA5DSC O F 301 301 DISK A: Output sales discount file, 301-byte records, append mode (A).
- Extension Specifications (E Specs):
PCT 999 9 4: Array for discount percentages (999 elements, 9 digits, 4 decimals).AMT 999 7 2: Array for discount amounts (999 elements, 7 digits, 2 decimals).
-
Input Specifications (I Specs):
ARTRAN: Fields include company (ATCO#), customer (ATCUST), invoice (ATINV#), transaction amount (ATAMT), discount (ATDISC), type (ATTYPE), date (ATDATE), GL accounts (ATGLCR,ATGLDR), description (ATDESC), due date (ATDUDT), terms (ATTERM), salesman (ATSLS), miscellaneous amount (ATMISC), inter-company code (ATICCD), discount date (ATDSDT), notification flag (ATNOD), reference invoice (ATRFIV).SA5FIND: Invoice details like company (SACO#), customer (SACUST), invoice (SAINV#), ship-to (SASHIP), salesman (SASLMN), invoice date (SAINDT), quantity (SAQTY), price (SAPRCE), cost (SACOST), location (SALOC), product code (SAPROD), tank (SATANK), item (SAITEM), order (SAORD#), sequence (SASEQ), ship date (SASHDT), unit of measure (SAUM), product group (SAPRGP), taxes (SATXA1–SATXA5), etc.GSTABL,ARCUST: Not explicitly defined in input specs (MG01 additions), likely used for reference data (e.g., product/discount codes, customer details).
-
Initialization (ONETIM Subroutine):
-
Executes once:
- Resets accumulators (
ZERO6,ZERO7,ZERO9,ZERO11) to zero. - Converts journal date (
JRNLDTfromUDS) to YMD format (JRDYMD,JRCYMDas CCYYMMDD) usingMULT 10000.01and century prefix (20000000).
- Resets accumulators (
-
Main Processing Loop:
-
For each
ARTRANrecord (indicator 01):- Builds a 25-byte key (
ATKEY) combining company (ATCO#), customer (ATCUST), and invoice (ATINV#) forSA5FINDlookup. - Saves key (
ATSVKY) and sets total discount (TOTDIS = ATDISC).
- Builds a 25-byte key (
-
Gather Invoice Totals (First Pass):
- Sets lower limit (
SETLL) onSA5FINDusingATKEY. - Loops (
AGAIN1tag) to readSA5FINDrecords until EOF (indicator 10):- Skips sequence numbers > 100 (
SASEQ > 100, likely summary records). - Matches records by company (
ATCO# = SACO#), customer (ATCUST = SACUST), invoice (ATINV# = SAINV#). - Calculates extended price (
EXTPRC = SAQTY * SAPRCE). - Accumulates total price (
TOTPRC += EXTPRC) and line count (COUNT1 += 1).
- Skips sequence numbers > 100 (
-
After loop,
TOTPRCholds the total invoice price,COUNT1the number of line items. -
Allocate Discounts (Second Pass):
- Resets
SA5FINDtoATKEYand initializes counters (X = COUNT1,Y = 1). -
Loops (
AGAIN2tag) to readSA5FINDrecords:- Matches company, customer, invoice as before.
- Calculates extended price (
EXTPRC = SAQTY * SAPRCE). - Computes discount percentage (
PCTHLD = EXTPRC / TOTPRC) unlessTOTPRC = 0: - MG01: If
EXTPRC ≥ 0, setsPCTHLD = 1; else-1(handles credit/rebill issues). - Stores percentage in
PCT(Y)and multiplies byTOTDISto get item discount (AMT(Y) = PCT(Y) * TOTDIS). - Sets
AMTITM = AMT(Y)(item discount amount). - Calculates percentage as whole number (
AMTPCT = PCT(Y) * 100). - Increments counters (
COUNT2,Y). - If not the last line item (
COUNT2 < X), writes record viaGOODsubroutine and continues loop.
-
Adjust Final Record:
- For the last line item (
COUNT2 = X):- Sums discount amounts (
CLCAMT = XFOOT AMT) and percentages (CLCPCT = XFOOT PCT). - If
TOTDIS = CLCAMT, writes record (GOOD) and exits. - If
TOTDIS > CLCAMT, adds difference (DIFF1 = TOTDIS - CLCAMT) toAMTITM, writes, and exits. - If
TOTDIS < CLCAMT, subtracts difference (DIFF2 = CLCAMT - TOTDIS) fromAMT(X), writes, and exits.
- Sums discount amounts (
-
Ensures total discounts match
ATDISCfromARTRAN. -
Subroutine
GOOD: -
Writes a record to
SA5DSCwith fields fromSA5FIND(e.g.,SACO#,SACUST,SAINV#,SAQTY,SAPROD), plus:- Discount amount (
AMTITM). - Journal date (
JRCYMD). - Percentage (
AMTPCT). - Journal type (
GDJTYP) and number (GDJRN#) fromUDS.
- Discount amount (
-
Program Termination:
- Exits after processing all
ARTRANrecords or whenSA5FINDrecords are exhausted (SKIP2,EXITtags).
Business Rules¶
The program enforces rules for allocating AR discounts by product:
- Discount Allocation:
- Discounts (
ATDISC) from cash receipts are split across invoice line items inSA5FINDbased on their price contribution (EXTPRC / TOTPRC). - Each line item receives a proportional discount (
AMT(Y) = PCT(Y) * TOTDIS). -
The final line item is adjusted to ensure the sum of discounts equals
ATDISC, correcting rounding errors. -
Credit/Rebill Handling (MG01, 05/09/22):
-
Prevents false discounts in credit/rebill scenarios by setting
PCTHLD = 1(positiveEXTPRC) or-1(negativeEXTPRC) whenTOTPRC = 0, avoiding division-by-zero or incorrect percentages. -
Invoice Matching:
- Matches
ARTRANrecords toSA5FINDby company (ATCO# = SACO#), customer (ATCUST = SACUST), and invoice (ATINV# = SAINV#). -
Skips summary lines (
SASEQ > 100) to process only detail items. -
Output Structure:
- Writes detailed discount records to
SA5DSC, including invoice details, product code (SAPROD), discount amount (AMTITM), percentage (AMTPCT), and journal data (JRCYMD,GDJTYP,GDJRN#). -
Preserves all relevant
SA5FINDfields (e.g., quantity, location, taxes) for reporting. -
Y2K Compliance:
-
Converts journal date (
JRNLDT) to CCYYMMDD (JRCYMD) using a fixed century (20000000), assuming post-2000 transactions. -
No Updates to Input Files:
- Reads
ARTRAN,SA5FIND,GSTABL,ARCUSTin input mode (IF) without updates. -
Outputs only to
SA5DSC, ensuring no impact on master data. -
Error Handling:
- Validates matches via key (
ATKEY); unmatched records skip processing. - Adjusts final discount to ensure exact total (
TOTDIS), preventing discrepancies.
Tables Used¶
In RPG, "tables" refer to disk files. The program uses:
- Input Files:
ARTRAN(?9?CRTRGG): Cash receipts transactions (256 bytes, primary input).SA5FIND(?9?SA5FIND): Sales finder file (1024 bytes, keyed, invoice line items).GSTABL(?9?GSTABL): General table (256 bytes, keyed, likely product/discount codes, MG01).-
ARCUST(?9?ARCUST): Customer master (384 bytes, keyed, customer details, MG01). -
Output Files:
-
SA5DSC(?9?SA5DSC): Sales discount file (301 bytes, append, stores discount details by product). -
No Printer Files:
- No reports generated, unlike
AR200orAR211.
External Programs Called¶
The AR2011 program does not call external programs (no CALL operations). All logic is internal via subroutines:
- Subroutines: ONETIM (initialization), GOOD (write output).
- No External Calls: Self-contained, relies on file inputs from OCL.
Integration with OCL and Other Programs¶
- OCL Context: Called by
AR2011.ocl36.txt, which is invoked byAR200.ocl36.txtafterAR211. Uses?9?(e.g., 'GG') for file labels, supporting the Atrium environment (JB01, 11/28/23). - Input Dependency: Requires
?9?CRTRGGfromAR200,?9?SA5FINDand?9?SA5DSCfrom prior data entry,?9?GSTABLfor reference data, and?9?ARCUSTfor customer info. - Output: Produces
?9?SA5DSCfor discount reporting or analysis, likely used downstream (e.g., financial reporting). - Flow: Runs before final cleanup in
AR200.ocl36.txt(deletingCRTRGG, etc.). Conditional execution (IF ?F'A,?9?CRTRGG') ensures processing only if transactions exist.
Notes¶
- Purpose: Allocates discounts to product-level invoice lines for detailed analysis, critical for product profitability or sales reporting.
- MG01 Modification: Addresses credit/rebill issues, ensuring accurate discount percentages.
- Truncation: The source is truncated (19,980 characters), but key logic (loops, calculations, output) is clear. Subroutines like
GOODand output specs confirm the process. - Assumptions:
GSTABLandARCUST(MG01) likely provide lookup data (e.g., product groups, customer names), but their exact role depends on undefined input specs.
If you have the full source, SA5FIND/SA5DSC layouts, or downstream usage details, I can refine the analysis. Let me know for further clarification!