示例#1
0
        /// <summary>
        /// Update a table in the word document
        /// </summary>
        /// <param name="oDoc"></param>
        /// <param name="tableName">This is the Alt text for the table in word.  Used to find the table to udpate</param>
        /// <param name="rows">list of payments</param>
        /// <param name="isDonation">Only adds row if the isDonation property of the row matches this parameter </param>
        /// <returns></returns>
        private bool UpdateTable(Word.Document oDoc, string tableName, List <PaymentItem> rows, bool isDonation)
        {
            Word.Table table;
            if (!FindTable(tableName, oDoc, out table))
            {
                m_logger("Unable to find table to update: " + tableName);
                return(false);
            }
            bool bAddedRow = false;

            foreach (PaymentItem item in rows)
            {
                if (item.IsDonation == isDonation)
                {
                    AppendRowToTable(item.Fields, ref table);
                    bAddedRow = true;
                }
            }

            if (!bAddedRow)
            {
                // Add a comment to the table about no payments
                PaymentItem item = new PaymentItem();
                item.IsDonation = isDonation;
                item.Fields.Add(string.Empty);
                if (isDonation)
                {
                    item.Fields.Add("No Donations");
                }
                else
                {
                    item.Fields.Add("No Other Payments");
                }
                AppendRowToTable(item.Fields, ref table);
            }


            return(true);
        }
示例#2
0
        public void CreateDoc(DataTable table)
        {
            if (m_word == null)
            {
                m_word = new Word.Application();
            }

            if (table.Rows.Count == 0)
            {
                return;  // should never happen
            }
            // Get name, in case one is empty, make them both equal
            // if both empty give up
            string customerName  = table.Rows[0]["Name Contact"].ToString();
            string nameLastFirst = table.Rows[0]["Name"].ToString();

            if (string.IsNullOrWhiteSpace(customerName))
            {
                customerName = nameLastFirst;
            }
            if (string.IsNullOrWhiteSpace(customerName))
            {
                return;  // should never happen
            }
            if (string.IsNullOrEmpty(nameLastFirst))
            {
                nameLastFirst = customerName;
            }

            // If the first row does not have an item, skip because it is probably a time stamp
            if (string.IsNullOrWhiteSpace(table.Rows[0]["Item"].ToString()))
            {
                m_logger("Skipping user: "******" because Item column is empty");
                return;
            }

            //create a new document.
            Word.Document oDoc;
            try
            {
                m_word.Visible = true;
            }
            catch (System.Runtime.InteropServices.COMException)
            {
                // user probably closed Word, create a new instance
                m_word         = new Word.Application();
                m_word.Visible = true;
            }
            object oTemplate = FormMain.Config.WordTemplateFileName;

            oDoc = m_word.Documents.Add(ref oTemplate, ref m_oMissing,
                                        ref m_oMissing, ref m_oMissing);

            // create replacement values for the bookmarks
            List <KeyValuePair <string, string> > bookMarks = new List <KeyValuePair <string, string> >();

            bookMarks.Add(new KeyValuePair <string, string>("Name", customerName));
            string date = DateTime.Now.ToString("M") + ", " + DateTime.Now.ToString("yyyy");

            bookMarks.Add(new KeyValuePair <string, string>("StatementDate", date));

            StringBuilder builderToAddress = new StringBuilder();

            builderToAddress.AppendLine(customerName);
            builderToAddress.AppendLine(table.Rows[0]["Name Street1"].ToString());
            string street2      = table.Rows[0]["Name Street2"].ToString();
            bool   bSkippedLine = false;

            if (string.IsNullOrWhiteSpace(street2))
            {
                bSkippedLine = true;
            }

            builderToAddress.AppendFormat("{0}, {1}  {2}", table.Rows[0]["Name City"].ToString(), table.Rows[0]["Name State"].ToString(), table.Rows[0]["Name Zip"].ToString());
            if (bSkippedLine)
            {
                builderToAddress.AppendLine();
            }

            bookMarks.Add(new KeyValuePair <string, string>("ToAddress", builderToAddress.ToString()));
            bookMarks.Add(new KeyValuePair <string, string>("YearDateRange", FormMain.Config.DateRange));

            // used to convert numbers to strings
            const string formatNumberSmall = ",0.00";  // 1.12
            const string formatNumberLarge = "0,0.00"; // 123,456.78

            //table of payments
            decimal            totalDoations = 0;
            decimal            total         = 0;
            List <PaymentItem> payments      = new List <PaymentItem>();

            foreach (DataRow row in table.Rows)
            {
                PaymentItem payment = new PaymentItem();
                string      item    = row["Item"].ToString();
                // Binary search returns  0 based index of find, negative number if not found
                if (FormMain.Config.ItemListSelected.BinarySearch(item) >= 0)
                {
                    payment.IsDonation = true;
                }

                payment.Fields.Add(row["Date"].ToString());
                payment.Fields.Add(item);
                payment.Fields.Add(row["Memo"].ToString());
                string  paid       = row["Paid Amount"].ToString();
                decimal thisAmount = 0;
                if (decimal.TryParse(paid, out thisAmount))
                {
                    total += thisAmount;
                    if (payment.IsDonation)
                    {
                        totalDoations += thisAmount;
                    }
                }
                payment.Fields.Add(thisAmount.ToString(thisAmount < 10 ? formatNumberSmall : formatNumberLarge, System.Globalization.CultureInfo.InvariantCulture));

                //check to see if the item should be ignored, if so drop it
                if (FormMain.Config.ItemListIgnore.BinarySearch(item) >= 0)
                {
                    continue;
                }

                payments.Add(payment);
            }
            if (total == 0)
            {
                m_logger("Skipping: " + customerName + ", No items were found, or item amount was zero.");
                oDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges, m_oMissing, m_oMissing);
                return;
            }
            string email = table.Rows[0]["Name E-Mail"].ToString();

            // create filename replacing certain characters
            string fileName = nameLastFirst;

            fileName  = fileName.Replace(',', '.');
            fileName  = fileName.Replace('&', '-');
            fileName  = fileName.Replace('\'', '_');
            fileName  = fileName.Replace(" ", string.Empty);
            fileName += ".pdf";
            fileName  = Path.Combine(FormMain.Config.OutputDirectory, fileName);

            string amount = string.Format("{0:C2}", totalDoations);

            bookMarks.Add(new KeyValuePair <string, string>("Total", amount));

            CreatePdfFile(bookMarks, payments, oDoc, fileName);

            DonorRecord donorRecord = new DonorRecord(customerName, fileName, email, nameLastFirst);

            m_Files.Add(donorRecord);
            m_word.Visible = false;
        } // end CreateDoc