public List <DriverAllowanceViewModel> GetDriverAllowances(string customerMainC, string customerSubC) { var driverAllowances = _driverAllowanceRepository.Query(p => //p.CustomerMainC == customerMainC && //p.CustomerSubC == customerSubC && p.ApplyD < DateTime.Now); var groupMaxDriverAllowances = from p in driverAllowances group p by new { p.CustomerMainC, p.CustomerSubC } into g select new { g.Key.CustomerMainC, g.Key.CustomerSubC, ApplyD = g.Max(p => p.ApplyD) }; var result = from p in driverAllowances join q in groupMaxDriverAllowances on new { p.CustomerMainC, p.CustomerSubC, p.ApplyD } equals new { q.CustomerMainC, q.CustomerSubC, q.ApplyD } select new DriverAllowanceViewModel() { CustomerMainC = customerMainC, CustomerSubC = customerSubC, ApplyD = (DateTime)p.ApplyD, UnitPriceMethodI = p.UnitPriceMethodI, DepartureC = p.DepartureC, DestinationC = p.DestinationC, ContainerSizeI = p.ContainerSizeI, EmptyGoods = p.EmptyGoods, UnitPrice = p.UnitPrice, UnitPriceRate = p.UnitPriceRate }; return(result.ToList()); }
public Result GetInfoFromTransportDistance(DateTime orderD, string orderNo, int detailNo, string location1C, string location2C, string operation1C, string operation2C, DateTime transportD, string waytype) { var loc1 = _locationRepository.Query(l => l.LocationC == location1C).FirstOrDefault(); location1C = loc1 != null ? (!string.IsNullOrEmpty(loc1.AreaC) ? loc1.AreaC : location1C) : location1C; var loc2 = _locationRepository.Query(l => l.LocationC == location2C).FirstOrDefault(); location2C = loc2 != null ? (!string.IsNullOrEmpty(loc2.AreaC) ? loc2.AreaC : location2C) : location2C; if (waytype == "4") { var obj = new Result(); var checkUnitPriceMethod = _driverAllowanceRepository.Query(p => DateTime.Compare(transportD, p.ApplyD) >= 0).ToList(); var maxApplyD = checkUnitPriceMethod.Max(p => p.ApplyD); var selectResult = _driverAllowanceRepository.Query(p => p.ApplyD == maxApplyD).FirstOrDefault(); if (selectResult != null) { var oD = _orderDRepository.Query(p => p.OrderD == orderD && p.OrderNo == orderNo && p.DetailNo == detailNo) .FirstOrDefault(); var transport = _transportDistanceRepository.Query(p => p.FromAreaC == location1C && p.ToAreaC == location2C).FirstOrDefault(); if (selectResult.UnitPriceMethodI == "2") { if (transport != null) { if (oD != null) { var flagup = _driverAllowanceRepository.Query(p => DateTime.Compare(transportD, p.ApplyD) >= 0 && p.UnitPriceMethodI == "2") .ToList(); var maxflagup = flagup.Max(p => p.ApplyD); var selectmaxflagup = _driverAllowanceRepository.Query(p => p.ApplyD == maxflagup && p.ContainerSize == oD.ContainerSizeI) .FirstOrDefault(); if (selectmaxflagup != null) { switch (transport.WayType) { case "0": { obj = new Result() { Km = transport.Km ?? 0, EmptyCommodity = selectmaxflagup.Empty ?? 0, UnitPrice = selectmaxflagup.ShortRoad ?? 0, WayType = transport.WayType, IsDistance = "0" }; break; } case "1": { obj = new Result() { Km = transport.Km ?? 0, EmptyCommodity = selectmaxflagup.Empty ?? 0, UnitPrice = selectmaxflagup.LongRoad ?? 0, WayType = transport.WayType, IsDistance = "0" }; break; } case "2": { obj = new Result() { Km = transport.Km ?? 0, EmptyCommodity = selectmaxflagup.Empty ?? 0, UnitPrice = selectmaxflagup.GradientRoad ?? 0, WayType = transport.WayType, IsDistance = "0" }; break; } } } else { obj = new Result() { Km = transport.Km ?? 0, EmptyCommodity = 0, UnitPrice = 0, WayType = transport.WayType, IsDistance = "0" }; } } } } else if (selectResult.UnitPriceMethodI == "0") { var oH = _orderRepository.Query(p => p.OrderD == orderD && p.OrderNo == orderNo).FirstOrDefault(); if (oH != null && oD != null) { decimal?empty = 0; decimal?commodity = 0; selectResult = _driverAllowanceRepository.Query( p => p.ApplyD == maxApplyD && p.DepartureC == location1C && p.DestinationC == location2C && p.ContainerSizeI == oD.ContainerSizeI).FirstOrDefault(); if (selectResult != null) { empty = (operation1C == "LR" || operation1C == "TR" || operation2C == "LR" || operation2C == "TR") ? selectResult.EmptyGoods : 0; commodity = (operation1C != "LR" && operation1C != "TR" && operation2C != "LR" && operation2C != "TR") ? selectResult.UnitPrice : 0; obj = new Result() { Km = transport != null ? (transport.Km ?? 0) : 0, EmptyCommodity = empty ?? 0, UnitPrice = commodity ?? 0, WayType = transport != null ? (transport.WayType ?? waytype) : waytype, IsDistance = "1" }; } } } if (obj.Km == null && obj.EmptyCommodity == null && obj.UnitPrice == null && obj.WayType == null) { obj = new Result() { Km = transport != null ? (transport.Km ?? 0) : 0, EmptyCommodity = 0, UnitPrice = 0, WayType = transport != null ? (transport.WayType ?? waytype) : waytype, IsDistance = "0" }; } } return(obj); } else { var obj = new Result(); var checkUnitPriceMethod = _driverAllowanceRepository.Query(p => DateTime.Compare(transportD, p.ApplyD) >= 0).ToList(); var maxApplyD = checkUnitPriceMethod.Max(p => p.ApplyD); var selectResult = _driverAllowanceRepository.Query(p => p.ApplyD == maxApplyD).FirstOrDefault(); if (selectResult != null) { var oD = _orderDRepository.Query(p => p.OrderD == orderD && p.OrderNo == orderNo && p.DetailNo == detailNo) .FirstOrDefault(); var transport = _transportDistanceRepository.Query(p => p.FromAreaC == location1C && p.ToAreaC == location2C).FirstOrDefault(); if (selectResult.UnitPriceMethodI == "2") { if (transport != null) { if (oD != null) { var flagup = _driverAllowanceRepository.Query(p => DateTime.Compare(transportD, p.ApplyD) >= 0 && p.UnitPriceMethodI == "2") .ToList(); var maxflagup = flagup.Max(p => p.ApplyD); var selectmaxflagup = _driverAllowanceRepository.Query(p => p.ApplyD == maxflagup && p.ContainerSize == oD.ContainerSizeI) .FirstOrDefault(); if (selectmaxflagup != null) { switch (waytype) { case "0": { obj = new Result() { Km = transport.Km ?? 0, EmptyCommodity = selectmaxflagup.Empty ?? 0, UnitPrice = selectmaxflagup.ShortRoad ?? 0, WayType = waytype, IsDistance = "0" }; break; } case "1": { obj = new Result() { Km = transport.Km ?? 0, EmptyCommodity = selectmaxflagup.Empty ?? 0, UnitPrice = selectmaxflagup.LongRoad ?? 0, WayType = waytype, IsDistance = "0" }; break; } case "2": { obj = new Result() { Km = transport.Km ?? 0, EmptyCommodity = selectmaxflagup.Empty ?? 0, UnitPrice = selectmaxflagup.GradientRoad ?? 0, WayType = waytype, IsDistance = "0" }; break; } } } else { obj = new Result() { Km = transport.Km ?? 0, EmptyCommodity = 0, UnitPrice = 0, WayType = waytype, IsDistance = "0" }; } } } } else if (selectResult.UnitPriceMethodI == "0") { var oH = _orderRepository.Query(p => p.OrderD == orderD && p.OrderNo == orderNo).FirstOrDefault(); if (oH != null && oD != null) { decimal?empty = 0; decimal?commodity = 0; selectResult = _driverAllowanceRepository.Query( p => p.ApplyD == maxApplyD && p.DepartureC == location1C && p.DestinationC == location2C && p.ContainerSizeI == oD.ContainerSizeI).FirstOrDefault(); if (selectResult != null) { empty = (operation1C == "LR" || operation1C == "TR" || operation2C == "LR" || operation2C == "TR") ? selectResult.EmptyGoods : 0; commodity = (operation1C != "LR" && operation1C != "TR" && operation2C != "LR" && operation2C != "TR") ? selectResult.UnitPrice : 0; obj = new Result() { Km = transport != null ? (transport.Km ?? 0) : 0, EmptyCommodity = empty ?? 0, UnitPrice = commodity ?? 0, WayType = waytype, IsDistance = "1" }; } } } if (obj.Km == null && obj.EmptyCommodity == null && obj.UnitPrice == null && obj.WayType == null) { selectResult = _driverAllowanceRepository.Query(p => p.ApplyD == maxApplyD && oD.ContainerSizeI == p.ContainerSize) .FirstOrDefault(); if (selectResult != null) { switch (waytype) { case "0": { obj = new Result() { Km = transport != null ? (transport.Km ?? 0) : 0, EmptyCommodity = selectResult.Empty ?? 0, UnitPrice = selectResult.ShortRoad ?? 0, WayType = waytype, IsDistance = "0" }; break; } case "1": { obj = new Result() { Km = transport != null ? (transport.Km ?? 0) : 0, EmptyCommodity = selectResult.Empty ?? 0, UnitPrice = selectResult.LongRoad ?? 0, WayType = waytype, IsDistance = "0" }; break; } case "2": { obj = new Result() { Km = transport != null ? (transport.Km ?? 0) : 0, EmptyCommodity = selectResult.Empty ?? 0, UnitPrice = selectResult.GradientRoad ?? 0, WayType = waytype, IsDistance = "0" }; break; } } } else { obj = new Result() { Km = transport != null ? (transport.Km ?? 0) : 0, EmptyCommodity = 0, UnitPrice = 0, WayType = waytype, IsDistance = "0" }; } } } return(obj); } }