Automating Jiwa using Powershell

Samples and Examples of using the Jiwa 7 framework - including plugins, stand-alone applications and scripts.

Automating Jiwa using Powershell

Postby Mike.Sheen » Tue Jan 28, 2014 4:38 pm

One of the cool things you can do in Powershell is interact with .NET assemblies.

As Jiwa 7 is a .NET product, there's a lot of scope for automating tasks in Jiwa using powershell.

As an example, here is a powershell script to import sales orders from a csv file, and send an email at the conclusion of the import.

Code: Select all
## Script will import sales orders from a CSV file
## And at the conclusion will email a notification
## that the import completed.

## Load assemblies
[Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Jiwa Financials\Jiwa 7\JiwaApplication.dll")
[Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Jiwa Financials\Jiwa 7\JiwaLib.dll")
[Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Jiwa Financials\Jiwa 7\JiwaODBC.dll")
[Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Jiwa Financials\Jiwa 7\JiwaEncryption.dll")
[Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Jiwa Financials\Jiwa 7\JiwaJrnlPost.dll")
[Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Jiwa Financials\Jiwa 7\JiwaPriceSchemes.dll")
[Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Jiwa Financials\Jiwa 7\JiwaJobCosting.dll")
[Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Jiwa Financials\Jiwa 7\JiwaLock.dll")
[Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Jiwa Financials\Jiwa 7\JiwaSales.dll")
[Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Jiwa Financials\Jiwa 7\JiwaSendEmail.dll")

## Create new JiwaApplication Manager, and login
$appman = [JiwaFinancials.Jiwa.JiwaApplication.Manager]::Instance
$appman.Logon("JiwaMike", "JiwaDemo", 1, "Admin", "password")

## Get the business logic factory, which we will need later to create business logic objects
$factory = $appman.BusinessLogicFactory
$method = [JiwaFinancials.Jiwa.JiwaApplication.BusinessLogicFactory].GetMethod("CreateBusinessLogic")

## Load a sales order
## Using the business logic factory is not very intuitive in powershell as it uses a generic, and poweshell makes you jump through a few hoops
$closedMethod = $method.MakeGenericMethod([JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder])
$salesorder = $closedMethod.Invoke($factory, $null)

## $salesorder.Find(4, "SO_Main.InvoiceNo", "", "")

## Sample CSV
##OrderNo,OrderDate,DebtorAccountNo,PartNo,QuantityOrdered,PriceExGST
##000100,2006-05-30,1001,1170,5,15.45
##000100,2006-05-30,1001,1171,5,15.45
##000100,2006-05-30,1001,1172,5,15.45
##000100,2006-05-30,1001,1173,5,15.45
##000101,2006-06-01,1002,1170,5,15.45
##000101,2006-06-01,1002,1171,5,15.45
##000101,2006-06-01,1002,1172,5,15.45
##000102,2006-06-02,1003,1173,5,15.45

$csv = Import-Csv "C:\users\mikes\documents\sample.txt"
$previousOrderNo = ""
$newKey = ""
$InvoiceNos = @()

foreach ($line in $csv) {
   if ($previousOrderNo -ne $line.OrderNo) {
                #if the order no. has changed, then we've finished adding to this order - save and move on.
      If ($salesorder.InsertFlag) {
         $salesorder.Save()
         $InvoiceNos += $salesorder.InvoiceNo
      }
      
      #$salesorder.CreateNew(0, $line.DebtorAccountNo, 1, "", "", "", "")
      $salesorder.CreateNew(0, $line.DebtorAccountNo, $null, $null, $null, $null, $null)
      $salesorder.OrderNo = $line.OrderNo
      $salesorder.InitiatedDate = $line.OrderDate      
   }
   
   $salesOrder.SalesOrderLines.AddInventoryItem($line.PartNo, 1, [ref]$newKey, $null, 0)
   $salesOrderLine = $salesOrder.SalesOrderLines[$newKey]
   $salesOrderLine.QuantityOrdered = $line.QuantityOrdered
   $salesOrderLine.DiscountedPrice = $line.PriceExGST
   $previousOrderNo = $line.OrderNo
}

If ($salesorder.InsertFlag) {
   $salesorder.Save()
   $InvoiceNos += $salesorder.InvoiceNo
}

#send an email
$closedMethod = $method.MakeGenericMethod([JiwaFinancials.Jiwa.JiwaApplication.JiwaEmail.EmailMessage])
$mail = $closedMethod.Invoke($factory, $null)
$mail.CreateNew()
$mail.EmailTo = "[email protected]"
$mail.EmailSubject = "Sales Order Import Completed"
$body = "The following sales orders were imported:"
$body += $InvoiceNos | out-string

$mail.EmailBody = $body
#By default the mail status is "sent" so that when you save the email, it will attempt to send it.
#You can opt to set the status to "ready to send", and have another process send it - if desired.
#Note that the transport mechanism is controlled by the Jiwa system settings - You can opt to send directly (not recommended), via relay server (recommended) or by using a locally installed Outlook instance.
$mail.Save()

Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2445
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 757

Return to Samples and Examples

Who is online

Users browsing this forum: Google [Bot] and 1 guest