project QadFinancials > class BDInvoice > method ApiCreateDInvoicesFinCharge
Description
This Method creates DInvoices of type Finance Charge
The invoice itself, the DI posting (non-taxable) and the movement record will be created.
Following tables will be updated : DInvoice, DInvoicePosting, DInvoiceMovement
(The balance and the status will also be updated.)
Parameters
icAction | input | character | This parameter defines what should happen with the input data : -SAVE: Save the data if there were no errors, -SAVESTORE : Save the data if there were no errors. If there were errors, save as draft. -STORE : Save the data as draft -VALIDATE : Validate the input data, but do no save the data. |
tApiDInvoice | input | temp-table | Input temp-table defined like class table tDInvoice and that is used to fill the input temp-table for calling CreateDInvoices |
tUpdateDILastFinChargeDate | input | temp-table | temp-table holding the date of the finance charge. This data is used for calling method CreateDInvoicesFinChargeSub |
bcLstReturn | input-output | character | Char 4 separated list with the return status of the newly created main-table records. Zero means not errors occurred. |
bcLstPrimKey | input-output | character | Character 4 separated list with the primary keys of the newly created main-table records. The fields of the primary key are separated with Character 2. |
bcLstRowid | input-output | character | Char 4 separated list with the temporarely rowids (negative for new ones) of the newly created main-table records. |
olSaveAsDraft | output | logical | Output parameter that keeps if the data is saved as draft or not |
oiReturnStatus | output | integer | Return status of the method. |
Internal usage
QadFinancials
program code (program9/bdinvoice.p)
assign oiReturnStatus = -98.
empty temp-table tDIOpenBalance.
for each tApiDInvoice :
empty temp-table tDIOpenBalance.
viLastDInvoiceID = tApiDInvoice.DInvoice_ID.
/* Initialise: Validate the value for the Company */
/* Replace unknown values */
assign vlWarningsFound = false.
if tApiDInvoice.Company_ID <> 0 and tApiDInvoice.Company_ID <> ? and tApiDInvoice.Company_ID <> viCompanyId
then do:
assign vcMessage = trim(substitute(#T-1'The entity (&1) of the customer invoice must match the current entity (&2).':255(60907)T-1#, 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-7 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-6195':U (icFcMsgNumber),
input '':U (icFcExplanation),
input '':U (icFcIdentification),
input '':U (icFcContext),
output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
Return.
end. /* if tApiDInvoice.Company_ID <> 0 and */
if tApiDInvoice.Debtor_ID = ? then assign tApiDInvoice.Debtor_ID = 0.
if tApiDInvoice.tcDebtorCode = ? then assign tApiDInvoice.tcDebtorCode = "":U.
if tApiDInvoice.DInvoiceType = ? then assign tApiDInvoice.DInvoiceType = "":U.
/* Create tOpenBalance record that will be used for a call to a submethod */
create tDIOpenBalance.
assign tDIOpenBalance.tiCompanyId = tApiDInvoice.Company_ID
tDIOpenBalance.tcAction = icAction
tDIOpenBalance.tcDebtorCode = tApiDInvoice.tcDebtorCode
tDIOpenBalance.tcInvoiceType = tApiDInvoice.DInvoiceType
tDIOpenBalance.tiPeriodYear = tApiDInvoice.DInvoicePostingYear
tDIOpenBalance.tiPeriodPeriod = tApiDInvoice.DInvoicePostingPeriod
tDIOpenBalance.tcJournalCode = tApiDInvoice.tcJournalCode
tDIOpenBalance.tiInvoiceVoucher = tApiDInvoice.DInvoiceVoucher
tDIOpenBalance.ttInvoiceDate = tApiDInvoice.DInvoiceDate
tDIOpenBalance.ttPostingDate = tApiDInvoice.DInvoicePostingDate
tDIOpenBalance.ttInvoiceDueDate = tApiDInvoice.DInvoiceDueDate
tDIOpenBalance.tcPostingType = if tApiDInvoice.DInvoiceOriginalDebitTC <> 0 and tApiDInvoice.DInvoiceOriginalDebitTC <> ? then {&POSTINGTYPE-DEBIT} else {&POSTINGTYPE-CREDIT}
tDIOpenBalance.tcPostingText = trim(#T-32'Finance Charge':50(999890579)T-32#)
tDIOpenBalance.tdInvoiceAmountTC = if tApiDInvoice.DInvoiceOriginalDebitTC <> 0 and tApiDInvoice.DInvoiceOriginalDebitTC <> ? then tApiDInvoice.DInvoiceOriginalDebitTC else tApiDInvoice.DInvoiceOriginalCreditTC
tDIOpenBalance.tdInvoiceAmountLC = if tApiDInvoice.DInvoiceOriginalDebitLC <> 0 and tApiDInvoice.DInvoiceOriginalDebitLC <> ? then tApiDInvoice.DInvoiceOriginalDebitLC else tApiDInvoice.DInvoiceOriginalCreditLC
tDIOpenBalance.tdExchangeRateTCLC = tApiDInvoice.DInvoiceExchangeRate
tDIOpenBalance.tdExchangeRateScale = tApiDInvoice.DInvoiceRateScale
tDIOpenBalance.tcTCCurrencyCode = tApiDInvoice.tcCurrencyCode
tDIOpenBalance.ttInvoiceTaxPointDate = tApiDInvoice.DInvoicePostingDate
tDIOpenBalance.tcProjectCode = tApiDInvoice.tcProjectCode
tDIOpenBalance.tcReasonCode = tApiDInvoice.tcReasonCode
tDIOpenBalance.tcCostCentreCode = tApiDInvoice.tcCostCentreCode
tDIOpenBalance.tcDivisionCode = tApiDInvoice.tcDivisionCode
tDIOpenBalance.tcInvoiceReference = trim(tDIOpenBalance.tcPostingText) + " ":U + trim(string(today)) + " (":U +
if length(string(tDIOpenBalance.tdInvoiceAmountTC),"CHARACTER") > 7 then
trim(substring(string(tDIOpenBalance.tdInvoiceAmountTC),1,7,'CHARACTER')) + ")":U
else
trim(string(Round(tDIOpenBalance.tdInvoiceAmountTC,1))) + ")":U
tDIOpenBalance.tcInvoiceDescription = tDIOpenBalance.tcInvoiceReference
tDIOpenBalance.tcNormalPaymentConditionCode = tApiDInvoice.tcNormalPaymentConditionCode
tDIOpenBalance.tcKey = "KEY":U.
/* Get the AVRCode of the current company (used te retrieve the VAT-defaults */
<Q-8 run CompanyPropertyByBusinessRel (all) (Read) (NoCache)
(input tDIOpenBalance.tiCompanyId, (CompanyId)
input ?, (AddressType)
output dataset tqCompanyPropertyByBusinessRel) in BCompanyProperty >
find first tqCompanyPropertyByBusinessRel no-error.
if available tqCompanyPropertyByBusinessRel
then do:
<Q-9 run VatNumberByIdBusRelCntryVat (all) (Read) (NoCache)
(input ?, (BusinessRelationId)
input ?, (IdentityCountryId)
input ?, (IdentityCountryCode)
input tqCompanyPropertyByBusinessRel.tcVatNumberIdentity, (VatNumberIdentity)
input tqCompanyPropertyByBusinessRel.tiVatNumber_ID, (VatNumberId)
input ?, (VatNumberIsActive)
output dataset tqVatNumberByIdBusRelCntryVat) in BBusinessRelation >
find first tqVatNumberByIdBusRelCntryVat no-error.
assign vcCompanyAVRCode = if available tqVatNumberByIdBusRelCntryVat then tqVatNumberByIdBusRelCntryVat.tcBusinessRelationAVRCode else "":U
vcShipFromTaxZone = tqCompanyPropertyByBusinessRel.tcTxzTaxZone
vcShipFromCountryCode = tqCompanyPropertyByBusinessRel.tcAddressCountryCode
vlShipFromIsEUCountry = tqCompanyPropertyByBusinessRel.tlIdentityIsEUCountry.
end. /* if available tqCompanyPropertyByBusinessRel */
/* Get some values from the debtor */
if tApiDInvoice.Company_ID <> 0 and tApiDInvoice.Company_ID <> ? and
tApiDInvoice.tcDebtorCode <> "":U and tApiDInvoice.tcDebtorCode <> ?
then do:
<Q-10 run DebtorByDebtor (all) (Read) (NoCache)
(input tApiDInvoice.Company_ID, (CompanyId)
input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressType)
input tApiDInvoice.Debtor_ID, (DebtorId)
input ?, (DebtorCode)
output dataset tqDebtorByDebtor) in BDebtor >
find first tqDebtorByDebtor no-error.
if available tqDebtorByDebtor
then do:
assign tDIOpenBalance.tcBusinessRelationCode = tqDebtorByDebtor.tcBusinessRelationCode
vcVatDeliveryType = tqDebtorByDebtor.tcVatDeliveryType
vcVatPercentageLevel = tqDebtorByDebtor.tcVatPercentageLevel
vcTxclTaxClass = tqDebtorByDebtor.tcTxclTaxCls
vcTxuTaxUsage = tqDebtorByDebtor.tcTxuTaxUsage
vcDebtorCode = tqDebtorByDebtor.tcDebtorCode
vlAddressIsTaxInCity = tqDebtorByDebtor.tlAddressIsTaxInCity
vcShipToTaxZone = tqDebtorByDebtor.tcAddressTxzTaxZone
vcShipToCountryCode = tqDebtorByDebtor.tcCountryCode
vlShipToIsEUCountry = tqDebtorByDebtor.tlCountryIsEUCountry
vcDebtorTaxZone = tqDebtorByDebtor.tcTxzTaxZone.
/* Get AVRCode */
<Q-11 run AddressByAddressBusRelType (all) (Read) (NoCache)
(input ?, (AddressTypeId)
input {&ADDRESSTYPECODESYSTEM-HEADOFFICE}, (AddressTypeCode)
input tqDebtorByDebtor.tiBusinessRelation_ID, (BusinessRelationId)
input tqDebtorByDebtor.tcBusinessRelationCode, (BusinessRelationCode)
input ?, (AddressId)
output dataset tqAddressByAddressBusRelType) in BBusinessRelation >
find first tqAddressByAddressBusRelType no-error.
if available tqAddressByAddressBusRelType
then assign vcDebtorAVRCode = tqAddressByAddressBusRelType.tcBusinessRelationAVRCode.
/* Get division */
if tqDebtorByDebtor.tiDivisionProfile_ID <> ? and tqDebtorByDebtor.tiDivisionProfile_ID <> 0
then do:
<Q-12 run GetDivisionFromProfile (all) (Read) (NoCache)
(input tApiDInvoice.Company_ID, (CompanyId)
input tqDebtorByDebtor.tiDivisionProfile_ID, (DivisionProfileId)
output dataset tqDivisionFromProfile) in BProfile >
find first tqDivisionFromProfile no-error.
if available tqDivisionFromProfile
then assign tDIOpenBalance.tcDivisionCode = tqDivisionFromProfile.tcDivisionCode.
end. /* if tqDebtorByDebtor.tiDivisionProfile_ID <> ? and tqDebtorByDebtor.tiDivisionProfile_ID <> 0 */
/* Get the Payment-conditions from the Debtor */
if tApiDInvoice.tcNormalPaymentConditionCode = "":U or tApiDInvoice.tcNormalPaymentConditionCode = ?
then assign tDIOpenBalance.tcNormalPaymentConditionCode = (if tDIOpenBalance.tcNormalPaymentConditionCode = "":U or tDIOpenBalance.tcNormalPaymentConditionCode = ? then tqDebtorByDebtor.tcNormalPaymentConditionCode else tDIOpenBalance.tcNormalPaymentConditionCode).
/* Get the bank information if the debtor uses directdebit payment instruments */
assign vhFcComponent = ?.
<M-28 run GetParentBankData
(input viCompanyID (iiCompanyID),
input ? (icCreditorCode),
input tqDebtorByDebtor.tcDebtorCode (icDebtorCode),
input {&PAYFORMATPAYINSTRUMENT-DIRECTDEBIT} (icPaymentInstrument),
output tDIOpenBalance.tiBankNumber_ID (oiBankNumberID),
output tDIOpenBalance.tcBankNumber (ocBankNumber),
output tDIOpenBalance.tcBankNumberExtension (ocBankNumberExtension),
input ? (icOwnBankNumber),
input ? (icGLCode),
output viFcReturnSuper (oiReturnStatus)) in BBankNumber>
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 */
end. /* if available tqDebtorByDebtor */
end. /* if tApiDInvoice.Company_ID <> 0 */
/* Calculate due dates */
<M-14 run ApiStdMaintainTTWithIntPostDates
(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 */
assign tDIOpenBalance.ttInvoiceDueDate = tApiDInvoice.DInvoiceDueDate
tDIOpenBalance.ttInvoiceDiscountDueDate = tApiDInvoice.DInvoiceDueDate. /* discount due date is same as normal due date for finance charges (FIN-1328) */
/* Call a submethod that will create the debtor invoices */
/* Result: DInvoice, DInvoicePosting and DInvoiceMovement records created */
<M-15 run CreateDInvoices
(input-output tDIOpenBalance (tDIOpenBalance),
input 0 (iiBJournalEntryId),
input 0 (iiPostingId),
output vcListNewInvoices (ocNewRecordInfo),
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 */
/* Search the debtor invoice that you have just created */
assign viListNewInvoices = integer(entry(2,vcListNewInvoices)) no-error.
if error-status:error = false
then find first tDInvoice where
tDInvoice.DInvoice_ID = viListNewInvoices
no-error.
if error-status:error = true or
not available tDInvoice
then do:
assign vcMessage = trim(substitute(#T-3'An internal error occurred on the customer invoice integration. The call towards the method (&1) that initiates the invoice returned unexpected information.':255(60908)T-3#, "CreateDInvoices":U)) + chr(10) +
trim(substitute(#T-4'Returned data: &1.':255(60909)T-4#,viListNewInvoices))
oiReturnStatus = -1
bcLstReturn = bcLstReturn + chr(4) + string(-1)
bcLstPrimKey = bcLstPrimKey + chr(4) + "*":U
bcLstRowid = bcLstRowid + chr(4) + "*":U
vlSaveAsDraft = false.
if error-status:error = true and
error-status:num-messages >= 1
then assign vcMessage = vcMessage + chr(10) + ERROR-STATUS:GET-MESSAGE(1).
<M-16 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-6207':U (icFcMsgNumber),
input '':U (icFcExplanation),
input '':U (icFcIdentification),
input '':U (icFcContext),
output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
Return.
end. /* if error-status:error = true or */
/* Copy all data into the new cinvoice */
/* 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 */
assign vcDInvoiceRowId = tDInvoice.tc_Rowid
vcCommitNumber = vcCommitNumber + ",":U + tDInvoice.tc_Rowid
tDInvoice.tcReasonCode = tApiDInvoice.tcReasonCode
tDInvoice.ShipToAddress_ID = tApiDInvoice.ShipToAddress_ID
tDInvoice.tcShipToAddressCity = tApiDInvoice.tcShipToAddressCity
tDInvoice.tcShipToAddressStreet1 = tApiDInvoice.tcShipToAddressStreet1
tDInvoice.tcShipToAddressStreet2 = tApiDInvoice.tcShipToAddressStreet2
tDInvoice.tcShipToAddressStreet3 = tApiDInvoice.tcShipToAddressStreet3
tDInvoice.tcShipToAddressZip = tApiDInvoice.tcShipToAddressZip
tDInvoice.tcShipToAddressTypeCode = tApiDInvoice.tcShipToAddressTypeCode
tDInvoice.tcShipToBusinessRelationCode = tApiDInvoice.tcShipToBusinessRelationCode
tApiDInvoice.DInvoice_ID = tDInvoice.DInvoice_ID.
end. /* for each tApiDInvoice */
/* 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 */
/* Method to update the Invoices involved with the Finance Charge Calculation Date */
<M-29 run CreateDInvoicesFinChargeSub
(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
vcMessage = trim(#T-30'An error occurred while updating Last Finance Charge Run Date. Check other error messages for more details.':150(64859)T-30#).
<M-31 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-6804':U (icFcMsgNumber),
input '':U (icFcExplanation),
input '':U (icFcIdentification),
input '':U (icFcContext),
output viFcReturnSuper (oiReturnStatus)) in BDInvoice>
Return.
end. /* if oiReturnStatus < 0 */
end. /* if viFcReturnSuper <> 0 */
/* Validate the data and performa AddUpd */
<M-25 run ValidateBC
(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 */
<M-26 run AdditionalUpdates
(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 */
/* Save the data */
if icAction <> {&DAEMONACTION-VALIDATE}
then do:
<M-27 run DataSave
(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 */
end. /* if icAction <> {&DAEMONACTION-VALIDATE} */
/* 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(viLastDInvoiceID)
bcLstRowid = bcLstRowid + chr(4) + vcDInvoiceRowId
oiReturnStatus = (if vlWarningsFound = true then 1 else 0).