Ignore unresolved supply data in planning
Some checks failed
Build and Deploy / build-and-deploy (push) Failing after 49s

This commit is contained in:
Cauê Faleiros
2026-08-04 13:51:25 -03:00
parent 30cdcd9b0e
commit c780b3134b
3 changed files with 36 additions and 49 deletions

View File

@@ -17,6 +17,11 @@ const normalizeNonNegativeNumber = (value) => {
return Number.isFinite(number) && number >= 0 ? number : null;
};
// Tiny may temporarily report a negative balance while its own adjustments are
// being reconciled. Negative inventory must not inflate the material demand:
// for planning, it represents no available stock, never an extra shortage.
const getAvailablePlanningStock = (value) => Math.max(0, Number(value) || 0);
const normalizeKey = (value) => normalizeText(value)
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
@@ -176,15 +181,16 @@ const buildPurchaseNeeds = (plans, lots, receipts) => {
return Array.from(needsByMaterial.values())
.map(need => {
const purchaseKg = Math.max(need.plannedKg - need.stockKg - need.pendingKg, 0);
const stockKg = getAvailablePlanningStock(need.stockKg);
const purchaseKg = Math.max(need.plannedKg - stockKg - need.pendingKg, 0);
let status = 'ok';
if (purchaseKg > 0 && (need.priority === 'Crítico' || need.stockKg === 0)) status = 'critical';
if (purchaseKg > 0 && (need.priority === 'Crítico' || stockKg === 0)) status = 'critical';
else if (purchaseKg > 0) status = 'attention';
return {
material: need.material,
plannedKg: need.plannedKg,
stockKg: need.stockKg,
stockKg,
pendingKg: need.pendingKg,
purchaseKg,
priority: need.priority,
@@ -543,15 +549,16 @@ const buildProjectPurchaseNeeds = async (lots, receipts) => {
});
return Array.from(needsByMaterial.values()).map(need => {
const purchaseKg = Math.max(need.plannedKg - need.stockKg - need.pendingKg, 0);
const stockKg = getAvailablePlanningStock(need.stockKg);
const purchaseKg = Math.max(need.plannedKg - stockKg - need.pendingKg, 0);
let status = 'ok';
if (purchaseKg > 0 && (need.priority === 'Crítico' || need.stockKg === 0)) status = 'critical';
if (purchaseKg > 0 && (need.priority === 'Crítico' || stockKg === 0)) status = 'critical';
else if (purchaseKg > 0) status = 'attention';
return {
material: need.material,
plannedKg: need.plannedKg,
stockKg: need.stockKg,
stockKg,
pendingKg: need.pendingKg,
purchaseKg,
priority: need.priority,
@@ -607,12 +614,14 @@ const mergePurchaseNeeds = (manualNeeds, projectNeeds) => {
return Array.from(mergedByKey.values())
.map(need => {
const purchaseKg = Math.max(need.plannedKg - need.stockKg - need.pendingKg, 0);
const stockKg = getAvailablePlanningStock(need.stockKg);
const purchaseKg = Math.max(need.plannedKg - stockKg - need.pendingKg, 0);
return {
...need,
stockKg,
purchaseKg,
status: purchaseKg > 0
? (need.priority === 'Crítico' || need.stockKg === 0 ? 'critical' : 'attention')
? (need.priority === 'Crítico' || stockKg === 0 ? 'critical' : 'attention')
: 'ok',
suppliers: Array.from(need.suppliers),
colors: Array.from(need.colors)