public RequestResult EditDispatch([FromBody] EditDispatch editDispatch, bool isCMR)
        {
            if (UserHelper.IsAuthorize(new List <int> {
                (int)UserType.SuperAdmin
            }))
            {
                Dispatch      dispatchToEdit = _context.Dispatches.FirstOrDefault(o => o.Id == editDispatch.Id && o.Deleted_At == null);
                DateTime      dateOfEdit     = DateTime.Now;
                RequestResult result         = new RequestResult();
                try
                {
                    int            CMRId       = Convert.ToInt32(dispatchToEdit.CMR_Id);
                    CMR_Dispatches CMRDispatch = _context.CMR_Dispatches.FirstOrDefault(c => c.Id == CMRId);
                    if (isCMR)
                    {
                        if (CMRDispatch != null)
                        {
                            CMRDispatch.Edited_At           = dateOfEdit;
                            CMRDispatch.Destination         = editDispatch.CMRDispatch.Destination;
                            CMRDispatch.Sender_Address      = editDispatch.CMRDispatch.Sender_Address;
                            CMRDispatch.Sender_Email        = editDispatch.CMRDispatch.Sender_Email;
                            CMRDispatch.Sender_Name         = editDispatch.CMRDispatch.Sender_Name;
                            CMRDispatch.Sender_PrefixVat_Id = editDispatch.CMRDispatch.Sender_PrefixVat_Id;
                            CMRDispatch.Sender_VAT_Id       = editDispatch.CMRDispatch.Sender_VAT_Id.RemoveWhiteSpaces();
                            _context.SaveChanges();
                        }
                        else
                        {
                            CMRDispatch                     = new CMR_Dispatches();
                            CMRDispatch.Created_At          = dateOfEdit;
                            CMRDispatch.Destination         = editDispatch.CMRDispatch.Destination;
                            CMRDispatch.Dispatch_Id         = 0;
                            CMRDispatch.Sender_Address      = editDispatch.CMRDispatch.Sender_Address;
                            CMRDispatch.Sender_Email        = editDispatch.CMRDispatch.Sender_Email;
                            CMRDispatch.Sender_Name         = editDispatch.CMRDispatch.Sender_Name;
                            CMRDispatch.Sender_PrefixVat_Id = editDispatch.CMRDispatch.Sender_PrefixVat_Id;
                            CMRDispatch.Sender_VAT_Id       = editDispatch.CMRDispatch.Sender_VAT_Id.RemoveWhiteSpaces();
                            _context.CMR_Dispatches.Add(CMRDispatch);
                            _context.SaveChanges();
                        }
                    }
                    else
                    {
                        if (CMRDispatch != null)
                        {
                            CMRDispatch.Deleted_At = dateOfEdit;
                            _context.SaveChanges();
                        }
                    }

                    if (dispatchToEdit != null)
                    {
                        dispatchToEdit.Carrier_Address      = editDispatch.Carrier.Carrier_Address;
                        dispatchToEdit.Carrier_Email        = editDispatch.Carrier.Carrier_Email;
                        dispatchToEdit.Carrier_Name         = editDispatch.Carrier.Carrier_Name;
                        dispatchToEdit.Carrier_PrefixVat_Id = editDispatch.Carrier.Carrier_PrefixVat_Id;
                        dispatchToEdit.Carrier_VAT_Id       = editDispatch.Carrier.Carrier_VAT_Id.RemoveWhiteSpaces();
                        dispatchToEdit.Carrier_PhoneNumber  = editDispatch.Carrier.Carrier_PhoneNumber.RemoveWhiteSpaces();
                        dispatchToEdit.Car_Id                = editDispatch.Car_Id.RemoveWhiteSpaces();
                        dispatchToEdit.Creation_Date         = editDispatch.Creation_Date;
                        dispatchToEdit.Dispatch_Number       = editDispatch.Dispatch_Number;
                        dispatchToEdit.Receiver_Address      = editDispatch.Receiver.Receiver_Address;
                        dispatchToEdit.Receiver_Email        = editDispatch.Receiver.Receiver_Email;
                        dispatchToEdit.Receiver_Name         = editDispatch.Receiver.Receiver_Name;
                        dispatchToEdit.Receiver_PrefixVat_Id = editDispatch.Receiver.Receiver_PrefixVat_Id;
                        dispatchToEdit.Receiver_VAT_Id       = editDispatch.Receiver.Receiver_VAT_Id.RemoveWhiteSpaces();
                        dispatchToEdit.Receiver_PhoneNumber  = editDispatch.Receiver.Receiver_PhoneNumber.RemoveWhiteSpaces();
                        dispatchToEdit.Edited_At             = dateOfEdit;
                        dispatchToEdit.If_CMR                = isCMR;
                        dispatchToEdit.Duty_Doc_Id           = editDispatch.Duty_Doc_Id;
                        dispatchToEdit.CMR_Id                = CMRDispatch == null ? null : CMRDispatch.Id.ToString();

                        _context.SaveChanges();
                        var dispatchPositionsFromDB = _context.Dispatches_Positions.Where(d => d.Dispatch_Id == editDispatch.Id && d.Deleted_At == null).ToList();

                        //Editing and adding orderPositions
                        foreach (var dispatchPosition in editDispatch.DispatchPositions)
                        {
                            Dispatches_Positions dispatchPositionForParameter = _context.Dispatches_Positions
                                                                                .FirstOrDefault(d => d.Order_Position_Id == dispatchPosition.Id && d.Dispatch_Id == editDispatch.Id && d.Deleted_At == null);

                            //foreach (var dispatchPositionFromDB in dispatchPositionsFromDB)
                            //{
                            //if (dispatchPosition.Id == dispatchPositionFromDB.Order_Position_Id)
                            //{
                            //Dispatches_Positions dispatchPositionForParameter = _context.Dispatches_Positions.FirstOrDefault(d => d.Order_Position_Id == dispatchPosition.Id && d.Dispatch_Id == editDispatch.Id && d.Deleted_At == null);
                            if (dispatchPositionForParameter != null)
                            {
                                Orders_Positions orderPosition = _context.Orders_Positions.FirstOrDefault(o => o.Id == dispatchPositionForParameter.Order_Position_Id && o.Deleted_At == null);
                                dispatchPositionForParameter.Amount       = dispatchPosition.Amount;
                                dispatchPositionForParameter.Edited_At    = dateOfEdit;
                                dispatchPositionForParameter.Weight_Gross = Math.Round((decimal)(dispatchPosition.Amount * orderPosition.Unit_Weight), 2);
                                _context.SaveChanges();
                                Delivery delivery = _context.Deliveries.FirstOrDefault(d => d.Order_Id == orderPosition.Order_id && d.Deleted_At == null);
                                List <Orders_Positions> listOfOrdersPositionsForOrderPosition = _context.Orders_Positions.Where(o => o.Order_id == orderPosition.Order_id && o.Deleted_At == null).ToList();
                                bool isBalanced = true;
                                foreach (var orderPositionForOrder in listOfOrdersPositionsForOrderPosition)
                                {
                                    List <Dispatches_Positions> dispatchPositions = _context.Dispatches_Positions.Where(d => d.Order_Position_Id == orderPositionForOrder.Id && d.Deleted_At == null).ToList();
                                    if (orderPositionForOrder.Amount_Received != dispatchPositions.Sum(d => d.Amount))
                                    {
                                        isBalanced = false;
                                    }
                                }
                                if (isBalanced)
                                {
                                    delivery.If_Delivery_Dispatch_Balanced = true;
                                }
                                _context.SaveChanges();
                            }
                            else
                            {
                                Orders_Positions orderPosition = _context.Orders_Positions.FirstOrDefault(o => o.Id == dispatchPosition.Id && o.Deleted_At == null);

                                if (orderPosition == null)
                                {
                                    throw new Exception(string.Format("Order position (id = {0}) not exist in database", dispatchPosition.Id));
                                }

                                Dispatches_Positions dispatchPostionToAdd = new Dispatches_Positions();
                                dispatchPostionToAdd.Amount            = dispatchPosition.Amount;
                                dispatchPostionToAdd.Created_At        = dateOfEdit;
                                dispatchPostionToAdd.Dispatch_Id       = editDispatch.Id;
                                dispatchPostionToAdd.Order_Position_Id = dispatchPosition.Id;
                                dispatchPostionToAdd.Weight_Gross      = Math.Round((decimal)(dispatchPosition.Amount * orderPosition.Unit_Weight), 2);
                                _context.Dispatches_Positions.Add(dispatchPostionToAdd);
                                _context.SaveChanges();


                                Delivery delivery = _context.Deliveries.FirstOrDefault(d => d.Order_Id == orderPosition.Order_id && d.Deleted_At == null);

                                if (delivery == null)
                                {
                                    throw new Exception(string.Format("Delivery not exist in database for order (id = {0})", orderPosition.Order_id));
                                }

                                if (_context.Deliveries_Dispatches.FirstOrDefault(d => d.Delivery_Id == delivery.Id && d.Dispatch_Id == dispatchPostionToAdd.Id && d.Deleted_At == null) == null)
                                {
                                    Deliveries_Dispatches deliveryDispatch = new Deliveries_Dispatches();
                                    deliveryDispatch.Created_At  = dateOfEdit;
                                    deliveryDispatch.Dispatch_Id = (int)dispatchPostionToAdd.Dispatch_Id;
                                    deliveryDispatch.Delivery_Id = delivery.Id;
                                    _context.Deliveries_Dispatches.Add(deliveryDispatch);
                                    _context.SaveChanges();
                                }

                                List <Orders_Positions> listOfOrdersPositionsForOrderPosition = _context.Orders_Positions.Where(o => o.Order_id == orderPosition.Order_id && o.Deleted_At == null).ToList();
                                bool isBalanced = true;
                                foreach (var orderPositionForOrder in listOfOrdersPositionsForOrderPosition)
                                {
                                    List <Dispatches_Positions> dispatchPositions = _context.Dispatches_Positions.Where(d => d.Order_Position_Id == orderPositionForOrder.Id && d.Deleted_At == null).ToList();
                                    if (orderPositionForOrder.Amount_Received != dispatchPositions.Sum(d => d.Amount))
                                    {
                                        isBalanced = false;
                                    }
                                }
                                if (isBalanced)
                                {
                                    delivery.If_Delivery_Dispatch_Balanced = true;
                                }
                            }


                            //}
                            //if (dispatchPosition.Id == null)//jak dispatchPositionId jest nullem to tworze nowy na podstawie OrderId
                            //{
                            //    if (dispatchPosition.OrderId != null)
                            //    {

                            //    }

                            //}

                            //}
                        }
                        _context.SaveChanges();

                        //Deleting orderPosition
                        List <int> listOfIdsToDelete = _dispatchManager.GetIdstoRemove(editDispatch.DispatchPositions, dispatchPositionsFromDB);
                        foreach (var id in listOfIdsToDelete)
                        {
                            var dispatchPositionToDelete = _context.Dispatches_Positions.FirstOrDefault(d => d.Order_Position_Id == id && d.Dispatch_Id == editDispatch.Id && d.Deleted_At == null);
                            dispatchPositionToDelete.Deleted_At = dateOfEdit;
                            //Dispatches_Positions dispatchPositionForParameter = _context.Dispatches_Positions.FirstOrDefault(d => d.Id == id && d.Deleted_At == null);
                            Orders_Positions orderPosition = _context.Orders_Positions.FirstOrDefault(o => o.Id == dispatchPositionToDelete.Order_Position_Id && o.Deleted_At == null);
                            Delivery         delivery      = _context.Deliveries.FirstOrDefault(d => d.Order_Id == orderPosition.Order_id && d.Deleted_At == null);
                            delivery.If_Delivery_Dispatch_Balanced = false;
                            _context.SaveChanges();


                            //sprawdzam czy jakiś orderPOsition z delivery dla tego dispatchPositions należy do tego dispatcha
                            bool       isDeliveryDispatch           = false;
                            List <int> orderPositionsForDeliveryIds = _context.Orders_Positions.Where(o => o.Order_id == delivery.Order_Id && o.Deleted_At == null).Select(o => o.Id).ToList();
                            List <int> listOfdispatchPositionsIds   = _context.Dispatches_Positions.Where(d => d.Dispatch_Id == editDispatch.Id && d.Deleted_At == null).Select(d => d.Id).ToList();
                            foreach (var item in orderPositionsForDeliveryIds)
                            {
                                if (listOfdispatchPositionsIds.Contains(item))
                                {
                                    isDeliveryDispatch = true;
                                }
                            }

                            if (!isDeliveryDispatch)
                            {
                                Deliveries_Dispatches deliveryDispatch = _context.Deliveries_Dispatches.FirstOrDefault(d => d.Delivery_Id == delivery.Id && d.Dispatch_Id == editDispatch.Id && d.Deleted_At == null);
                                deliveryDispatch.Deleted_At = dateOfEdit;
                                _context.SaveChanges();
                            }
                        }

                        _context.SaveChanges();
                        dispatchToEdit.Number_Of_Positions = _context.Dispatches_Positions.Where(d => d.Dispatch_Id == editDispatch.Id && d.Deleted_At == null).Count();
                        _context.SaveChanges();
                    }
                    else
                    {
                        result.Status  = false;
                        result.Message = "Dispatch not found";
                    }
                    result.Status  = true;
                    result.Message = "Order has been edited";
                }
                catch (Exception ex)
                {
                    result.Status  = false;
                    result.Message = ex.ToString();
                }
                return(result);
            }
            else
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "User don't have acces to this method"));
            }
        }