| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Linq;
|
|---|
| 4 | using System.Web;
|
|---|
| 5 | using System.Web.Mvc;
|
|---|
| 6 | using System.Web.Mvc.Ajax;
|
|---|
| 7 | using Wierszowki.Core.Interfaces;
|
|---|
| 8 | using Wierszowki.Core.Linq;
|
|---|
| 9 | using Wierszowki.Models;
|
|---|
| 10 | using System.Globalization;
|
|---|
| 11 |
|
|---|
| 12 | namespace Wierszowki.Controllers
|
|---|
| 13 | {
|
|---|
| 14 | [Authorize]
|
|---|
| 15 | [HandleError]
|
|---|
| 16 | public class ReportController : Controller
|
|---|
| 17 | {
|
|---|
| 18 | private readonly IRepository<MagazineItem> _repository;
|
|---|
| 19 |
|
|---|
| 20 | private readonly WierszowkiDataContext _context;
|
|---|
| 21 |
|
|---|
| 22 | public ReportController()
|
|---|
| 23 | {
|
|---|
| 24 | _context = new WierszowkiDataContext();
|
|---|
| 25 | _repository = new LinqRepository<MagazineItem>();
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | public ReportController(IRepository<MagazineItem> repository, WierszowkiDataContext dataContext)
|
|---|
| 29 | {
|
|---|
| 30 | _repository = repository;
|
|---|
| 31 | _context = dataContext;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | public ActionResult Index()
|
|---|
| 35 | {
|
|---|
| 36 |
|
|---|
| 37 | var viewData = new ReportViewData
|
|---|
| 38 | {
|
|---|
| 39 | Authors = _context.GetAuthors().OrderBy(a => a.FullName).ToList(),
|
|---|
| 40 | EmploymentTypeList = new SelectList(_context.GetEmploymentTypes().ToList(),"Id","Name"),
|
|---|
| 41 | UserList = new SelectList(_context.GetUsers().OrderBy(u => u.FullName).ToList(),"Id","FullName")
|
|---|
| 42 | };
|
|---|
| 43 | return View(viewData);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | public ActionResult All(int year, int month, int user)
|
|---|
| 47 | {
|
|---|
| 48 | var allViewData = new AllViewData();
|
|---|
| 49 | string error = "";
|
|---|
| 50 | string naglowek = "";
|
|---|
| 51 |
|
|---|
| 52 | if (user <= 0)
|
|---|
| 53 | {
|
|---|
| 54 | error = "Nie mo¿na wywietliæ wierszówek. Spróbuj jeszcze raz. ";
|
|---|
| 55 | allViewData = InitAllViewData("", error, null, null);
|
|---|
| 56 | return View("Error",allViewData);
|
|---|
| 57 | }
|
|---|
| 58 | var name = "";
|
|---|
| 59 | var items = new List<MagazineItem>();
|
|---|
| 60 | if (user > 0)
|
|---|
| 61 | {
|
|---|
| 62 | var u = _context.GetUserById(user);
|
|---|
| 63 | if (u != null)
|
|---|
| 64 | {
|
|---|
| 65 | name = u.FullName;
|
|---|
| 66 | items = _repository.Find(m => m.Date.Month == month && m.Date.Year == year && m.CreatedBy == user)
|
|---|
| 67 | .OrderByDescending(m => m.UpdatedOn).ToList();
|
|---|
| 68 |
|
|---|
| 69 | if (items.Count <= 0)
|
|---|
| 70 | {
|
|---|
| 71 | error = "Brak wierszówek dla tego u¿ytkownika.";
|
|---|
| 72 | allViewData = InitAllViewData("", error, null, null);
|
|---|
| 73 | return View("Error", allViewData);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | else
|
|---|
| 77 | {
|
|---|
| 78 | //U¿ytkownik bo taki nie istnieje
|
|---|
| 79 | error = "Nie mo¿na wyswietliæ wierszówek, gdy¿ u¿ytkownik nie istnieje. ";
|
|---|
| 80 | allViewData = InitAllViewData("", error, null, null);
|
|---|
| 81 | return View("Error", allViewData);
|
|---|
| 82 | }
|
|---|
| 83 | DateTime dat = new DateTime(1, 1, 1);
|
|---|
| 84 | dat = dat.AddYears(year-1);
|
|---|
| 85 | dat = dat.AddMonths(month-1);
|
|---|
| 86 |
|
|---|
| 87 | naglowek = "<div class='st1'>Wierszówki z " + dat.ToString("MM/yyyy", CultureInfo.CreateSpecificCulture("en-US")) + "</div>";
|
|---|
| 88 | if ( name != "")
|
|---|
| 89 | {
|
|---|
| 90 | naglowek += "<div class='st1'>Wprowadzaj¹cy: <span>" + name + "</span></div>";
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | Info inf = new Info();
|
|---|
| 94 | inf.powrot = "2";
|
|---|
| 95 | inf.month = month;
|
|---|
| 96 | inf.year = year;
|
|---|
| 97 | inf.user_id = user;
|
|---|
| 98 |
|
|---|
| 99 | allViewData = InitAllViewData(naglowek, error, inf, items);
|
|---|
| 100 | return View("All", allViewData);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | public ActionResult ToPrice()
|
|---|
| 104 | {
|
|---|
| 105 | string naglowek = "Wierszówki do wyceny";
|
|---|
| 106 | string error = "";
|
|---|
| 107 | var allViewData = new AllViewData();
|
|---|
| 108 | Info inf = new Info();
|
|---|
| 109 | inf.powrot = "1";
|
|---|
| 110 | var items = _repository.Find(m => m.Price <= 0).ToList();
|
|---|
| 111 | if ( items.Count <= 0 )
|
|---|
| 112 | {
|
|---|
| 113 | error = "Brak wierszówek do wyceny.";
|
|---|
| 114 | allViewData = InitAllViewData(naglowek, error, null, null);
|
|---|
| 115 | return View("Error", allViewData);
|
|---|
| 116 | }
|
|---|
| 117 | allViewData = InitAllViewData(naglowek, "", inf, items);
|
|---|
| 118 | return View("All", allViewData);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | public ActionResult Show(int year, int month, int employment)
|
|---|
| 122 | {
|
|---|
| 123 | var magazineItems = _context.FindItemsByYearMonthAndEmploymentType(year, month, employment);
|
|---|
| 124 | return View(magazineItems);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | public ActionResult AuthorCard(int year, int month, int author)
|
|---|
| 128 | {
|
|---|
| 129 | var magazineItems = _context.FindItemsByYearMonthAndAuthor(year, month, author);
|
|---|
| 130 | if (magazineItems.Count() <= 0)
|
|---|
| 131 | return RedirectToAction("AuthorCardNotFound", _context.Authors.SingleOrDefault(a => a.Id == author));
|
|---|
| 132 |
|
|---|
| 133 | var viewData = new AuthorCardViewData
|
|---|
| 134 | {
|
|---|
| 135 | FullName = magazineItems[0].Author.FullName,
|
|---|
| 136 | Date = magazineItems[0].Date,
|
|---|
| 137 | MagazineItems = new Dictionary<string, List<MagazineItem>>()
|
|---|
| 138 | };
|
|---|
| 139 | foreach (var magazineItem in magazineItems)
|
|---|
| 140 | {
|
|---|
| 141 | var key = magazineItem.Issue.Magazine.NickName;
|
|---|
| 142 | if (!viewData.MagazineItems.ContainsKey(key))
|
|---|
| 143 | {
|
|---|
| 144 | viewData.MagazineItems[key] = new List<MagazineItem>();
|
|---|
| 145 | }
|
|---|
| 146 | viewData.MagazineItems[key].Add(magazineItem);
|
|---|
| 147 | viewData.Price += magazineItem.Price;
|
|---|
| 148 | viewData.Bonus += magazineItem.Bonus;
|
|---|
| 149 | viewData.Total += magazineItem.Price + magazineItem.Bonus;
|
|---|
| 150 | }
|
|---|
| 151 | return View(viewData);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | public ActionResult AuthorCardNotFound(Author author)
|
|---|
| 155 | {
|
|---|
| 156 | return View(author);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | public ActionResult Authors(int year, int month)
|
|---|
| 160 | {
|
|---|
| 161 | ViewData["ReportDate"] = new DateTime(year, month, 1);
|
|---|
| 162 | var reports = _context.GetAuthorsReport(year, month);
|
|---|
| 163 | return View(reports);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | public ActionResult AuthorsByMagazines(int year, int month)
|
|---|
| 167 | {
|
|---|
| 168 | var viewData = new ReportViewData();
|
|---|
| 169 | viewData.ReportDate = new DateTime(year, month, 1);
|
|---|
| 170 | viewData.Authors = _context.GetAuthors();
|
|---|
| 171 | viewData.Magazines = _context.GetMagazines();
|
|---|
| 172 | viewData.ReportData = _context.GetAuthorsByMagzinesReport(year, month, viewData.Authors, viewData.Magazines);
|
|---|
| 173 | return View(viewData);
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | public ActionResult AuthorsByMagazinesByEmployment(int year, int month, int? employment)
|
|---|
| 177 | {
|
|---|
| 178 | var employmentId = employment.HasValue ? employment.Value : 1;
|
|---|
| 179 |
|
|---|
| 180 | var viewData = new ReportViewData();
|
|---|
| 181 | viewData.ReportDate = new DateTime(year, month, 1);
|
|---|
| 182 | viewData.Employment = _context.EmploymentTypes.SingleOrDefault(e => e.Id == employmentId);
|
|---|
| 183 | viewData.Authors = _context.GetAuthors().Where(a => a.EmploymentType == viewData.Employment).ToList();
|
|---|
| 184 | viewData.Magazines = _context.GetMagazines();
|
|---|
| 185 | viewData.ReportData = _context.GetAuthorsByMagzinesReport(year, month, viewData.Authors, viewData.Magazines);
|
|---|
| 186 | return View("AuthorsByMagazines", viewData);
|
|---|
| 187 | }
|
|---|
| 188 | private AllViewData InitAllViewData(string str, string er, Info inf, List<MagazineItem> list)
|
|---|
| 189 | {
|
|---|
| 190 | var viewData = new AllViewData
|
|---|
| 191 | {
|
|---|
| 192 | naglowek = str,
|
|---|
| 193 | error = er,
|
|---|
| 194 | info = inf,
|
|---|
| 195 | magazine = list
|
|---|
| 196 | };
|
|---|
| 197 | return viewData;
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 | } |
|---|