first commit
This commit is contained in:
126
app/src/modules/simulation/analysis/ROI/roiData.tsx
Normal file
126
app/src/modules/simulation/analysis/ROI/roiData.tsx
Normal file
@@ -0,0 +1,126 @@
|
||||
import React, { useEffect, useState } from 'react'
|
||||
import { useInputValues, useProductionCapacityData, useROISummaryData } from '../../../../store/builder/store';
|
||||
import { usePlayButtonStore } from '../../../../store/usePlayButtonStore';
|
||||
import { useProductContext } from '../../products/productContext';
|
||||
|
||||
export default function ROIData() {
|
||||
const { selectedProductStore } = useProductContext();
|
||||
const { inputValues } = useInputValues();
|
||||
const { productionCapacityData } = useProductionCapacityData()
|
||||
const { selectedProduct } = selectedProductStore();
|
||||
const { isPlaying } = usePlayButtonStore();
|
||||
const { setRoiSummaryData } = useROISummaryData();
|
||||
useEffect(() => {
|
||||
if (!isPlaying) {
|
||||
setRoiSummaryData({
|
||||
productName: "",
|
||||
roiPercentage: 0,
|
||||
paybackPeriod: 0,
|
||||
totalCost: 0,
|
||||
revenueGenerated: 0,
|
||||
netProfit: 0,
|
||||
netLoss: 0,
|
||||
})
|
||||
return;
|
||||
}
|
||||
|
||||
if (inputValues === undefined) return;
|
||||
|
||||
const electricityCost = parseFloat(inputValues["Electricity cost"]);
|
||||
const fixedCost = parseFloat(inputValues["Fixed costs"]);
|
||||
const laborCost = parseFloat(inputValues["Labor Cost"]);
|
||||
const maintenanceCost = parseFloat(inputValues["Maintenance cost"]); // Remove space typ
|
||||
const materialCost = parseFloat(inputValues["Material cost"]);
|
||||
const productionPeriod = parseFloat(inputValues["Production period"]);
|
||||
const salvageValue = parseFloat(inputValues["Salvage value"]);
|
||||
const sellingPrice = parseFloat(inputValues["Selling price"]);
|
||||
const initialInvestment = parseFloat(inputValues["Initial Investment"]);
|
||||
const shiftLength = parseFloat(inputValues["Shift length"]);
|
||||
const shiftsPerDay = parseFloat(inputValues["Shifts / day"]);
|
||||
const workingDaysPerYear = parseFloat(inputValues["Working days / year"]);
|
||||
|
||||
if (!isNaN(electricityCost) && !isNaN(fixedCost) && !isNaN(laborCost) && !isNaN(maintenanceCost) &&
|
||||
!isNaN(materialCost) && !isNaN(productionPeriod) && !isNaN(salvageValue) && !isNaN(sellingPrice) &&
|
||||
!isNaN(shiftLength) && !isNaN(shiftsPerDay) && !isNaN(workingDaysPerYear) && productionCapacityData > 0) {
|
||||
|
||||
|
||||
|
||||
|
||||
const totalHoursPerYear = shiftLength * shiftsPerDay * workingDaysPerYear;
|
||||
|
||||
// Total good units produced per year
|
||||
const annualProductionUnits = productionCapacityData * totalHoursPerYear;
|
||||
|
||||
// Revenue for a year
|
||||
const annualRevenue = annualProductionUnits * sellingPrice;
|
||||
|
||||
// Costs
|
||||
const totalMaterialCost = annualProductionUnits * materialCost;
|
||||
const totalLaborCost = laborCost * totalHoursPerYear;
|
||||
const totalEnergyCost = electricityCost * totalHoursPerYear;
|
||||
const totalMaintenanceCost = maintenanceCost + fixedCost;
|
||||
|
||||
const totalAnnualCost = totalMaterialCost + totalLaborCost + totalEnergyCost + totalMaintenanceCost;
|
||||
|
||||
// Annual Profit
|
||||
const annualProfit = annualRevenue - totalAnnualCost;
|
||||
console.log('annualProfit: ', annualProfit);
|
||||
|
||||
// Net Profit over production period
|
||||
const netProfit = annualProfit * productionPeriod;
|
||||
|
||||
// ROI
|
||||
const roiPercentage = ((netProfit + salvageValue - initialInvestment) / initialInvestment) * 100;
|
||||
|
||||
// Payback Period
|
||||
const paybackPeriod = initialInvestment / (annualProfit || 1); // Avoid division by 0
|
||||
console.log('paybackPeriod: ', paybackPeriod);
|
||||
|
||||
// console.log("--- ROI Breakdown ---");
|
||||
// console.log("Annual Production Units:", annualProductionUnits.toFixed(2));
|
||||
// console.log("Annual Revenue:", annualRevenue.toFixed(2));
|
||||
// console.log("Total Annual Cost:", totalAnnualCost.toFixed(2));
|
||||
// console.log("Annual Profit:", annualProfit.toFixed(2));
|
||||
// console.log("Net Profit:", netProfit.toFixed(2));
|
||||
// console.log("ROI %:", roiPercentage.toFixed(2));
|
||||
// console.log("Payback Period (years):", paybackPeriod.toFixed(2));
|
||||
|
||||
setRoiSummaryData({
|
||||
productName: selectedProduct.productName,
|
||||
roiPercentage: parseFloat((roiPercentage / 100).toFixed(2)), // normalized to 0.x format
|
||||
paybackPeriod: parseFloat(paybackPeriod.toFixed(2)),
|
||||
totalCost: parseFloat(totalAnnualCost.toFixed(2)),
|
||||
revenueGenerated: parseFloat(annualRevenue.toFixed(2)),
|
||||
netProfit: netProfit > 0 ? parseFloat(netProfit.toFixed(2)) : 0,
|
||||
netLoss: netProfit < 0 ? -netProfit : 0
|
||||
});
|
||||
|
||||
const productCount = 1000;
|
||||
|
||||
// Cost per unit (based on full annual cost)
|
||||
const costPerUnit = totalAnnualCost / annualProductionUnits;
|
||||
|
||||
const costForTargetUnits = productCount * costPerUnit;
|
||||
const revenueForTargetUnits = productCount * sellingPrice;
|
||||
const profitForTargetUnits = revenueForTargetUnits - costForTargetUnits;
|
||||
|
||||
const netProfitForTarget = profitForTargetUnits > 0 ? profitForTargetUnits : 0;
|
||||
const netLossForTarget = profitForTargetUnits < 0 ? -profitForTargetUnits : 0;
|
||||
|
||||
// console.log("--- Fixed Product Count (" + productCount + ") ---");
|
||||
// console.log("Cost per Unit:", costPerUnit.toFixed(2));
|
||||
// console.log("Total Cost for " + productCount + " Units:", costForTargetUnits.toFixed(2));
|
||||
// console.log("Revenue for " + productCount + " Units:", revenueForTargetUnits.toFixed(2));
|
||||
// console.log("Profit:", netProfitForTarget.toFixed(2));
|
||||
// console.log("Loss:", netLossForTarget.toFixed(2));
|
||||
|
||||
}
|
||||
|
||||
}, [inputValues, productionCapacityData]);
|
||||
|
||||
return (
|
||||
<></>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import React, { useEffect } from 'react'
|
||||
import { useInputValues, useProductionCapacityData, useThroughPutData } from '../../../../store/builder/store'
|
||||
import { usePlayButtonStore } from '../../../../store/usePlayButtonStore';
|
||||
|
||||
export default function ProductionCapacityData() {
|
||||
const { throughputData } = useThroughPutData()
|
||||
const { productionCapacityData, setProductionCapacityData } = useProductionCapacityData()
|
||||
const { inputValues } = useInputValues();
|
||||
const { isPlaying } = usePlayButtonStore();
|
||||
|
||||
useEffect(() => {
|
||||
if (!isPlaying) {
|
||||
setProductionCapacityData(0);
|
||||
return;
|
||||
}
|
||||
if (!inputValues || throughputData === undefined) return;
|
||||
|
||||
const shiftLength = parseFloat(inputValues["Shift length"]);
|
||||
const shiftsPerDay = parseFloat(inputValues["Shifts / day"]);
|
||||
const workingDaysPerYear = parseFloat(inputValues["Working days / year"]);
|
||||
const yieldRate = parseFloat(inputValues["Yield rate"]);
|
||||
|
||||
if (!isNaN(shiftLength) && !isNaN(shiftsPerDay) && !isNaN(workingDaysPerYear) &&
|
||||
!isNaN(yieldRate) && throughputData >= 0) {
|
||||
// Total units produced per day before yield
|
||||
const dailyProduction = throughputData * shiftLength * shiftsPerDay;
|
||||
|
||||
|
||||
// Units after applying yield rate
|
||||
const goodUnitsPerDay = dailyProduction * (yieldRate / 100);
|
||||
|
||||
|
||||
// Annual output
|
||||
const annualProduction = goodUnitsPerDay * workingDaysPerYear;
|
||||
|
||||
|
||||
// Final production capacity per hour (after yield)
|
||||
const productionPerHour = throughputData * (yieldRate / 100);
|
||||
|
||||
|
||||
// Set the final capacity (units/hour)
|
||||
setProductionCapacityData(Number(productionPerHour.toFixed(2)));
|
||||
}
|
||||
}, [throughputData, inputValues]);
|
||||
|
||||
return (
|
||||
<></>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
25
app/src/modules/simulation/analysis/simulationAnalysis.tsx
Normal file
25
app/src/modules/simulation/analysis/simulationAnalysis.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import React, { useEffect } from 'react'
|
||||
import { usePlayButtonStore } from '../../../store/usePlayButtonStore'
|
||||
import ProductionCapacityData from './productionCapacity/productionCapacityData'
|
||||
import ThroughPutData from './throughPut/throughPutData'
|
||||
import ROIData from './ROI/roiData'
|
||||
|
||||
function SimulationAnalysis() {
|
||||
const { isPlaying } = usePlayButtonStore()
|
||||
// useEffect(()=>{
|
||||
// if (isPlaying) {
|
||||
//
|
||||
// } else {
|
||||
//
|
||||
// }
|
||||
// },[isPlaying])
|
||||
return (
|
||||
<>
|
||||
<ThroughPutData />
|
||||
<ProductionCapacityData />
|
||||
<ROIData />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default SimulationAnalysis
|
||||
@@ -0,0 +1,208 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useProductStore } from '../../../../store/simulation/useProductStore';
|
||||
import { determineExecutionMachineSequences } from '../../simulator/functions/determineExecutionMachineSequences';
|
||||
import { useMachineCount, useMachineUptime, useMaterialCycle, useProcessBar, useThroughPutData } from '../../../../store/builder/store';
|
||||
import { usePlayButtonStore } from '../../../../store/usePlayButtonStore';
|
||||
import { useSceneContext } from '../../../scene/sceneContext';
|
||||
import { useProductContext } from '../../products/productContext';
|
||||
|
||||
export default function ThroughPutData() {
|
||||
const { materialStore, armBotStore, machineStore, conveyorStore, vehicleStore, storageUnitStore } = useSceneContext();
|
||||
const { selectedProductStore } = useProductContext();
|
||||
const { selectedProduct } = selectedProductStore();
|
||||
const { products, getProductById } = useProductStore();
|
||||
const { armBots } = armBotStore();
|
||||
const { vehicles } = vehicleStore();
|
||||
const { machines } = machineStore();
|
||||
const { conveyors } = conveyorStore();
|
||||
const { storageUnits } = storageUnitStore();
|
||||
const { materialHistory, materials } = materialStore();
|
||||
const { machineCount, setMachineCount } = useMachineCount();
|
||||
const { machineActiveTime, setMachineActiveTime } = useMachineUptime();
|
||||
const { materialCycleTime, setMaterialCycleTime } = useMaterialCycle();
|
||||
const { setProcessBar } = useProcessBar();
|
||||
const { setThroughputData } = useThroughPutData()
|
||||
const { isPlaying } = usePlayButtonStore();
|
||||
|
||||
// Setting machine count
|
||||
let totalItems = 0;
|
||||
let totalActiveTime = 0;
|
||||
useEffect(() => {
|
||||
if (!isPlaying) {
|
||||
totalActiveTime = 0;
|
||||
totalItems = 0;
|
||||
setMachineCount(0);
|
||||
setMachineActiveTime(0);
|
||||
setMaterialCycleTime(0);
|
||||
setProcessBar([]);
|
||||
setThroughputData(0);
|
||||
return;
|
||||
} else {
|
||||
let process: any = [];
|
||||
const fetchProductSequenceData = async () => {
|
||||
const productData = getProductById(selectedProduct.productUuid);
|
||||
if (productData) {
|
||||
const productSequenceData = await determineExecutionMachineSequences([productData])
|
||||
if (productSequenceData?.length > 0) {
|
||||
productSequenceData.forEach((sequence) => {
|
||||
sequence.forEach((item) => {
|
||||
if (item.type === "roboticArm") {
|
||||
armBots.filter(arm => arm.modelUuid === item.modelUuid)
|
||||
.forEach(arm => {
|
||||
if (arm.activeTime > 0) {
|
||||
process.push({ modelid: arm.modelUuid, modelName: arm.modelName, activeTime: arm?.activeTime })
|
||||
totalActiveTime += arm.activeTime;
|
||||
}
|
||||
});
|
||||
} else if (item.type === "vehicle") {
|
||||
vehicles.filter(vehicle => vehicle.modelUuid === item.modelUuid)
|
||||
.forEach(vehicle => {
|
||||
if (vehicle.activeTime > 0) {
|
||||
process.push({ modelid: vehicle.modelUuid, modelName: vehicle.modelName, activeTime: vehicle?.activeTime })
|
||||
|
||||
totalActiveTime += vehicle.activeTime;
|
||||
}
|
||||
});
|
||||
} else if (item.type === "machine") {
|
||||
machines.filter(machine => machine.modelUuid === item.modelUuid)
|
||||
.forEach(machine => {
|
||||
if (machine.activeTime > 0) {
|
||||
process.push({ modelid: machine.modelUuid, modelName: machine.modelName, activeTime: machine?.activeTime })
|
||||
totalActiveTime += machine.activeTime;
|
||||
}
|
||||
});
|
||||
} else if (item.type === "transfer") {
|
||||
conveyors.filter(conveyor => conveyor.modelUuid === item.modelUuid)
|
||||
.forEach(conveyor => {
|
||||
if (conveyor.activeTime > 0) {
|
||||
// totalActiveTime += conveyor.activeTime;
|
||||
}
|
||||
});
|
||||
} else if (item.type === "storageUnit") {
|
||||
storageUnits.filter(storage => storage.modelUuid === item.modelUuid)
|
||||
.forEach(storage => {
|
||||
if (storage.activeTime > 0) {
|
||||
// totalActiveTime += storage.activeTime;
|
||||
//
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
totalItems += sequence.length;
|
||||
});
|
||||
|
||||
|
||||
setMachineCount(totalItems);
|
||||
setMachineActiveTime(totalActiveTime);
|
||||
let arr = process.map((item: any) => ({
|
||||
name: item.modelName,
|
||||
completed: Math.round((item.activeTime / totalActiveTime) * 100)
|
||||
}));
|
||||
setProcessBar(arr);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fetchProductSequenceData();
|
||||
}
|
||||
// if (materialCycleTime <= 0) return
|
||||
}, [products, selectedProduct, getProductById, setMachineCount, materialCycleTime, armBots, vehicles, machines]);
|
||||
|
||||
useEffect(() => {
|
||||
async function getMachineActive() {
|
||||
const productData = getProductById(selectedProduct.productUuid);
|
||||
let anyArmActive;
|
||||
let anyVehicleActive;
|
||||
let anyMachineActive;
|
||||
|
||||
if (productData) {
|
||||
const productSequenceData = await determineExecutionMachineSequences([productData]);
|
||||
if (productSequenceData?.length > 0) {
|
||||
productSequenceData.forEach(sequence => {
|
||||
sequence.forEach(item => {
|
||||
if (item.type === "roboticArm") {
|
||||
armBots
|
||||
.filter(arm => arm.modelUuid === item.modelUuid)
|
||||
.forEach(arm => {
|
||||
if (arm.isActive) {
|
||||
anyArmActive = true;
|
||||
} else {
|
||||
anyArmActive = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
if (item.type === "vehicle") {
|
||||
vehicles
|
||||
.filter(vehicle => vehicle.modelUuid === item.modelUuid)
|
||||
.forEach(vehicle => {
|
||||
if (vehicle.isActive) {
|
||||
anyVehicleActive = true;
|
||||
} else {
|
||||
anyVehicleActive = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
if (item.type === "machine") {
|
||||
machines
|
||||
.filter(machine => machine.modelUuid === item.modelUuid)
|
||||
.forEach(machine => {
|
||||
if (machine.isActive) {
|
||||
anyMachineActive = true;
|
||||
} else {
|
||||
anyMachineActive = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const allInactive = !anyArmActive && !anyVehicleActive && !anyMachineActive;
|
||||
if (allInactive && materials.length === 0 && materialHistory.length > 0) {
|
||||
|
||||
let totalCycleTimeSum = 0;
|
||||
let cycleCount = 0;
|
||||
|
||||
materialHistory.forEach((material) => {
|
||||
const start = material.material.startTime ?? 0;
|
||||
const end = material.material.endTime ?? 0;
|
||||
if (start === 0 || end === 0) return;
|
||||
|
||||
const totalCycleTime = (end - start) / 1000; // Convert milliseconds to seconds
|
||||
totalCycleTimeSum += totalCycleTime;
|
||||
cycleCount++;
|
||||
});
|
||||
|
||||
if (cycleCount > 0) {
|
||||
const averageCycleTime = totalCycleTimeSum / cycleCount;
|
||||
setMaterialCycleTime(Number(averageCycleTime.toFixed(2)));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isPlaying) {
|
||||
setTimeout(() => {
|
||||
getMachineActive();
|
||||
}, 500)
|
||||
}
|
||||
}, [armBots, materials, materialHistory, machines, vehicles, selectedProduct])
|
||||
|
||||
useEffect(() => {
|
||||
if (machineActiveTime > 0 && materialCycleTime > 0 && machineCount > 0) {
|
||||
|
||||
|
||||
|
||||
const utilization = machineActiveTime / 3600; // Active time per hour
|
||||
const unitsPerMachinePerHour = 3600 / materialCycleTime;
|
||||
const throughput = unitsPerMachinePerHour * machineCount * utilization;
|
||||
setThroughputData(Number(throughput.toFixed(2))); // Keep as number
|
||||
//
|
||||
}
|
||||
}, [machineActiveTime, materialCycleTime, machineCount]);
|
||||
|
||||
return (
|
||||
<>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user