//
        // GET: /Invoices/
        public async Task <ActionResult> Index()
        {
            if (!ActiveCustomer.showMyInvoices)
            {
                return(HttpNotFound());
            }

            DateTime startDate = DateTime.Today.AddMonths(-1);
            DateTime endDate   = DateTime.Today.AddMonths(1);

            if (Session["StartDate"] != null && Session["EndDate"] != null)
            {
                startDate = (DateTime)Session["StartDate"];
                endDate   = (DateTime)Session["EndDate"];
            }
            else
            {
                Session["StartDate"] = startDate;
                Session["EndDate"]   = endDate;
            }

            var invoices = await GetInvoices(this.ActiveCustomer, startDate, endDate);

            //var validCustomers = (await new OverviewDAL().GetCustomersRegistered()).Where(x => x.DatabaseName == ActiveCustomer.DatabaseName && x.CompanyId == ActiveCustomer.CompanyId);
            //invoices = invoices.Join(validCustomers, x => x.InvoiceAccount, y => y.CustomerId, (inv, cust) => inv);

            InvoicesViewModel model = new InvoicesViewModel();

            model.PaidCheck.Add("0");
            model.StartDate = startDate;
            model.EndDate   = endDate;
            model.Process(invoices, ActiveCustomer);
            return(View(model));
        }
        public async Task <PartialViewResult> GetInvoices(InvoicesViewModel model)
        {
            /*if (model.OverdueCheck.Contains("1") || model.PaidCheck.Contains("0"))
             * {
             *  model.StartDate = new DateTime(2007, 1, 1);
             *  model.EndDate = DateTime.Today;
             * }*/

            if (model.StartDate == DateTime.MinValue)
            {
                model.StartDate = new DateTime(DateTime.Today.AddMonths(-1).Year, DateTime.Today.AddMonths(-1).Month, 1);
            }

            if (model.EndDate == DateTime.MinValue)
            {
                model.EndDate = DateTime.Today;
            }

            var invoices = await this.GetInvoices(this.ActiveCustomer, model.StartDate, model.EndDate);

            //var validCustomers = (await new OverviewDAL().GetCustomersRegistered()).Where(x => x.DatabaseName == ActiveCustomer.DatabaseName && x.CompanyId == ActiveCustomer.CompanyId);
            //invoices = invoices.Join(validCustomers, x => x.InvoiceAccount, y => y.CustomerId, (inv, cust) => inv);

            model.Process(invoices, ActiveCustomer);

            return(PartialView("_InvoicesPartial", model));
        }
        public async Task <ActionResult> InvoiceList(InvoicesViewModel model)
        {
            IEnumerable <Invoice> invoices = await GetInvoices(this.ActiveCustomer, model.StartDate, model.EndDate);

            var validCustomers = (await new OverviewDAL().GetCustomersRegistered()).Where(x => x.DatabaseName == ActiveCustomer.DatabaseName && x.CompanyId == ActiveCustomer.CompanyId);

            invoices = invoices.Join(validCustomers, x => x.InvoiceAccount, y => y.CustomerId, (inv, cust) => inv);

            invoices = model.Process(invoices, ActiveCustomer);

            using (var invoicesExport = new ExcelPackage())
            {
                var ws = invoicesExport.Workbook.Worksheets.Add("Table1");

                ws.Cells["A1"].Value = Resources.InvoiceAccount;
                ws.Cells["B1"].Value = Resources.AccountName;
                ws.Cells["C1"].Value = Resources.InvoiceNumber;
                ws.Cells["D1"].Value = Resources.InvoiceDate;
                ws.Cells["E1"].Value = Resources.DueDate;
                ws.Cells["F1"].Value = Resources.Amount;
                ws.Cells["G1"].Value = Resources.VenueName;
                ws.Cells["H1"].Value = Resources.VenueCity;
                ws.Cells["I1"].Value = Resources.Paid;
                ws.Cells["J1"].Value = Resources.InvoiceType;
                ws.Cells["K1"].Value = Resources.YourPurchaseOrder;
                ws.Cells["L1"].Value = Resources.ContactPerson;

                int rowCount = 2;

                for (int i = 0; i < invoices.Count(); i++)
                {
                    ws.Cells["A" + rowCount.ToString()].Value = invoices.ElementAt(i).InvoiceAccount;
                    ws.Cells["B" + rowCount.ToString()].Value = invoices.ElementAt(i).AccountName;
                    ws.Cells["C" + rowCount.ToString()].Value = invoices.ElementAt(i).InvoiceId;
                    ws.Cells["D" + rowCount.ToString()].Value = invoices.ElementAt(i).InvoiceDate;
                    ws.Cells["E" + rowCount.ToString()].Value = invoices.ElementAt(i).DueDate;
                    ws.Cells["F" + rowCount.ToString()].Value = invoices.ElementAt(i).InvoiceAmountMST;
                    ws.Cells["G" + rowCount.ToString()].Value = invoices.ElementAt(i).DeliveryName;
                    ws.Cells["H" + rowCount.ToString()].Value = invoices.ElementAt(i).DeliveryCity;
                    ws.Cells["I" + rowCount.ToString()].Value = invoices.ElementAt(i).IsPaid ? Resources.Yes : Resources.No;
                    ws.Cells["J" + rowCount.ToString()].Value = Invoice.GetSalesTypeName(this.ActiveCustomer.DatabaseName, invoices.ElementAt(i).SalesType, invoices.ElementAt(i).IsHistoric);
                    ws.Cells["K" + rowCount.ToString()].Value = invoices.ElementAt(i).PurchaseOrder;
                    ws.Cells["L" + rowCount.ToString()].Value = invoices.ElementAt(i).ContactPersonName;

                    rowCount++;
                }
                return(File(invoicesExport.GetAsByteArray(), ".xlsx", Resources.Invoices + ".xlsx"));
            };
        }