public static int Update(tblVehicleType vehicletype)
 {
     var _db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     _db.Connection.Open();
     var _vehicletype = (from vt in _db.tblVehicleTypes
                         where vt.VehicleTypeID == vehicletype.VehicleTypeID
                         select vt).First();
     _vehicletype.Name = vehicletype.Name;
     _vehicletype.Capacity = vehicletype.Capacity;
     _vehicletype.Description = vehicletype.Description;
     vehicletype.UpdatedDate = CommonFactory.GetCurrentDate();
     try
     {
         if (_db.Connection.State == ConnectionState.Closed) _db.Connection.Open();
         _db.SaveChanges();
     }
     catch (Exception ex)
     {
         return 1;
     }
     finally
     {
         if (_db.Connection.State == ConnectionState.Closed) _db.Connection.Open();
         _db.Connection.Close();
     }
     return 0;
 }
 /// <summary>
 /// Delete Decleration by ID
 /// </summary>
 /// <param name="declerationID">DeclerationID</param>
 /// <returns>Number of rows are effected</returns>
 public static int DeleteByID(int declerationID)
 {
     var db = new dbEcustomEntities(Utilities.Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     var declaration = db.tblDeclarations.Where(g => g.DeclarationID == declerationID).FirstOrDefault();
     db.DeleteObject(declaration);
     return db.SaveChanges();
 }
 /// <summary>
 /// Delete vehicle by ID
 /// </summary>
 /// <param name="vehicleID">Vehicle ID</param>
 /// <returns>Number of rows are effected</returns>
 public static int DeleteByID(int vehicleID)
 {
     var db = new dbEcustomEntities(Utilities.Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     var vehicle = db.tblVehicles.Where(g => g.VehicleID == vehicleID).FirstOrDefault();
     db.DeleteObject(vehicle);
     return db.SaveChanges();
 }
        public bool DeleteItem(string[] itemParams)
        {
            if (itemParams.Length < 2) return false;

            string id = itemParams[0];
            string branchId = itemParams[1];

            var _db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));

            try
            {
                var deleteItem =
                    _db.tblPermissionTypes.FirstOrDefault(
                        item => item.TypeCode == id && item.BranchId == branchId);
                if (deleteItem != null)
                {
                    _db.DeleteDirectly(deleteItem);
                    _db.SaveChanges();
                }

                return true;
            }
            catch (Exception exception)
            {
                LogManager.GetLogger("ECustoms.PermissionTypeFactory").Error(exception.ToString());
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }
        public bool DeleteItem(string[] itemParams)
        {
            if (itemParams.Length < 3) return false;

            int id = itemParams[0].StringToInt();
            int verhiceId = itemParams[1].StringToInt();
            string branchId = itemParams[2];

            var _db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));

            try
            {
                var deleteItem =
                    _db.tblDeclarationVehicles.FirstOrDefault(
                        item => item.DeclarationID == id && item.VehicleID == verhiceId && item.BranchId == branchId);
                if (deleteItem != null)
                {
                    _db.DeleteDirectly(deleteItem);
                    _db.SaveChanges();
                }

                return true;
            }
            catch (Exception exception)
            {
                LogManager.GetLogger("ECustoms.DeclarationVehicleFactory").Error(exception.ToString());
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }
        public bool BatchInsert(object[] items)
        {
            var _db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));

            try
            {
                _db.Connection.Open();
                foreach (object item in items)
                {
                    _db.AddObjectDirectly("tblProfileConfigs", item);
                }

                _db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.GetLogger("ECustoms.AllFactory").Error(ex.ToString());
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }

            return false;
        }
 public static int Insert(tblUserInGroup userGroup)
 {
   var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
   db.AddTotblUserInGroups(userGroup);
   int re= db.SaveChanges();
   db.Connection.Close();
   return re;
 }
 /// <summary>
 /// Update Vehicle
 /// </summary>
 /// <param name="vehicle"></param>
 /// <returns></returns>
 public static int UpdateVehicle(tblVehicle vehicle )
 {
     var db = new dbEcustomEntities(Utilities.Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     // Get orgin vehicle
     var vehicleOrgin = db.tblVehicles.Where(g => g.VehicleID == vehicle.VehicleID).FirstOrDefault();
     db.Attach(vehicleOrgin);
     db.ApplyPropertyChanges("tblVehicles", vehicle);
     return db.SaveChanges();
 }
 public  static int Insert(tblVehicleCheck tblVehicleCheck)
 {
     var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     tblVehicleCheck.CreatedDate = CommonFactory.GetCurrentDate();
     db.AddTotblVehicleChecks(tblVehicleCheck);
     int re = db.SaveChanges();
     db.Connection.Close();
     return re;
 }
 public static int DeleteUserInGroup(int groupID, int userID)
 {
   var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
   tblUserInGroup tblUserInGroup = db.tblUserInGroups.Where(g => g.GroupID == groupID && g.UserID == userID).FirstOrDefault();
   if(tblUserInGroup != null)
     db.DeleteObject(tblUserInGroup);
   int re = db.SaveChanges();
   db.Connection.Close();
   return re;
 }
 public static int Insert(tblGroup group)
 {
     var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     group.CreatedDate = CommonFactory.GetCurrentDate();
     group.ModifiedDate = CommonFactory.GetCurrentDate();
     db.AddTotblGroups(group);
     int re = db.SaveChanges();
     db.Connection.Close();
     return re;
 }
 public static int Delete(long checkID)
 {
     var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     var check = db.tblChecks.Where(g => g.CheckID == checkID).FirstOrDefault();
     if (check == null) return -1;
     db.DeleteObject(check);
     int re = db.SaveChanges();
     db.Connection.Close();
     return re;
 }
 /// <summary>
 /// update declaration
 /// </summary>
 /// <param name="declaration"></param>
 /// <param name="modifiedByID"></param>
 /// <returns></returns>
 public static int Update(tblDeclaration declaration)
 {
     var db = new dbEcustomEntities(Utilities.Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     declaration.ModifiedDate = DateTime.Now;
     // Get orgin object
     var objOrginDeclaration = db.tblDeclarations.Where(g => g.DeclarationID.Equals(declaration.DeclarationID)).FirstOrDefault();
     db.Attach(objOrginDeclaration);
     db.ApplyPropertyChanges("tblDeclarations", declaration);
     return db.SaveChanges();
 }
 /// <summary>
 /// Insert User
 /// </summary>
 /// <param name="userInfo">UserInfo object</param>
 /// <returns>Number of rows are effected</returns>
 public static int Insert(tblUser userInfo)
 {
     // Encode password & username
     userInfo.Password = Common.Encode(userInfo.Password);
     var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     db.AddTotblUsers(userInfo);
     int re = db.SaveChanges();
     db.Connection.Close();
     return re;
 }
        public static bool UpdateSettings(string  version, DateTime lastSync, int interval, string  syncTime, bool  isStartSync)
        {
            dbEcustomEntities _db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
            try
            {
                if (_db.Connection.State == ConnectionState.Closed) _db.Connection.Open();

                var existing = _db.tblSettings.FirstOrDefault(p => p.Version == version);
                if(existing==null)
                {
                    tblSetting setting = new tblSetting();
                    setting.Version = version;
                    setting.LastSync = lastSync;
                    setting.SyncInterval = interval;
                    setting.SyncTime = syncTime;
                    setting.IsStartingSync = isStartSync;
                    _db.AddTotblSettings(setting);

                    return _db.SaveChanges() > 0;
                }
                else
                {
                    //existing.Version = version;
                    existing.LastSync = lastSync;
                    existing.SyncInterval = interval;
                    existing.SyncTime = syncTime;
                    existing.IsStartingSync = isStartSync;

                    return _db.SaveChanges() > 0;
                }
            }
            catch(Exception e)
            {
                return false;
            }
            finally
            {
                _db.Connection.Close();
            }

            return false;
        }
 public static int Update(tblGroup group)
 {          
   var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
   var groupOrgin = db.tblGroups.Where(g => g.GroupID == group.GroupID).FirstOrDefault();
   if (groupOrgin == null) return -1;
   db.Attach(groupOrgin);
   db.ApplyPropertyChanges("tblGroups", group);
   int re = db.SaveChanges();
   db.Connection.Close();
   return re;
 }
 public  static int Insert(long vehicleID, long declarationID)
 {
     var db = new dbEcustomEntities(Utilities.Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     var declarationVehicle = new tblDeclarationVehicle();
     declarationVehicle.VehicleID = vehicleID;
     declarationVehicle.DeclarationID = declarationID;
     db.AddTotblDeclarationVehicles(declarationVehicle);
     int re = db.SaveChanges();
     db.Connection.Close();
     return re;
 }
 public static int Update(tblVehicleCheck vehicleCheck)
 {
     var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));            
     // Get orgin object
     var objOrginVehicleCheck = db.tblVehicleChecks.Where(g => g.VehicleCheckID == vehicleCheck.VehicleCheckID).FirstOrDefault();
     db.Attach(objOrginVehicleCheck);
     db.ApplyPropertyChanges("tblVehicleChecks", vehicleCheck);
     int re = db.SaveChanges();
     db.Connection.Close();
     return re;        
 }
    public static int Insert(List<tblUserGroupPermission> listUserGroupPermission)
    {
      var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));

      foreach (tblUserGroupPermission userGroupPermission in listUserGroupPermission)
      {
        db.AddTotblUserGroupPermissions(userGroupPermission);
      }
      int re = db.SaveChanges();
      db.Connection.Close();
      return re;
    }
 public static int DeleteUserInGroupByGroupID(Int32 groupID)
 {
   var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
   List<tblUserInGroup> listTblUserInGroup = db.tblUserInGroups.Where(g => g.GroupID == groupID).ToList();
   foreach (tblUserInGroup userInGroup in listTblUserInGroup)
   {
     db.DeleteObject(userInGroup);
   }
   int re = db.SaveChanges();
   db.Connection.Close();
   return re;
 }
 public static int DeleteUserGroupPermissionByUserID(int userID)
 {
   var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
   List<tblUserGroupPermission> listUserGroupPermission = db.tblUserGroupPermissions.Where(g => g.UserID == userID && g.PermissionType == PERMISSION_TYPE_USER).ToList();
   foreach (tblUserGroupPermission userGroupPermission in listUserGroupPermission)
   {
     db.DeleteObject(userGroupPermission);
   }
   int re = db.SaveChanges();
   db.Connection.Close();
   return re;
 }
 public static int Update(tblCheck check)
 {
     check.ModifedDate = CommonFactory.GetCurrentDate();
     var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     var checkOrgin = db.tblChecks.Where(g => g.CheckID == check.CheckID).FirstOrDefault();
     if (checkOrgin == null) return -1;
     db.Attach(checkOrgin);
     db.ApplyPropertyChanges("tblChecks", check);
     int re = db.SaveChanges();
     db.Connection.Close();
     return re;
 }
 public static int Update(tblDeclarationLoan loan)
 {
     var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     loan.ModifiedDate = CommonFactory.GetCurrentDate();
     // Get orgin object
     var objOrginDeclaration = db.tblDeclarationLoans.Where(g => g.ID.Equals(loan.ID)).FirstOrDefault();
     db.Attach(objOrginDeclaration);
     db.ApplyPropertyChanges("tblDeclarationLoans", loan);
     int re = db.SaveChanges();
     db.Connection.Close();
     return re;
 }
        public static void DeleteVehicleByDecarationID(long declarationID)
        {
            var db = new dbEcustomEntities(Utilities.Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
            var declarationVehicles =
                db.tblDeclarationVehicles.Where((g => g.DeclarationID == declarationID)).ToList();

            foreach (var tblDeclarationVehicle in declarationVehicles)
            {
                db.DeleteObject(tblDeclarationVehicle);
                db.SaveChanges();
            }
            db.Connection.Close();
        }
        public static int Update(tblTrain train)
        {
            var _db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
            train.ModifiedDate = CommonFactory.GetCurrentDate();
            var originTrain = _db.tblTrains.Where(g => g.TrainID == train.TrainID).FirstOrDefault();

            if (originTrain == null)
            {
                return -1;
            }
            _db.Attach(originTrain);
            _db.ApplyPropertyChanges("tblTrains", train);
            
            return _db.SaveChanges();
        }
        public static int DeleteByVehicleDeclarationID(long vehicleID, long declarationID)
        {
            var db = new dbEcustomEntities(Utilities.Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
            var declarationVehicle =
                db.tblDeclarationVehicles.Where((g => g.DeclarationID == declarationID && g.VehicleID == vehicleID)).
                    FirstOrDefault();
            if (declarationVehicle != null)
            {
                db.DeleteObject(declarationVehicle);
                int re = db.SaveChanges();
                db.Connection.Close();
                return re;
            }
            return -1;

        }
 public static int Delete(long trainId)
 {
     var _db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     try
     {
         if (_db.Connection.State == ConnectionState.Closed) _db.Connection.Open();
         var train = _db.tblTrains.FirstOrDefault(g => g.TrainID == trainId);
         _db.DeleteObject(train);
         return _db.SaveChanges();
     }
     catch
     {
         return -1;
     }
     finally
     {
         _db.Connection.Close();
     }
 }
 public static int Insert(tblTrain train)
 {
     var _db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     _db.Connection.Open();
     train.CreatedDate = CommonFactory.GetCurrentDate();
     train.BranchId= FDHelper.RgCodeOfUnit();
     _db.AddTotblTrains(train);
     try
     {
         if (_db.Connection.State == ConnectionState.Closed) _db.Connection.Open();
         return _db.SaveChanges();
     }
     catch (Exception ex)
     {
         return -1;
     }
     finally
     {
         _db.Connection.Close();
     }
 }
 public static int Insert(tblType type)
 {
     dbEcustomEntities _db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     _db.Connection.Open();
     type.CreatedDate = CommonFactory.GetCurrentDate();
     type.ModifiedDate = CommonFactory.GetCurrentDate();
     _db.AddTotblTypes(type);
     try
     {
         if (_db.Connection.State == ConnectionState.Closed) _db.Connection.Open();
         return _db.SaveChanges();
     }
     catch (Exception ex)
     {
         return -1;
     }
     finally
     {
         _db.Connection.Close();
     }
 }
        public bool UpdatePatch(object[] items)
        {
            var _db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));

            try
            {
                _db.Connection.Open();
                var updateItems = items.OfType<tblApplicationObject>().ToList();

                foreach (var updateItem in updateItems)
                {
                    var item =
                        _db.tblApplicationObjects.FirstOrDefault(
                            p => p.BranchId == updateItem.BranchId && p.ApplicationObjectID == updateItem.ApplicationObjectID);
                    item.IsSynced = true;
                }
                _db.SaveChanges();
                _db.Connection.Close();
                return true;
            }
            catch (Exception ex)
            {
                LogManager.GetLogger("ECustoms.ApplicationObjectFactory").Error(ex.ToString());
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }