Example #1
0
        public ActionResult ContributionStatements(DateTime?fromDate, DateTime?endDate, string startswith, string sort, int?tagid, bool excludeelectronic, string customstatement = null, bool exportcontributors = false)
        {
            if (!fromDate.HasValue || !endDate.HasValue)
            {
                return(Content("<h3>Must have a Startdate and Enddate</h3>"));
            }
            var runningtotals = new ContributionsRun
            {
                Started   = DateTime.Now,
                Count     = 0,
                Processed = 0
            };
            var cs = Models.Report.ContributionStatements.GetStatementSpecification(customstatement);

            if (!startswith.HasValue())
            {
                startswith = null;
            }
            if (exportcontributors)
            {
                var        db          = DbUtil.Db;
                var        noaddressok = !db.Setting("RequireAddressOnStatement", true);
                const bool useMinAmt   = true;
                if (tagid == 0)
                {
                    tagid = null;
                }
                var qc = APIContribution.Contributors(db, fromDate.Value, endDate.Value, 0, 0, 0, cs.Funds, noaddressok, useMinAmt, startswith, sort, tagid: tagid, excludeelectronic: excludeelectronic);
                return(ExcelExportModel.ToDataTable(qc.ToList()).ToExcel("Contributors.xlsx"));
            }
            DbUtil.Db.ContributionsRuns.InsertOnSubmit(runningtotals);
            DbUtil.Db.SubmitChanges();
            var cul  = DbUtil.Db.Setting("Culture", "en-US");
            var host = Util.Host;

            var output = Output();

            if (tagid == 0)
            {
                tagid = null;
            }

            HostingEnvironment.QueueBackgroundWorkItem(ct =>
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(cul);
                Thread.CurrentThread.CurrentCulture   = CultureInfo.CreateSpecificCulture(cul);
                var m = new ContributionStatementsExtract(host, fromDate.Value, endDate.Value, output, startswith, sort, tagid, excludeelectronic);
                m.DoWork(cs);
            });
            return(Redirect("/Statements/Progress"));
        }
        public ActionResult ContributionStatements(DateTime?fromDate, DateTime?endDate, string startswith, string sort, int?tagid, bool excludeelectronic, string customstatement = null, bool exportcontributors = false)
        {
            if (!fromDate.HasValue || !endDate.HasValue)
            {
                return(Content("<h3>Must have a Startdate and Enddate</h3>"));
            }

            if (fromDate.Value > endDate.Value)
            {
                return(Content("<h3>The Startdate must be earlier than the Enddate</h3>"));
            }

            var spec = ContributionStatementsExtract.GetStatementSpecification(CurrentDatabase, customstatement);

            if (!startswith.HasValue())
            {
                startswith = null;
            }
            var        noaddressok = !CurrentDatabase.Setting("RequireAddressOnStatement", true);
            const bool useMinAmt   = true;

            if (tagid == 0)
            {
                tagid = null;
            }
            var qc           = APIContribution.Contributors(CurrentDatabase, fromDate.Value, endDate.Value, 0, 0, 0, spec.Funds, noaddressok, useMinAmt, startswith, sort, tagid: tagid, excludeelectronic: excludeelectronic);
            var contributors = qc.ToList();

            if (exportcontributors)
            {
                return(ExcelExportModel.ToDataTable(contributors).ToExcel("Contributors.xlsx"));
            }
            var statementsRun = new ContributionsRun
            {
                Started   = DateTime.Now,
                Count     = contributors.Count,
                Processed = 0,
                UUId      = Guid.NewGuid(),
                UserId    = CurrentDatabase.UserId,
            };

            CurrentDatabase.ContributionsRuns.InsertOnSubmit(statementsRun);
            CurrentDatabase.SubmitChanges();
            var cul  = CurrentDatabase.Setting("Culture", "en-US");
            var host = CurrentDatabase.Host;
            var id   = $"{statementsRun.UUId:n}";

            var output = Output(host, id);

            if (tagid == 0)
            {
                tagid = null;
            }

            var showCheckNo = CurrentDatabase.Setting("RequireCheckNoOnStatement");
            var showNotes   = CurrentDatabase.Setting("RequireNotesOnStatement");
            var statements  = new ContributionStatements
            {
                UUId     = Guid.Parse(id),
                FromDate = fromDate.Value,
                ToDate   = endDate.Value,
                typ      = 3,
                //TODO: once we switch to entirely html-based statement templates we won't need to check for these options
                NumberOfColumns = showCheckNo || showNotes ? 1 : 2,
                ShowCheckNo     = showCheckNo,
                ShowNotes       = showNotes,
            };

            if (CurrentDatabase.Setting("UseNewStatementsFormat"))
            {
                // Must do this before entering the background worker because it relies on the Application context
                statements.GetConverter();
            }

            var elmah = Elmah.ErrorLog.GetDefault(System.Web.HttpContext.Current);

            HostingEnvironment.QueueBackgroundWorkItem(ct =>
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(cul);
                Thread.CurrentThread.CurrentCulture   = CultureInfo.CreateSpecificCulture(cul);
                try
                {
                    var m = new ContributionStatementsExtract(host, fromDate.Value, endDate.Value, output, startswith, sort, tagid, excludeelectronic)
                    {
                        id = id
                    };
                    m.DoWork(statements, spec, contributors);
                }
                catch (Exception e)
                {
                    elmah.Log(new Elmah.Error(e));
                    var db        = CMSDataContext.Create(host);
                    var run       = db.ContributionsRuns.Single(c => c.UUId == Guid.Parse(id));
                    run.Error     = e.Message;
                    run.Completed = DateTime.Now;
                    db.SubmitChanges();
                }
            });
            return(Redirect($"/Statements/Progress/{id}"));
        }