public async Task <GarmentDeliveryReturn> Handle(RemoveGarmentDeliveryReturnCommand request, CancellationToken cancellationToken)
        {
            var garmentDeliveryReturn = _garmentDeliveryReturnRepository.Find(o => o.Identity == request.Id).FirstOrDefault();

            if (garmentDeliveryReturn == null)
            {
                throw Validator.ErrorValidation(("Id", "Invalid Id: " + request.Id));
            }

            var garmentDeliveryReturnItems = _garmentDeliveryReturnItemRepository.Find(x => x.DRId == request.Id);

            foreach (var item in garmentDeliveryReturnItems)
            {
                item.Remove();
                await _garmentDeliveryReturnItemRepository.Update(item);

                if (item.ProductName == "FABRIC")
                {
                    var garmentPreparingItem = _garmentPreparingItemRepository.Find(o => o.Identity == Guid.Parse(item.PreparingItemId)).Single();

                    garmentPreparingItem.setRemainingQuantityZeroValue(garmentPreparingItem.RemainingQuantity + item.Quantity);

                    garmentPreparingItem.SetModified();
                    await _garmentPreparingItemRepository.Update(garmentPreparingItem);
                }
            }

            garmentDeliveryReturn.Remove();

            await _garmentDeliveryReturnRepository.Update(garmentDeliveryReturn);

            _storage.Save();

            return(garmentDeliveryReturn);
        }
Exemplo n.º 2
0
        public async Task <GarmentDeliveryReturn> Handle(PlaceGarmentDeliveryReturnCommand request, CancellationToken cancellationToken)
        {
            request.Items = request.Items.Where(item => item.IsSave == true).ToList();
            var garmentDeliveryReturn = _garmentDeliveryReturnRepository.Find(o =>
                                                                              o.DRNo == request.DRNo &&
                                                                              o.RONo == request.RONo &&
                                                                              o.Article == request.Article &&
                                                                              o.UnitDOId == request.UnitDOId &&
                                                                              o.UnitDONo == request.UnitDONo &&
                                                                              o.UENId == request.UENId &&
                                                                              o.PreparingId == request.PreparingId &&
                                                                              o.ReturnDate == request.ReturnDate &&
                                                                              o.ReturnType == request.ReturnType &&
                                                                              o.UnitId == request.Unit.Id &&
                                                                              o.UnitCode == request.Unit.Code &&
                                                                              o.UnitName == request.Unit.Name &&
                                                                              o.StorageId == request.Storage.Id &&
                                                                              o.StorageCode == request.Storage.Code &&
                                                                              o.StorageName == request.Storage.Name &&
                                                                              o.IsUsed == request.IsUsed).FirstOrDefault();
            List <GarmentDeliveryReturnItem> garmentDeliveryReturnItem = new List <GarmentDeliveryReturnItem>();

            if (garmentDeliveryReturn == null)
            {
                garmentDeliveryReturn = new GarmentDeliveryReturn(Guid.NewGuid(), GenerateDRNo(request), request.RONo, request.Article, request.UnitDOId, request.UnitDONo, request.UENId, request.PreparingId, request.ReturnDate, request.ReturnType, new UnitDepartmentId(request.Unit.Id), request.Unit.Code, request.Unit.Name, new StorageId(request.Storage.Id), request.Storage.Name, request.Storage.Code, request.IsUsed);
                request.Items.Select(x => new GarmentDeliveryReturnItem(Guid.NewGuid(), garmentDeliveryReturn.Identity, x.UnitDOItemId, x.UENItemId, x.PreparingItemId, new ProductId(x.Product.Id), x.Product.Code, x.Product.Name, x.DesignColor, x.RONo, x.Quantity, new UomId(x.Uom.Id), x.Uom.Unit)).ToList()
                .ForEach(async x => await _garmentDeliveryReturnItemRepository.Update(x));
            }

            foreach (var itemDeliveryReturn in request.Items)
            {
                if (itemDeliveryReturn.Product.Name == "FABRIC")
                {
                    var garmentPreparingItem = _garmentPreparingItemRepository.Find(o => o.Identity == Guid.Parse(itemDeliveryReturn.PreparingItemId)).Single();

                    garmentPreparingItem.setRemainingQuantityZeroValue(garmentPreparingItem.RemainingQuantity - itemDeliveryReturn.Quantity);

                    garmentPreparingItem.SetModified();
                    await _garmentPreparingItemRepository.Update(garmentPreparingItem);
                }
            }

            garmentDeliveryReturn.SetModified();

            await _garmentDeliveryReturnRepository.Update(garmentDeliveryReturn);

            _storage.Save();

            return(garmentDeliveryReturn);
        }
        public async Task <GarmentDeliveryReturn> Handle(UpdateGarmentDeliveryReturnCommand request, CancellationToken cancellaitonToken)
        {
            var requestTempItems = request.Items.Where(item => item.IsSave != true);

            request.Items = request.Items.Where(item => item.IsSave == true).ToList();
            var garmentDeliveryReturn = _garmentDeliveryReturnRepository.Find(o => o.Identity == request.Identity).FirstOrDefault();

            if (garmentDeliveryReturn == null)
            {
                throw Validator.ErrorValidation(("Id", "Invalid Id: " + request.Identity));
            }

            garmentDeliveryReturn.setDRNo(request.DRNo);
            garmentDeliveryReturn.setRONo(request.RONo);
            garmentDeliveryReturn.setArticle(request.Article);
            garmentDeliveryReturn.setUnitDOId(request.UnitDOId);
            garmentDeliveryReturn.setUnitDONo(request.UnitDONo);
            garmentDeliveryReturn.setUENId(request.UENId);
            garmentDeliveryReturn.setPreparingId(request.PreparingId);
            garmentDeliveryReturn.setReturnDate(request.ReturnDate);
            garmentDeliveryReturn.setReturnType(request.ReturnType);
            garmentDeliveryReturn.setUnitId(new UnitDepartmentId(request.Unit.Id));
            garmentDeliveryReturn.setUnitCode(request.Unit.Code);
            garmentDeliveryReturn.setUnitName(request.Unit.Name);
            garmentDeliveryReturn.setStorageId(new StorageId(request.Storage.Id));
            garmentDeliveryReturn.setStorageCode(request.Storage.Code);
            garmentDeliveryReturn.setStorageName(request.Storage.Name);
            garmentDeliveryReturn.setIsUsed(request.IsUsed);

            var dbGarmentDeliveryReturnItem = _garmentDeliveryReturnItemRepository.Find(y => y.DRId == garmentDeliveryReturn.Identity);
            var updatedItems = request.Items.Where(x => dbGarmentDeliveryReturnItem.Any(y => y.DRId == garmentDeliveryReturn.Identity));
            var addedItems   = request.Items.Where(x => !dbGarmentDeliveryReturnItem.Any(y => y.DRId == garmentDeliveryReturn.Identity));


            foreach (var item in updatedItems)
            {
                var dbItem = dbGarmentDeliveryReturnItem.Find(x => x.Identity == item.Id);
                dbItem.setUnitDOItemId(item.UnitDOItemId);
                dbItem.setUENItemId(item.UENItemId);
                dbItem.setPreparingItemId(item.PreparingItemId);
                dbItem.setProductId(new ProductId(item.Product.Id));
                dbItem.setProductCode(item.Product.Code);
                dbItem.setProductName(item.Product.Name);
                dbItem.setDesignColor(item.DesignColor);
                dbItem.setRONo(item.RONo);

                dbItem.setUomId(new UomId(item.Uom.Id));
                dbItem.setUomUnit(item.Uom.Unit);

                if (item.Product.Name == "FABRIC")
                {
                    var garmentPreparingItem = _garmentPreparingItemRepository.Query.Where(x => x.Identity == Guid.Parse(item.PreparingItemId)).Select(s => new GarmentPreparingItem(s)).Single();
                    garmentPreparingItem.setRemainingQuantity(garmentPreparingItem.RemainingQuantity + dbItem.Quantity - item.Quantity);
                    garmentPreparingItem.SetModified();
                    await _garmentPreparingItemRepository.Update(garmentPreparingItem);
                }
                dbItem.setQuantity(item.Quantity);

                dbItem.SetModified();
                await _garmentDeliveryReturnItemRepository.Update(dbItem);
            }

            addedItems.Select(x => new GarmentDeliveryReturnItem(Guid.NewGuid(), garmentDeliveryReturn.Identity, x.UnitDOItemId, x.UENItemId, x.PreparingItemId, new ProductId(x.Product.Id), x.Product.Code, x.Product.Name, x.DesignColor, x.RONo, x.Quantity, new UomId(x.Uom.Id), x.Uom.Unit)).ToList()
            .ForEach(async x => await _garmentDeliveryReturnItemRepository.Update(x));

            foreach (var itemDeleted in requestTempItems)
            {
                var dbItemDeleted = dbGarmentDeliveryReturnItem.Find(x => x.Identity == itemDeleted.Id);
                var deletedItems  = dbGarmentDeliveryReturnItem.Find(x => x.Identity == itemDeleted.Id);
                if (itemDeleted.Product.Name == "FABRIC")
                {
                    var garmentPreparingItemDeleted = _garmentPreparingItemRepository.Query.Where(x => x.Identity == Guid.Parse(itemDeleted.PreparingItemId)).Select(s => new GarmentPreparingItem(s)).Single();
                    garmentPreparingItemDeleted.setRemainingQuantity(garmentPreparingItemDeleted.RemainingQuantity + dbItemDeleted.Quantity);
                    garmentPreparingItemDeleted.SetModified();
                    await _garmentPreparingItemRepository.Update(garmentPreparingItemDeleted);
                }
                deletedItems.Remove();
                await _garmentDeliveryReturnItemRepository.Update(deletedItems);
            }

            garmentDeliveryReturn.SetModified();

            await _garmentDeliveryReturnRepository.Update(garmentDeliveryReturn);

            _storage.Save();

            return(garmentDeliveryReturn);
        }