Пример #1
0
        public ActionResult CreateReport(DateTime startDate, DateTime endDate)
        {
            ViewBag.Title = "Expense Report";
            string reportForUser = string.Empty;

            var    results    = ExpenseManager.GetExpensesByDate(User.Identity.GetUserName(), startDate, endDate);
            string jsonString = JsonConvert.SerializeObject(results, Newtonsoft.Json.Formatting.None, new IsoDateTimeConverter()
            {
                DateTimeFormat = "MM-dd-yyyy"
            });
            XmlDocument result = JsonConvert.DeserializeXmlNode("{\"Expense\":" + jsonString + "}", "Expenses");
            var         path   = Server.MapPath("~/Content/");

            result.Save(path + "\\Expenses.xml");

            /* The below code moves the data from the XML generated to the DataTable via string Lists
             * to display on the expenses on the screen
             * XML --> string Lists --> DataTable*/
            DataTable t = new DataTable();

            t.Columns.Add("ItemID", typeof(String));
            t.Columns.Add("ExpenseName", typeof(String));
            t.Columns.Add("ExpenseCategory", typeof(String));
            t.Columns.Add("Amount(in US. Dollars)", typeof(String));
            t.Columns.Add("ExpenseDate", typeof(String));

            List <String> ItemID          = new List <string>();
            List <String> ExpenseName     = new List <string>();
            List <String> ExpenseCategory = new List <string>();
            List <String> Amount          = new List <string>();
            List <String> ExpenseDate     = new List <string>();

            XmlDocument xdoc = new XmlDocument();

            xdoc.Load(Server.MapPath("~/Content/") + "\\Expenses.xml");

            /* Looping through the contents of XML using the XmlNodeReader to
             * move the contents into different String lists*/
            XmlReader xmlReader = new XmlNodeReader(xdoc);

            while (xmlReader.Read())
            {
                if (xmlReader.IsStartElement())
                {
                    String b = xmlReader.Name;
                    switch (xmlReader.Name)
                    {
                    case "Expenses":
                        break;

                    case "Expense":
                        break;

                    case "ItemID":
                        if (xmlReader.Read())
                        {
                            ItemID.Add(xmlReader.Value.Trim());
                        }
                        break;

                    case "ExpenseName":
                        if (xmlReader.Read())
                        {
                            ExpenseName.Add(xmlReader.Value.Trim());
                        }
                        break;

                    case "ExpenseCategory":
                        if (xmlReader.Read())
                        {
                            ExpenseCategory.Add(xmlReader.Value.Trim());
                        }
                        break;

                    case "Amount":
                        if (xmlReader.Read())
                        {
                            Amount.Add(xmlReader.Value.Trim());
                        }
                        break;

                    case "ExpenseDate":
                        if (xmlReader.Read())
                        {
                            ExpenseDate.Add(xmlReader.Value.Trim());
                        }
                        break;

                    case "UserName":
                        if (xmlReader.Read())
                        {
                            reportForUser = xmlReader.Value.Trim();
                        }
                        break;
                    }
                }
            }

            int     counter = 0;
            decimal total   = 0;

            /* creating a two dimensional array to hold the expenses category wise
             * for the bar graph . The first part of the code identifies the distinct categories. The
             * second part assigns amount to each of the categories*/
            List <string> distinctCategory = new List <string>();

            distinctCategory.AddRange(ExpenseCategory.Distinct());
            string[,] catergoryArray = new string[distinctCategory.Count, 2];
            for (int i = 0; i < distinctCategory.Count; i++)
            {
                catergoryArray[i, 0] = distinctCategory[i];
                catergoryArray[i, 1] = "0";
            }

            /* moving the contents from the string Lists to the DataTable */
            foreach (string line in ItemID)
            {
                DataRow r = t.NewRow();
                r["ItemID"]                 = counter + 1;
                r["ExpenseName"]            = ExpenseName[counter];
                r["ExpenseCategory"]        = ExpenseCategory[counter];
                r["Amount(in US. Dollars)"] = Amount[counter];
                r["ExpenseDate"]            = ExpenseDate[counter];
                t.Rows.Add(r);

                total += decimal.Parse(Amount[counter]);

                //creating expenses category wise
                for (int i = 0; i < (catergoryArray.Length) / 2; i++)
                {
                    if (ExpenseCategory[counter].Equals(catergoryArray[i, 0]))
                    {
                        int sum = Convert.ToInt32(catergoryArray[i, 1]) + Convert.ToInt32(Amount[counter]);
                        catergoryArray[i, 1] = sum.ToString();
                        break;
                    }
                }
                counter += 1;
            }

            //CreateCategoryXml creates an Xml on runtime for the differnt Categories and the amonts
            // This Xml will be further used to generate the bar graph
            CreateCategoryXml(catergoryArray);

            // CreateWordDoc method creates Expense Report between the selected Dates
            // The input for the method is the DataTable, Total amount of all expenses and the user for whom the report is generated
            CreateWordDoc(t, total, reportForUser);

            //Applying xslt on the category xml
            XslCompiledTransform transform = new XslCompiledTransform();
            XsltSettings         settings  = new XsltSettings();

            settings.EnableScript = true;
            transform.Load(path + "\\Transform.xsl", settings, null);
            transform.Transform(path + "\\Expenses.xml", path + "\\Expenses.html");
            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }