public async Task <int> UpdateAsync(int id, GarmentLeftoverWarehouseReceiptAval model)
        {
            int Updated = 0;

            using (var transaction = DbContext.Database.CurrentTransaction ?? DbContext.Database.BeginTransaction())
            {
                try
                {
                    GarmentLeftoverWarehouseReceiptAval existingModel = await DbSet.Where(w => w.Id == id).FirstOrDefaultAsync();

                    if (existingModel.ReceiptDate != model.ReceiptDate)
                    {
                        existingModel.ReceiptDate = model.ReceiptDate;
                    }
                    if (existingModel.Remark != model.Remark)
                    {
                        existingModel.Remark = model.Remark;
                    }
                    if (existingModel.TotalAval != model.TotalAval)
                    {
                        existingModel.TotalAval = model.TotalAval;
                    }

                    if (model.AvalType == "AVAL FABRIC")
                    {
                        if (existingModel.TotalAval != model.TotalAval)
                        {
                            GarmentLeftoverWarehouseStock stock = new GarmentLeftoverWarehouseStock
                            {
                                ReferenceType = GarmentLeftoverWarehouseStockReferenceTypeEnum.AVAL_FABRIC,
                                UnitId        = model.UnitFromId,
                                UnitCode      = model.UnitFromCode,
                                UnitName      = model.UnitFromName,
                                Quantity      = existingModel.TotalAval
                            };
                            await StockService.StockOut(stock, existingModel.AvalReceiptNo, model.Id, 0);

                            GarmentLeftoverWarehouseStock stock1 = new GarmentLeftoverWarehouseStock
                            {
                                ReferenceType = GarmentLeftoverWarehouseStockReferenceTypeEnum.AVAL_FABRIC,
                                UnitId        = model.UnitFromId,
                                UnitCode      = model.UnitFromCode,
                                UnitName      = model.UnitFromName,
                                Quantity      = model.TotalAval
                            };
                            await StockService.StockIn(stock1, model.AvalReceiptNo, model.Id, 0);

                            existingModel.TotalAval = model.TotalAval;
                        }
                    }
                    else if (model.AvalType == "AVAL KOMPONEN")
                    {
                        if (existingModel.TotalAval != model.TotalAval)
                        {
                            GarmentLeftoverWarehouseStock stock = new GarmentLeftoverWarehouseStock
                            {
                                ReferenceType = GarmentLeftoverWarehouseStockReferenceTypeEnum.COMPONENT,
                                UnitId        = model.UnitFromId,
                                UnitCode      = model.UnitFromCode,
                                UnitName      = model.UnitFromName,
                                Quantity      = existingModel.TotalAval
                            };
                            await StockService.StockOut(stock, existingModel.AvalReceiptNo, model.Id, 0);

                            GarmentLeftoverWarehouseStock stock1 = new GarmentLeftoverWarehouseStock
                            {
                                ReferenceType = GarmentLeftoverWarehouseStockReferenceTypeEnum.COMPONENT,
                                UnitId        = model.UnitFromId,
                                UnitCode      = model.UnitFromCode,
                                UnitName      = model.UnitFromName,
                                Quantity      = model.TotalAval
                            };
                            await StockService.StockIn(stock1, model.AvalReceiptNo, model.Id, 0);

                            existingModel.TotalAval = model.TotalAval;
                        }
                    }


                    existingModel.FlagForUpdate(IdentityService.Username, UserAgent);

                    Updated = await DbContext.SaveChangesAsync();

                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw e;
                }
            }

            return(Updated);
        }
        public async Task <int> CreateAsync(GarmentLeftoverWarehouseReceiptAval model)
        {
            int Created = 0;

            using (var transaction = DbContext.Database.CurrentTransaction ?? DbContext.Database.BeginTransaction())
            {
                try
                {
                    model.FlagForCreate(IdentityService.Username, UserAgent);
                    model.FlagForUpdate(IdentityService.Username, UserAgent);

                    model.AvalReceiptNo = GenerateNo(model);
                    List <string> avalItemIds = new List <string>();

                    foreach (var item in model.Items)
                    {
                        item.FlagForCreate(IdentityService.Username, UserAgent);
                        item.FlagForUpdate(IdentityService.Username, UserAgent);
                        avalItemIds.Add(item.GarmentAvalProductItemId.ToString());
                    }

                    DbSet.Add(model);

                    Created = await DbContext.SaveChangesAsync();

                    if (model.AvalType == "AVAL FABRIC")
                    {
                        GarmentLeftoverWarehouseStock stock = new GarmentLeftoverWarehouseStock
                        {
                            ReferenceType = GarmentLeftoverWarehouseStockReferenceTypeEnum.AVAL_FABRIC,
                            UnitId        = model.UnitFromId,
                            UnitCode      = model.UnitFromCode,
                            UnitName      = model.UnitFromName,
                            Quantity      = model.TotalAval
                        };
                        await StockService.StockIn(stock, model.AvalReceiptNo, model.Id, 0);


                        await UpdateAvalProductIsReceived(avalItemIds, true);
                    }
                    else if (model.AvalType == "AVAL BAHAN PENOLONG")
                    {
                        foreach (var item in model.Items)
                        {
                            GarmentLeftoverWarehouseStock stock = new GarmentLeftoverWarehouseStock
                            {
                                ReferenceType = GarmentLeftoverWarehouseStockReferenceTypeEnum.AVAL_BAHAN_PENOLONG,
                                UnitId        = model.UnitFromId,
                                UnitCode      = model.UnitFromCode,
                                UnitName      = model.UnitFromName,
                                Quantity      = item.Quantity,
                                ProductCode   = item.ProductCode,
                                ProductName   = item.ProductName,
                                ProductId     = item.ProductId,
                                UomId         = item.UomId,
                                UomUnit       = item.UomUnit,
                            };
                            await StockService.StockIn(stock, model.AvalReceiptNo, model.Id, item.Id);
                        }
                    }
                    else if (model.AvalType == "AVAL KOMPONEN")
                    {
                        GarmentLeftoverWarehouseStock stock = new GarmentLeftoverWarehouseStock
                        {
                            ReferenceType = GarmentLeftoverWarehouseStockReferenceTypeEnum.COMPONENT,
                            UnitId        = model.UnitFromId,
                            UnitCode      = model.UnitFromCode,
                            UnitName      = model.UnitFromName,
                            Quantity      = model.TotalAval
                        };
                        await StockService.StockIn(stock, model.AvalReceiptNo, model.Id, 0);

                        foreach (var item in model.Items)
                        {
                            await UpdateAvalComponentIsReceived(item.AvalComponentId.ToString(), true);
                        }
                    }


                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw e;
                }
            }

            return(Created);
        }