ria/app/Views/powerConsumptionDetails.php
2025-01-27 16:53:04 +05:30

1287 lines
53 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<style type="text/css">
.num {
text-align: right;
}
.fht-table,
.fht-table thead,
.fht-table tfoot,
.fht-table tbody,
.fht-table tr,
.fht-table th,
.fht-table td {
margin: 0;
}
.fht-table td {
text-align: center;
}
.fht-table td:hover {
background-color: #00a65a;
color: #ffffff;
}
.fht-table {
border: 0 none;
height: auto;
width: auto;
border-collapse: collapse;
border-spacing: 0;
/*table-layout: fixed; Algoritmo de distribucion fijo */
white-space: nowrap;
}
.fht-table th,
.fht-table td {
overflow: hidden;
}
.fht-table-wrapper,
.fht-table-wrapper .fht-thead,
.fht-table-wrapper .fht-tfoot,
.fht-table-wrapper .fht-fixed-column .fht-tbody,
.fht-table-wrapper .fht-fixed-body .fht-tbody,
.fht-table-wrapper .fht-tbody {
overflow: hidden;
position: relative;
}
.fht-table-wrapper .fht-fixed-body .fht-tbody,
.fht-table-wrapper .fht-tbody {
overflow: auto;
}
.fht-table-wrapper .fht-table .fht-cell {
overflow: hidden;
height: 1px;
}
.fht-table-wrapper .fht-fixed-column,
.fht-table-wrapper .fht-fixed-body {
top: 0;
left: 0;
position: absolute;
}
.fht-table-wrapper .fht-fixed-column {
z-index: 1;
}
.fht-fixed-body .fht-thead table {
margin-right: 20px;
border: 0 none;
}
/*For Examples*/
.ContenedorTabla {
height: 500px;
margin: 0 auto;
overflow: auto;
width: auto;
position: relative;
}
.header,
.main {
display: inline-block;
height: auto;
width: 100%;
}
.titulosHeader {
border-bottom: 1px solid #e8bb25;
height: auto;
margin-bottom: 10px;
padding-bottom: 8px;
padding-top: 8px;
width: 100%;
}
._Separador {
background-color: #fff;
height: 12px;
border-left: 1px solid #ccc;
border-right: 1px solid #ccc;
width: 7px;
}
._Separador div {
width: 4px;
}
/*style excel*/
.excel_cell {
border: 1px solid #CCC;
color: #222;
text-align: center;
font-size: 13px;
font-weight: normal;
padding: 4px;
white-space: pre-line;
empty-cells: show;
}
._cell_header {
background-color: #EEE;
}
._cell_Default {
background-color: #FFF;
text-align: left;
}
.excel_cell div {
width: 30px;
height: 20px;
}
.celda_encabezado_general {
background-color: #00a65a;
border: 1px solid #ccc;
color: #ffffff;
font-weight: bold;
padding: 6px 10px;
text-align: center;
}
.celda_encabezado_total {
background-color:#ffffff !important;
border: 1px solid #ffffff;
color: #ffffff !important;
font-weight: bold;
padding: 6px 10px;
text-align: center;
}
.celda_normal {
/*background-color: #fff;*/
/* <!-- ad9271 into ffffff brown-light to green-light --> */
text-align: center;
border: 1px solid #ccc;
padding: 10px 6px;
}
/* Make entire thead sticky */
.fht-table thead {
position: sticky;
top: 0;
z-index: 20; /* Ensure the entire header stays above table rows */
background-color: #00a65a; /* Header background color */
}
.dropdown-menu {
z-index :25 ;
}
/* Ensure each <th> in thead stays sticky */
.fht-table thead th {
position: sticky;
top: 0;
z-index: 11; /* Higher than table rows but lower than first column */
background-color: #00a65a; /* Green background */
color: #ffffff;
border: 1px solid #ddd;
padding: 10px;
}
/* Sticky First Column */
.fht-table th:nth-child(1),
.fht-table td:nth-child(1) {
position: sticky;
left: 0;
background-color: #00a65a;
z-index: 15; /* Ensures it stays above other cells */
border-right: 2px solid #ddd;
color: #ffffff;
}
/* Table Wrapper for Scrolling */
.table-responsive {
overflow-x: auto;
overflow-y: auto;
max-height: 450px; /* Adjust as needed */
position: relative;
}
td{
min-width: 100px;
max-width: 100px;
}
</style>
<?php
$date = DateTime::createFromFormat('M-Y', $month);
$startDate = $date->modify('first day of this month')->format('Y-m-d');
$endDate = $date->modify('last day of this month')->format('Y-m-d');
$datesInMonth = [];
$period = new DatePeriod(
new DateTime($startDate),
new DateInterval('P1D'),
(new DateTime($endDate))->modify('+1 day')
);
foreach ($period as $day) {
$datesInMonth[] = $day->format('Y-m-d');
}
?>
<div class="content-page">
<div class="content">
<!-- Start Content-->
<div class="container-fluid">
<!-- start page title -->
<div>
<div class="row">
<div class="col-6">
<div class="page-title-box page-title-box-alt">
<div class="page-title-right">
<ol class="breadcrumb m-0">
<li class="breadcrumb-item"><a href="javascript: void(0);">Stocks</a></li>
<li class="breadcrumb-item active">
<h4 class="page-title">Power Consumption Stock Details </h4>
</li>
</ol>
</div>
</div>
</div>
<div class="col-6 text-right">
<!-- Right-aligned buttons or links can be added here -->
</div>
</div>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<form action="<?= base_url('powerConsumptionDetails'); ?>" method="post">
<div class="row">
<div class="col-3">
<input type="text" name="month" id="month" value="<?php echo "$month" ?>"
class="form-control mt-2" data-provide="datepicker"
style="z-index:30;"
data-date-format="M-yyyy" data-date-min-view-mode="1">
</div>
<div class="form-group col-md-2">
<button type="submit" class="btn btn-success"> Change Month </button>
</div>
<div class="col-md-1"></div>
<div class="col-6">
<div class="dropdown float-right">
<a href="#" class="dropdown-toggle arrow-none" data-toggle="dropdown"
aria-expanded="false">
<i class="mdi mdi-dots-vertical m-0 text-muted h3"></i>
</a>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" data-toggle="modal" data-target="#bs-example-modal-lg">Navigation</a>
<a class="dropdown-item" href="#">Export</a>
</div>
</div>
</div>
</div>
</form>
<div class="table-responsive">
<table id="powerStockDetailsTableId" class="fht-table table-striped">
<thead class="celda_encabezado_general" style="padding: 10px;">
<tr>
<th class="celda_encabezado_general" colspan="1"> Machine </th>
<th class="celda_encabezado_general" colspan="8">
150 KW Power
</th>
<?php foreach ($electricMachines as $electricMachine) { ?>
<!-- this will loop till machines present -->
<th class="celda_encabezado_general" colspan="3">
<?= $electricMachine['machine_name'] ?>
</th>
<?php } ?>
</tr>
<tr>
<th class="celda_encabezado_general" colspan="1">Date </th>
<th class="celda_encabezado_general">Opening Reading </th>
<th class="celda_encabezado_general">Final Reading</th>
<th class="celda_encabezado_general">Total Reading</th>
<th class="celda_encabezado_general">Total Units </th>
<th class="celda_encabezado_general">Average PF</th>
<th class="celda_encabezado_general">Present PF</th>
<th class="celda_encabezado_general">MD </th>
<th class="celda_encabezado_general">MF *60</th>
<?php foreach ($electricMachines as $powerMachine) { ?>
<!-- this will loop till machines present -->
<th class="celda_encabezado_general" style="display:none">Date</th>
<th class="celda_encabezado_general" style="display:none"> Machine Id</th>
<th class="celda_encabezado_general">Opening Units</th>
<th class="celda_encabezado_general">Final Units</th>
<th class="celda_encabezado_general">Total Units</th>
<?php } ?>
</tr>
</thead>
<tbody style="background-color: #fff;">
<?php if (!empty($groupedPowerConsumptionStockDetails)) {
$summary = [];
foreach ($groupedPowerConsumptionStockDetails as $groupedIndex => $powerStockDetails) {
?>
<tr
<?php
$currentDate = date('d-m-Y');
$dateInMonthDmYFormat = DateTime::createFromFormat('Y-m-d', $powerStockDetails[0]['date'])->format('d-m-Y');
if ($dateInMonthDmYFormat === $currentDate) {
echo 'style="background: #cef0ad;"';
}
?>>
<?php
$startDate = DateTime::createFromFormat('Y-m-d', $totalPower[$groupedIndex]['date'])->format('d-m-Y');
?>
<td class="celda_normal">
<?= $startDate ?>
</td>
<td class="celda_normal openingTP"
data-id="<?=$totalPower[$groupedIndex]['date']?>"
<?php if ($groupedIndex == 0): ?>
oninput="openingTPStockChange(this)"
contenteditable="true"
<?php endif; ?>
><?= $totalPower[$groupedIndex]['opening_reading'] ?? ' ' ?></td>
<td class="celda_normal finalTP"
data-id="<?=$totalPower[$groupedIndex]['date']?>"
oninput="finalTPStockChange(this)"
contenteditable="true">
<?= $totalPower[$groupedIndex]['final_reading'] ?? ' ' ?>
</td>
<td class="celda_normal totalReadingTP"
data-id="<?=$totalPower[$groupedIndex]['date']?>" >
<?= $totalPower[$groupedIndex]['total_reading'] ?? ' ' ?>
</td>
<td class="celda_normal totalUnitsTP"
data-id="<?=$totalPower[$groupedIndex]['date']?>"
>
<?= $totalPower[$groupedIndex]['total_units'] ?? ' ' ?>
</td>
<td class="celda_normal averagePfTP"
data-id="<?=$totalPower[$groupedIndex]['date']?>"
>
<?= $totalPower[$groupedIndex]['average_pf'] ?? ' ' ?>
</td>
<td class="celda_normal presentPfTP"
data-id="<?=$totalPower[$groupedIndex]['date']?>"
>
<?= $totalPower[$groupedIndex]['present_pf'] ?? ' ' ?>
</td>
<td class="celda_normal mdTP"
oninput="mdTPStockChange(this)"
data-id="<?=$totalPower[$groupedIndex]['date']?>"
>
<?= $totalPower[$groupedIndex]['md'] ?? ' ' ?>
</td>
<td class="celda_normal mfTP"
data-id="<?=$totalPower[$groupedIndex]['date']?>"
>
<?= $totalPower[$groupedIndex]['mf'] ?? ' ' ?>
</td>
<?php
//before closing outer loop
if (!isset($summary['openingReading'])) {
$summary['openingReading'] = 0 ;
$summary['finalReading'] = 0 ;
$summary['totalReading'] = 0 ;
$summary['totalUnits'] = 0 ;
$summary['averagePf'] = 0 ;
$summary['presentPf'] = 0 ;
$summary['md'] = 0 ;
$summary['mf'] = 0 ;
}
$summary['openingReading'] = " " ;
$summary['finalReading'] = is_numeric($totalPower[$groupedIndex]['final_reading']) ? $totalPower[$groupedIndex]['final_reading'] : 0 ;
$summary['totalReading'] += is_numeric($totalPower[$groupedIndex]['total_reading']) ? $totalPower[$groupedIndex]['total_reading'] : 0 ;
$summary['totalUnits'] += is_numeric($totalPower[$groupedIndex]['total_units']) ? $totalPower[$groupedIndex]['total_units'] : 0 ;
$summary['averagePf'] = " " ;
$summary['presentPf'] = " ";
$summary['md'] = " " ;
$summary['mf'] = " " ;
?>
<?php foreach ($powerStockDetails as $powerStockDetailIndex => $powerStock) { ?>
<td style="display:none">
<?= $powerStock['date'] ?>
</td>
<td style="display:none"
data-id="<?= $powerStock['date'] ?> <?= $powerStock['machine_id'] ?>"
class="machine_id">
<?= $powerStock['machine_id'] ?>
</td>
<td class="celda_normal openingUnits"
data-id="<?= $powerStock['date'] ?> <?= $powerStock['machine_id'] ?>"
<?php if ($groupedIndex == 0): ?>
oninput="openingUnitsChange(this)"
contenteditable="true"
<?php endif; ?>>
<?= $powerStock['opening_units'] ?? " " ?>
</td>
<td class="celda_normal closingUnits"
data-id="<?= $powerStock['date'] ?> <?= $powerStock['machine_id'] ?>"
oninput="closingUnitsChange(this)"
contenteditable="true">
<?= $powerStock['closing_units'] ?? " " ?>
</td>
<td class="celda_normal totalUnits"
data-id="<?= $powerStock['date'] ?> <?= $powerStock['machine_id'] ?>"
oninput="totalUnitsChange(this)"
contenteditable="true">
<?= $powerStock['total_units'] ?? " " ?>
</td>
<?php
//before closing inner loop we just adding the total values inside it
$machineId = $powerStock['machine_id']; // Unique identifier
// Initialize the material entry if it doesn't exist
if (!isset($summary["machineId_$machineId"])) {
$summary["machineId_$machineId"] = [
'opening_units' => "-" ,
'closing_units' => 0 ,
'total_units' => 0 ,
];
}
// Sum values for the material
$summary["machineId_$machineId"]['opening_units'] = " " ;
$summary["machineId_$machineId"]['closing_units'] = " " ;
$summary["machineId_$machineId"]['total_units'] += is_numeric($powerStock['total_units']) ? $powerStock['total_units'] : 0 ;
} ?>
</tr>
<?php
}
?>
<div class="table-responsive">
<table id="powerStockDetailsTableSummaryId" class="fht-table table-striped">
<thead >
<tr>
<th class="celda_encabezado_total" colspan="1">Date </th>
<th class="celda_encabezado_total">Opening Reading </th>
<th class="celda_encabezado_total">Final Reading</th>
<th class="celda_encabezado_total">Total Reading</th>
<th class="celda_encabezado_total">Total Units </th>
<th class="celda_encabezado_total">Average PF</th>
<th class="celda_encabezado_total">Present PF</th>
<th class="celda_encabezado_total">MD </th>
<th class="celda_encabezado_total">MF *60</th>
<?php foreach ($electricMachines as $powerMachine) { ?>
<!-- this will loop till machines present -->
<th class="celda_encabezado_total" style="display:none">Date</th>
<th class="celda_encabezado_total" style="display:none"> Machine Id</th>
<th class="celda_encabezado_total">Opening Units</th>
<th class="celda_encabezado_total">Final Units</th>
<th class="celda_encabezado_total">Total Units</th>
<?php } ?>
</tr>
</thead>
<tbody>
<tr>
<td style="background-color:rgb(189, 9, 6); color:#ffff;">
<b> Total </b>
</td>
<td><?= $summary['openingReading'] ?></td>
<td><?= $summary['finalReading'] ?></td>
<td><?= $summary['totalReading'] ?></td>
<td><?= $summary['totalUnits'] ?></td>
<td><?= $summary['averagePf'] ?></td>
<td><?= $summary['presentPf'] ?></td>
<td><?= $summary['md'] ?></td>
<td><?= $summary['mf'] ?></td>
<?php foreach($summary as $each){
if(is_array($each)){
?>
<td class="celda_normal "><?=$each['opening_units']?> </td>
<td class="celda_normal "><?=$each['closing_units']?></td>
<td class="celda_normal "><?=$each['total_units']?></td>
<?php
}
}
?>
</tr>
</tbody>
</table>
</div>
<!-- this else condition work if groupedpowerStockDetails is empty and creating new records
with initial values 0 for entire month -->
<?php } else { ?>
<?php foreach ($datesInMonth as $datesInMonthIndex => $dateInMonth) { ?>
<tr
<?php
$currentDate = date('d-m-Y');
$dateInMonthDmYFormat = DateTime::createFromFormat('Y-m-d', $dateInMonth)->format('d-m-Y');
if ($dateInMonthDmYFormat === $currentDate) {
echo 'style="background: #cef0ad;"';
}
?> >
<?php
$startDate = DateTime::createFromFormat('Y-m-d', $dateInMonth)->format('d-m-Y');
?>
<td class="celda_normal">
<?= $startDate ?>
</td>
<td class="celda_normal openingTP"
data-id="<?= $dateInMonth?>"
<?php if ($datesInMonthIndex == 0) { ?>
oninput="openingTPStockChange(this)"
contenteditable="true"
<?php } ?>
><?php
if(empty( $previousMonthTotalPowerMachineStockDetails)){
echo " " ;
}else{
echo $previousMonthTotalPowerMachineStockDetails[0]['final_reading'] !=0 ?? " " ;
}
?>
</td>
<td class="celda_normal finalTP"
data-id="<?= $dateInMonth ?>"
oninput="finalTPStockChange(this)"
contenteditable="true">
<?php
if(empty( $previousMonthTotalPowerMachineStockDetails)){
echo " " ;
}else{
echo $previousMonthTotalPowerMachineStockDetails[0]['final_reading'] !=0 ?? " " ;
}
?>
</td>
<td class="celda_normal totalReadingTP"
data-id="<?= $dateInMonth ?>"> </td>
<td class="celda_normal totalUnitsTP"
data-id="<?= $dateInMonth ?>"
> </td>
<td class="celda_normal averagePfTP" contenteditable="true"
data-id="<?= $dateInMonth ?>"
> </td>
<td class="celda_normal presentPfTP" contenteditable="true"
data-id="<?= $dateInMonth ?>"
> </td>
<td class="celda_normal mdTP" contenteditable="true"
oninput="mdTPStockChange(this)"
data-id="<?= $dateInMonth ?>"
> </td>
<td class="celda_normal mfTP"
data-id="<?= $dateInMonth ?>"
> </td>
<?php
foreach ($electricMachines as $powerMachineIndex => $powerMachine) {
?>
<!-- this will loop till machines present -->
<td class="celda_normal" style="display:none">
<?= $dateInMonth ?>
</td>
<td class="celda_normal" style="display:none"
data-id="<?= $dateInMonth ?> <?= $powerMachine['id'] ?>">
<?= $powerMachine['id'] ?>
</td>
<td class="celda_normal openingUnits"
data-id="<?= $dateInMonth ?> <?= $powerMachine['id'] ?>"
<?php if ($datesInMonthIndex == 0) { ?>
oninput="openingUnitsChange(this)"
contenteditable="true"
<?php } ?>
><?= $previousMonthPowerMachineStockDetails[$powerMachineIndex]['closing_reading'] ?? ' ' ?>
</td>
<td class="celda_normal closingUnits"
data-id="<?= $dateInMonth ?> <?= $powerMachine['id'] ?>"
oninput="closingUnitsChange(this)"
contenteditable="true"><?= $previousMonthPowerMachineStockDetails[$powerMachineIndex]['closing_reading'] ?? ' ' ?>
</td>
<td class="celda_normal totalUnits"
data-id="<?= $dateInMonth ?> <?= $powerMachine['id'] ?>"
contenteditable="true"> </td>
<?php } ?>
</tr>
<?php } ?>
<?php } ?>
</tbody>
</table>
</div>
<div class="row">
<div class="col-md-12 text-right">
<input type="button" class="btn btn-success" id="saveId" value="save">
</div>
</div>
</div>
</div> <!-- End card-body -->
</div> <!-- End card -->
</div> <!-- End col -->
</div> <!-- End row -->
</div>
</div> <!-- End container-fluid -->
</div>
</div>
<div>
<div class="modal fade" id="bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myLargeModalLabel">Navigation Options</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<div class="row">
<!-- bag Dropdown -->
<div class="col-md-6">
<div class="form-group">
<label for="powerDropdown">Select Bag</label>
<select id="powerDropdown" class="form-control">
<option value="">Select Electric Machines</option>
<?php foreach ($electricMachines as $powerMachine): ?>
<option value=" <?= $powerMachine['machine_name'] ?>">
<?= $powerMachine['machine_name'] ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
<!-- Date Dropdown -->
<div class="col-md-6">
<div class="form-group">
<label for="dateDropdown">Select Date</label>
<select id="dateDropdown" class="form-control">
<?php foreach ($datesInMonth as $selectDate): ?>
<?php $selectDate = DateTime::createFromFormat('Y-m-d', $selectDate)->format('d-m-Y'); ?>
<option value="<?= $selectDate ?>"><?= $selectDate ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
</div>
<button class="btn btn-primary float-right" onclick="navigateToPower()">Navigate</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</div>
<script>
function navigateToPower() {
$('.close').click();
const powerDropdown = document.getElementById('powerDropdown');
const dateDropdown = document.getElementById('dateDropdown');
const selectedPower = powerDropdown.value;
const selectedDate = dateDropdown.value;
const table = document.getElementById('powerStockDetailsTableId');
const headerCells = table.querySelectorAll('thead tr:nth-child(1) th');
const rows = table.querySelectorAll('tbody tr');
let targetColumnIndex = -1;
headerCells.forEach((header, index) => {
if ((header.innerText.trim()) === selectedPower.trim()) {
targetColumnIndex = index;
return;
}
});
if (targetColumnIndex === -1) {
console.log("power column not found.");
return;
}
let targetRow = null;
rows.forEach(row => {
const dateCell = row.querySelector('td:first-child');
if (dateCell && dateCell.textContent.trim() === selectedDate) {
targetRow = row;
}
});
if (!targetRow) {
console.log("Date row not found.");
return;
}
targetColumnIndex = targetColumnIndex * 7;
targetRow.cells[targetColumnIndex].scrollIntoView({
behavior: 'smooth',
inline: 'center',
block: 'center'
});
const highlightDuration = 10000;
for (let i = targetColumnIndex - 4; i <= targetColumnIndex; i++) {
const cell = targetRow.cells[i];
if (cell) {
cell.style.transition = "background-color 0.5s";
cell.style.backgroundColor = "#df9675";
setTimeout(() => {
cell.style.backgroundColor = "";
}, highlightDuration);
}
}
}
</script>
<script type="text/javascript">
$(document).ready(function() {
$('#saveId').click(function() {
//table to json function
var updatePowerConsumptionDetails = tableToJson();
alert('Updation may take a while, And we appreciate your patience..!!');
$('#loader').show();
$.ajax({
data: {
updatePowerConsumptionDetails
},
type: "POST",
url: "<?php echo base_url() ?>updatePowerConsumptionDetails",
success: function(data) {
if (data) {
$('#loader').hide();
alert(data);
window.location.reload();
}
},
error: function(xhr, status, error) {
alert("An error occurred while processing the request. Please try again.");
console.error("Error Code:", xhr.status);
console.error("Error Message:", error);
console.error("Response Text:", xhr.responseText);
},
complete: function() {
$('#loader').hide();
}
});
});
});
</script>
<script>
function tableToJson() {
const table = $('#powerStockDetailsTableId');
const summaryRowPart = [] ;
const readingsRowPart = [];
const allRow = [] ;
//this table to json is customized for powerStock not for all
table.find('tbody tr').each(function(index) {
const rowCells = $(this).find('td');
const date = rowCells.eq(0).text().trim();
const formattedDate = `${date.split('-')[2]}-${date.split('-')[1]}-${date.split('-')[0]}`;
const summaryRowData = {
date: formattedDate,
opening_reading: rowCells.eq(1).text().trim(),
final_reading : rowCells.eq(2).text().trim(),
total_reading : rowCells.eq(3).text().trim(),
total_units : rowCells.eq(4).text().trim(),
average_pf : rowCells.eq(5).text().trim(),
present_pf : rowCells.eq(6).text().trim(),
md : rowCells.eq(7).text().trim(),
mf : rowCells.eq(8).text().trim()
};
summaryRowPart.push(summaryRowData);
/** we get eight entries against a machines so we loop from 1 to 8 and so on
in a single row **/
for (let i = 9 ; i < rowCells.length; i += 5 ) {
const readingsRowData = {
date: rowCells.eq(i).text().trim(),
machine_id: rowCells.eq(i + 1).text().trim(),
opening_units: rowCells.eq(i + 2).text().trim(),
closing_units: rowCells.eq(i + 3).text().trim(),
total_units : rowCells.eq(i + 4).text().trim(),
};
readingsRowPart.push(readingsRowData);
}
});
allRow.push(summaryRowPart ,readingsRowPart )
return JSON.stringify(allRow, null, 2);
}
</script>
<script>
function openingUnitsChange(tdElement) {
try {
let dataId = tdElement.getAttribute('data-id');
let [date, machine_id] = tdElement.getAttribute('data-id').split(' ');
let openingUnits = validateInput(tdElement.innerText);
let closingUnits = validateInput(document.querySelector(`td.closingUnits[data-id="${dataId}"]`).innerText);
let totalUnits = parseFloat(closingUnits) - parseFloat(openingUnits) ;
document.querySelector(`td.totalUnits[data-id="${dataId}"]`).innerText = (totalUnits).toFixed(2);
updateStockValue(date,machine_id,closingUnits)
} catch (error) {
console.error("There is an error updating stock ..!!" + error);
}
}
function closingUnitsChange(tdElement) {
try {
let dataId = tdElement.getAttribute('data-id');
let [date, machine_id] = tdElement.getAttribute('data-id').split(' ');
let openingUnits = validateInput(document.querySelector(`td.openingUnits[data-id="${dataId}"]`).innerText);
let closingUnits = validateInput(tdElement.innerText);
let totalUnits = parseFloat(closingUnits) - parseFloat(openingUnits) ;
document.querySelector(`td.totalUnits[data-id="${dataId}"]`).innerText = (totalUnits).toFixed(2);
updateStockValue(date,machine_id,closingUnits)
} catch (error) {
console.error("There is an error updating stock ..!!" + error);
}
}
function openingTPStockChange(tdElement){
try {
let dataId = tdElement.getAttribute('data-id') ;
let openingTP = validateInput(tdElement.innerText) ;
let finalTP = validateInput(document.querySelector(`td.finalTP[data-id="${dataId}"]`).innerText) ;
let totalReadingTP = (Number(finalTP) - Number(openingTP)) ;
let totalUnitsTP = totalReadingTP * 60 ;
document.querySelector(`td.totalReadingTP[data-id="${dataId}"]`).innerText = totalReadingTP ;
document.querySelector(`td.totalUnitsTP[data-id="${dataId}"]`).innerText = totalUnitsTP ;
updateTotalStockValue(dataId , finalTP) ;
} catch (error) {
console.error("There is an error updating stock ..!!" + error);
}
}
function finalTPStockChange(tdElement){
try {
let dataId = tdElement.getAttribute('data-id');
let openingTP = validateInput(document.querySelector(`td.openingTP[data-id="${dataId}"]`).innerText);
let finalTP = validateInput(tdElement.innerText);
let totalReadingTP = (Number(finalTP) - Number(openingTP)) ;
let totalUnitsTP = totalReadingTP * 60 ;
document.querySelector(`td.totalReadingTP[data-id="${dataId}"]`).innerText = totalReadingTP ;
document.querySelector(`td.totalUnitsTP[data-id="${dataId}"]`).innerText = totalUnitsTP ;
updateTotalStockValue(dataId , finalTP)
} catch (error) {
console.error("There is an error updating stock ..!!" + error);
}
}
function mdTPStockChange(tdElement){
try {
let dataId = tdElement.getAttribute('data-id');
let mdTP = validateInput(tdElement.innerText);
document.querySelector(`td.mfTP[data-id="${dataId}"]`).innerText = (mdTP * 60).toFixed(2) ;
} catch (error) {
console.error("There is an error updating stock ..!!" + error) ;
}
}
function validateInput(input) {
if (input == '' || input == ' ' || input == '-') {
return 0;
}
if (typeof input === 'string' && input.split('').map(char => char.charCodeAt(0))[0] == 10) {
return 0;
}
// Validate against the regex
const isValid = /^-?\d*\.?\d*$/.test(input);
if (!isValid) {
alert('Invalid input! Please enter a valid number.');
return 0;
}
return input;
}
</script>
<script>
function updateStockValue(date, machine_id, closingUnits) {
const table = $('#powerStockDetailsTableId');
const rows = [];
table.find('tbody tr').each(function() {
const rowCells = $(this).find('td');
let otherDate = rowCells.eq(0).text().trim();
otherDate = `${otherDate.split('-')[2]}-${otherDate.split('-')[1]}-${otherDate.split('-')[0]}`;
if (new Date(otherDate) > new Date(date)) {
let dataId = `${otherDate} ${machine_id}`;
document.querySelector(`td.openingUnits[data-id="${dataId}"]`).innerText = closingUnits == 0 ? " " : closingUnits ;
let openingUnits = validateInput(document.querySelector(`td.openingUnits[data-id="${dataId}"]`).innerText);
let totalUnits = validateInput(document.querySelector(`td.totalUnits[data-id="${dataId}"]`).innerText);
closingUnits = Math.abs(Number(openingUnits) + Number(totalUnits));
document.querySelector(`td.closingUnits[data-id="${dataId}"]`).innerText = closingUnits == 0 ? " " : closingUnits ;
}
})
}
function updateTotalStockValue(date, finalTP){
const table = $('#powerStockDetailsTableId');
const rows = [];
table.find('tbody tr').each(function() {
const rowCells = $(this).find('td');
let otherDate = rowCells.eq(0).text().trim();
otherDate = `${otherDate.split('-')[2]}-${otherDate.split('-')[1]}-${otherDate.split('-')[0]}`;
if (new Date(otherDate) > new Date(date)) {
let dataId = `${otherDate}`;
document.querySelector(`td.openingTP[data-id="${dataId}"]`).innerText = finalTP == 0 ? " " : finalTP ;
let openingTP = validateInput(document.querySelector(`td.openingTP[data-id="${dataId}"]`).innerText) ;
let totalReadingTP = validateInput(document.querySelector(`td.totalReadingTP[data-id="${dataId}"]`).innerText) ;
finalTP = Number(openingTP) + Number(totalReadingTP) ;
let totalUnitsTP = Number(totalReadingTP * 60 );
document.querySelector(`td.finalTP[data-id="${dataId}"]`).innerText = finalTP == 0 ? " " : finalTP;
document.querySelector(`td.totalReadingTP[data-id="${dataId}"]`).innerText = totalReadingTP == 0 ? " " : totalReadingTP ;
document.querySelector(`td.totalUnitsTP[data-id="${dataId}"]`).innerText = totalUnitsTP == 0 ? " " : totalUnitsTP;
}
})
}
</script>