Creditor allocations.  Topic is solved

Discussions relating to Jiwa 7 plugin development, and the Jiwa 7 API.

Creditor allocations.

Postby pricerc » Wed Jan 20, 2021 9:42 am

I've got a creditor payment batch working well:

Code: Select all
Function CreatePayment(ByVal purchaseInvoice As PurchaseInvoice)
   ...
   
   Dim paymentBatch = JiwaFactory.CreateBusinessLogic(Of JiwaCRBatchTX.CreditorBatchTrans)(Nothing)

   paymentBatch.CreateNew()
   paymentBatch.Setup()
   paymentBatch.RecID = NewJiwaID(20)
   paymentBatch.BatchType = JiwaCRBatchTX.CreditorBatchTrans.CreditorBatchTypes.Creditor_Payment
   paymentBatch.BatchDate = Date.Today
   paymentBatch.Description = "Payment for Receipt/Invoice #" & purchaseInvoice.InvoiceNo
   paymentBatch.Save()
   paymentBatch.Read(paymentBatch.RecID) ' reload

   Dim creditorID = purchaseInvoice.Creditor.CreditorID
   Dim receiptDate As Date = Date.Parse(purchaseInvoice.InvoiceDate)

   For Each pil As JiwaPurchaseInvoices.Line In purchaseInvoice.Lines
      Dim payment As JiwaCRBatchTX.CRBatchTranLine = New JiwaCRBatchTX.CRBatchTranLine() With {.Manager = paymentBatch.Manager}
      payment.Setup()
      payment.RecID = NewJiwaID(20)

      payment.RemitNo = purchaseInvoice.InvoiceNo
      payment.Creditor.ReadRecord(creditorID)

      payment.ReceiptDate = receiptDate
      payment.CurrencyRateUsed = pil.CurrencyRateUsed
      paymentBatch.TransLines.Add(payment)
      payment.HomeTransAmount = pil.LineTotal

      Dim dispersal As JiwaCRBatchTX.CRBatchDispersal = payment.Dispersals(1)
      dispersal.OtherLedger.ReadRecord(paymentMethod.RecID)
      dispersal.Remark = purchaseOrder.OrderNo
      If purchaseOrder.BackToBackSalesOrder IsNot Nothing AndAlso Not String.IsNullOrWhiteSpace(purchaseOrder.BackToBackSalesOrder.InvoiceNo) Then
         Dim so = purchaseOrder.BackToBackSalesOrder
         dispersal.LineReference = String.Format("{0} ({1} - {2})", so.InvoiceNo, so.DebtorAccountNo, so.DebtorName)
      End If
   Next

   paymentBatch.Save()


Is there a handy example on how to do the next bit, which is to allocate the payment (to the supplied invoice)?
/Ryan

ERP Consultant,
Advanced ERP Limited, NZ
https://aerp.co.nz
User avatar
pricerc
Senpai
Senpai
 
Posts: 518
Joined: Mon Aug 10, 2009 12:22 pm
Location: Auckland, NZ
Topics Solved: 21

Re: Creditor allocations.

Postby SBarnes » Wed Jan 20, 2021 3:46 pm

Hi Ryan

I had to do an import from excel into the debtor allocations screen recently, there is a creditors screen that looks like it works in a similar fashion and uses the object JiwaFinancials.Jiwa.JiwaCRAllocations.clsCRAllocations I would start by having a look there in a decompiler to see how that object does it.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1696
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 191

Re: Creditor allocations.

Postby pricerc » Fri Jan 22, 2021 1:42 pm

SBarnes wrote:Hi Ryan

I had to do an import from excel into the debtor allocations screen recently, there is a creditors screen that looks like it works in a similar fashion and uses the object JiwaFinancials.Jiwa.JiwaCRAllocations.clsCRAllocations I would start by having a look there in a decompiler to see how that object does it.


Thanks Stuart, but no joy from that direction. The Creditor Allocations screen doesn't have a 'new' button like the Debtor allocation Screen, so has no mechanism for creating new allocations.

I'm just trying to replace a couple of lines of SQL that was used in Jiwa 6 with a 'business object' approach in Jiwa 7; something I thought would be straightforward, but whatever the solution is, isn't immediately obvious.

I was also hoping to avoid having to spend hours reverse-engineering the solution (which using the decompiler always ends up requiring).

I've spent a bit of time in the decompiler, but I've not had any joy getting my head around it. Possibly because this a part of Jiwa I've very unfamiliar with, or it's the way the code decompiles that doesn't always help with understanding the logic.

I've just spotted the "automatic allocations" button on the creditor maintenance screen. Maybe that will help...
/Ryan

ERP Consultant,
Advanced ERP Limited, NZ
https://aerp.co.nz
User avatar
pricerc
Senpai
Senpai
 
Posts: 518
Joined: Mon Aug 10, 2009 12:22 pm
Location: Auckland, NZ
Topics Solved: 21

