Merge branch 'Storekeeper' into Worker_Raspaev

This commit is contained in:
Николай 2023-05-19 20:02:31 +04:00
commit f07b35cd87
7 changed files with 105 additions and 49 deletions

View File

@ -322,7 +322,7 @@ namespace HardwareShopStorekeeperApp.Controllers
{
throw new Exception($"Цена комплектующего не может быть меньше или равна 0");
}
APIClient.PostRequest("api/component/updatedata", new ComponentBindingModel
APIClient.PostRequest("api/component/updatecomponent", new ComponentBindingModel
{
Id = component,
ComponentName = name,

View File

@ -25,6 +25,7 @@
<thead>
<tr>
<th scope="col">Комплектующее</th>
<th scope="col">Стоимость</th>
<th scope="col">Количество</th>
<th scope="col">Сумма</th>
<th scope="col">Действие</th>
@ -74,28 +75,29 @@
console.log('try to add component')
var count = $('#count').val();
var component = $('#component').val();
if (component && count)
$.ajax({
method: "GET",
url: `/Storekeeper/GetComponent`,
data: { Id: component },
success: function (result) {
let flag = false
if (list.length > 0) {
list.forEach((elem) => {
if (elem.component.id === parseInt(result.id)) {
console.log('component already added')
flag = true
}
})
if (component && count && count > 0) {
$.ajax({
method: "GET",
url: `/Storekeeper/GetComponent`,
data: { Id: component },
success: function (result) {
let flag = false
if (list.length > 0) {
list.forEach((elem) => {
if (elem.component.id === parseInt(result.id)) {
console.log('component already added')
flag = true
}
})
}
if (!flag) list.push({ component: result, count: count })
reloadTable()
countElem.value = '1'
}
if (!flag) list.push({ component: result, count: count })
reloadTable()
countElem.value = '1'
}
}).fail(function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
})
}).fail(function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
})
}
})
saveBtn.addEventListener("click", () => {
@ -129,7 +131,7 @@
let price = 0;
let count = 0;
list.forEach((elem) => {
resultTable.innerHTML += `<tr><td>${elem.component.componentName}</td><td>${elem.count}</td><td>${Math.round(elem.component.cost * elem.count * 100) / 100}</td><td> \
resultTable.innerHTML += `<tr><td>${elem.component.componentName}</td><td>${elem.component.cost}</td><td>${elem.count}</td><td>${Math.round(elem.component.cost * elem.count * 100) / 100}</td><td> \
<div> \
<button onclick="deleteComponent(${count})" type="button" class="btn btn-danger"> \
<i class="fa fa-trash" aria-hidden="true"></i> \

View File

@ -28,6 +28,7 @@
<thead>
<tr>
<th scope="col">Комплектующее</th>
<th scope="col">Стоимость</th>
<th scope="col">Количество</th>
<th scope="col">Сумма</th>
<th scope="col">Действие</th>
@ -79,27 +80,28 @@
console.log('try to add component')
var count = $('#count').val();
var component = $('#component').val();
$.ajax({
method: "GET",
url: `/Storekeeper/GetComponent`,
data: { Id: component },
success: function (result) {
let flag = false
if (list.length > 0) {
list.forEach((elem) => {
if (elem.component.id === parseInt(result.id)) {
console.log('component already added')
flag = true
}
})
if (component && count && count > 0)
$.ajax({
method: "GET",
url: `/Storekeeper/GetComponent`,
data: { Id: component },
success: function (result) {
let flag = false
if (list.length > 0) {
list.forEach((elem) => {
if (elem.component.id === parseInt(result.id)) {
console.log('component already added')
flag = true
}
})
}
if (!flag) list.push({ component: result, count: count })
reloadTable()
countElem.value = '1'
}
if (!flag) list.push({ component: result, count: count })
reloadTable()
countElem.value = '1'
}
}).fail(function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
});
}).fail(function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
});
})
saveBtn.addEventListener("click", () => {
@ -133,7 +135,7 @@
let price = 0;
let count = 0;
list.forEach((elem) => {
resultTable.innerHTML += `<tr><td>${elem.component.componentName}</td><td>${elem.count}</td><td>${Math.round(elem.component.cost * elem.count * 100) / 100}</td><td> \
resultTable.innerHTML += `<tr><td>${elem.component.componentName}</td><td>${elem.component.cost}</td><td>${elem.count}</td><td>${Math.round(elem.component.cost * elem.count * 100) / 100}</td><td> \
<div> \
<button onclick="deleteComponent(${count})" type="button" class="btn btn-danger"> \
<i class="fa fa-trash" aria-hidden="true"></i> \

View File

@ -19,6 +19,7 @@ namespace HardwareShopDatabaseImplement.Implements.Storekeeper
if (element != null)
{
var componentGoods = context.GoodsComponents.Where(x => x.ComponentId == element.Id).ToList();
var componentBuilds = context.ComponentsBuilds.Where(x => x.ComponentId == element.Id).ToList();
componentGoods.ForEach(x =>
{
var good = context.Goods.Include(x => x.Components).FirstOrDefault(rec => rec.Id == x.GoodId);
@ -31,6 +32,15 @@ namespace HardwareShopDatabaseImplement.Implements.Storekeeper
good.Price = Math.Round(good.Price, 2);
}
});
componentBuilds.ForEach(x =>
{
var build = context.Builds.Include(x => x.Components).FirstOrDefault(rec => rec.Id == x.BuildId);
if (build != null)
{
build.Price -= element.Cost * x.Count;
build.Price = Math.Round(build.Price, 2);
}
});
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
@ -121,19 +131,36 @@ namespace HardwareShopDatabaseImplement.Implements.Storekeeper
using var transaction = context.Database.BeginTransaction();
try
{
var good = context.Components
var component = context.Components
.Include(x => x.Builds)
.ThenInclude(x => x.Build)
.FirstOrDefault(rec => rec.Id == model.Id);
if (good == null || good.UserId != model.UserId)
if (component == null || component.UserId != model.UserId)
{
return null;
}
good.Update(model);
component.Update(model);
context.SaveChanges();
if (model.ComponentBuilds == null)
{
context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Where(x => x.ContainsComponent(model.Id))
.ToList().ForEach(x => { x.Price = x.Components.Sum(x => x.Component.Cost * x.Count); });
}
else
component.UpdateBuilds(context, model);
context.Goods
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Where(x => x.ContainsComponent(model.Id))
.ToList().ForEach(x => { x.Price = x.Components.Sum(x => x.Component.Cost * x.Count); });
context.SaveChanges();
good.UpdateBuilds(context, model);
transaction.Commit();
return good.GetViewModel;
return component.GetViewModel;
}
catch
{

View File

@ -71,6 +71,11 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper
Price = model.Price;
}
public bool ContainsComponent(int componentId)
{
return GoodComponents.ContainsKey(componentId);
}
public GoodViewModel GetViewModel => new()
{
Id = Id,

View File

@ -91,6 +91,11 @@ namespace HardwareShopDatabaseImplement.Models.Worker
Price = model.Price;
}
public bool ContainsComponent(int componentId)
{
return BuildComponents.ContainsKey(componentId);
}
public BuildViewModel GetViewModel => new()
{
Id = Id,

View File

@ -105,6 +105,21 @@ namespace HardwareShopRestApi.Controllers
}
}
[HttpPost]
public void UpdateComponent(ComponentBindingModel model)
{
try
{
model.ComponentBuilds = null!;
_component.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных комплектующего");
throw;
}
}
[HttpPost]
public void DeleteComponent(ComponentBindingModel model)
{