prvi
This commit is contained in:
244
EveryThing/Pages/Invoices/Edit.cshtml.cs
Normal file
244
EveryThing/Pages/Invoices/Edit.cshtml.cs
Normal file
@@ -0,0 +1,244 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using EveryThing.Data;
|
||||
using EveryThing.Models;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using EveryThing.Models.Project;
|
||||
using System.Text.Json;
|
||||
using static EveryThing.Pages.Projects.EditModel;
|
||||
|
||||
namespace EveryThing.Pages.Invoices
|
||||
{
|
||||
[Authorize(Roles = "Administrator,InvoicingUser,ProjecThingUser")]
|
||||
public class EditModel : PageModel
|
||||
{
|
||||
public class EditInvoiceItemData
|
||||
{
|
||||
public Models.Invoice.InvoiceItem InvoiceItem { get; set; }
|
||||
|
||||
public SelectList SelectListItems { get; set; }
|
||||
//public SelectList SelectListMaterials { get; set; }
|
||||
//public SelectList SelectListSuppliers { get; set; }
|
||||
|
||||
}
|
||||
|
||||
private readonly ApplicationDbContext _context;
|
||||
private readonly UserManager<IdentityApplicationUser> _userManager;
|
||||
private readonly SignInManager<IdentityApplicationUser> _loginManager;
|
||||
private readonly RoleManager<IdentityApplicationRole> _roleManager;
|
||||
|
||||
public EditModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager, SignInManager<IdentityApplicationUser> loginManager, RoleManager<IdentityApplicationRole> roleManager)
|
||||
{
|
||||
_context = context;
|
||||
_userManager = userManager;
|
||||
_loginManager = loginManager;
|
||||
_roleManager = roleManager;
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
public Models.Invoice.Invoice Invoice { get; set; }
|
||||
public IList<Models.Invoice.InvoiceItem> InvoiceItems { get; set; }
|
||||
|
||||
[BindProperty]
|
||||
public string Reffer { get; set; }
|
||||
|
||||
public async Task<IActionResult> OnGetAsync(int? id)
|
||||
{
|
||||
if (id == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
Invoice = await _context.Invoices
|
||||
.Include(f => f.Company).FirstOrDefaultAsync(m => m.IdInvoice == id);
|
||||
|
||||
var user = _userManager.GetUserAsync(User).Result;
|
||||
|
||||
if (Invoice == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
InvoiceItems = await _context.InvoiceItems
|
||||
.Include(f => f.Item)
|
||||
.Where(m => m.IdInvoiceFk == id)
|
||||
.ToListAsync();
|
||||
|
||||
ViewData["Type"] = (int)Invoice.Type;
|
||||
ViewData["IdPartnerFk"] = new SelectList(_context.CodeTablePartners.Where(x => x.IdCompanyFk == user.IdCompanyFk && x.Active), "IdPartner", "Title");
|
||||
|
||||
ViewData["States"] = new SelectList(Enum.GetValues(typeof(Models.Invoice.Invoice.InvoiceState))
|
||||
.Cast<Models.Invoice.Invoice.InvoiceState>()
|
||||
.Where(x => GetAttribute<Models.Invoice.Invoice.InvoiceStateAttribute>(x).AllowedTypes.Split(',').Contains(((int)Invoice.Type).ToString()))
|
||||
.Select(x => new { Name = GetAttribute<DisplayAttribute>(x).Name, Value = x.ToString()})
|
||||
.ToList(), "Value", "Name");
|
||||
|
||||
if (!string.IsNullOrEmpty(Request.Headers["Referer"]))
|
||||
{
|
||||
ViewData["Reffer"] = Reffer = Request.Headers["Referer"].ToString();
|
||||
}
|
||||
|
||||
return Page();
|
||||
}
|
||||
|
||||
public static TAttribute GetAttribute<TAttribute>(Enum value)
|
||||
where TAttribute : Attribute
|
||||
{
|
||||
var enumType = value.GetType();
|
||||
var name = Enum.GetName(enumType, value);
|
||||
return enumType.GetField(name).GetCustomAttributes(false).OfType<TAttribute>().SingleOrDefault();
|
||||
}
|
||||
|
||||
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
||||
// more details see https://aka.ms/RazorPagesCRUD.
|
||||
public async Task<IActionResult> OnPostAsync()
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return Page();
|
||||
}
|
||||
|
||||
Invoice.PostText = Invoice.PostText.Replace("<div><br></div>", "");
|
||||
Invoice.PreText = Invoice.PreText.Replace("<div><br></div>", "");
|
||||
|
||||
_context.Attach(Invoice).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!FileExists(Invoice.IdInvoice))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return RedirectToPage("./Edit", new { id = Invoice.IdInvoice});
|
||||
|
||||
|
||||
if (Reffer != null)
|
||||
return RedirectToPage(Reffer);
|
||||
|
||||
return RedirectToPage("./Index", new { type = (int)Invoice.Type });
|
||||
}
|
||||
|
||||
private bool FileExists(int id)
|
||||
{
|
||||
return _context.Invoices.Any(e => e.IdInvoice == id);
|
||||
}
|
||||
|
||||
public IActionResult OnPostUpdateInvoiceItem(int idItem, string json)
|
||||
{
|
||||
var tmpItem = JsonSerializer.Deserialize<Models.Invoice.InvoiceItem>(json);
|
||||
|
||||
var item = _context.InvoiceItems.First(x => x.IdInvoiceItem == idItem);
|
||||
|
||||
if (item == null)
|
||||
{
|
||||
//TODO error
|
||||
return new OkResult();
|
||||
}
|
||||
|
||||
item.IdItemFk = tmpItem.IdItemFk;
|
||||
item.Quantity = tmpItem.Quantity;
|
||||
item.Price = tmpItem.Price;
|
||||
item.Discount = tmpItem.Discount;
|
||||
item.Tax = tmpItem.Tax;
|
||||
item.State = tmpItem.State;
|
||||
item.ItemDescription = tmpItem.ItemDescription;
|
||||
|
||||
_context.SaveChanges();
|
||||
|
||||
return new OkResult();
|
||||
}
|
||||
|
||||
public IActionResult OnGetInvoiceItemEdit(int id)
|
||||
{
|
||||
var user = _userManager.GetUserAsync(User).Result;
|
||||
|
||||
var item = _context.InvoiceItems
|
||||
.Include(x => x.Item)
|
||||
.First(x => x.IdInvoiceItem == id);
|
||||
var selListItems = new SelectList(_context.CodeTableItems.Where(x => x.IdCompanyFk == user.IdCompanyFk && x.Active), "IdItem", "Title");
|
||||
|
||||
|
||||
return Partial("InvoiceItemEdit", new EditInvoiceItemData
|
||||
{
|
||||
InvoiceItem = item,
|
||||
SelectListItems = selListItems,
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
public IActionResult OnGetInvoiceItemDetails(int id)
|
||||
{
|
||||
var item = _context.InvoiceItems
|
||||
.Include(x => x.Item)
|
||||
.First(x => x.IdInvoiceItem == id);
|
||||
|
||||
return Partial("InvoiceItemDetails", item);
|
||||
}
|
||||
|
||||
public IActionResult OnDeleteInvoiceItem(int idInvoiceItem)
|
||||
{
|
||||
var user = _userManager.GetUserAsync(User).Result;
|
||||
|
||||
var item = _context.InvoiceItems
|
||||
.Include(x => x.Invoice)
|
||||
.FirstOrDefault(x => x.IdInvoiceItem == idInvoiceItem
|
||||
&& x.Invoice.IdCompanyFk == user.IdCompanyFk);
|
||||
|
||||
if (item == null)
|
||||
{
|
||||
return new JsonResult(new { error = $"Invoice item with id {idInvoiceItem} not exists!", successful = false });
|
||||
}
|
||||
|
||||
_context.InvoiceItems.Remove(item);
|
||||
_context.SaveChanges();
|
||||
|
||||
return new JsonResult(new { error = "", successful = true });
|
||||
}
|
||||
|
||||
public IActionResult OnGetInvoiceItem(int idInvoiceItem)
|
||||
{
|
||||
var user = _userManager.GetUserAsync(User).Result;
|
||||
|
||||
var successful = true;
|
||||
var error = "";
|
||||
var inUse = false;
|
||||
|
||||
var invoiceItem = _context.InvoiceItems
|
||||
.Include(x => x.Invoice)
|
||||
.FirstOrDefault(x => x.IdInvoiceItem == idInvoiceItem
|
||||
&& x.Invoice.IdCompanyFk == user.IdCompanyFk);
|
||||
|
||||
if (invoiceItem == null)
|
||||
{
|
||||
successful = false;
|
||||
error = $"Invoice item with ID: {idInvoiceItem} not found";
|
||||
}
|
||||
else
|
||||
{
|
||||
inUse = false;
|
||||
//Cene se json zacikla neki IDK.
|
||||
invoiceItem.Invoice = null;
|
||||
}
|
||||
|
||||
return new JsonResult(new { invoiceItem, error, successful, inUse });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user