public async Task <IActionResult> Get(int page = 1, int size = 25, string order = "{}", [Bind(Prefix = "Select[]")] List <string> select = null, string keyword = null, string filter = "{}")
        {
            VerifyUser();

            var    query    = _garmentCuttingOutRepository.Read(page, size, order, "", filter);
            var    total    = query.Count();
            double totalQty = query.Sum(a => a.GarmentCuttingOutItem.Sum(b => b.GarmentCuttingOutDetail.Sum(c => c.CuttingOutQuantity)));

            query = query.Skip((page - 1) * size).Take(size);

            var garmentCuttingOutDto            = _garmentCuttingOutRepository.Find(query).Select(o => new GarmentSubconCuttingOutListDto(o)).ToArray();
            var garmentCuttingOutItemDto        = _garmentCuttingOutItemRepository.Find(_garmentCuttingOutItemRepository.Query).Select(o => new GarmentSubconCuttingOutItemDto(o)).ToList();
            var garmentCuttingOutItemDtoArray   = _garmentCuttingOutItemRepository.Find(_garmentCuttingOutItemRepository.Query).Select(o => new GarmentSubconCuttingOutItemDto(o)).ToArray();
            var garmentCuttingOutDetailDto      = _garmentCuttingOutDetailRepository.Find(_garmentCuttingOutDetailRepository.Query).Select(o => new GarmentSubconCuttingOutDetailDto(o)).ToList();
            var garmentCuttingOutDetailDtoArray = _garmentCuttingOutDetailRepository.Find(_garmentCuttingOutDetailRepository.Query).Select(o => new GarmentSubconCuttingOutDetailDto(o)).ToArray();

            Parallel.ForEach(garmentCuttingOutDto, itemDto =>
            {
                var garmentCuttingOutItems = garmentCuttingOutItemDto.Where(x => x.CutOutId == itemDto.Id).ToList();


                itemDto.Items = garmentCuttingOutItems;

                Parallel.ForEach(itemDto.Items, detailDto =>
                {
                    var garmentCuttingOutDetails = garmentCuttingOutDetailDto.Where(x => x.CutOutItemId == detailDto.Id).ToList();
                    detailDto.Details            = garmentCuttingOutDetails;

                    detailDto.Details = detailDto.Details.OrderBy(x => x.Id).ToList();
                });

                itemDto.Items = itemDto.Items.OrderBy(x => x.Id).ToList();

                itemDto.Products = itemDto.Items.Select(i => i.Product.Code).Distinct().ToList();
                itemDto.TotalCuttingOutQuantity = itemDto.Items.Sum(i => i.Details.Sum(d => d.CuttingOutQuantity));
                itemDto.TotalRemainingQuantity  = itemDto.Items.Sum(i => i.Details.Sum(d => d.RemainingQuantity));
            });

            if (!string.IsNullOrEmpty(keyword))
            {
                garmentCuttingOutItemDtoArray = garmentCuttingOutItemDto.Where(x => x.Product.Code.Contains(keyword, StringComparison.OrdinalIgnoreCase)).ToArray();
                List <GarmentSubconCuttingOutListDto> ListTemp = new List <GarmentSubconCuttingOutListDto>();
                foreach (var a in garmentCuttingOutItemDtoArray)
                {
                    var temp = garmentCuttingOutDto.Where(x => x.Id.Equals(a.CutOutId)).ToArray();
                    foreach (var b in temp)
                    {
                        ListTemp.Add(b);
                    }
                }

                var garmentCuttingOutDtoList = garmentCuttingOutDto.Where(x => x.CutOutNo.Contains(keyword, StringComparison.OrdinalIgnoreCase) ||
                                                                          x.RONo.Contains(keyword, StringComparison.OrdinalIgnoreCase) ||
                                                                          x.Article.Contains(keyword, StringComparison.OrdinalIgnoreCase)
                                                                          ).ToList();

                var i = 0;
                foreach (var data in ListTemp)
                {
                    i = 0;
                    foreach (var item in garmentCuttingOutDtoList)
                    {
                        if (data.Id == item.Id)
                        {
                            i++;
                        }
                    }
                    if (i == 0)
                    {
                        garmentCuttingOutDtoList.Add(data);
                    }
                }
                var garmentCuttingOutDtoListArray = garmentCuttingOutDtoList.ToArray();
                if (order != "{}")
                {
                    Dictionary <string, string> OrderDictionary = JsonConvert.DeserializeObject <Dictionary <string, string> >(order);
                    garmentCuttingOutDtoListArray = QueryHelper <GarmentSubconCuttingOutListDto> .Order(garmentCuttingOutDtoList.AsQueryable(), OrderDictionary).ToArray();
                }
                else
                {
                    garmentCuttingOutDtoListArray = garmentCuttingOutDtoList.OrderByDescending(x => x.LastModifiedDate).ToArray();
                }
                totalQty = garmentCuttingOutDtoListArray.Sum(a => a.Items.Sum(b => b.Details.Sum(c => c.CuttingOutQuantity)));
                //garmentCuttingOutDtoListArray = garmentCuttingOutDtoListArray.Take(size).Skip((page - 1) * size).ToArray();

                await Task.Yield();

                return(Ok(garmentCuttingOutDtoListArray, info: new
                {
                    page,
                    size,
                    total,
                    totalQty
                }));
            }
            else
            {
                //if (order != "{}")
                //{
                //    Dictionary<string, string> OrderDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(order);
                //    garmentCuttingOutDto = QueryHelper<GarmentSubconCuttingOutListDto>.Order(garmentCuttingOutDto.AsQueryable(), OrderDictionary).ToArray();
                //}
                //else
                //{
                //    garmentCuttingOutDto = garmentCuttingOutDto.OrderByDescending(x => x.LastModifiedDate).ToArray();
                //}

                //garmentCuttingOutDto = garmentCuttingOutDto.Take(size).Skip((page - 1) * size).ToArray();

                await Task.Yield();

                return(Ok(garmentCuttingOutDto, info: new
                {
                    page,
                    size,
                    total,
                    totalQty
                }));
            }
        }
