CodeNewbie Community 🌱

shigero-neko
shigero-neko

Posted on

turn this static code dynamic

I have this code where it currently runs on Static or hard-coded, in order for me to finish a module of the system that I am working on I need this to be in dynamic form but I am not sure how to do it and I have a deadline by next week. i hope someone can help, please reply or message me :)

this is my code in static

 <script>
        // Data for the items and their values
        const items = {
            aluminum:  7010.56,
            amorphous_ribbon:  26192.94,
            copper:  18483.72,
            silicon_steel: 0,
            silicon_steel_amdt_core_wrap:  417.78,
            dielectric_fluid_high_fire_point_f: 0,
            dielectric_fluid_high_fire_point_m: 0,
            dielectric_fluid_mineral:  10248.30,
            bushing:  3120.60,
            painted_tank:  9994.53,
            accessories:  1353.61 ,
            bus_bar:  0,
            core_coil_frame: 976.95,
            coil_support:  158.18,
            corrugated_fin:  0,
            epoxy:  335.50 ,
            gasket:  0,
            lv_terminal:23.48,
            nameplate:  40.18,
            paper_insulation:    2346.25,
            pole_line_hardware: 0,
            sticker:  82.32 
        };



        function calculateValues() {
            const modelNumber = document.getElementById("model_no").value;
            const customValueField = document.getElementById("custom_value");
            const bomValueField = document.getElementById("bom_value");
            const otherMaterialField = document.getElementById("other_materials_input");
            const otherMaterialsField = document.getElementById("other_materials_col");
            const hardwareInput = document.getElementById("hardware_input");
            const hardwareField = document.getElementById("hardware_col");
            const totalInput = document.getElementById("total_input");
            const totalField = document.getElementById("total_col");
            const directLaborInput = document.getElementById("direct_labor_input");
            const directLaborCol = document.getElementById("direct_labor_col");
            const overheadInput = document.getElementById("overhead_input");
            const overheadField = document.getElementById("overhead_col");
            const targetCostInput = document.getElementById("target_cost");
            const targetCostField = document.getElementById("target_cost_col");
            const allowanceInput = document.getElementById("allowance_input");
            const allowanceField = document.getElementById("allowance_col");
            const standardCostInput = document.getElementById("standard_cost");
            const standardCostField = document.getElementById("standard_cost_col");
            const markupInput = document.getElementById("markup");
            const markupField = document.getElementById("mark_up");
            const tpWithoutVatInput = document.getElementById("tp_without");
            const tpWithoutVatField = document.getElementById("tpwithout");
            const tpWithVatInput = document.getElementById("tp_with");
            const tpWithVatField = document.getElementById("tpwith");
            const gpmInput = document.getElementById("gpm");
            const gpmField = document.getElementById("g_pm");

            if (modelNumber === "NC533") {
                // Calculate the "Custom Value" based on the formula
                const customValue = items.aluminum + items.amorphous_ribbon +
                    items.copper + items.silicon_steel + items.silicon_steel_amdt_core_wrap +
                    items.dielectric_fluid_high_fire_point_f + items.dielectric_fluid_high_fire_point_m +
                    items.dielectric_fluid_mineral + items.bushing + items.painted_tank;


                // Limit the result to two decimal places
                const customValueRounded = customValue.toFixed(2);

                customValueField.value = customValueRounded;
                bomValueField.textContent = customValueRounded;



                 // Calculate the "Other Materials" based on the specified formula
                const otherMaterials = items.accessories + items.bus_bar + items.core_coil_frame +
                    items.coil_support + items.corrugated_fin + items.epoxy + items.gasket +
                    items.lv_terminal + items.nameplate + items.paper_insulation +
                    items.pole_line_hardware + items.sticker;

                // Limit the result to two decimal places
                const otherMaterialsRounded = otherMaterials.toFixed(2);

                // otherMaterialsField.textContent = otherMaterialsRounded;
                otherMaterialsField.textContent = otherMaterialsRounded;
                otherMaterialField.value = otherMaterialsRounded;


                 // Calculate "Hardware" based on the formula "totalDirectMaterials + totalOtherMaterials * 1%"
                const hardware = (customValue + otherMaterials) * 0.01;

                // // Limit the result to two decimal places
                const hardwareRounded = Math.round(hardware * 100) / 100;

                hardwareField.value = hardwareRounded.toFixed(2); // Display in the input with 2 decimal places
                hardwareField.textContent = hardwareRounded;


                //CALCULATE WHERE THE VALUE OF HARDWARE IS ZERO

                document.getElementById("hardware_input").value = 0;


                // Calculate the "TOTAL" based on the formula "total of direct material + total other material + hardware"
                const total = customValue + otherMaterials;

                // Limit the result to two decimal places
                const totalRounded = total.toFixed(2);

                totalInput.value = totalRounded;
                totalField.textContent = totalRounded;


                 // Define the array of values for direct labor
                const directLaborValues = [
                216.47, 20.10, 18.07, 0.00, 17.23, 17.39, 240.28, 37.94, 7.90, 0.00, 14.12, 
                47.92, 78.22, 113.61, 61.56, 66.17, 205.82, 152.09, 306.83, 161.08, 208.92, 423.70, 258.92, 80.86
                ];

                // Calculate the total direct labor
                const totalDirectLabor = directLaborValues.reduce((acc, value) => acc + value, 0);

                // Limit the result to two decimal places
                const totalDirectLaborRounded = totalDirectLabor.toFixed(2);

                // Set the value in the "Direct Labor" input field and the displayed field
               directLaborInput.value = totalDirectLaborRounded;
                directLaborCol.textContent = totalDirectLaborRounded;


                // Specific values for the overhead cost
                const overheadCosts = [2668.66, 4875.40, 1596.15, 1346.61, 832.25];



                // Calculate the "Overhead Cost" as the sum of the provided values
                const overheadCost = overheadCosts.reduce((acc, value) => acc + value, 0);

                // Limit the result to two decimal places
                const overheadCostRounded = overheadCost.toFixed(2);

                // Set the value in the "Overhead Cost" input field and displayed field
                overheadInput.value = overheadCostRounded;
                overheadField.textContent = overheadCostRounded;



               // Calculate the "Target Cost" based on the formula "customValue + totalDirectLabor + overheadCost"
                const targetCost = total + totalDirectLabor + overheadCost;

                 // Limit the result to two decimal places
                const targetCostRounded = targetCost.toFixed(2);

                targetCostInput.value = targetCostRounded;
                targetCostField.textContent = targetCostRounded;


                 //Allowance
                const allowanceValue = 10;
                allowanceInput.value = allowanceValue + '%'; // Add '%' symbol to indicate it's a percentage
                allowanceField.textContent = allowanceValue + '%'; // Update the displayed value




                 // Calculate the "Standard Cost" based on the formula "Target Cost + 10% of Total Material Cost"
                const standardCost = targetCost + (0.1 * total);


                //limit the result to two decimal places
                const standardCostRounded = standardCost.toFixed(2);

                standardCostInput.value = standardCostRounded;
                standardCostField.textContent = standardCostRounded;


                 //MarkUp
                const markupValue = 36.5;
                markupInput.value = markupValue + '%'; // Add '%' symbol to indicate it's a percentage
                markupField.textContent = markupValue + '%'; // Update the displayed value

            // Convert markupValue from percentage to decimal
            const markupDecimal = markupValue / 100;

            // Calculate "TP Without VAT" based on the formula "standardCost * (1 + markupDecimal)"
            const tpWithoutVat = standardCost * (1 + markupDecimal);

            // Limit the result to two decimal places
             const tpWithoutVatRounded = Math.round(tpWithoutVat * 100) / 100;

            tpWithoutVatInput.value = tpWithoutVatRounded.toFixed(2); // Display in the input with 2 decimal places
            tpWithoutVatField.textContent = tpWithoutVat.toFixed(2); // Update the displayed value


            // Assuming tpWithoutVat is calculated in the previous steps
            const vatPercentage = 12; // VAT

            // Convert vatPercentage from percentage to decimal
            const vatDecimal = vatPercentage / 100;

            // Calculate "TP With VAT" based on the formula "tpWithoutVat * (1 + vatDecimal)"
            const tpWithVat = tpWithoutVat * (1 + vatDecimal);

            // Limit the result to two decimal places
            const tpWithVatRounded = Math.round(tpWithVat * 100) / 100;

            tpWithVatInput.value = tpWithVatRounded.toFixed(2); // Display in the input with 2 decimal places
             tpWithVatField.textContent = tpWithVat.toFixed(2); // Update the displayed value



            // Calculate the "Gross Profit Margin" based on the formula "(tpWithoutVat - standardCost) / tpWithoutVat"
            const tpWithoutVat2 = parseFloat(tpWithoutVatInput.value);
            const standardCost2 = parseFloat(standardCostInput.value);
            const gpm = (tpWithoutVat - standardCost) / tpWithoutVat;

            // Update the "Gross Profit Margin" fields
            gpmInput.value = (gpm * 100).toFixed(2) + '%'; // Format to two decimal places and add a percentage sign
            gpmField.textContent = (gpm * 100).toFixed(2) + '%'; // Update the displayed value with percentage






            } else {
                // Reset all fields if the model number is not found
                customValueField.textContent = '0';
                bomValueField.textContent = '0';
                otherMaterialsInput.value = '';
                otherMaterialsField.textContent = '0';
                hardwareInput.value = '';
                hardwareField.textContent = '0';
                totalInput.value = '';
                totalField.textContent = '0';
                directLaborInput.value = '';
                directLaborField.textContent = '0';
                overheadInput.value = '';
                overheadField.textContent = '0';
                targetCostInput.value = '';
                targetCostField.textContent = '0';
                allowanceInput.value = '';
                allowanceField.textContent = '0%';
                markupInput.value = '';
                markupField.textContent = '';
                standardCostInput.value = '';
                standardCostField.textContent = '';
                tpWithoutVatInput.value = '';
                tpWithoutVatField.textContent = '';
                gpmInput.value = '';
                gpmField.textContent = '';

                }
        }

    </script>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)