Esempio n. 1
0
        public async Task <IActionResult> PutFurnitures(int id, Furnitures furnitures)
        {
            if (id != furnitures.FurnitureId)
            {
                return(BadRequest());
            }

            _context.Entry(furnitures).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FurnituresExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Esempio n. 2
0
        public async Task <ActionResult <Orders> > PostOrders([FromBody] OrderRequestDto orderDto)
        {
            var mapper = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <DeliveryInfoDto, DeliveryInfos>();

                cfg.CreateMap <CustomerDto, Customers>();

                cfg.CreateMap <AdditionalDetailsOrderedDto, AdditionalDetailsOrdered>();

                cfg.CreateMap <FurnitureOrderRowDto, FurnitureOrderRows>();

                cfg.CreateMap <OrderRequestDto, Orders>()
                .ForMember(o => o.OrderDate, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(o => o.Status, opt => opt.MapFrom(_ => OrderStatus.Accepted))
                .ForMember(o => o.FurnitureOrderRows, opt => opt.MapFrom(o => o.OrderedFurnitures))
                .ForPath(o => o.Profit.Money, opt => opt.MapFrom(o => o.TotalPrice / 6));
            }).CreateMapper();

            var order = mapper.Map <Orders>(orderDto);

            _context.Orders.Add(order);

            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetOrders", new { id = order.OrderId }, order));
        }
        public async Task <IActionResult> PostOrder(InDetailOrderDto order)
        {
            var detailOrder = _mapper.Map <DetailOrders>(order);

            detailOrder.Date = DateTime.UtcNow;

            detailOrder.DetailOrderRows = order.OrderRows.Select(or => _mapper.Map <DetailOrderRows>(or)).ToList();

            _context.DetailOrders.Add(detailOrder);

            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetOrderRows), new { id = order.DetailOrderId }, detailOrder));
        }
        public async Task <ActionResult <Details> > PutDetails(Details details)
        {
            var detail = await _context.Details.FirstOrDefaultAsync(d => d.DetailId == details.DetailId);

            if (detail == null)
            {
                return(NotFound());
            }

            _context.Entry(detail).State = EntityState.Modified;

            await _context.SaveChangesAsync();

            return(Ok(detail));
        }
