111 lines
3.6 KiB
C#
111 lines
3.6 KiB
C#
using EveryThing.Data;
|
|
using EveryThing.Models;
|
|
using EveryThing.Models.Invoice;
|
|
using EveryThing.Models.Project;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Microsoft.CodeAnalysis.Differencing;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EveryThing.Pages.Invoices
|
|
{
|
|
[Authorize(Roles = "Administrator,InvoicingUser,ProjecThingUser")]
|
|
public class CreateItemModel : PageModel
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly UserManager<IdentityApplicationUser> _userManager;
|
|
private readonly SignInManager<IdentityApplicationUser> _loginManager;
|
|
private readonly RoleManager<IdentityApplicationRole> _roleManager;
|
|
|
|
public CreateItemModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager, SignInManager<IdentityApplicationUser> loginManager, RoleManager<IdentityApplicationRole> roleManager)
|
|
{
|
|
_context = context;
|
|
_userManager = userManager;
|
|
_loginManager = loginManager;
|
|
_roleManager = roleManager;
|
|
}
|
|
|
|
[BindProperty]
|
|
public Models.Invoice.InvoiceItem InvoiceItem { get; set; }
|
|
public Models.Invoice.Invoice Invoice { get; set; }
|
|
|
|
public IActionResult OnGet(int idInvoice, bool? edit = null, int? idInvoiceItem = null)
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
ViewData["IdInvoice"] = idInvoice;
|
|
Invoice = _context.Invoices.FirstOrDefault(x => x.IdInvoice == idInvoice);
|
|
if (Invoice == null)
|
|
return NotFound();
|
|
|
|
ViewData["Items"] = new SelectList(_context.CodeTableItems.Where(x => x.IdCompanyFk == user.IdCompanyFk && x.Active), "IdItem", "Title");
|
|
ViewData["Edit"] = edit ?? false;
|
|
|
|
if (edit ?? false)
|
|
{
|
|
InvoiceItem = _context.InvoiceItems.FirstOrDefault(x => x.IdInvoiceItem == idInvoiceItem);
|
|
if (InvoiceItem == null)
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
InvoiceItem = new InvoiceItem
|
|
{
|
|
IdInvoiceFk = idInvoice
|
|
};
|
|
}
|
|
|
|
return Page();
|
|
}
|
|
|
|
|
|
public async Task<IActionResult> OnPostAsync(bool edit)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return Page();
|
|
}
|
|
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
if (!edit)
|
|
{
|
|
_context.InvoiceItems.Add(InvoiceItem);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
else
|
|
{
|
|
_context.Attach(InvoiceItem).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!InvoiceItemExists(InvoiceItem.IdInvoiceItem))
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return RedirectToPage("./Edit", new { id = InvoiceItem.IdInvoiceFk });
|
|
}
|
|
|
|
private bool InvoiceItemExists(int id)
|
|
{
|
|
return _context.InvoiceItems.Any(e => e.IdProjectPartItem == id);
|
|
}
|
|
}
|
|
}
|