public IActionResult BookCustomerOrderItems(uint id, [FromBody] OrderItemBookingDto orderItem)
        {
            if (orderItem == null)
            {
                return(StatusCode(400));
            }
            if (!ModelState.IsValid)
            {
                return(new InvalidInputResponse(ModelState));
            }

            var salesOrder = _salesOrderQuery.GetSalesOrderById(id);

            if (salesOrder == null)
            {
                return(StatusCode(404, $"Customer Order with id {id} Not Found"));
            }

            foreach (var item in orderItem.Items)
            {
                var customerOrderItem = _salesOrderQuery.GetSalesOrderItemById(item.CustomerOrderItemId);
                if (customerOrderItem == null)
                {
                    return(StatusCode(404, $"Customer Order Item with id {item.CustomerOrderItemId} Not Found"));
                }
                var availableStock = _bookingQuery.AvailableStockItemsForOrder((int)item.Quantity, item.CustomerOrderItemId);
                if (availableStock.Count != 0)
                {
                    var bookedItems = _bookingFactory.CreateBooking(availableStock, item.CustomerOrderItemId, orderItem.BookedBy);
                    var booked      = _bookingCommand.BookAvailableStockItems(bookedItems);
                }
                //TODO : replace calling workorder controller directly

                if (availableStock.Count < item.Quantity && orderItem.CreateManufactureOrder == 1)
                {
                    var workDto = _workOrderFactory.CreateCustomerOrderDto(
                        item.CustomerOrderItemId,
                        customerOrderItem.ItemId,
                        (uint)(item.Quantity - availableStock.Count),
                        (DateTime)orderItem.WorkEndDate,
                        (DateTime)orderItem.WorkEndDate,
                        orderItem.BookedBy,
                        "Created From Factory"
                        );
                    var workResult = _workOrderController.CreateNewWorkWorder(workDto);

                    if (workResult.GetType() == typeof(InvalidInputResponse))
                    {
                        return(workResult);
                    }
                }
            }

            return(StatusCode(201));
        }
        public IActionResult CreateNewWorkWorder([FromBody] NewWorkOrderDto newWork)
        {
            if (newWork == null)
            {
                return(StatusCode(400));
            }

            if (!ModelState.IsValid)
            {
                return(new InvalidInputResponse(ModelState));
            }

            var employee = _employeeQuery.GetEmployeeById(newWork.OrderedBy);

            if (employee == null)
            {
                return(StatusCode(404, "Employee Record Not Found"));
            }

            if (newWork.PurchaseOrderItemId != 0)
            {
                var salesOrder = _salesQuery.GetSalesOrderItemById((uint)newWork.PurchaseOrderItemId);
                if (salesOrder == null)
                {
                    return(StatusCode(404, $"Sales Order with id {newWork.PurchaseOrderItemId} Not Found"));
                }

                if (_query.saleOrderProductionExits((uint)newWork.PurchaseOrderItemId))
                {
                    ModelState.AddModelError("PurchaseOrderId", $"Sale Order With Id : {newWork.PurchaseOrderItemId} already has a manufacturing Order");
                    return(new InvalidInputResponse(ModelState));
                }

                newWork.Quantity = (newWork.Quantity > salesOrder.Quantity) ? salesOrder.Quantity : newWork.Quantity;
            }

            var workorder = _factory.CreateNewWorkOrder(newWork);

            var result = _command.CreateNewWorkOrder(workorder);

            if (result != null)
            {
                var workOrderView = _factory.CreateWorkOrderView(result);
                return(StatusCode(201, workOrderView));
            }
            else
            {
                return(StatusCode(500, "Server error Try Again"));
            }
        }