Esempio n. 5
0
        public async Task <IActionResult> PutWarehouseDetails([FromBody] MovementDto[] movementDtos)
        {
            foreach (var movementDto in movementDtos)
            {
                var fromWarehouseDetail = await _context.WarehouseDetails.FirstOrDefaultAsync(wd => wd.WarehouseDetailId == movementDto.FromWarehouseDetailId);

                fromWarehouseDetail.Count -= movementDto.Count;

                var toWarehouseDetail = await _context.WarehouseDetails
                                        .Where(wd => wd.WarehouseId == movementDto.ToWarehouseId)
                                        .FirstOrDefaultAsync(wd => wd.DetailId == movementDto.DetailId);

                if (toWarehouseDetail == null)
                {
                    toWarehouseDetail = new WarehouseDetails()
                    {
                        DetailId = movementDto.DetailId,

                        WarehouseId = movementDto.ToWarehouseId,

                        Count = movementDto.Count
                    };
                }
                else
                {
                    toWarehouseDetail.Count += movementDto.Count;
                }

                await _context.WarehouseMovements.AddAsync(new WarehouseMovements()
                {
                    FromWarehouseDetailId = fromWarehouseDetail.WarehouseDetailId,

                    ToWarehouseDetail = toWarehouseDetail,

                    Date = DateTime.UtcNow,

                    WorkerId = movementDto.WorkerId,

                    Count = movementDto.Count
                });
            }

            await _context.SaveChangesAsync();

            return(NoContent());
        }
        public async Task <IActionResult> PostWorker([FromBody] InWorkerDto workerDto)
        {
            var w = await _context.Workers.FirstOrDefaultAsync(w => w.Email == workerDto.Email);

            if (w != null)
            {
                return(BadRequest());
            }

            var worker = _mapper.Map <Workers>(workerDto);

            worker.Status = "Вільний";

            var post = await _context.Posts.FirstOrDefaultAsync(p => p.Name == workerDto.PostName);

            worker.PostId = post.PostId;

            await _context.AddAsync(worker);

            await _context.SaveChangesAsync();

            return(NoContent());
        }
        public async Task <ActionResult <Orders> > StartProduction(int id, int warehouseId)
        {
            var order = await _context.Orders
                        .Where(o => o.OrderId == id)
                        .Include(o => o.FurnitureOrderRows)
                        .ThenInclude(r => r.Furniture)
                        .ThenInclude(f => f.DetailsInFurnitures)
                        .ThenInclude(r => r.AdditionalDetailsOrdered)
                        .ThenInclude(a => a.DetailInFurniture)
                        .FirstOrDefaultAsync();

            if (order == null)
            {
                return(NotFound());
            }

            foreach (var row in order.FurnitureOrderRows)
            {
                //check if enough details
                var furnitureDetails = row.Furniture.DetailsInFurnitures
                                       .Select(d => new { d.DetailId, Count = d.Count * row.Count })
                                       .ToDictionary(k => k.DetailId);
                var additionalDetails = row.AdditionalDetailsOrdered
                                        .Select(d => new { d.DetailInFurniture.DetailId, Count = d.Count * row.Count })
                                        .ToDictionary(k => k.DetailId);
                var neededDetails = furnitureDetails.Concat(additionalDetails).GroupBy(d => d.Key)
                                    .ToDictionary(d => d.Key,
                                                  d => new
                {
                    d.First().Value.DetailId,
                    Count = d.Sum(d => d.Value.Count) as int?
                });

                var warehouseDetails = await _context.WarehouseDetails
                                       .Where(w => w.WarehouseId == warehouseId)
                                       .Select(wd => new { wd.DetailId, wd.Count })
                                       .ToDictionaryAsync(k => k.DetailId as int?);

                var missingDetails = new Dictionary <int, int>();

                foreach (var detail in neededDetails)
                {
                    if (!warehouseDetails.ContainsKey(detail.Key) ||
                        warehouseDetails[detail.Key].Count < detail.Value.Count)
                    {
                        //not enough
                        missingDetails.Add((int)detail.Key,
                                           (int)(detail.Value.Count - warehouseDetails[detail.Key].Count));
                    }
                    warehouseDetails[detail.Key] = new
                    {
                        DetailId = (int)detail.Value.DetailId,
                        Count    = warehouseDetails[detail.Key].Count - detail.Value.Count
                    };

                    //remove used details from warehouse
                    var wd = await _context.WarehouseDetails
                             .Where(wd => wd.DetailId == detail.Value.DetailId)
                             .FirstOrDefaultAsync();

                    wd.Count = warehouseDetails[detail.Key].Count;
                    _context.WarehouseDetails.Update(wd);
                }

                if (missingDetails.Any())
                {
                    return(BadRequest(missingDetails));
                }

                //add production
                var production = new Productions()
                {
                    WarehouseId         = warehouseId,
                    StartedDate         = DateTime.Now,
                    FurnitureOrderRowId = row.FurnitureOrderRowId,
                };

                //add consumptions
                foreach (var detail in neededDetails)
                {
                    var consumption = new Consumptions()
                    {
                        Date            = DateTime.Now,
                        WarehouseDetail = await _context.WarehouseDetails
                                          .Where(wd => wd.DetailId == detail.Value.DetailId)
                                          .FirstOrDefaultAsync(),
                        Count = (int)detail.Value.Count
                    };
                    production.Consumptions.Add(consumption);
                }

                _context.Productions.Add(production);
            }

            //change order status
            order.Status = OrderStatus.InProduction;
            _context.Orders.Update(order);

            await _context.SaveChangesAsync();

            return(order);
        }