Остались отчёты

This commit is contained in:
maxnes3 2023-05-19 21:07:14 +04:00
parent 39bc95794b
commit cc28f399a4
8 changed files with 194 additions and 9 deletions

View File

@ -25,12 +25,17 @@ namespace CaseAccountingContracts.BindingModels
public List<CaseViewModel> CaseViewModels { get; set; } = new(); public List<CaseViewModel> CaseViewModels { get; set; } = new();
public Dictionary<int, IContractModel> Contracts { get; set; } = new();
public List<ContractViewModel> ContractViewModels { get; set; } = new();
public DealBindingModel() { } public DealBindingModel() { }
[JsonConstructor] [JsonConstructor]
public DealBindingModel(Dictionary<int, CaseViewModel> Cases) public DealBindingModel(Dictionary<int, CaseViewModel> Cases, Dictionary<int, ContractViewModel> Contracts)
{ {
this.Cases = Cases.ToDictionary(x => x.Key, x => (ICaseModel)x.Value); this.Cases = Cases.ToDictionary(x => x.Key, x => (ICaseModel)x.Value);
this.Contracts = Contracts.ToDictionary(x => x.Key, x => (IContractModel)x.Value);
} }
} }
} }

View File

@ -115,9 +115,9 @@ namespace CaseAccountingDataBaseImplement.Implements
{ {
return null; return null;
} }
deal.Update(model); deal.Update(context, model);
context.SaveChanges(); context.SaveChanges();
deal.UpdateCases(context, model); if(model.Cases.Count > 0) deal.UpdateCases(context, model);
transaction.Commit(); transaction.Commit();
return deal.GetViewModel; return deal.GetViewModel;
} }

View File

@ -72,7 +72,7 @@ namespace CaseAccountingDataBaseImplement.Models
}; };
} }
public void Update(DealBindingModel? model) public void Update(CaseAccountingDatabase context, DealBindingModel? model)
{ {
if (model == null) if (model == null)
{ {
@ -81,6 +81,13 @@ namespace CaseAccountingDataBaseImplement.Models
Subject = model.Subject; Subject = model.Subject;
Responsibilities = model.Responsibilities; Responsibilities = model.Responsibilities;
Date = model.Date; Date = model.Date;
if (model.Contracts.Count > 0)
{
Contracts = model.Contracts.Select(x => new DealContract
{
Contract = context.Contracts.First(y => y.Id == x.Key)
}).ToList();
}
} }
public void UpdateCases(CaseAccountingDatabase context, DealBindingModel model) public void UpdateCases(CaseAccountingDatabase context, DealBindingModel model)

View File

@ -53,7 +53,14 @@ namespace CaseAccountingProviderView.Controllers
throw new Exception("403"); throw new Exception("403");
} }
dealModel.UserId = APIUser.User.Id; dealModel.UserId = APIUser.User.Id;
APIUser.PostRequest("api/deal/update", dealModel); var contractdict = new Dictionary<int, IContractModel>();
foreach (var element in dealModel.ContractViewModels)
{
var contractModel = APIUser.GetRequest<ContractViewModel>($"api/contract/get?id={element.Id}");
contractdict.Add(element.Id, contractModel);
}
dealModel.Contracts = contractdict;
APIUser.PostRequest("api/deal/update", dealModel);
Response.Redirect("/Home/Deals"); Response.Redirect("/Home/Deals");
} }
@ -127,5 +134,15 @@ namespace CaseAccountingProviderView.Controllers
DealViewModel? dealModel = APIUser.GetRequest<DealViewModel>($"api/deal/get?id={id}"); DealViewModel? dealModel = APIUser.GetRequest<DealViewModel>($"api/deal/get?id={id}");
return dealModel; return dealModel;
} }
}
public List<ContractViewModel> GetAllContracts()
{
if (APIUser.User == null)
{
return new();
}
List<ContractViewModel>? contractModel = APIUser.GetRequest<List<ContractViewModel>>($"api/deal/getallcontracts");
return contractModel ?? new();
}
}
} }

View File

@ -0,0 +1,37 @@
@{
ViewData["Title"] = "Договор";
}
@{
<h4 id="deal-data" class="fw-bold" data-id="@ViewBag.Deal.Id">Привязка контракта к договору</h4>
<div id="error-div-shell" class="error-div-shell mb-2">
<div>
<p id="error-p" class="error-p"></p>
</div>
</div>
<p class="mb-0">Номер:</p>
<input type="text" readonly value="@ViewBag.Deal.Id" id="name-input" name="name" class="form-control mb-3" />
<div>
<div class="scrollable-table">
<table class="table table-bordered">
<thead class="thead-light">
<tr>
<th>Услуга:</th>
<th>Цена:</th>
<th>Дата:</th>
</tr>
</thead>
<tbody id="scrollable-table__tbody">
</tbody>
</table>
</div>
</div>
<button id="save-button" type="button" class="btn btn-primary text-button">
Сохранить привязку
</button>
}
<script src="~/js/deal/deal-bind.js" asp-append-version="true"></script>

