project QadFinancials > class BDDocument > method DataLoadByInput

Description

This method loads database records corresponding to input values (records found in the t_s*** tables).
When nothing found in the database, a new record should be initialised.
Input values should also be updated with :
- identity fields (override input values)
- tc_Rowid / tc_ParentRowid / tc_Status

Important : code of this method must be overridden, not extended.

Specific: This method has been implemented in this class

REMARK : never pass tDDocumentPostingLine records, because they will be automatically created

Annotation

This template can be used to implement the code for this method.

* Replace [MainTableName] with the name of the main table in the class dataset (no prefixes).
* This template assumes that the main table has an identity field, and this field is the only field of the primary index of the table. Replace [PrimaryKeyFieldName] with the name of this field.
* This template assumes that the table has one logical alternate key (a key that is visible and editable to end-users) with one field. Replace [LogicalKeyFieldName] with the name of this field. If the logical key contains more than one field, repeat the code with this tag for every field in the key.
* The run statement following [METHOD CALL] should not be copied literally, but inserted as a method call.



if oiReturnStatus = 0
then oiReturnStatus = -98.

for each t_s[MainTableName] by rowid(t_s[MainTableName]) on error undo, return:

/* ================================================================= */
/* Lookup object by logical key */
/* When in create mode, skip lookup. */
/* ================================================================= */
if vcActivityCode <> "Create":U
then do:
vcFreeform = "for each [MainTableName] where [MainTableName].[LogicalKeyFieldName] = '":U + t_s[MainTableName].[LogicalKeyFieldName] + "'":U.
[METHOD CALL]
run DataLoad
(input '' (icRowids),
input '' (icPkeys),
input '' (icObjectIds),
input vcFreeform (icFreeform),
input yes (ilKeepPrevious),
output viFcReturnSuper (oiReturnStatus))
if viFcReturnSuper >= 0
then do:
/* DataLoad succeeded, no no-error needed */
find first t[MainTableName] where t[MainTableName].[LogicalKeyFieldName] = t_s[MainTableName].[LogicalKeyFieldName].
if t[MainTableName].tc_Status <> ""
then do:
[METHOD CALL]
run SetMessage
(input [duplicate key error] (icMessage),
input t_s[MainTableName].[LogicalKeyFieldName] (icArguments),
input 't[MainTableName].[LogicalKeyFieldName]':U (icFieldName),
input t_s[MainTableName].[LogicalKeyFieldName] (icFieldValue),
input 'E':U (icType),
input 3 (iiSeverity),
input t_s[MainTableName].tc_Rowid (icRowid),
input 'ERR-xyz':U (icFcMsgNumber),
input '' (icFcExplanation),
input '' (icFcIdentification),
input '' (icFcContext),
output viFcReturnSuper (oiReturnStatus))
assign oiReturnStatus = -1.
return.
end.
end.
end.

/* ================================================================= */
/* When in delete mode, ignore input data. */
/* When object not found, do not return an error. */
/* ================================================================= */
if vcActivityCode = "Delete":U
then do:
if viFcReturnSuper = -4
then delete t_s[MainTableName].
else do:
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
assign t_s[MainTableName].tc_Rowid = t[MainTableName].tc_Rowid
t_s[MainTableName].tc_ParentRowid = ""
ocPrimeKey = (if ocPrimeKey = "" then "" else ocPrimeKey + chr(4))
+ string(t[MainTableName].[PrimaryKeyFieldName]).
end.

next.
end.

if vcActivityCode = "Create":U
or viFcReturnSuper = -4
then do:
/* ================================================================= */
/* When object not found and in modify only mode, return an error. */
/* ================================================================= */
if vcActivityCode = "Modify":U
then do:
[METHOD CALL]
run SetMessage
(input #T'Object with key $1 does not exist.':100T# (icMessage),
input t_s[MainTableName].[LogicalKeyFieldName] (icArguments),
input 't[MainTableName].[LogicalKeyFieldName]':U (icFieldName),
input t_s[MainTableName].[LogicalKeyFieldName] (icFieldValue),
input 'E':U (icType),
input 3 (iiSeverity),
input t_s[MainTableName].tc_Rowid (icRowid),
input 'ERR-xyz':U (icFcMsgNumber),
input '' (icFcExplanation),
input '' (icFcIdentification),
input '' (icFcContext),
output viFcReturnSuper (oiReturnStatus))
assign oiReturnStatus = -1.
return.
end.

