// GET: VATRegisterBuys
        public async Task <IActionResult> Index(int?year, int?month)
        {
            VATRegisterBuy vATRegisterBuy = new VATRegisterBuy();

            vATRegisterBuy.DateOfIssue  = DateTime.Now.Date;
            vATRegisterBuy.DeliveryDate = DateTime.Now.Date;
            ViewData["VATRegisterBuy"]  = vATRegisterBuy;

            //test
            VATRegisterBuyViewModel vATRegisterBuyViewModel = new VATRegisterBuyViewModel();

            vATRegisterBuyViewModel.DateOfIssue  = DateTime.Now.Date;
            vATRegisterBuyViewModel.DeliveryDate = DateTime.Now.Date;
            ViewData["VATRegisterBuyViewModel"]  = vATRegisterBuyViewModel;
            //test end

            ViewData["ContractorId"] = new SelectList(_context.Contractor, "Id", "Name");
            ViewData["Month"]        = new SelectList(Tools.getMonthsDictionary(), "Key", "Value", DateTime.Now.Month);
            ViewData["Year"]         = new SelectList(Tools.getYearsList(), DateTime.Now.Year);

            if (HttpContext.Session.GetString(SelectedMonth) == "month" && HttpContext.Session.GetString(SelectedYear) == "year")
            {
                HttpContext.Session.SetInt32(SelectedMonth, DateTime.Now.Month);
                HttpContext.Session.SetInt32(SelectedYear, DateTime.Now.Year);
                // year = HttpContext.Session.GetInt32(SelectedYear);
                // month = HttpContext.Session.GetInt32(SelectedMonth);
            }

            if (year is null && month is null)
            {
                year  = HttpContext.Session.GetInt32(SelectedYear);
                month = HttpContext.Session.GetInt32(SelectedMonth);

                ViewData["Month"]         = new SelectList(Tools.getMonthsDictionary(), "Key", "Value", month);
                ViewData["SelectedMonth"] = month;
                ViewData["Year"]          = new SelectList(Tools.getYearsList(), year);
                ViewData["SelectedYear"]  = year;
            }

            var applicationDbContext = _context.VATRegisterBuy.Include(i => i.Contractor);

            if (month != null && year != null)
            {
                HttpContext.Session.SetInt32(SelectedMonth, (int)month);
                HttpContext.Session.SetInt32(SelectedYear, (int)year);

                ViewData["Month"]         = new SelectList(Tools.getMonthsDictionary(), "Key", "Value", month);
                ViewData["SelectedMonth"] = month;
                ViewData["Year"]          = new SelectList(Tools.getYearsList(), year);
                ViewData["SelectedYear"]  = year;

                var filteredResult = applicationDbContext.Where(p => p.DateOfIssue.Month == month && p.DateOfIssue.Year == year);

                return(View(await filteredResult.OrderBy(p => p.DateOfIssue).ToListAsync()));
            }
            else
            {
                return(View(await applicationDbContext.Where(p => p.Month == DateTime.Now.Month && p.Year == DateTime.Now.Year).ToListAsync()));
            }
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,Number,DeliveryDate,DateOfIssue,DocumentNumber,Contractor,ValueBrutto,ValueNetto,TaxDeductibleValue,TaxFreeBuysValue,NoTaxDeductibleBuysValue")] VATRegisterBuy vATRegisterBuy)
        {
            ViewData["ContractorId"] = new SelectList(_context.Contractor, "Id", "Name");

            if (id != vATRegisterBuy.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(vATRegisterBuy);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!VATRegisterBuyExists(vATRegisterBuy.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(vATRegisterBuy));
        }
        public async Task <IActionResult> CreateTest([Bind("Id,DeliveryDate,DateOfIssue,DocumentNumber,ContractorId,ValueBrutto,ValueNetto,TaxDeductibleValue,TaxFreeBuysValue,NoTaxDeductibleBuysValue,CarCost,BuyForTrade,OtherCost,DescriptionForTaxBook")] VATRegisterBuyViewModel vATRegisterBuyViewModel)
        {
            ViewData["ContractorId"] = new SelectList(_context.Contractor, "Id", "Name");
            if (ModelState.IsValid)
            {
                vATRegisterBuyViewModel.Month  = vATRegisterBuyViewModel.DateOfIssue.Month;
                vATRegisterBuyViewModel.Year   = vATRegisterBuyViewModel.DateOfIssue.Year;
                vATRegisterBuyViewModel.Number = vATRegisterBuyViewModel.getOrderNumber(_context);

                VATRegisterBuy vATRegisterBuy = vATRegisterBuyViewModel.getVATRegisterBuy();

                TaxBook taxBook = new TaxBook();
                taxBook.Date          = vATRegisterBuyViewModel.DateOfIssue;
                taxBook.InvoiceNumber = vATRegisterBuyViewModel.DocumentNumber;
                taxBook.ContractorId  = vATRegisterBuyViewModel.ContractorId;
                taxBook.Description   = vATRegisterBuyViewModel.DescriptionForTaxBook;

                if (vATRegisterBuyViewModel.BuyForTrade)
                {
                    string description = "zakup handlowy";
                    taxBook.GoodsBuys   = vATRegisterBuyViewModel.ValueNetto;
                    taxBook.Description = description;
                }
                else if (vATRegisterBuyViewModel.OtherCost)
                {
                    taxBook.OtherCosts = vATRegisterBuyViewModel.ValueNetto;
                }
                else if (vATRegisterBuyViewModel.CarCost)
                {
                    decimal carCostDeductibleValue = Decimal.Parse(_context.Parameter.Single(p => p.Name == "odl_koszty_pojazd").Value);
                    taxBook.OtherCosts = vATRegisterBuyViewModel.ValueNetto * carCostDeductibleValue;
                    vATRegisterBuy.TaxDeductibleValue = vATRegisterBuy.TaxDeductibleValue / 2;

                    CarCost carCost = new CarCost();
                    carCost.Date        = vATRegisterBuyViewModel.DateOfIssue;
                    carCost.Price       = vATRegisterBuyViewModel.ValueBrutto;
                    carCost.Description = vATRegisterBuyViewModel.DescriptionForTaxBook;

                    // if (_context.CarCostType.Single(p=>p.Contains(carCost.Description))=!null)
                    // {
                    //     carCost.CarCostTypeId = _context.CarCostType.Single(p=>p.Contains());
                    // }
                }


                _context.Add(taxBook);
                _context.Add(vATRegisterBuy);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(vATRegisterBuyViewModel));
        }
        // GET: VATRegisterBuys/Create
        public IActionResult Create()
        {
            ViewData["ContractorId"] = new SelectList(_context.Contractor, "Id", "Name");
            VATRegisterBuy vATRegisterBuy = new VATRegisterBuy();
            DateTime       currDate       = DateTime.Now;

            vATRegisterBuy.DateOfIssue  = currDate;
            vATRegisterBuy.DeliveryDate = currDate;
            vATRegisterBuy.Month        = currDate.Month;
            vATRegisterBuy.Year         = currDate.Year;

            return(View(vATRegisterBuy));
        }
        // GET: GenerateInvoice
        public async Task <IActionResult> GenerateVATRegister(int?year, int?month)
        {
            if (year == null || month == null)
            {
                return(NotFound());
            }

            var    vATRegisterBuy = new VATRegisterBuy();
            string pdfFilename    = vATRegisterBuy.generate(_context, (int)year, (int)month);
            string downFilename   = vATRegisterBuy.getDownloadFilename((int)year, (int)month);
            await Task.Delay(1000);

            return(RedirectToAction("GetPdfFile", new { filename = pdfFilename, downloadFilename = downFilename }));
        }
        public async Task <IActionResult> Create([Bind("Id,DeliveryDate,DateOfIssue,DocumentNumber,ContractorId,ValueBrutto,ValueNetto,TaxDeductibleValue,TaxFreeBuysValue,NoTaxDeductibleBuysValue")] VATRegisterBuy vATRegisterBuy)
        {
            ViewData["ContractorId"] = new SelectList(_context.Contractor, "Id", "Name");
            if (ModelState.IsValid)
            {
                vATRegisterBuy.Month  = vATRegisterBuy.DateOfIssue.Month;
                vATRegisterBuy.Year   = vATRegisterBuy.DateOfIssue.Year;
                vATRegisterBuy.Number = vATRegisterBuy.getOrderNumber(_context);
                _context.Add(vATRegisterBuy);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(vATRegisterBuy));
        }