773 lines
32 KiB
C#
773 lines
32 KiB
C#
using EgwCoreLib.Lux.Core.RestPayload;
|
|
using EgwCoreLib.Lux.Data.DbModel;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static EgwCoreLib.Lux.Core.Enums;
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
namespace EgwCoreLib.Lux.Data.Controllers
|
|
{
|
|
public class LuxController
|
|
{
|
|
// manca costruttore parametrico contoller...
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Elenco completo Customers da DB
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<CustomerModel> CustomersGetAll()
|
|
{
|
|
List<CustomerModel> dbResult = new List<CustomerModel>();
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetCustomer
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante CustomersGetAll{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco completo Dealers da DB
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<DealerModel> DealersGetAll()
|
|
{
|
|
List<DealerModel> dbResult = new List<DealerModel>();
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetDealer
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante DealersGetAll{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Eliminazione record item
|
|
/// </summary>
|
|
/// <param name="rec2del"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ItemDeleteAsync(ItemModel rec2del)
|
|
{
|
|
bool result = false;
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
var dbResult = await dbCtx
|
|
.DbSetItem
|
|
.Where(x => x.ItemID == rec2del.ItemID)
|
|
.FirstOrDefaultAsync();
|
|
if (dbResult != null)
|
|
{
|
|
dbCtx.DbSetItem.Remove(dbResult);
|
|
await dbCtx.SaveChangesAsync();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ItemDeleteAsync{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco completo Items
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<ItemModel> ItemGetAll()
|
|
{
|
|
List<ItemModel> dbResult = new List<ItemModel>();
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetItem
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ItemGetAll{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco item da ricerca filtro x gruppo/tipo
|
|
/// </summary>
|
|
/// <param name="SearchVal"></param>
|
|
/// <param name="CodGroup"></param>
|
|
/// <param name="ItemType"></param>
|
|
/// <returns></returns>
|
|
public List<ItemModel> ItemGetFilt(string CodGroup, ItemClassType ItemType)
|
|
{
|
|
List<ItemModel> dbResult = new List<ItemModel>();
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetItem
|
|
.Where(x => (string.IsNullOrEmpty(CodGroup) || x.CodGroup == CodGroup)
|
|
&& (ItemType == ItemClassType.ND || x.ItemType == ItemType))
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ItemGetFilt{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco item da ricerca completa
|
|
/// </summary>
|
|
/// <param name="CodGroup"></param>
|
|
/// <param name="ItemType"></param>
|
|
/// <param name="SearchVal"></param>
|
|
/// <returns></returns>
|
|
public List<ItemModel> ItemGetFilt(string CodGroup, ItemClassType ItemType, string SearchVal)
|
|
{
|
|
List<ItemModel> dbResult = new List<ItemModel>();
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetItem
|
|
.Where(x => (string.IsNullOrEmpty(CodGroup) || x.CodGroup == CodGroup)
|
|
&& (ItemType == ItemClassType.ND || x.ItemType == ItemType)
|
|
&& (string.IsNullOrEmpty(SearchVal) ||
|
|
x.Description.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase) ||
|
|
x.ExtItemCode.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase) ||
|
|
x.SupplCode.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase))
|
|
)
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ItemGetFilt{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco item da ricerca filtro x gruppo/tipo Async
|
|
/// </summary>
|
|
/// <param name="SearchVal"></param>
|
|
/// <param name="CodGroup"></param>
|
|
/// <param name="ItemType"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<ItemModel>> ItemGetFiltAsync(string CodGroup, ItemClassType ItemType)
|
|
{
|
|
List<ItemModel> dbResult = new List<ItemModel>();
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = await dbCtx
|
|
.DbSetItem
|
|
.Where(x => (string.IsNullOrEmpty(CodGroup) || x.CodGroup == CodGroup)
|
|
&& (ItemType == ItemClassType.ND || x.ItemType == ItemType))
|
|
.Include(g => g.ItemGroupNav)
|
|
.ToListAsync();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ItemGetFiltAsync{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco item da ricerca completa Async
|
|
/// </summary>
|
|
/// <param name="CodGroup"></param>
|
|
/// <param name="ItemType"></param>
|
|
/// <param name="SearchVal"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<ItemModel>> ItemGetFiltAsync(string CodGroup, ItemClassType ItemType, string SearchVal)
|
|
{
|
|
List<ItemModel> dbResult = new List<ItemModel>();
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = await dbCtx
|
|
.DbSetItem
|
|
.Where(x => (string.IsNullOrEmpty(CodGroup) || x.CodGroup == CodGroup)
|
|
&& (ItemType == ItemClassType.ND || x.ItemType == ItemType)
|
|
&& (string.IsNullOrEmpty(SearchVal) ||
|
|
x.Description.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase) ||
|
|
x.ExtItemCode.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase) ||
|
|
x.SupplCode.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase))
|
|
)
|
|
.ToListAsync();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ItemGetFiltAsync{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco item da ricerca
|
|
/// </summary>
|
|
/// <param name="SearchVal"></param>
|
|
/// <returns></returns>
|
|
public List<ItemModel> ItemGetSearch(string SearchVal)
|
|
{
|
|
List<ItemModel> dbResult = new List<ItemModel>();
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetItem
|
|
.Where(x => x.Description.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase) || x.ExtItemCode.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase) || x.SupplCode.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase))
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ItemGetSearch{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco item da ricerca async
|
|
/// </summary>
|
|
/// <param name="SearchVal"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<ItemModel>> ItemGetSearchAsync(string SearchVal)
|
|
{
|
|
List<ItemModel> dbResult = new List<ItemModel>();
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = await dbCtx
|
|
.DbSetItem
|
|
.Where(x => x.Description.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase) || x.ExtItemCode.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase) || x.SupplCode.Contains(SearchVal, StringComparison.InvariantCultureIgnoreCase))
|
|
.ToListAsync();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ItemGetSearchAsync{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco completo ItemGroup gestiti
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<ItemGroupModel> ItemGroupGetAll()
|
|
{
|
|
List<ItemGroupModel> dbResult = new List<ItemGroupModel>();
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetItemGroup
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ItemGroupGetAllAsync{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco completo ItemGroup gestiti async
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ItemGroupModel>> ItemGroupGetAllAsync()
|
|
{
|
|
List<ItemGroupModel> dbResult = new List<ItemGroupModel>();
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = await dbCtx
|
|
.DbSetItemGroup
|
|
.ToListAsync();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ItemGroupGetAllAsync{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
public bool ItemUpsert(ItemModel newRec)
|
|
{
|
|
bool answ = false;
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
var currRec = dbCtx
|
|
.DbSetItem
|
|
.Where(x => x.ItemID == newRec.ItemID)
|
|
.FirstOrDefault();
|
|
// se trovato --> aggiorno
|
|
if (currRec != null)
|
|
{
|
|
currRec.Description = newRec.Description;
|
|
currRec.SupplCode = newRec.SupplCode;
|
|
currRec.ItemCode = newRec.ItemCode;
|
|
currRec.Cost = newRec.Cost;
|
|
currRec.Description = newRec.Description;
|
|
currRec.IsService = newRec.IsService;
|
|
currRec.Margin = newRec.Margin;
|
|
dbCtx.Entry(currRec).State = EntityState.Modified;
|
|
}
|
|
// se mancasse --> aggiungo
|
|
else
|
|
{
|
|
dbCtx.DbSetItem.Add(newRec);
|
|
}
|
|
// salvo...
|
|
dbCtx.SaveChanges();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ItemGetSearch{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inserisce o aggiorna il record
|
|
/// </summary>
|
|
/// <param name="currRec"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ItemUpsertAsync(ItemModel currRec)
|
|
{
|
|
bool result = false;
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
var dbResult = await dbCtx
|
|
.DbSetItem
|
|
.Where(x => x.ItemID == currRec.ItemID)
|
|
.FirstOrDefaultAsync();
|
|
if (dbResult != null)
|
|
{
|
|
//dbCtx.DbSetItem.Remove(dbResult);
|
|
dbResult.CodGroup = currRec.CodGroup;
|
|
dbResult.ItemType = currRec.ItemType;
|
|
dbResult.IsService = currRec.IsService;
|
|
dbResult.ItemCode = currRec.ItemCode;
|
|
dbResult.ExtItemCode = currRec.ExtItemCode;
|
|
dbResult.Cost = currRec.Cost;
|
|
dbResult.Margin = currRec.Margin;
|
|
dbResult.Description = currRec.Description;
|
|
dbResult.QtyMin = currRec.QtyMin;
|
|
dbResult.QtyMax = currRec.QtyMax;
|
|
dbResult.UM = currRec.UM;
|
|
dbCtx.Entry(dbResult).State = EntityState.Modified;
|
|
}
|
|
else
|
|
{
|
|
dbCtx.DbSetItem.Add(currRec);
|
|
}
|
|
await dbCtx.SaveChangesAsync();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ItemUpsertAsync{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Upsert item ricevuti da BOM calcolata
|
|
/// </summary>
|
|
/// <param name="bomList"></param>
|
|
/// <returns></returns>
|
|
public bool ItemUpsertFromBom(List<BomItemDTO> bomList)
|
|
{
|
|
bool answ = false;
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
// prendo solo elementi a prezzo 0 da salvare sul DB
|
|
var item2save = bomList
|
|
.Where(x => x.Price == 0)
|
|
.ToList();
|
|
// ciclo x ogni elemento della BOM, cercando x gruppo e ExtItemCode
|
|
foreach (var item in item2save)
|
|
{
|
|
var currRec = dbCtx
|
|
.DbSetItem
|
|
.Where(x => x.CodGroup == item.ClassCode && x.ExtItemCode == item.ItemCode)
|
|
.FirstOrDefault();
|
|
|
|
// se nullo --> lo devo inserire!!!
|
|
if (currRec == null)
|
|
{
|
|
ItemModel newRec = new ItemModel()
|
|
{
|
|
CodGroup = item.ClassCode,
|
|
ItemType = Core.Enums.ItemClassType.Bom,
|
|
IsService = false,
|
|
// da calcolare meglio x gruppo
|
|
ItemCode = 0,
|
|
ExtItemCode = item.ItemCode,
|
|
SupplCode = "BOM ITEM",
|
|
Description = $"BOM | {item.ClassCode} | {item.ItemCode}",
|
|
Cost = 0,
|
|
Margin = 0,
|
|
QtyMin = 0,
|
|
QtyMax = 0,
|
|
UM = "#"
|
|
};
|
|
dbCtx.DbSetItem.Add(newRec);
|
|
}
|
|
}
|
|
|
|
// salvo...
|
|
dbCtx.SaveChanges();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante ItemUpsertFromBom{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco completo offerte da DB
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<OfferModel>> OfferGetAll()
|
|
{
|
|
List<OfferModel> dbResult = new List<OfferModel>();
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = await dbCtx
|
|
.DbSetOffer
|
|
.Include(c => c.CustomerNav)
|
|
.Include(d => d.DealerNav)
|
|
.Include(o => o.OfferRowNav)
|
|
.ToListAsync();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante OfferGetAll{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco righe offerta specificata
|
|
/// </summary>
|
|
/// <param name="OfferID"></param>
|
|
/// <returns></returns>
|
|
public List<OfferRowModel> OfferRowGetByOffer(int OfferID)
|
|
{
|
|
List<OfferRowModel> dbResult = new List<OfferRowModel>();
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
dbResult = dbCtx
|
|
.DbSetOfferRow
|
|
.Where(x => x.OfferID == OfferID)
|
|
.Include(s => s.SellingItemNav)
|
|
//.Include(d => d.DealerNav)
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante OfferRowGetByOffer{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua update dei costi di tutte le righe dell'offerta indicata
|
|
/// </summary>
|
|
/// <param name="OfferID"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> OffertUpdateCost(int OfferID)
|
|
{
|
|
bool answ = false;
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
// recupero righe offerta...
|
|
var offRowList = dbCtx
|
|
.DbSetOfferRow
|
|
.Where(x => x.OfferID == OfferID)
|
|
.ToList();
|
|
|
|
// recupero l'elenco degli itemGroup gestiti
|
|
var itemGroupList = dbCtx
|
|
.DbSetItemGroup
|
|
.ToList();
|
|
|
|
// recupero il subset item da BOM / BomAlt...
|
|
var bomGenList = dbCtx
|
|
.DbSetItem
|
|
.Where(x => (x.ItemType == Core.Enums.ItemClassType.Bom || x.ItemType == Core.Enums.ItemClassType.BomAlt))
|
|
.ToList();
|
|
|
|
// ciclo!
|
|
foreach (var currRec in offRowList)
|
|
{
|
|
// se contiene qualcosa x BOM...
|
|
if (!string.IsNullOrEmpty(currRec.ItemBOM) && currRec.ItemBOM.Length > 2)
|
|
{
|
|
// deserializzo
|
|
var bomList = JsonConvert.DeserializeObject<List<BomItemDTO>>(currRec.ItemBOM);
|
|
// se ho trovato elementi...
|
|
if (bomList != null)
|
|
{
|
|
// calcolo il NUOVO costo e lo aggiorno...
|
|
double totCost = 0;
|
|
int numGroupOk = 0;
|
|
int numItemOk = 0;
|
|
int numElems = bomList.Count;
|
|
// ciclo x ogni elemento della BOM, cercando x gruppo e ExtItemCode
|
|
foreach (var item in bomList)
|
|
{
|
|
// verifico item group esistente...
|
|
if (itemGroupList.Where(x => x.CodGroup == item.ClassCode).Count() > 0)
|
|
{
|
|
numGroupOk++;
|
|
}
|
|
// 2025.09.16: se il prezzo arriva dalla BOM calcolata uso quello...
|
|
if (item.Price > 0)
|
|
{
|
|
// conto l'item
|
|
numItemOk++;
|
|
item.PriceCalc = item.Price;
|
|
}
|
|
else
|
|
{
|
|
// cerco nella tab in memoria che ho precaricato il costo... cercando x dati di selezione + qtyRange
|
|
var recCost = bomGenList
|
|
.Where(x => x.CodGroup == item.ClassCode
|
|
&& x.ExtItemCode == item.ItemCode
|
|
&& item.Qty >= x.QtyMin
|
|
&& item.Qty < x.QtyMax)
|
|
.OrderByDescending(x => x.Cost)
|
|
.FirstOrDefault();
|
|
// se trovato valorizzo!
|
|
if (recCost != null)
|
|
{
|
|
numItemOk++;
|
|
item.ItemID = recCost.ItemID;
|
|
item.PriceCalc = recCost.Cost * (1 + recCost.Margin) * item.Qty;
|
|
}
|
|
else
|
|
{
|
|
item.ItemID = 0;
|
|
item.PriceCalc = 0;
|
|
}
|
|
// ...e aggiorno totale
|
|
totCost += item.PriceCalc;
|
|
}
|
|
}
|
|
// salvo BOM...
|
|
string itemBom = JsonConvert.SerializeObject(bomList);
|
|
currRec.ItemBOM = itemBom;
|
|
currRec.Cost = totCost;
|
|
currRec.BomOk = numElems == numGroupOk;
|
|
currRec.ItemOk = numElems == numItemOk;
|
|
dbCtx.Entry(currRec).State = EntityState.Modified;
|
|
}
|
|
}
|
|
}
|
|
|
|
// salvo TUTTI i cambiamenti...
|
|
await dbCtx.SaveChangesAsync();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante OffertUpdateCost{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Esegue upsert del record offerta data la BOM ricevuta
|
|
/// </summary>
|
|
/// <param name="uID"></param>
|
|
/// <param name="bomList"></param>
|
|
public bool OfferUpsertFromBom(string uID, List<BomItemDTO> bomList)
|
|
{
|
|
bool answ = false;
|
|
//using (DataLayerContext dbCtx = new DataLayerContext(configuration))
|
|
using (DataLayerContext dbCtx = new DataLayerContext())
|
|
{
|
|
try
|
|
{
|
|
var currRec = dbCtx
|
|
.DbSetOfferRow
|
|
.Where(x => x.OfferRowUID == uID)
|
|
.FirstOrDefault();
|
|
// se trovato --> salvo BOM e calcolo costi
|
|
if (currRec != null)
|
|
{
|
|
// recupero l'elenco degli itemGroup gestiti
|
|
var itemGroupList = dbCtx
|
|
.DbSetItemGroup
|
|
.ToList();
|
|
// recupero il subset item da BOM...
|
|
var bomGenList = dbCtx
|
|
.DbSetItem
|
|
.Where(x => x.ItemType == Core.Enums.ItemClassType.Bom)
|
|
.ToList();
|
|
|
|
// calcolo il NUOVO costo e lo aggiorno...
|
|
double totCost = 0;
|
|
int numGroupOk = 0;
|
|
int numItemOk = 0;
|
|
int numElems = bomList.Count;
|
|
// ciclo x ogni elemento della BOM, cercando x gruppo e ExtItemCode
|
|
foreach (var item in bomList)
|
|
{
|
|
// verifico item group esistente...
|
|
if (itemGroupList.Where(x => x.CodGroup == item.ClassCode).Count() > 0)
|
|
{
|
|
numGroupOk++;
|
|
}
|
|
|
|
// 2025.09.16: se il prezzo arriva dalla BOM calcolata uso quello...
|
|
if (item.Price > 0)
|
|
{
|
|
// resetto ItemID ma NON il prezzo
|
|
item.ItemID = 0;
|
|
// conto l'item
|
|
numItemOk++;
|
|
item.PriceCalc = item.Price;
|
|
}
|
|
else
|
|
{
|
|
// cerco nella tab in memoria che ho precaricato il costo... cercando x dati di selezione + qtyRange
|
|
var recCost = bomGenList
|
|
.Where(x => x.CodGroup == item.ClassCode
|
|
&& x.ExtItemCode == item.ItemCode
|
|
&& item.Qty >= x.QtyMin
|
|
&& item.Qty < x.QtyMax)
|
|
.OrderByDescending(x => x.Cost)
|
|
.FirstOrDefault();
|
|
// se trovato valorizzo!
|
|
if (recCost != null)
|
|
{
|
|
numItemOk++;
|
|
item.ItemID = recCost.ItemID;
|
|
item.PriceCalc = recCost.Cost * (1 + recCost.Margin) * item.Qty;
|
|
}
|
|
else
|
|
{
|
|
item.ItemID = 0;
|
|
item.PriceCalc = 0;
|
|
}
|
|
// ...e aggiorno totale
|
|
totCost += item.PriceCalc;
|
|
}
|
|
|
|
}
|
|
// salvo BOM...
|
|
string itemBom = JsonConvert.SerializeObject(bomList);
|
|
currRec.ItemBOM = itemBom;
|
|
currRec.Cost = totCost;
|
|
currRec.BomOk = numElems == numGroupOk;
|
|
currRec.ItemOk = numElems == numItemOk;
|
|
dbCtx.Entry(currRec).State = EntityState.Modified;
|
|
}
|
|
|
|
// salvo...
|
|
dbCtx.SaveChanges();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione durante OfferUpsertFromBom{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration;
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |