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.Interfaces;
|
||||
using FruitBank.Common.Models;
|
||||
using Mango.Nop.Core.Entities;
|
||||
using Mango.Nop.Core.Extensions;
|
||||
using Mango.Nop.Core.Loggers;
|
||||
using Mango.Nop.Data.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Nop.Core;
|
||||
using Nop.Core.Caching;
|
||||
using Nop.Core.Domain.Catalog;
|
||||
using Nop.Core.Domain.Common;
|
||||
using Nop.Core.Domain.Customers;
|
||||
using Nop.Core.Domain.Orders;
|
||||
using Nop.Core.Domain.Shipping;
|
||||
using Nop.Core.Events;
|
||||
using Nop.Data;
|
||||
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer.Interfaces;
|
||||
|
|
@ -65,6 +68,8 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
public IRepository<CustomerAddressMapping> CustomerAddressMappings { 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,
|
||||
PartnerDbTable partnerDbTable, ShippingDbTable shippingDbTable, ShippingDocumentDbTable shippingDocumentDbTable, ShippingItemDbTable shippingItemDbTable,
|
||||
|
|
@ -79,6 +84,8 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
IRepository<CustomerAddressMapping> customerAddressMappingRepository,
|
||||
IRepository<CustomerRole> customerRoleRepository,
|
||||
IRepository<GenericAttribute> genericAttributes,
|
||||
IRepository<StockQuantityHistory> stockQuantityHistories,
|
||||
IRepository<StockQuantityHistoryExt> stockQuantityHistoriesExt,
|
||||
IEventPublisher eventPublisher,
|
||||
IEnumerable<IAcLogWriterBase> logWriters) : base(productRepository, orderRepository, orderItemRepository, dataProvider, lockService, new Logger<FruitBankDbContext>(logWriters.ToArray()))
|
||||
{
|
||||
|
|
@ -110,6 +117,9 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
CustomerAddressMappings = customerAddressMappingRepository;
|
||||
|
||||
GenericAttributes = genericAttributes;
|
||||
|
||||
StockQuantityHistories = stockQuantityHistories;
|
||||
StockQuantityHistoriesExt = stockQuantityHistoriesExt;
|
||||
}
|
||||
|
||||
public IQueryable<Customer> GetCustomersBySystemRoleName(string systemRoleName)
|
||||
|
|
@ -282,38 +292,42 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
if (shippingItem.IsMeasured)
|
||||
{
|
||||
var quantityInc = productIdChanged ? shippingItem.MeasuredQuantity : shippingItem.MeasuredQuantity - dbShippingItem.MeasuredQuantity;
|
||||
productDto!.StockQuantity += quantityInc;
|
||||
|
||||
if (!await UpdateProductDtoStockQuantityAsync(productDto, true))
|
||||
throw new Exception($"UpdateProductStockQuantity() == false; shippingItem! product.Id: {productDto.Id}");
|
||||
//productDto!.StockQuantity += quantityInc;
|
||||
//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)
|
||||
{
|
||||
await _fruitBankAttributeService.UpdateGenericAttributeAsync<Product, int>
|
||||
(productDto.Id, nameof(IIncomingQuantity.IncomingQuantity), incomingQuantity.Value - quantityInc);
|
||||
}
|
||||
|
||||
var weightToChange = 0d;
|
||||
if (productIsMeasurable)
|
||||
{
|
||||
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(productDto.Id,
|
||||
productIdChanged ? shippingItem.MeasuredNetWeight : shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight,
|
||||
shippingItem.IsMeasurable, true);
|
||||
weightToChange = productIdChanged ? shippingItem.MeasuredNetWeight : shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight;
|
||||
|
||||
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 (!dbShippingItem.ProductId.HasValue || (!productIdChanged && (shippingItem.IsMeasured || !dbShippingItem.IsMeasured))) return true;
|
||||
|
||||
productDto = await ProductDtos.GetByIdAsync(dbShippingItem.ProductId!.Value, true);
|
||||
|
||||
|
||||
if (productDto != null)
|
||||
{
|
||||
productIsMeasurable = productDto.IsMeasurable;
|
||||
productDto.StockQuantity -= dbShippingItem.MeasuredQuantity;
|
||||
|
||||
if (!await UpdateProductDtoStockQuantityAsync(productDto, true))
|
||||
throw new Exception($"UpdateProductStockQuantity() == false; dbShippingItem! product.Id: {productDto.Id}");
|
||||
//productDto.StockQuantity -= dbShippingItem.MeasuredQuantity;
|
||||
//if (!await UpdateProductDtoStockQuantityAsync(productDto, true))
|
||||
// throw new Exception($"UpdateProductStockQuantity() == false; dbShippingItem! product.Id: {productDto.Id}");
|
||||
|
||||
var incomingQuantity = productDto.GenericAttributes.GetValueOrNull<int>(nameof(IIncomingQuantity.IncomingQuantity));
|
||||
if (incomingQuantity != null)
|
||||
|
|
@ -322,10 +336,14 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
(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);
|
||||
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>(measuringValues, true);
|
||||
}
|
||||
|
||||
var measuringValues = new MeasuringAttributeValues(productDto.Id, -dbShippingItem.MeasuredNetWeight, dbShippingItem.IsMeasurable);
|
||||
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 //TODO: productIdUnchanged-et lekezelni! - J.
|
||||
|
|
@ -466,7 +484,7 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
orderDto.OrderStatus = OrderStatus.Complete;
|
||||
|
||||
await OrderDtos.UpdateAsync(orderDto);
|
||||
|
||||
|
||||
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Order, int>(orderDto.Id, nameof(IOrderDto.RevisorId), revisorId);
|
||||
|
||||
foreach (var orderItemDto in orderDto.OrderItemDtos)
|
||||
|
|
@ -487,8 +505,12 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<OrderItem, double>
|
||||
(orderItemDto.Id, nameof(IMeasuringNetWeight.NetWeight), orderItemDto.NetWeight);
|
||||
|
||||
var weightToChange = -(orderItemDto.NetWeight - prevNetWeightFromGa);
|
||||
|
||||
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));
|
||||
|
|
@ -552,7 +574,7 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
public async Task<OrderItemPallet?> AddOrderItemPalletAsync(OrderItemPallet orderItemPallet)
|
||||
{
|
||||
if (!await SetupOrderItemPalletMeauringValues(orderItemPallet)) return null;
|
||||
|
||||
|
||||
await OrderItemPallets.InsertAsync(orderItemPallet);
|
||||
return orderItemPallet;
|
||||
}
|
||||
|
|
@ -571,7 +593,7 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
public async Task<OrderItemPallet?> AddOrUpdateOrderItemPalletAsync(OrderItemPallet orderItemPallet)
|
||||
{
|
||||
if (orderItemPallet.Id <= 0) return await AddOrderItemPalletAsync(orderItemPallet);
|
||||
|
||||
|
||||
return await UpdateOrderItemPalletAsync(orderItemPallet);
|
||||
}
|
||||
|
||||
|
|
@ -590,7 +612,7 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
|
||||
if (orderItemDto == null || orderItemPallet.OrderItemId != orderItemDto.Id || //orderItemDto.IsOtherMeasuringInProgress(orderItemPallet.CreatorId) ||
|
||||
orderItemPallet.TrayQuantity > orderItemDto.Quantity || !orderItemPallet.IsValidSafeMeasuringValues()) return false;
|
||||
|
||||
|
||||
orderItemDto.OrderItemPallets.UpdateCollection(orderItemPallet, false);
|
||||
if (orderItemDto.TrayQuantity > orderItemDto.Quantity) return false;
|
||||
|
||||
|
|
@ -598,32 +620,89 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
return true;
|
||||
}
|
||||
|
||||
private async Task<bool> UpdateProductDtoStockQuantityAsync(int productDtoId, bool publishEvent)
|
||||
{
|
||||
var productDto = await ProductDtos.GetByIdAsync(productDtoId, false);
|
||||
if (productDto != null) return await UpdateProductDtoStockQuantityAsync(productDto, publishEvent);
|
||||
//private async Task<bool> UpdateProductDtoStockQuantityAsync(int productDtoId, bool publishEvent)
|
||||
//{
|
||||
// var productDto = await ProductDtos.GetByIdAsync(productDtoId, false);
|
||||
// if (productDto != null) return await UpdateProductDtoStockQuantityAsync(productDto, publishEvent);
|
||||
|
||||
Logger.Error($"product == null; id: {productDtoId}");
|
||||
return await Task.FromResult(false);
|
||||
// Logger.Error($"product == null; id: {productDtoId}");
|
||||
// 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.
|
||||
if (productDto.StockQuantity < 0)
|
||||
Logger.Error($"productDto.StockQuantity < 0; Id: {productDto.Id}; StockQuantity: {productDto.StockQuantity}");
|
||||
var latStockQuantityHistoryId = 0;
|
||||
|
||||
//TODO: !!!!!!!!!!!!!!!! - J.
|
||||
//await _productService.AdjustInventoryAsync(product, quantityInc, string.Empty, "");
|
||||
if (quantityToChange != 0) await _productService.AdjustInventoryAsync(product, quantityToChange, string.Empty, message);
|
||||
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);
|
||||
return await Task.FromResult(true);
|
||||
var historyEntry = new StockQuantityHistory
|
||||
{
|
||||
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}");
|
||||
//if (updatedRowsCount == 1) return await Task.FromResult(true);
|
||||
await StockQuantityHistories.InsertAsync(historyEntry);
|
||||
latStockQuantityHistoryId = historyEntry.Id;
|
||||
}
|
||||
else return;
|
||||
|
||||
//Logger.Error($"Product updatedRowsCount != 1; id: {product.Id}");
|
||||
//return await Task.FromResult(false);
|
||||
//await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<Product, double>(product.Id, nameof(IMeasuringNetWeight.NetWeight), netWeight);
|
||||
//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)
|
||||
|
|
|
|||
|
|
@ -117,7 +117,10 @@ public class FruitBankEventConsumer :
|
|||
var productDtoNetWeight = productDto?.GenericAttributes.GetValueOrNull<double>(nameof(IMeasuringNetWeight.NetWeight));
|
||||
|
||||
if (productDtoNetWeight == null || double.Round(productDtoNetWeight.Value, 1) != 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
|
||||
var tare = double.Round(CommonHelper.To<double>(form[nameof(ITare.Tare)].ToString()), 1);
|
||||
|
|
|
|||
Loading…
Reference in New Issue