JSFiddle - React, Tailwind, and code Playground

by borisjavier

HTML

<div id="so"></div>

JavaScript

const data = {
  "1&1&1692392577358Ol": {
    "active": "si",
    "empresa": "1",
    "fecha": "2023-09-08",
    "planta": "Sangão",
    "preco": "2",
    "product": "Oleo",
    "qty": "1000",
    "responsible": "[email protected]",
    "um": "Lt"
  }
}

const a = ["Sangão", "Pirula", "Perenceja"];


function meetsConditions(key, element, fechaCorteStart, fechaCorteEnd) {
  const fechaTimestamp = Math.floor(parseInt(key.match(/&(\d+)[A-Za-z]{2}$/)[1]));
  const isActive = element.active === "si";
  const isWithinCortePeriod = fechaTimestamp >= fechaCorteStart && fechaTimestamp <= fechaCorteEnd;
  const isMatchingPlant = a.includes(element.planta);
  return isActive && isWithinCortePeriod && isMatchingPlant;
}

const today = new Date();
const currentTimestamp = Math.floor(today.getTime() / 1000); // Convert to seconds

const corteDates = [
  { startDate: currentTimestamp - 2419200, endDate: currentTimestamp - 1814400 }, // 28 days ago - 21 days ago
  { startDate: currentTimestamp - 1814400, endDate: currentTimestamp - 1209600 }, // 20 days ago - 14 days ago
  { startDate: currentTimestamp - 1209600, endDate: currentTimestamp - 604800 },  // 13 days ago - 7 days ago
  { startDate: currentTimestamp - 604800, endDate: currentTimestamp },            // 6 days ago - today
];

console.log("Corte Dates:", corteDates);

const plantDataArrays = {};

a.forEach(planta => {
  plantDataArrays[planta] = []; // Initialize an empty array for each plant

  Object.entries(data).forEach(([key, value]) => {
    const fechaTimestamp = Math.floor(parseInt(key.match(/&(\d+)[A-Za-z]{2}$/)[1]));

    corteDates.forEach(corteDate => {
      const comeca = parseInt(corteDate.startDate) * 1000;
      const termina = parseInt(corteDate.endDate) * 1000;
      
      console.log(`Planta: ${value.planta}, Fecha Timestamp: ${fechaTimestamp}, Start Date: ${comeca}, End Date: ${termina}`);
      const isWithinDateRange = fechaTimestamp >= comeca && fechaTimestamp <= termina;
      const...