project QadFinancials > class BEmployee > method ApiBusinessRelationInfo

Description

Returns all the Business Relation information needed on Employee (Head Office Address, Vat Numbers)


Parameters


iiEmployeeIdinputintegervalue of Employee.Employee_ID field
icBusinessRelationCodeinputcharactervalue of Employee.tcBusinessRelationCode field
tAddressRefForEmployeeoutputtemp-tableHead Office Address of the Employee
tVatNumberRefForEmployeeoutputtemp-tableVat Numbers of the Employee
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


QadFinancials
method BEmployee.Calculate


program code (program3/bemployee.p)

/*Employee must always be > 0 and must be available*/
if iiEmployeeId <= 0 or
   iiEmployeeId  = ?
then return.
/*first bEmployee - get the current BR*/
find first bEmployee where
           bEmployee.Employee_ID = iiEmployeeId no-error.
if not available bEmployee
then return.
/*BusinessRelationCode to ID*/
if icBusinessRelationCode <> "":U and
   icBusinessRelationCode <> "?":U and
   icBusinessRelationCode <> ?
then do:
    <Q-1 run BusinessRelationByIdCode (all) (Read) (NoCache)
          (input ?, (BusinessRelationId)
           input icBusinessRelationCode, (BusinessRelationCode)
           output dataset tqBusinessRelationByIdCode) in BBusinessRelation >
    find first tqBusinessRelationByIdCode no-error.
    if available tqBusinessRelationByIdCode
    then assign viNewBusinessRelationId               = tqBusinessRelationByIdCode.tiBusinessRelation_ID
                bEmployee.tlBusinessRelationIsInterco = tqBusinessRelationByIdCode.tlBusinessRelationIsInterco.
    else assign viNewBusinessRelationId               = 0
                bEmployee.tlBusinessRelationIsInterco = no.
end.
else assign viNewBusinessRelationId               = 0
            bEmployee.tlBusinessRelationIsInterco = no.

/*remember the old BusinessRelation_ID and assign the new one*/
assign viOldBusinessRelationId       = bEmployee.BusinessRelation_ID
       bEmployee.BusinessRelation_ID = viNewBusinessRelationId.       

/*Addresses and VatNumbers for the OLD BusinessRelation*/
/*if no other bEmployee can be found for this BR (different than EmployeeId) - 
delete all the addresses and VatNumbers for this BR
otherwise do nothing*/
if not can-find (first bEmployee where
                       bEmployee.BusinessRelation_ID = viOldBusinessRelationId and 
                       bEmployee.Employee_ID <> iiEmployeeId)
then do:
    for each tAddressRefForEmployee where
             tAddressRefForEmployee.tiBusinessRelation_ID = viOldBusinessRelationId:
        delete tAddressRefForEmployee.
    end.
    for each tVatNumberRefForEmployee where 
             tVatNumberRefForEmployee.BusinessRelation_ID = viOldBusinessRelationId:
        delete tVatNumberRefForEmployee.
    end.
end.

/*Addresses and VatNumbers for the NEW BusinessRelation*/
/*Calculates AddressRefForEmployee's data for a BusinessRelation_ID and a certain Employee_ID*/ 
if viNewBusinessRelationId > 0
then do:
    /*AddressByAddressType*/
    assign vcAddressTypeCode = {&ADDRESSTYPECODESYSTEM-HEADOFFICE}.
    <Q-2 run AddressByAddressType (all) (Read) (NoCache)
          (input viNewBusinessRelationId, (BusinessRelationId)
           input vcAddressTypeCode, (AddressTypeCode)
           output dataset tqAddressByAddressType) in BBusinessRelation >
    for each tqAddressByAddressType:
        /*first check if there is an address for this BR and address type*/
        find first tAddressRefForEmployee where
                   tAddressRefForEmployee.tiBusinessRelation_ID = tqAddressByAddressType.tiBusinessRelation_ID and
                   tAddressRefForEmployee.tcAddressTypeCode     = tqAddressByAddressType.tcAddressTypeCode no-error.
        if not available tAddressRefForEmployee
        then do:
            /*there can be an initial tAddressRefForEmployee which is empty (BusinessRelation_ID = 0, see InitialValues)*/
            find first tAddressRefForEmployee where
                       tAddressRefForEmployee.tiBusinessRelation_ID = 0 no-error.
            if not available tAddressRefForEmployee
            then create tAddressRefForEmployee.
        end.
        
        buffer-copy tqAddressByAddressType to tAddressRefForEmployee.
        /*tc_Rowid is necessary otherwise 'already exists' error*/
        assign tAddressRefForEmployee.tc_Rowid = string(rowid(tAddressRefForEmployee)).              
    end.
