DataTplClosingDisclosure.cs
//
// This code is part of GrapeCity Documents for Word samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using GrapeCity.Documents.Word;

namespace GcWordWeb.Samples
{
    // This data template sample prints the closing disclosure page
    // of a loan application. It uses data from the sample database,
    // adding a few calculated values based on the sample data.
    public class DataTplClosingDisclosure
    {
        public GcWordDocument CreateDocx(int _ = 0)
        {
            using (var ds = new DataSet())
            {
                // Read the sample's data from GcWordTplDataSet.xml:
                ds.ReadXml(Path.Combine("Resources", "data", "GcWordTplDataSet.xml"));
                var dtClosing = ds.Tables["ClosingDisclosure"];
                var row = dtClosing.Rows[0];

                // Calculate monthly loan payments using a standard formula:
                var monthlyPay = CalcMonthlyPayment(Convert.ToDecimal(row["loanAmnt"]), Convert.ToDecimal(row["loanInt"]), Convert.ToInt32(row["loanTerm"]));
                // Other calculated data:
                var calc = new
                {
                    totMon1 = monthlyPay + Convert.ToDecimal(row["insurance"]) + Convert.ToDecimal(row["escrow"]),
                    totMon8 = monthlyPay + Convert.ToDecimal(row["escrow"]),
                    closingCosts = Convert.ToDecimal(row["loanCosts"]) + Convert.ToDecimal(row["otherCosts"]) - Convert.ToDecimal(row["lenderCredit"]),
                };
                // Combined data for the template:
                var data = new
                {
                    issueDate = row["issueDate"].ToString(),
                    closingDate = row["closingDate"].ToString(),
                    disDate = row["disDate"].ToString(),
                    agent = row["agent"].ToString(),
                    fileNo = row["fileNo"].ToString(),
                    propAddr = row["propAddr"].ToString(),
                    salePrice = Convert.ToDecimal(row["salePrice"]),
                    borrower = row["borrower"].ToString(),
                    seller = row["seller"].ToString(),
                    lender = row["lender"].ToString(),
                    loanTerm = Convert.ToInt32(row["loanTerm"]),
                    purpose = row["purpose"].ToString(),
                    product = row["product"].ToString(),
                    loanID = row["loanID"].ToString(),
                    micNum = row["micNum"].ToString(),
                    loanAmnt = Convert.ToDecimal(row["loanAmnt"]),
                    loanInt = Convert.ToDecimal(row["loanInt"]),
                    penalty = Convert.ToDecimal(row["penalty"]),
                    insurance = Convert.ToDecimal(row["insurance"]),
                    escrow = Convert.ToDecimal(row["escrow"]),
                    taxes = Convert.ToDecimal(row["taxes"]),

                    cash2close = Convert.ToDecimal(row["cash2close"]),
                    loanCosts = Convert.ToDecimal(row["loanCosts"]),
                    otherCosts = Convert.ToDecimal(row["otherCosts"]),
                    lenderCredit = Convert.ToDecimal(row["lenderCredit"]),

                    monthlyPay,
                    calc.totMon1,
                    calc.totMon8,
                    calc.closingCosts,
                };

                // Load the template DOCX, add the data source and process the template:
                var doc = new GcWordDocument();
                doc.Load(Path.Combine("Resources", "WordDocs", "Closing_Disclosure_Template.docx"));
                doc.DataTemplate.DataSources.Add("ds", data);
                doc.DataTemplate.Process();

                // Done:
                return doc;
            }
        }

        // We use this formula to calculate loan payments for the sample:
        // https://cdn.vertex42.com/ExcelArticles/Images/amortization-calculation-formula.png
        private decimal CalcMonthlyPayment(decimal principal, decimal yearlyInterest, int years)
        {
            var r = (double)(yearlyInterest / 12);
            var n = 12 * years;
            var q = Math.Pow(1 + r, n);
            var a = (double)principal * ((r * q) / (q - 1));
            return (decimal)a;
        }

        public static List<string[]> GetSampleParamsList()
        {
            return new List<string[]>()
            {
                new string[] { "@data-templates/Closing Disclosure", "Generate the closing disclosure page of a loan application", null },
                new string[] { "@use-data-tpl/Closing Disclosure", "Generate the closing disclosure page of a loan application", null },
            };
        }
    }
}