/* ================================================================= */
/* When object not found or in create mode, create it. */
/* ================================================================= */
[METHOD CALL]
run AddDetailLine
(input '[MainTableName]':U (icTable),
input '' (icParentRowid),
output viFcReturnSuper (oiReturnStatus))

/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
assign t_s[MainTableName].tc_Status = "N":U.
end.
else do:
/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
assign t_s[MainTableName].tc_Status = "C":U
t[MainTableName].tc_Status = "C":U.
end.

if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

/* insert code for subtables here */

/* ================================================================= */
/* override value for identity fields in input */
/* ================================================================= */
assign t_s[MainTableName].[PrimaryKeyFieldName] = t[MainTableName].[PrimaryKeyFieldName]

/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
t_s[MainTableName].tc_Rowid = t[MainTableName].tc_Rowid
t_s[MainTableName].tc_ParentRowid = ""

ocPrimeKey = (if ocPrimeKey = "" then "" else ocPrimeKey + chr(4))
+ string(t[MainTableName].[PrimaryKeyFieldName]).
end.

if oiReturnStatus = -98
then oiReturnStatus = 0.






If the class dataset contains more than one table, insert this code for every additional table.

/* ================================================================= */
/* Delete existing detail */
/* ================================================================= */
for each t[ChildTableName] where
t[ChildTableName].tc_ParentRowid = t[ParentTableName].tc_Rowid:
assign t[ChildTableName].tc_Status = "D":U.
end.

/* ================================================================= */
/* Update / add detail */
/* ================================================================= */
for each t_s[ChildTableName] where
t_s[ChildTableName].tc_ParentRowid = t_s[ParentTableName].tc_Rowid:

find first t[ChildTableName] where
t[ChildTableName].tc_ParentRowid = t[ParentTableName].tc_Rowid and
t[ChildTableName].[LogicalKeyFieldName] = t_s[ChildTableName].[LogicalKeyFieldName] no-error.
if available t[ChildTableName]
then if t[ChildTableName].tc_Status = "D":U
then assign t_s[ChildTableName].tc_Status = "C":U
t[ChildTableName].tc_Status = "C":U.
else do:
[METHOD CALL]
run SetMessage
(input [duplicate key error] (icMessage),
input t_s[ChildTableName].[LogicalKeyFieldName] (icArguments),
input 't[ChildTableName].[LogicalKeyFieldName]':U (icFieldName),
input t_s[ChildTableName].[LogicalKeyFieldName] (icFieldValue),
input 'E':U (icType),
input 3 (iiSeverity),
input t_s[ChildTableName].tc_Rowid (icRowid),
input 'ERR-xyz':U (icFcMsgNumber),
input '' (icFcExplanation),
input '' (icFcIdentification),
input '' (icFcContext),
output viFcReturnSuper (oiReturnStatus))
assign oiReturnStatus = -1.
return.
end.
else do:
[METHOD CALL]
run AddDetailLine (input '[ChildTableName]':U (icTable),
input t[ParentTableName].tc_Rowid (icParentRowid),
output viFcReturnSuper (oiReturnStatus))
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.
assign t_s[ChildTableName].tc_Status = "N":U
t[ChildTableName].[LogicalKeyFieldName] = t_s[ChildTableName].[LogicalKeyFieldName].
end.

/* ================================================================= */
/* override value for identity fields in input */
/* ================================================================= */
assign t_s[ChildTableName].[ChildPrimaryKeyFieldName] = t[ChildTableName].[ChildPrimaryKeyFieldName]

/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
t_s[ChildTableName].[ParentPrimaryKeyFieldName] = t[ChildTableName].[ParentPrimaryKeyFieldName]
t_s[ChildTableName].tc_Rowid = t[ChildTableName].tc_Rowid
t_s[ChildTableName].tc_ParentRowid = t[ParentTableName].tc_Rowid.
end.

PreCondition

The code of this method must always be overridden, not extended.


Parameters


opPrimeKeyoutputlongcharPrimary Key field value of loaded record(s).
Keys are chr(4) separated.
Key fields are chr(2) separated.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program2/bddocument.p)

assign oiReturnStatus       = -98
       viLocalReturnStatus  = 0.

PROCESSBLOCK:


for each t_sDDocument 
         by t_sDDocument.tc_Rowid:
    
    /* ===================================================================== */
    /* if you don't fill the logical key, you can say you are in create mode */
    /* ===================================================================== */
    if (t_sDDocument.DDocumentType      = "":U  or t_sDDocument.DDocumentType   = ?) or
       (t_sDDocument.DDocumentYear      = 0     or t_sDDocument.DDocumentYear   = ? ) or
       (t_sDDocument.DDocumentNumber    = 0     or t_sDDocument.DDocumentNumber = ?)
    then assign vcActivityCode = "Create":U.

    /* =========================== */
    /* Fill company_id when blank. */
    /* =========================== */
    if t_sDDocument.Company_ID = 0 or
       t_sDDocument.Company_ID = ?
    then assign t_sDDocument.Company_ID = viCompanyId.    

    /* ================================= */
    /* Lookup object by logical key      */
    /* When in create mode, skip lookup. */
    /* ================================= */
    if vcActivityCode <> "Create":U
    then do:
        assign vcFreeform = "for each DDocument where DDocument.Company_ID = ":U + string(t_sDDocument.Company_ID) +
                            " and DDocument.DDocumentType = '":U + t_sDDocument.DDocumentType + "'":U +
                            " and DDocument.DDocumentYear = ":U + string(t_sDDocument.DDocumentYear) + 
                            " and DDocument.DDocumentNumber = ":U + string(t_sDDocument.DDocumentNumber).
        <M-1 run DataLoad
           (input  '':U (icRowids), 
            input  '':U (icPkeys), 
            input  '':U (icObjectIds), 
            input  vcFreeForm (icFreeform), 
            input  true (ilKeepPrevious), 
            output viFcReturnSuper (oiReturnStatus)) in BDDocument>

        
        if viFcReturnSuper = -4 and vcActivityCode = "Delete":U
        then do:
            /* ============================================== */
            /* When in delete mode, ignore input data.        */
            /* When object not found, do not return an error. */
            /* ============================================== */
            delete t_sDDocument.
            next.
        end.
        else do:
            if viFcReturnSuper <> 0
            then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave PROCESSBLOCK.
        
            find first tDDocument where 
                       tDDocument.Company_ID       = t_sDDocument.Company_ID    and
                       tDDocument.DDocumentType    = t_sDDocument.DDocumentType and
                       tDDocument.DDocumentYear    = t_sDDocument.DDocumentYear and
                       tDDocument.DDocumentNumber  = t_sDDocument.DDocumentNumber
                       no-error.
            if not available tDDocument
            then do:
                assign vcMessage            = trim(#T-18'Unable to find Customer Payment.':255(999890196)T-18#)                                         + chr(10) +
                                              trim(substitute(#T-19'Company ID: &1':255(69637)T-19#, trim(string(t_sDDocument.Company_ID)) ))           + chr(10) + 
                                              trim(substitute(#T-20'Document Type: &1':255(999890193)T-20#, trim(t_sDDocument.DDocumentType) ))         + chr(10) +
                                              trim(substitute(#T-21'Document Year: &1':222(999890194)T-21#, trim(string(t_sDDocument.DDocumentYear)) )) + chr(10) +
                                              trim(substitute(#T-22'Document Number: &1':200(999890195)T-22#, trim(string(t_sDDocument.DDocumentNumber)) ))
                       viLocalReturnStatus  = -1.
                <M-17 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'S':U (icType), 
                    input  3 (iiSeverity), 
                    input  t_sDDocument.tc_Rowid (icRowid), 
                    input  'QadFin-8977':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDDocument>
                next.
            end. /* end if not available tDDocument */

            /* ====================================== */
            /* Update input to allow correct matching */
            /* ====================================== */
            assign t_sDDocument.tc_Rowid       = tDDocument.tc_Rowid
                   t_sDDocument.tc_ParentRowid = "":U
                   opPrimeKey                  = (if opPrimeKey = "":U 
                                                  then "":U 
                                                  else opPrimeKey + chr(4)) + string(tDDocument.DDocument_ID).
        end. /* end if viFcReturnSuper = -4 ... */
    end. /* if vcActivityCode <> "Create":U or */

    if vcActivityCode  = "ExternalCreate":U or
       vcActivityCode  = "Create":U 
    then do:
        /* ====================================================== */
        /* put this value on zero, so that he will get new number */
        /* ====================================================== */
        assign t_sDDocument.DDocumentNumber = 0.

        /* ======================================= */
        /* When object in create mode, create it.  */
        /* ======================================= */
        <M-8 run AddDetailLine
           (input  'DDocument':U (icTable), 
            input  '':U (icParentRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BDDocument>
        if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then leave PROCESSBLOCK.

        /* ====================================== */
        /* Update input to allow correct matching */
        /* ====================================== */
        assign t_sDDocument.tc_Status     = "N":U
               /* fill the primary key with the t table values that are filled in initialvalues when not filled, needed for getnumber */
               t_sDDocument.Company_ID    = if t_sDDocument.Company_ID = ? or t_sDDocument.Company_ID = 0
                                            then tDDocument.Company_ID
                                            else t_sDDocument.Company_ID
               t_sDDocument.DDocumentType = if t_sDDocument.DDocumentType = ? or t_sDDocument.DDocumentType = "":U
                                            then tDDocument.DDocumentType
                                            else t_sDDocument.DDocumentType
               t_sDDocument.DDocumentYear = if t_sDDocument.DDocumentYear = ? or t_sDDocument.DDocumentYear = 0
                                            then tDDocument.DDocumentYear
                                            else t_sDDocument.DDocumentYear.
    end.  /* if vcActivityCode = Create */
    else 
        /* ====================================== */
        /* Update input to allow correct matching */
        /* ====================================== */
    assign t_sDDocument.tc_Status = "C":U.
    assign t_sDDocument.DDocument_ID = tDDocument.DDocument_ID.
   
    /* Determine BankNumber_ID if not specified */
    if t_sDDocument.BankNumber_ID = ? or t_sDDocument.BankNumber_ID = 0
       
    then do:
        if viBBankNumber2ID = 0 or viBBankNumber2ID = ?
        then do:
            <I-33 {bFcStartAndOpenInstance
                 &ADD-TO-TRANSACTION = "true"
                 &CLASS              = "BBankNumber"}>
        end.
        else do:
            <I-34 {bFcOpenInstance
                 &CLASS           = "BBankNumber"}>
        end.

        <M-35 run GetParentBankData
           (input  t_sDDocument.Company_ID (iiCompanyID), 
            input  ? (icCreditorCode), 
            input  t_sDDocument.tcDebtorCode (icDebtorCode), 
            input  t_sDDocument.DDocumentType (icPaymentInstrument), 
            output t_sDDocument.BankNumber_ID (oiBankNumberID), 
            output t_sDDocument.tcDebtorBankNumber (ocBankNumber), 
            output vcDummy (ocBankNumberExtension), 
            input  t_sDDocument.tcOwnBankNumber (icOwnBankNumber), 
            input  t_sDDocument.tcGLCode (icGLCode), 
            output viFcReturnSuper (oiReturnStatus)) in BBankNumber>
        if viFcReturnSuper <> 0 then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0
        then do:
            assign vcMessage           = trim(substitute(#T-39'Failed to retrieve bank information for customer &1':255(591273430)T-39#, t_sDDocument.tcDebtorCode)) + chr(10) +
                                         trim(substitute(#T-40'Company ID: &1':255(69637)T-40#, trim(string(t_sDDocument.Company_ID)) ))           + chr(10) + 
                                         trim(substitute(#T-41'Document Type: &1':255(999890193)T-41#, trim(t_sDDocument.DDocumentType) ))         + chr(10) +
                                         trim(substitute(#T-42'Document Year: &1':222(999890194)T-42#, trim(string(t_sDDocument.DDocumentYear)) )) + chr(10) +
                                         trim(substitute(#T-43'Document Number: &1':200(999890195)T-43#, trim(string(t_sDDocument.DDocumentNumber)) )).
            <M-38 run SetMessage
               (input  vcMessage (icMessage), 
                input  '':U (icArguments), 
                input  '':U (icFieldName), 
                input  '':U (icFieldValue), 
                input  'W':U (icType), 
                input  3 (iiSeverity), 
                input  t_sDDocument.tc_Rowid (icRowid), 
                input  'QadFin-9555':U:U (icFcMsgNumber), 
                input  '':U (icFcExplanation), 
                input  '':U (icFcIdentification), 
                input  '':U (icFcContext), 
                output viFcReturnSuper (oiReturnStatus)) in BDDocument>
            next.
        end.
    end.
    
    if t_sDDocument.BankNumber_ID <> ? and 
       t_sDDocument.BankNumber_ID <> 0 and 
      (t_sDDocument.tcDebtorBankNumber = "":U or 
       t_sDDocument.tcDebtorBankNumber = ?     )  
    then do :
       <Q-72 run BankNumberByID (all) (Read) (Cache)
          (input t_sDDocument.BankNumber_ID, (BankNumber_ID)
           input ?, (CompanyId)
           output dataset tqBankNumberByID) in BBankNumber>
         for first tqBankNumberByID where 
                   tqBankNumberByID.tiBankNumber_ID = t_sDDocument.BankNumber_ID  :
             assign   t_sDDocument.tcDebtorBankNumber = tqBankNumberByID.tcBankNumber .    
         end.        
     end. 
    /* Determine PayFormatTypeCode */
    if t_sDDocument.BankNumber_ID <> ? and t_sDDocument.BankNumber_ID <> 0 and
      ((t_sDDocument.tcPayFormatTypeCode = ? or t_sDDocument.tcPayFormatTypeCode = "":U) or
      ((tDDocument.GL_ID = ? or tDDocument.GL_ID = 0) and
      (tDDocument.tcGLCode = ? or tDDocument.tcGLCode = "":U)))
    then do:
        <Q-44 run BankNumberOwnBankGLByID (all) (Read) (Cache)
           (input t_sDDocument.BankNumber_ID, (BankNumber_ID)
            input ?, (CompanyId)
            output dataset tqBankNumberOwnBankGLByID) in BBankNumber>
        for first tqBankNumberOwnBankGLByID where
                  tqBankNumberOwnBankGLByID.tiBankNumber_ID = t_sDDocument.BankNumber_ID:
            assign t_sDDocument.tcPayFormatTypeCode = tqBankNumberOwnBankGLByID.tcPayFormatTypeCode
                   t_sDDocument.tcOwnBankNumber     = tqBankNumberOwnBankGLByID.tcOwnBankNumber
                   t_sDDocument.tcGLCode            = tqBankNumberOwnBankGLByID.tcGLCode
                   t_sDDocument.GL_ID               = tqBankNumberOwnBankGLByID.tiGL_ID.
        end.
    end.

    /* ============================= */
    /* Code for DDocumentPostingLine */
    /* Delete existing detail        */
    /* ============================= */
    for each tDDocumentPostingLine where
             tDDocumentPostingLine.tc_ParentRowid = tDDocument.tc_Rowid:
        assign tDDocumentPostingLine.tc_Status = "D":U.
    end. /* for each tDDocumentPostingLine where */

    /* =================== */
    /* Update / add detail */
    /* =================== */
    if can-find(first t_sDDocumentPostingLine where
                      t_sDDocumentPostingLine.tc_ParentRowid = t_sDDocument.tc_Rowid)
    then do:
        assign vcMessage           = trim(#T-27'It's not needed to pass Customer Payment Posting Line Records, they will be automatically created.':255(999890300)T-27#)                                         + chr(10) +
                                     trim(substitute(#T-28'Company ID: &1':255(69637)T-28#, trim(string(t_sDDocument.Company_ID)) ))           + chr(10) + 
                                     trim(substitute(#T-29'Document Type: &1':255(999890193)T-29#, trim(t_sDDocument.DDocumentType) ))         + chr(10) +
                                     trim(substitute(#T-30'Document Year: &1':222(999890194)T-30#, trim(string(t_sDDocument.DDocumentYear)) )) + chr(10) +
                                     trim(substitute(#T-31'Document Number: &1':200(999890195)T-31#, trim(string(t_sDDocument.DDocumentNumber)) ))
               viLocalReturnStatus = +1.
        <M-32 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'W':U (icType), 
            input  3 (iiSeverity), 
            input  t_sDDocument.tc_Rowid (icRowid), 
            input  'QadFin-8998':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDDocument>
                next.
    end. /* end can-find t_sDDocumentPostingLine */
    
    /* =========================================================== */
    /* delete passed records, will be created in additionalupdates */
    /* =========================================================== */
    for each t_sDDocumentPostingLine where
             t_sDDocumentPostingLine.tc_ParentRowid = t_sDDocument.tc_Rowid :
        assign t_sDDocumentPostingLine.tc_Status = "D":U.
    end.

    /* ============================= */
    /* Code for DDocumentInvoiceXref */
    /* Delete existing detail        */
    /* ============================= */
    for each tDDocumentInvoiceXref where
             tDDocumentInvoiceXref.tc_ParentRowid = tDDocument.tc_Rowid:
        assign tDDocumentInvoiceXref.tc_Status = "D":U.
    end. /* for each tDDocumentInvoiceXref where */

    /* =================== */
    /* Update / add detail */
    /* =================== */
    for each t_sDDocumentInvoiceXref where
             t_sDDocumentInvoiceXref.tc_ParentRowid = t_sDDocument.tc_Rowid and
             t_sDDocumentInvoiceXref.tc_Status     <> "D":U:

        assign t_sDDocumentInvoiceXref.DDocument_ID = t_sDDocument.DDocument_ID.

        find first tDDocumentInvoiceXref where
                   tDDocumentInvoiceXref.tc_ParentRowid = tDDocument.tc_Rowid                 and
                   tDDocumentInvoiceXref.DInvoice_ID    = t_sDDocumentInvoiceXref.DInvoice_ID and
                   (tDDocumentInvoiceXref.DInvoice_ID <> 0 and tDDocumentInvoiceXref.DInvoice_ID <> ?)
                   no-error.
        if available tDDocumentInvoiceXref
        then assign t_sDDocumentInvoiceXref.tc_Status   = "C":U.
        else do:
            <M-14 run AddDetailLine
               (input  'DDocumentInvoiceXref':U (icTable), 
                input  tDDocument.tc_Rowid (icParentRowid), 
                output viFcReturnSuper (oiReturnStatus)) in BDDocument>
            if viFcReturnSuper <> 0
            then assign viLocalReturnStatus = viFcReturnSuper.
            if viFcReturnSuper < 0 then leave PROCESSBLOCK.

            assign t_sDDocumentInvoiceXref.tc_Status = "N":U.
        end. /* else do: */    

        assign viDInvoicePostingYear = integer(entry(1, t_sDDocumentInvoiceXref.tcDInvoiceReference, "/":U))
               viDInvoiceVoucher     = integer(entry(3, t_sDDocumentInvoiceXref.tcDInvoiceReference, "/":U))
               vcJournalCode         = entry(2, t_sDDocumentInvoiceXref.tcDInvoiceReference, "/":U) no-error.
        if error-status:error
        then do:
            assign viLocalReturnStatus = -3
                   vcMessage           = trim(substitute(#T-23'Internal error: unable to get the invoice posting year, daybook and invoice voucher out of the invoice reference &1.':255(999890245)T-23#,t_sDDocumentInvoiceXref.tcDInvoiceReference)).
            if error-status:num-messages > 0
            then assign vcMessage = vcMessage + chr(10) + 
                                    trim(substitute(#T-24'Detailed info: &1 (&2)':255(16477)T-24#,error-status:Get-Message(1),string(error-status:Get-Number(1)))).
            <M-25 run SetMessage
                   (input  vcMessage (icMessage), 
                    input  '':U (icArguments), 
                    input  '':U (icFieldName), 
                    input  '':U (icFieldValue), 
                    input  'S':U (icType), 
                    input  3 (iiSeverity), 
                    input  t_sDDocument.tc_Rowid (icRowid), 
                    input  'QadFin-8978':U (icFcMsgNumber), 
                    input  '':U (icFcExplanation), 
                    input  '':U (icFcIdentification), 
                    input  '':U (icFcContext), 
                    output viFcReturnSuper (oiReturnStatus)) in BDDocument>
            leave PROCESSBLOCK.
        end.

        if not vlStartDInvoice
        then do:
            <Q-26 run DInvoiceByYearJournalVoucher  (Start) in BDInvoice >
            assign vlStartDInvoice = true.
        end.
        if t_sDDocumentInvoiceXref.tcCompanyCode <> "":U or 
           t_sDDocumentInvoiceXref.tcCompanyCode <> ?
        then do :
            <Q-84 run CompanyByCompanyIdCode (all) (Read) (NoCache)
               (input ?, (CompanyId)
                input t_sDDocumentInvoiceXref.tcCompanyCode, (CompanyCode)
                output dataset tqCompanyByCompanyIdCode) in BCompany>
            find first tqCompanyByCompanyIdCode 
                 where tqCompanyByCompanyIdCode.tcCompanyCode = t_sDDocumentInvoiceXref.tcCompanyCode no-error .
            if available tqCompanyByCompanyIdCode 
            then assign  viInvCompanyID = tqCompanyByCompanyIdCode.tiCompany_ID .
        end.
        else assign  viInvCompanyID = ? .   
        
        <Q-13 run DInvoiceByYearJournalVoucher (all) (Read) (Cache)
           (input viInvCompanyID, (CompanyId)
            input viDInvoicePostingYear, (DInvoicePostingYear)
            input vcJournalCode, (JournalCode)
            input viDInvoiceVoucher, (DInvoiceVoucher)
            output dataset tqDInvoiceByYearJournalVoucher) in BDInvoice>
                                     
        find first tqDInvoiceByYearJournalVoucher where
                   tqDInvoiceByYearJournalVoucher.tiCompany_ID          =  viInvCompanyID           and
                   tqDInvoiceByYearJournalVoucher.tiDInvoicePostingYear = viDInvoicePostingYear     and
                   tqDInvoiceByYearJournalVoucher.tcJournalCode         = vcJournalCode             and
                   tqDInvoiceByYearJournalVoucher.tiDInvoiceVoucher     = viDInvoiceVoucher
                   no-error.
                       
        if available tqDInvoiceByYearJournalVoucher
        then assign t_sDDocumentInvoiceXref.DInvoice_ID           = tqDInvoiceByYearJournalVoucher.tiDInvoice_ID
                    t_sDDocumentInvoiceXref.tcCurrencyCode        = tqDInvoiceByYearJournalVoucher.tcCurrencyCode
                    t_sDDocumentInvoiceXref.tcDivisionCode        = tqDInvoiceByYearJournalVoucher.tcDivisionCode
                    t_sDDocumentInvoiceXref.tcAllocAmountCrDt     = if tqDInvoiceByYearJournalVoucher.tdDInvoiceOriginalCreditTC <> 0 and
                                                                       tqDInvoiceByYearJournalVoucher.tdDInvoiceOriginalCreditTC <> ?
                                                                    then {&CREDITDEBITABBREVIATION-DEBIT}
                                                                    else {&CREDITDEBITABBREVIATION-CREDIT}
                    t_sDDocumentInvoiceXref.tcDInvoiceBalanceCrDt = if tqDInvoiceByYearJournalVoucher.tdDInvoiceOriginalCreditTC <> 0 and
                                                                       tqDInvoiceByYearJournalVoucher.tdDInvoiceOriginalCreditTC <> ?
                                                                    then {&CREDITDEBITABBREVIATION-CREDIT}
                                                                    else {&CREDITDEBITABBREVIATION-DEBIT}.
        /* ==================================== */
        /* Code needed for DDocInvoiceXrefStage */
        /* ==================================== */
        assign t_sDDocumentInvoiceXref.DDocumentInvoiceXref_ID = tDDocumentInvoiceXref.DDocumentInvoiceXref_ID.
        
        /* ====================== */
        /* Delete existing detail */
        /* ====================== */
        for each tDDocInvoiceXrefStage where
                 tDDocInvoiceXrefStage.tc_ParentRowid = tDDocumentInvoiceXref.tc_Rowid:
            assign tDDocInvoiceXrefStage.tc_Status = "D":U.
        end. /* for each tDDocInvoiceXrefStage where */
    
        /* =================== */
        /* Update / add detail */
        /* =================== */
        for each t_sDDocInvoiceXrefStage where
                 t_sDDocInvoiceXrefStage.tc_ParentRowid = t_sDDocumentInvoiceXref.tc_Rowid and
                 t_sDDocInvoiceXrefStage.tc_Status     <> "D":U:
    
            find first tDDocInvoiceXrefStage where
                       tDDocInvoiceXrefStage.tc_ParentRowid             = tDDocumentInvoiceXref.tc_Rowid              and                    
                       tDDocInvoiceXrefStage.DInvoiceStage_ID           = t_sDDocInvoiceXrefStage.DInvoiceStage_ID    and
                       (tDDocInvoiceXrefStage.DInvoiceStage_ID <> 0 and tDDocInvoiceXrefStage.DInvoiceStage_ID <> ?)
                       
                       no-error.
            if available tDDocInvoiceXrefStage
            then assign t_sDDocInvoiceXrefStage.tc_Status = "C":U.
            else do:
                <M-11 run AddDetailLine
                   (input  'DDocInvoiceXrefStage':U (icTable), 
                    input  tDDocumentInvoiceXref.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BDDocument>
                if viFcReturnSuper <> 0
                then assign viLocalReturnStatus = viFcReturnSuper.
                if viFcReturnSuper < 0 then leave PROCESSBLOCK.
                
                assign t_sDDocInvoiceXrefStage.tc_Status = "N":U.
            end. /* else do: */
    
            /* ================================================================= */
            /* override value for identity fields in input (DDocInvoiceXrefStage)          */
            /* ================================================================= */
            assign t_sDDocInvoiceXrefStage.DDocInvoiceXrefStage_ID  = tDDocInvoiceXrefStage.DDocInvoiceXrefStage_ID
    
                   /* ====================================== */
                   /* Update input to allow correct matching */
                   /* ====================================== */
                   t_sDDocInvoiceXrefStage.tc_Rowid                 = tDDocInvoiceXrefStage.tc_Rowid
                   t_sDDocInvoiceXrefStage.tc_ParentRowid           = tDDocumentInvoiceXref.tc_Rowid.
        end.  /* for each t_sDDocInvoiceXrefStage */
        /* ================================================================= */
        /* override value for identity fields in input (DDocumentInvoiceXref)         */
        /* ================================================================= */
        assign t_sDDocumentInvoiceXref.DDocumentInvoiceXref_ID  = tDDocumentInvoiceXref.DDocumentInvoiceXref_ID
            
               /* ====================================== */
               /* Update input to allow correct matching */
               /* ====================================== */
               t_sDDocumentInvoiceXref.DDocument_ID             = tDDocumentInvoiceXref.DDocument_ID
               t_sDDocumentInvoiceXref.tc_Rowid                 = tDDocumentInvoiceXref.tc_Rowid
               t_sDDocumentInvoiceXref.tc_ParentRowid           = tDDocument.tc_Rowid.       
    end. /* end for each t_sDDocumentInvoiceXref */
   
    if t_sDDocument.tc_Status = "N":U and
       t_sDDocument.Company_ID      <> 0    and t_sDDocument.Company_ID     <> ? and
       t_sDDocument.DDocumentType   <> "":U and t_sDDocument.DDocumentType  <> ? and
       t_sDDocument.DDocumentYear   <> 0    and t_sDDocument.DDocumentYear  <> ? and
      (t_sDDocument.DDocumentNumber = 0     or  t_sDDocument.DDocumentNumber = ?)
    then do:
        assign vhFcComponent = ?.
        <M-12 run GetNumber
           (input  t_sDDocument.Company_ID (iiCompanyId), 
            input  t_sDDocument.DDocumentYear (iiNumbrYear), 
            input  'D':U + t_sDDocument.DDocumentType (icNumbrType), 
            output t_sDDocument.DDocumentNumber (oiNumber), 
            input  viFcCurrentInstanceId (iiInstanceId), 
            input  vcFcComponentName (icClassName), 
            output viFcReturnSuper (oiReturnStatus)) in BNumber>
        if viFcReturnSuper <> 0
        then assign viLocalReturnStatus = viFcReturnSuper.
        if viFcReturnSuper < 0 then leave PROCESSBLOCK.
    end.

    /* ======================================================= */
    /* override value for identity fields in input (DDocument) */
    /* ======================================================= */            
    assign t_sDDocument.DDocument_ID        = tDDocument.DDocument_ID
           t_sDDocument.Company_ID          = tDDocument.Company_ID
           t_sDDocument.CreationUsr_ID      = viUsrId
           t_sDDocument.tcCreationUsrLogin  = vcUserLogin
        
           /* ====================================== */
           /* Update input to allow correct matching */
           /* ====================================== */
           t_sDDocument.tc_Rowid             = tDDocument.tc_Rowid
           t_sDDocument.tc_ParentRowid       = "":U
           opPrimeKey                        = (if opPrimeKey = "":U 
                                                then "":U 
                                                else opPrimeKey + chr(4)) + string(tDDocument.DDocument_ID).
end. /* PROCESSBLOCK */

     


if vlStartDInvoice
then do:
    <Q-16 run DInvoiceByYearJournalVoucher  (Stop) in BDInvoice >
end.

assign oiReturnStatus = viLocalReturnStatus.