| opPrimeKey | output | longchar | Primary Key field value of loaded record(s). Keys are chr(4) separated. Key fields are chr(2) separated. |
| oiReturnStatus | output | integer | Return status of the method. |
MAIN_BLOCK:
do on error undo, leave:
assign viLocalReturnStatus = 0
oiReturnStatus = -98.
/* ==================================================================== *
* To optimalize performance, keep the instances as open as possible *
* In case of API execution where the DataSave is done in the same run *
* instances can be kept open even after Additional Updates *
* ==================================================================== */
assign vlIsKeepInstancesOpen = true.
/* ====================================== */
/* Fill SharedSetID for excel integration */
/* ====================================== */
assign viSharedSetID = 0.
if vcActivityCode <> "Create":U
then do:
assign vcSharedSetType = {&SHAREDSETTYPECODE-DEBTOR}.
<Q-16 run LookupSharedSetForCompany (all) (Read) (NoCache)
(input viCompanyId, (CompanyId)
input vcSharedSetType, (SharedSetType)
output dataset tqSharedSetForCompany) in BCompany >
find first tqSharedSetForCompany no-error.
if available tqSharedSetForCompany
then assign viSharedSetID = tqSharedSetForCompany.tiSharedSet_ID.
end. /* if vcActivityCode <> "Create":U */
/* Create addresses */
empty temp-table tApiShiptoBusinessRelation.
empty temp-table tApiShiptoAddress.
empty temp-table tApiShiptoContact.
assign
viBusinessrelationCount = 0
viAaddressCount = 0 .
for each t_sDebtorShipTo
break by t_sDebtorShipTo.tcBusinessRelationCode:
if first-of (t_sDebtorShipTo.tcBusinessRelationCode)
then do:
create tApiShiptoBusinessRelation.
assign
viBusinessrelationCount = viBusinessrelationCount + 1
tApiShiptoBusinessRelation.businessrelationcode = t_sDebtorShipTo.tcBusinessRelationCode
tApiShiptoBusinessRelation.businessrelation_id = t_sDebtorShipTo.tiBusinessRelation_ID
tApiShiptoBusinessRelation.tc_rowid = string(viBusinessrelationCount).
end.
if t_sDebtorShipto.DebtorShiptoIsDebtor
then do:
/* address has to be headoffice of the customer */
vcAddressType = {&ADDRESSTYPECODESYSTEM-HEADOFFICE}.
<Q-85 run DebtorForShipto (all) (Read) (NoCache)
(input viCompanyID, (CompanyId)
input t_sDebtorShipTo.DebtorShipToCode, (DebtorCode)
input vcAddressType, (AddressTypeCode)
output dataset tqDebtorForShipto) in BDebtor>
for first tqDebtorForShipto
where tqDebtorForShipto.tcDebtorCode = t_sDebtorShipto.DebtorShiptoCode
and tqDebtorForShipto.tcAddressTypeCode = vcAddressType:
assign
t_sDebtorShipto.address_id = tqDebtorForShipto.tiAddress_ID
t_sDebtorShipTo.tiBusinessRelation_ID = tqDebtorForShipto.tiBusinessRelation_ID.
end.
end. /* end of DebtorShiptoIsDebtor */
else if t_sDebtorShipto.tcDebtorEndUserCode <> ""
then do:
/* address has to be headoffice of the customer */
<Q-63 run DebtorEndUserForShipto (all) (Read) (NoCache)
(input viCompanyID, (CompanyId)
input t_sDebtorShipTo.tcDebtorEndUserCode, (DebtorEndUserCode)
output dataset tqDebtorEndUserForShipto) in BDebtorEndUser>
for first tqDebtorEndUserForShipto
where tqDebtorEndUserForShipto.tcDebtorEndUserCode = t_sDebtorShipto.tcDebtorEndUserCode:
assign
t_sDebtorShipto.address_id = tqDebtorEndUserForShipto.tiAddress_ID
t_sDebtorShipTo.tiBusinessRelation_ID = tqDebtorEndUserForShipto.tiBusinessRelation_ID.
end.
end. /* end of tcDebtorEndUserCode <> "" */
else do:
for first tApiShiptoAddress
where tApiShiptoAddress.AddressStreet1 = t_sDebtorShipto.tcAddressStreet1
and tApiShiptoAddress.AddressStreet2 = t_sDebtorShipto.tcAddressStreet2
and tApiShiptoAddress.AddressStreet3 = t_sDebtorShipto.tcAddressStreet3
and tApiShiptoAddress.AddressZip = t_sDebtorShipto.tcAddressZip
and tApiShiptoAddress.AddressCity = t_sDebtorShipto.tcAddressCity
and tApiShiptoAddress.tcAddressTypeCode = t_sDebtorShipTo.tcAddressTypeCode
and tApiShiptoAddress.tc_parentrowid = tApiShiptoBusinessRelation.tc_rowid:
end.
if not available (tApiShiptoAddress)
then do:
/* get the Address_ID of this ship-to if exits */
<Q-47 run DebtorShipToByIDCode (all) (Read) (NoCache)
(input ?, (DebtorShiptoId)
input t_sDebtorShipTo.DebtorShipToCode, (DebtorShiptoCode)
input viCompanyId, (CompanyId)
output dataset tqDebtorShipToByIDCode) in BDebtorShipTo>
find first tqDebtorShipToByIDCode where
tqDebtorShipToByIDCode.tcDebtorShipToCode = t_sDebtorShipTo.DebtorShipToCode
no-error.
if available tqDebtorShipToByIDCode
then do :
<Q-86 assign vlFcQueryRecordsAvailable = DebtorShiptoByAddress (NoCache)
(input viCompanyId, (CompanyId)
input tqDebtorShipToByIDCode.tiAddress_ID, (AddressID)
input tqDebtorShipToByIDCode.tiDebtorShipTo_ID, (DebtorShiptoIDDifferentFrom)) in BDebtorShipTo>
end.
create tApiShiptoAddress.
assign
viAaddressCount = viAaddressCount + 1
tApiShiptoAddress.Address_ID = if (vlFcQueryRecordsAvailable = false and available tqDebtorShipToByIDCode)
then tqDebtorShipToByIDCode.tiAddress_ID
else ?
tApiShiptoAddress.AddressName = t_sDebtorShipto.tcAddressName
tApiShiptoAddress.AddressStreet1 = t_sDebtorShipto.tcAddressStreet1
tApiShiptoAddress.AddressStreet2 = t_sDebtorShipto.tcAddressStreet2
tApiShiptoAddress.AddressStreet3 = t_sDebtorShipto.tcAddressStreet3
tApiShiptoAddress.AddressTelephone = t_sDebtorShipto.tcAddressTelephone
tApiShiptoAddress.AddressCity = t_sDebtorShipto.tcAddressCity
tApiShiptoAddress.AddressZip = t_sDebtorShipto.tcAddressZip
tApiShiptoAddress.AddressEMail = t_sDebtorShipto.tcAddressEMail
tApiShiptoAddress.AddressFax = t_sDebtorShipto.tcAddressFax
tApiShiptoAddress.AddressIsTemporary = t_sDebtorShipto.tlAddressIsTemporary
tApiShiptoAddress.tcAddressTypeCode = t_sDebtorShipto.tcAddressTypeCode
tApiShiptoAddress.AddressWebSite = t_sDebtorShipto.tcAddressWebSite
tApiShiptoAddress.tcCountryCode = t_sDebtorShipTo.tcCountryCode
tApiShiptoAddress.TxzTaxZone = t_sDebtorShipTo.tcTxzTaxZone
tApiShipToAddress.tcCountyCode = t_sDebtorShipto.tcCountyCode
tApiShiptoAddress.tcStateCode = t_sDebtorShipto.tcStateCode
tApiShiptoAddress.AddressFormat = t_sDebtorShipto.tiAddressFormat
tApiShiptoAddress.AddressIsTaxable = t_sDebtorShipto.tlAddressIsTaxable
tApiShiptoAddress.AddressIsTaxInCity = t_sDebtorShipTo.tlAddressIsTaxInCity
tApiShiptoAddress.AddressSearchName = t_sDebtorShipTo.tcAddressSearchName
tApiShiptoAddress.AddressTaxIDState = t_sDebtorShipTo.tcAddressTaxIDState
tApiShiptoAddress.tc_ParentRowid = tApiShiptoBusinessRelation.tc_rowid
tApiShiptoAddress.BusinessRelation_ID = tApiShiptoBusinessRelation.businessrelation_id
tApiShiptoAddress.tcLngCode = t_sDebtorShipTo.tcLngCode
tApiShiptoAddress.AddressTaxIdMisc1 = t_sDebtorShipTo.tcAddressTaxIdMisc1
tApiShiptoAddress.AddressTaxIdMisc2 = t_sDebtorShipTo.tcAddressTaxIdMisc2
tApiShiptoAddress.AddressTaxIdMisc3 = t_sDebtorShipTo.tcAddressTaxIdMisc3
tApiShiptoAddress.TxclTaxCls = t_sDebtorShipTo.tcTxclTaxCls
tApiShiptoAddress.TxuTaxUsage = t_sDebtorShipTo.tcTxuTaxUsage
tApiShiptoAddress.AddressTaxIDFederal = t_sDebtorShipTo.tcAddresstaxIDFederal
tApiShiptoAddress.AddressTaxDeclaration = t_sDebtorShipTo.tiAddressTaxDeclaration
tApiShiptoAddress.AddressIsTaxIncluded = t_sDebtorShipTo.tlAddressIsTaxIncluded
tApiShiptoAddress.tc_rowid = string(viAaddressCount).
/* get the Address_ID of this ship-to if exits */
if t_sDebtorShipTo.DebtorShipToCode <> '' and t_sDebtorShipTo.DebtorShipToCode <> ?
then do :
<Q-40 run DebtorShipToByIDCode (all) (Read) (NoCache)
(input ?, (DebtorShiptoId)
input t_sDebtorShipTo.DebtorShipToCode, (DebtorShiptoCode)
input viCompanyId, (CompanyId)
output dataset tqDebtorShipToByIDCode) in BDebtorShipTo>
find first tqDebtorShipToByIDCode where
tqDebtorShipToByIDCode.tcDebtorShipToCode = t_sDebtorShipTo.DebtorShipToCode
no-error.
if available tqDebtorShipToByIDCode
then assign tApiShiptoAddress.Address_ID = tqDebtorShipToByIDCode.tiAddress_ID.
end. /* if t_sDebtorShipTo.DebtorShipToCode <> '' and t_sDebtorShipTo.DebtorShipToCode <> ? */
/* get country description */
if t_sDebtorShipTo.tcCountryCode <> '' and t_sDebtorShipTo.tcCountryCode <> ?
then do :
<Q-72 run CountryPrim (all) (Read) (Cache)
(input '':U, (CountryId)
input t_sDebtorShipTo.tcCountryCode, (CountryCode)
output dataset tqCountryPrim) in BCountry>
find first tqCountryPrim where
tqCountryPrim.tcCountryCode = t_sDebtorShipTo.tcCountryCode
no-error.
if available tqCountryPrim
then assign tApiShiptoAddress.tcCountryDescription = tqCountryPrim.tcCountryDescription.
end. /* t_sDebtorShipTo.tcCountryCode <> '' and t_sDebtorShipTo.tcCountryCode <> ? */
end. /* address not available */
/* Get the contacts for this address */
for each t_sDebtorShiptoContact where
t_sDebtorShiptoContact.tc_ParentRowid = t_sDebtorShipTo.tc_rowid:
for first tApiShiptoContact
where tApiShiptoContact.contactname = t_sDebtorShiptoContact.ContactName
and tApiShiptoContact.tc_ParentRowid = tApiShiptoAddress.tc_rowid:
end.
if not available (tApiShiptoContact)
then do:
create tApiShiptoContact.
buffer-copy t_sDebtorShiptoContact to tApiShiptoContact.
assign tApiShiptoContact.tc_ParentRowid = tApiShiptoAddress.tc_rowid.
end.
end. /* for each contact */
end. /* end of create address section */
end. /* end of not DebtorisShipto */
if can-find(first tApiShiptoAddress)
then do:
/* disable address replication - the address will be replicated as part of the shipto replication */
if not valid-handle(vhSessionInst)
then do:
<I-25 {bFcOpenInstance
&CLASS = "Session"}>
end.
<M-27 run SetLogicalValue
(input 'ReplicationEnabled' (icName),
input False (ilValue),
output viFcReturnSuper (oiReturnStatus)) in Session>
if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
/* Create/update the address in the Businress Relation */
if viBBusinessRelation1ID = 0 or
viBBusinessRelation1ID = ?
then do:
<I-75 {bFcStartAndOpenInstance
&ADD-TO-TRANSACTION = "true"
&CLASS = "BBusinessRelation"}>
assign vlIsBBusinessRelationStartedHr = true
vlIsBBusinessRelationOpenedHr = true.
end.
else if not valid-handle(vhBBusinessRelation1Inst)
then do:
<I-88 {bFcOpenInstance
&CLASS = "BBusinessRelation"}>
assign vlIsBBusinessRelationOpenedHr = true.
end. /* end if viBBusinessRelation1ID */
<M-22 run MaintainAddressMulti
(input-output tApiShiptoBusinessRelation (tApiBusinessRelation),
input-output tApiShiptoAddress (tApiAddress),
input-output tApiShiptoContact (tApiContact),
output viFcReturnSuper (oiReturnStatus)) in BBusinessRelation>
if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
/* re-enable address replication - the address will be replicated as part of the shipto replication */
if not valid-handle(vhSessionInst)
then do:
<I-28 {bFcOpenInstance
&CLASS = "Session"}>
end.
<M-29 run SetLogicalValue
(input 'ReplicationEnabled' (icName),
input ? (ilValue),
output viFcReturnSuper (oiReturnStatus)) in Session>
if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
end.
PROCESSBLOCK:
for each t_sDebtorShipTo:
assign t_sDebtorShipTo.SharedSet_ID = viSharedSetID.
/* ================================================================= */
/* override value for identity fields in input */
/* ================================================================= */
/* Verify the debtor code passed inns (all) (Read) (NoCache)
(input viCompanyID, (CompanyId) points to a valid debtor */
<Q-12 run DebtorByDebtorIDCodeNoJoins (all) (Read) (Cache)
(input viCompanyID, (CompanyId)
input '', (DebtorID)
input t_sDebtorShipto.tcDebtorCode, (DebtorCode)
output dataset tqDebtorByDebtorIDCodeNoJoins) in BDebtor >
for first tqDebtorByDebtorIDCodeNoJoins:
assign
t_sDebtorShipTo.SharedSet_ID = tqDebtorByDebtorIDCodeNoJoins.tiSharedSet_ID
t_sDebtorShipTo.tiDebtorBusinessRelation_ID = tqDebtorByDebtorIDCodeNoJoins.tiBusinessRelation_ID.
end.
/* Save the address id and business relation id values back into the shipto record */
if not t_sDebtorShipto.DebtorShiptoIsDebtor and t_sDebtorShipto.tcDebtorEndUserCode = ""
then do:
for first tApiShiptoBusinessRelation
where tApiShiptoBusinessRelation.BusinessRelationCode = t_sDebtorShipTo.tcBusinessRelationCode,
first tApiShiptoAddress
where tApiShiptoAddress.addressstreet1 = t_sDebtorShipTo.tcAddressStreet1
and tApiShiptoAddress.addressstreet2 = t_sDebtorShipTo.tcAddressStreet2
and tApiShiptoAddress.addressstreet3 = t_sDebtorShipTo.tcAddressStreet3
and tApiShiptoAddress.addresscity = t_sDebtorShipTo.tcaddresscity
and tApiShiptoAddress.AddressZip = t_sDebtorShipto.tcAddressZip
and tApiShiptoAddress.tcAddressTypeCode = t_sDebtorShipTo.tcAddressTypeCode
and tApiShiptoAddress.BusinessRelation_ID = tApiShiptoBusinessRelation.businessrelation_id:
assign
t_sDebtorShipTo.Address_ID = tApiShiptoAddress.Address_ID
t_sDebtorShipTo.tiBusinessRelation_ID = tApiShiptoAddress.BusinessRelation_ID.
end.
end. /* end not DebtorIsShipto */
/* ================================================================= */
/* Lookup object by logical key */
/* When in create mode, skip lookup. */
/* ================================================================= */
if vcActivityCode <> "Create":U
then do:
vcFreeform = "for each DebtorShipTo where DebtorShipTo.DebtorShipToCode = '":U + t_sDebtorShipTo.DebtorShipToCode + "'":U
+ " and DebtorShipTo.SharedSet_ID = ":U + string(t_sDebtorShipTo.SharedSet_ID).
<M-2 run DataLoad
(input '' (icRowids),
input '' (icPkeys),
input '' (icObjectIds),
input vcFreeform (icFreeform),
input yes (ilKeepPrevious),
output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
if viFcReturnSuper >= 0
then find first tDebtorShipTo where
tDebtorShipTo.DebtorShipToCode = t_sDebtorShipTo.DebtorShipToCode
no-error.
end. /* if vcActivityCode <> Create */
/* ================================================================= */
/* 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_sDebtorShipTo.
else do:
if viFcReturnSuper <> 0
then oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then leave PROCESSBLOCK.
/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
assign
t_sDebtorShipTo.tc_Rowid = tDebtorShipTo.tc_Rowid
t_sDebtorShipTo.tc_ParentRowid = ""
opPrimeKey = (if opPrimeKey = "" then "" else opPrimeKey + chr(4))
+ string(tDebtorShipTo.DebtorShipTo_ID).
end.
next.
end. /* if vcActivityCode = Delete */
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:
assign
vcMessage = trim(#T-1'Object with key $1 does not exist.':100(48021)T-1#).
<M-3 run SetMessage
(input vcMessage (icMessage),
input t_sDebtorShipTo.DebtorShipToCode (icArguments),
input 'tDebtorShipTo.DebtorShipToCode':U (icFieldName),
input t_sDebtorShipTo.DebtorShipToCode (icFieldValue),
input 'E':U (icType),
input 3 (iiSeverity),
input t_sDebtorShipTo.tc_Rowid (icRowid),
input 'QadFin-6103':U (icFcMsgNumber),
input '' (icFcExplanation),
input '' (icFcIdentification),
input '' (icFcContext),
output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
assign viLocalReturnStatus = -1.
leave MAIN_BLOCK.
end. /* if vcActivityCode = Modify */
/* ================================================================= */
/* When object not found or in create mode, create it. */
/* ================================================================= */
<M-4 run AddDetailLine
(input 'DebtorShipTo':U (icTable),
input '' (icParentRowid),
output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
if viFcReturnSuper < 0 or viFcReturnSuper > 0 and viLocalReturnStatus = 0 then assign viLocalReturnStatus = viFcReturnSuper.
if viLocalReturnStatus < 0 then leave MAIN_BLOCK.
/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
assign
t_sDebtorShipTo.tc_Status = "N":U
t_sDebtorShipTo.tlUpdateAllSharedAddRecords= false.
end. /* if vcActivityCode = Create */
else do:
/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
assign
t_sDebtorShipTo.tc_Status = "C":U
t_sDebtorShipTo.tlUpdateAllSharedAddRecords= false.
end.
/* clear out contacts as we will not be saving them in this component */
for each t_sDebtorShiptoContact:
delete t_sDebtorShiptoContact.
end.
/* insert code for subtables here */
assign
t_sDebtorShipTo.DebtorShipTo_ID = tDebtorShipTo.DebtorShipTo_ID
/* ================================================================= */
/* Update input to allow correct matching */
/* ================================================================= */
t_sDebtorShipTo.tc_Rowid = tDebtorShipTo.tc_Rowid
t_sDebtorShipTo.tc_ParentRowid = ""
opPrimeKey = (if opPrimeKey = "" then "" else opPrimeKey + chr(4))
+ string(tDebtorShipTo.DebtorShipTo_ID).
end.
end. /* of MAIN_BLOCK */
/* Stop queries */
<Q-8 run DebtorByDebtorIDCodeNoJoins (Stop) in BDebtor >
/* Stop instances */
if valid-handle(vhSessionInst)
then do:
<I-49 {bFcCloseInstance
&CLASS = "Session"}>
end.
if viLocalReturnStatus < 0
then do:
<M-98 run StopExternalInstances (output viFcReturnSuper (oiReturnStatus)) in BDebtorShipTo>
end.
/* Error handling */
assign oiReturnStatus = viLocalReturnStatus.