UpdateStockQuantityAndWeightAsync
This commit is contained in:
parent
f72a031a7e
commit
ad5f27a77c
|
|
@ -6,15 +6,18 @@ using FruitBank.Common.Dtos;
|
||||||
using FruitBank.Common.Entities;
|
using FruitBank.Common.Entities;
|
||||||
using FruitBank.Common.Interfaces;
|
using FruitBank.Common.Interfaces;
|
||||||
using FruitBank.Common.Models;
|
using FruitBank.Common.Models;
|
||||||
|
using Mango.Nop.Core.Entities;
|
||||||
using Mango.Nop.Core.Extensions;
|
using Mango.Nop.Core.Extensions;
|
||||||
using Mango.Nop.Core.Loggers;
|
using Mango.Nop.Core.Loggers;
|
||||||
using Mango.Nop.Data.Repositories;
|
using Mango.Nop.Data.Repositories;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Nop.Core;
|
using Nop.Core;
|
||||||
using Nop.Core.Caching;
|
using Nop.Core.Caching;
|
||||||
using Nop.Core.Domain.Catalog;
|
using Nop.Core.Domain.Catalog;
|
||||||
using Nop.Core.Domain.Common;
|
using Nop.Core.Domain.Common;
|
||||||
using Nop.Core.Domain.Customers;
|
using Nop.Core.Domain.Customers;
|
||||||
using Nop.Core.Domain.Orders;
|
using Nop.Core.Domain.Orders;
|
||||||
|
using Nop.Core.Domain.Shipping;
|
||||||
using Nop.Core.Events;
|
using Nop.Core.Events;
|
||||||
using Nop.Data;
|
using Nop.Data;
|
||||||
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer.Interfaces;
|
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer.Interfaces;
|
||||||
|
|
@ -65,6 +68,8 @@ public class FruitBankDbContext : MgDbContextBase,
|
||||||
public IRepository<CustomerAddressMapping> CustomerAddressMappings { get; set; }
|
public IRepository<CustomerAddressMapping> CustomerAddressMappings { get; set; }
|
||||||
|
|
||||||
public IRepository<GenericAttribute> GenericAttributes { get; set; }
|
public IRepository<GenericAttribute> GenericAttributes { get; set; }
|
||||||
|
public IRepository<StockQuantityHistory> StockQuantityHistories { get; set; }
|
||||||
|
public IRepository<StockQuantityHistoryExt> StockQuantityHistoriesExt { get; set; }
|
||||||
|
|
||||||
public FruitBankDbContext(INopDataProvider dataProvider, ILockService lockService, FruitBankAttributeService fruitBankAttributeService, IStoreContext storeContext,
|
public FruitBankDbContext(INopDataProvider dataProvider, ILockService lockService, FruitBankAttributeService fruitBankAttributeService, IStoreContext storeContext,
|
||||||
PartnerDbTable partnerDbTable, ShippingDbTable shippingDbTable, ShippingDocumentDbTable shippingDocumentDbTable, ShippingItemDbTable shippingItemDbTable,
|
PartnerDbTable partnerDbTable, ShippingDbTable shippingDbTable, ShippingDocumentDbTable shippingDocumentDbTable, ShippingItemDbTable shippingItemDbTable,
|
||||||
|
|
@ -79,6 +84,8 @@ public class FruitBankDbContext : MgDbContextBase,
|
||||||
IRepository<CustomerAddressMapping> customerAddressMappingRepository,
|
IRepository<CustomerAddressMapping> customerAddressMappingRepository,
|
||||||
IRepository<CustomerRole> customerRoleRepository,
|
IRepository<CustomerRole> customerRoleRepository,
|
||||||
IRepository<GenericAttribute> genericAttributes,
|
IRepository<GenericAttribute> genericAttributes,
|
||||||
|
IRepository<StockQuantityHistory> stockQuantityHistories,
|
||||||
|
IRepository<StockQuantityHistoryExt> stockQuantityHistoriesExt,
|
||||||
IEventPublisher eventPublisher,
|
IEventPublisher eventPublisher,
|
||||||
IEnumerable<IAcLogWriterBase> logWriters) : base(productRepository, orderRepository, orderItemRepository, dataProvider, lockService, new Logger<FruitBankDbContext>(logWriters.ToArray()))
|
IEnumerable<IAcLogWriterBase> logWriters) : base(productRepository, orderRepository, orderItemRepository, dataProvider, lockService, new Logger<FruitBankDbContext>(logWriters.ToArray()))
|
||||||
{
|
{
|
||||||
|
|
@ -110,6 +117,9 @@ public class FruitBankDbContext : MgDbContextBase,
|
||||||
CustomerAddressMappings = customerAddressMappingRepository;
|
CustomerAddressMappings = customerAddressMappingRepository;
|
||||||
|
|
||||||
GenericAttributes = genericAttributes;
|
GenericAttributes = genericAttributes;
|
||||||
|
|
||||||
|
StockQuantityHistories = stockQuantityHistories;
|
||||||
|
StockQuantityHistoriesExt = stockQuantityHistoriesExt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IQueryable<Customer> GetCustomersBySystemRoleName(string systemRoleName)
|
public IQueryable<Customer> GetCustomersBySystemRoleName(string systemRoleName)
|
||||||
|
|
@ -282,24 +292,28 @@ public class FruitBankDbContext : MgDbContextBase,
|
||||||
if (shippingItem.IsMeasured)
|
if (shippingItem.IsMeasured)
|
||||||
{
|
{
|
||||||
var quantityInc = productIdChanged ? shippingItem.MeasuredQuantity : shippingItem.MeasuredQuantity - dbShippingItem.MeasuredQuantity;
|
var quantityInc = productIdChanged ? shippingItem.MeasuredQuantity : shippingItem.MeasuredQuantity - dbShippingItem.MeasuredQuantity;
|
||||||
productDto!.StockQuantity += quantityInc;
|
|
||||||
|
|
||||||
if (!await UpdateProductDtoStockQuantityAsync(productDto, true))
|
//productDto!.StockQuantity += quantityInc;
|
||||||
throw new Exception($"UpdateProductStockQuantity() == false; shippingItem! product.Id: {productDto.Id}");
|
//if (!await UpdateProductDtoStockQuantityAsync(productDto, true))
|
||||||
|
// throw new Exception($"UpdateProductStockQuantity() == false; shippingItem! product.Id: {productDto.Id}");
|
||||||
|
|
||||||
var incomingQuantity = productDto.GenericAttributes.GetValueOrNull<int>(nameof(IIncomingQuantity.IncomingQuantity));
|
var incomingQuantity = productDto!.GenericAttributes.GetValueOrNull<int>(nameof(IIncomingQuantity.IncomingQuantity));
|
||||||
if (incomingQuantity != null)
|
if (incomingQuantity != null)
|
||||||
{
|
{
|
||||||
await _fruitBankAttributeService.UpdateGenericAttributeAsync<Product, int>
|
await _fruitBankAttributeService.UpdateGenericAttributeAsync<Product, int>
|
||||||
(productDto.Id, nameof(IIncomingQuantity.IncomingQuantity), incomingQuantity.Value - quantityInc);
|
(productDto.Id, nameof(IIncomingQuantity.IncomingQuantity), incomingQuantity.Value - quantityInc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var weightToChange = 0d;
|
||||||
if (productIsMeasurable)
|
if (productIsMeasurable)
|
||||||
{
|
{
|
||||||
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(productDto.Id,
|
weightToChange = productIdChanged ? shippingItem.MeasuredNetWeight : shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight;
|
||||||
productIdChanged ? shippingItem.MeasuredNetWeight : shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight,
|
|
||||||
shippingItem.IsMeasurable, true);
|
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(productDto.Id, weightToChange, shippingItem.IsMeasurable, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await UpdateStockQuantityAndWeightAsync(productDto, quantityInc, $"Bejövő mérés, shippingItem: #{shippingItem.Id}", weightToChange);
|
||||||
|
productDto!.StockQuantity += quantityInc;
|
||||||
}
|
}
|
||||||
|
|
||||||
//if (productIdUnchanged || !dbShippingItem.IsMeasured) return true;
|
//if (productIdUnchanged || !dbShippingItem.IsMeasured) return true;
|
||||||
|
|
@ -310,10 +324,10 @@ public class FruitBankDbContext : MgDbContextBase,
|
||||||
if (productDto != null)
|
if (productDto != null)
|
||||||
{
|
{
|
||||||
productIsMeasurable = productDto.IsMeasurable;
|
productIsMeasurable = productDto.IsMeasurable;
|
||||||
productDto.StockQuantity -= dbShippingItem.MeasuredQuantity;
|
|
||||||
|
|
||||||
if (!await UpdateProductDtoStockQuantityAsync(productDto, true))
|
//productDto.StockQuantity -= dbShippingItem.MeasuredQuantity;
|
||||||
throw new Exception($"UpdateProductStockQuantity() == false; dbShippingItem! product.Id: {productDto.Id}");
|
//if (!await UpdateProductDtoStockQuantityAsync(productDto, true))
|
||||||
|
// throw new Exception($"UpdateProductStockQuantity() == false; dbShippingItem! product.Id: {productDto.Id}");
|
||||||
|
|
||||||
var incomingQuantity = productDto.GenericAttributes.GetValueOrNull<int>(nameof(IIncomingQuantity.IncomingQuantity));
|
var incomingQuantity = productDto.GenericAttributes.GetValueOrNull<int>(nameof(IIncomingQuantity.IncomingQuantity));
|
||||||
if (incomingQuantity != null)
|
if (incomingQuantity != null)
|
||||||
|
|
@ -322,11 +336,15 @@ public class FruitBankDbContext : MgDbContextBase,
|
||||||
(productDto.Id, nameof(IIncomingQuantity.IncomingQuantity), incomingQuantity.Value + dbShippingItem.MeasuredQuantity);
|
(productDto.Id, nameof(IIncomingQuantity.IncomingQuantity), incomingQuantity.Value + dbShippingItem.MeasuredQuantity);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!productIsMeasurable) return true;
|
if (productIsMeasurable)
|
||||||
|
{
|
||||||
var measuringValues = new MeasuringAttributeValues(productDto.Id, -dbShippingItem.MeasuredNetWeight, dbShippingItem.IsMeasurable);
|
var measuringValues = new MeasuringAttributeValues(productDto.Id, -dbShippingItem.MeasuredNetWeight, dbShippingItem.IsMeasurable);
|
||||||
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(measuringValues, true);
|
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(measuringValues, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await UpdateStockQuantityAndWeightAsync(productDto, -dbShippingItem.MeasuredQuantity, $"Bejövő mérés, ShippingItem.Id: #{shippingItem.Id}. Product.Id megváltozott, #{productDto.Id}->#{shippingItem.ProductId}!", -dbShippingItem.MeasuredNetWeight);
|
||||||
|
productDto!.StockQuantity -= dbShippingItem.MeasuredQuantity;
|
||||||
|
}
|
||||||
else Logger.Warning($"product == null; dbShippingItem.ProductId: {dbShippingItem.ProductId}");
|
else Logger.Warning($"product == null; dbShippingItem.ProductId: {dbShippingItem.ProductId}");
|
||||||
//else //TODO: productIdUnchanged-et lekezelni! - J.
|
//else //TODO: productIdUnchanged-et lekezelni! - J.
|
||||||
|
|
||||||
|
|
@ -487,8 +505,12 @@ public class FruitBankDbContext : MgDbContextBase,
|
||||||
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<OrderItem, double>
|
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<OrderItem, double>
|
||||||
(orderItemDto.Id, nameof(IMeasuringNetWeight.NetWeight), orderItemDto.NetWeight);
|
(orderItemDto.Id, nameof(IMeasuringNetWeight.NetWeight), orderItemDto.NetWeight);
|
||||||
|
|
||||||
|
var weightToChange = -(orderItemDto.NetWeight - prevNetWeightFromGa);
|
||||||
|
|
||||||
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>
|
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>
|
||||||
(orderItemDto.ProductId, -(orderItemDto.NetWeight - prevNetWeightFromGa), orderItemDto.IsMeasurable, true);
|
(orderItemDto.ProductId, weightToChange, orderItemDto.IsMeasurable, true);
|
||||||
|
|
||||||
|
await UpdateStockQuantityAndWeightAsync(orderItemDto.ProductId, 0, $"Kimenő mérés, OrderStatus set to complete. Rendelés: #{orderDto.Id}, rendelés tétel: #{orderItemDto.Id}", weightToChange, prevNetWeightFromGa + weightToChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
//await _eventPublisher.PublishAsync(new OrderStatusChangedEvent(order, prevOrderStatus));
|
//await _eventPublisher.PublishAsync(new OrderStatusChangedEvent(order, prevOrderStatus));
|
||||||
|
|
@ -598,32 +620,89 @@ public class FruitBankDbContext : MgDbContextBase,
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<bool> UpdateProductDtoStockQuantityAsync(int productDtoId, bool publishEvent)
|
//private async Task<bool> UpdateProductDtoStockQuantityAsync(int productDtoId, bool publishEvent)
|
||||||
{
|
//{
|
||||||
var productDto = await ProductDtos.GetByIdAsync(productDtoId, false);
|
// var productDto = await ProductDtos.GetByIdAsync(productDtoId, false);
|
||||||
if (productDto != null) return await UpdateProductDtoStockQuantityAsync(productDto, publishEvent);
|
// if (productDto != null) return await UpdateProductDtoStockQuantityAsync(productDto, publishEvent);
|
||||||
|
|
||||||
Logger.Error($"product == null; id: {productDtoId}");
|
// Logger.Error($"product == null; id: {productDtoId}");
|
||||||
return await Task.FromResult(false);
|
// return await Task.FromResult(false);
|
||||||
|
//}
|
||||||
|
|
||||||
|
//private async Task<bool> UpdateProductDtoStockQuantityAsync(ProductDto productDto, bool publishEvent)
|
||||||
|
//{
|
||||||
|
// //TODO: !!!!!!!!!!!!!!!! - J.
|
||||||
|
// //await _productService.AdjustInventoryAsync(product, quantityInc, string.Empty, "");
|
||||||
|
|
||||||
|
// await ProductDtos.UpdateAsync(productDto, publishEvent);
|
||||||
|
// return await Task.FromResult(true);
|
||||||
|
|
||||||
|
// //var updatedRowsCount = await DataProvider.ExecuteNonQueryAsync($"update product set {nameof(Product.StockQuantity)} = {product.StockQuantity} where {nameof(Product.Id)} = {product.Id}");
|
||||||
|
// //if (updatedRowsCount == 1) return await Task.FromResult(true);
|
||||||
|
|
||||||
|
// //Logger.Error($"Product updatedRowsCount != 1; id: {product.Id}");
|
||||||
|
// //return await Task.FromResult(false);
|
||||||
|
//}
|
||||||
|
|
||||||
|
public async Task UpdateStockQuantityAndWeightAsync(int productId, int quantityToChange, string message, double weightToChange = 0, double stockWeight = 0)
|
||||||
|
{
|
||||||
|
var product = await Products.GetByIdAsync(productId);
|
||||||
|
if (weightToChange != 0 && stockWeight == 0) stockWeight = await _fruitBankAttributeService.GetGenericAttributeValueAsync<Product, double>(productId, nameof(IMeasuringNetWeight.NetWeight));
|
||||||
|
|
||||||
|
await UpdateStockQuantityAndWeightAsync(product, quantityToChange, message, weightToChange, stockWeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<bool> UpdateProductDtoStockQuantityAsync(ProductDto productDto, bool publishEvent)
|
public Task UpdateStockQuantityAndWeightAsync(ProductDto productDto, int quantityToChange, string message, double weightToChange = 0)
|
||||||
|
=> UpdateStockQuantityAndWeightAsync(productDto.Id, quantityToChange, message, weightToChange, productDto.NetWeight + weightToChange);
|
||||||
|
|
||||||
|
public async Task UpdateStockQuantityAndWeightAsync(Product product, int quantityToChange, string message, double weightToChange = 0, double stockWeight = 0)
|
||||||
{
|
{
|
||||||
//Itt mi legyen? RollBack? - J.
|
var latStockQuantityHistoryId = 0;
|
||||||
if (productDto.StockQuantity < 0)
|
|
||||||
Logger.Error($"productDto.StockQuantity < 0; Id: {productDto.Id}; StockQuantity: {productDto.StockQuantity}");
|
|
||||||
|
|
||||||
//TODO: !!!!!!!!!!!!!!!! - J.
|
if (quantityToChange != 0) await _productService.AdjustInventoryAsync(product, quantityToChange, string.Empty, message);
|
||||||
//await _productService.AdjustInventoryAsync(product, quantityInc, string.Empty, "");
|
else if (weightToChange != 0)
|
||||||
|
{
|
||||||
|
//Vizsgálja, h a quantityToChange != 0... - J.
|
||||||
|
//await _productService.AddStockQuantityHistoryEntryAsync(product, 0, product.StockQuantity, product.WarehouseId, message);
|
||||||
|
|
||||||
await ProductDtos.UpdateAsync(productDto, publishEvent);
|
var historyEntry = new StockQuantityHistory
|
||||||
return await Task.FromResult(true);
|
{
|
||||||
|
ProductId = product.Id,
|
||||||
|
CombinationId = null,
|
||||||
|
WarehouseId = product.WarehouseId > 0 ? (int?)product.WarehouseId : null,
|
||||||
|
QuantityAdjustment = 0,
|
||||||
|
StockQuantity = product.StockQuantity,
|
||||||
|
Message = message,
|
||||||
|
CreatedOnUtc = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
|
||||||
//var updatedRowsCount = await DataProvider.ExecuteNonQueryAsync($"update product set {nameof(Product.StockQuantity)} = {product.StockQuantity} where {nameof(Product.Id)} = {product.Id}");
|
await StockQuantityHistories.InsertAsync(historyEntry);
|
||||||
//if (updatedRowsCount == 1) return await Task.FromResult(true);
|
latStockQuantityHistoryId = historyEntry.Id;
|
||||||
|
}
|
||||||
|
else return;
|
||||||
|
|
||||||
//Logger.Error($"Product updatedRowsCount != 1; id: {product.Id}");
|
//await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Product, double>(product.Id, nameof(IMeasuringNetWeight.NetWeight), netWeight);
|
||||||
//return await Task.FromResult(false);
|
//if (weightToChange == 0 && stockWeight == 0) return;
|
||||||
|
|
||||||
|
if (latStockQuantityHistoryId <= 0)
|
||||||
|
{
|
||||||
|
//A LastOrDefaultAsync elszáll! - J.
|
||||||
|
latStockQuantityHistoryId = await StockQuantityHistories.Table.Where(x => x.ProductId == product.Id).MaxAsync(x => x.Id);
|
||||||
|
if (latStockQuantityHistoryId == 0)
|
||||||
|
{
|
||||||
|
Logger.Error($"UpdateProductDtoStockQuantityAndWeightAsync (latStockQuantityHistory == 0). product.Id: {product.Id}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var stockQuantityHistoryExt = new StockQuantityHistoryExt
|
||||||
|
{
|
||||||
|
StockQuantityHistoryId = latStockQuantityHistoryId,
|
||||||
|
NetWeightAdjustment = double.Round(weightToChange, 1),
|
||||||
|
NetWeight = double.Round(stockWeight, 1),
|
||||||
|
};
|
||||||
|
|
||||||
|
await StockQuantityHistoriesExt.InsertAsync(stockQuantityHistoryExt, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<ShippingDocument>> GetShippingDocumentsByShippingIdAsync(int shippingId)
|
public async Task<List<ShippingDocument>> GetShippingDocumentsByShippingIdAsync(int shippingId)
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,10 @@ public class FruitBankEventConsumer :
|
||||||
var productDtoNetWeight = productDto?.GenericAttributes.GetValueOrNull<double>(nameof(IMeasuringNetWeight.NetWeight));
|
var productDtoNetWeight = productDto?.GenericAttributes.GetValueOrNull<double>(nameof(IMeasuringNetWeight.NetWeight));
|
||||||
|
|
||||||
if (productDtoNetWeight == null || double.Round(productDtoNetWeight.Value, 1) != netWeight)
|
if (productDtoNetWeight == null || double.Round(productDtoNetWeight.Value, 1) != netWeight)
|
||||||
|
{
|
||||||
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Product, double>(product.Id, nameof(IMeasuringNetWeight.NetWeight), netWeight);
|
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Product, double>(product.Id, nameof(IMeasuringNetWeight.NetWeight), netWeight);
|
||||||
|
await _ctx.UpdateStockQuantityAndWeightAsync(productDto, 0, $"Manuális készlet súly változtatás az admin felületen.", netWeight - productDtoNetWeight.Value);
|
||||||
|
}
|
||||||
|
|
||||||
//Tára
|
//Tára
|
||||||
var tare = double.Round(CommonHelper.To<double>(form[nameof(ITare.Tare)].ToString()), 1);
|
var tare = double.Round(CommonHelper.To<double>(form[nameof(ITare.Tare)].ToString()), 1);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue