SalesOrder.Save() exception  Topic is solved

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

Re: SalesOrder.Save() exception  Topic is solved

Postby SBarnes » Thu Jul 30, 2020 2:44 pm

The usual way for reading data looks like the routine below, the important this to remember is to always close the reader


Code: Select all
   public int ValidCount(JiwaFinancials.Jiwa.JiwaApplication.Manager Manager,  string Cat1, string Cat2, string Cat3)
   {
       //System.Diagnostics.Debugger.Launch();
        string Sql = null;
        SqlDataReader SQLReader = null;

        SqlParameter SQLParam = null;
        int result = 0;
        try
        {



            var db = Manager.Database;

            Sql = @" SELECT coalesce(count(*), 0) as ValidCount
                FROM vAttk_ValidCategoryCombos
                where Category1 =  @Category1 and Category2 =  @Category2 and Category3 = @Category3   ";

         
         

            using (SqlCommand SQLCmd = new SqlCommand(Sql, db.SQLConnection, db.SQLTransaction))
            {
                SQLCmd.CommandTimeout = Manager.Database.DefaultCommandTimeout;
                SQLParam = new SqlParameter("@Category1", System.Data.SqlDbType.Char);
                SQLParam.Value = Cat1;
                SQLCmd.Parameters.Add(SQLParam);
            
            
               SQLParam = new SqlParameter("@Category2", System.Data.SqlDbType.Char);
                SQLParam.Value = Cat2;
                SQLCmd.Parameters.Add(SQLParam);         
            

               SQLParam = new SqlParameter("@Category3", System.Data.SqlDbType.Char);
                SQLParam.Value = Cat3;
                SQLCmd.Parameters.Add(SQLParam);            
            
                SQLReader = SQLCmd.ExecuteReader();

                while (SQLReader.Read() == true)
                {
               
               result = int.Parse( db.Sanitise(SQLReader, "ValidCount").ToString());
 

                }

                SQLReader.Close();
            }


        }
      catch(Exception ex )
      {
         System.Windows.Forms.MessageBox.Show(ex.Message);
         result = 0;
      }
        finally
        {


            if ((SQLReader != null))
            {
                SQLReader.Close();
            }
        }
      return result;
   }


Alternately something in ORMLite which is the ORM available through ServiceStack which Jiwa uses for the Rest API would be something like this, you just need to make sure the references for the ServiceStack DLLs are included in assembly references, the classes for all the tables and views are defined in JiwaFinancials.Jiwa.JiwaServiceModel, an example is below


Code: Select all
   private List<JiwaFinancials.Jiwa.JiwaServiceModel.Tables.IN_WarehouseSOH> GetWareHouseSOH(JiwaFinancials.Jiwa.JiwaApplication.Manager manager,  string InventoryID)
   {
        var dbFactory = new OrmLiteConnectionFactory(manager.Database.ConnectionString, ServiceStack.OrmLite.SqlServer2012Dialect.Provider);
         using (var db = dbFactory.Open())
         {
             return db.Select<JiwaFinancials.Jiwa.JiwaServiceModel.Tables.IN_WarehouseSOH>(wsoh => wsoh.InventoryID == InventoryID && wsoh.QuantityLeft > 0);
        }
   }
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1696
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 191

Re: SalesOrder.Save() exception

Postby Mike.Sheen » Thu Jul 30, 2020 4:18 pm

My plugin is pretty simple, but may not provide you what you are looking for


Code: Select all
public class PluginHarnessForm : AERP.WooCommerce.ConfigurationForm


Yes... without seeing that class AERP.WooCommerce.ConfigurationForm you're inheriting from I can't help much.
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

Re: SalesOrder.Save() exception

Postby Mark Shipman » Thu Jul 30, 2020 4:57 pm

Mike.Sheen wrote:We can best help you from here if you could post a plugin which demonstrates the issue.

Hi,

Am I able to do this or is it outside the capability of the plugin editor:
Code: Select all
        public static List<WooCommerceIdMap> FindJiwaMatches(this List<WooCommerceIdMap> idMaps, string jiwaEntity)
        {
            return (from WooCommerceIdMap map in idMaps
                    where map.JiwaEntity.Equals(jiwaEntity)
                    select map).ToList();
        }


Thanks
Mark Shipman
Software Developer
Mark Shipman
Occasional Contributor
Occasional Contributor
 
Posts: 21
Joined: Mon Mar 02, 2020 1:44 pm
Location: Auckland, New Zealand

Re: SalesOrder.Save() exception

Postby SBarnes » Thu Jul 30, 2020 5:20 pm

Yes the editor will support linq and extension methods, obviously you just need to define a static class for the extension method.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1696
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 191

Re: SalesOrder.Save() exception

Postby Mike.Sheen » Thu Jul 30, 2020 6:13 pm

SBarnes wrote:Yes the editor will support linq and extension methods, obviously you just need to define a static class for the extension method.


