<?xml version="1.0" encoding="UTF-8"?>
<!-- 
  MAGEC RAD - Explanation of ADD/DUP Function Flowchart (Figure 06)
  Source Diagram: ch05fig06.png (ADD/DUP Function) 
  Cross-referenced with: diags_main.htm, insert_main.htm, masteridx.htm
  Generated by aiMAGEC for diagram explanation.
  Date: Current session
-->

<addDupFunctionFlowchart 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  diagramId="ch05fig06"
  function="ADD/DUP"
  docSource="https://magec.com/DOC/diags_main.htm"
  masterIndex="https://magec.com/DOC/masteridx.htm">

  <overview>
    <description>
      This flowchart depicts the logical processing flow for the standard ADD/DUP (Add/Duplicate) Function in a MAGEC-generated MMP (online program). 
      It handles both new record addition and duplicate key detection. It shows the main paths: initialization, key normalization, read to check for duplicates, multi-stage editing, calculation, add to database, error handling, and screen output.
      Key concepts: Pseudo-conversational processing, insertion points (%XXXXX), continuation vs. new transaction modes, duplicate key checking, and business rule processing.
    </description>
    <note>
      The business rule (RULPROC) pertaining to any of the MMP's elements from its primary Data Class will be inserted immediately after the %CALC logic. 
      It could set the ERROR-FOUND condition causing the program to take the error path, bypassing the add to the database.
    </note>
  </overview>

  <flow>
    <!-- Top Level -->
    <step id="start" label="ADD/DUP Function">
      <insertionPoint>%PREINIT</insertionPoint>
      <next>BA100-INIT-STORAGE</next>
    </step>

    <step id="init-storage" label="BA100-INIT-STORAGE">
      <insertionPoint>%INIT</insertionPoint>
      <next>AAM200-MAINTENANCE-MAINLINE</next>
    </step>

    <step id="maint-mainline" label="AAM200-MAINTENANCE-MAINLINE">
      <insertionPoint>%PFKEYM</insertionPoint>
      <next>BA210-MAINT-SCREEN</next>
    </step>

    <step id="maint-screen" label="BA210-MAINT-SCREEN">
      <insertionPoint>%INITATB</insertionPoint>
      <next>BA400-NORMALIZE-MAINT-KEY</next>
    </step>

    <!-- Key Normalization -->
    <step id="normkey" label="BA400-NORMALIZE-MAINT-KEY">
      <insertionPoint>%NORMKEY</insertionPoint>
      <branches>
        <branch condition="INVALID KEY" next="ERROR-PATH" />
        <branch condition="DUP FUNCTION &amp; TEST-KEY = TWA-LAST-KEY" next="AAM410-READ-BY-KEY" />
      </branches>
    </step>

    <!-- Read for Duplicate Check -->
    <step id="read-by-key" label="AAM410-READ-BY-KEY">
      <insertionPoint>%REDKY</insertionPoint>
      <next>%JOIN</next>
    </step>

    <step id="logical-join" label="JA100-LOGICAL-JOIN">
      <insertionPoint>%JOIN</insertionPoint>
      <branches>
        <branch condition="NOT FOUND &amp; NEW TRANSACTION MODE" next="BB100-BLANK-SCREEN" />
        <branch condition="REC FOUND (duplicate on file)" next="BB200-FILL-SCREEN" />
        <branch condition="NOT FOUND &amp; CONTINUATION MODE" next="BB500-SCREEN-EDIT" />
      </branches>
    </step>

    <!-- Screen Edit Path (ADD specific) -->
    <step id="screen-edit" label="BB500-SCREEN-EDIT">
      <insertionPoint>%EDIT</insertionPoint>
      <branches>
        <branch condition="ERROR-FOUND" next="ERROR-PATH" />
      </branches>
      <next>BB500-SCREEN-EDIT-2</next>
    </step>

    <step id="screen-edit2" label="BB500-SCREEN-EDIT-2">
      <insertionPoint>%EDIT2</insertionPoint>
      <branches>
        <branch condition="ERROR-FOUND" next="ERROR-PATH" />
      </branches>
      <next>BB600-BUILD-REC</next>
    </step>

    <step id="build-rec-init" label="BB600-BUILD-REC">
      <insertionPoint>%ADDINIT</insertionPoint>
      <next>BB600-BUILD-REC (continued)</next>
    </step>

    <step id="build-rec-calc" label="BB600-BUILD-REC">
      <insertionPoint>%CALC</insertionPoint>
      <branches>
        <branch condition="ERROR-FOUND" next="ERROR-PATH" />
      </branches>
      <next>AAM710-ADD-TO-DATABASE</next>
    </step>

    <!-- Add Path -->
    <step id="add" label="AAM710-ADD-TO-DATABASE">
      <insertionPoint>%ADDIT</insertionPoint>
      <branches>
        <branch condition="NOT REC-FOUND" next="AAM753-GOODADD" />
        <branch condition="REC-FOUND" next="AAM751-UPDERR" />
      </branches>
    </step>

    <step id="goodadd" label="AAM753-GOODADD">
      <insertionPoint>%GOODADD</insertionPoint>
      <next>AA800-SEND-SCREEN / %SNDSCRN (GO TO TOP OF SEE FUNCTION)</next>
    </step>

    <!-- Other Screen Paths -->
    <step id="fill-screen" label="BB200-FILL-SCREEN">
      <insertionPoint>%COMP</insertionPoint>
      <next>AA800-SEND-SCREEN</next>
    </step>

    <step id="blank-screen" label="BB100-BLANK-SCREEN">
      <insertionPoint>%BLNKSCR</insertionPoint>
      <next>AA800-SEND-SCREEN</next>
    </step>

    <step id="send-screen" label="AA800-SEND-SCREEN">
      <insertionPoint>%SNDSCRN</insertionPoint>
      <next>AA900-GOBACK</next>
    </step>

    <step id="goback" label="AA900-GOBACK">
      <insertionPoint>%GOBACK</insertionPoint>
    </step>

    <!-- Error Path -->
    <step id="error" label="ERROR-FOUND">
      <insertionPoint>%UPDERR (AAM751-UPDERR)</insertionPoint>
      <next>AA800-SEND-SCREEN</next>
    </step>
  </flow>

  <keyInsertionPoints>
    <point name="%PREINIT" />
    <point name="%INIT" />
    <point name="%PFKEYM" />
    <point name="%INITATB" />
    <point name="%NORMKEY" />
    <point name="%REDKY" />
    <point name="%JOIN" />
    <point name="%EDIT" />
    <point name="%EDIT2" />
    <point name="%ADDINIT" />
    <point name="%CALC" />
    <point name="%ADDIT" />
    <point name="%GOODADD" />
    <point name="%COMP" />
    <point name="%BLNKSCR" />
    <point name="%SNDSCRN" />
    <point name="%GOBACK" />
  </keyInsertionPoints>

  <references>
    <link href="https://magec.com/DOC/diags_main.htm">Processing Diagrams - Figure 06 ADD/DUP Function</link>
    <link href="https://magec.com/DOC/insert_main.htm">Insertion Points Reference</link>
    <link href="https://magec.com/DOC/masteridx.htm">Master Index (search for %ADDINIT, %ADDIT, %GOODADD, etc.)</link>
  </references>

</addDupFunctionFlowchart>