public ActionResult AddOrRemoveShipment(ImportShipmentFormModel s_ImportShipment, int?i)
        {
            var Prevshipmentsession = Session["PreviousShipments"] as List <ImportShipmentDetail>;

            if (i.HasValue)
            {
                if (Prevshipmentsession == null)
                {
                }
                else
                {
                    foreach (var item in Prevshipmentsession)
                    {
                        s_ImportShipment.Shipments.Add(item);
                    }
                }
                s_ImportShipment.Shipments.RemoveAt(i.Value);
                Session["PreviousShipments"] = s_ImportShipment.Shipments;
            }
            else
            {
                if (s_ImportShipment.Shipments == null)
                {
                    s_ImportShipment.Shipments = new List <ImportShipmentDetail>();
                }
                var shipmentsession = Session["Shipmentdetails"] as ImportShipmentDetail;
                var Serialnumber    = Convert.ToInt32(Session["ShipSerialNumber"]);
                var isupdate        = Convert.ToBoolean(Session["IsUpdate"]);
                if (Prevshipmentsession == null)
                {
                }
                else
                {
                    foreach (var item in Prevshipmentsession)
                    {
                        s_ImportShipment.Shipments.Add(item);
                    }
                }
                if (isupdate == true)
                {
                    //s_ImportShipment.Shipments.RemoveAt(Serialnumber);
                    s_ImportShipment.Shipments[Serialnumber] = shipmentsession;
                }
                else
                {
                    s_ImportShipment.Shipments.Add(shipmentsession);
                }
                Session["Shipmentdetails"]  = new ImportShipmentDetail();
                Session["ShipSerialNumber"] = "";
                Session["IsUpdate"]         = false;
            }
            ViewBag.Cities               = db.CityMasters.ToList();
            ViewBag.Countries            = db.CountryMasters.ToList();
            ViewBag.DestinationCountryID = db.CountryMasters.ToList();
            ViewBag.CurrencyID           = db.CurrencyMasters.ToList();
            ViewBag.Currencies           = db.CurrencyMasters.ToList();

            return(PartialView("ShipmentList", s_ImportShipment));
        }
        // GET: ImportShipment/Details/5
        public ActionResult Details(int?id)
        {
            //AuthHelp Token = repos.Authenticate();
            //if (Token.Status)
            //{
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var s_ImportShipment = db.ImportShipments.Where(x => x.ID == id)
                                   .FirstOrDefault();

            if (s_ImportShipment == null)
            {
                return(HttpNotFound());
            }

            ImportShipmentFormModel model = new ImportShipmentFormModel();

            model = db.ImportShipments.Where(x => x.ID == id).Select(x => new ImportShipmentFormModel
            {
                ID            = x.ID,
                AgentID       = x.AgentID,
                AgentLoginID  = x.AgentLoginID,
                Bags          = x.Bags,
                CD            = x.CD,
                ConsignorName = x.ConsignorName,
                ConsignorAddress1_Building = x.ConsignorAddress1_Building,
                ConsignorAddress2_Street   = x.ConsignorAddress2_Street,
                ConsignorAddress3_PinCode  = x.ConsignorAddress3_PinCode,
                ConsignorCountryName       = x.ConsignorCountryName,
                ConsignorCityName          = x.ConsignorCityName,
                ConsigneeName = x.ConsigneeName,
                ConsigneeAddress1_Building = x.ConsigneeAddress1_Building,
                ConsigneeAddress2_Street   = x.ConsigneeAddress2_Street,
                ConsigneeAddress3_PinCode  = x.ConsigneeAddress3_PinCode,
                ConsigneeCountryName       = x.ConsigneeCountryName,
                ConsigneeCityName          = x.ConsigneeCityName,
                CreatedDate         = x.CreatedDate,
                FlightDate          = x.FlightDate,
                FlightNo            = x.FlightNo,
                LastEditedByLoginID = x.LastEditedByLoginID,
                ManifestNumber      = x.ManifestNumber,
                MAWB = x.MAWB,
                OriginAirportCity      = x.OriginAirportCity,
                DestinationAirportCity = x.DestinationAirportCity,
                RunNo          = x.RunNo,
                TotalAWB       = x.TotalAWB,
                ShipmentTypeId = x.ShipmentTypeId
            }).FirstOrDefault();

            model.ConsigneeAddress = model.ConsigneeAddress1_Building + "," + model.ConsigneeAddress2_Street + "\n" + model.ConsigneeAddress3_PinCode;
            model.ConsignorAddress = model.ConsignorAddress1_Building + "," + model.ConsignorAddress2_Street + "\n" + model.ConsignorAddress3_PinCode;

            model.Shipments = db.ImportShipmentDetails.Where(d => d.ImportID == id).ToList();

            return(View(model));
        }
 public JsonResult GetShipmentDetails(ImportShipmentFormModel s_ImportShipment, int?i)
 {
     if (i.HasValue)
     {
         var s = s_ImportShipment.Shipments[i.Value];
         return(Json(new { success = true, data = s, ival = i.Value }, JsonRequestBehavior.AllowGet));
     }
     else
     {
         return(Json(new { success = false }, JsonRequestBehavior.AllowGet));
     }
 }
        public ActionResult Create(ImportShipmentFormModel model)
        {
            var today  = DateTime.Now.Date;
            var userid = Convert.ToInt32(Session["UserID"]);
            //var agent1 = (from u2 in db.UserRegistrations join c1 in db.AgentMasters on u2.UserID equals c1.UserID where u2.UserID == userid select c1).FirstOrDefault();
            //var user = (from u2 in db.UserRegistrations select u2).FirstOrDefault();

            var agent    = db.AgentMasters.Where(aa => aa.UserID == userid).FirstOrDefault();
            var fwdagent = db.ForwardingAgentMasters.FirstOrDefault();

            //var company = db.AcCompanies.Select(x => new { Address = x.Address1 + ", " + x.Address2 + ", " + x.Address3, x.CountryID, x.Phone, x.AcCompany1, x.CountryName }).FirstOrDefault();

            var            company        = db.AcCompanies.FirstOrDefault(); //. Select(x => new { Address = x.Address1 + ", " + x.Address2 + ", " + x.Address3, x.CountryID, x.Phone, x.AcCompany1, x.CountryName }).FirstOrDefault();
            ImportShipment importShipment = new ImportShipment();

            if (ModelState.IsValid)
            {
                if (model.ID == 0)
                {
                    importShipment.ID = db.ImportShipments.Select(x => x.ID).DefaultIfEmpty(0).Max() + 1;

                    long manifestNumber = 0;
                    manifestNumber = db.ImportShipments.Where(x => x.AgentID == agent.AgentID).Count() + 1;
                    var Manifest = $"{DateTime.Now.ToString("yyyyMMdd")}{agent.AgentCode}{(manifestNumber.ToString("D4"))}";
                    importShipment.ManifestNumber             = Manifest;
                    importShipment.AgentID                    = agent.AgentID;
                    importShipment.CreatedDate                = DateTime.Now;
                    importShipment.ConsignorName              = agent.Name;
                    importShipment.ConsignorCityName          = agent.CityName;
                    importShipment.ConsignorCountryName       = agent.CountryName;
                    importShipment.ConsignorAddress1_Building = agent.Address1;
                    importShipment.ConsignorAddress2_Street   = agent.Address2;
                    importShipment.ConsignorAddress3_PinCode  = agent.Address3;
                    importShipment.ConsignorLocationName      = agent.LocationName;
                    importShipment.ConsigneeName              = company.AcCompany1;
                    importShipment.ConsigneeCountryName       = company.CountryName;
                    importShipment.ConsigneeCityName          = company.CityName;
                    importShipment.ConsigneeLocationName      = company.LocationName;
                    importShipment.ConsigneeAddress1_Building = company.Address1;
                    importShipment.ConsigneeAddress2_Street   = company.Address2;
                    importShipment.ConsigneeAddress3_PinCode  = company.Address3;
                    importShipment.AgentLoginID               = userid;
                }
                else
                {
                    importShipment = db.ImportShipments.Find(model.ID);
                }


                importShipment.Bags                   = model.Bags;
                importShipment.CD                     = model.CD;
                importShipment.FlightDate             = model.FlightDate;
                importShipment.FlightNo               = model.FlightNo;
                importShipment.LastEditedByLoginID    = userid;
                importShipment.MAWB                   = model.MAWB;
                importShipment.RunNo                  = model.RunNo;
                importShipment.TotalAWB               = model.TotalAWB;
                importShipment.Type                   = model.Type;
                importShipment.DestinationAirportCity = model.DestinationAirportCity;
                importShipment.OriginAirportCity      = model.OriginAirportCity;

                if (model.ID == 0)
                {
                    db.ImportShipments.Add(importShipment);
                    db.SaveChanges();
                }
                else
                {
                    db.Entry(importShipment).State = EntityState.Modified;
                    db.SaveChanges();
                }

                var max = db.ImportShipmentDetails.Select(x => x.ShipmentDetailID).DefaultIfEmpty(0).Max() + 1;
                if (model.Shipments != null)

                {
                    //model.Shipments.ForEach(x =>
                    //{
                    //    x.ShipmentDetailID = max;
                    //    x.ImportID = importShipment.ID;
                    //    x.Status = 1;
                    //    max++;
                    //});
                    foreach (var det in model.Shipments)
                    {
                        if (det.ShipmentDetailID == 0)
                        {
                            det.ShipmentDetailID = max;
                            det.ImportID         = importShipment.ID;
                            det.StatusTypeId     = db.tblStatusTypes.Where(cc => cc.Name == "FORWARDED").FirstOrDefault().ID;
                            det.CourierStatusID  = db.CourierStatus.Where(cc => cc.CourierStatus == "Forwarded to Agent").FirstOrDefault().CourierStatusID;
                            db.ImportShipmentDetails.Add(det);
                            db.SaveChanges();
                            max++;
                        }
                        else
                        {
                            det.ImportID        = importShipment.ID;
                            det.StatusTypeId    = db.tblStatusTypes.Where(cc => cc.Name == "FORWARDED").FirstOrDefault().ID;
                            det.CourierStatusID = db.CourierStatus.Where(cc => cc.CourierStatus == "Forwarded to Agent").FirstOrDefault().CourierStatusID;
                            db.Entry(det).State = EntityState.Modified;
                            db.SaveChanges();
                        }

                        //updateing awbstaus table for tracking
                        AWBTrackStatu _awbstatus = new AWBTrackStatu();
                        int?          id         = (from c in db.AWBTrackStatus orderby c.AWBTrackStatusId descending select c.AWBTrackStatusId).FirstOrDefault();

                        if (id == null)
                        {
                            id = 1;
                        }
                        else
                        {
                            id = id + 1;
                        }

                        _awbstatus.AWBTrackStatusId = Convert.ToInt32(id);
                        _awbstatus.AWBNo            = det.HAWB;
                        _awbstatus.EntryDate        = DateTime.Now;
                        _awbstatus.ShipmentDetailID = det.ShipmentDetailID;
                        _awbstatus.StatusTypeId     = Convert.ToInt32(det.StatusTypeId);
                        _awbstatus.CourierStatusId  = Convert.ToInt32(det.CourierStatusID);
                        _awbstatus.ShipmentStatus   = db.tblStatusTypes.Find(det.StatusTypeId).Name;
                        _awbstatus.CourierStatus    = db.CourierStatus.Find(det.CourierStatusID).CourierStatus;
                        _awbstatus.UserId           = userid;

                        db.AWBTrackStatus.Add(_awbstatus);
                        db.SaveChanges();
                    }

                    var li            = model.Shipments.Select(x => x.ShipmentDetailID).ToList();
                    var importdetails = db.ImportShipmentDetails.Where(x => !li.Contains(x.ShipmentDetailID) && x.ImportID == importShipment.ID).ToList();
                    foreach (var import in importdetails)
                    {
                        db.ImportShipmentDetails.Add(import);
                        db.SaveChanges();
                    }
                }
                else
                {
                    var remove_datas = db.ImportShipmentDetails.Where(s => s.ImportID == importShipment.ID).ToList();
                    foreach (var rdata in remove_datas)
                    {
                        db.ImportShipmentDetails.Remove(rdata);
                        db.SaveChanges();
                    }
                }
                return(RedirectToAction("Index"));
            }
            model.ConsignorName        = agent.Name;
            model.ConsignorCountryName = agent.CountryName;
            model.ConsignorCityName    = agent.CityName;
            model.ConsignorAddress     = agent.Address1 + "," + agent.Address2 + "," + agent.Address3;

            model.ConsigneeName        = company.AcCompany1;
            model.ConsigneeCityName    = company.CityName;
            model.ConsigneeCountryName = company.CountryName;
            model.ConsigneeAddress     = company.Address1 + "," + company.Address2 + "," + company.Address3;



            if (model.Shipments == null)
            {
                model.Shipments = new List <ImportShipmentDetail>();
            }
            var countries = db.CountryMasters.ToList();// db.CountryMasters.ToList();

            ViewBag.OriginCityID         = new SelectList(new List <CityMaster>(), "CityID", "City");
            ViewBag.DestinationCityID    = new SelectList(new List <CityMaster>(), "CityID", "City");
            ViewBag.OriginCountryID      = countries;
            ViewBag.DestinationCountryID = countries;
            ViewBag.CurrencyID           = db.CurrencyMasters.ToList();
            ViewBag.Cities    = db.CityMasters.ToList();
            ViewBag.Countries = db.CountryMasters.ToList();
            string selectedVal = null;
            var    types       = new List <SelectListItem>
            {
                new SelectListItem {
                    Text = "Transhipment", Value = "Transhipment", Selected = selectedVal == "Transhipment"
                },
                new SelectListItem {
                    Text = "Import", Value = "Import", Selected = selectedVal == "Import"
                },
            };

            ViewBag.Type        = types;
            ViewBag.Currencies  = db.CurrencyMasters.ToList();
            ViewBag.AgentName   = agent.Name;
            ViewBag.AgentCity   = agent.CityName;
            ViewBag.CompanyName = company.AcCompany1;

            return(View(model));
        }
        // GET: ImportShipment/Create
        public ActionResult Create(int?id = 0)
        {
            var userid  = Convert.ToInt32(Session["UserID"]);
            var agent   = db.AgentMasters.Where(cc => cc.UserID == userid).FirstOrDefault();
            var company = db.AcCompanies.FirstOrDefault(); // .Select(x => new { Address = x.Address1 + ", " + x.Address2 + ", " + x.Address3, x.CountryID, x.Phone, x.AcCompany1, x.CountryName }).FirstOrDefault();
            ImportShipmentFormModel model = new ImportShipmentFormModel();

            if (id == 0)
            {
                model.ConsignorName        = agent.Name;
                model.ConsignorCountryName = agent.CountryName;
                model.ConsignorCityName    = agent.CityName;
                model.ConsignorAddress     = agent.Address1 + "," + agent.Address2 + "," + agent.Address3;
                model.ConsigneeName        = company.AcCompany1;
                model.ConsigneeCityName    = company.CityName;
                model.ConsigneeCountryName = company.CountryName;
                model.ConsigneeAddress     = company.Address1 + "," + company.Address2 + "," + company.Address3;
                long manifestNumber = 0;
                manifestNumber = db.ImportShipments.Where(x => x.AgentID == agent.AgentID).Count() + 1;
                var Manifest = $"{DateTime.Now.ToString("yyyyMMdd")}{agent.AgentCode}{(manifestNumber.ToString("D4"))}";

                model.ManifestNumber = Manifest;
                model.FlightDate     = DateTime.Now;
                model.CreatedDate    = DateTime.Now;

                if (model.Shipments == null)
                {
                    model.Shipments = new List <ImportShipmentDetail>();
                }
            }
            else
            {
                model = db.ImportShipments.Where(x => x.ID == id).Select(x => new ImportShipmentFormModel
                {
                    ID            = x.ID,
                    AgentID       = x.AgentID,
                    AgentLoginID  = x.AgentLoginID,
                    Bags          = x.Bags,
                    CD            = x.CD,
                    ConsignorName = x.ConsignorName,
                    ConsignorAddress1_Building = x.ConsignorAddress1_Building,
                    ConsignorAddress2_Street   = x.ConsignorAddress2_Street,
                    ConsignorAddress3_PinCode  = x.ConsignorAddress3_PinCode,
                    ConsignorCountryName       = x.ConsignorCountryName,
                    ConsignorCityName          = x.ConsignorCityName,
                    ConsigneeName = x.ConsigneeName,
                    ConsigneeAddress1_Building = x.ConsigneeAddress1_Building,
                    ConsigneeAddress2_Street   = x.ConsigneeAddress2_Street,
                    ConsigneeAddress3_PinCode  = x.ConsigneeAddress3_PinCode,
                    ConsigneeCountryName       = x.ConsigneeCountryName,
                    ConsigneeCityName          = x.ConsigneeCityName,
                    CreatedDate         = x.CreatedDate,
                    FlightDate          = x.FlightDate,
                    FlightNo            = x.FlightNo,
                    LastEditedByLoginID = x.LastEditedByLoginID,
                    ManifestNumber      = x.ManifestNumber,
                    MAWB = x.MAWB,
                    OriginAirportCity      = x.OriginAirportCity,
                    DestinationAirportCity = x.DestinationAirportCity,
                    RunNo    = x.RunNo,
                    TotalAWB = x.TotalAWB,
                    Type     = x.Type
                }).FirstOrDefault();

                model.ConsigneeAddress = model.ConsigneeAddress1_Building + "," + model.ConsigneeAddress2_Street + "\n" + model.ConsigneeAddress3_PinCode;
                model.ConsignorAddress = model.ConsignorAddress1_Building + "," + model.ConsignorAddress2_Street + "\n" + model.ConsignorAddress3_PinCode;

                model.Shipments = db.ImportShipmentDetails.Where(d => d.ImportID == id).ToList();
            }

            string selectedVal = model.Type;
            var    types       = new List <SelectListItem>
            {
                new SelectListItem {
                    Text = "Select Shipment Type", Value = null, Selected = selectedVal == null
                },
                new SelectListItem {
                    Text = "Transhipment", Value = "Transhipment", Selected = selectedVal == "Transhipment"
                },
                new SelectListItem {
                    Text = "Import", Value = "Import", Selected = selectedVal == "Import"
                },
            };

            ViewBag.Type = types;
            var currency = new SelectList(db.CurrencyMasters.OrderBy(x => x.CurrencyName), "CurrencyID", "CurrencyName").ToList();

            ViewBag.CurrencyID  = db.CurrencyMasters.ToList(); // db.CurrencyMasters.ToList();
            ViewBag.Currencies  = db.CurrencyMasters.ToList();
            ViewBag.AgentName   = agent.Name;
            ViewBag.AgentCity   = agent.CityName;
            ViewBag.CompanyName = company.AcCompany1;
            return(View(model));
        }
        public ActionResult Create(ImportShipmentFormModel model)
        {
            int yearid = Convert.ToInt32(Session["fyearid"].ToString());
            var today  = DateTime.Now.Date;
            var userid = Convert.ToInt32(Session["UserID"]);
            //var agent1 = (from u2 in db.UserRegistrations join c1 in db.AgentMasters on u2.UserID equals c1.UserID where u2.UserID == userid select c1).FirstOrDefault();
            //var user = (from u2 in db.UserRegistrations select u2).FirstOrDefault();

            var agent    = db.AgentMasters.Where(aa => aa.UserID == userid).FirstOrDefault();
            var fwdagent = db.ForwardingAgentMasters.FirstOrDefault();

            //var company = db.AcCompanies.Select(x => new { Address = x.Address1 + ", " + x.Address2 + ", " + x.Address3, x.CountryID, x.Phone, x.AcCompany1, x.CountryName }).FirstOrDefault();

            var            company        = db.AcCompanies.FirstOrDefault(); //. Select(x => new { Address = x.Address1 + ", " + x.Address2 + ", " + x.Address3, x.CountryID, x.Phone, x.AcCompany1, x.CountryName }).FirstOrDefault();
            ImportShipment importShipment = new ImportShipment();

            if (ModelState.IsValid)
            {
                if (model.ID == 0)
                {
                    importShipment.ID = db.ImportShipments.Select(x => x.ID).DefaultIfEmpty(0).Max() + 1;

                    long manifestNumber = 0;
                    manifestNumber = db.ImportShipments.Where(x => x.AgentID == agent.AgentID).Count() + 1;
                    var Manifest = $"{DateTime.Now.ToString("yyyyMMdd")}{agent.AgentCode}{(manifestNumber.ToString("D4"))}";
                    importShipment.ManifestNumber             = Manifest;
                    importShipment.AgentID                    = agent.AgentID;
                    importShipment.CreatedDate                = DateTime.Now;
                    importShipment.ConsignorName              = agent.Name;
                    importShipment.ConsignorCityName          = agent.CityName;
                    importShipment.ConsignorCountryName       = agent.CountryName;
                    importShipment.ConsignorAddress1_Building = agent.Address1;
                    importShipment.ConsignorAddress2_Street   = agent.Address2;
                    importShipment.ConsignorAddress3_PinCode  = agent.Address3;
                    importShipment.ConsignorLocationName      = agent.LocationName;
                    importShipment.ConsigneeName              = company.AcCompany1;
                    importShipment.ConsigneeCountryName       = company.CountryName;
                    importShipment.ConsigneeCityName          = company.CityName;
                    importShipment.ConsigneeLocationName      = company.LocationName;
                    importShipment.ConsigneeAddress1_Building = company.Address1;
                    importShipment.ConsigneeAddress2_Street   = company.Address2;
                    importShipment.ConsigneeAddress3_PinCode  = company.Address3;
                    importShipment.AcFinancialYearID          = yearid;
                    importShipment.AgentLoginID               = userid;
                    importShipment.Status = 0; // db.CourierStatus.Where(cc => cc.CourierStatus == "Forwarded to Agent").First().CourierStatusID;
                }
                else
                {
                    importShipment = db.ImportShipments.Find(model.ID);
                }


                importShipment.Bags                   = model.Bags;
                importShipment.CD                     = model.CD;
                importShipment.FlightDate             = model.FlightDate;
                importShipment.FlightNo               = model.FlightNo;
                importShipment.LastEditedByLoginID    = userid;
                importShipment.MAWB                   = model.MAWB;
                importShipment.RunNo                  = model.RunNo;
                importShipment.TotalAWB               = model.TotalAWB;
                importShipment.ShipmentTypeId         = model.ShipmentTypeId;
                importShipment.DestinationAirportCity = model.DestinationAirportCity;
                importShipment.OriginAirportCity      = model.OriginAirportCity;
                importShipment.Type                   = "import"; //not used
                if (model.ID == 0)
                {
                    db.ImportShipments.Add(importShipment);
                    db.SaveChanges();
                }
                else
                {
                    db.Entry(importShipment).State = EntityState.Modified;
                    db.SaveChanges();
                }

                var max = db.ImportShipmentDetails.Select(x => x.ShipmentDetailID).DefaultIfEmpty(0).Max() + 1;
                if (model.Shipments != null)

                {
                    //model.Shipments.ForEach(x =>
                    //{
                    //    x.ShipmentDetailID = max;
                    //    x.ImportID = importShipment.ID;
                    //    x.Status = 1;
                    //    max++;
                    //});
                    foreach (var det in model.Shipments)
                    {
                        if (det.ShipmentDetailID == 0)
                        {
                            det.HAWB             = "";
                            det.ShipmentDetailID = max;
                            det.ImportID         = importShipment.ID;
                            var couriercstatus = db.CourierStatus.Where(c => c.CourierStatus == "Export Manifest Prepared").FirstOrDefault();
                            if (couriercstatus != null)
                            {
                                det.CourierStatusID = couriercstatus.CourierStatusID;
                            }
                            var statustype = db.tblStatusTypes.Where(c => c.Name == "READY TO EXPORT").FirstOrDefault();
                            if (statustype != null)
                            {
                                det.StatusTypeId = statustype.ID;
                            }

                            db.ImportShipmentDetails.Add(det);
                            db.SaveChanges();
                            max++;
                        }
                        else
                        {
                            if (det.CourierStatusID == null)
                            {
                                det.HAWB = "";
                                var couriercstatus = db.CourierStatus.Where(c => c.CourierStatus == "Export Manifest Prepared").FirstOrDefault();
                                if (couriercstatus != null)
                                {
                                    det.CourierStatusID = couriercstatus.CourierStatusID;
                                }
                            }
                            if (det.StatusTypeId == null)
                            {
                                det.HAWB = "";
                                var statustype = db.tblStatusTypes.Where(c => c.Name == "READY TO EXPORT").FirstOrDefault();
                                if (statustype != null)
                                {
                                    det.StatusTypeId = statustype.ID;
                                }
                            }
                            det.HAWB            = "";
                            det.ImportID        = importShipment.ID;
                            db.Entry(det).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                    }

                    var li            = model.Shipments.Select(x => x.ShipmentDetailID).ToList();
                    var importdetails = db.ImportShipmentDetails.Where(x => !li.Contains(x.ShipmentDetailID) && x.ImportID == importShipment.ID).ToList();
                    foreach (var import in importdetails)
                    {
                        db.ImportShipmentDetails.Remove(import);
                        //db.ImportShipmentDetails.Add(import);
                        db.SaveChanges();
                    }
                }
                else
                {
                    var remove_datas = db.ImportShipmentDetails.Where(s => s.ImportID == importShipment.ID).ToList();
                    foreach (var rdata in remove_datas)
                    {
                        db.ImportShipmentDetails.Remove(rdata);
                        db.SaveChanges();
                    }
                }
                return(RedirectToAction("Index"));
            }
            model.ConsignorName        = agent.Name;
            model.ConsignorCountryName = agent.CountryName;
            model.ConsignorCityName    = agent.CityName;
            model.ConsignorAddress     = agent.Address1 + "," + agent.Address2 + "," + agent.Address3;

            model.ConsigneeName        = company.AcCompany1;
            model.ConsigneeCityName    = company.CityName;
            model.ConsigneeCountryName = company.CountryName;
            model.ConsigneeAddress     = company.Address1 + "," + company.Address2 + "," + company.Address3;



            if (model.Shipments == null)
            {
                model.Shipments = new List <ImportShipmentDetail>();
            }
            var countries = db.CountryMasters.ToList();// db.CountryMasters.ToList();

            ViewBag.OriginCityID         = new SelectList(new List <CityMaster>(), "CityID", "City");
            ViewBag.DestinationCityID    = new SelectList(new List <CityMaster>(), "CityID", "City");
            ViewBag.OriginCountryID      = countries;
            ViewBag.DestinationCountryID = countries;
            ViewBag.CurrencyID           = db.CurrencyMasters.ToList();
            ViewBag.Cities    = db.CityMasters.ToList();
            ViewBag.Countries = db.CountryMasters.ToList();

            ViewBag.Type        = db.tblShipmentTypes.ToList();
            ViewBag.Currencies  = db.CurrencyMasters.ToList();
            ViewBag.AgentName   = agent.Name;
            ViewBag.AgentCity   = agent.CityName;
            ViewBag.CompanyName = company.AcCompany1;

            return(View(model));
        }