<?xml version="1.0" encoding="UTF-8"?>
<flowchart title="ADD-DUP-Function">
    <settings theme="bold" exportWidth="600" grid="true" logo="ALA_Logo_gray.png"/>
    <nodes>

        <node id="title" type="title" label="ADD / DUP Function">
            <position x="300" y="20"/>
            <narrative>ADD / DUP Function — Add Record / Duplicate Record logic flow diagram. Handles both new record addition and duplicate key detection, with multi-stage editing, calculation, and add-to-database processing.</narrative>
        </node>

        <node id="subtitle" type="comment" label="Add New Record / Duplicate Existing Record "><position x="300" y="70"/><narrative>The business rule RULPROC pertaining to any of the MMP's elements from its primary Data Class is inserted immediately after the %CALC logic. It could set the ERROR-FOUND condition, causing the program to take the error path and bypass the add to the database.</narrative></node>

        <node id="1" type="start" label="Begin">
            <position x="210" y="140"/>
            <narrative>Entry into the ADD/DUP Function (SFUNCT = xxxADD or xxxDUP).</narrative>
        </node>

        <node id="2" type="process" label="AA100-CHECK-FUNCTION   %PREINIT">
            <position x="210" y="240"/>
            <narrative>Check function code and perform pre-initialization processing. Earliest developer intercept point — runs for every transaction and every function.</narrative>
            <cobolparagraph>AA100-CHECK-FUNCTION</cobolparagraph>
            <link>insert_main.htm#insert001029</link>
            <target>MAIN</target>
        </node>
        <node id="2asidea" type="aside" label=" Narrative of Steps - -ADD">
            <position x="480" y="200"/>
            <narrative>Narrative for ADD function.</narrative>
            <link>https://www.magec.com/DOC/genmmp_main.htm#genmmp002003</link>
            <target>MAIN</target>
        </node>

        <edge from="2" to="2asidea" style="dotted" label="see also"/>
        <node id="2asided" type="aside" label=" Narrative of Steps - -DUP">
            <position x="480" y="270"/>
            <narrative>Narrative for DUP function.</narrative>
            <link>https://www.magec.com/DOC/genmmp_main.htm#genmmp002013</link>
            <target>MAIN</target>
        </node>

        <edge from="2" to="2asided" style="dotted" label="see also"/>

        <node id="3" type="process" label="BA100-INIT-STORAGE   %INIT">
            <position x="210" y="340"/>
            <narrative>Initialize working storage areas. Runs for all maintenance functions.</narrative>
            <cobolparagraph>BA100-INIT-STORAGE</cobolparagraph>
            <link>insert_main.htm#insert001016</link>
            <target>MAIN</target>
        </node>
        <node id="3aside" type="aside" label="Maintenance Mainline">
            <position x="480" y="340"/>
            <narrative>Mainline logic for all maintenance functions.</narrative>
            <link>https://www.magec.com/DOC/genmmp_main.htm#genmmp004057</link>
            <target>MAIN</target>
        </node>

        <edge from="3" to="3aside" style="dotted" label="see also"/>

        <node id="4" type="process" label="AAM200-MAINTENANCE-MAINLINE   %PFKEYM" width="200">
  <position x="210" y="440"/>
  <narrative>Maintenance mainline processing; handles PF key detection.</narrative>
  <link>insert_main.htm#insert001028</link>
  <target>MAIN</target>
  <cobolparagraph>AAM200-MAINTENANCE-MAINLINE</cobolparagraph>
