public List <DeliveryState> GetDeliveryState(int orderId, int dispatchId = 0) { if (UserHelper.IsAuthorize(new List <int> { (int)UserType.SuperAdmin, (int)UserType.Admin, (int)UserType.Client })) { try { List <DeliveryState> result = new List <DeliveryState>(); Delivery deliveryFromDB = _context.Deliveries.FirstOrDefault(d => d.Order_Id == orderId && d.Deleted_At == null); if (deliveryFromDB != null) { List <int?> listOfDispatchesPositionsOrderPositionsIds = new List <int?>(); List <Orders_Positions> orderPositionFromDB = _context.Orders_Positions.Where(o => o.Order_id == orderId && o.Deleted_At == null).ToList(); if (dispatchId != 0) { listOfDispatchesPositionsOrderPositionsIds = _context.Dispatches_Positions.Where(d => d.Dispatch_Id == dispatchId && d.Deleted_At == null).Select(d => d.Order_Position_Id).ToList(); } foreach (var orderPosition in orderPositionFromDB) { DeliveryState deliveryState = new DeliveryState(); List <Dispatches_Positions> dispatchPositionsfromDB = _context.Dispatches_Positions.Where(d => d.Order_Position_Id == orderPosition.Id && d.Deleted_At == null).ToList(); int dispatchedAmount = dispatchPositionsfromDB == null ? 0 : (dispatchPositionsfromDB.Sum(d => d.Amount) ?? 0); decimal dispatchedWeight = dispatchPositionsfromDB == null ? 0 : (dispatchPositionsfromDB.Sum(d => d.Weight_Gross) ?? 0); deliveryState.Id = orderPosition.Id; deliveryState.Name = orderPosition.Name; deliveryState.Amount = (int)orderPosition.Amount_Received - dispatchedAmount; if (listOfDispatchesPositionsOrderPositionsIds != null && listOfDispatchesPositionsOrderPositionsIds.Contains(orderPosition.Id)) { Dispatches_Positions dispatchPositionsToAdd = _context.Dispatches_Positions.FirstOrDefault(d => d.Order_Position_Id == orderPosition.Id && d.Deleted_At == null); deliveryState.Amount += (int)dispatchPositionsToAdd.Amount; } if (deliveryState.Amount > 0) { result.Add(deliveryState); } } return(result); } else { throw new Exception("Not found delivery for this order id."); // throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Not found delivery for this order id.")); } } catch (Exception ex) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } } else { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "User don't have acces to this method")); } }
public RequestResult CreateDispatch([FromBody] CreateDispatch newDispatch, bool isCMR) { if (UserHelper.IsAuthorize(new List <int> { (int)UserType.SuperAdmin, (int)UserType.Admin })) { RequestResult result = new RequestResult(); try { if (_context.Orders.OrderByDescending(o => o.Created_At).FirstOrDefault() == null || _context.Orders.OrderByDescending(o => o.Created_At).FirstOrDefault().Created_At.Value.Month != DateTime.Now.Month) { var counter = _context.Counters.FirstOrDefault(c => c.Name == "DispatchCounter"); counter.Count = 1; _context.SaveChanges(); } DateTime dateOfCreate = DateTime.Now; Dispatch dispatchToAdd = new Dispatch(); CMR_Dispatches cmrDispatch = new CMR_Dispatches(); dispatchToAdd.Carrier_Address = newDispatch.Carrier.Carrier_Address; dispatchToAdd.Carrier_Email = newDispatch.Carrier.Carrier_Email; dispatchToAdd.Carrier_Name = newDispatch.Carrier.Carrier_Name; dispatchToAdd.Carrier_PrefixVat_Id = newDispatch.Carrier.Carrier_PrefixVat_Id; dispatchToAdd.Carrier_VAT_Id = newDispatch.Carrier.Carrier_VAT_Id.RemoveWhiteSpaces(); dispatchToAdd.Carrier_PhoneNumber = newDispatch.Carrier.Carrier_PhoneNumber.RemoveWhiteSpaces(); dispatchToAdd.Car_Id = newDispatch.Car_Id.RemoveWhiteSpaces(); dispatchToAdd.Creation_Date = dateOfCreate; dispatchToAdd.Dispatch_Number = _context.Counters.FirstOrDefault(c => c.Name == "DispatchCounter").Count.ToString() + "/" + ((DateTime)dispatchToAdd.Creation_Date).Month.ToString() + "/" + ((DateTime)dispatchToAdd.Creation_Date).Year.ToString(); dispatchToAdd.Receiver_Address = newDispatch.Receiver.Receiver_Address; dispatchToAdd.Receiver_Email = newDispatch.Receiver.Receiver_Email; dispatchToAdd.Receiver_Name = newDispatch.Receiver.Receiver_Name; dispatchToAdd.Receiver_PrefixVat_Id = newDispatch.Receiver.Receiver_PrefixVat_Id; dispatchToAdd.Receiver_VAT_Id = newDispatch.Receiver.Receiver_VAT_Id.RemoveWhiteSpaces(); dispatchToAdd.Receiver_PhoneNumber = newDispatch.Receiver.Receiver_PhoneNumber.RemoveWhiteSpaces(); dispatchToAdd.Created_At = dateOfCreate; if (isCMR) { cmrDispatch.Created_At = dateOfCreate; cmrDispatch.Destination = newDispatch.CMRDispatch.Destination; cmrDispatch.Dispatch_Id = 0; cmrDispatch.Sender_Address = newDispatch.CMRDispatch.Sender_Address; cmrDispatch.Sender_Email = newDispatch.CMRDispatch.Sender_Email; cmrDispatch.Sender_Name = newDispatch.CMRDispatch.Sender_Name; cmrDispatch.Sender_PrefixVat_Id = newDispatch.CMRDispatch.Sender_PrefixVat_Id; cmrDispatch.Sender_VAT_Id = newDispatch.CMRDispatch.Sender_VAT_Id.RemoveWhiteSpaces(); cmrDispatch.Dispatch_Id = dispatchToAdd.Id; cmrDispatch.Commodity_Type = newDispatch.CMRDispatch.Commodity_Type; _context.CMR_Dispatches.Add(cmrDispatch); _context.SaveChanges(); dispatchToAdd.CMR_Id = cmrDispatch.Id == null ? null : cmrDispatch.Id.ToString(); _context.SaveChanges(); } else { dispatchToAdd.CMR_Id = null; } dispatchToAdd.Number_Of_Positions = newDispatch.DispatchPositions.Count; dispatchToAdd.If_PDF_And_Sent = false; dispatchToAdd.If_CMR_And_Sent = false; dispatchToAdd.If_CMR = isCMR; dispatchToAdd.Duty_Doc_Id = newDispatch.Duty_Doc_Id; dispatchToAdd.Creator_Id = UserHelper.GetCurrentUserId(); _context.Dispatches.Add(dispatchToAdd); _context.SaveChanges(); foreach (var item in newDispatch.DispatchPositions) { Orders_Positions orderPosition = _context.Orders_Positions.FirstOrDefault(o => o.Id == item.Id && o.Deleted_At == null); Dispatches_Positions dispatchPostion = new Dispatches_Positions(); dispatchPostion.Amount = item.Amount; dispatchPostion.Created_At = dateOfCreate; dispatchPostion.Dispatch_Id = dispatchToAdd.Id; dispatchPostion.Order_Position_Id = item.Id; dispatchPostion.Weight_Gross = Math.Round((decimal)(item.Amount * orderPosition.Unit_Weight), 2); _context.Dispatches_Positions.Add(dispatchPostion); _context.SaveChanges(); Delivery delivery = _context.Deliveries.FirstOrDefault(d => d.Order_Id == orderPosition.Order_id && d.Deleted_At == null); if (_context.Deliveries_Dispatches.FirstOrDefault(d => d.Delivery_Id == delivery.Id && d.Dispatch_Id == dispatchToAdd.Id && d.Deleted_At == null) == null) { Deliveries_Dispatches deliveryDispatch = new Deliveries_Dispatches(); deliveryDispatch.Created_At = dateOfCreate; deliveryDispatch.Dispatch_Id = (int)dispatchPostion.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; } } var orderCounter = _context.Counters.FirstOrDefault(c => c.Name == "DispatchCounter"); orderCounter.Count++; _context.SaveChanges(); 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")); } }
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")); } }