<?php
include'connection.php';

// ********************** Upload plate to tables *************************
$sepa=mysqli_query($conn, "SELECT `Number`, `Plate` FROM `vehicles` WHERE `Plate` != '' AND `Status`='0' GROUP BY `Plate` ORDER BY `Plate` ASC LIMIT 100");
while($repa=mysqli_fetch_assoc($sepa)){
    $pla=$repa['Plate'];
    $veh=$repa['Number'];
    
$then=mysqli_query($conn, "UPDATE `repair` SET `Plate`='$pla' WHERE `Vehicle`='$veh' AND `Plate`!='$veh' ORDER BY `Number` DESC LIMIT 10");	

$then=mysqli_query($conn, "UPDATE `consumption` SET `Plate`='$pla' WHERE `Vehicle`='$veh' AND `Plate`!='$veh' ORDER BY `Number` DESC LIMIT 100");

$then=mysqli_query($conn, "UPDATE `income` SET `Plate`='$pla' WHERE `Vehicle`='$veh' AND `Plate`!='$veh' ORDER BY `Number` DESC LIMIT 10");
}

$then=mysqli_query($conn, "UPDATE `repair` SET `Type`='1' WHERE `Garage`='OTHER EXPENSE' ORDER BY `Number` DESC LIMIT 10");


// **************************** Upload income to trip *********************
$tripe=mysqli_query($conn, "SELECT `Number`, `Vehicle` FROM `trips` WHERE `Status`='0' AND `Income`='0' AND `Date`>='2025-01-01' ORDER BY RAND() LIMIT 30");
if($fripe=mysqli_num_rows($tripe)){
			while($ripe=mysqli_fetch_assoc($tripe)){
				$tri=$ripe['Number'];
				$veh=$ripe['Vehicle'];
				
	$incom=mysqli_query($conn, "SELECT SUM(`Amount`*`Rate`) AS 'Amon' FROM `income` WHERE `Trip`='$tri' AND `Status`='0' AND `Vehicle`='$veh'");
	if($finco=mysqli_num_rows($incom)){
	$rinco=mysqli_fetch_assoc($incom);
	    $inco=$rinco['Amon'];

$then=mysqli_query($conn, "UPDATE `trips` SET `Income`='$inco' WHERE `Number`='$tri' ORDER BY `Number` DESC LIMIT 1");	 
}
}
}



    $dano=str_replace('-', '', $Date);
           
        // ************************ Transport cashing **********************
                $cash=0;
    $setaca=mysqli_query($conn, "SELECT SUM(`Amount`*`Rate`) AS `Cash` FROM `payment` WHERE `Date`='$Date' AND `Pline`='CASH' AND `Status`='0' AND `Action`='TRANSPORT' AND `Amount`>'0' AND `Bname`=''");
	if($fetaca=mysqli_num_rows($setaca)){
		$reca=mysqli_fetch_assoc($setaca);
		    $cash=$reca['Cash'];
		    $bran="TRANSPORT";
            $desc="CASH TRANSPORT";
       
       if($cash){ 
    $so=mysqli_query($conn, "SELECT `Number` FROM `deposit` WHERE `Date`='$Date' AND `Item`='DEPOSIT' AND `Descri`='$desc' AND `Operation`='DEPOSIT' AND `Account`='30' AND `Source`='$bran' AND `Voucher`='$dano' ORDER BY `Number` ASC LIMIT 1");
    if($fo=mysqli_num_rows($so))
    $sso=mysqli_query($conn, "UPDATE `deposit` SET `Amount` = '$cash' WHERE `Date`='$Date' AND `Item`='DEPOSIT' AND `Descri`='$desc' AND `Operation`='DEPOSIT' AND `Account`='30' AND `Source`='$bran' AND `Voucher`='$dano' ORDER BY `Number` ASC LIMIT 1");
    else{
        $do=mysqli_query($conn, "SELECT *FROM `baccount` WHERE `Number`='30' ORDER BY `Number` ASC");
				$ro=mysqli_fetch_assoc($do);
					$bank=$ro['Account'];
					$rat=$ro['Rate'];
    $sso=mysqli_query($conn, "INSERT INTO `deposit` (`Number`, `Date`, `Time`, `User`, `Item`, `Refer`, `Amount`, `Customer`, `Operation`, `Status`, `Valid`, `Account`, `Descri`, `Client`, `Voucher`, `Source`, `Record`, `Branche`, `Bank`, `Rate`) VALUES (NULL, '$Date', '$Time', 'SYSTEM', 'DEPOSIT', 'x1', '$cash', 'From sales', 'DEPOSIT', '0', '0', '30', '$desc', '0', '$dano', '$bran', '0', '0', '$bank', '$rat')");
    }
	}
	}
	
	

$do=mysqli_query($conn, "SELECT `Number`, `Rate` FROM `repair` WHERE `Rate`!='0' AND `Currency`='' AND `Date`<='$Date' ORDER BY `Number` ASC LIMIT 30");
while($ro=mysqli_fetch_assoc($do)){
    
     $doi=mysqli_query($conn, "SELECT `Code` FROM `currency` WHERE `Rate`='".$ro['Rate']."' AND `Status`='0' ORDER BY `Number` ASC");
				$roi=mysqli_fetch_assoc($doi);
					
$so=mysqli_query($conn, "UPDATE `repair` SET `Currency`='".$roi['Code']."' WHERE `Number` = '".$ro['Number']."'");
}
		mysqli_close($conn);		
			?>