View File

@ -28,6 +28,9 @@
<th> <th>
Дата составления Дата составления
</th> </th>
<th>
Привязать запись
</th>
<th> <th>
Изменить запись Изменить запись
</th> </th>
@ -49,6 +52,9 @@
<td> <td>
@item.Date.ToString("yyyy-MM-dd") @item.Date.ToString("yyyy-MM-dd")
</td> </td>
<td>
<a id="update-button-@item.Id" class="btn btn-primary" asp-controller="Deal" asp-action="Bind" asp-route-id="@item.Id">Привязка</a>
</td>
<td> <td>
<a id="update-button-@item.Id" class="btn btn-warning" asp-controller="Deal" asp-action="Update" asp-route-id="@item.Id">Изменить</a> <a id="update-button-@item.Id" class="btn btn-warning" asp-controller="Deal" asp-action="Update" asp-route-id="@item.Id">Изменить</a>
</td> </td>

View File

@ -0,0 +1,97 @@
const saveBtn = document.getElementById("save-button");
const tbody = document.getElementById("scrollable-table__tbody");
const currentDealId = document.getElementById("deal-data").dataset.id;
var contracts = [];
var dataArray = [];
var currentDeal = null;
window.addEventListener("load", async () => {
try {
await $.ajax({
url: `/deal/getallcontracts`,
type: "GET",
contentType: "json"
}).done((result) => {
contracts = result;
console.log(contracts);
contracts.forEach((contract) => {
const { id, service, coast, date } = contract;
const row = tbody.insertRow();
row.setAttribute("data-id", id);
const cells = [service, coast, date];
cells.forEach((value) => {
const cell = row.insertCell();
cell.textContent = value;
});
row.addEventListener('click', () => addAndRemoveFromList(row));
});
});
await $.ajax({
url: `/deal/get?id=${currentDealId}`,
type: "GET",
contentType: "json"
}).done((result) => {
currentDeal = result;
console.log(currentDeal)
});
} catch (error) {
console.error(error);
}
});
saveBtn.addEventListener("click", () => {
if (!correctData()) {
return;
}
if (!validate()) {
return;
}
});
const correctData = function () {
return true;
};
const validate = function () {
return true;
};
saveBtn.addEventListener("click", () => {
let dealModel = {
"Id": currentDeal.id,
"Subject": currentDeal.subject,
"Responsibilities": currentDeal.responsibilities,
"Date": currentDeal.date,
"ContractViewModels": dataArray
};
console.log(dealModel);
console.log(dataArray);
$.ajax({
url: "/deal/update",
type: "POST",
contentType: "application/json",
data: JSON.stringify(dealModel)
}).done(() => {
window.location.href = "/Home/Deals";
});
});
const addAndRemoveFromList = (row) => {
var id = parseInt(row.dataset.id);
console.log(contracts.find(x => x.id === id))
var index = dataArray.indexOf(contracts.find(x => x.id === id));
if (index === -1) {
dataArray.push(contracts.find(x => x.id === id));
row.classList.add("bg-primary");
} else {
dataArray.splice(index, 1);
row.classList.remove("bg-primary");
}
console.log(dataArray);
}

View File

@ -1,4 +1,5 @@
using CaseAccountingContracts.BindingModels; using CaseAccountingBusinessLogic.BusinessLogics;
using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.BusinessLogicContracts; using CaseAccountingContracts.BusinessLogicContracts;
using CaseAccountingContracts.SearchModels; using CaseAccountingContracts.SearchModels;
using CaseAccountingContracts.ViewModels; using CaseAccountingContracts.ViewModels;
@ -11,10 +12,12 @@ namespace CaseAccountingRestApi.Controllers
public class DealController : Controller public class DealController : Controller
{ {
private readonly IDealLogic _logic; private readonly IDealLogic _logic;
private readonly IContractLogic _contractLogic;
public DealController(IDealLogic logic) public DealController(IDealLogic logic, IContractLogic contractLogic)
{ {
_logic = logic; _logic = logic;
_contractLogic = contractLogic;
} }
[HttpGet] [HttpGet]
@ -43,7 +46,20 @@ namespace CaseAccountingRestApi.Controllers
} }
} }
[HttpPost] [HttpGet]
public List<ContractViewModel>? GetAllContracts()
{
try
{
return _contractLogic.ReadList(null);
}
catch (Exception)
{
throw;
}
}
[HttpPost]
public void Create(DealBindingModel model) public void Create(DealBindingModel model)
{ {
try try