And make sure you import the Linq namespace with a Using.
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

Re: SalesOrder.Save() exception

Postby Mark Shipman » Fri Jul 31, 2020 3:57 pm

Hi,

I have attached a plugin XML export. What else will you need?

The code has been trimmed to only the import sales order.

We were using a nuget package for the WooCommerce integration, but decompiling the DLL showed it is working asynchronously, so this reference has been removed and the consequantial errors corrected.

The result is the "web store manager" class now deserialises a json string to create a list of one sales order with 3 lines.

The "New transaction..." exception is still present (even without the nuget package).

Logging has also been removed from the database and sent to the users roaming data directory in AERP\Logs.

When you run the plugin, there should be a YYYYMMDD.txt file created containing the log entries and a json.txt file that contains the order list as json. If the json.txt file is not present there may be a configuration issue.

The configuration tab allows you to set things up. The URL and secret/key do point to a test web-store, but it is not actually used.

The only mandatory Jiwa piece for the import is a cash-sales debtor. If logical warehouse or staff is not set, the Jiwa Manager settings are used.

Please ask if there is anything else I might provide.
Attachments
Plugin Mark Shipman Test C# plugin20200731.xml
(1.37 MiB) Downloaded 1065 times
Mark Shipman
Software Developer
Mark Shipman
Occasional Contributor
Occasional Contributor
 
Posts: 21
Joined: Mon Mar 02, 2020 1:44 pm
Location: Auckland, New Zealand

Re: SalesOrder.Save() exception

Postby Mark Shipman » Mon Aug 03, 2020 4:40 pm

The goal today was to strip all database-related code out of the plugin to prove this was the source of the exception then to add back in small pieces of code in a controlled manner.

This involved removing the Jiwa system settings calls and, instead, populating using the literal values copied from the SY_SysValues database table. Any methods involving SqlClient.SqlDataReader were also either removed or replaced with json string conversions.

The sales order was successfully added to the database in this stripped down environment.

The system settings code was returned and the exception remained in abeyance.

The method to read all of the mappings between the Jiwa DB entities and the WooCommerce entities was reintroduced. This involves constructing a SqlDataReader from a command using the Jiwa database object to provide the SQL Connection and SQL Transaction:
Code: Select all
        private System.Data.SqlClient.SqlDataReader WooCommerceIdMapReader()
        {
            // The T-SQL here is EXECUTE [dbo].[usp_WooCommerceSelectAllMaps];
            using (System.Data.SqlClient.SqlCommand allWooCommerceIdMapCommand =
                new System.Data.SqlClient.SqlCommand(GetAllWooCommerceIdMapsSql(), JiwaDatabase.SQLConnection, JiwaDatabase.SQLTransaction))
            {
                System.Data.SqlClient.SqlDataReader rdr = JiwaDatabase.ExecuteReader(allWooCommerceIdMapCommand);

                return rdr;
            }
        }


The exception is still in abeyance.

The next step is the read of SO_Main to check an order does not already exist with the same reference (SOReference). The exception now occurs when the SalesOrder.Save() is invoked.

The attached XML export of the plugin includes these changes and the exception is present during testing.

How do I check for an existing reference AND save the sales order without causing an exception?
Attachments
Plugin Mark Shipman Test C# plugin20200803.xml
(1.7 MiB) Downloaded 1034 times
Mark Shipman
Software Developer
Mark Shipman
Occasional Contributor
Occasional Contributor
 
Posts: 21
Joined: Mon Mar 02, 2020 1:44 pm
Location: Auckland, New Zealand

Re: SalesOrder.Save() exception

Postby Mike.Sheen » Mon Aug 03, 2020 5:05 pm

At a glance the first thing which I found unfamiliar to me was how the method FirstSalesOrderUsingReferenceReader returns a SQL data reader inside a using statement for the SQLCommand. I'd change that method to close the reader before exiting.

Having imported your plugin... what do I do now to reproduce the error?

Edit: Added bold bit to clarify that the using was not for the DataReader, but the SQLCommand
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

Re: SalesOrder.Save() exception

Postby Scott.Pearce » Mon Aug 03, 2020 5:26 pm

Mike.Sheen wrote:the method FirstSalesOrderUsingReferenceReader returns a SQL data reader inside a using statement.?


I'm putting my money on this being the problem.
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: SalesOrder.Save() exception

Postby pricerc » Mon Aug 03, 2020 5:57 pm

Mike.Sheen wrote:At a glance the first thing which I found unfamiliar to me was how the method FirstSalesOrderUsingReferenceReader returns a SQL data reader inside a using statement. I'd change that method to close the reader before exiting.


If the data reader is instantiated *by* a 'using' block, then closing the statement block will close the DataReader. By definition. Some refactoring tools can actually recommend losing the .Close().
/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

PreviousNext

Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 5 guests