protected virtual void ARSalesPrice_SalesPrice_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
        {
            ARSalesPrice row = e.Row as ARSalesPrice;

            if (row != null)
            {
                InventoryItem item = PXSelect <InventoryItem, Where <InventoryItem.inventoryID, Equal <Required <ARSalesPrice.inventoryID> > > > .Select(this, row.InventoryID);

                if (item != null)
                {
                    if (row.CuryID == Company.Current.BaseCuryID)
                    {
                        if (row.UOM == item.BaseUnit)
                        {
                            e.NewValue = item.BasePrice;
                        }
                        else
                        {
                            e.NewValue = INUnitAttribute.ConvertToBase(sender, item.InventoryID, row.UOM ?? item.SalesUnit, item.BasePrice.Value, INPrecision.UNITCOST);
                        }
                    }
                }
            }
            else
            {
                e.NewValue = 0m;
            }
        }
        protected virtual void ARSalesPrice_IsPromotionalPrice_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e)
        {
            ARSalesPrice row = (ARSalesPrice)e.Row;

            if (row.IsPromotionalPrice != true)
            {
                row.ExpirationDate = null;
            }
        }
        protected virtual void ARSalesPrice_RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
        {
            ARSalesPrice row = (ARSalesPrice)e.Row;

            if (row.IsPromotionalPrice == true && row.ExpirationDate == null)
            {
                sender.RaiseExceptionHandling <ARSalesPrice.expirationDate>(row, null, new PXSetPropertyException(ErrorMessages.FieldIsEmpty, typeof(ARSalesPrice.expirationDate).Name));
            }
            if (row.IsPromotionalPrice == true && row.ExpirationDate < row.EffectiveDate)
            {
                sender.RaiseExceptionHandling <ARSalesPrice.expirationDate>(row, row.ExpirationDate, new PXSetPropertyException(Messages.EffectiveDateExpirationDate, PXErrorLevel.RowError));
            }
            //if (row.CuryID == Company.Current.BaseCuryID && row.CustPriceClassID == AR.ARPriceClass.EmptyPriceClass && row.IsPromotionalPrice != true)
            //{
            //	InventoryItem item = PXSelect<InventoryItem, Where<InventoryItem.inventoryID, Equal<Required<ARSalesPrice.inventoryID>>>>.Select(this, row.InventoryID);

            //	if (item != null && sender.GetStatus(e.Row) == PXEntryStatus.Inserted || sender.GetStatus(e.Row) == PXEntryStatus.Updated)
            //	{
            //		if (SalesPriceUpdateUnit == SalesPriceUpdateUnitType.BaseUnit)
            //		{
            //			if (row.UOM == item.BaseUnit)
            //			{
            //				List<PXDataFieldParam> updatedFields = new List<PXDataFieldParam>();

            //				if (row.PendingPrice != null)
            //				{
            //					updatedFields.Add(new PXDataFieldAssign("PendingBasePrice", PXDbType.Decimal, row.PendingPrice ?? 0));
            //					updatedFields.Add(new PXDataFieldAssign("PendingBasePriceDate", PXDbType.DateTime, row.EffectiveDate));
            //					updatedFields.Add(new PXDataFieldAssign("LastModifiedDateTime", PXDbType.DateTime, DateTime.Now));
            //				}

            //				updatedFields.Add(new PXDataFieldRestrict("InventoryID", PXDbType.Int, item.InventoryID));
            //				PXDatabase.Update<InventoryItem>(updatedFields.ToArray());
            //			}
            //		}
            //		else
            //		{
            //			if (row.UOM == item.SalesUnit)
            //			{
            //				List<PXDataFieldParam> updatedFields = new List<PXDataFieldParam>();

            //				if (row.PendingPrice != null)
            //				{
            //					decimal pendingBasePrice = INUnitAttribute.ConvertFromBase(sender, item.InventoryID, row.UOM, row.PendingPrice ?? 0, INPrecision.UNITCOST);
            //					updatedFields.Add(new PXDataFieldAssign("PendingBasePrice", PXDbType.Decimal, pendingBasePrice));
            //					updatedFields.Add(new PXDataFieldAssign("PendingBasePriceDate", PXDbType.DateTime, row.EffectiveDate));
            //					updatedFields.Add(new PXDataFieldAssign("LastModifiedDateTime", PXDbType.DateTime, DateTime.Now));
            //				}

            //				updatedFields.Add(new PXDataFieldRestrict("InventoryID", PXDbType.Int, item.InventoryID));
            //				PXDatabase.Update<InventoryItem>(updatedFields.ToArray());
            //			}
            //		}
            //	}
            //}
        }
        protected virtual void ARSalesPrice_RowSelected(PXCache sender, PXRowSelectedEventArgs e)
        {
            ARSalesPrice row = (ARSalesPrice)e.Row;

            if (row != null)
            {
                PXUIFieldAttribute.SetEnabled <ARSalesPrice.priceType>(sender, row, Filter.Current.PriceType == PriceTypes.AllPrices);
                PXUIFieldAttribute.SetEnabled <ARSalesPrice.inventoryID>(sender, row, Filter.Current.InventoryID == null);
                PXUIFieldAttribute.SetEnabled <ARSalesPrice.expirationDate>(sender, row, row.IsPromotionalPrice == true);
            }
        }
        protected virtual void ARSalesPrice_PriceType_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
        {
            ARSalesPrice row = e.Row as ARSalesPrice;

            if (row != null)
            {
                if (Filter.Current != null && Filter.Current.PriceType != PriceTypes.AllPrices)
                {
                    e.NewValue = Filter.Current.PriceType;
                }
                else
                {
                    e.NewValue = PriceTypes.Customer;
                }
            }
        }
 public static void ValidateDuplicate(PXGraph graph, PXCache sender, ARSalesPrice price)
 {
     //PXSelectBase<ARSalesPrice> selectDuplicate = new PXSelect<ARSalesPrice, Where<ARSalesPrice.curyID, Equal<Required<ARSalesPrice.curyID>>, And<ARSalesPrice.custPriceClassID, Equal<Required<ARSalesPrice.custPriceClassID>>, And<ARSalesPrice.inventoryID, Equal<Required<ARSalesPrice.inventoryID>>, And<ARSalesPrice.uOM, Equal<Required<ARSalesPrice.uOM>>, And<ARSalesPrice.recordID, NotEqual<Required<ARSalesPrice.recordID>>, And<ARSalesPrice.breakQty, Equal<Required<ARSalesPrice.breakQty>>, And<ARSalesPrice.isPromotionalPrice, Equal<Required<ARSalesPrice.isPromotionalPrice>>>>>>>>>>(graph);
     //ARSalesPrice duplicate;
     //if (price.IsPromotionalPrice == true)
     //{
     //	//selectDuplicate.WhereAnd<Where2<Where<Required<ARSalesPrice.lastDate>, Between<ARSalesPrice.lastDate, ARSalesPrice.expirationDate>>, Or<Required<ARSalesPrice.expirationDate>, Between<ARSalesPrice.lastDate, ARSalesPrice.expirationDate>, Or<ARSalesPrice.lastDate, Between<Required<ARSalesPrice.lastDate>, Required<ARSalesPrice.expirationDate>>, Or<ARSalesPrice.expirationDate, Between<Required<ARSalesPrice.lastDate>, Required<ARSalesPrice.expirationDate>>>>>>>();
     //	//duplicate = selectDuplicate.SelectSingle(price.CuryID, price.CustPriceClassID, price.InventoryID, price.UOM, price.RecordID, price.PendingBreakQty, price.BreakQty, price.IsPromotionalPrice, price.LastDate, price.ExpirationDate, price.LastDate, price.ExpirationDate, price.LastDate, price.ExpirationDate);
     //}
     //else
     //{
     //	//duplicate = selectDuplicate.SelectSingle(price.CuryID, price.CustPriceClassID, price.InventoryID, price.UOM, price.RecordID, price.PendingBreakQty, price.BreakQty, price.IsPromotionalPrice);
     //}
     //if (duplicate != null)
     //{
     //	sender.RaiseExceptionHandling<ARSalesPrice.uOM>(price, price.UOM, new PXSetPropertyException(SO.Messages.DuplicateSalesPrice, PXErrorLevel.RowError));
     //}
 }
        public static SalesPriceItem FindSalesPrice(PXCache sender, string custPriceClass, int?customerID, int?inventoryID, string curyID, decimal?quantity, string UOM, DateTime date)
        {
            PXSelectBase <ARSalesPrice> salesPrice = new PXSelect <ARSalesPrice, Where <ARSalesPrice.inventoryID, Equal <Required <ARSalesPrice.inventoryID> >,
                                                                                        And2 <Where2 <Where <ARSalesPrice.priceType, Equal <Required <ARSalesPrice.priceType> >, And <ARSalesPrice.priceCode, Equal <Required <ARSalesPrice.priceCode> > > >,
                                                                                                      Or <Where <ARSalesPrice.priceType, Equal <Required <ARSalesPrice.priceType> >, And <ARSalesPrice.priceCode, Equal <Required <ARSalesPrice.priceCode> > > > > >,
                                                                                              And <ARSalesPrice.curyID, Equal <Required <ARSalesPrice.curyID> >,
                                                                                                   And <ARSalesPrice.uOM, Equal <Required <ARSalesPrice.uOM> >,

                                                                                                        And <Where2 <Where <ARSalesPrice.breakQty, LessEqual <Required <ARSalesPrice.breakQty> > >,
                                                                                                                     And <Where2 <Where <ARSalesPrice.effectiveDate, LessEqual <Required <ARSalesPrice.effectiveDate> >,
                                                                                                                                         And <ARSalesPrice.expirationDate, Greater <Required <ARSalesPrice.expirationDate> > > >,
                                                                                                                                  Or2 <Where <ARSalesPrice.effectiveDate, LessEqual <Required <ARSalesPrice.effectiveDate> >,
                                                                                                                                              And <ARSalesPrice.expirationDate, IsNull> >,
                                                                                                                                       Or <Where <ARSalesPrice.expirationDate, Greater <Required <ARSalesPrice.expirationDate> >,
                                                                                                                                                  And <ARSalesPrice.effectiveDate, IsNull> > > > > > > > > > > >,

                                                                   OrderBy <Asc <ARSalesPrice.priceType, Desc <ARSalesPrice.isPromotionalPrice, Desc <ARSalesPrice.breakQty> > > > >(sender.Graph);

            PXSelectBase <ARSalesPrice> selectWithBaseUOM = new PXSelectJoin <ARSalesPrice, InnerJoin <InventoryItem,
                                                                                                       On <InventoryItem.inventoryID, Equal <ARSalesPrice.inventoryID>,
                                                                                                           And <InventoryItem.baseUnit, Equal <ARSalesPrice.uOM> > > >, Where <ARSalesPrice.inventoryID, Equal <Required <ARSalesPrice.inventoryID> >,
                                                                                                                                                                               And2 <Where2 <Where <ARSalesPrice.priceType, Equal <Required <ARSalesPrice.priceType> >, And <ARSalesPrice.priceCode, Equal <Required <ARSalesPrice.priceCode> > > >,
                                                                                                                                                                                             Or <Where <ARSalesPrice.priceType, Equal <Required <ARSalesPrice.priceType> >, And <ARSalesPrice.priceCode, Equal <Required <ARSalesPrice.priceCode> > > > > >,
                                                                                                                                                                                     And <ARSalesPrice.curyID, Equal <Required <ARSalesPrice.curyID> >,
                                                                                                                                                                                          And <ARSalesPrice.uOM, Equal <Required <ARSalesPrice.uOM> >,

                                                                                                                                                                                               And <Where2 <Where <ARSalesPrice.breakQty, LessEqual <Required <ARSalesPrice.breakQty> > >,
                                                                                                                                                                                                            And <Where2 <Where <ARSalesPrice.effectiveDate, LessEqual <Required <ARSalesPrice.effectiveDate> >,
                                                                                                                                                                                                                                And <ARSalesPrice.expirationDate, Greater <Required <ARSalesPrice.expirationDate> > > >,
                                                                                                                                                                                                                         Or2 <Where <ARSalesPrice.effectiveDate, LessEqual <Required <ARSalesPrice.effectiveDate> >,
                                                                                                                                                                                                                                     And <ARSalesPrice.expirationDate, IsNull> >,
                                                                                                                                                                                                                              Or <Where <ARSalesPrice.expirationDate, Greater <Required <ARSalesPrice.expirationDate> >,
                                                                                                                                                                                                                                         And <ARSalesPrice.effectiveDate, IsNull> > > > > > > > > > > >,

                                                                              OrderBy <Desc <ARSalesPrice.customerID, Desc <ARSalesPrice.isPromotionalPrice, Desc <ARSalesPrice.breakQty> > > > >(sender.Graph);

            ARSalesPrice item = salesPrice.SelectWindowed(0, 1, inventoryID, PriceTypes.Customer, PriceCodeInfo.CustomerPrefix + customerID, PriceTypes.CustomerPriceClass, PriceCodeInfo.CustomerPriceClassPrefix + custPriceClass, curyID, UOM, quantity, date, date, date, date);

            string uomFound = null;

            if (item == null)
            {
                decimal baseUnitQty = INUnitAttribute.ConvertToBase(sender, inventoryID, UOM, (decimal)quantity, INPrecision.QUANTITY);
                item = selectWithBaseUOM.Select(inventoryID, PriceTypes.Customer, PriceCodeInfo.CustomerPrefix + customerID, PriceTypes.CustomerPriceClass, PriceCodeInfo.CustomerPriceClassPrefix + custPriceClass, curyID, UOM, quantity, date, date, date, date);

                if (item == null)
                {
                    item = salesPrice.SelectWindowed(0, 1, inventoryID, PriceTypes.Customer, PriceCodeInfo.CustomerPrefix + customerID, PriceTypes.CustomerPriceClass, PriceCodeInfo.CustomerPriceClassPrefix + AR.ARPriceClass.EmptyPriceClass, curyID, UOM, quantity, date, date, date, quantity, date);
                    if (item == null)
                    {
                        item = selectWithBaseUOM.Select(inventoryID, PriceTypes.Customer, PriceCodeInfo.CustomerPrefix + customerID, PriceTypes.CustomerPriceClass, PriceCodeInfo.CustomerPriceClassPrefix + AR.ARPriceClass.EmptyPriceClass, curyID, UOM, quantity, date, date, date, quantity, date);

                        if (item == null)
                        {
                            return(null);
                        }
                        else
                        {
                            uomFound = item.UOM;
                        }
                    }
                    else
                    {
                        uomFound = UOM;
                    }
                }
                else
                {
                    uomFound = item.UOM;
                }
            }
            else
            {
                uomFound = UOM;
            }
            return(new SalesPriceItem(uomFound, (item.SalesPrice ?? 0), item.CuryID));
        }
        protected virtual void ARSalesPrice_SalesPrice_FieldVerifying(PXCache sender, PXFieldVerifyingEventArgs e)
        {
            ARSalesPrice row = e.Row as ARSalesPrice;

            if (row != null && MinGrossProfitValidation != MinGrossProfitValidationType.None && row.EffectiveDate != null)
            {
                var r = (PXResult <InventoryItem, INItemCost>)
                        PXSelectJoin <InventoryItem,
                                      LeftJoin <INItemCost, On <INItemCost.inventoryID, Equal <InventoryItem.inventoryID> > >,
                                      Where <InventoryItem.inventoryID, Equal <Required <InventoryItem.inventoryID> > > > .SelectWindowed(this, 0, 1, row.InventoryID);

                InventoryItem item = r;
                INItemCost    cost = r;
                if (item != null)
                {
                    decimal newValue = (decimal)e.NewValue;
                    if (row.UOM != item.BaseUnit)
                    {
                        try
                        {
                            newValue = INUnitAttribute.ConvertFromBase(sender, item.InventoryID, row.UOM, newValue, INPrecision.UNITCOST);
                        }
                        catch (PXUnitConversionException)
                        {
                            sender.RaiseExceptionHandling <ARSalesPrice.salesPrice>(row, e.NewValue, new PXSetPropertyException(SO.Messages.FailedToConvertToBaseUnits, PXErrorLevel.Warning));
                            return;
                        }
                    }

                    decimal minPrice = PXPriceCostAttribute.MinPrice(item, cost);
                    if (row.CuryID != Company.Current.BaseCuryID)
                    {
                        ARSetup arsetup = PXSetup <ARSetup> .Select(this);

                        if (string.IsNullOrEmpty(arsetup.DefaultRateTypeID))
                        {
                            throw new PXException(SO.Messages.DefaultRateNotSetup);
                        }

                        minPrice = ConvertAmt(Company.Current.BaseCuryID, row.CuryID, arsetup.DefaultRateTypeID, row.EffectiveDate.Value, minPrice);
                    }


                    if (newValue < minPrice)
                    {
                        switch (MinGrossProfitValidation)
                        {
                        case MinGrossProfitValidationType.Warning:
                            sender.RaiseExceptionHandling <ARSalesPrice.salesPrice>(row, e.NewValue, new PXSetPropertyException(SO.Messages.GrossProfitValidationFailed, PXErrorLevel.Warning));
                            break;

                        case MinGrossProfitValidationType.SetToMin:
                            e.NewValue = minPrice;
                            sender.RaiseExceptionHandling <ARSalesPrice.salesPrice>(row, e.NewValue, new PXSetPropertyException(SO.Messages.GrossProfitValidationFailedAndPriceFixed, PXErrorLevel.Warning));
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
        }
 public static void UpdateSalesPrice(ARUpdateSalesPriceProcess graph, ARSalesPrice item)
 {
     graph.UpdateSalesPrice(item);
 }
        public virtual void UpdateSalesPrice(ARSalesPrice item)
        {
            using (PXConnectionScope cs = new PXConnectionScope())
            {
                using (PXTransactionScope ts = new PXTransactionScope())
                {
                    DateTime updateTime = DateTime.Now;

                    //FOR UI:
                    item.LastPrice       = item.SalesPrice;
                    item.SalesPrice      = item.PendingPrice;
                    item.LastBreakQty    = item.BreakQty;
                    item.BreakQty        = item.PendingBreakQty;
                    item.LastDate        = item.EffectiveDate;
                    item.EffectiveDate   = null;
                    item.PendingPrice    = 0;
                    item.PendingBreakQty = 0;
                    item.LastTaxID       = item.TaxID;
                    item.TaxID           = item.PendingTaxID;
                    item.PendingTaxID    = null;


                    PXDatabase.Update <ARSalesPrice>(
                        new PXDataFieldAssign("LastPrice", PXDbType.Decimal, item.LastPrice),
                        new PXDataFieldAssign("LastTaxID", PXDbType.NVarChar, item.LastTaxID),
                        new PXDataFieldAssign("LastBreakQty", PXDbType.Decimal, item.LastBreakQty),
                        new PXDataFieldAssign("SalesPrice", PXDbType.Decimal, item.SalesPrice),
                        new PXDataFieldAssign("TaxID", PXDbType.NVarChar, item.TaxID),
                        new PXDataFieldAssign("BreakQty", PXDbType.Decimal, item.BreakQty),
                        new PXDataFieldAssign("LastDate", PXDbType.DateTime, item.LastDate),
                        new PXDataFieldAssign("PendingPrice", PXDbType.Decimal, 0m),
                        new PXDataFieldAssign("PendingTaxID", PXDbType.Decimal, null),
                        new PXDataFieldAssign("EffectiveDate", PXDbType.DateTime, null),
                        new PXDataFieldAssign("PendingBreakQty", PXDbType.Decimal, 0m),
                        new PXDataFieldAssign("LastModifiedDateTime", PXDbType.DateTime, updateTime),

                        new PXDataFieldRestrict("RecordID", PXDbType.Int, item.RecordID),
                        PXDataFieldRestrict.OperationSwitchAllowed
                        );

                    PXDatabase.Update <ARSalesPrice>(
                        new PXDataFieldAssign("SalesPrice", PXDbType.Decimal, item.SalesPrice),
                        new PXDataFieldAssign("LastDate", PXDbType.DateTime, item.LastDate),
                        new PXDataFieldAssign("LastModifiedDateTime", PXDbType.DateTime, updateTime),
                        new PXDataFieldRestrict("InventoryID", PXDbType.Int, item.InventoryID),
                        item.IsCustClassPrice == true
                                                                                ? new PXDataFieldRestrict("CustPriceClassID", PXDbType.NVarChar, item.CustPriceClassID)
                                                                                : new PXDataFieldRestrict("CustomerID", PXDbType.Int, item.CustomerID),
                        new PXDataFieldRestrict("CuryID", PXDbType.NVarChar, item.CuryID),
                        new PXDataFieldRestrict("UOM", PXDbType.NVarChar, item.UOM),
                        new PXDataFieldRestrict("IsPromotionalPrice", PXDbType.Bit, false),
                        new PXDataFieldRestrict("BreakQty", PXDbType.Decimal, item.BreakQty),
                        new PXDataFieldRestrict("RecordID", PXDbType.Int, 4, item.RecordID, PXComp.NE)
                        );

                    InventoryItem ii = PXSelect <InventoryItem, Where <InventoryItem.inventoryID, Equal <Required <InventoryItem.inventoryID> > > > .Select(this, item.InventoryID);

                    string uom = PXAccess.FeatureInstalled <FeaturesSet.distributionModule>() && sosetup.Current.SalesPriceUpdateUnit == SalesPriceUpdateUnitType.SalesUnit ? ii.SalesUnit : ii.BaseUnit;
                    if (item.UOM == uom && item.CuryID == Company.Current.BaseCuryID && item.CustPriceClassID == AR.ARPriceClass.EmptyPriceClass && item.BreakQty == 0 && item.PendingBreakQty == 0 && item.IsPromotionalPrice == false)
                    {
                        decimal price     = PXAccess.FeatureInstalled <FeaturesSet.distributionModule>() && sosetup.Current.SalesPriceUpdateUnit == SalesPriceUpdateUnitType.SalesUnit ? INUnitAttribute.ConvertFromBase(Inventory.Cache, ii.InventoryID, ii.SalesUnit, item.SalesPrice ?? 0m, INPrecision.UNITCOST) : item.SalesPrice ?? 0m;
                        decimal lastPrice = PXAccess.FeatureInstalled <FeaturesSet.distributionModule>() && sosetup.Current.SalesPriceUpdateUnit == SalesPriceUpdateUnitType.SalesUnit ? INUnitAttribute.ConvertFromBase(Inventory.Cache, ii.InventoryID, ii.SalesUnit, item.LastPrice ?? 0m, INPrecision.UNITCOST) : item.LastPrice ?? 0m;
                        PXDatabase.Update <InventoryItem>(
                            new PXDataFieldAssign("BasePrice", PXDbType.Decimal, price),
                            new PXDataFieldAssign("BasePriceDate", PXDbType.DateTime, item.LastDate),
                            new PXDataFieldAssign("PendingBasePrice", PXDbType.Decimal, 0m),
                            new PXDataFieldAssign("PendingBasePriceDate", PXDbType.DateTime, null),
                            new PXDataFieldAssign("LastBasePrice", PXDbType.Decimal, lastPrice),
                            new PXDataFieldAssign("LastModifiedDateTime", PXDbType.DateTime, updateTime),
                            new PXDataFieldRestrict("InventoryID", PXDbType.Int, item.InventoryID)
                            );
                    }


                    ts.Complete();
                }
            }
        }