project QadFinancials > class BCInvoiceAPMatching > method CreateCInvoice


Parameters


oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BCInvoiceAPMatching.CreateCInvoiceAPMatchingAllInOneTran
method BCInvoiceAPMatching.CreateCInvoiceAPMatchingOneInOneTran


program code (program6/bcinvoiceapmatching.p)

/* =================================================================================================== */
/* Method      : CreateCInvoice                                                                        */
/* Desc        : Create supplier invoice                                                               */
/*               Method has to be in the same segment as CreateCInvoiceAPMatching                      */
/* --------------------------------------------------------------------------------------------------- */
/* Params:       tCInvoiceRef         There has to be active record in this table                      */
/* =================================================================================================== */

assign oiReturnStatus = -98
       viLocalReturn  = 0.

CINVOICE_BLOCK:
do on error undo, return:

    /* =================================================================================================== */
    /* Get supportive data                                                                                 */
    /* =================================================================================================== */
    /* Company should be the current company - the one the user logged on to */
    /* This implicates the after this check we can use viCompanyID to find data as tCInvoiceRef does not hold a company-ID */
    if tCInvoiceRef.tcCompanyCode <> vcCompanyCode
    then do:
        assign vcMessage = #T-29'Current entity (&1) differs from endity (&2) where supplier invoices should be created to.':255(71133)T-29#
               vcMessage = substitute( vcMessage, vcCompanyCode, tCInvoiceRef.tcCompanyCode)
               vcContext = 'InvoiceCompCode=&1|CompanyCode=&2':U
               vcContext = substitute(vcContext, tCInvoiceRef.tcCompanyCode, vcCompanyCode)
               vcContext = replace(vcContext, "|":U, chr(2)).
        <M-28 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  tCInvoiceRef.tc_Rowid (icRowid), 
            input  'QadFin-8542':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  vcContext (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
        assign viLocalReturn = -1.
        leave CINVOICE_BLOCK.
    end. /* if tCInvoiceRef.tcCompanyCode <> vcCompanyCode */

    /* Get Accounting Period */
    if tCInvoiceRef.ttPostingDate = ?
    then assign tCInvoiceRef.ttPostingDate = tCInvoiceRef.ttCInvoiceDate.
    <Q-5 run PeriodByStartEndDate (all) (Read) (Cache)
       (input viCompanyID, (CompanyId)
        input tCInvoiceRef.ttPostingDate, (Date)
        output dataset tqPeriodByStartEndDate) in BPeriod>
    find first tqPeriodByStartEndDate where
               tqPeriodByStartEndDate.tiCompany_ID       = viCompanyID                and
               tqPeriodByStartEndDate.ttPeriodStartDate <= tCInvoiceRef.ttPostingDate and
               tqPeriodByStartEndDate.ttPeriodEndDate   >= tCInvoiceRef.ttPostingDate
               no-error.
    if not available tqPeriodByStartEndDate
    then do:
        assign vcMessage = #T-2'There is not available valid Accounting Period for Posting date &1.':255(71132)T-2#
               vcMessage = substitute(vcMessage, tCInvoiceRef.ttPostingDate).
        <M-9 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  tCInvoiceRef.tc_Rowid (icRowid), 
            input  'QadFin-8400':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
        assign viLocalReturn = -1.
        leave CINVOICE_BLOCK.
    end. /* if not available tqPeriodByStartEndDate */
    
    /* Creditor details */
    if tCInvoiceRef.tcCreditorCode <> ? and
       tCInvoiceRef.tcCreditorCode <> '':U
    then do:
        <Q-24 run CreditorByCreditor (first) (Read) (NoCache)
           (input ?, (CreditorId)
            input viCompanyID, (CompanyId)
            input tCInvoiceRef.tcCreditorCode, (CreditorCode)
            input ?, (AddressType)
            output dataset tqCreditorByCreditor) in BCreditor>
    end. /* if tCInvoiceRef.tcCreditorCode <> ? and */
    find first tqCreditorByCreditor where
               tqCreditorByCreditor.tcCreditorCode = tCInvoiceRef.tcCreditorCode
               no-error.
    if available tqCreditorByCreditor
    then assign viCreditorDivisionProfileId = tqCreditorByCreditor.tiDivisionProfile_ID.
    
    /* Payment condition */
    if (tCInvoiceRef.tcNormalPaymentConditionCode = '':U or
        tCInvoiceRef.tcNormalPaymentConditionCode = ?)  and
       available tqCreditorByCreditor
    then assign tCInvoiceRef.tcNormalPaymentConditionCode = tqCreditorByCreditor.tcNormalPaymentConditionCode.
    
    /* Due dates */
    if tCInvoiceRef.tcNormalPaymentConditionCode <> ?     and
       tCInvoiceRef.tcNormalPaymentConditionCode <> '':U  and
       (tCInvoiceRef.ttCInvoiceDueDate            = ?    or
        tCInvoiceRef.ttCInvoiceDiscountDueDate    = ?)
    then do:
        assign vhFcComponent = ?.
        <M-11 run CalculateDueDiscountDate
           (input  ? (iiPaymentConditionID), 
            input  tCInvoiceRef.tcNormalPaymentConditionCode (icPaymentConditionCode), 
            input  tCInvoiceRef.ttCInvoiceDate (itInvoiceDate), 
            output vtCInvoiceDueDate (otDueDate), 
            output vtCInvoiceDiscountDueDate (otDiscountDate), 
            output viFcReturnSuper (oiReturnStatus)) in BPaymentCondition>
        assign tCInvoiceRef.ttCInvoiceDueDate         = (if tCInvoiceRef.ttCInvoiceDueDate = ?
                                                         then vtCInvoiceDueDate
                                                         else tCInvoiceRef.ttCInvoiceDueDate)
               tCInvoiceRef.ttCInvoiceDiscountDueDate = (if tCInvoiceRef.ttCInvoiceDiscountDueDate = ?
                                                         then vtCInvoiceDiscountDueDate
                                                         else tCInvoiceRef.ttCInvoiceDiscountDueDate).
    end. /* if tCInvoiceRef.tcNormalPaymentConditionCode <> ?     and */
    
    /* Reason code */
    /* Get reason code:                                                                                    *
     *   - first try default creditor reason code                                                          *
     *   - then get first reason code which                                                                */
    assign vlIsLogisticMatching = can-find(first tAPMatchingLnRef where
                                                 tAPMatchingLnRef.tc_ParentRowid = tCInvoiceRef.tc_Rowid)
           vlIsInitialStatus    = tCInvoiceRef.tlIsInitialStatus = true or
                                  vlIsLogisticMatching
           vcReasonCode         = ?.
    
    if available tqCreditorByCreditor            and
       tqCreditorByCreditor.tcReasonCode <> '':U and
       tqCreditorByCreditor.tcReasonCode <> ?
    then do:
        <Q-12 run ReasonByIdCode (all) (Read) (NoCache)
           (input ?, (ReasonId)
            input tqCreditorByCreditor.tcReasonCode, (ReasonCode)
            output dataset tqReasonByIdCode) in BReason >
       find first tqReasonByIdCode no-error.
       if tqReasonByIdCode.tlReasonIsInitialStatus    = vlIsInitialStatus and
          tqReasonByIdCode.tlReasonIsLogisticMatching = vlIsLogisticMatching
       then assign vcReasonCode = tqReasonByIdCode.tcReasonCode.
    end. /* if vcCreditorReasonCode <> '':U and */
    if vcReasonCode = ?
    then do:
        <Q-13 run ReasonForMatching (all) (Read) (NoCache)
           (input ?, (ReasonID)
            input ?, (ReasonCode)
            input vlIsLogisticMatching, (ReasonIsLogisticMatching)
            input true, (ReasonIsActive)
            input vlIsInitialStatus, (ReasonIsInitialStatus)
            output dataset tqReasonForMatching) in BReason >
        find first tqReasonForMatching where
                   tqReasonForMatching.tlReasonIsActive           = true                 and
                   tqReasonForMatching.tlReasonIsInitialStatus    = vlIsInitialStatus    and
                   tqReasonForMatching.tlReasonIsLogisticMatching = vlIsLogisticMatching and
                   (if vlIsLogisticMatching 
                    then tqReasonForMatching.tcMatchingReasonCode > '':U
                    else true) 
                    no-error.
        if available tqReasonForMatching
        then assign vcReasonCode = tqReasonForMatching.tcReasonCode.
    end. /* if vcReasonCode = ? */
    if vcReasonCode = ? or
       vcReasonCode = '':U
    then do:
        assign vcMessage = #T-31'System cannot determine Invoice Status Code to be used for creation of Supplier Invoice. Reason code have to have these properties: Active = &1, Initial status = &2, Receiver matching = &3.':255(71102)T-31#
               vcMessage = substitute(vcMessage, "yes", vlIsInitialStatus, vlIsLogisticMatching)
               vcContext = 'CompanyCode=&1':U
               vcContext = substitute(vcContext, tCInvoiceRef.tcCompanyCode).
        <M-30 run SetMessage
           (input  vcMessage (icMessage), 
            input  '':U (icArguments), 
            input  '':U (icFieldName), 
            input  '':U (icFieldValue), 
            input  'E':U (icType), 
            input  3 (iiSeverity), 
            input  tCInvoiceRef.tc_Rowid (icRowid), 
            input  'QadFin-8620':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  vcContext (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching>
        assign viLocalReturn = -1.
        leave CINVOICE_BLOCK.
    end. /* if vcReasonCode = ? or */
    
    /* Exchange rate */
    if (tCInvoiceRef.tdExchangeRate = ? or
        tCInvoiceRef.tdExchangeRate = 0 or
        tCInvoiceRef.tdRateScale    = ? or
        tCInvoiceRef.tdRateScale    = 0) and
       tCInvoiceRef.tcCurrencyCode <> ?  and
       tCInvoiceRef.tcCurrencyCode <> "":U
    then do:
        if tCInvoiceRef.tcCurrencyCode = vcCompanyLC
        then assign tCInvoiceRef.tdExchangeRate = 1
                    tCInvoiceRef.tdRateScale    = 1.
        else do:
            assign vhFcComponent = ?.                                  
            <I-74 {bFcOpenInstance
                 &CLASS           = "Session"}>            
            <M-76 run GetLogicalValue
               (input  'APInvoiceExchangeRateDate':U (icName), 
                output vlExchangeRateDateUseInvDate (olValue), 
                output viFcReturnSuper (oiReturnStatus)) in Session>
             <I-82 {bFcCloseInstance
                  &CLASS           = "Session"}>    
            if vlExchangeRateDateUseInvDate 
            then assign vtExchangeRateValidityDate = tCInvoiceRef.ttCInvoiceDate.
            else assign vtExchangeRateValidityDate = tCInvoiceRef.ttPostingDate.            
            <M-68 run GetExchangeRate
               (input  viCompanyID (iiCompanyID), 
                input  ? (iiFromCurrencyID), 
                input  tCInvoiceRef.tcCurrencyCode (icFromCurrencyCode), 
                input  viCompanyLCId (iiToCurrencyID), 
                input  ? (icToCurrencyCode), 
                input  ? (iiExchangeRateTypeID), 
                input  {&EXCHANGERATETYPE-ACCOUNTING} (icExchangeRateTypeCode), 
                input  vtExchangeRateValidityDate (itValidityDate), 
                output tCInvoiceRef.tdExchangeRate (odExchangeRate), 
                output tCInvoiceRef.tdRateScale (odExchangeScaleFactor), 
                output viFcReturnSuper (oiReturnStatus)) in BCInvoiceAPMatching> 
            if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
            if viFcReturnSuper <  0 then leave CINVOICE_BLOCK.
        end. /* if tCInvoiceRef.tcCurrencyCode = vcCompanyLC */ 
    end. /* if (tCInvoiceRef.tdExchangeRate = ? or */
    
    /* Control G/L Account */
    assign viControlGLProfileId = ?.
    if available tqCreditorByCreditor
    then do:
        if tCInvoiceRef.tcCInvoiceType = {&INVOICETYPE-INVOICE} or
           tCInvoiceRef.tcCInvoiceType = {&INVOICETYPE-INVOICECORRECTION}
        then assign viControlGLProfileId = tqCreditorByCreditor.tiInvControlGLProfile_ID.
        else assign viControlGLProfileId = tqCreditorByCreditor.tiCnControlGLProfile_ID.
    end. /* if available tqCreditorByCreditor */
    assign viGLDivisionProfileId   = ?
           viGLCostCentreProfileId = ?
           viGLProjectProfileId    = ?.
    if viControlGLProfileId <> ? and
       viControlGLProfileId <> 0
    then do:
        <Q-25 run ProfileLinkByGL (all) (Read) (NoCache)
           (input viControlGLProfileId, (GlProfileId)
            input viCompanyId, (CompanyId)
            output dataset tqProfileLinkByGL) in BProfile >
        find first tqProfileLinkByGL where
                   tqProfileLinkByGL.tiProfile_ID = viControlGLProfileId
                   no-error.
        if available tqProfileLinkByGL
        then assign viGLDivisionProfileId   = tqProfileLinkByGL.tiDivisionProfile_ID
                    viGLCostCentreProfileId = tqProfileLinkByGL.tiCostCentreProfile_ID
                    viGLProjectProfileId    = tqProfileLinkByGL.tiProjectProfile_ID.
    end. /* if viControlGLProfileId <> ? and */

    /* Division */
    assign vcDivision = tCInvoiceRef.tcCInvoiceDivisionCode.
    /* from supplier definition */
    if (vcDivision = '':U or vcDivision = ?)          and
       viCreditorDivisionProfileId <> ? and
       viCreditorDivisionProfileId <> 0
    then do:
        <Q-19 run GetDivisionFromProfile (all) (Read) (NoCache)
           (input viCompanyId, (CompanyId)
            input viCreditorDivisionProfileId, (DivisionProfileId)
            output dataset tqDivisionFromProfile) in BProfile >
        find first tqDivisionFromProfile where
                   tqDivisionFromProfile.tiProfile_ID = viCreditorDivisionProfileId
                   no-error.
        if available tqDivisionFromProfile
        then assign vcDivision = tqDivisionFromProfile.tcDivisionCode.
    end. /* if (vcDivision = '':U or vcDivision = ?)          and */
    /* from G/L definition */
    if (vcDivision = '':U or vcDivision = ?) and
       viGLDivisionProfileId <> ?            and
       viGLDivisionProfileId <> 0
    then do:
        <Q-26 run GetDivisionFromProfile (all) (Read) (NoCache)
           (input viCompanyId, (CompanyId)
            input viGLDivisionProfileId, (DivisionProfileId)
            output dataset tqDivisionFromProfile) in BProfile >
        find first tqDivisionFromProfile where
                   tqDivisionFromProfile.tiProfile_ID = viGLDivisionProfileId
                   no-error.
        if available tqDivisionFromProfile
        then assign vcDivision = tqDivisionFromProfile.tcDivisionCode.
    end. /* if (vcDivision = '':U or vcDivision = ?) and */
    
    /* Cost centre and project */
    assign vcCostCentre = tCInvoiceRef.tcCInvoiceCostCentreCode.    
    if (vcCostCentre = '':U or
        vcCostCentre = ?)      and
       viGLCostCentreProfileId <> ? and
       viGLCostCentreProfileId <> 0
    then do:
        <Q-21 run GetCostCentreFromProfile (all) (Read) (NoCache)
           (input viCompanyId, (CompanyId)
            input viGLCostCentreProfileId, (CostCentreProfileId)
            output dataset tqCostCentreFromProfile) in BProfile >
        find first tqCostCentreFromProfile where
                   tqCostCentreFromProfile.tiProfile_ID = viGLCostCentreProfileId
                   no-error.
        if available tqCostCentreFromProfile
        then assign vcCostCentre = tqCostCentreFromProfile.tcCostCentreCode.
    end. /* if tqProfileLinkByGL.tiCostCentreProfile_ID <> 0 and */
    
    /* Project */
    assign vcProject = ?.
    if (vcProject = '':U or vcProject = ?) and
       viGLProjectProfileId <> ?           and
       viGLProjectProfileId <> 0
    then do:        
        <Q-23 run GetProjectFromProfile (all) (Read) (NoCache)
           (input viCompanyId, (CompanyId)
            input viGLProjectProfileId, (ProjectProfileID)
            output dataset tqProjectFromProfile) in BProfile >
        find first tqProjectFromProfile where
                   tqProjectFromProfile.tiProfile_ID = viGLProjectProfileId
                   no-error. 
        if available tqProjectFromProfile
        then assign vcProject = tqProjectFromProfile.tcProjectCode.
    end. /* if (vcProject = '':U or vcProject = ?) and */
    
    /* =================================================================================================== */
    /* Prepare data for Invoice Creation                                                                   */
    /* =================================================================================================== */
    empty temp-table tCInvoiceBCIAPM.
    create tCInvoiceBCIAPM.
    assign tCInvoiceBCIAPM.tcAction                     = {&DAEMONACTION-SAVE}
           tCInvoiceBCIAPM.tiCompanyId                  = viCompanyID
           tCInvoiceBCIAPM.tcInvoiceReference           = tCInvoiceRef.tcCInvoiceReference
           tCInvoiceBCIAPM.tcInvoiceType                = tCInvoiceRef.tcCInvoiceType
           tCInvoiceBCIAPM.tiPeriodYear                 = tqPeriodByStartEndDate.tiPeriodYear
           tCInvoiceBCIAPM.tiPeriodPeriod               = tqPeriodByStartEndDate.tiPeriodPeriod
           tCInvoiceBCIAPM.tcJournalCode                = tCInvoiceRef.tcCInvoiceJournalCode
           tCInvoiceBCIAPM.tiInvoiceVoucher             = ?
           tCInvoiceBCIAPM.ttInvoiceDate                = tCInvoiceRef.ttCInvoiceDate 
           tCInvoiceBCIAPM.ttInvoiceTaxPointDate        = tCInvoiceRef.ttTaxPointDate
           tCInvoiceBCIAPM.tcCreditorCode               = tCInvoiceRef.tcCreditorCode
           tCInvoiceBCIAPM.tcInvoiceDescription         = tCInvoiceRef.tcCInvoiceDescription
           tCInvoiceBCIAPM.tcDivisionCode               = vcDivision
           tCInvoiceBCIAPM.tcProjectCode                = vcProject
           tCInvoiceBCIAPM.tcCostCentreCode             = vcCostCentre
           tCInvoiceBCIAPM.ttPostingDate                = tCInvoiceRef.ttPostingDate
           tCInvoiceBCIAPM.tcPostingType                = (if tCInvoiceRef.tcCInvoiceType = {&INVOICETYPE-CREDITNOTE} 
                                                           then {&POSTINGTYPE-DEBIT}
                                                           else {&POSTINGTYPE-CREDIT})
           tCInvoiceBCIAPM.tdInvoiceAmountTC            = 0
           tCInvoiceBCIAPM.tdInvoiceAmountLC            = 0
           tCInvoiceBCIAPM.tdInvoiceHoldAmountTC        = tCInvoiceRef.tdCInvoiceHoldAmountTC
           tCInvoiceBCIAPM.tdExchangeRateTCLC           = tCInvoiceRef.tdExchangeRate
           tCInvoiceBCIAPM.tdExchangeRateScale          = tCInvoiceRef.tdRateScale
           tCInvoiceBCIAPM.tcPostingText                = tCInvoiceRef.tcCInvoiceReference
           tCInvoiceBCIAPM.tlOnlyControlAccount         = false
           tCInvoiceBCIAPM.tdInvoiceAmountTC            = tCInvoiceRef.tdCInvoiceAmountTC
           tCInvoiceBCIAPM.tcTCCurrencyCode             = tCInvoiceRef.tcCurrencyCode
           tCInvoiceBCIAPM.ttInvoiceDueDate             = tCInvoiceRef.ttCInvoiceDueDate
           tCInvoiceBCIAPM.ttInvoiceDiscountDueDate     = tCInvoiceRef.ttCInvoiceDiscountDueDate
           tCInvoiceBCIAPM.tcReasonCode                 = vcReasonCode
           tCInvoiceBCIAPM.tcNormalPaymentConditionCode = tCInvoiceRef.tcNormalPaymentConditionCode
           tCInvoiceBCIAPM.tiBankNumber_ID              = tCInvoiceRef.tiBankNumberID
           tCInvoiceBCIAPM.tlInvoiceIsERS               = tCInvoiceRef.tlCInvoiceIsERS
           tCInvoiceBCIAPM.tcInvoiceBatchNumber         = tCInvoiceRef.tcCInvoiceBatchNumber
           tCInvoiceBCIAPM.tcInvoiceTSMNumber           = tCInvoiceRef.tcCInvoiceTSMNumber.           

    /* =================================================================================================== */
    /* Create CInvoice purchase orders                                                                     */
    /* =================================================================================================== */   
    empty temp-table tCInvoicePOBCIAPM.
    for each tCInvoicePORef where
             tCInvoicePORef.tc_ParentRowid = tCInvoiceRef.tc_Rowid:
        create tCInvoicePOBCIAPM.
        buffer-copy tCInvoicePORef to tCInvoicePOBCIAPM.
        assign tCInvoicePOBCIAPM.CInvoice_ID = tCInvoiceRef.tiCInvoice_ID
               vcPoNbr                       = tCInvoicePORef.CInvoicePOPoNbr.               
    end. /* for each tCInvoicePORef where */
    if vcPoNbr <> "":U and 
       vcPoNbr <> ? 
    then do :
        <Q-47 run PurchaseOrderByDomainNbrVend (all) (Read) (NoCache)
           (input vcDomainCode, (DomainCode)
            input vcPoNbr, (PONbr)
            input ?, (POVendor)
            input ?, (POCurrency)
            input ?, (POType)
            output dataset tqPurchaseOrderByDomainNbrVend) in BMfgPO>        
        find tqPurchaseOrderByDomainNbrVend
             no-error.
        if available tqPurchaseOrderByDomainNbrVend             and 
           tqPurchaseOrderByDomainNbrVend.tcpo_cr_terms <> "":U and 
           tqPurchaseOrderByDomainNbrVend.tcpo_cr_terms <> ?
        then assign tCInvoiceBCIAPM.tcNormalPaymentConditionCode  = tqPurchaseOrderByDomainNbrVend.tcpo_cr_terms.   
    end. /* if vcPoNbr <> "":U and  */
         
end. /* of CINVOICE_BLOCK: */

/* =================================================================================================== */
/* Return                                                                                              */
/* =================================================================================================== */
assign oiReturnStatus = viLocalReturn.