/// <summary>
        /// Metoto encargado de guardar o actualizar un vehiculo
        /// en la base de datos de systime
        /// </summary>
        /// <param name="entityVehicle"></param>
        /// <param name="userModify"></param>
        /// <returns></returns>
        public Vehicles SaveVehicle(Vehicles entityVehicle, String userModify)
        {
            Vehicles currentVehicle     = null;
            Boolean  changeUpdateEntity = false;

            if (entityVehicle != null)
            {
                using (SystimedbEntities Systimedb = new SystimedbEntities(ConectionString))
                {
                    List <Vehicles> entitiesCurrentVehicles = Systimedb.Vehicles.Where(v => v.Plate == entityVehicle.Plate).ToList();
                    if (entitiesCurrentVehicles == null || entitiesCurrentVehicles.Count == 0)
                    {
                        entitiesCurrentVehicles = Systimedb.Vehicles.Where(v => v.IdVinNumber == entityVehicle.IdVinNumber).ToList();
                    }
                    if (entitiesCurrentVehicles.Count > 0)
                    {
                        if (entitiesCurrentVehicles.Count == 1)
                        {
                            currentVehicle = entitiesCurrentVehicles[0];
                        }
                        else if (entitiesCurrentVehicles.Count > 1)
                        {
                            currentVehicle = entitiesCurrentVehicles.FirstOrDefault(v => v.IdCity == entityVehicle.IdCity);
                        }

                        if (currentVehicle != null)
                        {
                            //Comienza el proceso de actualizacion

                            //Entity framwwork comienza a hacer seguimiento a la entidad
                            Systimedb.Vehicles.Attach(currentVehicle);
                            //Cambia el etado a modificado
                            currentVehicle.Color = UtilsDataAcces.ValidateDiferentString(currentVehicle.Color, entityVehicle.Color, false, changeUpdateEntity, out changeUpdateEntity);

                            currentVehicle.DealerVehicleModel = UtilsDataAcces.ValidateDiferentString(currentVehicle.DealerVehicleModel, entityVehicle.DealerVehicleModel, true, changeUpdateEntity, out changeUpdateEntity);

                            currentVehicle.IdCity = (UtilsDataAcces.ValidateDiferentInt(currentVehicle.IdCity, entityVehicle.IdCity, false, changeUpdateEntity, out changeUpdateEntity)) ?? 0;

                            currentVehicle.IdInsuranceCompany = UtilsDataAcces.ValidateDiferentInt(currentVehicle.IdInsuranceCompany, entityVehicle.IdInsuranceCompany, true, changeUpdateEntity, out changeUpdateEntity);

                            currentVehicle.IdVehicleModel = (UtilsDataAcces.ValidateDiferentInt(currentVehicle.IdVehicleModel, entityVehicle.IdVehicleModel, false, changeUpdateEntity, out changeUpdateEntity)) ?? 0;

                            currentVehicle.IdVehicleServiceType = (UtilsDataAcces.ValidateDiferentInt(currentVehicle.IdVehicleServiceType, entityVehicle.IdVehicleServiceType, false, changeUpdateEntity, out changeUpdateEntity));

                            currentVehicle.IdVehicleType = (UtilsDataAcces.ValidateDiferentInt(currentVehicle.IdVehicleType, entityVehicle.IdVehicleType, false, changeUpdateEntity, out changeUpdateEntity));

                            if (currentVehicle.LastMileage != entityVehicle.LastMileage && entityVehicle.LastMileage != null && currentVehicle.LastMileage < entityVehicle.LastMileage)
                            {
                                currentVehicle.LastMileage = entityVehicle.LastMileage;
                                changeUpdateEntity         = true;
                            }

                            currentVehicle.ModelYear = (UtilsDataAcces.ValidateDiferentInt(currentVehicle.ModelYear, entityVehicle.ModelYear, false, changeUpdateEntity, out changeUpdateEntity)).Value;

                            currentVehicle.Plate = UtilsDataAcces.ValidateDiferentString(currentVehicle.Plate, entityVehicle.Plate, false, changeUpdateEntity, out changeUpdateEntity);

                            currentVehicle.DealerCity = UtilsDataAcces.ValidateDiferentString(currentVehicle.DealerCity, entityVehicle.DealerCity, true, changeUpdateEntity, out changeUpdateEntity);

                            currentVehicle.IdCustomerOwner = (UtilsDataAcces.ValidateDiferentDecimal(currentVehicle.IdCustomerOwner, entityVehicle.IdCustomerOwner, true, changeUpdateEntity, out changeUpdateEntity)).Value;

                            currentVehicle.SaleGuaranteeAt = UtilsDataAcces.ValidateDiferentDateTime(currentVehicle.SaleGuaranteeAt, entityVehicle.SaleGuaranteeAt, changeUpdateEntity, out changeUpdateEntity);

                            currentVehicle.GuaranteedSaleDistanceTraveled = UtilsDataAcces.ValidateDiferentInt(currentVehicle.GuaranteedSaleDistanceTraveled, entityVehicle.GuaranteedSaleDistanceTraveled, false, changeUpdateEntity, out changeUpdateEntity);

                            if (changeUpdateEntity)
                            {
                                currentVehicle.UpdatedAt              = DateTime.Now;
                                currentVehicle.UpdatedById            = userModify;
                                Systimedb.Entry(currentVehicle).State = EntityState.Modified;
                                Systimedb.SaveChanges();
                            }
                            return(currentVehicle);
                        }
                    }
                    else
                    {
                        entityVehicle.CreatedAt   = DateTime.Now;
                        entityVehicle.CreatedById = userModify;
                        Systimedb.Vehicles.Add(entityVehicle);
                        if (Systimedb.SaveChanges() > 0)
                        {
                            return(entityVehicle);
                        }
                    }
                }
            }
            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Metoto encargado de grabar o actualizar un entitdad de tipo Customer
        /// en la base de datos de systime
        /// </summary>
        /// <param name="entityCustomers">entidad customer</param>
        /// <returns>id en la base de datos asignado a customer</returns>
        public Customers SaveCustomer(Customers entityCustomers, String userModify)
        {
            Boolean changeUpdateEntity = false;

            if (entityCustomers != null)
            {
                using (SystimedbEntities Systimedb = new SystimedbEntities(ConectionString))
                {
                    List <Customers> entitiesCurrent = Systimedb.Customers.Where(c => c.DocumentNumber == entityCustomers.DocumentNumber && c.IdCountry == entityCustomers.IdCountry).ToList();
                    Customers        entitieCurrent  = null;
                    if (entitiesCurrent.Count > 0)
                    {
                        if (entitiesCurrent.Count == 1)
                        {
                            entitieCurrent = entitiesCurrent[0];
                        }
                        else if (entitiesCurrent.Count > 1)
                        {
                            entitieCurrent = entitiesCurrent.FirstOrDefault(c => c.IdCountry == entityCustomers.IdCountry);
                        }

                        if (entitieCurrent != null)
                        {
                            //Entity framwwork comienza a hacer seguimiento a la entidad
                            Systimedb.Customers.Attach(entitieCurrent);
                            //Cambia el etado a modificado

                            entitieCurrent.IdCountry = UtilsDataAcces.ValidateDiferentString(entitieCurrent.IdCountry, entityCustomers.IdCountry, true, changeUpdateEntity, out changeUpdateEntity);

                            entitieCurrent.IdCity = (UtilsDataAcces.ValidateDiferentInt(entitieCurrent.IdCity, entityCustomers.IdCity, false, changeUpdateEntity, out changeUpdateEntity)).Value;

                            entitieCurrent.Email = UtilsDataAcces.ValidateDiferentString(entitieCurrent.Email, entityCustomers.Email, false, changeUpdateEntity, out changeUpdateEntity);

                            entitieCurrent.FullName = UtilsDataAcces.ValidateDiferentString(entitieCurrent.FullName, entityCustomers.FullName, false, changeUpdateEntity, out changeUpdateEntity);

                            entitieCurrent.DocumentNumber = UtilsDataAcces.ValidateDiferentString(entitieCurrent.DocumentNumber, entityCustomers.DocumentNumber, false, changeUpdateEntity, out changeUpdateEntity);

                            entitieCurrent.Mobile = UtilsDataAcces.ValidateDiferentString(entitieCurrent.Mobile, entityCustomers.Mobile, false, changeUpdateEntity, out changeUpdateEntity);

                            entitieCurrent.Phone = UtilsDataAcces.ValidateDiferentString(entitieCurrent.Phone, entityCustomers.Phone, false, changeUpdateEntity, out changeUpdateEntity);


                            if (changeUpdateEntity)
                            {
                                Systimedb.Entry(entitieCurrent).State = EntityState.Modified;
                                entitieCurrent.UpdatedAt = DateTime.Now;
                                Systimedb.SaveChanges();
                            }
                            return(entitieCurrent);
                        }
                    }
                    else
                    {
                        entityCustomers.CreatedById = userModify;
                        entityCustomers.CreatedAt   = DateTime.Now;
                        Systimedb.Customers.Add(entityCustomers);

                        if (Systimedb.SaveChanges() > 0)
                        {
                            return(entityCustomers);
                        }
                    }
                }
            }
            return(null);
        }