context: /* Author: David Carroll
   * Copyright (c) 2008, 2009 Bellevue Baptist Church 
   * Licensed under the GNU General Public License (GPL v2)
   * you may not use this code except in compliance with the License.
   * You may obtain a copy of the License at http://bvcms.codeplex.com/license 
   */
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using iTextSharp.text;
  using iTextSharp.text.pdf;
  using System.IO;
  using System.Collections;
  using CmsData;
  using UtilityExtensions;
  using System.Text;
  using System.Text.RegularExpressions;
  using System.Web.Mvc;
  
  namespace CmsWeb.Areas.Reports.Models
  {
      public class WeeklyAttendanceResult : ActionResult
      {
          private Font monofont = FontFactory.GetFont(FontFactory.COURIER, 8);
          private Font boldfont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10);
          private Font font = FontFactory.GetFont(FontFactory.HELVETICA, 10);
          private Font smallfont = FontFactory.GetFont(FontFactory.HELVETICA, 8, new GrayColor(50));
          private Font xsmallfont = FontFactory.GetFont(FontFactory.HELVETICA, 7, new GrayColor(50));
          private PageEvent pageEvents = new PageEvent();
          private PdfPTable MainTable;
          private Document doc;
          private DateTime dt;
          private PdfContentByte dc;
  
          private Guid qid;
          public WeeklyAttendanceResult(Guid id)
          {
              qid = id;
          }
  
          public override void ExecuteResult(ControllerContext context)
          {
              var Response = context.HttpContext.Response;
              Response.ContentType = "application/pdf";
              Response.AddHeader("content-disposition", "filename=foo.pdf");
  
              dt = Util.Now;
  
              doc = new Document(PageSize.LETTER.Rotate(), 36, 36, 64, 64);
              var w = PdfWriter.GetInstance(doc, Response.OutputStream);
              w.PageEvent = pageEvents;
              doc.Open();
              dc = w.DirectContent;
  
              StartPageSet();
              if (qid != null) // print using a query
              {
                  var q = from p in DbUtil.Db.PeopleQuery(qid)
                          orderby p.Name2
                          select p;
                  foreach (var p in q)
                      AddRow(p);
                  if (MainTable.Rows.Count > 1)
                      doc.Add(MainTable);
                  else
                      doc.Add(new Phrase("no data"));
              }
              else
                  doc.Add(new Phrase("no data"));
              pageEvents.EndPageSet();
              doc.Close();
          }
  
          float[] HeaderWids = new float[] { 40 + 70 + 80, 40 + 130 };
          float[] LeftWids = new float[] { 40, 70, 80 };
          float[] RightWids = new float[] { 40, 130 };
          int border = PdfPCell.NO_BORDER; //PdfPCell.BOX;
  
          private void StartPageSet()
          {
              MainTable = new PdfPTable(HeaderWids);
              MainTable.WidthPercentage = 100;
              MainTable.DefaultCell.Border = border;
              MainTable.DefaultCell.Padding = 5;
              MainTable.HeaderRows = 1;
              pageEvents.StartPageSet("Attendance Report: {0:d}".Fmt(dt));
  
              var LeftTable = new PdfPTable(LeftWids);
              LeftTable.WidthPercentage = 100;
              LeftTable.DefaultCell.Border = border;
              LeftTable.DefaultCell.Padding = 5;
              LeftTable.AddCell(new Phrase("\nPerson", boldfont));
              LeftTable.AddCell(new Phrase("\nAddress", boldfont));
              LeftTable.AddCell(new Phrase("Phone\nEmail", boldfont));
              var cell = new PdfPCell(MainTable.DefaultCell);
              cell.AddElement(LeftTable);
              cell.Padding = 0;
              MainTable.AddCell(cell);
  
              var RightTable = new PdfPTable(RightWids);
              RightTable.WidthPercentage = 100;
              RightTable.DefaultCell.Border = border;
              RightTable.DefaultCell.Padding = 5;
              RightTable.DefaultCell.PaddingBottom = 0;
              RightTable.AddCell(new Phrase("\nBirthday", boldfont));
              RightTable.AddCell(new Phrase("\nMember Status", boldfont));
              cell = new PdfPCell(MainTable.DefaultCell);
              cell.Padding = 0;
              cell.AddElement(RightTable);
              MainTable.AddCell(cell);
          }
          BaseColor color = BaseColor.BLACK;
  
          private void AddRow(Person p)
          {
              if (color == BaseColor.WHITE)
                  color = new GrayColor(240);
              else
                  color = BaseColor.WHITE;
  
              MainTable.DefaultCell.BackgroundColor = color;
  
              var LeftTable = new PdfPTable(LeftWids);
              LeftTable.WidthPercentage = 100;
              LeftTable.DefaultCell.Border = border;
              LeftTable.DefaultCell.Padding = 5;
              var name = new Phrase(p.Name + "\n", font);
              name.Add(new Chunk("  ({0})".Fmt(p.PeopleId), smallfont));
              LeftTable.AddCell(name);
              var addr = new StringBuilder(p.PrimaryAddress);
              AddLine(addr, p.PrimaryAddress2);
              AddLine(addr, "{0}, {1} {2}".Fmt(p.PrimaryCity, p.PrimaryState, p.PrimaryZip.FmtZip()));
              LeftTable.AddCell(new Phrase(addr.ToString(), font));
              var phones = new StringBuilder();
              AddPhone(phones, p.HomePhone, "h ");
              AddPhone(phones, p.CellPhone, "c ");
              AddPhone(phones, p.WorkPhone, "w ");
              AddLine(phones, p.EmailAddress);
              LeftTable.AddCell(new Phrase(phones.ToString(), font));
  
              var c = new PdfPCell(MainTable.DefaultCell);
              c.Padding = 0;
              c.AddElement(LeftTable);
              MainTable.AddCell(c);
  
              var RightTable = new PdfPTable(RightWids);
              RightTable.WidthPercentage = 100;
              RightTable.DefaultCell.Border = border;
              RightTable.DefaultCell.Padding = 5;
              RightTable.DefaultCell.PaddingBottom = 0;
              RightTable.AddCell(new Phrase(p.DOB, font));
              RightTable.AddCell(new Phrase(p.MemberStatus.Description));
  
              c = new PdfPCell(MainTable.DefaultCell);
              c.Padding = 0;
              c.AddElement(RightTable);
              MainTable.AddCell(c);
  
              c = new PdfPCell(MainTable.DefaultCell);
              c.Colspan = 2;
              c.AddElement(GetAttendance(p));
              MainTable.AddCell(c);
          }
          private void AddLine(StringBuilder sb, string value)
          {
              AddLine(sb, value, String.Empty);
          }
          private void AddLine(StringBuilder sb, string value, string postfix)
          {
              if(value.HasValue())
              {
                  if (sb.Length > 0)
                      sb.Append("\n");
                  sb.Append(value);
                  if(postfix.HasValue())
                      sb.Append(postfix);
              }
          }
          private void AddPhone(StringBuilder sb, string value, string prefix)
          {
              if (value.HasValue())
              {
                  value = value.FmtFone(prefix);
                  if (sb.Length > 0)
                      sb.Append("\n");
                  sb.Append(value);
              }
          }
  
          private Paragraph GetAttendance(Person p)
          {
              var q = from a in p.Attends
                      where a.AttendanceFlag == true
                      orderby a.MeetingDate.Date descending
                      group a by a.MeetingDate.Date into g
                      select g.Key;
              var list = q.ToList();
  
              var attstr = new StringBuilder();
              var dt = Util.Now;
              var yearago = dt.AddYears(-1);
              var tp = 0;
              while (dt > yearago)
              {
  
gt:                 var dt2 = dt.AddDays(-7);