<?php
ob_start();
// require("includes/check_auth_user.php");
include('includes/config.php');
require("includes/dbconnect.php");
include('includes/function.php');

$formname = 'Shipment Update Status';
$maindb = 'shipment';

// $admin = $_SESSION["admin_" . $dbname];
// $zdb = $_SESSION["zdbyear_" . $dbname];

$year = mysqli_query($conn, "SELECT * FROM $dbname.`compyear`  order by `lastdate` DESC LIMIT 0,1") OR die(mysqli_error($conn));
$fyear = mysqli_fetch_array($year);
$myyear = $fyear['finyear'];
$zroot1 = left($dbname, -4);
$zdb = $zroot1 . '1_' . $myyear;

mysqli_select_db($conn, $zdb);

$today = date("Y-m-d");

$result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE delvdate = '0000-00-00' ") or die(mysqli_error($conn));
$get = mysqli_fetch_array($result);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_array($result)) {
        $vouchno = $row['vouchno'];
        $arrvport = $row['arrvport'];
        $status = $row['status'];

        $newStatus = $status;

        if (!empty($arrvport) && $arrvport != "0000-00-00" && $arrvport <= $today) {
            $newStatus = 7;
        }

        if ($newStatus != $status) {
              mysqli_query($conn, "UPDATE `$maindb` SET `status`='$newStatus' WHERE `vouchno` = '$vouchno' ") or die(mysqli_error($conn));
        }
    }
}
?>