Recalcing Sell Price when GRN or PI activated  Topic is solved

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

Recalcing Sell Price when GRN or PI activated

Postby DannyC » Fri Jun 11, 2021 2:27 pm

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)?
User avatar
DannyC
Senpai
Senpai
 
Posts: 718
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 31

Re: Recalcing Sell Price when GRN or PI activated

Postby SBarnes » Fri Jun 11, 2021 5:40 pm

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.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1696
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 191

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

Postby nsbandara » Tue Jun 15, 2021 1:37 am

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
User avatar
nsbandara
Occasional Contributor
Occasional Contributor
 
Posts: 43
Joined: Tue Jul 16, 2013 5:02 pm
Location: Sri Lanka
Topics Solved: 11


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 4 guests