Example #2
0
        public async Task <GarmentSubconCuttingOut> Handle(RemoveGarmentSubconCuttingOutCommand request, CancellationToken cancellationToken)
        {
            var cutOut = _garmentCuttingOutRepository.Query.Where(o => o.Identity == request.Identity).Select(o => new GarmentSubconCuttingOut(o)).Single();

            Dictionary <Guid, double>         cuttingInDetailToBeUpdated = new Dictionary <Guid, double>();
            Dictionary <string, double>       cuttingSubconToBeUpdated   = new Dictionary <string, double>();
            Dictionary <string, List <Guid> > cuttingSubconToBeUpdatedId = new Dictionary <string, List <Guid> >();

            _garmentCuttingOutItemRepository.Find(o => o.CutOutId == cutOut.Identity).ForEach(async cutOutItem =>
            {
                _garmentCuttingOutDetailRepository.Find(o => o.CutOutItemId == cutOutItem.Identity).ForEach(async cutOutDetail =>
                {
                    string key = cutOut.RONo + "~" + cutOutDetail.SizeId.Value.ToString() + "~" + cutOutDetail.SizeName + "~"
                                 + cutOutItem.ProductId.Value.ToString() + "~" + cutOutItem.ProductCode + "~" + cutOutItem.ProductName + "~"
                                 + cutOut.ComodityId.Value.ToString() + "~" + cutOut.ComodityCode + "~" + cutOut.ComodityName + "~"
                                 + cutOutItem.DesignColor + "~" + cutOutDetail.Remark + "~" + cutOutDetail.BasicPrice;

                    if (cuttingSubconToBeUpdated.ContainsKey(key))
                    {
                        cuttingSubconToBeUpdated[key] += cutOutDetail.CuttingOutQuantity;
                        cuttingSubconToBeUpdatedId[key].Add(cutOutDetail.Identity);
                    }
                    else
                    {
                        cuttingSubconToBeUpdated.Add(key, cutOutDetail.CuttingOutQuantity);
                        cuttingSubconToBeUpdatedId.Add(key, new List <Guid> {
                            cutOutDetail.Identity
                        });
                    }

                    if (cuttingInDetailToBeUpdated.ContainsKey(cutOutItem.CuttingInDetailId))
                    {
                        cuttingInDetailToBeUpdated[cutOutItem.CuttingInDetailId] += cutOutDetail.RemainingQuantity;
                    }
                    else
                    {
                        cuttingInDetailToBeUpdated.Add(cutOutItem.CuttingInDetailId, cutOutDetail.RemainingQuantity);
                    }

                    cutOutDetail.Remove();
                    await _garmentCuttingOutDetailRepository.Update(cutOutDetail);
                });

                cutOutItem.Remove();
                await _garmentCuttingOutItemRepository.Update(cutOutItem);
            });

            foreach (var cuttingInItem in cuttingInDetailToBeUpdated)
            {
                var garmentCuttingInDetail = _garmentCuttingInDetailRepository.Query.Where(x => x.Identity == cuttingInItem.Key).Select(s => new GarmentCuttingInDetail(s)).Single();
                garmentCuttingInDetail.SetRemainingQuantity(garmentCuttingInDetail.RemainingQuantity + cuttingInItem.Value);
                garmentCuttingInDetail.Modify();
                await _garmentCuttingInDetailRepository.Update(garmentCuttingInDetail);
            }

            foreach (var subconCutting in cuttingSubconToBeUpdated)
            {
                var RONo         = subconCutting.Key.Split("~")[0];
                var SizeId       = subconCutting.Key.Split("~")[1];
                var SizeName     = subconCutting.Key.Split("~")[2];
                var ProductId    = subconCutting.Key.Split("~")[3];
                var ProductCode  = subconCutting.Key.Split("~")[4];
                var ProductName  = subconCutting.Key.Split("~")[5];
                var ComodityId   = subconCutting.Key.Split("~")[6];
                var ComodityCode = subconCutting.Key.Split("~")[7];
                var ComodityName = subconCutting.Key.Split("~")[8];
                var designColor  = subconCutting.Key.Split("~")[9];
                var remark       = subconCutting.Key.Split("~")[10];
                var basicPrice   = subconCutting.Key.Split("~")[11];

                GarmentSubconCutting garmentSubconCutting = _garmentSubconCuttingRepository.Query.Where(a => a.RONo == RONo && a.SizeId == Convert.ToInt32(SizeId) && a.ComodityId == Convert.ToInt32(ComodityId) && a.ProductId == Convert.ToInt32(ProductId) && a.Remark == remark && a.DesignColor == designColor && a.BasicPrice == Convert.ToDouble(basicPrice)).Select(a => new GarmentSubconCutting(a)).FirstOrDefault();

                garmentSubconCutting.SetQuantity(garmentSubconCutting.Quantity - subconCutting.Value);
                garmentSubconCutting.Modify();
                await _garmentSubconCuttingRepository.Update(garmentSubconCutting);

                foreach (var detailId in cuttingSubconToBeUpdatedId[subconCutting.Key] ?? new List <Guid>())
                {
                    GarmentSubconCuttingRelation garmentSubconCuttingRelation = _garmentSubconCuttingRelationRepository.Query.Where(w => w.GarmentSubconCuttingId == garmentSubconCutting.Identity && w.GarmentCuttingOutDetailId == detailId).Select(s => new GarmentSubconCuttingRelation(s)).FirstOrDefault();
                    if (garmentSubconCuttingRelation != null)
                    {
                        garmentSubconCuttingRelation.Remove();
                        await _garmentSubconCuttingRelationRepository.Update(garmentSubconCuttingRelation);
                    }
                }
            }

            cutOut.Remove();
            await _garmentCuttingOutRepository.Update(cutOut);

            _storage.Save();

            return(cutOut);
        }