Sales order logging incorrect dates  Topic is solved

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

Sales order logging incorrect dates

Postby jeromeh » Wed Oct 07, 2015 2:02 pm

Hi,

We are having a problem in Jiwa 7.0.129 in where the dates being saved on new sales orders appear to be wrong for some reason.

We have an AFTER INSERT trigger set up on SO_History to log every record along with the SQL server GETDATE() datetime and the InvoiceInitDate from the SO_Main table.

This is the kind of thing we are seeing in our logs that we cannot explain:


ServerGETDATEInvoiceHistoryIDLastModifiedByRecordDateHistoryNoRefDateProcessedDatePosted
2015-10-06 16:45:06.243c8fb941cf962428588e3NeotekSep 30 2015 2:16PM1D01 - Inserted By Neotek 30/09/2015 02:16:13NULLSep 30 2015 2:16PM


DateLastSavedDateCreatedDeliveredDateSOINITDATE
Oct 6 2015 4:45PMSep 30 2015 2:16PMNULLSep 30 2015 2:16PM



The server date is set correctly on the machine that these sales orders are being entered in on.
We are using the Jiwa DLLs to insert our records and we are not specifying the initdate ourselves.
This is not happening for every order we save, only some.
The dates always seem to be roughly the same e.g. we have three in a row with the incorrect date but 2 seconds apart each.

Is there some reason as to why this might be occurring?
jeromeh
Occasional Contributor
Occasional Contributor
 
Posts: 17
Joined: Wed Apr 08, 2009 9:53 am

Re: Sales order logging incorrect dates

Postby perry » Wed Oct 07, 2015 5:21 pm

+1
we are having exactly the same issue with .129.

SO is created from webservice and not specifying init date or any date properties.
I cant see any pattern, the difference varies between seconds to hours...
Perry Ma
S. Programmer
Lonicera Pty Ltd
http://www.lonicera.com.au
perry
Frequent Contributor
Frequent Contributor
 
Posts: 173
Joined: Mon Oct 27, 2008 2:26 pm
Topics Solved: 15

Re: Sales order logging incorrect dates

Postby jeromeh » Wed Oct 14, 2015 2:36 pm

We have had another instance of this occur on 7.0.136, installed on a completely separate server to our 129 install.
jeromeh
Occasional Contributor
Occasional Contributor
 
Posts: 17
Joined: Wed Apr 08, 2009 9:53 am

Re: Sales order logging incorrect dates

Postby Scott.Pearce » Tue Oct 20, 2015 9:23 am

So you are saying that the sales order was entered on 2015-10-06, but the SOINITDATE is recorded as 2015-09-30?
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 765
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 230

Re: Sales order logging incorrect dates

Postby jeromeh » Thu Oct 22, 2015 7:17 am

Yes that is correct.
jeromeh
Occasional Contributor
Occasional Contributor
 
Posts: 17
Joined: Wed Apr 08, 2009 9:53 am

Re: Sales order logging incorrect dates

Postby Scott.Pearce » Thu Oct 22, 2015 8:06 am

The InitDate is set in the Jiwa sales order CreateNew() method thus:

Code: Select all
If JiwaApplication.Manager.Instance.Database.ReadSysData("InvoicingParams", "InvoicesFromQuotesUseActivateDate", 0) <> "1" Then
   If lSalesQuote.SalesQuoteHistorys(lSalesQuote.SelectedHistoryNo).SalesQuoteActivate.RepeatPeriod = 0 Then 'e_SalesQuoteActivatePeriodNone
      m_InitiatedDate = lSalesQuote.InitiatedDate
   Else
      m_InitiatedDate = lSalesQuote.SalesQuoteHistorys(lSalesQuote.SelectedHistoryNo).SalesQuoteActivate.NextActivate
   End If
Else
   m_InitiatedDate = JiwaApplication.Manager.Instance.SysDateTime
End If
m_ExpectedDeliveryDate = lSalesQuote.ExpectedDeliveryDate
lSalesOrderHistory = New SalesOrderHistory(GstTaxRates)

SalesOrderHistorys.SupressEvents = True
lSalesOrderHistory.RecordDate = m_InitiatedDate


Note that "JiwaApplication.Manager.Instance.SysDateTime" is the login date. Just to confuse everyone, JiwaApplication.Manager.Instance.SysDateTime is a read-only property that simply returns JiwaApplication.Manager.Instance.Database.SysDateTime. THIS IS THE LOCAL MACHINE DATE NOT THE DATABASE DATE (misleading I know).

When a user (or program) logs into Jiwa, the login date is set to match the local date and time of the machine the user/program is on. The user/program is then able to change the login date (ie. by right-clicking on the date-time at the lower right of the main Jiwa screen, or by setting JiwaApplication.Manager.Instance.Database.SysDateTime).

