From 57966ac9de72543014451b7ccd75539296ccaa51 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Mon, 1 Sep 2025 11:08:47 -0500 Subject: [PATCH] refactor(mm query): changes to the query to see pkg and materials properly and not duplicates --- .../sqlServer/querys/ocp/mainMaterial.ts | 104 +++++++++--------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/server/services/sqlServer/querys/ocp/mainMaterial.ts b/server/services/sqlServer/querys/ocp/mainMaterial.ts index 42c31e1..6f026d1 100644 --- a/server/services/sqlServer/querys/ocp/mainMaterial.ts +++ b/server/services/sqlServer/querys/ocp/mainMaterial.ts @@ -23,69 +23,59 @@ export const mmQuery = ` use [test1_AlplaPROD2.0_Read] +declare @lot as NVARCHAR(max) = [lotNumber] /* -checks all needed material including pkg - -we only want to monitor the manual materials and the mm materail to make sure they are good. - -for auto consume materails this will be compared with another query. - +Material demands for this lot */ -SELECT lot.ProductionLotHumanReadableId -,MaterialHumanReadableId -,MaterialDescription - --IsMainMaterial, - ,case when SourcingState in (1, 2) then 1 +select +MaterialHumanReadableId +,MaterialDescription +-- check box is staged so we could book in even if we used preprint. +,case when SourcingState in (1, 2) then 1 when x.ProvidedAmount > 0 then 1 when x.EffectiveConsumption > 0 then 1 else 0 end as Staged - ,x.ProvidedAmount as Provided - ,x.EffectiveConsumption as consumption - ,x.TotalDemand as totalDemand , - case when cp.Pieces = 1 then (lot.TotalProducedLoadingUnits+1) * p.LoadingUnitPieces else - (a.Weight *((case when cp.Percentage is null then 80.25 else cp.Percentage end) / 100) * ((lot.TotalProducedLoadingUnits+1) * p.LoadingUnitPieces)) / 1000 end totalNeeded - ,l.qty as invForAutoConsume - /* remaining needed to complete the lot */ - --,x.TotalDemand - x.EffectiveConsumption as remainingNeeded - /* do we have enough staged or scanned to the lot? */ - ,case when (case when x.ProvidedAmount <> 0 - then x.ProvidedAmount else x.EffectiveConsumption end) > - (case when cp.Pieces = 1 then (lot.TotalProducedLoadingUnits+1) * p.LoadingUnitPieces else - (a.Weight *((case when cp.Percentage is null then 80.25 else cp.Percentage end) / 100) * ((lot.TotalProducedLoadingUnits+1) * p.LoadingUnitPieces)) / 1000 end) - then 'good' - else 'no' end as noMaterialShortage - -- pkg check - ,case when pkg.QuantityPosition is null then null else - (case when l.qty > (lot.TotalProducedLoadingUnits+1) * pkg.QuantityPosition then 'pkgGood' else 'noPkg' end) end as noPKGShortage - - ,case when cp.Percentage is null then - case when l.qty > ((lot.TotalProducedLoadingUnits +1) * pkg.QuantityPosition) then 'autoConsumeOk' else 'autoConsumeNOK' end else - (case when l.qty > (a.Weight *((case when cp.Percentage is null then 80.25 else cp.Percentage end) / 100) * ((lot.TotalProducedLoadingUnits+1) * p.LoadingUnitPieces)) / 1000 and IsManualProcess = 0 - then 'autoConsumeOk' else 'autoConsumeNOK' end) end as autoConsumeCheck ,x.IsManualProcess as isManual ,IsMainMaterial + -- lot stuff ,lot.TotalPlannedLoadingUnits ,lot.TotalProducedLoadingUnits ,lot.TotalPlannedLoadingUnits - lot.TotalProducedLoadingUnits as remainingPallets - ,case when cp.Percentage is null then 80.25 else cp.Percentage end as Percentage -- this is to over come the alternate mm put in planning - ,case when cp.Pieces is not null then cp.Pieces else pkg.QuantityPosition end as peices -FROM [issueMaterial].[MaterialDemand] x (nolock) + ,x.ProvidedAmount as Provided -- this is what has been provided most of the time this is due to a silo attachment + ,x.EffectiveConsumption as consumption -- this is how much was consummed via cmd 112 + ,x.TotalDemand as totalDemand -- the total demand needed to finish the lot out + ,case when pkg.QuantityPosition >= 0.001 then ((lot.TotalProducedLoadingUnits+1) * pkg.QuantityPosition) else + (a.Weight *(cp.Percentage / 100) * ((lot.TotalProducedLoadingUnits+1) * p.LoadingUnitPieces)) / 1000 end totalNeeded + + ,case when IsMainMaterial = 1 then case when (case when x.ProvidedAmount <> 0 + then x.ProvidedAmount else x.EffectiveConsumption end) > + (case when pkg.QuantityPosition >= 0.001 then (lot.TotalProducedLoadingUnits+1) * pkg.QuantityPosition else + (a.Weight *(cp.Percentage / 100) * ((lot.TotalProducedLoadingUnits+1) * p.LoadingUnitPieces)) / 1000 end) + then 'mmGood' + else 'noMM' end else null end as noMaterialShortage + + -- pkg check + ,case when pkg.QuantityPosition is null then null else + (case when l.qty > ((lot.TotalProducedLoadingUnits+1) * pkg.QuantityPosition) then 'pkgGood' else 'noPkg' end) end as noPKGShortage + -- autoconsume + ,case when cp.Percentage is null then + case when l.qty > ((lot.TotalProducedLoadingUnits +1) * pkg.QuantityPosition) then 'autoConsumeOk' else 'autoConsumeNOK' end else + (case when l.qty > (a.Weight *(cp.Percentage / 100) * ((lot.TotalProducedLoadingUnits+1) * p.LoadingUnitPieces)) / 1000 and IsManualProcess = 0 + then 'autoConsumeOk' else 'autoConsumeNOK' end) end as autoConsumeCheck + -- stock amounts + ,l.qty as invForAutoConsume + ,x.IsManualProcess as isManual + ,IsMainMaterial + ,case when cp.Percentage is null then 0 else cp.Percentage end as Percentage + ,pkg.QuantityPosition +from [issueMaterial].[MaterialDemand] x (nolock) + +/* production lot info */ left join [productionControlling].[ProducedLot] as lot on lot.Id = x.ProductionLotId -/* av data */ -left join - [masterData].[Article] as a on - a.id = lot.ArticleId - -/* compound*/ -left join -[masterData].[CompoundPosition] as cp on -cp.CompoundId = a.CompoundId -and cp.ArticleId = x.MaterialId - /* packagaing */ left join [masterData].[PackagingInstructionPosition] as pkg on @@ -97,6 +87,17 @@ left join [masterData].[PackagingInstruction] as p on p.id = lot.PackagingId +/* av data */ +left join + [masterData].[Article] as a on + a.id = lot.ArticleId + +/* compound*/ +left join +[masterData].[CompoundPosition] as cp on +cp.CompoundId = a.CompoundId +and cp.ArticleId = x.MaterialId + /* current stock info for auto consumption*/ left join (select @@ -104,14 +105,15 @@ IdArtikelVarianten ,ArtikelVariantenBez ,sum(VerfuegbareMengeSum) as qty -from AlplaPROD_test1.dbo.V_LagerPositionenBarcodes as i (nolock) +from AlplaPROD_usweb1.dbo.V_LagerPositionenBarcodes as i (nolock) left join -AlplaPROD_test1.dbo.V_LagerAbteilungen as l (nolock) on +AlplaPROD_usweb1.dbo.V_LagerAbteilungen as l (nolock) on l.IdLagerAbteilung = i.IdLagerAbteilung where autoverbrauch = 1 and aktiv = 1 group by IdArtikelVarianten,ArtikelVariantenBez ) as l on l.IdArtikelVarianten = MaterialHumanReadableId -where lot.ProductionLotHumanReadableId = [lotNumber] + +where lot.ProductionLotHumanReadableId = @lot `;