project QadFinancials > class BDebtor > method ApiUpdateDebtorPaymentSales

Description

Method that allows callers to update some field in table debtor: DebtorTotalDaysLate, DebtorTotalNbrOfInvoices, DebtorLastPayment and DebtorLastSale.
This method acts like a real api-method and thus it clear the instance in the beginning and perform the save-actions at the end.

Temp-table tUpdateDebtorPaymentSales that is used as input for method ApiUpdateDebtorPaymentSales: The fields (except DebtorID) that have the unknown-value in this temp-table will not be taken into account to update the Debtor-data.


Parameters


tUpdateDebtorPaymentSalesinputtemp-tabletUpdateDebtorPaymentSales: temp-table that is used as input for method ApiUpdateDebtorPaymentSales. The fields (except DebtorID) that have the unknown-value in this temp-table will not be taken into account to update the Debtor-data.
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


unused


program code (program9/bdebtor.p)

/* =============================================================================================== */ 
/* Note: this method is not run from within CB anymore - it was replaced by WriteDirect Statements */ 
/* You can have a look in BDInvoice.AdditionalUpdatesAllDebtorUpdates and BDInvoice.PostSave       */ 
/* The method is still available to be used as api                                                 */ 
/* =============================================================================================== */ 


/* ================================================================================= */
/* This method has the capapbility to update following fields in some detor-records: */
/* - Debtor.DebtorTotalDaysLate                                                      */
/* - Debtor.DebtorTotalNbrOfInvoices                                                 */
/* - Debtor.DebtorLastPayment                                                        */
/* - Debtor.DebtorLastSale                                                           */
/* - Debtor.DebtorSalesOrderBalance                                                  */    
/* ================================================================================= */

/* ================================ */
/* Return in case there is no input */
/* ================================ */
if not can-find (first tUpdateDebtorPaymentSales) 
then Return.
                      
/* =========================== */
/* Exception handling          */
/* Start with a clean instance */
/* =========================== */
assign oiReturnStatus = -98.
<M-2 run ClearData
   (output viFcReturnSuper (oiReturnStatus)) in BDebtor>
if viFcReturnSuper <> 0 
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then Return.