If I didn't know any better, I'd be claiming that the machine the user/program is on has it's date wrong - please double check this. Further, for debug, can the program creating the sales orders perhaps dump out JiwaApplication.Manager.Instance.SysDateTime, along with an InvoiceID to a file on the local file-system every-time it creates a sales order? This may reveal something we can work with.

Finally, are you able to post your code that is responsible for creating the sales orders?
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 765
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 230

Re: Sales order logging incorrect dates

Postby perry » Fri Nov 06, 2015 5:45 pm

Hi Scot,

I understand system date time, except, I'm 100% sure in my case, date/time on web server and sql server are the same.

In my case, we have,
- web server hosts the interface (code below) and creates sales order using Dlls
- we login by using database object every time there is a new order
- we log time we received web request and comparing that with order init date
- time varies between 1min to 2 hours sometime

I dont want to update the code to log system date time as you suggested now but we will be upgrading the customer soon so I have chance then.
Is there anything else you want me to log?

Code: Select all
Public Shared Function SubmitOrder(doc As cXML) As cXML

        Dim docReturn As New cXML(cXML.DocType.Response)
        Dim docReturnStatus As New cXML.clsStatus
        Dim sSubmittedByUserName As String = ""
        Try

            JiwaApplication.Manager.Instance.Logon(My.Settings.SqlServerName, My.Settings.SqlDatabaseName, JiwaFinancials.Jiwa.JiwaODBC.database.AuthenticationModes.JiwaAuthentication, My.Settings.JiwaLoginUserName, My.Settings.JiwaLoginUserPassword)

            Dim splugins As String = ""
            For Each plugin As JiwaApplication.Plugin.Plugin In JiwaApplication.Manager.Instance.PluginCollection
                splugins += plugin.Name
            Next
            Logger.Write("Plugins loaded: " & splugins, "general")

            Dim salesOrder As JiwaSales.SalesOrder.SalesOrder = JiwaApplication.Manager.Instance.BusinessLogicFactory.CreateBusinessLogic(Of JiwaSales.SalesOrder.SalesOrder)(Nothing)
            Dim debtor As JiwaDebtors.Debtor = JiwaApplication.Manager.Instance.BusinessLogicFactory.CreateBusinessLogic(Of JiwaDebtors.Debtor)(Nothing)

            Dim addFreightItem As Boolean = False
            Dim processFeeItemNo As String = ""
            Dim processFeeAmount As Decimal

            debtor.Find(JiwaApplication.IJiwaNavigable.ReadModes.Actual, "DB_Main.AltAccountNo", doc.Request.OrderRequest.OrderRequestHeader.ShipTo.Address.AddressID, String.Empty)


            With salesOrder

                .OnCreateStart()
                .OnCreating(JiwaSales.SalesOrder.SalesOrder.NewSalesOrderTypes.e_NewSalesOrder, debtor.DebtorID, True, "", "", "")
                .CreateNew(JiwaSales.SalesOrder.SalesOrder.NewSalesOrderTypes.e_NewSalesOrder, debtor.DebtorID, True, "", "", "", My.Settings.JiwaDefaultLogicalID)

                .OrderNo = doc.Request.OrderRequest.OrderRequestHeader.OrderID

'......               

            salesOrder.OnCreateEnd()

            salesOrder.OnSaveStart()
            salesOrder.Save()
            salesOrder.OnSaveEnd()


            With docReturnStatus
                .Code = cXML.ResponseStatus.Status200.Code
                .Text = "OK"
            End With

        Catch ex As Exception
            Logger.Write("SubmitOrder: " & ex.ToString, "error")
            With docReturnStatus
                .Code = cXML.ResponseStatus.Status200.Code
                .Text = ex.Message
            End With
        Finally
            If Not IsNothing(JiwaApplication.Manager.Instance) Then
                JiwaApplication.Manager.Instance.LogOff()
            End If
        End Try

        'docReturn.Timestamp = Now
        docReturn.Response.Status = docReturnStatus

        Return docReturn
    End Function
Last edited by perry on Mon Nov 09, 2015 9:01 am, edited 2 times in total.
Perry Ma
S. Programmer
Lonicera Pty Ltd
http://www.lonicera.com.au
perry
Frequent Contributor
Frequent Contributor
 
Posts: 173
Joined: Mon Oct 27, 2008 2:26 pm
Topics Solved: 15

Re: Sales order logging incorrect dates

Postby Scott.Pearce » Mon Nov 09, 2015 8:56 am

Is there anything else you want me to log?


All the date time information you can think of. I can think of:

Local machine (i.e. Now())
SQL Server (i.e. GetDate())
JiwaApplication.Manager.Instance.SysDateTime
JiwaApplication.Manager.Instance.ServerDateTime
salesOrder.InitiatedDate *before* .Save
salesOrder.InitiatedDate *after* .Save
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 765
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 230

