public ActionResult DeliveryReconcileUpdate([DataSourceRequest] DataSourceRequest request, DispatchViewModelForReconcile dispatchViewModelForReconcile)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if(dispatchViewModelForReconcile.DeliveryReconcileID!=null)
                    {
                        var deliveryReconcile =
                            _deliveryReconcileService.FindById((int) dispatchViewModelForReconcile.DeliveryReconcileID);
                        deliveryReconcile.GRN = dispatchViewModelForReconcile.GRN;
                        deliveryReconcile.FDPID = dispatchViewModelForReconcile.FDPID;
                        deliveryReconcile.DispatchID = dispatchViewModelForReconcile.DispatchID;
                        deliveryReconcile.WayBillNo = dispatchViewModelForReconcile.WayBillNo;
                        deliveryReconcile.RequsitionNo = dispatchViewModelForReconcile.RequisitionNo;
                        deliveryReconcile.HubID = dispatchViewModelForReconcile.HubID;
                        deliveryReconcile.GIN = dispatchViewModelForReconcile.GIN;
                        deliveryReconcile.ReceivedAmount = (dispatchViewModelForReconcile.ReceivedAmount ?? 0);
                        deliveryReconcile.ReceivedDate = (dispatchViewModelForReconcile.ReceivedDate ?? DateTime.Now);
                        deliveryReconcile.LossAmount = dispatchViewModelForReconcile.LossAmount;
                        deliveryReconcile.LossReason = dispatchViewModelForReconcile.LossReasonId;
                        _deliveryReconcileService.EditDeliveryReconcile(deliveryReconcile);
                        ModelState.AddModelError("Success", @"Success: Delivery Reconcilation Data Updated.");
                    }
                    else
                    {
                        var dvmfr = dispatchViewModelForReconcile;
                        if (dvmfr.GRN != null  && dvmfr.RequisitionNo != null && dvmfr.GIN != null
                            && dvmfr.ReceivedAmount != null && dvmfr.ReceivedDate != null)
                        {
                            var deliveryReconcile = new DeliveryReconcile
                            {
                                GRN = dvmfr.GRN,
                                FDPID = dvmfr.FDPID,
                                DispatchID = dvmfr.DispatchID,
                                WayBillNo = dvmfr.WayBillNo,
                                RequsitionNo = dvmfr.RequisitionNo,
                                HubID = dvmfr.HubID,
                                GIN = dvmfr.GIN,
                                ReceivedAmount =
                                    (dvmfr.ReceivedAmount ?? 0),
                                ReceivedDate =
                                    (dvmfr.ReceivedDate ?? DateTime.Now),
                                LossAmount = dvmfr.LossAmount,
                                LossReason = dvmfr.LossReasonId
                            };
                            _deliveryReconcileService.AddDeliveryReconcile(deliveryReconcile);
                            _transactionService.PostDeliveryReconcileReceipt(deliveryReconcile.DeliveryReconcileID);
                            ModelState.AddModelError("Success", @"Success: Delivery Reconcilation Data Added.");
                        }
                    }

                    //return RedirectToAction("Index", new {regionID = });
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Errors", @"Error: FDP not registered. All fields need to be filled.");
                }
            }
            return Json(new[] { dispatchViewModelForReconcile }.ToDataSourceResult(request, ModelState));
        }
        private IEnumerable<DispatchViewModelForReconcile> BindDispatchViewModelForReconciles(IEnumerable<Dispatch> dispatches)
        {
            var dispatchViewModelForReconciles = new List<DispatchViewModelForReconcile>();
            foreach (var dispatch in dispatches)
            {
                var firstOrDefault = dispatch.DispatchDetails.FirstOrDefault();
                if (firstOrDefault != null)
                {
                    var dispatchViewModelForReconcile = new DispatchViewModelForReconcile();
                    dispatchViewModelForReconcile.DispatchID = dispatch.DispatchID;
                    dispatchViewModelForReconcile.FDP = dispatch.FDP.Name;
                    dispatchViewModelForReconcile.DispatchedByStoreMan = dispatch.DispatchedByStoreMan;
                    dispatchViewModelForReconcile.DispatchDate = dispatch.DispatchDate;
                    dispatchViewModelForReconcile.RequisitionNo = dispatch.RequisitionNo;
                    dispatchViewModelForReconcile.GIN = dispatch.GIN;
                    dispatchViewModelForReconcile.BidNumber = dispatch.BidNumber;
                    dispatchViewModelForReconcile.DriverName = dispatch.DriverName;
                    if (dispatch.DispatchAllocation != null && dispatch.DispatchAllocation.Year != null)
                    {
                        dispatchViewModelForReconcile.MonthYear = dispatch.DispatchAllocation.Month.ToString() + " - " + dispatch.DispatchAllocation.Year.ToString();
                    }
                    dispatchViewModelForReconcile.WeighBridgeTicketNumber = dispatch.WeighBridgeTicketNumber??"";
                    dispatchViewModelForReconcile.CreatedDate = dispatch.CreatedDate;
                    dispatchViewModelForReconcile.DispatchAllocationID = dispatch.DispatchAllocationID??new Guid();
                    dispatchViewModelForReconcile.FDPID = dispatch.FDPID??0;
                    dispatchViewModelForReconcile.Region = dispatch.FDP.AdminUnit.AdminUnit2.AdminUnit2.Name;
                    dispatchViewModelForReconcile.Zone = dispatch.FDP.AdminUnit.AdminUnit2.Name;
                    dispatchViewModelForReconcile.Woreda = dispatch.FDP.AdminUnit.Name;
                    dispatchViewModelForReconcile.HubID = dispatch.HubID;
                    dispatchViewModelForReconcile.PlateNo_Prime = dispatch.PlateNo_Prime;
                    dispatchViewModelForReconcile.PlateNo_Trailer = dispatch.PlateNo_Trailer??"";
                    dispatchViewModelForReconcile.Remark = dispatch.Remark ?? "";
                    dispatchViewModelForReconcile.Round = dispatch.Round;
                    dispatchViewModelForReconcile.TransporterID = dispatch.TransporterID;
                    dispatchViewModelForReconcile.CommodityID = firstOrDefault.CommodityID;
                    dispatchViewModelForReconcile.Commodity = firstOrDefault.Commodity.Name;
                    dispatchViewModelForReconcile.Quantity = firstOrDefault.RequestedQuantityInMT;
                    dispatchViewModelForReconcile.QuantityInUnit = firstOrDefault.RequestedQunatityInUnit;
                    dispatchViewModelForReconcile.UnitID = firstOrDefault.UnitID;

                    dispatchViewModelForReconciles.Add(dispatchViewModelForReconcile);
                }
            }
            return dispatchViewModelForReconciles;
        }