public List<TransporterPaymentRequestViewModel> TransporterPaymentRequestViewModelBinder(List<TransporterPaymentRequest> transporterPaymentRequests)
 {
     var transporterPaymentRequestViewModels = new List<TransporterPaymentRequestViewModel>();
     foreach (var transporterPaymentRequest in transporterPaymentRequests)
     {
         var request = transporterPaymentRequest;
         var dispatch = _dispatchService.Get(t => t.DispatchID == request.Delivery.DispatchID, null, "Hub, FDP").FirstOrDefault();
         var firstOrDefault = _bidWinnerService.Get(t => t.SourceID == dispatch.HubID && t.DestinationID == dispatch.FDPID
             && t.TransporterID == request.TransportOrder.TransporterID && t.Bid.BidNumber == dispatch.BidNumber).FirstOrDefault();
         var tarrif = (decimal)0.00;
         if (firstOrDefault != null)
         {
             tarrif = firstOrDefault.Tariff != null ? (decimal)firstOrDefault.Tariff : (decimal)0.00;
         }
         if (dispatch != null && request.Delivery.DeliveryDetails.FirstOrDefault() != null)
         {
             var deliveryDetail = request.Delivery.DeliveryDetails.FirstOrDefault();
             var businessProcess = _BusinessProcessService.FindById(request.BusinessProcessID);
             if (request.LabourCost == null)
                 request.LabourCost = (decimal)0.00;
             if (request.RejectedAmount == null)
                 request.RejectedAmount = (decimal)0.00;
             if (deliveryDetail != null)
             {
                 var transporterPaymentRequestViewModel = new TransporterPaymentRequestViewModel()
                 {
                     RequisitionNo = dispatch.RequisitionNo,
                     GIN = request.Delivery.InvoiceNo,
                     GRN = request.Delivery.ReceivingNumber,
                     Commodity = deliveryDetail.Commodity.Name,
                     Source = dispatch.Hub.Name,
                     Destination = dispatch.FDP.Name,
                     ReceivedQty = deliveryDetail.ReceivedQuantity,
                     Tarrif = tarrif,
                     ShortageQty = deliveryDetail.SentQuantity - deliveryDetail.ReceivedQuantity,
                     ShortageBirr = request.ShortageBirr,
                     SentQty = deliveryDetail.SentQuantity,
                     BusinessProcessID = request.BusinessProcessID,
                     DeliveryID = request.DeliveryID,
                     ReferenceNo = request.ReferenceNo,
                     TransportOrderID = request.TransportOrderID,
                     TransporterPaymentRequestID = request.TransporterPaymentRequestID,
                     FreightCharge = (decimal)(request.ShortageBirr != null ? (deliveryDetail.ReceivedQuantity * tarrif) - request.ShortageBirr + request.LabourCost - request.RejectedAmount : (deliveryDetail.ReceivedQuantity * tarrif) + request.LabourCost - request.RejectedAmount),
                     BusinessProcess = businessProcess,
                     LabourCost = request.LabourCost,
                     LabourCostRate = request.LabourCostRate,
                     RejectedAmount = request.RejectedAmount,
                     RejectionReason = request.RejectionReason,
                     RequestedDate = request.RequestedDate
                 };
                 transporterPaymentRequestViewModels.Add(transporterPaymentRequestViewModel);
             }
         }
     }
     return transporterPaymentRequestViewModels;
 }
        public List<TransporterPaymentRequestViewModel> TransporterPaymentRequestViewModelBinder(
            List<TransporterPaymentRequest> transporterPaymentRequests)
        {
            var currentUser = _userAccountService.GetUserInfo(HttpContext.User.Identity.Name);

            var datePref = currentUser.DatePreference;

            var transporterPaymentRequestViewModels = new List<TransporterPaymentRequestViewModel>();
            foreach (var transporterPaymentRequest in transporterPaymentRequests)
            {
                var request = transporterPaymentRequest;

                var dispatch =
                    _dispatchService.Get(t => t.DispatchID == request.Delivery.DispatchID, null, "Hub, FDP").
                        FirstOrDefault();
                var transportOrderdetail =
                    _transportOrderDetailService.FindBy(
                        m =>
                        m.TransportOrderID == request.TransportOrderID && m.SourceWarehouseID == dispatch.HubID &&
                        m.FdpID == dispatch.FDPID).FirstOrDefault();
                //var firstOrDefault = _bidWinnerService.Get(t => t.SourceID == dispatch.HubID && t.DestinationID == dispatch.FDPID
                //    && t.TransporterID == request.TransportOrder.TransporterID && t.Bid.BidNumber == dispatch.BidNumber).FirstOrDefault();
                var tarrif = (decimal)0.00;
                var bidDocNo = string.Empty;
                if (transportOrderdetail != null)
                {
                    tarrif = (decimal)transportOrderdetail.TariffPerQtl;
                    bidDocNo = transportOrderdetail.TransportOrder.BidDocumentNo;
                }
                if (dispatch != null && request.Delivery.DeliveryDetails.FirstOrDefault() != null)
                {
                    {
                        var dispathedAmount = (decimal)0.0;
                        var childCommodity = string.Empty;
                        var firstOrDefault = dispatch.DispatchDetails.FirstOrDefault();
                        if (firstOrDefault != null)
                        {
                            dispathedAmount = firstOrDefault.DispatchedQuantityInMT.ToQuintal();
                        }

                        var dispatchedDate = dispatch.DispatchDate.Date;
                        var dispatchDetail = dispatch.DispatchDetails.FirstOrDefault();
                        if (dispatchDetail != null)
                        {
                            var childCommodityId = dispatchDetail.CommodityChildID;

                            var orDefault = _commodityService.GetCommodities(c => c.CommodityID == childCommodityId).FirstOrDefault();
                            if (orDefault != null)
                            {
                                childCommodity = orDefault.Name;
                            }
                        }
                        var deliveryDetail = request.Delivery.DeliveryDetails.FirstOrDefault();
                        var businessProcess = _BusinessProcessService.FindById(request.BusinessProcessID);
                        if (request.LabourCost == null)
                            request.LabourCost = (decimal)0.00;
                        if (request.RejectedAmount == null)
                            request.RejectedAmount = (decimal)0.00;
                        if (deliveryDetail != null)
                        {
                            var transporterPaymentRequestViewModel = new TransporterPaymentRequestViewModel()
                                                                         {
                                                                             RequisitionNo = dispatch.RequisitionNo,
                                                                             GIN = request.Delivery.InvoiceNo,
                                                                             GRN = request.Delivery.ReceivingNumber,
                                                                             Commodity = deliveryDetail.Commodity.Name,
                                                                             Source = dispatch.Hub.Name,
                                                                             Destination = dispatch.FDP.Name,
                                                                             Region = dispatch.FDP.AdminUnit.AdminUnit2.AdminUnit2.Name,
                                                                             ReceivedQty =
                                                                                 deliveryDetail.ReceivedQuantity.
                                                                                 ToQuintal(),
                                                                             Tarrif = tarrif,
                                                                             ShortageQty = request.ShortageQty != null ? (decimal)(request.ShortageQty) : (deliveryDetail.SentQuantity.ToQuintal()) -
                                                                                                                                                          (deliveryDetail.ReceivedQuantity.
                                                                                                                                                              ToQuintal()),
                                                                             ShortageBirr = request.ShortageBirr,
                                                                             SentQty = deliveryDetail.SentQuantity,
                                                                             BusinessProcessID =
                                                                                 request.BusinessProcessID,
                                                                             DeliveryID = request.DeliveryID,
                                                                             ReferenceNo = request.ReferenceNo,
                                                                             TransportOrderID = request.TransportOrderID,
                                                                             TransporterPaymentRequestID =
                                                                                 request.TransporterPaymentRequestID,
                                                                             FreightCharge =
                                                                                 (decimal)
                                                                                 (request.ShortageBirr != null
                                                                                      ? (Math.Min(deliveryDetail.ReceivedQuantity
                                                                                             .ToQuintal(), dispathedAmount) * tarrif) - (
                                                                                        request.ShortageBirr +
                                                                                        request.LabourCost -
                                                                                        request.RejectedAmount)
                                                                                      : (deliveryDetail.ReceivedQuantity
                                                                                             .ToQuintal() * tarrif) +
                                                                                        request.LabourCost -
                                                                                        request.RejectedAmount),
                                                                             BusinessProcess = businessProcess,
                                                                             LabourCost = request.LabourCost,
                                                                             LabourCostRate = request.LabourCostRate,
                                                                             RejectedAmount = request.RejectedAmount,
                                                                             RejectionReason = request.RejectionReason,
                                                                             RequestedDate = request.RequestedDate,
                                                                             Program = dispatch.DispatchAllocation.Program,
                                                                             Transporter = dispatch.Transporter,
                                                                             ChildCommodity = childCommodity,
                                                                             DispatchDate = dispatchedDate.ToCTSPreferedDateFormat(datePref),
                                                                             DispatchedAmount = dispathedAmount,
                                                                             BidDocumentNo = bidDocNo,
                                                                             Checked = false,
                                                                             ContractNumber = _TransportOrderService.FindBy(t => t.TransportOrderID == dispatch.DispatchAllocation.TransportOrderID).FirstOrDefault().ContractNumber

                                                                         };
                            transporterPaymentRequestViewModels.Add(transporterPaymentRequestViewModel);
                        }
                    }
                }
            }
            return transporterPaymentRequestViewModels;
        }