/* ======================================================================= */
/* Go through the input-table and build up a list of the ID's to be loaded */
/* ======================================================================= */
for each tUpdateDebtorPaymentSales :

    /* =========================================================================== */
    /* Error in case the DebtorID is not filled or in case all fields are unknown  */
    /* =========================================================================== */
    if tUpdateDebtorPaymentSales.tiDebtorID = 0 or 
       tUpdateDebtorPaymentSales.tiDebtorID = ?
    then do :
        assign oiReturnStatus = -3
               vcMessage      = trim(substitute(#T-3'Field &1 in the input table of method &2 is mandatory.':213(59595)t-3#,"DebtorID":U,"BDebtor.ApiUpdateDebtorPaymentSales":U)).
        <M-4 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-6064':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDebtor>
        Return.
    end. /* if tUpdateDebtorPaymentSales.tiDebtorID = 0 or */

    if tUpdateDebtorPaymentSales.ttDebtorLastPayment    = ? and 
       tUpdateDebtorPaymentSales.ttDebtorLastSale       = ? and 
       tUpdateDebtorPaymentSales.tiDebtorDaysLate       = ? and 
       tUpdateDebtorPaymentSales.tiDebtorNbrOfInvoices  = ?
    then do :
        assign oiReturnStatus = -3
               vcMessage      = trim(substitute(#T-10'Not all fields in the input table of method &2 can be unknown.':225(59597)t-10#,"BDebtor.ApiUpdateDebtorPaymentSales":U)).
        <M-9 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-6067':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDebtor>
        Return.
    end. /* if tUpdateDebtorPaymentSales.ttDebtorLastPayment    = ? and */

    /* ======================== */
    /* Build up the list of IDs */                           
    /* ======================== */
    assign vcListDebtorIDs = vcListDebtorIDs + chr(4) + string(tUpdateDebtorPaymentSales.tiDebtorID).

end. /* for each tUpdateDebtorPaymentSales */
    
/* ============================ */
/* Load all Debtors of the List */
/* ============================ */
assign vcListDebtorIDs = substring(vcListDebtorIDs,2,-1,"character":U).
<M-1 run DataLoad
   (input  ? (icRowids), 
    input  vcListDebtorIDs (icPkeys), 
    input  ? (icObjectIds), 
    input  ? (icFreeform), 
    input  false (ilKeepPrevious), 
    output viFcReturnSuper (oiReturnStatus)) in BDebtor>
if viFcReturnSuper <> 0 
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then Return.

/* ======================================================================= */
/* Go through the input-table and update the content of the instance-data  */
/* ======================================================================= */
for each tUpdateDebtorPaymentSales :

    /* =============================== */
    /* Find the record in the instance */
    /* =============================== */
    find tDebtor where 
         tDebtor.Debtor_ID  = tUpdateDebtorPaymentSales.tiDebtorID and 
         tDebtor.tc_Status <> "D":U
         no-lock no-error.
    if not available tDebtor
    then do :
        assign oiReturnStatus = -3
               vcMessage      = trim(substitute(#T-6'Internal error: cannot find the debtor record in the instance based on its ID &1 although a successful DataLoad was performed.':255(59596)t-6#,string(tUpdateDebtorPaymentSales.tiDebtorID))).
        <M-5 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-6065':U (icFcMsgNumber), 
            input  '':U (icFcExplanation), 
            input  '':U (icFcIdentification), 
            input  '':U (icFcContext), 
            output viFcReturnSuper (oiReturnStatus)) in BDebtor>
        Return.
    end. /* if not available tDebtor */

    /* =========================================================================== */
    /* Update the field in debtor with the new input                               */
    /* Only input-fields that differ from the unknown-value are taken into account */
    /* =========================================================================== */
    if tUpdateDebtorPaymentSales.tiDebtorDaysLate <> ?
    then do :
        assign tDebtor.DebtorTotalDaysLate = (if tDebtor.DebtorTotalDaysLate = ? 
                                              then 0 
                                              else tDebtor.DebtorTotalDaysLate) + 
                                             tUpdateDebtorPaymentSales.tiDebtorDaysLate.
        if tDebtor.tc_Status = "":U
        then assign tDebtor.tc_Status= "C":U.
    end. /* if */
    if tUpdateDebtorPaymentSales.tiDebtorNbrOfInvoices <> ?
    then do :
        assign tDebtor.DebtorTotalNbrOfInvoices = (if tDebtor.DebtorTotalNbrOfInvoices = ?
                                                   then 0 
                                                   else tDebtor.DebtorTotalNbrOfInvoices) + 
                                                  tUpdateDebtorPaymentSales.tiDebtorNbrOfInvoices.
        if tDebtor.tc_Status = "":U
        then assign tDebtor.tc_Status= "C":U.
    end. /* if */
    if tUpdateDebtorPaymentSales.ttDebtorLastPayment <> ? and 
       (tUpdateDebtorPaymentSales.ttDebtorLastPayment > tDebtor.DebtorLastPayment or 
        tDebtor.DebtorLastPayment                     = ?)
    then do :
        assign tDebtor.DebtorLastPayment = tUpdateDebtorPaymentSales.ttDebtorLastPayment.
        if tDebtor.tc_Status = "":U
        then assign tDebtor.tc_Status= "C":U.
    end. /* if */
    if tUpdateDebtorPaymentSales.ttDebtorLastSale <> ? and 
       (tUpdateDebtorPaymentSales.ttDebtorLastSale > tDebtor.DebtorLastSale or 
        tDebtor.DebtorLastSale                     = ?)
    then do :
        assign tDebtor.DebtorLastSale = tUpdateDebtorPaymentSales.ttDebtorLastSale.
        if tDebtor.tc_Status = "":U
        then assign tDebtor.tc_Status= "C":U.
    end. /* if */
    if tUpdateDebtorPaymentSales.tdInvoicetotal <> ? and
       tUpdateDebtorPaymentSales.tdInvoicetotal <> 0
    then do:
         /* 
         * as we are decreasing the invoice we need to multiple the invoice total by -1 
         * note this will work for credit notes also as it will be neg * -1 = pos
         */
        <M-15 run CalcOpenOrderTotal
           (input  tUpdateDebtorPaymentSales.tdInvoicetotal * -1 (idNewSOAmount), 
            input  0 (idOldSOAmount), 
            input  tUpdateDebtorPaymentSales.tcInvoiceCurrency (icSOCurrencyCode), 
            input  tDebtor.tcCurrencyCode (icDebtorCurrencyCode), 
            input-output tDebtor.DebtorSalesOrderBalance (bdDebtorTotalSalesOrderBalance), 
            output viFcReturnSuper (oiReturnStatus)) in BDebtor>
                       
        if viFcReturnSuper <> 0 then assign oiReturnStatus = viFcReturnSuper.
        if viFcReturnSuper <  0 then return.
        
        if tDebtor.tc_Status = "":U
        then assign tDebtor.tc_Status= "C":U.            
    end.
end. /* for each tUpdateDebtorPaymentSales */

/* ========================= */
/* Validate, AddUpd and Save */
/* ========================= */
<M-11 run ValidateBC
   (output viFcReturnSuper (oiReturnStatus)) in BDebtor>
if viFcReturnSuper <> 0 
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then Return.
<M-12 run AdditionalUpdates
   (output viFcReturnSuper (oiReturnStatus)) in BDebtor>
if viFcReturnSuper <> 0 
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then Return.
<M-13 run DataSave
   (output viFcReturnSuper (oiReturnStatus)) in BDebtor>
if viFcReturnSuper <> 0 
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then Return.

/* ================== */
/* Exception handling */
/* ================== */
if oiReturnStatus = -98
then assign oiReturnStatus = 0.


Sample code: how to call this method through RPCRequestService (QXtend Inbound)

define temp-table ttContext no-undo
    field propertyQualifier as character
    field propertyName as character
    field propertyValue as character
    index entityContext is primary unique
        propertyQualifier
        propertyName
    index propertyQualifier
        propertyQualifier.

define dataset dsContext for ttContext.

define variable vhContextDS as handle no-undo.
define variable vhExceptionDS as handle no-undo.
define variable vhServer as handle no-undo.
define variable vhInputDS as handle no-undo.
define variable vhInputOutputDS as handle no-undo.
define variable vhOutputDS as handle no-undo.
define variable vhParameter as handle no-undo.

/* Create context */
create ttContext.
assign ttContext.propertyName = "programName"
       ttContext.propertyValue = "BDebtor".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "ApiUpdateDebtorPaymentSales".
create ttContext.
assign ttContext.propertyName = "applicationId"
       ttContext.propertyValue = "fin".
create ttContext.
assign ttContext.propertyName = "entity"
       ttContext.propertyValue = "1000".
create ttContext.
assign ttContext.propertyName = "userName"
       ttContext.propertyValue = "mfg".
create ttContext.
assign ttContext.propertyName = "password"
       ttContext.propertyValue = "".

/* Create input dataset */
create dataset vhInputDS.
vhInputDS:read-xmlschema("file", "xml/bdebtor.apiupdatedebtorpaymentsales.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tUpdateDebtorPaymentSales").
vhParameter:buffer-create().
assign vhParameter::<field-name-1> = <field-value-1>
       vhParameter::<field-name-2> = <field-value-2>
       ...

/* Connect the AppServer */
create server vhServer.
vhServer:connect("-URL <appserver-url>").

if not vhServer:connected()
then do:
    message "Could not connect AppServer" view-as alert-box error title "Error".
    return.
end.

/* Run */
assign vhContextDS = dataset dsContext:handle.

run program/rpcrequestservice.p on vhServer
    (input-output dataset-handle vhContextDS by-reference,
           output dataset-handle vhExceptionDS,
     input        dataset-handle vhInputDS by-reference,
     input-output dataset-handle vhInputOutputDS by-reference,
           output dataset-handle vhOutputDS).

/* Handle output however you want, in this example, we dump it to xml */
if valid-handle(vhExceptionDS)
then vhExceptionDS:write-xml("file", "Exceptions.xml", true).

if valid-handle(vhOutputDS)
then vhOutputDS:write-xml("file", "Output.xml", true).

/* Cleanup */
vhServer:disconnect().
assign vhServer = ?.

if valid-handle(vhInputDS)
then delete object vhInputDS.

if valid-handle(vhOutputDS)
then delete object vhOutputDS.

if valid-handle(vhExceptionDS)
then delete object vhExceptionDS.