Re: Creditor allocations.

Postby SBarnes » Fri Jan 22, 2021 2:13 pm

Hi Ryan,

Have a look inside JiwaFinancials.Jiwa.JiwaDebtorsUI.frmAllocations but from the form code it looks like you need to use JiwaFinancials.Jiwa.JiwaDebtors.DebtorAllocation in conjunction with a debtor.Allocations which is a Jiwa collection called JiwaFinancials.Jiwa.JiwaDebtors.DebtorAllocationCollection of JiwaFinancials.Jiwa.JiwaDebtors.DebtorAllocation then I think you just call save on the debtor.

I would assume from that the creditor would work along similar lines as I am more familiar with the debtor side of this.

I always find the best way to work it out with the decompiler is open the relevant form and look at the AddHandlers function as from there you can drill to the code such as a button click on a grid etc. and then see what is being called on the business objects themselves.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1696
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 191

Re: Creditor allocations.  Topic is solved

Postby pricerc » Fri Jan 22, 2021 4:35 pm

Got it going.

You have to:
1) Load the creditor.
2) Load unaged transactions
3) Load allocations
4) Create a new allocation
5) Set its values
6) Add it to the creditor's allocations
7) Save the creditor.

Mostly straightforward, the bit that took me a few (impatient) takes to get right was allocation.CreditTransID. You have to look in Creditor.Transactions to find the transaction whose SourceID.Trim() = paymentBatch.RecID

This code has not been optimised, but it is working for my scenario; there is some refactoring that is probably in order for my plugin, but this will at least help the next person who needs something similar.

Code: Select all
        Private Sub CreateAllocation(purchaseInvoice As PurchaseInvoice, paymentBatch As CreditorBatchTrans, creditorID As String, localValue As Decimal, foreignValue As Decimal)
            If Debugger.IsAttached Then Debugger.Break()

            Dim creditor As JiwaCreditors.Creditor = JiwaFactory.CreateBusinessLogic(Of JiwaCreditors.Creditor)(Nothing)
            creditor.Setup()
            creditor.Read(creditorID)

            creditor.ReadTransactions(False)
            Dim paymentTransaction = (From ct In creditor.Transactions.ToEnumerable() Where ct.SourceID.Trim() = paymentBatch.RecID.Trim()).FirstOrDefault()
            If paymentTransaction Is Nothing Then
                Throw New ApplicationException("Error processing payment allocation: payment not found in creditor transactions.")
            End If

            Dim allocation = creditor.Manager.CollectionItemFactory.CreateCollectionItem(Of JiwaCreditors.CreditorAllocation)()
            allocation.Manager = creditor.Manager
            allocation.Setup()
            allocation.DebitTransID = purchaseInvoice.RecID
            allocation.CreditTransID = paymentTransaction.RecID
            allocation.CreditAmount = localValue
            allocation.DebitAmount = localValue
            allocation.FXAmount = foreignValue
            allocation.DateAlloc = paymentBatch.BatchDate

            creditor.Allocations.Read()
            creditor.Allocations.Add(allocation)

            creditor.Save()
        End Sub
/Ryan

ERP Consultant,
Advanced ERP Limited, NZ
https://aerp.co.nz
User avatar
pricerc
Senpai
Senpai
 
Posts: 518
Joined: Mon Aug 10, 2009 12:22 pm
Location: Auckland, NZ
Topics Solved: 21

Re: Creditor allocations.

Postby SBarnes » Fri Jan 22, 2021 4:48 pm

Yeah pretty much the way the debtor works as well I am also pretty sure that if you read the allocations and then remove an allocation from the collection that's how you can clear an allocation out.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1696
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 191

Re: Creditor allocations.

Postby pricerc » Fri Jan 22, 2021 5:11 pm

SBarnes wrote:Yeah pretty much the way the debtor works as well I am also pretty sure that if you read the allocations and then remove an allocation from the collection that's how you can clear an allocation out.


it was really easy once I'd spotted the "automatic allocations" button. A bit of a Homer "Doh!" moment, really.

I don't actually *use* Jiwa a lot. I do a fair bit of coding around it, but I hardly ever actually use it outside of testing the plugins I'm working on.

So I don't always know what all the UI options are for doing something. I have a colleague who usually helps me out with that, but when I was asking about how creditor allocations would be done manually, he (it seems) didn't think the "automatic allocations" would be relevant, because they just do a blind FIFO allocation of the transactions, whereas I needed to do a one-to-one allocation of specific transactions. So he thought they were completely different.
/Ryan

ERP Consultant,
Advanced ERP Limited, NZ
https://aerp.co.nz
User avatar
pricerc
Senpai
Senpai
 
Posts: 518
Joined: Mon Aug 10, 2009 12:22 pm
Location: Auckland, NZ
Topics Solved: 21


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 4 guests