예제 #1
0
        /// <summary>
        /// Insert to Declaration and list vehicle
        /// </summary>
        /// <param name="declarationInfo"></param>
        /// <param name="vehicleInfos"></param>
        public int AddDeclaration(tblDeclaration declarationInfo, List<tblVehicle> vehicleInfos, int userID)
        {
            var result = -1;

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

            declarationInfo.tblUser = db.tblUsers.Where(g => g.UserID.Equals(userID)).FirstOrDefault();
            // Set Created date and Last modified date
            declarationInfo.CreatedDate = DateTime.Now;
            declarationInfo.ModifiedDate = DateTime.Now;
            db.AddTotblDeclarations(declarationInfo);
            db.SaveChanges();
            // Return if insert fail
            if(declarationInfo.DeclarationID <= 0) return - 1;
            var declarationID = declarationInfo.DeclarationID;
            // Add vehicle
            foreach (var vehicle in vehicleInfos)
            {
                vehicle.tblDeclaration = declarationInfo;
                db.AddTotblVehicles(vehicle);
                db.SaveChanges();
            }
            return result;
        }
 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;
        }