dev
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
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 EveryThing.Data;
|
||||
using EveryThing.Models;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.CodeAnalysis.Differencing;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EveryThing.Pages.Invoices
|
||||
{
|
||||
@@ -28,20 +32,41 @@ namespace EveryThing.Pages.Invoices
|
||||
_roleManager = roleManager;
|
||||
}
|
||||
|
||||
public IActionResult OnGet(int idInvoice)
|
||||
[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();
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
public Models.Invoice.InvoiceItem InvoiceItem { get; set; }
|
||||
|
||||
public async Task<IActionResult> OnPostAsync(int idInvoice)
|
||||
public async Task<IActionResult> OnPostAsync(bool edit)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
@@ -50,12 +75,36 @@ namespace EveryThing.Pages.Invoices
|
||||
|
||||
var user = _userManager.GetUserAsync(User).Result;
|
||||
|
||||
InvoiceItem.IdInvoiceFk = idInvoice;
|
||||
if (!edit)
|
||||
{
|
||||
_context.InvoiceItems.Add(InvoiceItem);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
_context.Attach(InvoiceItem).State = EntityState.Modified;
|
||||
|
||||
_context.InvoiceItems.Add(InvoiceItem);
|
||||
await _context.SaveChangesAsync();
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!InvoiceItemExists(InvoiceItem.IdInvoiceItem))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return RedirectToPage("./Edit", new { id = idInvoice });
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return RedirectToPage("./Edit", new { id = InvoiceItem.IdInvoiceFk });
|
||||
}
|
||||
|
||||
private bool InvoiceItemExists(int id)
|
||||
{
|
||||
return _context.InvoiceItems.Any(e => e.IdProjectPartItem == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user