public async Task <int> UpdateAsync(int id, GarmentLeftoverWarehouseReceiptFabric model)
        {
            using (var transaction = DbContext.Database.CurrentTransaction ?? DbContext.Database.BeginTransaction())
            {
                try
                {
                    int Updated = 0;

                    GarmentLeftoverWarehouseReceiptFabric 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;
                    }

                    existingModel.FlagForUpdate(IdentityService.Username, UserAgent);

                    Updated = await DbContext.SaveChangesAsync();

                    transaction.Commit();

                    return(Updated);
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw e;
                }
            }
        }
        public async Task <GarmentLeftoverWarehouseReceiptFabric> GetTestData()
        {
            GarmentLeftoverWarehouseReceiptFabric data = GetNewData();

            await Service.CreateAsync(data);

            return(data);
        }
        public async Task <int> CreateAsync(GarmentLeftoverWarehouseReceiptFabric model)
        {
            using (var transaction = DbContext.Database.CurrentTransaction ?? DbContext.Database.BeginTransaction())
            {
                try
                {
                    int Created = 0;

                    model.FlagForCreate(IdentityService.Username, UserAgent);
                    model.FlagForUpdate(IdentityService.Username, UserAgent);

                    model.ReceiptNoteNo = GenerateNo(model);

                    foreach (var item in model.Items)
                    {
                        item.FlagForCreate(IdentityService.Username, UserAgent);
                        item.FlagForUpdate(IdentityService.Username, UserAgent);
                    }
                    DbSet.Add(model);
                    Created = await DbContext.SaveChangesAsync();

                    foreach (var item in model.Items)
                    {
                        GarmentLeftoverWarehouseStock stock = new GarmentLeftoverWarehouseStock
                        {
                            ReferenceType = GarmentLeftoverWarehouseStockReferenceTypeEnum.FABRIC,
                            UnitId        = model.UnitFromId,
                            UnitCode      = model.UnitFromCode,
                            UnitName      = model.UnitFromName,
                            PONo          = item.POSerialNumber,
                            UomId         = item.UomId,
                            UomUnit       = item.UomUnit,
                            Quantity      = item.Quantity,
                            ProductCode   = item.ProductCode,
                            ProductId     = item.ProductId,
                            ProductName   = item.ProductName,
                            BasicPrice    = item.BasicPrice
                        };
                        await StockService.StockIn(stock, model.ReceiptNoteNo, model.Id, item.Id);
                    }

                    await UpdateUnitExpenditureNoteIsReceived(model.UENId, true);

                    transaction.Commit();

                    return(Created);
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw e;
                }
            }
        }
        private string GenerateNo(GarmentLeftoverWarehouseReceiptFabric model)
        {
            string prefix = "BMF" + model.UnitFromCode.Trim() + model._CreatedUtc.ToString("yy") + model._CreatedUtc.ToString("MM");

            var lastNo = DbSet.Where(w => w.ReceiptNoteNo.StartsWith(prefix))
                         .OrderByDescending(o => o.ReceiptNoteNo)
                         .Select(s => int.Parse(s.ReceiptNoteNo.Replace(prefix, "")))
                         .FirstOrDefault();

            var curNo = $"{prefix}{(lastNo + 1).ToString("D4")}";

            return(curNo);
        }
        public GarmentLeftoverWarehouseReceiptFabricViewModel MapToViewModel(GarmentLeftoverWarehouseReceiptFabric model)
        {
            GarmentLeftoverWarehouseReceiptFabricViewModel viewModel = new GarmentLeftoverWarehouseReceiptFabricViewModel();

            PropertyCopier <GarmentLeftoverWarehouseReceiptFabric, GarmentLeftoverWarehouseReceiptFabricViewModel> .Copy(model, viewModel);

            viewModel.UnitFrom = new UnitViewModel
            {
                Id   = model.UnitFromId.ToString(),
                Code = model.UnitFromCode,
                Name = model.UnitFromName
            };

            viewModel.StorageFrom = new StorageViewModel
            {
                _id  = model.StorageFromId.ToString(),
                code = model.StorageFromCode,
                name = model.StorageFromName
            };

            if (model.Items != null)
            {
                viewModel.Items = new List <GarmentLeftoverWarehouseReceiptFabricItemViewModel>();
                foreach (var modelItem in model.Items)
                {
                    GarmentLeftoverWarehouseReceiptFabricItemViewModel viewModelItem = new GarmentLeftoverWarehouseReceiptFabricItemViewModel();
                    PropertyCopier <GarmentLeftoverWarehouseReceiptFabricItem, GarmentLeftoverWarehouseReceiptFabricItemViewModel> .Copy(modelItem, viewModelItem);

                    viewModelItem.Product = new ProductViewModel
                    {
                        Id   = modelItem.ProductId.ToString(),
                        Code = modelItem.ProductCode,
                        Name = modelItem.ProductName
                    };

                    viewModelItem.Uom = new UomViewModel
                    {
                        Id   = modelItem.UomId.ToString(),
                        Unit = modelItem.UomUnit
                    };

                    viewModel.Items.Add(viewModelItem);
                }
            }

            return(viewModel);
        }
        public GarmentLeftoverWarehouseReceiptFabric CopyData(GarmentLeftoverWarehouseReceiptFabric oldData)
        {
            GarmentLeftoverWarehouseReceiptFabric newData = new GarmentLeftoverWarehouseReceiptFabric();

            PropertyCopier <GarmentLeftoverWarehouseReceiptFabric, GarmentLeftoverWarehouseReceiptFabric> .Copy(oldData, newData);

            newData.Items = new List <GarmentLeftoverWarehouseReceiptFabricItem>();
            foreach (var oldItem in oldData.Items)
            {
                GarmentLeftoverWarehouseReceiptFabricItem newItem = new GarmentLeftoverWarehouseReceiptFabricItem();

                PropertyCopier <GarmentLeftoverWarehouseReceiptFabricItem, GarmentLeftoverWarehouseReceiptFabricItem> .Copy(oldItem, newItem);

                newData.Items.Add(newItem);
            }

            return(newData);
        }
        public GarmentLeftoverWarehouseReceiptFabric MapToModel(GarmentLeftoverWarehouseReceiptFabricViewModel viewModel)
        {
            GarmentLeftoverWarehouseReceiptFabric model = new GarmentLeftoverWarehouseReceiptFabric();

            PropertyCopier <GarmentLeftoverWarehouseReceiptFabricViewModel, GarmentLeftoverWarehouseReceiptFabric> .Copy(viewModel, model);

            if (viewModel.UnitFrom != null)
            {
                model.UnitFromId   = long.Parse(viewModel.UnitFrom.Id);
                model.UnitFromCode = viewModel.UnitFrom.Code;
                model.UnitFromName = viewModel.UnitFrom.Name;
            }

            if (viewModel.StorageFrom != null)
            {
                model.StorageFromId   = long.Parse(viewModel.StorageFrom._id);
                model.StorageFromCode = viewModel.StorageFrom.code;
                model.StorageFromName = viewModel.StorageFrom.name;
            }

            model.Items = new List <GarmentLeftoverWarehouseReceiptFabricItem>();
            foreach (var viewModelItem in viewModel.Items)
            {
                GarmentLeftoverWarehouseReceiptFabricItem modelItem = new GarmentLeftoverWarehouseReceiptFabricItem();
                PropertyCopier <GarmentLeftoverWarehouseReceiptFabricItemViewModel, GarmentLeftoverWarehouseReceiptFabricItem> .Copy(viewModelItem, modelItem);

                if (viewModelItem.Product != null)
                {
                    modelItem.ProductId   = long.Parse(viewModelItem.Product.Id);
                    modelItem.ProductCode = viewModelItem.Product.Code;
                    modelItem.ProductName = viewModelItem.Product.Name;
                }

                if (viewModelItem.Uom != null)
                {
                    modelItem.UomId   = long.Parse(viewModelItem.Uom.Id);
                    modelItem.UomUnit = viewModelItem.Uom.Unit;
                }

                model.Items.Add(modelItem);
            }

            return(model);
        }