Page 1 of 1

Creating Creditor Purchase line items

PostPosted: Fri Sep 04, 2015 6:04 pm
by neil.interactit
Hi guys,

I am in the process of creating a creditor purchase based on a purchase order. Going well creating (and displaying) the actual creditor purchase, however I am stuck creating line items. I have tried emulating the various approaches that I can find in examples (purchase order line items, sales order line items, etc) but I can't crack it!
Code: Select all
        Dim purchasesUi = JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance.FormFactory.CreateForm(Of JiwaCRBatchTXUI.CreditorPurchases)()

        purchasesUi.InitialReadMode = JiwaApplication.IJiwaNavigable.ReadModes.None
        purchasesUi.Start()
        purchasesUi.CRBatchTransObject.CreateNew()

        purchasesUi.CRBatchTransObject.Description = String.Format("PO {0} {1} Created by {2}", purchaseOrder.OrderNo, purchaseOrder.Reference, purchaseOrder.Staff.DisplayName)
        purchasesUi.CRBatchTransObject.BatchType = CreditorBatchTrans.CreditorBatchTypes.Creditor_Purchase

        For Each poLine As JiwaPurchaseOrders.Line In purchaseOrder.Lines
            Dim item = New CRBatchTranLine()
            item.Creditor = purchaseOrder.Creditor
            purchasesUi.CRBatchTransObject.TransLines.Add(item)
        Next

        purchasesUi.CRBatchTransObject.Save()


This produces an error "item.Creditor is read only" ... is this anywhere close to the correct approach for creating creditor purchase line items?

Thanks in advance,
Neil.

Re: Creating Creditor Purchase line items

PostPosted: Sat Sep 05, 2015 3:03 pm
by Mike.Sheen
Are you really wanting to use the form to create a creditor purchase? You should probably use the business logic, unless you want to display the form to the user at some point.

The Creditor property is read-only, because it is an entity object - if you want to set the creditor then instead of:

Code: Select all
item.Creditor = purchaseOrder.Creditor


Try this instead:

Code: Select all
item.Creditor.ReadRecord(purchaseOrder.Creditor.CreditorID)


And that will set the creditor information for you.

Re: Creating Creditor Purchase line items

PostPosted: Mon Sep 07, 2015 10:52 am
by neil.interactit
Hi Mike,

Thanks - that has got me on the right track. I am now stumbling over a few further details ...

I have attached the complete plugin, the specific code snip is:
Code: Select all
        For Each poLine As JiwaPurchaseOrders.Line In purchaseOrder.Lines
            Dim item = New CRBatchTranLine()
            item.Creditor.ReadRecord(purchaseOrder.Creditor.CreditorID)
            Dim dispersal = New CRBatchDispersal()
            dispersal.OtherLedger.ReadRecordFromAccountNo("802-3004")
            dispersal.HomeDispersedAmountIncGST = 93.94
            item.Dispersals.Add(dispersal)
            purchasesUi.CRBatchTransObject.TransLines.Add(item)
        Next


If I now process this purchase order, I get this creditor purchase:
1a.png


And if I process this purchase order, I get this creditor purchase:
2a.png


So, aside from these error messages, I am having trouble mapping to some of the creditor purchase line item columns (invoice no., remark, etc).

Any chance of a code snippet showing the correct approach?

Many thanks,
Neil.

Re: Creating Creditor Purchase line items  Topic is solved

PostPosted: Mon Sep 07, 2015 5:33 pm
by Mike.Sheen
neil.interactit wrote:So, aside from these error messages, I am having trouble mapping to some of the creditor purchase line item columns (invoice no., remark, etc).

Any chance of a code snippet showing the correct approach?


Hi Neil,

You're close - there are just some quirks in that object (took me a few attempts with your plugin to discover them) - the key points to note I put as comments in the following snippet of code:

Code: Select all
For Each poLine As JiwaPurchaseOrders.Line In purchaseOrder.Lines
   Dim item = New CRBatchTranLine()
   item.Creditor.ReadRecord(purchaseOrder.Creditor.CreditorID)     
   item.HomeTransAmount = poLine.LineTotalExTax         
   item.RemitNo = creditorInvoiceNumber
            
   purchasesUi.CRBatchTransObject.TransLines.Add(item)
   
   ' The adding of a line to the batch automatically also adds a dispersal for the line - all lines must have at least one disperals, so we adjust that with:
   item.Dispersals(1).HomeDispersedAmount = poLine.LineTotalExTax * .6 ' 60%
   item.Dispersals(1).Remark = poLine.Description
   
   ' And now if we choose to, we can add another dispersal
   Dim dispersal = New CRBatchDispersal()
   dispersal.OtherLedger.ReadRecordFromAccountNo("1200-200-00")
   item.Dispersals.Add(dispersal)
   
   ' NOTE: If we try to set the dispersed amount BEFORE adding it to the collection, then we have issues (this is bug)
   dispersal.HomeDispersedAmount = poLine.LineTotalExTax * .4 ' 40%      
   dispersal.Remark = poLine.Description         
Next

Re: Creating Creditor Purchase line items

PostPosted: Tue Sep 08, 2015 10:54 am
by neil.interactit
Phew! ... not that I'm glad there's a bug ... but when it looked like it should work and didn't, I was starting to question my sanity!

Thanks Mike. Sweet!

It turns out my requirement was for multiple PO line items to map to multiple dispersals (as they have a common client and invoice number) ... so this purchase order ...

3a.png

Now produces this creditor purchase ...

3b.png

I tweaked the code to reflect this, appropriately handling the one-or-many dispersals. In case it helps others, here is the final code snip ...

Code: Select all
        Dim item = New CRBatchTranLine()
        item.Creditor.ReadRecord(purchaseOrder.Creditor.CreditorID)
        item.HomeTransAmount = purchaseOrder.TotalNet
        item.RemitNo = creditorInvoiceNumber
        purchasesUi.CRBatchTransObject.TransLines.Add(item)
        ' NOTE: If we try to set ReceiptDate BEFORE adding it to the collection it doesn't stick
        item.ReceiptDate = creditorInvoiceDate
        Dim dispersalNum = 1

        For Each poLine As JiwaPurchaseOrders.Line In purchaseOrder.Lines
            Dim dispersal = New CRBatchDispersal()
            If dispersalNum = 1 Then
                ' The adding of a line to the batch automatically also adds a dispersal for the line - all lines must have at least one disperals, so we adjust that with:
                dispersal = item.Dispersals(1)
            Else
                ' And now if we choose to, we can add another dispersal
                item.Dispersals.Add(dispersal)
            End If
            ' NOTE: If we try to set the dispersed amount BEFORE adding it to the collection, then we have issues (this is bug)
            dispersal.OtherLedger.ReadRecordFromAccountNo(poLine.UserDefinedString1)
            dispersal.HomeDispersedAmount = poLine.LineTotal - poLine.TaxAmount
            dispersal.Remark = poLine.Description
            dispersalNum += 1
        Next

I discovered another gotcha, commented above, with ReceiptDate ... I was onto this much quicker thanks to your previous post! The other change, due to client version (v115), poLine.LineTotalExTax wasn't available, so I used poLine.LineTotal - poLine.TaxAmount instead.

Thanks again,
Neil.