</node>
        <node id="4aside" type="aside" label="TWA-MSK-AREA">
            <position x="480" y="440"/>
            <narrative>First 10-bytes of Mask Area give Mask ID, Cursor position, Entry key detection (ENTER, CLEAR, PF1, PA1, etc.).</narrative>
            <cobolparagraph>TWA-MSK-AREA</cobolparagraph>
            <link>https://www.magec.com/DOC/genmmp_main.htm#genmmp004025</link>
            <target>MAIN</target>
        </node>
        <edge from="4" to="4aside" style="dotted" label="see also"/>

        <node id="5" type="process" label="BA210-MAINT-SCREEN   %INITATB">
            <position x="210" y="540"/>
            <narrative>Initialize screen attributes and table processing.</narrative>
            <cobolparagraph>BA210-MAINT-SCREEN</cobolparagraph>
            <link>insert_main.htm#insert001017</link>
            <target>MAIN</target>
        </node>
        <node id="5aside" type="aside" label="3270 Attributes">
            <position x="480" y="540"/>
            <narrative>list of 3270 attributes and their meanings.</narrative>
            <cobolparagraph>3270 Attributes</cobolparagraph>
            <link>https://www.magec.com/DOC/magref_main.htm#MAGREF001001</link>
            <target>MAIN</target>
        </node>
        <edge from="5" to="5aside" style="dotted" label="see also"/>


        <node id="6" type="process" label="BA400-NORMALIZE-MAINT-KEY   %NORMKEY" width="200">
  <position x="210" y="640"/>
  <narrative>Normalize the key value entered by the operator — reformat and validate it.</narrative>
  <link>insert_main.htm#insert001026</link>
  <target>MAIN</target>
  <cobolparagraph>BA400-NORMALIZE-MAINT-KEY</cobolparagraph>