end.
/*There must be at least one AddressRefForEmployee record, that is Head Office address.*/
if viNewBusinessRelationId <= 0 or
   viNewBusinessRelationId  = ? or       
   not can-find (first tAddressRefForEmployee where 
                       tAddressRefForEmployee.tiBusinessRelation_ID = viNewBusinessRelationId)
then do:
    create tAddressRefForEmployee.
    assign tAddressRefForEmployee.tiBusinessRelation_ID = 0
           /*tc_Rowid is necessary otherwise 'already exists' error*/
           tAddressRefForEmployee.tc_Rowid            = string(rowid(tAddressRefForEmployee)).
end.

/*VATNumber records*/
if viNewBusinessRelationId > 0
then do:
    <Q-3 run VatNumberByIdBusRelCntryVat (all) (Read) (NoCache)
          (input viNewBusinessRelationId, (BusinessRelationId)
           input ?, (IdentityCountryId)
           input ?, (IdentityCountryCode)
           input ?, (VatNumberIdentity)
           input ?, (VatNumberId)
           input ?, (VatNumberIsActive)
           output dataset tqVatNumberByIdBusRelCntryVat) in BBusinessRelation >
    for each tqVatNumberByIdBusRelCntryVat:
        /*find first existing or create*/
        find first tVatNumberRefForEmployee where
                   tVatNumberRefForEmployee.VatNumber_ID = tqVatNumberByIdBusRelCntryVat.tiVatNumber_ID no-error.
        if not available tVatNumberRefForEmployee
        then create tVatNumberRefForEmployee.
        /*TODO use BufferCopy method instead*/
        assign tVatNumberRefForEmployee.BusinessRelation_ID        = tqVatNumberByIdBusRelCntryVat.tiBusinessRelation_ID
               tVatNumberRefForEmployee.DeclarationCountry_ID      = tqVatNumberByIdBusRelCntryVat.tiDeclarationCountry_ID
               tVatNumberRefForEmployee.IdentityCountry_ID         = tqVatNumberByIdBusRelCntryVat.tiIdentityCountry_ID
               tVatNumberRefForEmployee.tcDeclarationCountryCode   = tqVatNumberByIdBusRelCntryVat.tcIdentityCountryCode
               tVatNumberRefForEmployee.tcIdentityCountryCode      = tqVatNumberByIdBusRelCntryVat.tcDeclarationCountryCode
               tVatNumberRefForEmployee.VatNumberDeclaration       = tqVatNumberByIdBusRelCntryVat.tcVatNumberDeclaration
               tVatNumberRefForEmployee.VatNumberIdentity          = tqVatNumberByIdBusRelCntryVat.tcVatNumberIdentity
               tVatNumberRefForEmployee.VatNumberIsActive          = tqVatNumberByIdBusRelCntryVat.tlVatNumberIsActive
               tVatNumberRefForEmployee.VatNumber_ID               = tqVatNumberByIdBusRelCntryVat.tiVatNumber_ID
               /*tc_Rowid is necessary otherwise 'already exists' error*/
               tVatNumberRefForEmployee.tc_Rowid                   = string(rowid(tVatNumberRefForEmployee)).
    end.
end.


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 = "BEmployee".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "ApiBusinessRelationInfo".
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/bemployee.apibusinessrelationinfo.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::iiEmployeeId = <parameter value>
       vhParameter::icBusinessRelationCode = <parameter value>.

/* 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.