Exemplo n.º 1
0
        public ActionResult Index(InvoiceIndexViewModel invoiceIndexViewModel, string button)
        {
            invoiceIndexViewModel = QueryInvoices(invoiceIndexViewModel);

            if (button == "Export")
            {
                return Export(invoiceIndexViewModel);
            }

            PopulateEditViewBagProperties(includeAllOptionsWhenAppropriate: true);

            return View(invoiceIndexViewModel);
        }
Exemplo n.º 2
0
        private InvoiceIndexViewModel QueryInvoices(InvoiceIndexViewModel invoiceIndexViewModel)
        {
            if (invoiceIndexViewModel == null)
            {
                invoiceIndexViewModel = new InvoiceIndexViewModel();
            }

            if (!string.IsNullOrEmpty(Request[ResetFiltersRequestKey]))
            {
                invoiceIndexViewModel.InvoiceFilter = GetDefaultInvoiceFilter();
            }

            if (invoiceIndexViewModel.InvoiceFilter == null)
            {
                var SessionInvoiceFilter = Session[InvoiceFilterSessionKey] as InvoiceFilter;
                invoiceIndexViewModel.InvoiceFilter = SessionInvoiceFilter ?? GetDefaultInvoiceFilter();
            }

            var Filter = invoiceIndexViewModel.InvoiceFilter;

            // apply filters from the view
            var invoices = repo.Invoices
                .Include(i => i.Items)
                .Include(i => i.Items.Select(item => item.Service))
                .Include(i => i.Servicer)
                .Include(i => i.Site)
                .Include(i => i.Site.Organization)
                .Where(i => Filter.SiteId == 0 || i.SiteId == Filter.SiteId)
                .Where(i => i.ServicerId == Filter.ServicerId || Filter.ServicerId == 0)
                .Where(i => Filter.KeyRec == null || Filter.KeyRec == "" || i.KeyRec.Contains(Filter.KeyRec))
                .Where(i => Filter.FrtBill == null || Filter.FrtBill == "" || i.FrtBill.Contains(Filter.FrtBill))
                .Where(i => Filter.PurchaseOrder == null || Filter.PurchaseOrder == "" || i.PurchaseOrder.Contains(Filter.PurchaseOrder))
                // apply user-specific filtering
                .Where(DataContextExtensions.GetInvoiceFilterForCurrentUser());

            // add date filters
            switch (Filter.DateFilterType)
            {
                case service_tracker_mvc.Classes.DateFilterType.ServiceDate:
                    invoices = invoices
                        .Where(i => i.ServiceDate >= Filter.StartDate)
                        .Where(i => i.ServiceDate <= Filter.EndDate);
                    break;
                case service_tracker_mvc.Classes.DateFilterType.EntryDate:
                    invoices = invoices
                        .Where(i => i.EntryDate >= Filter.StartDate)
                        .Where(i => i.EntryDate <= Filter.EndDate);
                    break;
                default:
                    throw new ArgumentException("Given DateFilterType is not supported", "DateFilterType");
            }

            // evaluate query
            invoiceIndexViewModel.Invoices = invoices
                // sort
                .OrderBy(i => i.ServiceDate)
                .ThenBy(i => i.InvoiceId)
                // evaluate
               .ToList();

            // save the filter for future use
            Session[InvoiceFilterSessionKey] = invoiceIndexViewModel.InvoiceFilter;

            return invoiceIndexViewModel;
        }
Exemplo n.º 3
0
        public ActionResult Export(InvoiceIndexViewModel invoiceIndexViewModel)
        {
            // note: the model already has invoices when passed to this action

            var reportItems = invoiceIndexViewModel.Invoices.SelectMany(invoice =>
                invoice.Items.Select(item =>
                    new InvoiceItemReport
                    {
                        InvoiceId = invoice.InvoiceId,
                        ServiceDate = invoice.ServiceDate,
                        EntryDate = invoice.EntryDate,
                        Store = invoice.Site.ToString(),
                        Employee = invoice.Servicer.Name,
                        FrtBill = invoice.FrtBill,
                        KeyRec = invoice.KeyRec,
                        PO = invoice.PurchaseOrder,
                        InvoiceTotal = invoice.Total,

                        Sku = item.Service == null ? "" : item.Service.Sku,
                        Service = item.Service == null ? "" : item.Service.Description,
                        Quantity = item.Quantity,
                        UnitPrice = item.Service == null ? 0M : item.Service.Cost,
                        TotalLineItemPrice = item.Total
                    }
                )
            );

            // Create the report and turn our query into a ReportSource
            var report = new Report(reportItems.ToReportSource());

            // Customize the Text Fields
            var Title = string.Format("{0:yyyy.MM.dd} to {1:yyyy.MM.dd}", invoiceIndexViewModel.InvoiceFilter.StartDate, invoiceIndexViewModel.InvoiceFilter.EndDate);
            var Filename = Title + ".xlsx";

            report.TextFields.Title = Title;
            report.TextFields.SubTitle = string.Format("Generated on {0} UTC", DateTime.UtcNow);

            report.TextFields.Header = string.Format(@"
                            Report Generated: {0}
                            Invoices: {1}
                            Total: {2:c}", DateTime.UtcNow, invoiceIndexViewModel.Invoices.Count, invoiceIndexViewModel.Invoices.Sum(i => i.Total));

            // Render hints allow you to pass additional hints to the reports as they are being rendered
            report.RenderHints.BooleansAsYesNo = true;

            report.DataFields["InvoiceTotal"].DataFormatString = "{0:c}";
            report.DataFields["UnitPrice"].DataFormatString = "{0:c}";
            report.DataFields["TotalLineItemPrice"].DataFormatString = "{0:c}";

            return new ReportResult(report, new ExcelReportWriter(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileName = Filename
            };
        }