public ActionResult Details(int?id, int?exp_id, int?item_id, string searchterm = null, int page = 1)
        {
            var model  = new Export_view();
            var userId = User.Identity.GetUserId();

            var lic_ids = from l in db.Licenses where l.UserId == userId select l.Id;
            var ids     = lic_ids.ToList();

            model.exports = db.Exports.Where(r => ((searchterm == null || r.Reference_No.StartsWith(searchterm)) && (ids.Contains(r.License_Id.Value)))).Include(k => k.License).
                            Include(i => i.Export_Country.Select(z => z.Country)).Include(y => y.Export_Item.Select(p => p.Item)).OrderBy(u => u.Reference_No).ToPagedList(page, 4);



            if (exp_id != null)
            {
                var items = db.Exports.Where(x => x.Id == exp_id.Value).Include(z => z.Export_Item.Select(y => y.Item));

                model.exp_item = items;
                ViewBag.t_id   = exp_id.Value;
            }
            if (item_id != null)
            {
                var items = db.Exports.Where(x => x.Id == item_id.Value).Include(z => z.Export_Country.Select(y => y.Country));

                model.exp_country = items;
                ViewBag.c_id      = item_id.Value;
            }


            return(View(model));
        }
        public ActionResult GetCountryDetails(int?exp_id)
        {
            var model = new Export_view();


            var country = db.Exports.Where(x => x.Id == exp_id.Value).Include(z => z.Export_Country.Select(y => y.Country));

            model.exp_country = country;
            return(PartialView("Displaycountry", model));
        }
        public ActionResult Saveedit(Export_view exportvalues)
        {
            if (ModelState.IsValid)
            {
                var export = from ex in db.Exports where ex.Id == exportvalues.export_val.Id select ex;

                export.First().Reference_No = exportvalues.export_val.Reference_No;
                export.First().Export_Date  = exportvalues.export_val.Export_Date;

                db.SaveChanges();
            }
            return(RedirectToAction("Details"));
        }
        public ActionResult DeleteExportitem(int exp_id, List <int> itemCheckedIds, List <int> countryCheckboxes)
        {
            var model = new Export_view();

            if (itemCheckedIds != null)
            {
                foreach (var itm_id in itemCheckedIds)
                {
                    Export_Item exp_item = db.Export_Item.Find(itm_id);
                    db.Export_Item.Remove(exp_item);
                    db.SaveChanges();
                }
            }
            if (countryCheckboxes != null)
            {
                foreach (var c_id in countryCheckboxes)
                {
                    Export_Country c_item = db.Export_Country.Find(c_id);
                    db.Export_Country.Remove(c_item);
                    db.SaveChanges();
                }

                var country = db.Exports.Where(x => x.Id == exp_id).Include(z => z.Export_Country.Select(y => y.Country));

                model.exp_country = country;
                //    ViewBag.t_id = exp_id.Value;

                return(PartialView("Displaycountry", model));
            }



            var items = db.Exports.Where(x => x.Id == exp_id).Include(z => z.Export_Item.Select(y => y.Item));

            model.exp_item = items;


            return(PartialView("Itemdisplay", model));
        }
        public ActionResult GetExpDetails(int?exp_id, string msg)
        {
            if (exp_id != null)
            {
                var exp_detail = from l in db.Exports
                                 where l.Id == exp_id.Value
                                 select new { ref_no = l.Reference_No, exp_date = l.Export_Date, lic_date = l.License.Expiry_Date, lic_no = l.License.License_No };
                var item_list =
                    from itm in db.Items
                    join exp in db.Export_Item on itm.Id equals exp.Item_Id
                    where exp.Exporter_Id == exp_id.Value
                    select new { item_id = itm.Id, item_name = itm.Name, units = exp.No_Of_Units };

                var model = new Export_view();

                model.msg        = msg;
                model.export_val = db.Exports.Where(x => x.Id == exp_id.Value).Include(z => z.License).Single();

                var items = db.Exports.Where(x => x.Id == exp_id.Value).Include(z => z.Export_Item.Select(y => y.Item));

                model.exp_item = items;


                if (Request.IsAjaxRequest())
                {
                    return(PartialView("Itemdisplay", model));
                }

                var country = db.Exports.Where(x => x.Id == exp_id.Value).Include(z => z.Export_Country.Select(y => y.Country));

                model.exp_country = country;



                return(View(model));
            }

            return(RedirectToAction("Details"));
        }
        public JsonResult Additem(ViewExportAdd exportvalues)
        {
            var exp_id = exportvalues.exp_id;

            ViewExportAdd model = new ViewExportAdd();
            Export_view   view  = new Export_view();

            var lic_id = db.Exports.Where(z => z.Id == exp_id).Include(x => x.License).Single();

            if (exportvalues.Selectedcountrylist != null)
            {
                foreach (var i in exportvalues.Selectedcountrylist)
                {
                    var c_add      = new Export_Country();
                    var item_found = from itm in db.Export_Country where itm.Country_Id == i && itm.Export_Id == exp_id select itm;

                    if (item_found.Any())
                    {
                        var item_name = from itm in db.Countries where itm.Id == i select itm.Name;
                        model.msg = "Item - " + item_name.FirstOrDefault() + "item already added ";
                        view.msg  = model.msg;
                        //return RedirectToAction("GetExpDetails",new { exp_id = exp_id.Value, msg = model.msg });
                        break;
                    }


                    else
                    {
                        c_add.Country_Id = i;

                        c_add.Export_Id = exp_id;
                        db.Export_Country.Add(c_add);
                        db.SaveChanges();
                        model.msg = "country Added";
                    }
                }
            }
            if (exportvalues.SelectedItems != null)
            {
                foreach (var i in exportvalues.SelectedItems)
                {
                    var items_add   = new Export_Item();
                    var units_total = from itm in db.License_Item where itm.Item_Id == i && itm.License_Id == lic_id.License_Id select itm.No_Units;
                    var exp_ids     = db.Exports.Where(z => z.License_Id == lic_id.License_Id).Select(y => y.Id);

                    var sum_units = from f in db.Export_Item
                                    where exp_ids.Contains(f.Exporter_Id)
                                    group f by f.Item_Id into g
                                    orderby g.Sum(x => x.No_Of_Units)
                                    select
                                    new
                    {
                        item_code = g.Key,
                        //item_value = g.First().,
                        total = g.Sum(x => x.No_Of_Units)
                    };

                    var sum = 0;

                    foreach (var n in sum_units)
                    {
                        if (n.item_code == i)
                        {
                            sum = n.total;
                            break;
                        }
                    }

                    var units_allowed = (units_total.SingleOrDefault()) - sum;
                    if ((
                            units_allowed) < exportvalues.No_Of_Units)
                    {
                        model.msg = "no of units can't  exceed than" + (
                            units_allowed);
                        view.msg = model.msg;
                        break;
                        //  return RedirectToAction("GetExpDetails", new { exp_id = exp_id.Value, msg = model.msg });
                    }
                    var item_found = from itm in db.Export_Item where itm.Item_Id == i && itm.Exporter_Id == exp_id select itm;

                    if (item_found.Any())
                    {
                        item_found.First().No_Of_Units = exportvalues.No_Of_Units;
                        db.SaveChanges();
                        var item_name = from itm in db.Items where itm.Id == i select itm.Name;
                        model.msg = "Item - " + item_name.FirstOrDefault() + "item updated ";
                        view.msg  = model.msg;
                        //return RedirectToAction("GetExpDetails",new { exp_id = exp_id.Value, msg = model.msg });
                        break;
                    }


                    else
                    {
                        items_add.Item_Id     = i;
                        items_add.No_Of_Units = exportvalues.No_Of_Units;
                        items_add.Exporter_Id = exp_id;
                        db.Export_Item.Add(items_add);
                        db.SaveChanges();
                        model.msg = "item Added";
                    }
                }
            }


            model.exp_id = exp_id;
            //  model.Items= new SelectListItem(item_list.ToList(), "Id", "Name")



            // db.SaveChanges();
            // return PartialView("EdititemPartialView", model);
            // return View(model);
            //   return RedirectToAction("GetExpDetails", new { exp_id = exp_id.Value, msg = "item added" });
            return(Json(new { msg = model.msg }, JsonRequestBehavior.AllowGet));
        }