public ProductSaleQueryResponse GetProductPriceThresholdByAccountId(int productId, int?accountId = null)
        {
            if (!accountId.HasValue || accountId == 0)
            {
                return(GetProductSalePriceById(productId));
            }

            var product = GetProductMasterWithSpecialPrice(productId, accountId ?? 0);

            var minSellPrice = GetPercentageMarginPrice(product.LandedCost, product.PercentMargin);

            var minThresholdPrice = product.MinThresholdPrice ?? (minSellPrice <= 0 ? product.SellPrice : minSellPrice) ?? 0;

            var account = _accountServices.GetAccountsById(accountId.Value);

            var minSellPriceForAccount = GetProductPriceByAccountId(productId, accountId);

            var finalThresholdPrice = new[] { minThresholdPrice, minSellPriceForAccount, minSellPrice }.Min();

            var thresholdInfo = new ProductSaleQueryResponse()
            {
                MinimumThresholdPrice = ConvertBaseRates(accountId ?? 0, finalThresholdPrice), SellPrice = ConvertBaseRates(accountId ?? 0, product.SellPrice ?? 0), LandingCost = ConvertBaseRates(accountId ?? 0, product.LandedCost ?? 0), LandingCostWithMargin = ConvertBaseRates(accountId ?? 0, minSellPrice), PriceGroupID = account.PriceGroupID, PriceGroupPercent = account.TenantPriceGroups.Percent, ProfitMargin = product.PercentMargin
            };

            return(LoadThresholdInfo(thresholdInfo, product));
        }
        public ProductSaleQueryResponse GetProductSalePriceById(int productId)
        {
            var product       = GetProductMasterWithSpecialPrice(productId, 0);
            var thresholdInfo = new ProductSaleQueryResponse()
            {
                MinimumThresholdPrice = product.SellPrice ?? 0, SellPrice = product.SellPrice ?? 0
            };

            return(LoadThresholdInfo(thresholdInfo, product));
        }
        private ProductSaleQueryResponse LoadThresholdInfo(ProductSaleQueryResponse thresholdInfo, ProductMaster product)
        {
            var tenantConfig = _context.TenantConfigs.FirstOrDefault(m => m.TenantId == product.TenantId);

            if (tenantConfig != null)
            {
                thresholdInfo.ShowWarning    = tenantConfig.AlertMinimumProductPrice;
                thresholdInfo.CanProceed     = !tenantConfig.EnforceMinimumProductPrice;
                thresholdInfo.FailureMessage = tenantConfig.AlertMinimumPriceMessage;
                thresholdInfo.StopMessage    = tenantConfig.EnforceMinimumPriceMessage;
            }
            return(thresholdInfo);
        }