CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs

357 lines
11 KiB
C#
Raw Normal View History

2024-07-29 21:12:31 +04:00
using DocumentFormat.OpenXml.Spreadsheet;
using ElectronicsShopContracts.BindingModels;
2024-06-09 20:35:36 +04:00
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopEmployeeApp.Models;
using Microsoft.AspNetCore.Mvc;
2024-05-29 21:21:17 +04:00
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Diagnostics;
namespace ElectronicsShopEmployeeApp.Controllers {
public class HomeController : Controller {
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger) {
_logger = logger;
}
2024-05-28 12:50:24 +04:00
public IActionResult CostItem() {
if (APIEmployee.Employee == null) {
return Redirect("~/Home/Enter");
}
return View(APIEmployee.GetRequset<List<CostItemViewModel>>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}"));
}
public IActionResult Index() {
if (APIEmployee.Employee == null) {
return Redirect("~/Home/Enter");
}
2024-05-28 18:22:47 +04:00
return View(APIEmployee.GetRequset<List<ProductViewModel>>($"api/main/getproducts"));
}
[HttpGet]
public IActionResult Privacy() {
if (APIEmployee.Employee == null) {
return Redirect("~/Home/Enter");
}
return View(APIEmployee.Employee);
}
[HttpPost]
public void Privacy(string login, string password, string fio) {
if (APIEmployee.Employee == null) {
throw new Exception("<22><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD>");
}
APIEmployee.PostRequest("api/employee/updatedata", new EmployeeBindingModel {
ID = APIEmployee.Employee.ID,
EmployeeFIO = fio,
Login = login,
Password = password,
});
APIEmployee.Employee.EmployeeFIO = fio;
APIEmployee.Employee.Login = login;
APIEmployee.Employee.Password = password;
Response.Redirect("Index");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error() {
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
[HttpGet]
public IActionResult Enter() {
return View();
}
2024-05-27 17:35:14 +04:00
[HttpPost]
public void Enter(string login, string password) {
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
2024-05-27 17:35:14 +04:00
APIEmployee.Employee = APIEmployee.GetRequset<EmployeeViewModel>($"api/employee/login?login={login}&password={password}");
if (APIEmployee.Employee == null) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
Response.Redirect("Index");
}
[HttpGet]
public IActionResult Register() {
return View();
}
2024-05-27 17:35:14 +04:00
[HttpPost]
public void Register(string login, string password, string fio) {
if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(fio)) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD>");
}
APIEmployee.PostRequest("api/employee/register", new EmployeeBindingModel {
EmployeeFIO = fio,
Login = login,
Password = password
});
Response.Redirect("Enter");
return;
}
2024-05-27 22:05:47 +04:00
[HttpGet]
public IActionResult CreateCostItem() {
return View();
}
[HttpPost]
public void CreateCostItem(string name, double price, int costNum) {
if (APIEmployee.Employee == null) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
if (price <= 0) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0");
}
2024-05-28 12:50:24 +04:00
APIEmployee.PostRequest("api/employee/createcostitem", new CostItemBindingModel {
2024-05-27 22:05:47 +04:00
EmployeeID = APIEmployee.Employee.ID,
Name = name,
Price = price,
CostNum = costNum
});
2024-05-31 19:54:47 +04:00
Response.Redirect("CostItem");
2024-05-27 22:05:47 +04:00
}
2024-05-30 13:08:24 +04:00
[HttpGet]
public IActionResult EditCostItem(int id) {
var _costitem = APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={id}");
if (_costitem == null) {
return Redirect("/Home/CostItem");
}
var obj = new CostItemViewModel {
ID = _costitem.ID,
Name = _costitem.Name,
Price = _costitem.Price,
CostNum = _costitem.CostNum,
};
return View(obj);
}
[HttpPost]
public void EditCostItem(string name, double price, int costNum, int _ID) {
if (APIEmployee.Employee == null) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
if (price <= 0) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0");
}
2024-06-09 20:35:36 +04:00
var _costitem = APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={_ID}");
if (_costitem == null) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
double oldPrice = _costitem.Price;
2024-05-30 13:08:24 +04:00
APIEmployee.PostRequest("api/employee/editcostitem", new CostItemBindingModel {
ID = _ID,
Name = name,
Price = price,
CostNum = costNum,
EmployeeID = APIEmployee.Employee.ID
});
2024-06-09 20:35:36 +04:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> costitemid
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
var productList = APIEmployee.GetRequset<List<ProductViewModel>>($"api/main/getproducts?_costitemid={_ID}");
if (productList != null) {
foreach (var item in productList) {
APIEmployee.PostRequest("api/employee/editproduct", new ProductBindingModel {
ID = item.ID,
CostItemID = item.CostItemID,
ProductName = item.ProductName,
Price = Calc(item.CostItemID, item.Price - oldPrice)
});
}
}
2024-05-30 13:08:24 +04:00
Response.Redirect("CostItem");
}
[HttpGet]
2024-05-28 12:50:24 +04:00
public IActionResult CreateProduct() {
2024-06-09 20:35:36 +04:00
ViewBag.CostItems = APIEmployee.GetRequset<List<CostItemViewModel>>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee?.ID}");
return View();
}
2024-05-27 18:36:13 +04:00
[HttpPost]
2024-05-30 13:08:24 +04:00
public void CreateProduct(string name, int costitem, double productprice, double price) {
2024-05-27 18:36:13 +04:00
if (APIEmployee.Employee == null) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
2024-05-30 13:08:24 +04:00
if (price <= 0) {
2024-05-27 18:36:13 +04:00
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0");
}
2024-05-28 12:50:24 +04:00
APIEmployee.PostRequest("api/employee/createproduct", new ProductBindingModel {
CostItemID = costitem,
ProductName = name,
Price = Calc(costitem, productprice)
2024-05-27 18:36:13 +04:00
});
Response.Redirect("Index");
}
2024-05-27 18:36:13 +04:00
2024-05-30 13:08:24 +04:00
[HttpGet]
public IActionResult DeleteCostItem(int id) {
var _costItem = APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={id}");
if (_costItem == null) {
2024-06-09 20:35:36 +04:00
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
var _product = APIEmployee.GetRequset<ProductViewModel>($"api/main/getproduct?_costitemID={_costItem.ID}");
if (_product != null) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
2024-05-30 13:08:24 +04:00
}
APIEmployee.PostRequest("api/employee/deletecostitem", new CostItemBindingModel { ID = id });
return RedirectToAction("CostItem");
}
2024-05-29 21:21:17 +04:00
[HttpGet]
public IActionResult EditProduct(int id) {
var _product = APIEmployee.GetRequset<ProductViewModel>($"api/main/getproduct?_productid={id}");
2024-06-09 20:35:36 +04:00
2024-05-29 21:21:17 +04:00
if (_product == null) {
2024-06-09 20:35:36 +04:00
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
var _costitem = APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={_product.CostItemID}");
if (_costitem == null) {
2024-05-29 21:21:17 +04:00
return Redirect("/Home/Index");
}
2024-05-30 13:08:24 +04:00
ViewBag.CostItems = APIEmployee.GetRequset<List<CostItemViewModel>>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee?.ID}");
2024-05-29 21:21:17 +04:00
var obj = new ProductViewModel {
ProductName = _product.ProductName,
CostItemID = _product.CostItemID,
2024-06-09 20:35:36 +04:00
Price = _product.Price - _costitem.Price,
2024-05-29 21:21:17 +04:00
ID = _product.ID
};
2024-05-30 13:08:24 +04:00
if (ViewBag.CostItems.Count != 0) {
var _costitemLoad = (APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={obj.CostItemID}"));
2024-05-29 21:21:17 +04:00
2024-05-30 13:08:24 +04:00
if (_costitemLoad == null) {
return Redirect("/Home/Index");
2024-05-29 21:21:17 +04:00
}
2024-05-30 13:08:24 +04:00
if (_costitemLoad?.Name != ViewBag.CostItems[0].Name) {
int index = 0;
for (int i = 0; i < ViewBag.CostItems.Count; i++) {
if (ViewBag.CostItems[i].Name == _costitemLoad?.Name) {
index = i;
break;
}
}
var tmp = ViewBag.CostItems[0];
ViewBag.CostItems[0] = _costitemLoad;
ViewBag.CostItems[index] = tmp;
}
2024-05-29 21:21:17 +04:00
}
2024-05-30 13:08:24 +04:00
2024-05-29 21:21:17 +04:00
return View(obj);
}
[HttpPost]
2024-06-09 20:35:36 +04:00
public void EditProduct(string name, int costitem, int _ID, double price, double productprice) {
2024-05-29 21:21:17 +04:00
if (APIEmployee.Employee == null) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
2024-05-30 13:08:24 +04:00
if (price <= 0) {
2024-05-29 21:21:17 +04:00
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0");
}
APIEmployee.PostRequest("api/employee/editproduct", new ProductBindingModel {
ID = _ID,
CostItemID = costitem,
ProductName = name,
Price = Calc(costitem, productprice)
});
2024-07-28 13:06:28 +04:00
Response.Redirect("/Home/Index");
2024-05-29 21:21:17 +04:00
}
2024-05-29 21:45:37 +04:00
[HttpGet]
public IActionResult DeleteProduct(int id) {
APIEmployee.PostRequest("api/employee/deleteproduct", new ProductBindingModel {
ID = id
});
return RedirectToAction("Index");
}
2024-05-29 21:21:17 +04:00
2024-05-28 12:50:24 +04:00
[HttpPost]
2024-05-28 18:22:47 +04:00
public double Calc(int costitem, double productprice) {
2024-05-28 12:50:24 +04:00
var _costItem = APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={costitem}");
2024-06-09 20:35:36 +04:00
return productprice + (_costItem?.Price ?? 0);
}
[HttpPost]
public double CalcReload(int costitem, int productid) {
var _costItem = APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={costitem}");
var product = APIEmployee.GetRequset<ProductViewModel>($"api/main/getproduct?_productid={productid}");
if (product == null) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
return product.Price + (_costItem?.Price ?? 0);
2024-05-27 18:36:13 +04:00
}
2024-07-28 13:06:28 +04:00
[HttpGet]
public IActionResult Report() {
if (APIEmployee.Employee == null) {
return RedirectToAction("~/Home/Index");
}
return View();
}
[HttpPost]
public void Report(DateTime DateFrom, DateTime DateTo) {
if (DateTo == DateTime.MinValue || DateFrom > DateTo) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
Response.Redirect($"ReportSearch?_datefrom={DateFrom}&_dateto={DateTo + DateTime.Now.TimeOfDay}");
}
[HttpGet]
public IActionResult ReportSearch(string _datefrom, string _dateto) {
var reports = APIEmployee.GetRequset<List<PaymeantViewModel>>($"api/client/getreport?_start={_datefrom}&_end={_dateto}");
(DateTime, DateTime, List<PaymeantViewModel>?) tuple = (DateTime.Parse(_datefrom), DateTime.Parse(_dateto), reports);
return View(tuple);
}
2024-07-29 21:12:31 +04:00
[HttpGet]
public IActionResult CreateWordReport(string DateFrom, string DateTo) {
var fileMemStream = APIEmployee.GetRequset<byte[]>($"api/Employee/CreateDocxReport?from={DateFrom}&to={DateTo}");
if (fileMemStream == null) {
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
}
return File(fileMemStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Report.docx");
}
2024-07-28 13:06:28 +04:00
}
}