Page 1 of 1

PO Line field change loop

PostPosted: Wed Oct 10, 2018 10:18 pm
by Riyaz
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

Re: PO Line field change loop

PostPosted: Fri Oct 12, 2018 11:14 am
by Riyaz
Hello

Pls advise on this, thanks

Re: PO Line field change loop

PostPosted: Fri Oct 12, 2018 12:20 pm
by SBarnes
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.

Re: PO Line field change loop

PostPosted: Fri Oct 12, 2018 12:45 pm
by Riyaz
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;
            }
         }
}

Re: PO Line field change loop  Topic is solved

PostPosted: Fri Oct 19, 2018 10:00 am
by perry
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