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;
 }
        /// <summary>
        /// Update Vehicle
        /// </summary>
        /// <param name="vehicle"></param>
        /// <param name="declerationID"></param>
        /// <returns></returns>
        public static int UpdateVehicle(tblVehicle vehicle, long declerationID)
        {
            // Set last modified date to now
            vehicle.ModifiedDate = CommonFactory.GetCurrentDate();
            var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
            switch (declerationID)
            {
                case 0:
                    {
                        // Get orgin vehicle
                        var vehicleOrgin = db.tblVehicles.Where(g => g.VehicleID == vehicle.VehicleID).FirstOrDefault();
                        if (vehicleOrgin != null)
                        {
                            db.Attach(vehicleOrgin);
                            db.ApplyPropertyChanges("tblVehicles", vehicle);

                            if (vehicle.ListVehicleChangeGood != null)
                            {
                                //update tblVehicleChange 
                                VehicleFactory.DeleteVehicleChangeByVehicleId(vehicle.VehicleID);
                                VehicleFactory.AddVehicleChangeByVehicleId(vehicle.VehicleID, vehicle.ListVehicleChangeGood.Select(x => x.VehicleId).ToList());
                            }
                            db.Connection.Close();
                            
                            int re = db.SaveChanges();
                            return re;
                        }
                        return -1;
                    }
                default:
                    {
                        var vehicleOrgin = db.tblVehicles.Where(g => g.VehicleID == vehicle.VehicleID).FirstOrDefault();
                        db.Attach(vehicleOrgin);
                        db.ApplyPropertyChanges("tblVehicles", vehicle);
                        //db.SaveChanges();
                        // Insert to tblVehicleDeclerateion
                        var vehicleDeclara = new tblDeclarationVehicle();
                        vehicleDeclara.VehicleID = vehicle.VehicleID;
                        vehicleDeclara.DeclarationID = declerationID;
                        db.AddTotblDeclarationVehicles(vehicleDeclara);
                        int re = db.SaveChanges();
                        db.Connection.Close();
                        db.Dispose();
                        return re;

                    }
            }
        }
 public static int InsertVehicle(tblVehicle vehicle, long declarationID)
 {
     // Update created, last modified date by now
     var now = CommonFactory.GetCurrentDate();
     vehicle.CreatedDate = now;
     vehicle.ModifiedDate = now;
     var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     db.tblDeclarations.Where(g => g.DeclarationID == declarationID).FirstOrDefault();
     db.AddTotblVehicles(vehicle);
     db.SaveChanges();
     // Insert to tblVehicleDeclerate
     var vehicleDeclere = new tblDeclarationVehicle();
     vehicleDeclere.VehicleID = vehicle.VehicleID;
     vehicleDeclere.DeclarationID = declarationID;
     db.AddTotblDeclarationVehicles(vehicleDeclere);
     int re = db.SaveChanges();
     db.Connection.Close();
     return re;
 }
        /// <summary>
        /// Insert to Declaration and list vehicle
        /// </summary>
        /// <param name="declarationInfo"></param>
        /// <param name="vehicleInfos"></param>
        /// <param name="listVehicleUpdate"></param>
        /// <param name="userID"></param>
        public static int AddDeclaration(tblDeclaration declarationInfo, List<tblVehicle> vehicleInfos, List<tblVehicle> listVehicleUpdate, int userID)
        {
            var result = -1;
            var currentDate = CommonFactory.GetCurrentDate();
            var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));

            //--------------Luu vào bảng tờ khai ----------------
            //declarationInfo.tblUser = db.tblUsers.Where(g => g.UserID.Equals(userID)).FirstOrDefault();
            // Set Created date and Last modified date
            declarationInfo.CreatedDate = currentDate;
            declarationInfo.ModifiedDate = currentDate;
            declarationInfo.CreatedByID = userID;
            db.AddTotblDeclarations(declarationInfo);
            db.SaveChanges();
            // Return if insert fail
            if (declarationInfo.DeclarationID <= 0) return -1;
            
            // -------- Trong trường hợp đây là tạo mới, thì add vehicle ---------------
            // Add vehicle
            foreach (var vehicle in vehicleInfos)
            {
                // Update movidifedDate and Created date
                vehicle.CreatedDate = currentDate;
                vehicle.ModifiedDate = currentDate;
                db.AddTotblVehicles(vehicle);
                db.SaveChanges();

                //insert to the tblVehicleChange
                List<long> listVehicleId = new List<long>();
                if (vehicle.ListVehicleChangeGood != null)
                {
                    listVehicleId= vehicle.ListVehicleChangeGood.Select(x => x.VehicleId).ToList();
                }
                VehicleFactory.AddVehicleChangeByVehicleId(vehicle.VehicleID, listVehicleId);
                
                // Insert to the tblVehicleDeclerateion
                var vehicleDeclara = new tblDeclarationVehicle();
                vehicleDeclara.VehicleID = vehicle.VehicleID;
                vehicleDeclara.DeclarationID = declarationInfo.DeclarationID;
                db.AddTotblDeclarationVehicles(vehicleDeclara);
                db.SaveChanges();
                // Add data to tblDecleVehicle
            }

            // -------- Trong trường hợp đây là cập nhật, thì add vehicle ---------------

            // Update the vehicle
            if (listVehicleUpdate.Count > 0)
            {
                foreach (var vehicle in listVehicleUpdate)
                {
                    // Update Vehicle info
                    VehicleFactory.UpdateVehicle(vehicle, 0);
                    // Add to tblDeclerateVehcle
                    var declerartionTem =
                        db.tblDeclarations.Where(g => g.DeclarationID == declarationInfo.DeclarationID).FirstOrDefault();

                    var vehicleDeclara = new tblDeclarationVehicle();
                    vehicleDeclara.VehicleID = vehicle.VehicleID;
                    vehicleDeclara.DeclarationID = declerartionTem.DeclarationID;
                    db.AddTotblDeclarationVehicles(vehicleDeclara);
                    db.SaveChanges();
                }
            }
            db.Connection.Close();
            return result;
        }