public async Task <IActionResult> Get(string id)
        {
            Guid guid = Guid.Parse(id);

            VerifyUser();

            GarmentFinishingOutDto garmentFinishingOutDto = _garmentFinishingOutRepository.Find(o => o.Identity == guid).Select(finishOut => new GarmentFinishingOutDto(finishOut)
            {
                Items = _garmentFinishingOutItemRepository.Find(o => o.FinishingOutId == finishOut.Identity).OrderBy(i => i.Color).ThenBy(i => i.SizeName).Select(finishOutItem => new GarmentFinishingOutItemDto(finishOutItem)
                {
                    Details = _garmentFinishingOutDetailRepository.Find(o => o.FinishingOutItemId == finishOutItem.Identity).OrderBy(i => i.SizeName).Select(finishOutDetail => new GarmentFinishingOutDetailDto(finishOutDetail)
                    {
                    }).ToList()
                }).ToList()
            }
                                                                                                                                ).FirstOrDefault();

            await Task.Yield();

            return(Ok(garmentFinishingOutDto));
        }
Example #2
0
        public async Task <GarmentFinishingOut> Handle(RemoveGarmentFinishingOutCommand request, CancellationToken cancellationToken)
        {
            var finishOut = _garmentFinishingOutRepository.Query.Where(o => o.Identity == request.Identity).Select(o => new GarmentFinishingOut(o)).Single();

            Dictionary <Guid, double> finishingInItemToBeUpdated  = new Dictionary <Guid, double>();
            Dictionary <GarmentFinishedGoodStock, double> finGood = new Dictionary <GarmentFinishedGoodStock, double>();

            GarmentComodityPrice garmentComodityPrice = _garmentComodityPriceRepository.Query.Where(a => a.IsValid == true && new UnitDepartmentId(a.UnitId) == finishOut.UnitToId && new GarmentComodityId(a.ComodityId) == finishOut.ComodityId).Select(s => new GarmentComodityPrice(s)).Single();

            _garmentFinishingOutItemRepository.Find(o => o.FinishingOutId == finishOut.Identity).ForEach(async finishOutItem =>
            {
                if (finishOut.IsDifferentSize)
                {
                    _garmentFinishingOutDetailRepository.Find(o => o.FinishingOutItemId == finishOutItem.Identity).ForEach(async finishOutDetail =>
                    {
                        if (finishingInItemToBeUpdated.ContainsKey(finishOutItem.FinishingInItemId))
                        {
                            finishingInItemToBeUpdated[finishOutItem.FinishingInItemId] += finishOutDetail.Quantity;
                        }
                        else
                        {
                            finishingInItemToBeUpdated.Add(finishOutItem.FinishingInItemId, finishOutDetail.Quantity);
                        }

                        if (finishOut.FinishingTo == "GUDANG JADI")
                        {
                            var garmentFinishedGoodExist = _garmentFinishedGoodStockRepository.Query.Where(
                                a => a.RONo == finishOut.RONo &&
                                a.Article == finishOut.Article &&
                                a.BasicPrice == finishOutItem.BasicPrice &&
                                new UnitDepartmentId(a.UnitId) == finishOut.UnitToId &&
                                new SizeId(a.SizeId) == finishOutDetail.SizeId &&
                                new GarmentComodityId(a.ComodityId) == finishOut.ComodityId &&
                                new UomId(a.UomId) == finishOutDetail.UomId
                                ).Select(s => new GarmentFinishedGoodStock(s)).Single();

                            if (finGood.ContainsKey(garmentFinishedGoodExist))
                            {
                                finGood[garmentFinishedGoodExist] += finishOutDetail.Quantity;
                            }
                            else
                            {
                                finGood.Add(garmentFinishedGoodExist, finishOutDetail.Quantity);
                            }

                            GarmentFinishedGoodStockHistory garmentFinishedGoodStockHistory = _garmentFinishedGoodStockHistoryRepository.Query.Where(a => a.FinishingOutDetailId == finishOutDetail.Identity).Select(a => new GarmentFinishedGoodStockHistory(a)).Single();
                            garmentFinishedGoodStockHistory.Remove();
                            await _garmentFinishedGoodStockHistoryRepository.Update(garmentFinishedGoodStockHistory);
                        }

                        finishOutDetail.Remove();
                        await _garmentFinishingOutDetailRepository.Update(finishOutDetail);
                    });
                }
                else
                {
                    if (finishingInItemToBeUpdated.ContainsKey(finishOutItem.FinishingInItemId))
                    {
                        finishingInItemToBeUpdated[finishOutItem.FinishingInItemId] += finishOutItem.Quantity;
                    }
                    else
                    {
                        finishingInItemToBeUpdated.Add(finishOutItem.FinishingInItemId, finishOutItem.Quantity);
                    }

                    if (finishOut.FinishingTo == "GUDANG JADI")
                    {
                        var garmentFinishedGoodExist = _garmentFinishedGoodStockRepository.Query.Where(
                            a => a.RONo == finishOut.RONo &&
                            a.Article == finishOut.Article &&
                            a.BasicPrice == finishOutItem.BasicPrice &&
                            new UnitDepartmentId(a.UnitId) == finishOut.UnitToId &&
                            new SizeId(a.SizeId) == finishOutItem.SizeId &&
                            new GarmentComodityId(a.ComodityId) == finishOut.ComodityId &&
                            new UomId(a.UomId) == finishOutItem.UomId
                            ).Select(s => new GarmentFinishedGoodStock(s)).Single();

                        if (finGood.ContainsKey(garmentFinishedGoodExist))
                        {
                            finGood[garmentFinishedGoodExist] += finishOutItem.Quantity;
                        }
                        else
                        {
                            finGood.Add(garmentFinishedGoodExist, finishOutItem.Quantity);
                        }
                        GarmentFinishedGoodStockHistory garmentFinishedGoodStockHistory = _garmentFinishedGoodStockHistoryRepository.Query.Where(a => a.FinishingOutItemId == finishOutItem.Identity).Select(a => new GarmentFinishedGoodStockHistory(a)).Single();
                        garmentFinishedGoodStockHistory.Remove();

                        await _garmentFinishedGoodStockHistoryRepository.Update(garmentFinishedGoodStockHistory);
                    }
                }


                finishOutItem.Remove();
                await _garmentFinishingOutItemRepository.Update(finishOutItem);
            });

            foreach (var finInItem in finishingInItemToBeUpdated)
            {
                var garmentSewInItem = _garmentFinishingInItemRepository.Query.Where(x => x.Identity == finInItem.Key).Select(s => new GarmentFinishingInItem(s)).Single();
                garmentSewInItem.SetRemainingQuantity(garmentSewInItem.RemainingQuantity + finInItem.Value);
                garmentSewInItem.Modify();
                await _garmentFinishingInItemRepository.Update(garmentSewInItem);
            }
            if (finishOut.FinishingTo == "GUDANG JADI")
            {
                foreach (var finGoodStock in finGood)
                {
                    var garmentFinishedGoodExist = _garmentFinishedGoodStockRepository.Query.Where(
                        a => a.Identity == finGoodStock.Key.Identity
                        ).Select(s => new GarmentFinishedGoodStock(s)).Single();

                    var qty = garmentFinishedGoodExist.Quantity - finGoodStock.Value;

                    garmentFinishedGoodExist.SetQuantity(qty);
                    garmentFinishedGoodExist.SetPrice((garmentFinishedGoodExist.BasicPrice + (double)garmentComodityPrice.Price) * (qty));
                    garmentFinishedGoodExist.Modify();

                    await _garmentFinishedGoodStockRepository.Update(garmentFinishedGoodExist);
                }
            }


            finishOut.Remove();
            await _garmentFinishingOutRepository.Update(finishOut);

            _storage.Save();

            return(finishOut);
        }
        public async Task <GarmentFinishingOut> Handle(UpdateGarmentFinishingOutCommand request, CancellationToken cancellationToken)
        {
            var finishOut = _garmentFinishingOutRepository.Query.Where(o => o.Identity == request.Identity).Select(o => new GarmentFinishingOut(o)).Single();

            Dictionary <Guid, double> finishingInItemToBeUpdated = new Dictionary <Guid, double>();

            _garmentFinishingOutItemRepository.Find(o => o.FinishingOutId == finishOut.Identity).ForEach(async finishOutItem =>
            {
                var item = request.Items.Where(o => o.Id == finishOutItem.Identity).Single();

                var diffSewInQuantity = item.IsSave ? (finishOutItem.Quantity - (request.IsDifferentSize ? item.TotalQuantity : item.Quantity)) : finishOutItem.Quantity;

                if (finishingInItemToBeUpdated.ContainsKey(finishOutItem.FinishingInItemId))
                {
                    finishingInItemToBeUpdated[finishOutItem.FinishingInItemId] += diffSewInQuantity;
                }
                else
                {
                    finishingInItemToBeUpdated.Add(finishOutItem.FinishingInItemId, diffSewInQuantity);
                }

                if (!item.IsSave)
                {
                    item.Quantity = 0;

                    if (request.IsDifferentSize)
                    {
                        _garmentFinishingOutDetailRepository.Find(o => o.FinishingOutItemId == finishOutItem.Identity).ForEach(async finishOutDetail =>
                        {
                            finishOutDetail.Remove();
                            await _garmentFinishingOutDetailRepository.Update(finishOutDetail);
                        });
                    }

                    finishOutItem.Remove();
                }
                else
                {
                    if (request.IsDifferentSize)
                    {
                        _garmentFinishingOutDetailRepository.Find(o => o.FinishingOutItemId == finishOutItem.Identity).ForEach(async finishOutDetail =>
                        {
                            if (finishOutDetail.Identity != Guid.Empty)
                            {
                                var detail = item.Details.Where(o => o.Id == finishOutDetail.Identity).SingleOrDefault();

                                if (detail != null)
                                {
                                    finishOutDetail.SetQuantity(detail.Quantity);
                                    finishOutDetail.SetSizeId(new SizeId(detail.Size.Id));
                                    finishOutDetail.SetSizeName(detail.Size.Size);

                                    finishOutDetail.Modify();
                                }
                                else
                                {
                                    finishOutDetail.Remove();
                                }
                                await _garmentFinishingOutDetailRepository.Update(finishOutDetail);
                            }
                            else
                            {
                                GarmentFinishingOutDetail garmentFinishingOutDetail = new GarmentFinishingOutDetail(
                                    Guid.NewGuid(),
                                    finishOutItem.Identity,
                                    finishOutDetail.SizeId,
                                    finishOutDetail.SizeName,
                                    finishOutDetail.Quantity,
                                    finishOutDetail.UomId,
                                    finishOutDetail.UomUnit
                                    );
                                await _garmentFinishingOutDetailRepository.Update(garmentFinishingOutDetail);
                            }
                        });

                        foreach (var detail in item.Details)
                        {
                            if (detail.Id == Guid.Empty)
                            {
                                GarmentFinishingOutDetail garmentFinishingOutDetail = new GarmentFinishingOutDetail(
                                    Guid.NewGuid(),
                                    finishOutItem.Identity,
                                    new SizeId(detail.Size.Id),
                                    detail.Size.Size,
                                    detail.Quantity,
                                    new UomId(detail.Uom.Id),
                                    detail.Uom.Unit
                                    );
                                await _garmentFinishingOutDetailRepository.Update(garmentFinishingOutDetail);
                            }
                        }
                        finishOutItem.SetQuantity(item.TotalQuantity);
                        finishOutItem.SetRemainingQuantity(item.TotalQuantity);
                    }
                    else
                    {
                        finishOutItem.SetQuantity(item.Quantity);
                        finishOutItem.SetRemainingQuantity(item.Quantity);
                    }

                    finishOutItem.SetPrice(item.Price);
                    finishOutItem.Modify();
                }


                await _garmentFinishingOutItemRepository.Update(finishOutItem);
            });

            foreach (var finishingInItem in finishingInItemToBeUpdated)
            {
                var garmentSewInItem = _garmentFinishingInItemRepository.Query.Where(x => x.Identity == finishingInItem.Key).Select(s => new GarmentFinishingInItem(s)).Single();
                garmentSewInItem.SetRemainingQuantity(garmentSewInItem.RemainingQuantity + finishingInItem.Value);
                garmentSewInItem.Modify();
                await _garmentFinishingInItemRepository.Update(garmentSewInItem);
            }

            finishOut.SetDate(request.FinishingOutDate.GetValueOrDefault());
            finishOut.Modify();
            await _garmentFinishingOutRepository.Update(finishOut);

            _storage.Save();

            return(finishOut);
        }