public async Task <IActionResult> CreateProductionOrder(ProdOrderHeaderForDetailDto prodOrderHeaderForDetailDto)
        {
            if (prodOrderHeaderForDetailDto == null)
            {
                return(BadRequest("Empty Body"));
            }

            var prodOrderHeaderToCreate = _mapper.Map <ProductionOrderHeader>(prodOrderHeaderForDetailDto);

            prodOrderHeaderToCreate.BusinessPlace = await _repository.Get <BusinessPlace>(prodOrderHeaderForDetailDto.BusinessPlaceId);

            prodOrderHeaderToCreate.Session = await _repository.Get <ProductionSession>(prodOrderHeaderForDetailDto.SessionId);

            prodOrderHeaderToCreate.User = await _repository.Get <User>(prodOrderHeaderForDetailDto.UserId);

            await _repository.CreateProductionOrder(prodOrderHeaderToCreate);

            if (await _repository.SaveAll())
            {
                var prodOrderHeaderToReturn = _mapper.Map <ProdOrderHeaderForDetailDto>(prodOrderHeaderToCreate);
                return(CreatedAtRoute(nameof(GetProductionOrder), new { prodOrderHeaderToCreate.Id }, prodOrderHeaderToReturn));
            }

            return(BadRequest("Could not create production Order"));
        }
        public async Task <IActionResult> CreateIngredient(IngredientHeaderForDetailDto IngredientHeaderForDetailDto)
        {
            if (IngredientHeaderForDetailDto == null)
            {
                return(BadRequest(new ErrorModel(1, 400, "Empty Body")));
            }

            var item = await _context.IngredientHeaders.FirstOrDefaultAsync(a => a.ItemId == IngredientHeaderForDetailDto.ItemId);

            if (item != null)
            {
                return(BadRequest(new ErrorModel(4, 400, "Recipe For Item Already exist")));
            }

            var IngredientHeaderToCreate = _mapper.Map <IngredientHeader>(IngredientHeaderForDetailDto);

            await _repository.CreateIngredient(IngredientHeaderToCreate);

            if (await _repository.SaveAll())
            {
                var IngredientHeaderToReturn = _mapper.Map <IngredientHeaderForDetailDto>(IngredientHeaderToCreate);
                return(CreatedAtRoute(nameof(GetIngredient), new { IngredientHeaderToCreate.Id }, IngredientHeaderToReturn));
            }

            return(BadRequest(new ErrorModel(2, 400, "Could not create Ingredient")));
        }
        public async Task <IActionResult> CreateProductionPlan(ProdPlanHeaderForDetailDto ProdPlanHeaderForDetailDto)
        {
            if (ProdPlanHeaderForDetailDto == null)
            {
                return(BadRequest(new ErrorModel(1, 400, "Empty Body")));
            }
            if (ProdPlanHeaderForDetailDto.ProdOrdrIds == null)
            {
                return(BadRequest(new ErrorModel(2, 400, "Production orders needed")));
            }

            if (ProdPlanHeaderForDetailDto.ProductionPlanDetails == null ||
                ProdPlanHeaderForDetailDto.ProductionPlanRecipes == null ||
                ProdPlanHeaderForDetailDto.ProductionPlanWorkers == null ||
                ProdPlanHeaderForDetailDto.ProductionPlanMachines == null)
            {
                return(BadRequest(new ErrorModel(3, 400, "all nested details required")));
            }

            var planHeaderToCreate = _mapper.Map <ProductionPlanHeader>(ProdPlanHeaderForDetailDto);

            planHeaderToCreate.BusinessPlace = await _repository.Get <BusinessPlace>(ProdPlanHeaderForDetailDto.BusinessPlaceId);

            planHeaderToCreate.ProductionSession = await _repository.Get <ProductionSession>(ProdPlanHeaderForDetailDto.ProductionSessionId);

            planHeaderToCreate.User = await _repository.Get <User>(ProdPlanHeaderForDetailDto.UserId);



            await _repository.CreateProductionPlan(planHeaderToCreate);

            if (await _repository.SaveAll())
            {
                foreach (var prodOrderId in ProdPlanHeaderForDetailDto.ProdOrdrIds)
                {
                    var prodOrder = await _repository.GetProductionOrder(prodOrderId);

                    prodOrder.IsNotEditable = true;
                    prodOrder.PlanId        = planHeaderToCreate.Id;
                    prodOrder.isProcessed   = 0;
                }

                if (await _repository.SaveAll())
                {
                    var planHeaderToReturn = _mapper.Map <ProdPlanHeaderForDetailDto>(planHeaderToCreate);
                    return(CreatedAtRoute(nameof(GetProductionPlan), new { planHeaderToCreate.Id }, planHeaderToReturn));
                }
                return(BadRequest(new ErrorModel(4, 400, "created production plan, but some error occured")));
            }
            return(BadRequest(new ErrorModel(4, 400, "Could not create production plan")));
        }