project QadFinancials > class BDInvoice > method StdMaintainTTWithExtPost

Description

copy the tables, make no di posting : WITHOUT ValidateBC, AddUpd and DataSave


Parameters


icActioninputcharacter
bcLstReturninput-outputcharacter
bcLstPrimKeyinput-outputcharacter
bcLstRowIdinput-outputcharacter
olSaveAsDraftoutputlogicalSaveSaDraft
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BDInvoice.StdMaintainTT


program code (program7/bdinvoice.p)

/* ========================= */
/* Set default return status */
/* ========================= */
assign oiReturnStatus = -98.

/* ================================== */
/* Initialise and Create new instance */
/* Validate the value for the Company */
/* ================================== */
assign vlWarningsFound = false.
<M-48 run DataNew (output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
if viFcReturnSuper <> 0
then do:
    assign oiReturnStatus = viFcReturnSuper.
    if oiReturnStatus < 0
    then do: 
        assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
               bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
               bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
               bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
        return.
    end. /* if oiReturnStatus < 0 */
    else assign vlWarningsFound = true.
end. /* if viFcReturnSuper <> 0 */ 
if tApiDInvoice.Company_ID <> 0 and
   tApiDInvoice.Company_ID <> ? and 
   tApiDInvoice.Company_ID <> viCompanyId
then do :
    assign vcMessage      = trim(substitute(#T-59'The entity of the customer invoice (&1) must match the current entity (&2).':255(14472)T-59#, string(tApiDInvoice.Company_ID),string(viCompanyId)))
           oiReturnStatus = -1
           bcLstReturn    = bcLstReturn  + chr(4) + string(-1)
           bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
           bcLstRowid     = bcLstRowid   + chr(4) + "*":U
           vlSaveAsDraft  = false.
    <M-49 run SetMessage (input  vcMessage (icMessage),
                     input  '':U (icArguments),
                     input  '':U (icFieldName),
                     input  '':u (icFieldValue),
                     input  'E':U (icType),
                     input  3 (iiSeverity),
                     input  '':U (icRowid),
                     input  'QadFin-7176':U (icFcMsgNumber),
                     input  '' (icFcExplanation),
                     input  '' (icFcIdentification),
                     input  '' (icFcContext),
                     output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    Return.
end. /* if tApiDInvoice.Company_ID <> 0 and */
assign tDInvoice.Company_ID = viCompanyId.

/* =========================================================================================== */
/* Copy all data into the new Dinvoice                                                         */
/* Before continuing, we copy the primary-key-fields of the main table to the input parameters */
/* This is just to hold it there so it can be assigned to ocPrimKey if the save succeeds. This */
/* is done because tDInvoice is no longer available after the save. We use ocRowid in stead    */
/* =========================================================================================== */
buffer-copy tApiDInvoice 
    except DInvoice_ID  
           company_ID
           tc_rowid 
           tc_ParentRowid 
           tc_status
    to tDInvoice.
assign tApiDInvoice.DInvoice_ID        = tDInvoice.DInvoice_ID
       vcDInvoiceRowId                 = tDInvoice.tc_Rowid
       tDInvoice.tcLocalCurrencyCode   = vcCompanyLC
       tDInvoice.tdDInvoiceOriginalTC  = if tDInvoice.tdDInvoiceOriginalTC <> 0 and tDInvoice.tdDInvoiceOriginalTC <> ?
                                         then tDInvoice.tdDInvoiceOriginalTC
                                         else if tDInvoice.DInvoiceType = {&INVOICETYPE-INVOICE} or tDInvoice.DInvoiceType = {&INVOICETYPE-FINANCECHARGE} or tDInvoice.DInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                              then tDInvoice.DInvoiceOriginalDebitTC
                                              else tDInvoice.DInvoiceOriginalCreditTC
       tDInvoice.tdDInvoiceOriginalLC  = if tDInvoice.tdDInvoiceOriginalLC <> 0 and tDInvoice.tdDInvoiceOriginalLC <> ?
                                         then tDInvoice.tdDInvoiceOriginalLC
                                         else if tDInvoice.DInvoiceType = {&INVOICETYPE-INVOICE} or tDInvoice.DInvoiceType = {&INVOICETYPE-FINANCECHARGE} or tDInvoice.DInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
                                              then tDInvoice.DInvoiceOriginalDebitLC
                                              else tDInvoice.DInvoiceOriginalCreditLC
       tDInvoice.tdTotalAmountDebitTC  = tDInvoice.DInvoiceVatBaseDebitTC + tDInvoice.DInvoiceVatDebitTC
       tDInvoice.tdTotalAmountCreditTC = tDInvoice.DInvoiceVatBaseCreditTC + tDInvoice.DInvoiceVatCreditTC.

/* ======================= */
/* Default unfilled values */
/* ======================= */ 
assign tDInvoice.DInvoiceIsOpen           = true
       tDInvoice.DInvoiceIsSelected       = false
       tDInvoice.DInvoiceIsPaymentAllowed = false
       vcCommitNumber                     = vcCommitNumber + ",":U + tDInvoice.tc_Rowid.

/* If no ship from address is specified the default from the company head-office address */
if ((tDInvoice.ShipFromAddress_ID = 0 or tDInvoice.ShipFromAddress_ID = ?) and
    tDInvoice.tcShipFromBusinessRelationCode = "":U or tDInvoice.tcShipFromBusinessRelationCode = ?)
then do:
    <Q-41 run CompanyPropertyByActBusRelAddr (all) (Read) (NoCache)
       (input viCompanyId, (CompanyId)
        output dataset tqCompanyPropertyByActBusRelAddr) in BCompanyProperty >
        
    find first tqCompanyPropertyByActBusRelAddr no-error.
    if available tqCompanyPropertyByActBusRelAddr then do:
       <Q-42 run AddressByAddressBusRelType (all) (Read) (NoCache)
          (input ?, (AddressTypeId)
           input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressTypeCode)
           input tqCompanyPropertyByActBusRelAddr.tiBusinessRelation_ID, (BusinessRelationId)
           input '':U, (BusinessRelationCode)
           input ?, (AddressId)
           output dataset tqAddressByAddressBusRelType) in BBusinessRelation >
    
        find first tqAddressByAddressBusRelType no-error.
        if available tqAddressByAddressBusRelType then do:
            assign tDInvoice.ShipFromAddress_ID = tqAddressByAddressBusRelType.tiAddress_ID
                tDInvoice.tcShipFromBusinessRelationCode = tqAddressByAddressBusRelType.tcBusinessRelationCode
                tDInvoice.tcShipFromAddressCity = tqAddressByAddressBusRelType.tcAddressCity
                tDInvoice.tcShipFromAddressState = tqAddressByAddressBusRelType.tcAddressState
                tDInvoice.tcShipFromAddressStreet1 = tqAddressByAddressBusRelType.tcAddressStreet1
                tDInvoice.tcShipFromAddressStreet2 = tqAddressByAddressBusRelType.tcAddressStreet2
                tDInvoice.tcShipFromAddressStreet3 = tqAddressByAddressBusRelType.tcAddressStreet3
                tDInvoice.tcShipFromAddressTypeCode = tqAddressByAddressBusRelType.tcAddressTypeCode
                tDInvoice.tcShipFromAddressZip = tqAddressByAddressBusRelType.tcAddressZip
                tDInvoice.tcShipFromBusinessRelationCode = tqAddressByAddressBusRelType.tcBusinessRelationCode
                tDInvoice.tcShipFromCountryCode = tqAddressByAddressBusRelType.tcCountryCode.  
                
        end. 
    end.
end.      

/* Populate the ship from address fields from the id */
if ((tDInvoice.ShipFromAddress_ID <> 0 and tDInvoice.ShipFromAddress_ID <> ?) and
    (tDInvoice.tcShipFromBusinessRelationCode = "":U or tDInvoice.tcShipFromBusinessRelationCode = ?))
then do:
       <Q-43 run AddressByAddressBusRelType (all) (Read) (NoCache)
          (input ?, (AddressTypeId)
           input '':U, (AddressTypeCode)
           input ?, (BusinessRelationId)
           input '':U, (BusinessRelationCode)
           input tDInvoice.ShipFromAddress_ID, (AddressId)
           output dataset tqAddressByAddressBusRelType) in BBusinessRelation >
       find first tqAddressByAddressBusRelType no-error.
       if available tqAddressByAddressBusRelType then
            assign tDInvoice.tcShipFromBusinessRelationCode = tqAddressByAddressBusRelType.tcBusinessRelationCode
                tDInvoice.tcShipFromAddressCity = tqAddressByAddressBusRelType.tcAddressCity
                tDInvoice.tcShipFromAddressState = tqAddressByAddressBusRelType.tcAddressState
                tDInvoice.tcShipFromAddressStreet1 = tqAddressByAddressBusRelType.tcAddressStreet1
                tDInvoice.tcShipFromAddressStreet2 = tqAddressByAddressBusRelType.tcAddressStreet2
                tDInvoice.tcShipFromAddressStreet3 = tqAddressByAddressBusRelType.tcAddressStreet3
                tDInvoice.tcShipFromAddressTypeCode = tqAddressByAddressBusRelType.tcAddressTypeCode
                tDInvoice.tcShipFromAddressZip = tqAddressByAddressBusRelType.tcAddressZip
                tDInvoice.tcShipFromBusinessRelationCode = tqAddressByAddressBusRelType.tcBusinessRelationCode
                tDInvoice.tcShipFromCountryCode = tqAddressByAddressBusRelType.tcCountryCode.
end.

/* Populate the ship to address fields from the id */
if ((tDInvoice.ShipToAddress_ID <> 0 and tDInvoice.ShipToAddress_ID <> ?) and
    (tDInvoice.tcShipToBusinessRelationCode = "":U or tDInvoice.tcShipToBusinessRelationCode = ?))
then do:
       <Q-44 run AddressByAddressBusRelType (all) (Read) (NoCache)
          (input ?, (AddressTypeId)
           input '':U, (AddressTypeCode)
           input ?, (BusinessRelationId)
           input '':U, (BusinessRelationCode)
           input tDInvoice.ShipToAddress_ID, (AddressId)
           output dataset tqAddressByAddressBusRelType) in BBusinessRelation >
       find first tqAddressByAddressBusRelType no-error.
       if available tqAddressByAddressBusRelType then do:
            assign tDInvoice.tcShipToBusinessRelationCode = tqAddressByAddressBusRelType.tcBusinessRelationCode
                tDInvoice.tcShipToAddressCity = tqAddressByAddressBusRelType.tcAddressCity
                tDInvoice.tcShipToAddressState = tqAddressByAddressBusRelType.tcAddressState
                tDInvoice.tcShipToAddressStreet1 = tqAddressByAddressBusRelType.tcAddressStreet1
                tDInvoice.tcShipToAddressStreet2 = tqAddressByAddressBusRelType.tcAddressStreet2
                tDInvoice.tcShipToAddressStreet3 = tqAddressByAddressBusRelType.tcAddressStreet3
                tDInvoice.tcShipToAddressTypeCode = tqAddressByAddressBusRelType.tcAddressTypeCode
                tDInvoice.tcShipToAddressZip = tqAddressByAddressBusRelType.tcAddressZip
                tDInvoice.tcShipToBusinessRelationCode = tqAddressByAddressBusRelType.tcBusinessRelationCode
                tDInvoice.tcShipToCountryCode = tqAddressByAddressBusRelType.tcCountryCode.
        end.
end.
/* ===================== */
/* Add the Stage details */
/* ===================== */
for each tApiDInvoiceStage where
         tApiDInvoiceStage.tc_ParentRowid = tApiDInvoice.tc_Rowid
         by tApiDInvoiceStage.tc_Rowid:
    <M-47 run AddDetailLine (input  'DInvoiceStage':U (icTable), 
                     input  tDInvoice.tc_Rowid (icParentRowid), 
                     output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then do: 
            assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                   bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                   bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
                   bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
            return.
        end. /* if oiReturnStatus < 0 */
        else assign vlWarningsFound = true.
    end. /* if viFcReturnSuper <> 0 */     
    buffer-copy tApiDInvoiceStage
         except tApiDInvoiceStage.DInvoiceStage_ID
                tApiDInvoiceStage.DInvoice_ID
                tApiDInvoiceStage.tc_ParentRowid
                tApiDInvoiceStage.tc_Rowid
                tApiDInvoiceStage.tc_Status
             to tDInvoiceStage.
end. /*  for each tApiDInvoiceStage where */

/* ===================== */
/* Add the VAT details   */
/* ===================== */
for each tApiDInvoiceVat where 
         tApiDInvoiceVat.tc_ParentRowid = tApiDInvoice.tc_Rowid
         by tApiDInvoiceVat.tc_Rowid :
    /* replace unknown values */
    if tApiDInvoiceVat.DInvoiceVatVatBaseCreditTC = ? then assign tApiDInvoiceVat.DInvoiceVatVatBaseCreditTC = 0.
    if tApiDInvoiceVat.DInvoiceVatVatBaseDebitTC = ? then assign tApiDInvoiceVat.DInvoiceVatVatBaseDebitTC = 0.
    if tApiDInvoiceVat.DInvoiceVatNTBaseDebitTC = ? then assign tApiDInvoiceVat.DInvoiceVatNTBaseDebitTC = 0.
    if tApiDInvoiceVat.DInvoiceVatNTBaseCreditTC = ? then assign tApiDInvoiceVat.DInvoiceVatNTBaseCreditTC = 0.
    if tApiDInvoiceVat.DInvoiceVatVatCreditTC = ? then assign tApiDInvoiceVat.DInvoiceVatVatCreditTC = 0.
    if tApiDInvoiceVat.DInvoiceVatVatDebitTC = ? then assign tApiDInvoiceVat.DInvoiceVatVatDebitTC = 0.
    /* Create a new DInvoiceVat and copy all data */
    <M-46 run AddDetailLine (input  'DInvoiceVat':U (icTable), 
                            input  tDInvoice.tc_Rowid (icParentRowid), 
                            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then do: 
            assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                   bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                   bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
                   bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
            return.
        end. /* if oiReturnStatus < 0 */
        else assign vlWarningsFound = true.
    end. /* if viFcReturnSuper <> 0 */  
    buffer-copy tApiDInvoiceVat 
        except DInvoiceVat_ID 
               DInvoice_ID 
               tc_rowid 
               tc_ParentRowid 
               tc_status 
            to tDInvoiceVat.
end. /* for each tApiDInvoiceVat where */

/* ======================= */
/* Add the DI-Bank details */
/* ======================= */
for each tApiDInvoiceBank where
         tApiDInvoiceBank.tc_ParentRowid = tApiDInvoice.tc_Rowid
         by tApiDInvoiceBank.tc_Rowid :
    <M-53 run AddDetailLine (input  'DInvoiceBank':U (icTable), 
                         input  tDInvoice.tc_Rowid (icParentRowid), 
                         output viFcReturnSuper (oiReturnStatus)) in BDInvoice>    
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then do: 
            assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                   bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                   bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
                   bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
            return.
        end. /* if oiReturnStatus < 0 */
        else assign vlWarningsFound = true.
    end. /* if viFcReturnSuper <> 0 */
    buffer-copy tApiDInvoiceBank
         except tApiDInvoiceBank.DInvoiceBank_ID
                tApiDInvoiceBank.DInvoice_ID
                tApiDInvoiceBank.tc_ParentRowid
                tApiDInvoiceBank.tc_Rowid
                tApiDInvoiceBank.tc_Status
             to tDInvoiceBank.
    /* ======================= */
    /* Add the DI-BankPayCode details */
    /* ======================= */
    for each tApiDInvoiceBankPayCode where
             tApiDInvoiceBankPayCode.tc_ParentRowid = tApiDInvoiceBank.tc_Rowid
             by tApiDInvoiceBankPayCode.tc_Rowid :
        <M-61 run AddDetailLine
           (input  'DInvoiceBankPayCode':U (icTable), 
            input  tDInvoiceBank.tc_Rowid (icParentRowid), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>    
        if viFcReturnSuper <> 0
        then do:
            assign oiReturnStatus = viFcReturnSuper.
            if oiReturnStatus < 0
            then do: 
                assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                       bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                       bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
                       bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
                return.
            end. /* if oiReturnStatus < 0 */
            else assign vlWarningsFound = true.
        end. /* if viFcReturnSuper <> 0 */
        buffer-copy tApiDInvoiceBankPayCode
             except tApiDInvoiceBankPayCode.DInvoiceBankPayCode_ID
                    tApiDInvoiceBankPayCode.DInvoiceBank_ID
                    tApiDInvoiceBankPayCode.tc_ParentRowid
                    tApiDInvoiceBankPayCode.tc_Rowid
                    tApiDInvoiceBankPayCode.tc_Status
                 to tDInvoiceBankPayCode.
    end. /* for each tApiDInvoiceBankPayCode where */
end. /* for each tApiDInvoiceBank where */

/* ======================= */
/* Add the OpInfo details  */
/* ======================= */
for each tApiDInvoiceOpInfo where
         tApiDInvoiceOpInfo.tc_ParentRowid = tApiDInvoice.tc_Rowid
         by tApiDInvoiceOpInfo.tc_Rowid:
    <M-45 run AddDetailLine
       (input  'DInvoiceOpInfo':U (icTable), 
        input  tDInvoice.tc_Rowid (icParentRowid), 
        output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then do: 
            assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                   bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                   bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
                   bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
            return.
        end. /* if oiReturnStatus < 0 */
        else assign vlWarningsFound = true.
    end. /* if viFcReturnSuper <> 0 */     
    buffer-copy tApiDInvoiceOpInfo
         except tApiDInvoiceOpInfo.DInvoiceOpInfo_ID
                tApiDInvoiceOpInfo.DInvoice_ID
                tApiDInvoiceOpInfo.tc_ParentRowid
                tApiDInvoiceOpInfo.tc_Rowid
                tApiDInvoiceOpInfo.tc_Status
             to tDInvoiceOpInfo.
end. /*  for each tApiDInvoiceOpInfo where */

/* ============================================ */
/* Add DInvoicePosting details (ControlAccount) */
/* ============================================ */
<Q-52 run DebtorByDebtor (first) (Read) (NoCache)
   (input viCompanyId, (CompanyId)
    input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressType)
    input tDInvoice.Debtor_ID, (DebtorId)
    input if tDInvoice.Debtor_ID = 0 then tDInvoice.tcDebtorCode else '':U, (DebtorCode)
    output dataset tqDebtorByDebtor) in BDebtor >
find first tqDebtorByDebtor NO-error.
if available tqDebtorByDebtor
then do:
    assign viControlGLProfileId = if tDInvoice.DInvoiceType = {&INVOICETYPE-PREPAYMENT} 
                                  then tqDebtorByDebtor.tiPrePayControlGLProfile_ID 
                                  else if tDInvoice.DInvoiceType = {&INVOICETYPE-INVOICE} or tDInvoice.DInvoiceType = {&INVOICETYPE-FINANCECHARGE} or tDInvoice.DInvoiceType = {&INVOICETYPE-INVOICECORRECTION} 
                                       then tqDebtorByDebtor.tiInvControlGLProfile_ID 
                                       else tqDebtorByDebtor.tiCnControlGLProfile_ID.
    <Q-54 run ProfileLinkByGL (all) (Read) (NoCache)
       (input viControlGLProfileId, (GlProfileId)
        input viCompanyId, (CompanyId)
        output dataset tqProfileLinkByGL) in BProfile >
    find first tqProfileLinkByGl no-error.
    if available tqProfileLinkByGl
    then assign vcControlGLCode = tqProfileLinkByGL.tcGLCode.
    assign tDInvoice.tcControlGLCode = vcControlGLCode.
end. /* if available tqDebtorByDebtor */
find first tApiDInvoicePosting where 
           tApiDInvoicePosting.tc_ParentRowid = tApiDInvoice.tc_Rowid
           no-error.
if available tApiDInvoicePosting
then do:
    /* Create a new DInvoicePosting and copy all data */
    <M-50 run AddDetailLine (input  'DInvoicePosting':U (icTable), 
                    input  tDInvoice.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then do: 
            assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                   bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                   bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
                   bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
            return.
        end. /* if oiReturnStatus < 0 */
        else assign vlWarningsFound = true.
    end. /* if viFcReturnSuper <> 0 */
    buffer-copy tApiDInvoicePosting 
                except DInvoicePosting_ID DInvoice_ID tc_rowid tc_ParentRowid tc_status 
                to tDInvoicePosting.
    /* fill in the dinvoice voucher, search in instance */
    <M-51 run GetPostingInfoByPostingId (input  tDInvoicePosting.Posting_ID (iiPostingId), 
                                 input  vcControlGLCode (icGLCode), 
                                 output viDummy (oiPostingPeriodYear), 
                                 output vcDummy (ocPostingJournalCode), 
                                 output tDInvoice.DInvoiceVoucher (oiPostingVoucher), 
                                 output viDummy (oiPostingPeriodPeriod), 
                                 output viControlPostingLineId (oiPostingLineId), 
                                 output vdControlPostingDebitTC (odPostingLineDebitTC), 
                                 output vdControlPostingCreditTC (odPostingLineCreditTC), 
                                 output vdControlPostingDebitLC (odPostingLineDebitLC), 
                                 output vdControlPostingCreditLC (odPostingLineCreditLC), 
                                 output vdControlPostingDebitCC (odPostingLineDebitCC), 
                                 output vdControlPostingCreditCC (odPostingLineCreditCC), 
                                 output viFcReturnSuper (oiReturnStatus)) in BJournalEntry>
    if viFcReturnSuper <> 0
    then do:
        assign oiReturnStatus = viFcReturnSuper.
        if oiReturnStatus < 0
        then do: 
            assign olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
                   bcLstReturn    = bcLstReturn  + chr(4) + string(viFcReturnSuper)
                   bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
                   bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
            return.
        end. /* if oiReturnStatus < 0 */
        else assign vlWarningsFound = true.
    end. /* if viFcReturnSuper <> 0 */
    /* =============================================== */
    /* Create a new DInvoiceMovement and copy all data */
    /* =============================================== */
    assign vlControlPostingError = false.
    if viControlPostingLineId <> ? and viControlPostingLineId <> 0
    then do:
      /*  if (tDInvoice.DInvoiceType = {&INVOICETYPE-INVOICE} or tDInvoice.DInvoiceType = {&INVOICETYPE-FINANCECHARGE} or tDInvoice.DInvoiceType = {&INVOICETYPE-INVOICECORRECTION}) and vdControlPostingDebitLC = 0
        then assign vlControlPostingError = true.
        if (tDInvoice.DInvoiceType = {&INVOICETYPE-CREDITNOTE} or tDInvoice.DInvoiceType = {&INVOICETYPE-CREDITNOTECORRECTION}) and vdControlPostingCreditLC = 0
        then assign vlControlPostingError = true.*/
        <M-55 run AddDetailLine (input  'DInvoiceMovement':U (icTable), 
                    input  tDInvoice.tc_Rowid (icParentRowid), 
                    output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        if viFcReturnSuper < 0
        then assign vlControlPostingError = true.
        assign tDInvoiceMovement.DInvoiceMovementType = {&MOVEMENTTYPE-INITIAL}
               tDInvoiceMovement.PostingLine_ID       = viControlPostingLineId.
    end. /* if viControlPostingLineId <> ? and viControlPostingLineId <> 0 */
    else assign vlControlPostingError = true.
    /* posting must be on control account, and on right side */
    if vlControlPostingError = true
    then do:
        assign oiReturnStatus = -1
               vcMessage      = trim(substitute(#T-60'The posting must be on control account (&1): for &2 or &3 on &4, for &5 or &6 on &7.':255(13948)T-60# , vcControlGLCode, {&INVOICETYPE-INVOICE-TR}, {&INVOICETYPE-INVOICECORRECTION-TR}, {&CREDITDEBITABBREVIATION-DEBIT-TR}, {&INVOICETYPE-CREDITNOTE-TR}, {&INVOICETYPE-CREDITNOTECORRECTION-TR}, {&CREDITDEBITABBREVIATION-CREDIT-TR} ))
               olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
               bcLstReturn    = bcLstReturn  + chr(4) + string(-1)
               bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
               bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
        <M-40 run SetMessage (input  vcMessage (icMessage),
                 input  '':U (icArguments),
                 input  '':U (icFieldName),
                 input  '':u (icFieldValue),
                 input  'E':U (icType),
                 input  3 (iiSeverity),
                 input  '':U (icRowid),
                 input  'QadFin-7175':U (icFcMsgNumber),
                 input  '' (icFcExplanation),
                 input  '' (icFcIdentification),
                 input  '' (icFcContext),
                 output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        return.
    end. /* if vlControlPostingError = true */

    /* Validate DInvoiceOriginalAmount with ControlPosting */
    if  tDInvoice.DInvoiceOriginalCreditTC <> vdControlPostingCreditTC or 
        tDInvoice.DInvoiceOriginalCreditLC <> vdControlPostingCreditLC or
        tDInvoice.DInvoiceOriginalCreditCC <> vdControlPostingCreditCC or
        tDInvoice.DInvoiceOriginalDebitTC  <> vdControlPostingDebitTC  or 
        tDInvoice.DInvoiceOriginalDebitLC  <> vdControlPostingDebitLC  or
        tDInvoice.DInvoiceOriginalDebitCC  <> vdControlPostingDebitCC
    then do:
        assign oiReturnStatus = -1
               vcMessage      = trim(substitute(#T-81'The posting amount on control account (&1) must be same as customer invoice original amount':255(43859033)T-81# , vcControlGLCode ))           
               olSaveAsDraft  = (icAction <> {&DAEMONACTION-SAVE} and icAction <> {&DAEMONACTION-VALIDATE})
               bcLstReturn    = bcLstReturn  + chr(4) + string(-1)
               bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
               bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
        <M-953 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  '':U (icRowid), 
            input  'qadfin-330111':U (icFcMsgNumber), 
            input  '' (icFcExplanation), 
            input  '' (icFcIdentification), 
            input  '' (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
        return.
    end. /* if  tDInvoice.DInvoiceOriginalCreditTC <> vdControlPostingCreditTC or */
    
end. /* if available tApiDInvoicePosting */
/* =================================================== */
/* This is where it ends if only SaveAsDraft is needed */
/* =================================================== */
if vlSaveAsDraft
then do:
    assign oiReturnStatus = -1
           bcLstReturn    = bcLstReturn  + chr(4) + "-1":U
           bcLstPrimKey   = bcLstPrimKey + chr(4) + "*":U
           bcLstRowid     = bcLstRowid   + chr(4) + "*":U.
    Return.
end. /* if vlSaveAsDraft */

/* ============================================ */
/* Indicate that everything was saved correctly */
/* Set return status = OK                       */
/* ============================================ */
assign bcLstReturn    = bcLstReturn  + chr(4) + (if vlWarningsFound = true then "1":U else "0":U)
       bcLstPrimKey   = bcLstPrimKey + chr(4) + string(tApiDInvoice.DInvoice_ID)
       bcLstRowid     = bcLstRowid   + chr(4) + vcDInvoiceRowId
       oiReturnStatus = (if vlWarningsFound = true then 1 else 0).