public long SaveBatchOutPut(BatchOutPut batchOutPut, string userId)
        {
            List <StockGradeSize> stockGradeSizeList = new List <StockGradeSize>();
            double batchQuantity  = BatchQuantity(batchOutPut.BatchId);
            var    batchOutPutDTO = new DTO.BatchOutPutDTO()
            {
                BatchId         = batchOutPut.BatchId,
                BrandOutPut     = batchOutPut.BrandOutPut,
                FlourOutPut     = batchOutPut.FlourOutPut,
                BranchId        = batchOutPut.BranchId,
                SectorId        = batchOutPut.SectorId,
                Loss            = ComputeBatchLoss(batchOutPut.BrandOutPut, batchOutPut.FlourOutPut, batchQuantity),
                FlourPercentage = ComputePercentageBatchFlourOutPut(batchOutPut.FlourOutPut, batchQuantity),
                BrandPercentage = ComputePercentageBatchBrandOutPut(batchOutPut.BrandOutPut, batchQuantity),
                LossPercentage  = ComputePercentageBatchLoss(batchOutPut.BrandOutPut, batchOutPut.FlourOutPut, batchQuantity),
                Deleted         = batchOutPut.Deleted,
                CreatedBy       = batchOutPut.CreatedBy,
                CreatedOn       = batchOutPut.CreatedOn
            };

            var batchOutPutId = this._dataService.SaveBatchOutPut(batchOutPutDTO, userId);

            if (batchOutPut.Grades != null)
            {
                double totalBuveraQuantity = 0;
                if (batchOutPut.Grades.Any())
                {
                    List <BatchGradeSize> batchGradeSizeList = new List <BatchGradeSize>();

                    foreach (var grade in batchOutPut.Grades)
                    {
                        var gradeId = grade.GradeId;
                        if (grade.Denominations != null)
                        {
                            if (grade.Denominations.Any())
                            {
                                foreach (var denomination in grade.Denominations)
                                {
                                    var batchGradeSize = new BatchGradeSize()
                                    {
                                        GradeId       = gradeId,
                                        SizeId        = denomination.DenominationId,
                                        BatchOutPutId = batchOutPutId,
                                        Quantity      = denomination.Quantity,
                                        TimeStamp     = DateTime.Now
                                    };
                                    batchGradeSizeList.Add(batchGradeSize);

                                    var stockGradeSize = new StockGradeSize()
                                    {
                                        GradeId   = gradeId,
                                        SizeId    = denomination.DenominationId,
                                        Quantity  = denomination.Quantity,
                                        TimeStamp = DateTime.Now
                                    };
                                    stockGradeSizeList.Add(stockGradeSize);



                                    var inOrOut = false;
                                    //Method that updates buvera into storeBuveraGradeSize table(storeBuvera stock)
                                    var storeBuveraGradeSize = new StoreBuveraGradeSizeDTO()
                                    {
                                        StoreId  = batchOutPut.StoreId,
                                        GradeId  = batchGradeSize.GradeId,
                                        SizeId   = batchGradeSize.SizeId,
                                        Quantity = batchGradeSize.Quantity,
                                    };

                                    this._buveraDataService.SaveStoreBuveraGradeSize(storeBuveraGradeSize, inOrOut);
                                    totalBuveraQuantity = denomination.Quantity + totalBuveraQuantity;
                                }
                            }
                        }
                    }

                    var buveraDTO = new DTO.BuveraDTO()
                    {
                        TotalQuantity = totalBuveraQuantity,
                        BranchId      = batchOutPut.BranchId,
                        FromSupplier  = Convert.ToString(batchOutPut.StoreId),
                        ToReceiver    = Convert.ToString(batchOutPut.BatchId),
                        StoreId       = batchOutPut.StoreId,
                    };

                    var buveraId = this._buveraDataService.SaveBuvera(buveraDTO, userId);
                    this._dataService.PurgeBatchGradeSize(batchOutPutId);
                    this.SaveBatchGradeSizeList(batchGradeSizeList);
                }
            }
            UpdateBatchSupply(batchOutPut.BatchId, supplyStatusIdComplete, userId);
            if (batchOutPut.BrandOutPut != 0 && batchOutPut.FlourOutPut != 0)
            {
                var stock = new Stock()
                {
                    SectorId      = batchOutPut.SectorId,
                    BranchId      = batchOutPut.BranchId,
                    BatchId       = batchOutPut.BatchId,
                    InOrOut       = true,
                    StoreId       = batchOutPut.StoreId,
                    ProductId     = brandId,
                    ProductOutPut = batchOutPut.BrandOutPut,
                };
                _stockService.SaveStock(stock, userId);
                var flourStock = new Stock()
                {
                    SectorId       = batchOutPut.SectorId,
                    BranchId       = batchOutPut.BranchId,
                    BatchId        = batchOutPut.BatchId,
                    InOrOut        = true,
                    StoreId        = batchOutPut.StoreId,
                    ProductId      = flourId,
                    ProductOutPut  = batchOutPut.FlourOutPut,
                    StockGradeSize = stockGradeSizeList
                };
                _stockService.SaveStock(flourStock, userId);
            }

            return(batchOutPutId);
        }
        public long SaveBatchOutPut(BatchOutPut batchOutPut, string userId)
        {
            List <StockGradeSize> stockGradeSizeList = new List <StockGradeSize>();
            double batchQuantity  = BatchQuantity(batchOutPut.BatchId);
            var    batchOutPutDTO = new DTO.BatchOutPutDTO()
            {
                BatchId         = batchOutPut.BatchId,
                BrandOutPut     = batchOutPut.BrandOutPut,
                FlourOutPut     = batchOutPut.FlourOutPut,
                BranchId        = batchOutPut.BranchId,
                SectorId        = batchOutPut.SectorId,
                Loss            = ComputeBatchLoss(batchOutPut.BrandOutPut, batchOutPut.FlourOutPut, batchQuantity),
                FlourPercentage = ComputePercentageBatchFlourOutPut(batchOutPut.FlourOutPut, batchQuantity),
                BrandPercentage = ComputePercentageBatchBrandOutPut(batchOutPut.BrandOutPut, batchQuantity),
                LossPercentage  = ComputePercentageBatchLoss(batchOutPut.BrandOutPut, batchOutPut.FlourOutPut, batchQuantity),
                Deleted         = batchOutPut.Deleted,
                CreatedBy       = batchOutPut.CreatedBy,
                CreatedOn       = batchOutPut.CreatedOn
            };

            var batchOutPutId = this._dataService.SaveBatchOutPut(batchOutPutDTO, userId);

            if (batchOutPut.Grades != null)
            {
                if (batchOutPut.Grades.Any())
                {
                    List <BatchGradeSize> batchGradeSizeList = new List <BatchGradeSize>();

                    foreach (var grade in batchOutPut.Grades)
                    {
                        var gradeId = grade.GradeId;
                        if (grade.Denominations != null)
                        {
                            if (grade.Denominations.Any())
                            {
                                foreach (var denomination in grade.Denominations)
                                {
                                    var batchGradeSize = new BatchGradeSize()
                                    {
                                        GradeId       = gradeId,
                                        SizeId        = denomination.DenominationId,
                                        BatchOutPutId = batchOutPutId,
                                        Quantity      = denomination.Quantity,
                                        TimeStamp     = DateTime.Now
                                    };
                                    batchGradeSizeList.Add(batchGradeSize);

                                    var stockGradeSize = new StockGradeSize()
                                    {
                                        GradeId   = gradeId,
                                        SizeId    = denomination.DenominationId,
                                        Quantity  = denomination.Quantity,
                                        TimeStamp = DateTime.Now
                                    };
                                    stockGradeSizeList.Add(stockGradeSize);
                                }
                            }
                        }
                    }
                    this._dataService.PurgeBatchGradeSize(batchOutPutId);
                    this.SaveBatchGradeSizeList(batchGradeSizeList);
                }
            }
            UpdateBatchSupply(batchOutPut.BatchId, supplyStatusIdComplete, userId);
            if (batchOutPut.BrandOutPut != 0 && batchOutPut.FlourOutPut != 0)
            {
                var stock = new Stock()
                {
                    SectorId      = batchOutPut.SectorId,
                    BranchId      = batchOutPut.BranchId,
                    BatchId       = batchOutPut.BatchId,
                    InOrOut       = true,
                    StoreId       = batchOutPut.StoreId,
                    ProductId     = brandId,
                    ProductOutPut = batchOutPut.BrandOutPut,
                };
                _stockService.SaveStock(stock, userId);
                var flourStock = new Stock()
                {
                    SectorId       = batchOutPut.SectorId,
                    BranchId       = batchOutPut.BranchId,
                    BatchId        = batchOutPut.BatchId,
                    InOrOut        = true,
                    StoreId        = batchOutPut.StoreId,
                    ProductId      = flourId,
                    ProductOutPut  = batchOutPut.FlourOutPut,
                    StockGradeSize = stockGradeSizeList
                };
                _stockService.SaveStock(flourStock, userId);
            }

            return(batchOutPutId);
        }