Skip to content

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:

  1. Program Setup and File Definitions:
  2. 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.
  3. 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).
  4. 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).
  5. 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 (SATXA1SATXA5), etc.
    • GSTABL, ARCUST: Not explicitly defined in input specs (MG01 additions), likely used for reference data (e.g., product/discount codes, customer details).
  6. Initialization (ONETIM Subroutine):

  7. Executes once:

    • Resets accumulators (ZERO6, ZERO7, ZERO9, ZERO11) to zero.
    • Converts journal date (JRNLDT from UDS) to YMD format (JRDYMD, JRCYMD as CCYYMMDD) using MULT 10000.01 and century prefix (20000000).
  8. Main Processing Loop:

  9. For each ARTRAN record (indicator 01):

    • Builds a 25-byte key (ATKEY) combining company (ATCO#), customer (ATCUST), and invoice (ATINV#) for SA5FIND lookup.
    • Saves key (ATSVKY) and sets total discount (TOTDIS = ATDISC).
  10. Gather Invoice Totals (First Pass):

  11. Sets lower limit (SETLL) on SA5FIND using ATKEY.
  12. Loops (AGAIN1 tag) to read SA5FIND records 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).
  13. After loop, TOTPRC holds the total invoice price, COUNT1 the number of line items.

  14. Allocate Discounts (Second Pass):

  15. Resets SA5FIND to ATKEY and initializes counters (X = COUNT1, Y = 1).
  16. Loops (AGAIN2 tag) to read SA5FIND records:

    • Matches company, customer, invoice as before.
    • Calculates extended price (EXTPRC = SAQTY * SAPRCE).
    • Computes discount percentage (PCTHLD = EXTPRC / TOTPRC) unless TOTPRC = 0:
    • MG01: If EXTPRC ≥ 0, sets PCTHLD = 1; else -1 (handles credit/rebill issues).
    • Stores percentage in PCT(Y) and multiplies by TOTDIS to 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 via GOOD subroutine and continues loop.
  17. Adjust Final Record:

  18. 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) to AMTITM, writes, and exits.
    • If TOTDIS < CLCAMT, subtracts difference (DIFF2 = CLCAMT - TOTDIS) from AMT(X), writes, and exits.
  19. Ensures total discounts match ATDISC from ARTRAN.

  20. Subroutine GOOD:

  21. Writes a record to SA5DSC with fields from SA5FIND (e.g., SACO#, SACUST, SAINV#, SAQTY, SAPROD), plus:

    • Discount amount (AMTITM).
    • Journal date (JRCYMD).
    • Percentage (AMTPCT).
    • Journal type (GDJTYP) and number (GDJRN#) from UDS.
  22. Program Termination:

  23. Exits after processing all ARTRAN records or when SA5FIND records are exhausted (SKIP2, EXIT tags).

Business Rules

The program enforces rules for allocating AR discounts by product:

  1. Discount Allocation:
  2. Discounts (ATDISC) from cash receipts are split across invoice line items in SA5FIND based on their price contribution (EXTPRC / TOTPRC).
  3. Each line item receives a proportional discount (AMT(Y) = PCT(Y) * TOTDIS).
  4. The final line item is adjusted to ensure the sum of discounts equals ATDISC, correcting rounding errors.

  5. Credit/Rebill Handling (MG01, 05/09/22):

  6. Prevents false discounts in credit/rebill scenarios by setting PCTHLD = 1 (positive EXTPRC) or -1 (negative EXTPRC) when TOTPRC = 0, avoiding division-by-zero or incorrect percentages.

  7. Invoice Matching:

  8. Matches ARTRAN records to SA5FIND by company (ATCO# = SACO#), customer (ATCUST = SACUST), and invoice (ATINV# = SAINV#).
  9. Skips summary lines (SASEQ > 100) to process only detail items.

  10. Output Structure:

  11. Writes detailed discount records to SA5DSC, including invoice details, product code (SAPROD), discount amount (AMTITM), percentage (AMTPCT), and journal data (JRCYMD, GDJTYP, GDJRN#).
  12. Preserves all relevant SA5FIND fields (e.g., quantity, location, taxes) for reporting.

  13. Y2K Compliance:

  14. Converts journal date (JRNLDT) to CCYYMMDD (JRCYMD) using a fixed century (20000000), assuming post-2000 transactions.

  15. No Updates to Input Files:

  16. Reads ARTRAN, SA5FIND, GSTABL, ARCUST in input mode (IF) without updates.
  17. Outputs only to SA5DSC, ensuring no impact on master data.

  18. Error Handling:

  19. Validates matches via key (ATKEY); unmatched records skip processing.
  20. Adjusts final discount to ensure exact total (TOTDIS), preventing discrepancies.

Tables Used

In RPG, "tables" refer to disk files. The program uses:

  1. Input Files:
  2. ARTRAN (?9?CRTRGG): Cash receipts transactions (256 bytes, primary input).
  3. SA5FIND (?9?SA5FIND): Sales finder file (1024 bytes, keyed, invoice line items).
  4. GSTABL (?9?GSTABL): General table (256 bytes, keyed, likely product/discount codes, MG01).
  5. ARCUST (?9?ARCUST): Customer master (384 bytes, keyed, customer details, MG01).

  6. Output Files:

  7. SA5DSC (?9?SA5DSC): Sales discount file (301 bytes, append, stores discount details by product).

  8. No Printer Files:

  9. No reports generated, unlike AR200 or AR211.

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 by AR200.ocl36.txt after AR211. Uses ?9? (e.g., 'GG') for file labels, supporting the Atrium environment (JB01, 11/28/23).
  • Input Dependency: Requires ?9?CRTRGG from AR200, ?9?SA5FIND and ?9?SA5DSC from prior data entry, ?9?GSTABL for reference data, and ?9?ARCUST for customer info.
  • Output: Produces ?9?SA5DSC for discount reporting or analysis, likely used downstream (e.g., financial reporting).
  • Flow: Runs before final cleanup in AR200.ocl36.txt (deleting CRTRGG, 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 GOOD and output specs confirm the process.
  • Assumptions: GSTABL and ARCUST (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!