Changes between Version 26 and Version 27 of Docs/825gen2/Dev/Networking/PHP


Ignore:
Timestamp:
11/12/25 08:04:32 (31 hours ago)
Author:
Don Wilson
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Docs/825gen2/Dev/Networking/PHP

    v26 v27  
    161161[[Image(825webpage_ids_captured_images.png)]]
    162162
    163 
    164 
    165 
    166 
     163This example report has date range selections and produces a material summary or detail:
     164
     165{{{#!php
     166<?php
     167session_start();
     168if(!isset($_SESSION['user']) || empty($_SESSION['user'])) {
     169    header("location: ../login.php"); exit();
     170}
     171$type = $_GET['rpttype'];
     172if(!$type) {
     173    $type = "sum";
     174}
     175?>
     176<!DOCTYPE html>
     177<html lang="en">
     178<script>
     179function toggleInputs() {
     180    const selectElement = document.getElementById("rangeSelect");
     181    const selectedValue = selectElement.value;
     182
     183    const customInputs = document.getElementById("customInputs");
     184
     185    // Hide all input containers initially
     186    customInputs.style.display = "none";
     187
     188    // Show the relevant input container based on selection
     189    if (selectedValue === "custom") {
     190        customInputs.style.display = "block";
     191    }
     192}
     193</script>
     194<head>
     195<?php
     196if($type == 'det') {
     197    echo '<title>Product Detail</title>';
     198} else {
     199    echo '<title>Product Summary</title>';
     200}
     201?>
     202<style type="text/css" media="screen">
     203#tblSum {
     204 tr td,th {
     205    text-align: right;
     206    padding: 0 15px;
     207}
     208 td:nth-child(1) { text-align: left; }
     209 th:nth-child(1) { text-align: left; }
     210}
     211#tblDet {
     212tr td,th {
     213    text-align: left;
     214    padding: 0 15px;
     215}
     216td:nth-child(1) { text-align: right; }
     217td:nth-child(7) { text-align: right; }
     218th:nth-child(4) { text-align: right; }
     219}
     220</style>
     221</head>
     222<body>
     223
     224<?php
     225
     226$ranges = [
     227['id' => 'today', 'name' => 'Today'],
     228['id' => 'tweek', 'name' => 'This week'],
     229['id' => 'tmonth', 'name' => 'This month'],
     230['id' => 'tyear', 'name' => 'This year'],
     231['id' => 'lweek', 'name' => 'Last week'],
     232['id' => 'lmonth', 'name' => 'Last month'],
     233['id' => 'lyear', 'name' => 'Last year'],
     234['id' => 'custom', 'name' => 'Custom'],
     235];
     236
     237$types = [
     238['id' => 'sum', 'name' => 'Summary'],
     239['id' => 'det', 'name' => 'Detail'],
     240];
     241
     242
     243$jsonFile = '/mnt/nand/apps/ids/db.json';
     244$jsonContent = file_get_contents($jsonFile);
     245$data = json_decode($jsonContent, true);
     246if (!$data) {
     247    echo "ID Storage database not configured";
     248} else {
     249   if ($data['dbtype'] != 'mysql') {
     250      echo "ID Storage database type must by MySQL for reporting";
     251   } else {
     252
     253       $range = $_GET['range'];
     254        if(!$range) {
     255            $range = "today";
     256        }
     257
     258        echo '<form action="/reports/idsprod.php" method="GET">';
     259
     260        echo '<label>Report Type: </label><select name="rpttype" id="typeSelect">';
     261        foreach ($types as $t):
     262            echo '<option value="' . $t['id'] . '"';
     263        if($t['id'] == $type) {
     264            echo " selected";
     265        }
     266        echo '>' . $t['name'];
     267        echo '</option>';
     268        endforeach;
     269        echo '</select>';
     270
     271        echo '<label> Date Range: </label><select name="range" id="rangeSelect" onchange="toggleInputs()">';
     272        foreach ($ranges as $r):
     273            echo '<option value="' . $r['id'] . '"';
     274            if($r['id'] == $range) {
     275                echo " selected";
     276            }
     277            echo '>' . $r['name'];
     278            echo '</option>';
     279        endforeach;
     280        echo '</select>';
     281
     282        $dateFrom = $_GET['from'];
     283        $dateTo = $_GET['to'];
     284
     285        if($range != "custom") {
     286            echo '<div id="customInputs" style="display: none;">';
     287        } else {
     288            echo '<div id="customInputs">';
     289        }
     290        echo '<label for="from">From: </label>';
     291        echo '<input type="date" name="from" id="from" value="' . $dateFrom . '">';
     292        echo '<label for="to"> To: </label>';
     293        echo '<input type="date" name="to" id="to" value="' . $dateTo . '">';
     294        echo '</div>';
     295        echo ' ';
     296        echo '<input type="submit" value="Submit">';
     297        echo '</form>';
     298
     299       $tzpath = realpath("/etc/localtime");
     300       if(substr($tzpath, 0, 20) == '/usr/share/zoneinfo/') {
     301           $tz = substr($tzpath, 20);
     302           date_default_timezone_set($tz);
     303       }
     304       // echo "Date/time " . date('Y-m-d H:i:s');
     305
     306        $conn = mysqli_connect($data['server'], $data['user'], $data['passw'], $data['database']);
     307        if(mysqli_connect_error()) {
     308            echo 'Server ' . $data['server'] . ' Connection error: ' . mysqli_connect_error();
     309        } else {
     310            //echo "range " . $range;
     311
     312            $units = "";
     313
     314            $dateTo = "";
     315            switch($range) {
     316                case "today":
     317                    $dateFrom = date('Y-m-d');
     318                    $dateTo = date('Y-m-d');
     319                    break;
     320
     321                case "tweek":
     322                    $dayOfWeek = date('w');
     323                    $d = new DateTime();
     324                    if($dayOfWeek != 0) {
     325                        $d->modify('-' . strval($dayOfWeek) . ' days');
     326                    }
     327                    $dateFrom = date('Y-m-d', $d->GetTimestamp());
     328                    break;
     329
     330                case "tmonth":
     331                    $dateFrom = date('Y-m') . '-01';
     332                    break;
     333
     334                case "tyear":
     335                    $dateFrom = date('Y') . '-01-01';
     336                    break;
     337
     338                case "lweek":
     339                    $dayOfWeek = date('w');
     340                    $d = new DateTime();
     341                    $d->modify('-' . strval($dayOfWeek + 7) . ' days');
     342
     343                    $dateFrom = date('Y-m-d', $d->GetTimestamp());
     344                    $d->modify('+ 6 days');
     345                    $dateTo = date('Y-m-d', $d->GetTimestamp());
     346                    //echo 'Day of week ' . $dayOfWeek . ' Last week ' . $dateFrom . ' - ' . $dateTo . '<br>';
     347                    break;
     348
     349                case "lmonth":
     350                    $d1 = strtotime("-1 months");
     351                     $dateFrom = date("Y-m", $d1) . "-01";
     352                     $dateTo = date("Y-m-t", $d1);
     353                    break;
     354
     355                case "lyear":
     356                    $d1 = strtotime("-1 years");
     357                    $dateFrom = date("Y", $d1) . "-01-01";
     358                    $dateTo = date("Y", $d1) . "-12-31";
     359                    break;
     360
     361                case "custom":
     362                    $dateFrom = $_GET['from'];
     363                    $dateTo = $_GET['to'];
     364                    break;
     365            }
     366            //echo "where ". $where;
     367
     368            if($dateFrom == $dateTo) {
     369                $where = "WHERE datetm >= '" . $dateFrom . " 00:00:00' AND datetm <= '". $dateTo . " 23:59:59'";
     370            } else {
     371                if($dateTo != "") {
     372                    $where = "WHERE datetm >= '" . $dateFrom . " 00:00:00' AND datetm <= '". $dateTo . " 23:59:59'";
     373                } else {
     374                    $where = "WHERE datetm >= '" . $dateFrom . " 00:00:00'";
     375                }
     376            }
     377
     378            if($type == "sum") {
     379                echo "<h2>Product Summary</h2>";
     380            } else {
     381                echo "<h2>Product Detail</h2>";
     382            }
     383
     384        if($dateFrom == $dateTo) {
     385            echo "Date: " . $dateFrom;
     386        } else {
     387            echo "Date Range: " . $dateFrom . " - ";
     388            if($dateTo != "") {
     389                echo $dateTo;
     390            }
     391        }
     392         echo "<br><br>";
     393
     394            if($type == "sum") {
     395          $sql = 'SELECT prod, units, COUNT(*) AS ProdCount, SUM(net) AS NetTotal FROM trans ' . $where . ' GROUP BY prod;';
     396            $result = $conn->query($sql);
     397            if(!$result) {
     398                echo "query error ";
     399            }
     400            if($result->num_rows > 0) {
     401                $totCount = 0;
     402                $totNet = 0;
     403                echo '<table id="tblSum"><tr><th>Product</th><th>Loads</th><th>Net weight total</th></tr>';
     404                while($row = $result->fetch_assoc()) {
     405                    $prod = $row['prod'];
     406                    $units = $row['units'];
     407
     408                    $count = $row['ProdCount'];
     409                    $netTotal = $row['NetTotal'];
     410                    echo '<tr><td>' . $prod . '</td><td>' . $count . '</td><td>' . $netTotal .' ' . $units . '</td></tr>';
     411                    $totCount += $count;
     412                    $totNet += $netTotal;
     413                }
     414                echo '<tr><th>Total</th><th>' . $totCount . '</th><th>' . $totNet .' ' . $units . '</th></tr>';
     415                echo '</table>';
     416            }
     417        }
     418         else {
     419
     420                $sql = 'SELECT * FROM trans ' . $where . ' ORDER BY prod,datetm;';
     421                $result = $conn->query($sql);
     422                if(!$result) {
     423                    echo "query error ";
     424                }
     425                if($result->num_rows > 0) {
     426                    $prodCnt = 0;
     427                    $rptCnt = 0;
     428                    $prodNetWt = 0;
     429                    $rptNetWt = 0;
     430                    $rowCnt = 0;
     431                    $prvProd = "";
     432                    echo '<table id="tblDet"><tr><th>Tran Num</th><th>Date/time</th><th>Vehicle</th><th>Custom</th><th>Job</th><th>Product</th><th>Net Wt</th></tr>';
     433                    while($row = $result->fetch_assoc()) {
     434                        $tkt_num = $row['tkt_num'];
     435                        $datetime = $row['datetm'];
     436                        $veh = $row['truck'];
     437                        $prod = $row['prod'];
     438                        $cust = $row['cust'];
     439                        $job = $row['job'];
     440                        //$gross = $row['gross'];
     441                        //$tare = $row['tare'];
     442                        $net = $row['net'];
     443                        $units = $row['units'];
     444
     445                        if($rowCnt == 0) {
     446                            $prvProd = $prod;
     447                        }
     448
     449                        if($prod != $prvProd) {
     450                            echo '<tr><th>Subtotal</th><th colspan="4"> Loads ' . $prodCnt . '</th><th>' . $prvProd. '</th><th>' . $prodNetWt .' ' . $units . '</th></tr>';
     451                           // echo '<tr><th>' . $prvProd. '</th><th colspan="4">Total ' . $prodCnt . ' loads</th><th>' . $prodNetWt . '</th><th></th></tr>';
     452                            $prodCnt = 0;
     453                            $prodNetWt = 0;
     454
     455                            $prvProd = $prod;
     456                            echo '<tr><td colspan="6"> &nbsp;  </td></tr>';
     457                        }
     458
     459                        echo '<tr><td><a href="/reports/idstrannum.php?num=' . $tkt_num . '">' . $tkt_num . '</a></td><td>' . $datetime . '</td><td>'  . $veh . '</td><td>' . $cust .  '</td><td>' . $job . '</td><td>' . $prod . '</td><td>'. $net  .' ' . $units . '</td>';
     460                        echo '</tr>';
     461
     462                        $prodCnt++;
     463                        $rptCnt++;
     464
     465                        $prodNetWt += $net;
     466                        $rptNetWt += $net;
     467
     468                        $rowCnt++;
     469                    }
     470                    echo '<tr><th>Subtotal</th><th colspan="4"> Loads ' . $prodCnt . '</th><th>' . $prvProd. '</th><th>' . $prodNetWt . ' ' . $units .'</th></tr>';
     471                    echo '<tr><td colspan="6">&nbsp; </td></tr>';
     472                    echo '<tr><th>Total</th><th colspan="4">Loads ' . $rptCnt . '</th><th></th><th>' . $rptNetWt .' ' . $units . '</th><th></th></tr>';
     473                    echo '</table>';
     474
     475                }
     476
     477            }
     478        }
     479    }
     480}
     481
     482?>
     483</body>
     484</html>
     485
     486}}}
     487
     488
     489
     490