PO Line field change loop  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

PO Line field change loop

Postby Riyaz » Wed Oct 10, 2018 10:18 pm

Hi There

Am converting one of the breakouts to plugin, what we did in the breakout is, depending on the value of quantity field in PO line, we change UDF1, 2 and 3 values , and vice versa, so if UDF3 got changed instead the quantity gets adjusted. This works fine in breakout, but it goes into a infinite loop in plugin, as once changes the other and so on. Can you pls advise, if this can be possible. Thanks
Riyaz
Kohai
Kohai
 
Posts: 233
Joined: Wed Dec 02, 2015 2:05 pm
Topics Solved: 2

Re: PO Line field change loop

Postby Riyaz » Fri Oct 12, 2018 11:14 am

Hello

Pls advise on this, thanks
Riyaz
Kohai
Kohai
 
Posts: 233
Joined: Wed Dec 02, 2015 2:05 pm
Topics Solved: 2

Re: PO Line field change loop

Postby SBarnes » Fri Oct 12, 2018 12:20 pm

Without see the code it is hard to comment but by the sounds of it if you have a attached to the property changed event, if this is correct you need to declare a flag at the plugin/class level and set this flag before you change anything and then set it back to false after you have updated everything and at the top of the property change event immediately return if the flag is set to true.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1620
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: PO Line field change loop

Postby Riyaz » Fri Oct 12, 2018 12:45 pm

Pls see below

