Copy Freight Details to new Sales order snaphot  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

Copy Freight Details to new Sales order snaphot

Postby indikad » Tue Oct 10, 2023 10:04 pm

Jiwa 7.2.1

idea is to copy the Carier details and the Freight Items details from the previous snapshot when a new snapshot is created in sales order.
I am testing catching the salesOrder.CreatedNewSnapshot event in the Inventory - Back Order - Manual Back order Release
however my code does not quite work.
Hope someone can spot the problem or give me a tip.

code below
plugin is attached. ( Plugin att_test_catchSnapshot (COPY).xml)
Code: Select all
public void Setup(JiwaFinancials.Jiwa.JiwaApplication.IJiwaBusinessLogic JiwaBusinessLogic, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
    {
      if (JiwaBusinessLogic is JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder)
      {
         JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder salesOrder = (JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder)JiwaBusinessLogic;
         //salesOrder.BackorderProcessStart += SalesOrder_BackorderProcessStart;
         salesOrder.CreatedNewSnapshot += SalesOrder_CreatedNewSnapshot;   
      }      
    }
   
   private void SalesOrder_CreatedNewSnapshot(object sender, System.EventArgs e)
   {
      System.Diagnostics.Debugger.Launch();
      System.Diagnostics.Debugger.Break();
      JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder salesOrder =  (JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder)sender;
      JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrderHistory SalesOrderH = salesOrder.SalesOrderHistorys[salesOrder.CurrentHistoryNo];
      MessageBox.Show("SalesOrder_CreatedNewSnapshot bo - att_test_catchSnapshot");
      MySalesOrderClass oMySalesOrderClass = new MySalesOrderClass();
      oMySalesOrderClass.SetFreghtDetailsInItemTab(ref salesOrder , ref SalesOrderH );
            
   }



Code: Select all
public class MySalesOrderClass
{
   public void SetFreghtDetailsInItemTab(ref JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder salesOrder , ref JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrderHistory SalesOrderH)
   {
      if ( SalesOrderH.HistoryNo > 1 )
      {
         //-- then there is a previous snapshot
         JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrderHistory SalesOrderHPrevious = salesOrder.SalesOrderHistorys[SalesOrderH.HistoryNo - 1];         
         bool bCarierRecExists = false;
         //---Carier
         if ( ! SalesOrderH.Carrier.CarrierRecordExists )
         {            
            // this line and probably together with the line folloing it seems to break jiwa into delivering all and not creating a new snapshot???
//            salesOrder.Read(salesOrder.RecID);
//            SalesOrderH = salesOrder.SalesOrderHistorys[salesOrder.CurrentHistoryNo];            
            SalesOrderH.Carrier = SalesOrderHPrevious.Carrier;
            bCarierRecExists = true;
         }
         else
         {
            bCarierRecExists = true;   
         }   
         
         //--- Freight Items   this block belo errors so its commented
//         JiwaFinancials.Jiwa.JiwaSales.SalesOrder.FreightItemCollection oFreightItemCollectionPrevious = null;
//         JiwaFinancials.Jiwa.JiwaSales.SalesOrder.FreightItemCollection oFreightItemCollectionCurr = null;
//         oFreightItemCollectionPrevious = SalesOrderHPrevious.Carrier.FreightItemCollection; 
//         oFreightItemCollectionCurr = SalesOrderH.Carrier.FreightItemCollection; 
//         foreach( JiwaFinancials.Jiwa.JiwaSales.SalesOrder.FreightItem oPrevFrtItem  in oFreightItemCollectionPrevious)
//         {
//            if ( !(  oFreightItemCollectionCurr == null ))
//            {               
//               foreach( JiwaFinancials.Jiwa.JiwaSales.SalesOrder.FreightItem oCurrFrtItem  in oFreightItemCollectionCurr)
//               {
//                  oFreightItemCollectionCurr.Add(oPrevFrtItem);
//               }   
//            }               
//         }      
         
         SalesOrderH.Save();
         salesOrder.Save();
      }
      return;
   }
}
Attachments
Plugin att_test_catchSnapshot (COPY).xml
(33.52 KiB) Downloaded 166 times
indikad
Frequent Contributor
Frequent Contributor
 
Posts: 182
Joined: Thu Jun 18, 2009 1:14 pm
Topics Solved: 2

Re: Copy Freight Details to new Sales order snaphot  Topic is solved

Postby Mike.Sheen » Wed Oct 11, 2023 12:34 pm

You can't copy carrier fields from one snapshot to another the way you are:
Code: Select all
SalesOrderH.Carrier = SalesOrderHPrevious.Carrier;


This keeps the same RecID, insertflag and everything and so when saving it's just saving the old existing one.

What you need to do is set the fields, one by one. In the case of collections such as Freight Items you also need to iterate the collection from the source and create a new item, set the new items properties to be that of the source item and add the new item to the destination.

You need to do it the long way:

Code: Select all
private void SalesOrder_CreatedNewSnapshot(object sender, System.EventArgs e)
{
   JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder salesOrder = (JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder)sender;
   JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrderHistory previousHistory = salesOrder.SalesOrderHistorys[salesOrder.CurrentHistoryNo - 1];
   JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrderHistory currentHistory = salesOrder.SalesOrderHistorys[salesOrder.CurrentHistoryNo];
   
   currentHistory.CourierDetails = previousHistory.CourierDetails;
   currentHistory.Carrier.ReadRecord(previousHistory.Carrier.RecID);
   foreach(JiwaFinancials.Jiwa.JiwaSales.SalesOrder.FreightItem previousFreightItem in previousHistory.Carrier.FreightItemCollection)
   {
      JiwaFinancials.Jiwa.JiwaSales.SalesOrder.FreightItem newFreightItem = salesOrder.Manager.CollectionItemFactory.CreateCollectionItem<JiwaFinancials.Jiwa.JiwaSales.SalesOrder.FreightItem>();
      
      newFreightItem.FreightDescription = currentHistory.Carrier.FreightDescriptionCollection[previousFreightItem.FreightDescription.RecID];
      newFreightItem.NumberItems = previousFreightItem.NumberItems;         
      newFreightItem.ItemLength = previousFreightItem.ItemLength;
      newFreightItem.ItemHeight = previousFreightItem.ItemHeight;
      newFreightItem.ItemWidth = previousFreightItem.ItemWidth;
      newFreightItem.ItemWeight = previousFreightItem.ItemWeight;
      newFreightItem.ItemCubic = previousFreightItem.ItemCubic;
      
      currentHistory.Carrier.FreightItemCollection.Add(newFreightItem);
   }
}


You also don't need to be calling save at all - you're calling save on both the history and then the sales order:
Code: Select all
SalesOrderH.Save();
salesOrder.Save();


I'm surprised you're not getting concurrency conflict errors by doing that.

Plugin demonstrating how to do that you described is attached. I opted to create my own rather than edit yours.

You should also be aware of our Plugin Development Guidelines which is a work-in-progress guide on how to achieve a high level of quality with plugins.
Attachments
Plugin New Snapshot Copy Fields.xml
(5.96 KiB) Downloaded 151 times
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: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Copy Freight Details to new Sales order snaphot

Postby indikad » Mon Oct 16, 2023 11:23 am

Thanks much Mike. Tested this and it worked.

<< You should also be aware of our Plugin Development Guidelines which is a work-in-progress guide on how to achieve a high level of quality with plugins. >>

Thanks very much for this one too. Those guidelines really helps.
note that I did not consider too much of quality code at all when I uploaded the sample because I just wanted to provide the simplest code here in the forum..
indikad
Frequent Contributor
Frequent Contributor
 
Posts: 182
Joined: Thu Jun 18, 2009 1:14 pm
Topics Solved: 2


Return to Technical and or Programming

Who is online

Users browsing this forum: Bing [Bot] and 36 guests