Re: Sales order logging incorrect dates

Postby perry » Tue Jan 19, 2016 5:23 pm

Hi Scott,

I've got some logs for you,
Below function is part of a web service interface and gets called every time we receive an order
Code: Select all
Public Shared Function SubmitOrder(doc As cXML) As cXML

        Dim docReturn As New cXML(cXML.DocType.Response)
        Dim docReturnStatus As New cXML.clsStatus
        Dim sSubmittedByUserName As String = ""
        Try

            JiwaApplication.Manager.Instance.Logon(My.Settings.SqlServerName, My.Settings.SqlDatabaseName, JiwaFinancials.Jiwa.JiwaODBC.database.AuthenticationModes.JiwaAuthentication, My.Settings.JiwaLoginUserName, My.Settings.JiwaLoginUserPassword)

            'PM 2015-07-01, for testing
            Dim splugins As String = ""
            For Each plugin As JiwaApplication.Plugin.Plugin In JiwaApplication.Manager.Instance.PluginCollection
                splugins += plugin.Name
            Next
            Logger.Write("Plugins loaded: " & splugins, "general")

            'v1.3.1
            Logger.Write("datebase date : " & GetDBDate(), "general")
            Logger.Write("system.date : " & Date.Now, "general")
            Logger.Write("JiwaApplication.Manager.Instance.SysDateTime : " & JiwaApplication.Manager.Instance.SysDateTime, "general")
            Logger.Write("JiwaApplication.Manager.Instance.SysDateTime : " & JiwaApplication.Manager.Instance.Database.ServerDateTime, "general")

            Dim salesOrder As JiwaSales.SalesOrder.SalesOrder = JiwaApplication.Manager.Instance.BusinessLogicFactory.CreateBusinessLogic(Of JiwaSales.SalesOrder.SalesOrder)(Nothing)
            Dim debtor As JiwaDebtors.Debtor = JiwaApplication.Manager.Instance.BusinessLogicFactory.CreateBusinessLogic(Of JiwaDebtors.Debtor)(Nothing)

........'skipped

            Logger.Write("salesOrder.InitiatedDate (before) : " & salesOrder.InitiatedDate, "general") 'v1.3.1

            salesOrder.OnCreateEnd()
            salesOrder.OnSaveStart()
            salesOrder.Save()
            Logger.Write("salesOrder.InitiatedDate (after) : " & salesOrder.InitiatedDate, "general") 'v1.3.1
            salesOrder.OnSaveEnd()


log output (note that the 2nd JiwaApplication.Manager.Instance.SysDateTime log is actually JiwaApplication.Manager.Instance.Database.ServerDateTime)
----------------------------------------
Timestamp: 1/11/2016 1:50:40 PM

Message: datebase date : 1/11/2016 1:50:36 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 1:50:40 PM

Message: system.date : 1/11/2016 1:50:40 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 1:50:40 PM

Message: JiwaApplication.Manager.Instance.SysDateTime : 1/11/2016 1:37:18 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 1:50:40 PM

Message: JiwaApplication.Manager.Instance.SysDateTime : 1/11/2016 1:50:36 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 1:50:41 PM

Message: salesOrder.InitiatedDate (before) : 1/11/2016 1:37:18 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 1:50:41 PM

Message: salesOrder.InitiatedDate (after) : 1/11/2016 1:37:18 PM

----------------------------------------


the very next order is not too far away
----------------------------------------
Timestamp: 1/11/2016 2:26:01 PM

Message: datebase date : 1/11/2016 2:25:57 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 2:26:01 PM

Message: system.date : 1/11/2016 2:26:01 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 2:26:01 PM

Message: JiwaApplication.Manager.Instance.SysDateTime : 1/11/2016 2:26:07 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 2:26:01 PM

Message: JiwaApplication.Manager.Instance.SysDateTime : 1/11/2016 2:25:57 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 2:26:02 PM

Message: salesOrder.InitiatedDate (before) : 1/11/2016 2:26:07 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 2:26:02 PM

Message: salesOrder.InitiatedDate (after) : 1/11/2016 2:26:07 PM

----------------------------------------


and the 3rd one went wrong again
----------------------------------------
Timestamp: 1/11/2016 2:31:18 PM

Message: datebase date : 1/11/2016 2:31:14 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 2:31:18 PM

Message: system.date : 1/11/2016 2:31:18 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 2:31:18 PM

Message: JiwaApplication.Manager.Instance.SysDateTime : 1/11/2016 2:26:09 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 2:31:18 PM

Message: JiwaApplication.Manager.Instance.SysDateTime : 1/11/2016 2:31:15 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 2:31:18 PM

Message: salesOrder.InitiatedDate (before) : 1/11/2016 2:26:09 PM

----------------------------------------
----------------------------------------
Timestamp: 1/11/2016 2:31:19 PM

