Beispiel #1
0
        public ActionResult GenerateTaxes()
        {
            int year       = DateTime.Now.Year;
            var properties = db.Properties.ToList();
            var taxes      = db.Taxes.ToList();

            //int i = 0;

            using (var transaction = db.Database.BeginTransaction())
            {
                try
                {
                    foreach (var property in properties)
                    {
                        var taxProperty = db.TaxProperties
                                          .Where(tp => tp.PropertyId == property.PropertyId &&
                                                 tp.Year == year)
                                          .FirstOrDefault();

                        //i++;
                        //if (i == 3)
                        //{
                        //    int a = 0;
                        //    i /= a;
                        //}

                        if (taxProperty == null)
                        {
                            var rate = taxes.Where(t => t.Stratum == property.Stratum).FirstOrDefault();
                            if (rate != null)
                            {
                                taxProperty = new TaxProperty
                                {
                                    DateGenerated = DateTime.Now,
                                    IsPay         = false,
                                    PropertyId    = property.PropertyId,
                                    Year          = year,
                                    Value         = property.Value * (decimal)rate.Rate,
                                };

                                db.TaxProperties.Add(taxProperty);
                                db.SaveChanges();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return(RedirectToAction("Fail", new { message = ex.Message }));
                }
                transaction.Commit();
            }

            return(RedirectToAction("Success"));
        }
Beispiel #2
0
        public ActionResult GenerateTaxes()
        {
            int year       = DateTime.Now.Year;
            var properties = db.Properties.ToList();
            //aqui tengo la lista de taxes en memoria:
            var taxes = db.Taxes.ToList();

            //desde aqui provoco que se reventar la aplicacion
            int i = 0;

            //aqui genero el proceso de trnassacion:
            using (var transaction = db.Database.BeginTransaction())
            {
                try
                {
                    foreach (var property in properties)
                    {
                        var taxProperty = db.TaxProperties.
                                          Where(tp => tp.PropertyId == property.PropertyId && tp.Year == year).
                                          FirstOrDefault();

                        if (taxProperty == null)
                        {
                            var rate = taxes.Where(t => t.Stratum == property.Stratum).FirstOrDefault();

                            if (rate != null)
                            {
                                taxProperty = new TaxProperty
                                {
                                    DateGenarated = DateTime.Now,
                                    IsPay         = false,
                                    PropertyId    = property.PropertyId,
                                    Year          = year,
                                    Value         = property.Value * (decimal)rate.Rate,
                                };

                                //Codigo de simulacion para reventar el programa:
                                //i++;

                                //if (i == 3)
                                //{
                                //    int a = 0;

                                //    i /= a;
                                //}

                                db.TaxProperties.Add(taxProperty);
                                db.SaveChanges();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    //si falla:
                    transaction.Rollback();
                    return(RedirectToAction("Fail", "Taxes", ex.Message));
                }

                //si todo es bueno:
                transaction.Commit();
            }//fin transacction
            return(RedirectToAction("Success"));
        }