</node>

        <node id="7" type="decision" label="Key Result?">
            <position x="210" y="750"/>
            <narrative>Determines the path after key normalization. An invalid key routes to the error path. If this is the DUP function and TEST-KEY equals TWA-LAST-KEY (duplicating the previously displayed record), the flow reads that record by key to get the data to be duplicated. Otherwise (new ADD), the flow proceeds directly toward the screen edit / build steps.</narrative>
        </node>
        <node id="7a" type="elbow" label="">
            <position x="430" y="750"/>
            <narrative>Dthis is the DUP function and TEST-KEY equals TWA-LAST-KEY (duplicating the previously displayed record), the flow reads that record by key to get data to be duplicated. Otherwise (new ADD), the flow proceeds directly toward the screen edit / build steps.</narrative>
        </node>

        <node id="8" type="process" label="AAM410-READ-BY-KEY   %REDKY"><position x="430" y="910"/><narrative>Read the record by key — used by the DUP function to retrieve the source record being duplicated.</narrative><link>insert_main.htm#insert001031</link><target>MAIN</target><cobolparagraph>AAM410-READ-BY-KEY</cobolparagraph></node>

        <node id="9" type="process" label="JA100-LOGICAL-JOIN   %JOIN">
            <position x="430" y="980"/>
            <narrative>Perform logical join validation across related data classes after the duplicate-check read.</narrative>
            <cobolparagraph>JA100-LOGICAL-JOIN</cobolparagraph>
            <link>insert_main.htm#insert001018</link>
            <target>MAIN</target>
        </node>

        <node id="10" type="decision" label="Join Result?">
            <position x="430" y="1090"/>
            <narrative>Evaluates the result of the duplicate-check read and join. NOT FOUND in New Transaction Mode blanks the screen (key is available for use). REC FOUND means a duplicate already exists on file — fill the screen to show it to the operator. NOT FOUND in Continuation Mode means the key is confirmed available — proceed to screen edit and build the new record.</narrative>
        </node>
        <node id="10nfc" type="elbow" label="">
            <position x="210" y="1090"/>
            <narrative>Evaluates the result of the duplicate-check read and join. NOT FOUND in New Transaction Mode blanks the screen (key is available for use). REC FOUND means a duplicate already exists on file — fill the screen to show it to the operator. NOT FOUND in Continuation Mode means the key is confirmed available — proceed to screen edit and build the new record.</narrative>
        </node>
        <node id="10nf" type="elbow" label="">
            <position x="650" y="1090"/>
            <narrative>NOT FOUND in New Transaction Mode blanks the screen (key is available for use). REC FOUND means a duplicate already exists on file — fill the screen to show it to the operator. NOT FOUND in Continuation Mode means the key is confirmed available — proceed to screen edit and build the new record.</narrative>
        </node>

        <node id="11" type="process" label="BB100-BLANK-SCREEN   %BLNKSCR">
            <position x="650" y="1200"/>
            <narrative>Blank the screen — reached when the key was not found in New Transaction Mode, meaning the key is available to use.</narrative>
            <cobolparagraph>BB100-BLANK-SCREEN</cobolparagraph>
            <link>insert_main.htm#insert001003</link>
            <target>MAIN</target>
        </node>

        <node id="12" type="process" label="BB200-FILL-SCREEN   %COMP">
            <position x="430" y="1200"/>
            <narrative>Fill screen with data when a duplicate record is found on file — shown to the operator as a conflict.</narrative>
            <cobolparagraph>BB200-FILL-SCREEN</cobolparagraph>
            <link>insert_main.htm#insert001005</link>
            <target>MAIN</target>
        </node>

        <node id="13" type="process" label="BB500-SCREEN-EDIT   %EDIT">
            <position x="210" y="1200"/>
            <narrative>Perform first-pass screen editing for the new record being added.</narrative>
            <cobolparagraph>BB500-SCREEN-EDIT</cobolparagraph>
            <link>insert_main.htm#insert001009</link>
            <target>MAIN</target>
        </node>

        <node id="13a" type="elbow" label="">
            <position x="30" y="1200"/>
            <narrative>Perform second-pass screen editing.</narrative>
            <cobolparagraph>BB500-SCREEN-EDIT</cobolparagraph>
            <link>insert_main.htm#insert001010</link>
            <target>MAIN</target>
        </node>
        <node id="14" type="process" label="BB500-SCREEN-EDIT-2   %EDIT2">
            <position x="210" y="1300"/>
            <narrative>Perform second-pass screen editing.</narrative>
            <cobolparagraph>BB500-SCREEN-EDIT</cobolparagraph>
            <link>insert_main.htm#insert001010</link>
            <target>MAIN</target>
        </node>
        <node id="14a" type="elbow" label="">
            <position x="30" y="1300"/>
            <narrative>Perform second-pass screen editing.</narrative>
            <cobolparagraph>BB500-SCREEN-EDIT-2</cobolparagraph>
            <link>insert_main.htm#insert001010</link>
            <target>MAIN</target>
        </node>

        <node id="15" type="process" label="BB600-BUILD-REC   %ADDINIT">
            <position x="210" y="1400"/>
            <narrative>Initialize the new record area before building it from screen data.</narrative>
            <cobolparagraph>BB600-BUILD-REC</cobolparagraph>
            <link>insert_main.htm#insert001001</link>
            <target>MAIN</target>
        </node>

        <node id="16" type="process" label="BB600-BUILD-REC   %CALC">
            <position x="210" y="1500"/>
            <narrative>Build the new record from screen data and perform calculations. Note: the RULPROC business rule is inserted immediately after %CALC; it may set the ERROR-FOUND condition, bypassing the add to the database.</narrative>
            <cobolparagraph>BB600-BUILD-REC</cobolparagraph>
            <link>insert_main.htm#insert001004</link>
            <target>MAIN</target>
        </node>
        <node id="16a" type="elbow" label="">
            <position x="30" y="1500"/>
            <narrative>Build the new record from screen data and perform calculations. Note: the RULPROC business rule is inserted immediately after %CALC; it may set the ERROR-FOUND condition, bypassing the add to the database.</narrative>
            <cobolparagraph>BB600-BUILD-REC</cobolparagraph>
            <link>insert_main.htm#insert001004</link>
            <target>MAIN</target>
        </node>

        <node id="17" type="process" label="AAM710-ADD-TO-DATABASE   %ADDIT">
            <position x="210" y="1600"/>
            <narrative>Add the new record to the database.</narrative>
            <cobolparagraph>AAM710-ADD-TO-DATABASE</cobolparagraph>
            <link>insert_main.htm#insert001002</link>
            <target>MAIN</target>
        </node>

        <node id="18" type="decision" label="Rec Added?">
            <position x="210" y="1710"/>
            <narrative>Tests the result of the add.  Rec-Added means the add succeeded (no conflicting record was found). Not Rec-Added means the add failed (probably) because a record already exists — route to the update error path.</narrative>
        </node>

        <node id="19" type="process" label="AAM753-GOODADD   %GOODADD">
            <position x="210" y="1820"/>
            <narrative>Process successful add completion. Routes to AA800-SEND-SCREEN (GO TO TOP OF SEE FUNCTION).</narrative>
            <cobolparagraph>AAM753-GOODADD</cobolparagraph>
            <link>insert_main.htm#insert001013</link>
            <target>MAIN</target>
        </node>
        <node id="19a" type="elbow" label="">
            <position x="30" y="1820"/>
            <narrative>Process successful add completion. Routes to AA800-SEND-SCREEN (GO TO TOP OF SEE FUNCTION).</narrative>
            <cobolparagraph>AAM753-GOODADD</cobolparagraph>
            <link>insert_main.htm#insert001013</link>
            <target>MAIN</target>
        </node>

        <node id="20" type="process" label="AAM751-UPDERR   %UPDERR">
            <position x="430" y="1710"/>
            <narrative>Handle update error condition — the add failed because a conflicting record was found.</narrative>
            <cobolparagraph>AAM751-UPDERR</cobolparagraph>
            <link>insert_main.htm#insert001044</link>
            <target>MAIN</target>
        </node>

        <node id="21a" type="elbow" label="">
            <position x="30" y="1940"/>
            <narrative>Send the resulting screen back to the terminal. SCOMPL usually contains a "completion message" telling what the results were,</narrative>
        </node>

        <node id="21b" type="elbow" label="">
            <position x="650" y="1940"/>
            <narrative>Send the resulting screen back to the terminal. SCOMPL usually contains a "completion message" telling what the results were,</narrative>
        </node>

        <node id="21" type="process" label="AA800-SEND-SCREEN   %SNDSCRN">
            <position x="430" y="1940"/>
            <narrative>Send the resulting screen back to the terminal. All paths converge here: blank screen, fill screen, good add, and update error.</narrative>
            <cobolparagraph>AA800-SEND-SCREEN</cobolparagraph>
            <link>insert_main.htm#insert001039</link>
            <target>MAIN</target>
        </node>

        <node id="22" type="process" label="AA900-GOBACK   %GOBACK">
            <position x="430" y="2040"/>
            <narrative>Return control to the caller (GOBACK).</narrative>
            <cobolparagraph>AA900-GOBACK</cobolparagraph>
            <link>insert_main.htm#insert001012</link>
            <target>MAIN</target>
        </node>

        <node id="99" type="end" label="End">
            <position x="430" y="2140"/>
            <narrative>ADD/DUP Function processing complete.</narrative>
        </node>
        <node id="999" type="comment" label="Figure 06 - ADD/DUP">
            <position x="350" y="2240"/>
            <narrative>Figure 06 - ADD/DUP</narrative>
        </node>

    </nodes>
    <edges>
        <!-- Main top-to-bottom path -->
        <edge from="1" to="2"/>
        <edge from="2" to="3"/>
        <edge from="3" to="4"/>
        <edge from="4" to="5"/>
        <edge from="5" to="6"/>
        <edge from="6" to="7"/>
        <edge from="21a" to="21"/>
        <edge from="21b" to="21"/>

        <!-- Key Result decision -->
        <edge from="7" to="7a" label="DUP-Function &amp;"/>
        <edge from="7a" to="8" label="Test-Key = Last Key"/>
        <edge from="7" to="13" label="New ADD"/>

        <!-- Duplicate-check read path -->
        <edge from="8" to="9"/>
        <edge from="9" to="10"/>

        <!-- Join Result decision -->
        <edge from="10" to="10nf" label="✅ Not Found "/>
        <edge from="10nf" to="11" label="(New Transaction)"/>
        <edge from="10" to="12" label="⚠️ Rec Found (Duplicate)"/>
        <edge from="10" to="10nfc" label="✅ Not Found "/>
        <edge from="10nfc" to="13" label="(Continuation)"/>

        <!-- Blank / fill screen converge to send screen -->
        <edge from="11" to="21b"/>
        <edge from="12" to="21"/>

        <!-- Screen edit chain -->
        <edge from="13" to="13a" label="❌ Error Found"/>
        <edge from="13" to="14" label="✅ No Error Found "/>
        <edge from="14" to="14a" label="❌ Error Found"/>
        <edge from="13a" to="14a"/>
        <edge from="14a" to="21a"/>
        <edge from="14" to="15" label="✅ No Error Found "/>

        <!-- Build record -->
        <edge from="15" to="16"/>
        <edge from="16" to="16a" label="❌ Error Found"/>
        <edge from="16" to="17"/>

        <!-- Add to database -->
        <edge from="17" to="18"/>
        <edge from="18" to="19" label="✅ OK"/>
        <edge from="18" to="20" label="❌ Fail"/>

        <!-- Converge -->
        <edge from="19" to="19a"/>
        <edge from="20" to="21"/>

        <!-- Finish -->
        <edge from="21" to="22"/>
        <edge from="22" to="99"/>
    </edges>
</flowchart>