<?php
ini_set('display_errors', 0);


include_once("../inc/db_config.php");

date_default_timezone_set('Asia/Dhaka');
$today=date('Y-m-d');

$sql="UPDATE `events` as t1 inner join bookings t2 on (t1.temp_id = t2.id) SET t1.state = '1' where t1.date < '$today' AND (t1.state = 0 AND t2.status=1)";
$sqlqry = mysqli_query($connection, $sql); 



$settingsSQL = mysqli_query($connection, "SELECT * FROM `settings` where name='delivery_policy_day' ");
$settingsSQL = mysqli_fetch_object($settingsSQL);
									
$delivery_policy_day = $settingsSQL->value;	

$sql="UPDATE `events` SET `d_date`= DATE_ADD(date, INTERVAL $delivery_policy_day DAY) WHERE `d_date` is null";
$sqlqry = mysqli_query($connection, $sql);

$sqltab = "SELECT * FROM `events` where d_date_set=0 ";
$sqltab = mysqli_query($connection, $sqltab);
while ($list = mysqli_fetch_object($sqltab)) {
$temp_id = $list->temp_id;
$ev_id = $list->id;
$max_ev = mysqli_query($connection, "SELECT max(date) max_date FROM `events` where temp_id=$temp_id;");
$max_ev = mysqli_fetch_object($max_ev);
$evdate = $max_ev->max_date;

$new_d_date_vd=date('Y-m-d', strtotime($evdate. ' + '.$delivery_policy_day.' days'));




echo '</br> Event ID '.$ev_id.' ev_date '.$evdate.' new dl date '.$new_d_date_vd;

$sqlUpdate = "UPDATE events SET
  d_date_vd = '$new_d_date_vd',d_date_set='3'  WHERE
 id = '$ev_id'           
";
$sqlUpdate = mysqli_query($connection, $sqlUpdate);	



}

$sqltab = "SELECT * FROM `events` where d_date_ph_set=0 ";
$sqltab = mysqli_query($connection, $sqltab);
while ($list = mysqli_fetch_object($sqltab)) {
$temp_id = $list->temp_id;
$ev_id = $list->id;
$max_ev = mysqli_query($connection, "SELECT max(date) max_date FROM `events` where temp_id=$temp_id;");
$max_ev = mysqli_fetch_object($max_ev);
$evdate = $max_ev->max_date;

$new_d_date_ph=date('Y-m-d', strtotime($evdate. ' + '.$delivery_policy_day.' days'));




echo '</br> Event ID '.$ev_id.' ev_date '.$evdate.' new dl date '.$new_d_date_ph;

$sqlUpdate = "UPDATE events SET
  d_date = '$new_d_date_ph',d_date_ph_set='3'  WHERE
 id = '$ev_id'           
";
$sqlUpdate = mysqli_query($connection, $sqlUpdate);	



}

echo "done";

?>