public async Task <int> StockIn(GarmentLeftoverWarehouseStock stock, string StockReferenceNo, int StockReferenceId, int StockReferenceItemId)
        {
            try
            {
                int Affected = 0;

                var Query = DbSetStock.Where(w => w.ReferenceType == stock.ReferenceType && w.UnitId == stock.UnitId);

                switch (stock.ReferenceType)
                {
                case GarmentLeftoverWarehouseStockReferenceTypeEnum.FABRIC:
                    Query = Query.Where(w => w.PONo == stock.PONo && w.UomId == stock.UomId);
                    break;

                case GarmentLeftoverWarehouseStockReferenceTypeEnum.FINISHED_GOOD:
                    Query = Query.Where(w => w.RONo == stock.RONo && w.LeftoverComodityId == stock.LeftoverComodityId && w.UnitId == stock.UnitId);
                    break;

                case GarmentLeftoverWarehouseStockReferenceTypeEnum.AVAL_FABRIC:
                    break;

                case GarmentLeftoverWarehouseStockReferenceTypeEnum.AVAL_BAHAN_PENOLONG:
                    Query = Query.Where(w => w.ProductId == stock.ProductId && w.UomId == stock.UomId);
                    break;

                case GarmentLeftoverWarehouseStockReferenceTypeEnum.COMPONENT:
                    break;

                case GarmentLeftoverWarehouseStockReferenceTypeEnum.ACCESSORIES:
                    Query = Query.Where(w => w.PONo == stock.PONo && w.UomId == stock.UomId && w.ProductId == stock.ProductId);
                    break;
                }

                var existingStock = Query.SingleOrDefault();
                if (existingStock == null)
                {
                    stock.FlagForCreate(IdentityService.Username, UserAgent);
                    stock.FlagForUpdate(IdentityService.Username, UserAgent);

                    stock.Histories = new List <GarmentLeftoverWarehouseStockHistory>();
                    var stockHistory = new GarmentLeftoverWarehouseStockHistory
                    {
                        StockReferenceNo     = StockReferenceNo,
                        StockReferenceId     = StockReferenceId,
                        StockReferenceItemId = StockReferenceItemId,
                        StockType            = GarmentLeftoverWarehouseStockTypeEnum.IN,
                        BeforeQuantity       = 0,
                        Quantity             = stock.Quantity,
                        AfterQuantity        = stock.Quantity
                    };
                    stockHistory.FlagForCreate(IdentityService.Username, UserAgent);
                    stockHistory.FlagForUpdate(IdentityService.Username, UserAgent);
                    stock.Histories.Add(stockHistory);

                    DbSetStock.Add(stock);
                }
                else
                {
                    existingStock.Quantity += stock.Quantity;
                    existingStock.FlagForUpdate(IdentityService.Username, UserAgent);

                    var lastStockHistory = DbSetStockHistory.Where(w => w.StockId == existingStock.Id).OrderBy(o => o._CreatedUtc).Last();
                    var beforeQuantity   = lastStockHistory.AfterQuantity;

                    var stockHistory = new GarmentLeftoverWarehouseStockHistory
                    {
                        StockId              = existingStock.Id,
                        StockReferenceNo     = StockReferenceNo,
                        StockReferenceId     = StockReferenceId,
                        StockReferenceItemId = StockReferenceItemId,
                        StockType            = GarmentLeftoverWarehouseStockTypeEnum.IN,
                        BeforeQuantity       = beforeQuantity,
                        Quantity             = stock.Quantity,
                        AfterQuantity        = beforeQuantity + stock.Quantity
                    };
                    stockHistory.FlagForCreate(IdentityService.Username, UserAgent);
                    stockHistory.FlagForUpdate(IdentityService.Username, UserAgent);

                    DbSetStockHistory.Add(stockHistory);
                }

                Affected = await DbContext.SaveChangesAsync();

                return(Affected);
            }
            catch (Exception e)
            {
                throw e;
            }
        }