public async Task <int> Handle(UpdateDatesGarmentFinishingOutCommand request, CancellationToken cancellationToken)
        {
            List <Guid> guids = new List <Guid>();

            foreach (var id in request.Identities)
            {
                guids.Add(Guid.Parse(id));
            }
            var FinOuts = _garmentFinishingOutRepository.Query.Where(a => guids.Contains(a.Identity)).Select(a => new GarmentFinishingOut(a)).ToList();

            foreach (var model in FinOuts)
            {
                model.SetDate(request.Date);
                model.Modify();
                await _garmentFinishingOutRepository.Update(model);
            }
            _storage.Save();

            return(guids.Count());
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
        public async Task <GarmentFinishingOut> Handle(PlaceGarmentFinishingOutCommand request, CancellationToken cancellationToken)
        {
            request.Items = request.Items.Where(item => item.IsSave == true).ToList();

            GarmentComodityPrice garmentComodityPrice = _garmentComodityPriceRepository.Query.Where(a => a.IsValid == true && a.UnitId == request.UnitTo.Id && a.ComodityId == request.Comodity.Id).Select(s => new GarmentComodityPrice(s)).Single();
            Guid garmentFinishingOutId = Guid.NewGuid();
            GarmentFinishingOut garmentFinishingOut = new GarmentFinishingOut(
                garmentFinishingOutId,
                GenerateFinOutNo(request),
                new UnitDepartmentId(request.UnitTo.Id),
                request.UnitTo.Code,
                request.UnitTo.Name,
                request.FinishingTo,
                request.FinishingOutDate.GetValueOrDefault(),
                request.RONo,
                request.Article,
                new UnitDepartmentId(request.Unit.Id),
                request.Unit.Code,
                request.Unit.Name,
                new GarmentComodityId(request.Comodity.Id),
                request.Comodity.Code,
                request.Comodity.Name,
                request.IsDifferentSize
                );

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

            Dictionary <string, double> finGood = new Dictionary <string, double>();

            foreach (var item in request.Items)
            {
                if (item.IsSave)
                {
                    Guid garmentFinishingOutItemId = Guid.NewGuid();
                    GarmentFinishingOutItem garmentFinishingOutItem = new GarmentFinishingOutItem(
                        garmentFinishingOutItemId,
                        garmentFinishingOut.Identity,
                        item.FinishingInId,
                        item.FinishingInItemId,
                        new ProductId(item.Product.Id),
                        item.Product.Code,
                        item.Product.Name,
                        item.DesignColor,
                        new SizeId(item.Size.Id),
                        item.Size.Size,
                        request.IsDifferentSize ? item.TotalQuantity : item.Quantity,
                        new UomId(item.Uom.Id),
                        item.Uom.Unit,
                        item.Color,
                        request.IsDifferentSize ? item.TotalQuantity : item.Quantity,
                        item.BasicPrice,
                        item.Price
                        );
                    item.Id = garmentFinishingOutItemId;
                    if (request.IsDifferentSize)
                    {
                        foreach (var detail in item.Details)
                        {
                            Guid garmentFinishingOutDetailId = Guid.NewGuid();
                            GarmentFinishingOutDetail garmentFinishingOutDetail = new GarmentFinishingOutDetail(
                                garmentFinishingOutDetailId,
                                garmentFinishingOutItem.Identity,
                                new SizeId(detail.Size.Id),
                                detail.Size.Size,
                                detail.Quantity,
                                new UomId(detail.Uom.Id),
                                detail.Uom.Unit
                                );
                            detail.Id = garmentFinishingOutDetailId;
                            if (finishingInItemToBeUpdated.ContainsKey(item.FinishingInItemId))
                            {
                                finishingInItemToBeUpdated[item.FinishingInItemId] += detail.Quantity;
                            }
                            else
                            {
                                finishingInItemToBeUpdated.Add(item.FinishingInItemId, detail.Quantity);
                            }

                            await _garmentFinishingOutDetailRepository.Update(garmentFinishingOutDetail);

                            if (request.FinishingTo == "GUDANG JADI")
                            {
                                string finStock = detail.Size.Id + "~" + detail.Size.Size + "~" + detail.Uom.Id + "~" + detail.Uom.Unit + "~" + item.BasicPrice;

                                if (finGood.ContainsKey(finStock))
                                {
                                    finGood[finStock] += detail.Quantity;
                                }
                                else
                                {
                                    finGood.Add(finStock, detail.Quantity);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (finishingInItemToBeUpdated.ContainsKey(item.FinishingInItemId))
                        {
                            finishingInItemToBeUpdated[item.FinishingInItemId] += item.Quantity;
                        }
                        else
                        {
                            finishingInItemToBeUpdated.Add(item.FinishingInItemId, item.Quantity);
                        }

                        if (request.FinishingTo == "GUDANG JADI")
                        {
                            string finStock = item.Size.Id + "~" + item.Size.Size + "~" + item.Uom.Id + "~" + item.Uom.Unit + "~" + item.BasicPrice;

                            if (finGood.ContainsKey(finStock))
                            {
                                finGood[finStock] += item.Quantity;
                            }
                            else
                            {
                                finGood.Add(finStock, item.Quantity);
                            }
                        }
                    }
                    await _garmentFinishingOutItemRepository.Update(garmentFinishingOutItem);
                }
            }

            foreach (var finInItem in finishingInItemToBeUpdated)
            {
                var garmentFinishingInItem = _garmentFinishingInItemRepository.Query.Where(x => x.Identity == finInItem.Key).Select(s => new GarmentFinishingInItem(s)).Single();
                garmentFinishingInItem.SetRemainingQuantity(garmentFinishingInItem.RemainingQuantity - finInItem.Value);
                garmentFinishingInItem.Modify();

                await _garmentFinishingInItemRepository.Update(garmentFinishingInItem);
            }

            if (request.FinishingTo == "GUDANG JADI")
            {
                int count = 1;
                List <GarmentFinishedGoodStock> finGoodStocks = new List <GarmentFinishedGoodStock>();
                foreach (var finGoodStock in finGood)
                {
                    SizeId sizeId     = new SizeId(Convert.ToInt32(finGoodStock.Key.Split("~")[0]));
                    string sizeName   = finGoodStock.Key.Split("~")[1];
                    UomId  uomId      = new UomId(Convert.ToInt32(finGoodStock.Key.Split("~")[2]));
                    string uomUnit    = finGoodStock.Key.Split("~")[3];
                    double basicPrice = Convert.ToDouble(finGoodStock.Key.Split("~")[4]);
                    var    garmentFinishedGoodExist = _garmentFinishedGoodStockRepository.Query.Where(
                        a => a.RONo == request.RONo &&
                        a.Article == request.Article &&
                        a.BasicPrice == basicPrice &&
                        a.UnitId == request.UnitTo.Id &&
                        new SizeId(a.SizeId) == sizeId &&
                        a.ComodityId == request.Comodity.Id &&
                        new UomId(a.UomId) == uomId
                        ).Select(s => new GarmentFinishedGoodStock(s)).SingleOrDefault();

                    double qty = garmentFinishedGoodExist == null ? finGoodStock.Value : (finGoodStock.Value + garmentFinishedGoodExist.Quantity);

                    double price = (basicPrice + (double)garmentComodityPrice.Price) * qty;

                    if (garmentFinishedGoodExist == null)
                    {
                        var now    = DateTime.Now;
                        var year   = now.ToString("yy");
                        var month  = now.ToString("MM");
                        var prefix = $"ST{request.UnitTo.Code.Trim()}{year}{month}";

                        var lastFnGoodNo = _garmentFinishedGoodStockRepository.Query.Where(w => w.FinishedGoodStockNo.StartsWith(prefix))
                                           .OrderByDescending(o => o.FinishedGoodStockNo)
                                           .Select(s => int.Parse(s.FinishedGoodStockNo.Replace(prefix, "")))
                                           .FirstOrDefault();
                        var FinGoodNo = $"{prefix}{(lastFnGoodNo + count).ToString("D4")}";
                        GarmentFinishedGoodStock finishedGood = new GarmentFinishedGoodStock(
                            Guid.NewGuid(),
                            FinGoodNo,
                            request.RONo,
                            request.Article,
                            new UnitDepartmentId(request.UnitTo.Id),
                            request.UnitTo.Code,
                            request.UnitTo.Name,
                            new GarmentComodityId(request.Comodity.Id),
                            request.Comodity.Code,
                            request.Comodity.Name,
                            sizeId,
                            sizeName,
                            uomId,
                            uomUnit,
                            qty,
                            basicPrice,
                            price
                            );
                        count++;
                        await _garmentFinishedGoodStockRepository.Update(finishedGood);

                        finGoodStocks.Add(finishedGood);
                    }
                    else
                    {
                        garmentFinishedGoodExist.SetQuantity(qty);
                        garmentFinishedGoodExist.SetPrice(price);
                        garmentFinishedGoodExist.Modify();

                        await _garmentFinishedGoodStockRepository.Update(garmentFinishedGoodExist);

                        var stock = finGoodStocks.Where(a => a.RONo == request.RONo &&
                                                        a.Article == request.Article &&
                                                        a.BasicPrice == garmentFinishedGoodExist.BasicPrice &&
                                                        a.UnitId == new UnitDepartmentId(request.UnitTo.Id) &&
                                                        a.SizeId == garmentFinishedGoodExist.SizeId &&
                                                        a.ComodityId == new GarmentComodityId(request.Comodity.Id) &&
                                                        a.UomId == garmentFinishedGoodExist.UomId).SingleOrDefault();
                        finGoodStocks.Add(garmentFinishedGoodExist);
                    }
                }

                foreach (var item in request.Items)
                {
                    if (item.IsSave)
                    {
                        if (request.IsDifferentSize)
                        {
                            foreach (var detail in item.Details)
                            {
                                var stock = finGoodStocks.Where(a => a.RONo == request.RONo &&
                                                                a.Article == request.Article &&
                                                                a.BasicPrice == item.BasicPrice &&
                                                                a.UnitId == new UnitDepartmentId(request.UnitTo.Id) &&
                                                                a.SizeId == new SizeId(detail.Size.Id) &&
                                                                a.ComodityId == new GarmentComodityId(request.Comodity.Id) &&
                                                                a.UomId == new UomId(detail.Uom.Id)).Single();

                                double price = (stock.BasicPrice + (double)garmentComodityPrice.Price) * detail.Quantity;

                                GarmentFinishedGoodStockHistory garmentFinishedGoodStockHistory = new GarmentFinishedGoodStockHistory(
                                    Guid.NewGuid(),
                                    stock.Identity,
                                    item.Id,
                                    detail.Id,
                                    Guid.Empty,
                                    Guid.Empty,
                                    Guid.Empty,
                                    Guid.Empty,
                                    Guid.Empty,
                                    Guid.Empty,
                                    "IN",
                                    stock.RONo,
                                    stock.Article,
                                    stock.UnitId,
                                    stock.UnitCode,
                                    stock.UnitName,
                                    stock.ComodityId,
                                    stock.ComodityCode,
                                    stock.ComodityName,
                                    stock.SizeId,
                                    stock.SizeName,
                                    stock.UomId,
                                    stock.UomUnit,
                                    detail.Quantity,
                                    stock.BasicPrice,
                                    price
                                    );
                                await _garmentFinishedGoodStockHistoryRepository.Update(garmentFinishedGoodStockHistory);
                            }
                        }
                        else
                        {
                            var stock = finGoodStocks.Where(a => a.RONo == request.RONo &&
                                                            a.Article == request.Article &&
                                                            a.BasicPrice == item.BasicPrice &&
                                                            a.UnitId == new UnitDepartmentId(request.UnitTo.Id) &&
                                                            a.SizeId == new SizeId(item.Size.Id) &&
                                                            a.ComodityId == new GarmentComodityId(request.Comodity.Id) &&
                                                            a.UomId == new UomId(item.Uom.Id)).Single();

                            double price = (stock.BasicPrice + (double)garmentComodityPrice.Price) * item.Quantity;

                            GarmentFinishedGoodStockHistory garmentFinishedGoodStockHistory = new GarmentFinishedGoodStockHistory(
                                Guid.NewGuid(),
                                stock.Identity,
                                item.Id,
                                Guid.Empty,
                                Guid.Empty,
                                Guid.Empty,
                                Guid.Empty,
                                Guid.Empty,
                                Guid.Empty,
                                Guid.Empty,
                                "IN",
                                stock.RONo,
                                stock.Article,
                                stock.UnitId,
                                stock.UnitCode,
                                stock.UnitName,
                                stock.ComodityId,
                                stock.ComodityCode,
                                stock.ComodityName,
                                stock.SizeId,
                                stock.SizeName,
                                stock.UomId,
                                stock.UomUnit,
                                item.Quantity,
                                stock.BasicPrice,
                                price
                                );
                            await _garmentFinishedGoodStockHistoryRepository.Update(garmentFinishedGoodStockHistory);
                        }
                    }
                }
            }

            await _garmentFinishingOutRepository.Update(garmentFinishingOut);

            _storage.Save();

            return(garmentFinishingOut);
        }
        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);
        }