Code: Select all
private void poLineChange(object sender, System.ComponentModel.PropertyChangedEventArgs e)
   {
JiwaFinancials.Jiwa.JiwaPurchaseOrders.Line  purchaseOrder= (JiwaFinancials.Jiwa.JiwaPurchaseOrders.Line)sender;
      
      var db = purchaseOrder.Manager.Database;
      System.Data.SqlClient.SqlParameter SQLParam = null;
      System.Data.SqlClient.SqlDataReader SQLReader = null;
      MessageBox.Show(e.PropertyName);                  
      StringBuilder sb = new StringBuilder();   
      //for(int i=1; i<= purchaseOrderobj.Lines.Count; i++)
      //{
         //JiwaFinancials.Jiwa.JiwaPurchaseOrders.Line  purchaseOrder= purchaseOrderobj.Lines[i];         
         string InventoryID = "";
         decimal Width = 0;
         decimal Quantity=0;
         decimal FXPrice=0;
         InventoryID = purchaseOrder.InventoryID;
         Width=purchaseOrder.UserDefinedFloat1;
         if(e.PropertyName == "Quantity")
         {   
            if(Width==0)
            {
               sb.Append("SELECT dbo.fnNumberIn(sz.SizeCode) as Width ");
               sb.Append("FROM   IN_Main im, IN_Sizes sz  ");
               sb.Append("WHERE   im.InventoryID =@InventoryID  ");
               sb.Append("AND   sz.RecID = im.SizeID");   
               try
               {
                  using (SqlCommand SQLCmd = new SqlCommand(sb.ToString(), db.SQLConnection, db.SQLTransaction))
                  {                             
                     SQLParam = new SqlParameter("@InventoryID", System.Data.SqlDbType.Char, 50);
                     SQLParam.Value = InventoryID;
                     SQLCmd.Parameters.Add(SQLParam);
                     SQLReader = db.ExecuteReader(SQLCmd);                  
                     if(SQLReader.HasRows==true){
                     while (SQLReader.Read())
                        {            
                           Width = Convert.ToDecimal(db.Sanitise(SQLReader, "Width").ToString());                           
                        }   
                     }
                     else{
                        Width = 0;
                     }
                     purchaseOrder.UserDefinedFloat1=Width;
                  }

                  SQLReader.Close();
               }
               catch(Exception ex)
               {                           
                  MessageBox.Show(ex.Message+ex.StackTrace);
               }
               finally
               {
                  if (SQLReader != null)
                  {
                    SQLReader.Close();
                  }
               }               
            }
            Quantity=purchaseOrder.Quantity;
            sb.Length = 0;      
            try
            {
               using ( SqlCommand SQLCmd = new SqlCommand("spsupplierprice", db.SQLConnection, db.SQLTransaction))
               {                             
                  SQLCmd.CommandType = System.Data.CommandType.StoredProcedure;
                  SQLCmd.Parameters.AddWithValue("@CreditorID",purchaseOrder.Lines.PurchaseOrder.Creditor.CreditorID);
                  SQLCmd.Parameters.AddWithValue("@InventoryID",InventoryID);
                  SQLCmd.Parameters.AddWithValue("@Quantity",Quantity);
                  SQLReader  = db.ExecuteReader(SQLCmd);                  
                  if(SQLReader.HasRows==true)
                  {
                     while (SQLReader.Read())
                     {            
                        FXPrice = Convert.ToDecimal(db.Sanitise(SQLReader, "Price").ToString());                           
                     }   
                  }
                  else
                  {
                     MessageBox.Show("No price found");
                  }
                  purchaseOrder.FXCost=FXPrice;
               }

               SQLReader.Close();
            }
            catch(Exception ex)
            {                           
               MessageBox.Show(ex.Message+ex.StackTrace);
            }
            finally
            {
               if (SQLReader != null)
               {
                 SQLReader.Close();
               }
            }
            if(Width > 0)
            {
               purchaseOrder.UserDefinedFloat2 = Quantity * Width / 100;
               purchaseOrder.UserDefinedFloat3 = FXPrice * 100 / Width;
            }
            else
            {
               purchaseOrder.UserDefinedFloat1 = 0;
               purchaseOrder.UserDefinedFloat2 = 0;
               purchaseOrder.UserDefinedFloat3 = 0;
            }            
         }
         else if(e.PropertyName == "UserDefinedFloat2")
         {            
            if(Width == 0 )
            {
               sb.Length = 0;
               sb.Append("SELECT dbo.fnNumberIn(sz.SizeCode) as Width ");
               sb.Append("FROM   IN_Main im, IN_Sizes sz  ");
               sb.Append("WHERE im.InventoryID =@InventoryID ");
               sb.Append("AND   sz.RecID = im.SizeID ");
               try
               {
                  using (SqlCommand SQLCmd = new SqlCommand(sb.ToString(), db.SQLConnection, db.SQLTransaction))
                  {                             
                     SQLParam = new SqlParameter("@InventoryID", System.Data.SqlDbType.Char, 50);
                     SQLParam.Value = InventoryID;
                     SQLCmd.Parameters.Add(SQLParam);
                     SQLReader = db.ExecuteReader(SQLCmd);                  
                     if(SQLReader.HasRows==true)
                     {
                        while (SQLReader.Read())
                        {            
                           Width = Convert.ToDecimal(db.Sanitise(SQLReader, "Width").ToString());                           
                        }   
                     }
                     else{
                        Width = 0;
                     }
                     purchaseOrder.UserDefinedFloat1=Width;
                  }

                  SQLReader.Close();
               }
               catch(Exception ex)
               {                           
                  MessageBox.Show(ex.Message+ex.StackTrace);
               }
               finally
               {
                  if (SQLReader != null)
                  {
                    SQLReader.Close();
                  }
               }
            }
            if( Width > 0)
            {
               Quantity = purchaseOrder.UserDefinedFloat2 * 100 / Width;
               purchaseOrder.Quantity = Quantity;
            }
            else
            {
               purchaseOrder.UserDefinedFloat1 = 0;
               purchaseOrder.UserDefinedFloat2 = 0;
               purchaseOrder.UserDefinedFloat3 = 0;
            }
            sb.Length = 0;
            sb.Append("spsupplierprice ");
            try
            {
               using (SqlCommand SQLCmd = new SqlCommand(sb.ToString(), db.SQLConnection, db.SQLTransaction))
               {                                               
                  SQLCmd.CommandType = System.Data.CommandType.StoredProcedure;
                  SQLCmd.Parameters.AddWithValue("@CreditorID",purchaseOrder.Lines.PurchaseOrder.Creditor.CreditorID);
                  SQLCmd.Parameters.AddWithValue("@InventoryID",InventoryID);
                  SQLCmd.Parameters.AddWithValue("@Quantity",Quantity);
                  SQLReader = db.ExecuteReader(SQLCmd);                  
                  if(SQLReader.HasRows==true){
                  while (SQLReader.Read())
                     {            
                        FXPrice = Convert.ToDecimal(db.Sanitise(SQLReader, "Price").ToString());                           
                     }   
                  }
                  else{
                     MessageBox.Show("No price found");
                  }
                  purchaseOrder.FXCost=FXPrice;
               }
            SQLReader.Close();
            }
            catch(Exception ex)
            {                           
               MessageBox.Show(ex.Message+ex.StackTrace);
            }
            finally
            {
               if (SQLReader != null)
               {
                 SQLReader.Close();
               }
            }
            if(Width > 0)
            {
               purchaseOrder.UserDefinedFloat3 = FXPrice * 100 / Width;
            }
            else
            {
               purchaseOrder.UserDefinedFloat1 = 0;
               purchaseOrder.UserDefinedFloat2 = 0;
               purchaseOrder.UserDefinedFloat3 = 0;
            }
         }
         else if(e.PropertyName == "UserDefinedFloat3")
         {   
            if(Width == 0 )
            {
               sb.Length = 0;
               sb.Append("SELECT dbo.fnNumberIn(sz.SizeCode) as Width  ");
               sb.Append("FROM   IN_Main im, IN_Sizes sz  ");
               sb.Append("WHERE im.InventoryID =@InventoryID ");
               sb.Append("AND   sz.RecID = im.SizeID ");
               try
               {
                  using (SqlCommand SQLCmd = new SqlCommand(sb.ToString(), db.SQLConnection, db.SQLTransaction))
                  {                             
                     SQLParam = new SqlParameter("@InventoryID", System.Data.SqlDbType.Char, 50);
                     SQLParam.Value = InventoryID;
                     SQLCmd.Parameters.Add(SQLParam);
                     SQLReader = db.ExecuteReader(SQLCmd);                  
                     if(SQLReader.HasRows==true)
                     {
                        while (SQLReader.Read())
                        {            
                           Width = Convert.ToDecimal(db.Sanitise(SQLReader, "Width").ToString());                           
                        }   
                     }
                     else{
                        Width = 0;
                     }
                     purchaseOrder.UserDefinedFloat1=Width;
                  }
                  SQLReader.Close();
               }
               catch(Exception ex)
               {                           
                  MessageBox.Show(ex.Message+ex.StackTrace);
               }
               finally
               {
                  if (SQLReader != null)
                  {
                    SQLReader.Close();
                  }
               }
            }
            if(Width > 0)
            {
               purchaseOrder.FXCost = purchaseOrder.UserDefinedFloat3 * Width / 100;
            }
            else
            {
               purchaseOrder.UserDefinedFloat1 = 0;
               purchaseOrder.UserDefinedFloat2 = 0;
               purchaseOrder.UserDefinedFloat3 = 0;
            }
         }
         else if(e.PropertyName == "FXCost")
         {   
            if(Width == 0 )
            {
               sb.Length = 0;
               sb.Append("SELECT dbo.fnNumberIn(sz.SizeCode) as Width  ");
               sb.Append("FROM   IN_Main im, IN_Sizes sz  ");
               sb.Append("WHERE im.InventoryID =@InventoryID ");
               sb.Append("AND   sz.RecID = im.SizeID ");
               try
               {
                  using (SqlCommand SQLCmd = new SqlCommand(sb.ToString(), db.SQLConnection, db.SQLTransaction))
                  {                             
                     SQLParam = new SqlParameter("@InventoryID", System.Data.SqlDbType.Char, 50);
                     SQLParam.Value = InventoryID;
                     SQLCmd.Parameters.Add(SQLParam);
                     SQLReader = db.ExecuteReader(SQLCmd);                  
                     if(SQLReader.HasRows==true)
                     {
                        while (SQLReader.Read())
                        {            
                           Width = Convert.ToDecimal(db.Sanitise(SQLReader, "Width").ToString());                           
                        }   
                     }
                     else{
                        Width = 0;
                     }
                     purchaseOrder.UserDefinedFloat1=Width;
                  }
               SQLReader.Close();
               }
               catch(Exception ex)
               {                           
                  MessageBox.Show(ex.Message+ex.StackTrace);
               }
               finally
               {
                  if (SQLReader != null)
                  {
                    SQLReader.Close();
                  }
               }
            }
            if(Width > 0)
            {
               purchaseOrder.UserDefinedFloat3 =purchaseOrder.FXCost * 100 / Width;
            }
            else
            {
               purchaseOrder.UserDefinedFloat1 = 0;
               purchaseOrder.UserDefinedFloat2 = 0;
               purchaseOrder.UserDefinedFloat3 = 0;
            }
         }
}
Riyaz
Kohai
Kohai
 
Posts: 233
Joined: Wed Dec 02, 2015 2:05 pm
Topics Solved: 2

Re: PO Line field change loop  Topic is solved

Postby perry » Fri Oct 19, 2018 10:00 am

remove event handler at the beginning and re-add it at the end should work for you

Code: Select all
Private Sub ShipmentLinesChanged(item As JiwaLandedCost.Shipment.Line, e As PropertyChangedEventArgs)

        Try
                  RemoveHandler item.Lines.Changed, AddressOf ShipmentLinesChanged
                  'your codes

        Catch ex As Exception

        Finally
           AddHandler item.Lines.Changed, AddressOf ShipmentLinesChanged
        End Try
End Sub
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


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 16 guests

cron