ria/app/Views/dieselStockDetails.php
2025-01-24 17:51:33 +05:30

1291 lines
54 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">Diesel Machine 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('dieselMachineDetails'); ?>" 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="dieselStockDetailsTableId" 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="4">
Total Diesel
</th>
<?php foreach ($dieselMachines as $dieselMachine) { ?>
<!-- this will loop till machines present -->
<th class="celda_encabezado_general" colspan="6">
<?= $dieselMachine['machine_name'] ?>
</th>
<?php } ?>
</tr>
<tr>
<th class="celda_encabezado_general" colspan="1">Date </th>
<th class="celda_encabezado_general">Opening </th>
<th class="celda_encabezado_general">Purchase Diesel</th>
<th class="celda_encabezado_general">Filling Diesel</th>
<th class="celda_encabezado_general">Balance Stock</th>
<?php foreach ($dieselMachines as $dieselMachine) { ?>
<!-- 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 Reading</th>
<th class="celda_encabezado_general">Closing Reading</th>
<th class="celda_encabezado_general">Filling Diesel</th>
<th class="celda_encabezado_general">Consumption</th>
<th class="celda_encabezado_general">Running Hours</th>
<th class="celda_encabezado_general">Mileage</th>
<?php } ?>
</tr>
</thead>
<tbody style="background-color: #fff;">
<?php if (!empty($groupedDieselMachineStockDetails)) {
$summary = [];
foreach ($groupedDieselMachineStockDetails as $groupedDieselMachineStockDetailsIndex => $dieselStockDetails) {
?>
<tr
<?php
$currentDate = date('d-m-Y');
$dateInMonthDmYFormat = DateTime::createFromFormat('Y-m-d', $dieselStockDetails[0]['date'])->format('d-m-Y');
if ($dateInMonthDmYFormat === $currentDate) {
echo 'style="background: #cef0ad;"';
}
?>>
<?php
$startDate = DateTime::createFromFormat('Y-m-d', $totalDiesel[$groupedDieselMachineStockDetailsIndex]['date'])->format('d-m-Y');
?>
<td class="celda_normal">
<?= $startDate ?>
</td>
<td class="celda_normal openingTD"
data-id="<?=$totalDiesel[$groupedDieselMachineStockDetailsIndex]['date']?>"
<?php if ($groupedDieselMachineStockDetailsIndex == 0): ?>
oninput="openingTDStockChange(this)"
contenteditable="true"
<?php endif; ?>
><?= $totalDiesel[$groupedDieselMachineStockDetailsIndex]['opening_stock'] ?? ' ' ?></td>
<td class="celda_normal purchaseTD"
data-id="<?=$totalDiesel[$groupedDieselMachineStockDetailsIndex]['date']?>"
oninput="purchaseTDStockChange(this)"
contenteditable="true">
<?= $totalDiesel[$groupedDieselMachineStockDetailsIndex]['purchase_diesel'] ?? ' ' ?>
</td>
<td class="celda_normal fillingTD"
data-id="<?=$totalDiesel[$groupedDieselMachineStockDetailsIndex]['date']?>" >
<?= $totalDiesel[$groupedDieselMachineStockDetailsIndex]['total_filling_diesel'] ?? ' ' ?>
</td>
<td class="celda_normal balanceTD"
data-id="<?=$totalDiesel[$groupedDieselMachineStockDetailsIndex]['date']?>"
>
<?= $totalDiesel[$groupedDieselMachineStockDetailsIndex]['balance_stock'] ?? ' ' ?>
</td>
<?php
//before closing outer loop
if (!isset($summary['totalPurchaseDiesel'])) {
$summary['opening_diesel'] = '-' ;
$summary['totalPurchaseDiesel'] = 0 ;
$summary['totalFillingDiesel'] = 0 ;
$summary['balanceStock'] = 0 ;
}
$summary['totalPurchaseDiesel'] += $totalDiesel[$groupedDieselMachineStockDetailsIndex]['purchase_diesel'] == '-' ? 0 : $totalDiesel[$groupedDieselMachineStockDetailsIndex]['purchase_diesel'] ;
$summary['totalFillingDiesel'] += $totalDiesel[$groupedDieselMachineStockDetailsIndex]['total_filling_diesel'] == '-' ? 0 : $totalDiesel[$groupedDieselMachineStockDetailsIndex]['total_filling_diesel'] ;
$summary['balanceStock'] = $totalDiesel[$groupedDieselMachineStockDetailsIndex]['balance_stock'] == '-' ? 0 : $totalDiesel[$groupedDieselMachineStockDetailsIndex]['balance_stock'] ;
?>
<?php foreach ($dieselStockDetails as $dieselStockIndex => $dieselStock) { ?>
<td style="display:none">
<?= $dieselStock['date'] ?>
</td>
<td style="display:none"
data-id="<?= $dieselStock['date'] ?> <?= $dieselStock['machine_id'] ?>"
class="machine_id">
<?= $dieselStock['machine_id'] ?>
</td>
<td class="celda_normal openingReading"
data-id="<?= $dieselStock['date'] ?> <?= $dieselStock['machine_id'] ?>"
<?php if ($groupedDieselMachineStockDetailsIndex == 0): ?>
oninput="openingReadingChange(this)"
contenteditable="true"
<?php endif; ?>>
<?= $dieselStock['opening_reading'] ?? " " ?>
</td>
<td class="celda_normal closingReading"
data-id="<?= $dieselStock['date'] ?> <?= $dieselStock['machine_id'] ?>"
oninput="closingReadingChange(this)"
contenteditable="true">
<?= $dieselStock['closing_reading'] ?? " " ?>
</td>
<td class="celda_normal fillingDiesel"
data-id="<?= $dieselStock['date'] ?> <?= $dieselStock['machine_id'] ?>"
oninput="fillingReadingChange(this)"
contenteditable="true">
<?= $dieselStock['filling_diesel'] ?? " " ?>
</td>
<td class="celda_normal consumption"
data-id="<?= $dieselStock['date'] ?> <?= $dieselStock['machine_id'] ?>"
oninput="consumptionChange(this)"
contenteditable="true">
<?= $dieselStock['consumption'] ?? " " ?>
</td>
<td class="celda_normal runningHours"
data-id="<?= $dieselStock['date'] ?> <?= $dieselStock['machine_id'] ?>"
>
<?= $dieselStock['running_hours'] ?? " " ?>
</td>
<td class="celda_normal mileage"
data-id="<?= $dieselStock['date'] ?> <?= $dieselStock['machine_id'] ?>">
<?= $dieselStock['mileage'] ?? " " ?>
</td>
<?php
//before closing inner loop we just adding the total values inside it
$machineId = $dieselStock['machine_id']; // Unique identifier
// Initialize the material entry if it doesn't exist
if (!isset($summary["machineId_$machineId"])) {
$summary["machineId_$machineId"] = [
'opening_reading' => "-" ,
'closing_reading' => 0 ,
'filling_diesel' => 0 ,
'consumption' => 0 ,
'running_hours' => 0 ,
'mileage' => 0 ,
];
}
// Sum values for the material
$summary["machineId_$machineId"]['opening_reading'] = 0 ;
$summary["machineId_$machineId"]['closing_reading'] = is_numeric($dieselStock['closing_reading']) ? $dieselStock['closing_reading'] : 0 ;
$summary["machineId_$machineId"]['filling_diesel'] += is_numeric($dieselStock['filling_diesel']) ? $dieselStock['filling_diesel'] : 0 ;
$summary["machineId_$machineId"]['consumption'] += is_numeric($dieselStock['consumption']) ? $dieselStock['consumption'] : 0 ;
$summary["machineId_$machineId"]['running_hours'] += is_numeric($dieselStock['running_hours']) ? $dieselStock['running_hours'] : 0 ;
$summary["machineId_$machineId"]['mileage'] += is_numeric($dieselStock['mileage']) ? $dieselStock['mileage'] : 0 ;
} ?>
</tr>
<?php
}
?>
<div class="table-responsive">
<table id="dieselStockDetailsTableSummaryId" class="fht-table table-striped">
<thead >
<th class="celda_encabezado_total" >Total Sum</th>
<th class="celda_encabezado_total"> Opening</th>
<th class="celda_encabezado_total"> Purchase Diesel</th>
<th class="celda_encabezado_total"> Filling Diesel</th>
<th class="celda_encabezado_total"> Balance Stock</th>
<?php foreach($summary as $each){
if(is_array($each)){
?>
<th class="celda_encabezado_total">Opening Reading</th>
<th class="celda_encabezado_total">Closing Reading</th>
<th class="celda_encabezado_total">Filling Diesel</th>
<th class="celda_encabezado_total">Consumption</th>
<th class="celda_encabezado_total">Running Hours</th>
<th class="celda_encabezado_total">Mileage</th>
<?php }
} ?>
</thead>
<tbody>
<tr>
<td style="background-color:rgb(189, 9, 6); color:#ffff;">
<b> Total </b>
</td>
<td><?= $summary['opening_diesel'] ?></td>
<td><?= $summary['totalPurchaseDiesel'] ?></td>
<td><?= $summary['totalFillingDiesel'] ?></td>
<td><?= $summary['balanceStock'] ?></td>
<?php foreach($summary as $each){
if(is_array($each)){
?>
<td class="celda_normal "><?=$each['opening_reading']?> </td>
<td class="celda_normal "><?=$each['closing_reading']?></td>
<td class="celda_normal "><?=$each['filling_diesel']?></td>
<td class="celda_normal "><?=$each['consumption']?></td>
<td class="celda_normal "><?=$each['running_hours']?></td>
<td class="celda_normal "><?=$each['mileage']?></td>
<?php
}
}
?>
</tr>
</tbody>
</table>
</div>
<!-- this else condition work if groupeddieselStockDetails 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 openingTD"
data-id="<?= $dateInMonth?>"
<?php if ($datesInMonthIndex == 0) { ?>
oninput="openingTDStockChange(this)"
contenteditable="true"
<?php } ?>
><?= $previousMonthTotalDieselMachineStockDetails[0]['balance_stock'] !=0 ?? " " ?></td>
<td class="celda_normal purchaseTD"
data-id="<?= $dateInMonth ?>"
oninput="purchaseTDStockChange(this)"
contenteditable="true"> </td>
<td class="celda_normal fillingTD"
data-id="<?= $dateInMonth ?>"> </td>
<td class="celda_normal balanceTD"
data-id="<?= $dateInMonth ?>"
><?= $previousMonthTotalDieselMachineStockDetails[0]['balance_stock'] !=0 ?? " " ?></td>
<?php
foreach ($dieselMachines as $dieselMachineIndex => $dieselMachine) {
?>
<!-- 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 ?> <?= $dieselMachine['id'] ?>">
<?= $dieselMachine['id'] ?>
</td>
<td class="celda_normal openingReading"
data-id="<?= $dateInMonth ?> <?= $dieselMachine['id'] ?>"
<?php if ($datesInMonthIndex == 0) { ?>
oninput="openingReadingChange(this)"
contenteditable="true"
<?php } ?>
><?= $previousMonthDieselMachineStockDetails[$dieselMachineIndex]['closing_reading'] ?? ' ' ?>
</td>
<td class="celda_normal closingReading"
data-id="<?= $dateInMonth ?> <?= $dieselMachine['id'] ?>"
oninput="closingReadingChange(this)"
contenteditable="true"><?= $previousMonthDieselMachineStockDetails[$dieselMachineIndex]['closing_reading'] ?? ' ' ?>
</td>
<td class="celda_normal fillingDiesel"
data-id="<?= $dateInMonth ?> <?= $dieselMachine['id'] ?>"
oninput="fillingReadingChange(this)"
contenteditable="true"> </td>
<td class="celda_normal consumption"
data-id="<?= $dateInMonth ?> <?= $dieselMachine['id'] ?>"
oninput="consumptionChange(this)"
contenteditable="true"> </td>
<td class="celda_normal runningHours"
data-id="<?= $dateInMonth ?> <?= $dieselMachine['id'] ?>"> </td>
<td class="celda_normal mileage"
data-id="<?= $dateInMonth ?> <?= $dieselMachine['id'] ?>"> </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="dieselDropdown">Select Bag</label>
<select id="dieselDropdown" class="form-control">
<option value="">Select Diesel</option>
<?php foreach ($dieselMachines as $dieselMachine): ?>
<option value=" <?= $dieselMachine['machine_name'] ?>">
<?= $dieselMachine['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="navigateToDiesel()">Navigate</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</div>
<script>
function navigateToDiesel() {
$('.close').click();
const dieselDropdown = document.getElementById('dieselDropdown');
const dateDropdown = document.getElementById('dateDropdown');
const selectedDiesel = dieselDropdown.value;
const selectedDate = dateDropdown.value;
const table = document.getElementById('dieselStockDetailsTableId');
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()) === selectedDiesel.trim()) {
targetColumnIndex = index;
return;
}
});
if (targetColumnIndex === -1) {
console.log("Diesel 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;
}
console.log(targetColumnIndex);
console.log(targetRow);
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 updateDieselMachineDetails = tableToJson();
alert('Updation may take a while, And we appreciate your patience..!!');
$('#loader').show();
$.ajax({
data: {
updateDieselMachineDetails
},
type: "POST",
url: "<?php echo base_url() ?>updateDieselMachineDetails",
success: function(data) {
if (data) {
$('#loader').hide();
console.log(data);
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 = $('#dieselStockDetailsTableId');
const summaryRowPart = [] ;
const readingsRowPart = [];
const allRow = [] ;
//this table to json is customized for dieselStock not for all
table.find('tbody tr').each(function(index) {
const rowCells = $(this).find('td');
console.log(rowCells.length);
const date = rowCells.eq(0).text().trim();
const formattedDate = `${date.split('-')[2]}-${date.split('-')[1]}-${date.split('-')[0]}`;
const summaryRowData = {
date: formattedDate,
opening_stock: rowCells.eq(1).text().trim(),
purchase_diesel: rowCells.eq(2).text().trim(),
total_filling_diesel: rowCells.eq(3).text().trim(),
balance_stock: rowCells.eq(4).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 = 5 ; i < rowCells.length; i += 8) {
const readingsRowData = {
date: rowCells.eq(i).text().trim(),
machine_id: rowCells.eq(i + 1).text().trim(),
opening_reading: rowCells.eq(i + 2).text().trim(),
closing_reading: rowCells.eq(i + 3).text().trim(),
filling_diesel: rowCells.eq(i + 4).text().trim(),
consumption: rowCells.eq(i + 5).text().trim(),
running_hours: rowCells.eq(i + 6).text().trim(),
mileage: rowCells.eq(i + 7).text().trim()
};
readingsRowPart.push(readingsRowData);
}
});
allRow.push(summaryRowPart ,readingsRowPart )
return JSON.stringify(allRow, null, 2);
}
</script>
<script>
function openingReadingChange(tdElement) {
try {
let dataId = tdElement.getAttribute('data-id');
let [date, machine_id] = tdElement.getAttribute('data-id').split(' ');
let openingReading = validateInput(tdElement.innerText);
let closingReading = validateInput(document.querySelector(`td.closingReading[data-id="${dataId}"]`).innerText);
let consumption = validateInput(document.querySelector(`td.consumption[data-id="${dataId}"]`).innerText);
let runningHours = Math.abs(Number(closingReading) - Number(openingReading));
let mileage = consumption == 0 || runningHours == 0 ? 0 : (Number(consumption) / Number(runningHours)).toFixed(2) ;
document.querySelector(`td.runningHours[data-id="${dataId}"]`).innerText = runningHours;
document.querySelector(`td.mileage[data-id="${dataId}"]`).innerText = mileage;
updateStockValue(date,machine_id,closingReading)
} catch (error) {
console.error("There is an error updating stock ..!!" + error);
}
}
function closingReadingChange(tdElement) {
try {
let dataId = tdElement.getAttribute('data-id');
let [date, machine_id] = tdElement.getAttribute('data-id').split(' ');
let closingReading = validateInput(tdElement.innerText);
let openingReading = validateInput(document.querySelector(`td.openingReading[data-id="${dataId}"]`).innerText);
let consumption = validateInput(document.querySelector(`td.consumption[data-id="${dataId}"]`).innerText);
let runningHours = Math.abs(Number(closingReading) - Number(openingReading));
let mileage = consumption == 0 || runningHours == 0 ? 0 : (Number(consumption) / Number(runningHours)).toFixed(2) ;
document.querySelector(`td.runningHours[data-id="${dataId}"]`).innerText = runningHours;
document.querySelector(`td.mileage[data-id="${dataId}"]`).innerText = mileage;
updateStockValue(date, machine_id, closingReading)
} catch (error) {
console.error("There is an error updating stock ..!!" + error);
}
}
function consumptionChange(tdElement) {
try {
let dataId = tdElement.getAttribute('data-id');
let [date, machine_id] = tdElement.getAttribute('data-id').split(' ');
let closingReading = validateInput(document.querySelector(`td.closingReading[data-id="${dataId}"]`).innerText);
let openingReading = validateInput(document.querySelector(`td.openingReading[data-id="${dataId}"]`).innerText);
let consumption = validateInput(tdElement.innerText);
let runningHours = Math.abs(Number(closingReading) - Number(openingReading));
let mileage = consumption == 0 || runningHours == 0 ? 0 : (Number(consumption) / Number(runningHours)).toFixed(2) ;
document.querySelector(`td.runningHours[data-id="${dataId}"]`).innerText = runningHours;
document.querySelector(`td.mileage[data-id="${dataId}"]`).innerText = mileage;
updateStockValue(date, machine_id, closingReading)
} catch (error) {
console.error("There is an error updating stock ..!!" + error);
}
}
function fillingReadingChange(tdElement) {
try {
let dataId = tdElement.getAttribute('data-id');
let [date,machine_id] = tdElement.getAttribute('data-id').split(" ");
let fillingDiesel = tdElement.innerText;
let totalFillingDiesel = 0 ;
let machines = <?= json_encode($dieselMachines) ?> ;
machines.forEach((each)=>{
totalFillingDiesel += Number(document.querySelector(`td.fillingDiesel[data-id="${date} ${each.id}"]`).innerText
)});
document.querySelector(`td.fillingTD[data-id="${date}"]`).innerText = totalFillingDiesel;
let openingTD = validateInput(document.querySelector(`td.openingTD[data-id="${date}"]`).innerText);
let purchaseTD = validateInput(document.querySelector(`td.purchaseTD[data-id="${date}"]`).innerText);
let balanceTD = (Number(openingTD) + Number(purchaseTD)) - Number(totalFillingDiesel) ;
document.querySelector(`td.balanceTD[data-id="${date}"]`).innerText = balanceTD ;
updateTotalStockValue(date, balanceTD) ;
} catch (error) {
console.error("There is an error updating stock ..!!" + error);
}
}
function openingTDStockChange(tdElement){
try {
let dataId = tdElement.getAttribute('data-id');
let openingTD = validateInput(tdElement.innerText);
let purchaseTD = validateInput(document.querySelector(`td.purchaseTD[data-id="${dataId}"]`).innerText);
let fillingTD = validateInput(document.querySelector(`td.fillingTD[data-id="${dataId}"]`).innerText);
let balanceTD = (Number(openingTD) + Number(purchaseTD)) - Number(fillingTD) ;
document.querySelector(`td.balanceTD[data-id="${dataId}"]`).innerText = balanceTD ;
updateTotalStockValue(dataId , balanceTD)
} catch (error) {
console.error("There is an error updating stock ..!!" + error);
}
}
function purchaseTDStockChange(tdElement){
try {
let dataId = tdElement.getAttribute('data-id');
let purchaseTD = validateInput(tdElement.innerText);
let openingTD = validateInput(document.querySelector(`td.openingTD[data-id="${dataId}"]`).innerText);
let fillingTD = validateInput(document.querySelector(`td.fillingTD[data-id="${dataId}"]`).innerText);
let balanceTD = validateInput(document.querySelector(`td.balanceTD[data-id="${dataId}"]`).innerText);
balanceTD = (Number(openingTD) + Number(purchaseTD)) - Number(fillingTD) ;
document.querySelector(`td.balanceTD[data-id="${dataId}"]`).innerText = balanceTD ;
updateTotalStockValue(dataId, balanceTD) ;
} 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, closingReading) {
const table = $('#dieselStockDetailsTableId');
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)) {
//other records present after it
/**this data-id ,openingStock ,used stock , current Balance Stock
is for changing subsequent dates not the earlier one **/
let dataId = `${otherDate} ${machine_id}`;
document.querySelector(`td.openingReading[data-id="${dataId}"]`).innerText = closingReading == 0 ? " " : closingReading ;
let openingReading = validateInput(document.querySelector(`td.openingReading[data-id="${dataId}"]`).innerText);
let runningHours = validateInput(document.querySelector(`td.runningHours[data-id="${dataId}"]`).innerText);
closingReading = Math.abs(Number(openingReading) + Number(runningHours));
let consumption = validateInput(document.querySelector(`td.consumption[data-id="${dataId}"]`).innerText);
runningHours = Math.abs(Number(closingReading) - Number(openingReading));
let mileage = consumption == 0 || runningHours == 0 ? 0 : (Number(consumption) / Number(runningHours)).toFixed(2) ;
document.querySelector(`td.runningHours[data-id="${dataId}"]`).innerText = runningHours == 0 ? " " : runningHours ;
document.querySelector(`td.mileage[data-id="${dataId}"]`).innerText = mileage == 0 ? " " : mileage ;
document.querySelector(`td.closingReading[data-id="${dataId}"]`).innerText = closingReading == 0 ? " " : closingReading ;
}
})
}
function updateTotalStockValue(date, balanceTD){
const table = $('#dieselStockDetailsTableId');
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)) { //other records present after it
/**this data-id ,openingStock ,used stock , current Balance Stock
is for changing subsequent dates not the earlier one **/
let dataId = `${otherDate}`;
document.querySelector(`td.openingTD[data-id="${dataId}"]`).innerText = balanceTD;
let openingTD = document.querySelector(`td.openingTD[data-id="${dataId}"]`).innerText;
let purchaseTD = document.querySelector(`td.purchaseTD[data-id="${dataId}"]`).innerText ;
let fillingTD = document.querySelector(`td.fillingTD[data-id="${dataId}"]`).innerText ;
balanceTD = (Number(openingTD) + Number(purchaseTD)) - Number(fillingTD) ;
document.querySelector(`td.balanceTD[data-id="${dataId}"]`).innerText = balanceTD ;
}
})
}
</script>