Page 1 of 1

Recalcing Sell Price when GRN or PI activated

PostPosted: Fri Jun 11, 2021 2:27 pm
by DannyC
When inventory last cost is updated via a GRN activation or a Purchase Invoice activation I want to recalculate the Sell Price based on the Minimum GP.

I thought I would just need to use the inventory business logic on the PropertyChanged event but it doesn't get fired at all upon GRN or PI activation.
It only fires when I manually edit the Last Cost in the GUI.

Is there another event I can use on the inventory business logic?

Or some other way to update IN_Main.DefaultPrice? (Aside from doing a foreach loop on the GRN lines or PI lines)?

Re: Recalcing Sell Price when GRN or PI activated

PostPosted: Fri Jun 11, 2021 5:40 pm
by SBarnes
Both line properties of the business objects should be Jiwa Collections, try attaching to the save ending and rollover the collection and look at the insert and update flags of the line and then use an inventory object to do what you want, if by save ending the flags have been reset (this I haven't checked) then in the save start put all the inventory ids that have changed in a string list and put it in the generic object collection and use it to get the ids in the save ending.

Re: Recalcing Sell Price when GRN or PI activated  Topic is solved

PostPosted: Tue Jun 15, 2021 1:37 am
by nsbandara
Simplest way to do this update would be subscribing to GRN save ending event and run SQL command to update IN_Main.DefaultPrice based on GP% and last cost for all inventory items in GRN, if GRN being activated.

Please see code example below. ( ** not tested)

Code: Select all
#region "BusinessLogicPlugin"
using System;
using System.Data.SqlClient;
using System.Linq;

public class BusinessLogicPlugin : System.MarshalByRefObject, JiwaFinancials.Jiwa.JiwaApplication.IJiwaBusinessLogicPlugin
{

    public override object InitializeLifetimeService()
    {
        // returning null here will prevent the lease manager
        // from deleting the Object.
        return null;
    }

    public void Setup(JiwaFinancials.Jiwa.JiwaApplication.IJiwaBusinessLogic JiwaBusinessLogic, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
    {
        if (JiwaBusinessLogic is JiwaFinancials.Jiwa.JiwaInvReceival.Receival)
        {
            JiwaFinancials.Jiwa.JiwaInvReceival.Receival invReceival = JiwaBusinessLogic as JiwaFinancials.Jiwa.JiwaInvReceival.Receival;

            invReceival.SaveEnding += delegate (object sender, EventArgs e)
            {
                if (invReceival.OriginalStatus == JiwaFinancials.Jiwa.JiwaInvReceival.Receival.Statuses.UnActivated
                    && invReceival.Status == JiwaFinancials.Jiwa.JiwaInvReceival.Receival.Statuses.Activated) //GRN being activated
                {
                    if (invReceival.SystemSettings.UpdateLastCost == true) //if last cost being updated
                    {
                        string[] inventoryIDs = invReceival.Lines.Cast<JiwaFinancials.Jiwa.JiwaInvReceival.Line>()
                                                                .Where(l => l.LineType == JiwaFinancials.Jiwa.JiwaInvReceival.Line.ReceivalLineType.Inventory)
                                                                .Select(l => l.InventoryID)
                                                                .ToArray();

                        if (inventoryIDs != null && inventoryIDs.Any())
                        {
                            string updatePriceSQLCommand = @"UPDATE
                                                               IN_Main
                                                            SET
                                                               DefaultPrice = LCost / (( 100.0 - 25.0 /*gp % = 25*/) / 100.0)
                                                            WHERE   
                                                               InventoryID IN ({0})";
                            updatePriceSQLCommand = string.Format(updatePriceSQLCommand, string.Join(",", inventoryIDs.Select(invId => string.Format("'{0}'", invId))));

                            var database = invReceival.Manager.Database;

                            using (SqlCommand sqlCommand = new SqlCommand(updatePriceSQLCommand, database.SQLConnection, database.SQLTransaction))
                            {
                                sqlCommand.ExecuteNonQuery();
                            }
                        }
                    }
                }
            };
        }
    }
}
#endregion