production: time in production date

This commit is contained in:
Michał Zieliński
2025-10-07 17:54:59 +02:00
parent 0e8227fc82
commit 4374f2cd65
3 changed files with 176 additions and 6 deletions

View File

@@ -106,6 +106,7 @@ $(document).ready(function () {
$("#deleteBtn").click(() => { removePositions(); });
$("#excelBtn").click(() => { exportExcel(); });
$("#pdfBtn").click(() => { exportPDF(); });
$("#changeDateBtn").click(() => { changeDate(); });
$("#rawMaterialsExcelBtn").click(() => { exportRawMaterialsExcel(); });
$("#productionBtn").click(() => { window.alert("In progress."); });
$("#createInsideOrder").click(createInsideOrder);
@@ -314,17 +315,31 @@ function saveProductionDate(id) {
time = '';
} else {
if (time.length === 5) {
// Sprawdź format HH:mm
const timeRegex = /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/;
const timeRegex = /^([0-5]?[0-9]|1[0-3]):[0-5][0-9]$|^14:[0-2][0-9]$|^14:30$/;
if (!timeRegex.test(time)) {
time = "06:00";
} else {
const timeParts = time.split(':');
const hours = parseInt(timeParts[0], 10);
const minutes = parseInt(timeParts[1], 10);
if (hours < 6 || (hours === 14 && minutes > 30) || hours > 14) {
time = "06:00";
}
}
} else if (time.length === 4) {
// Wstaw : pomiędzy i sprawdź format
time = time.substring(0, 2) + ":" + time.substring(2);
const timeRegex = /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/;
const timeRegex = /^([0-5]?[0-9]|1[0-3]):[0-5][0-9]$|^14:[0-2][0-9]$|^14:30$/;
if (!timeRegex.test(time)) {
time = "06:00";
} else {
const timeParts = time.split(':');
const hours = parseInt(timeParts[0], 10);
const minutes = parseInt(timeParts[1], 10);
if (hours < 6 || (hours === 14 && minutes > 30) || hours > 14) {
time = "06:00";
}
}
} else {
time = "06:00";
@@ -630,3 +645,42 @@ function updateTimeSummary() {
return formattedTime;
}
function changeDate() {
var ids = [];
$('input.allCheck:checkbox:checked').each(function () {
ids.push($(this).val());
});
if (ids.length === 0) {
alert('Wybierz pozycje.');
return;
}
let hours = prompt("Podaj ilość godzin:");
if (hours === null) {
return null; // no response
}
hours = parseFloat(hours);
if (hours === "") {
window.alert("Niepoprawna wartość (-100 : 100)");
return null;
}
const hoursNum = parseFloat(hours);
if (isNaN(hoursNum)) {
window.alert("Niepoprawna wartość (-100 : 100)");
return null;
}
if (hoursNum < -100 || hoursNum > 100) {
window.alert("Niepoprawna wartość (-100 : 100)");
return null;
}
$.ajax({
method: "get",
url: $(location).attr("href") + "&to_pdf=1&ajaxAction=updateProductionDate&ids=" + ids.join("|") + "&hours=" + hoursNum,
success: function () {
window.location.reload();
},
error: function() {
window.alert("Aktualizacja nie powiodła się.");
window.location.reload();
}
});
}

View File

@@ -53,6 +53,9 @@ if (!isset($_GET['ajaxAction'])) {
case 'getRawMaterials':
getRawMaterials($_POST['ids']);
break;
case 'updateProductionDate':
updateProductionDate($_GET['ids'], $_GET['hours']);
break;
}
}
function loadSchedulers($dateFrom = null, $dateTo = null, $ids = null)
@@ -293,7 +296,7 @@ function saveProductionDate($id, $date, $time)
if ($parsedDate == '1970-01-01') {
$db->query("UPDATE productionScheduler SET production_date=NULL WHERE id='$id'");
} else {
$parsetDateTime = date("Y-m-d H:i", strtotime($date. ' ' . $time));
$parsetDateTime = date("Y-m-d H:i", strtotime($date . ' ' . $time));
$db->query("UPDATE productionScheduler SET production_date='$parsetDateTime' WHERE id='$id'");
}
}
@@ -472,6 +475,7 @@ function generateUuidV4()
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
function exportExcelWYSIWYG($tableDataJson)
{
if (function_exists('from_html')) {
@@ -506,4 +510,115 @@ function exportExcelWYSIWYG($tableDataJson)
'fileContent' => base64_encode($xlsx_content),
'fileName' => 'harmonogram_produkcji.xlsx'
));
}
}
function updateProductionDate($ids, $hours) {
$db = $GLOBALS['db'];
$ids = explode("|", $ids);
foreach ($ids as $id) {
$res = $db->fetchByAssoc($db->query("SELECT production_date FROM productionScheduler WHERE id ='$id'"));
if ($res['production_date'] && $res['production_date'] != '0000-00-00 00:00:00') {
$newDate = calculateProductionDate($res['production_date'], $hours);
$db->query("UPDATE productionScheduler SET production_date='$newDate:00' WHERE id='$id'");
}
}
}
function calculateProductionDate($currentDateTime, $hoursToAdd) {
$WORK_START = 6 * 60;
$WORK_END = 14 * 60 + 30;
$date = new DateTime($currentDateTime);
$totalMinutesToAdd = $hoursToAdd * 60;
if ($totalMinutesToAdd > 0) {
while ($totalMinutesToAdd > 0) {
$dayOfWeek = $date->format('w');
if ($dayOfWeek == 0 || $dayOfWeek == 6) {
$date = getNextWorkDay($date);
continue;
}
$currentMinutes = $date->format('H') * 60 + $date->format('i');
if ($currentMinutes < $WORK_START) {
$date->setTime(6, 0, 0);
continue;
}
if ($currentMinutes >= $WORK_END) {
$date = getNextWorkDay($date);
continue;
}
$remainingMinutesToday = $WORK_END - $currentMinutes;
if ($totalMinutesToAdd <= $remainingMinutesToday) {
$date->modify('+' . $totalMinutesToAdd . ' minutes');
$totalMinutesToAdd = 0;
} else {
$totalMinutesToAdd -= $remainingMinutesToday;
$date = getNextWorkDay($date);
}
}
}
else if ($totalMinutesToAdd < 0) {
$totalMinutesToAdd = abs($totalMinutesToAdd);
while ($totalMinutesToAdd > 0) {
$dayOfWeek = $date->format('w');
if ($dayOfWeek == 0 || $dayOfWeek == 6) {
$date = getPreviousWorkDay($date);
continue;
}
$currentMinutes = $date->format('H') * 60 + $date->format('i');
if ($currentMinutes > $WORK_END) {
$date->setTime(14, 30, 0);
continue;
}
if ($currentMinutes <= $WORK_START) {
$date = getPreviousWorkDay($date);
continue;
}
$minutesFromStart = $currentMinutes - $WORK_START;
if ($totalMinutesToAdd <= $minutesFromStart) {
$date->modify('-' . $totalMinutesToAdd . ' minutes');
$totalMinutesToAdd = 0;
} else {
$totalMinutesToAdd -= $minutesFromStart;
$date = getPreviousWorkDay($date);
}
}
}
return $date->format('Y-m-d H:i');
}
function getNextWorkDay($date) {
$nextDay = clone $date;
$nextDay->modify('+1 day');
$nextDay->setTime(6, 0, 0);
while ($nextDay->format('w') == 0 || $nextDay->format('w') == 6) {
$nextDay->modify('+1 day');
}
return $nextDay;
}
function getPreviousWorkDay($date) {
$prevDay = clone $date;
$prevDay->modify('-1 day');
$prevDay->setTime(14, 30, 0);
while ($prevDay->format('w') == 0 || $prevDay->format('w') == 6) {
$prevDay->modify('-1 day');
}
return $prevDay;
}

View File

@@ -133,6 +133,7 @@
<input class="button" id="excelBtn" value="Excel" type="button">
<input class="button" id="pdfBtn" value="PDF" type="button">
<input class="button" value="Utwórz przyjęcie produkcyjne" type="button" id="createInsideOrder"/>
<input class="button" id="changeDateBtn" value="Aktualizuj datę produkcji" type="button">
<table id="allTable">
<thead>
<tr>