public ActionResult ImportShipmentCreate([Bind(Include = "ID,importDate,productID,importPrice,quantity")] ImportShipment importShipment)
 {
     if (ModelState.IsValid)
     {
         db.ImportShipments.Add(importShipment);
         var inventory = db.Inventories.Where(i => i.productID == importShipment.productID).FirstOrDefault();
         if (inventory == null)
         {
             Inventory inven = new Inventory();
             inven.productID       = importShipment.productID;
             inven.productQuantity = importShipment.quantity;
             db.Inventories.Add(inven);
         }
         else
         {
             inventory.productQuantity += importShipment.quantity;
         }
         ViewBag.ErrorMessege = "<script>" +
                                "alert('Thêm thành công');" +
                                "window.location.href = '../Inventories/Index'" +
                                "</script>";
         ViewBag.productID = new SelectList(db.Products, "productID", "productName");
         db.SaveChanges();
     }
     return(View(importShipment));
 }
 public ActionResult DeleteConfirmed(int id)
 {
     try
     {
         ImportShipment s_ImportShipment     = db.ImportShipments.Find(id);
         List <ImportShipmentDetail> Details = db.ImportShipmentDetails.Where(d => d.ImportID == id).ToList();
         db.ImportShipments.Remove(s_ImportShipment);
         foreach (var det in Details)
         {
             db.ImportShipmentDetails.Remove(det);
         }
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     catch
     {
         return(RedirectToAction("Index"));
     }
 }
        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));
        }
        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));
        }