Message: salesOrder.InitiatedDate (after) : 1/11/2016 2:26:09 PM


I can send you the log file, if you want. It looks to me that the very first order of a day is correct (and this get reset during the day), then the next order will just use the time from previous order...

**EDIT
I log off at the end of the function
Code: Select all
Finally
            If Not IsNothing(JiwaApplication.Manager.Instance) Then
                JiwaApplication.Manager.Instance.LogOff()
            End If
Perry Ma
S. Programmer
Lonicera Pty Ltd
http://www.lonicera.com.au
perry
Frequent Contributor
Frequent Contributor
 
Posts: 173
Joined: Mon Oct 27, 2008 2:26 pm
Topics Solved: 15

Re: Sales order logging incorrect dates  Topic is solved

Postby Mike.Sheen » Thu Jan 21, 2016 12:19 pm

After looking at those logs, JiwaApplication.Manager.Instance.SysDateTime is definitely out in your case, Perry.

Note: Your log is a little misleading, your message description for both SysDateTime and ServerDateTime is logged as SysDateTime. i.e.:
Code: Select all
Logger.Write("JiwaApplication.Manager.Instance.SysDateTime : " & JiwaApplication.Manager.Instance.SysDateTime, "general")
Logger.Write("JiwaApplication.Manager.Instance.SysDateTime : " & JiwaApplication.Manager.Instance.Database.ServerDateTime, "general")

Should be:
Code: Select all
Logger.Write("JiwaApplication.Manager.Instance.SysDateTime : " & JiwaApplication.Manager.Instance.SysDateTime, "general")
Logger.Write("JiwaApplication.Manager.Instance.ServerDateTime: " & JiwaApplication.Manager.Instance.Database.ServerDateTime, "general")


Having said that, I was still able to make sense of your logs because you did provide the logging code - so it all worked out well :)

When we log into Jiwa, we take the local system date time, the SQL server date time and store those values into variables _SysDateTime and _ServerDateTime - we also compute a difference in seconds between the local and sql date time and store that away into a variable localServerDifference.

Every second we increment the local and server datetime by one second using a System.Timers.Timer.

Every 60 seconds in an attempt to prevent drift with the serverdate, we query the sql server for the time again, store that into the _ServerDateTime variable and set _SysDateTime to be a dateadd of _ServerDateTime and the localServerDifference calculated at login - but we only add the time component, not the date component.

So, for your problem to occur, there are several possible causes:

1. The SQL Server date / time changes after the client has logged in.
Or
2. Our timer code is flawed. This is probably likely - I've looked at the timer code and whilst I can't see anything obvious it does look overly complicated for what it needs to do.

In the meantime you can in fact set the SysDateTime to be your desired value before creating your sales order. It will get overwritten again once the timer ticks over for the 60 second drift adjustment, but if you set it immediately before creating a sales order it should suffice.

The trick is how to set it. SysDateTime is a read-only property of JiwaApplication.Manager.Instance - but that property simply returns Database.SysDateTime which is writable - so you can set it by:
Code: Select all
JiwaApplication.Manager.Instance.Database.SysDateTime  = Now


EDIT: I think I have the cause - our timer is a System.Timers.Timer - and we shouldn't be using that timer if running as a service - System.Threading.Timer is what we should be using. We DID use to have that timer as System.Threading.Timer, but changed to System.Timers.Timer a while ago to try and solve another issue.

At least I think that's the cause - others seem to be of the opinion System.Timers.Timer is unreliable when running as a service, despite the guidance to use System.Timers.Timer.

I've logged this as bug 12410

EDIT #2: After reading BUG: The Elapsed event of the System.Timers.Timer class is not raised in a Windows service and Introduction to Server-Based Timers I'm now even more convinced we should be using System.Threading.Timer.

EDIT #3: I'm a little puzzled by this in your code:
Code: Select all
salesOrder.OnCreateEnd()
salesOrder.OnSaveStart()
salesOrder.Save()
Logger.Write("salesOrder.InitiatedDate (after) : " & salesOrder.InitiatedDate, "general") 'v1.3.1
salesOrder.OnSaveEnd()


You shouldn't be calling the OnCreateEnd, OnSaveStart or OnSaveEnd methods. They are methods invoked by the sales order methods CreatNew() and Save() - you have no need to invoke those - they simply raise the corresponding events and your code will result in multiple events fired which may lead to undesired results if your code or any other plugin is listening to those.

To illustrate, this is what happens when you call Save():

Code: Select all
Public Sub Save()
    OnSaveStart()
    ... some SQL queries and other stuff is performed
    OnSaveEnding()
    ... a database commit is performed
    OnSaveEnd()


OnSaveStart() simply does this:

Code: Select all
Public Sub OnSaveStart()
    RaiseEvent SaveStart()
End Sub
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: 2583
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 807

Next

Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 1 guest