internal static string AddEditCustClear(CustomClearVm custClearVm)
        {
            string isSaved = "true";
            int    cCId    = custClearVm.CCId;

            OperationsEntities   db = new OperationsEntities();
            CustomClearanceOrder custClearDb;

            if (cCId == 0)
            {
                custClearDb = new CustomClearanceOrder();
            }
            else
            {
                custClearDb = db.CustomClearanceOrders.Where(x => x.CCId == cCId).FirstOrDefault();
            }

            Mapper.CreateMap <CustomClearVm, CustomClearanceOrder>().IgnoreAllNonExisting();
            Mapper.Map(custClearVm, custClearDb);

            if (cCId == 0)
            {
                db.CustomClearanceOrders.Add(custClearDb);
            }

            try
            {
                db.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                isSaved = "false " + e.Message;
            }
            catch (Exception e)
            {
                isSaved = "false " + e.Message;
            }

            return(isSaved);
        }
        internal static string AddEditCustClearDet(CustomClearanceDetailMainVm custClearDetailMain, string notes = "", string comment = "")
        {
            OperationsEntities db  = new OperationsEntities();
            var custClearDetaiList = custClearDetailMain.CustomClearanceDetailVms;
            int ccId = custClearDetaiList.FirstOrDefault().CCId;
            //Get costId sent from screen
            List <int> screenCostIds = custClearDetaiList.Select(x => x.CCDetailsId).ToList();
            //Get db saved list
            var custCostDbList = db.CustomClearanceDetails.Where(x => x.CCId == ccId).ToList();

            //delete removed items
            var costToDel = custCostDbList.Where(x => !screenCostIds.Contains(x.CCDetailsId)).ToList();

            foreach (var item in costToDel)
            {
                db.CustomClearanceDetails.Remove(item);
            }

            //Map the Vm to db list
            Mapper.CreateMap <CustomClearanceDetailVm, CustomClearanceDetail>().IgnoreAllNonExisting();
            Mapper.Map(custClearDetaiList, custCostDbList);

            string isSaved = "true";

            using (var dbCtx = new OperationsEntities())
            {
                foreach (var item in custCostDbList)
                {
                    if (item.CCDetailsId == 0)
                    {
                        dbCtx.CustomClearanceDetails.Add(item);
                    }
                    else
                    {
                        dbCtx.Entry(item).State = System.Data.Entity.EntityState.Modified;
                    }
                }
                try
                {
                    dbCtx.SaveChanges();

                    OperationsEntities   db2 = new OperationsEntities();
                    CustomClearanceOrder custClearDb;
                    custClearDb         = db2.CustomClearanceOrders.Where(x => x.CCId == ccId).FirstOrDefault();
                    custClearDb.Notes   = notes;
                    custClearDb.Comment = comment;
                    try {
                        db2.SaveChanges();
                    }
                    catch { }
                }
                catch (DbEntityValidationException e)
                {
                    isSaved = "false " + e.Message;
                }
                catch (Exception e)
                {
                    isSaved = "false " + e.InnerException;
                }
            }

            if (isSaved == "true")
            {
                CustomClearanceOrder custOrder = db.CustomClearanceOrders.Where(x => x.CCId == ccId).FirstOrDefault();
                custOrder.StatusId = 2;
                db.SaveChanges();
            }

            return(isSaved);
        }