<?php
session_start();
include 'load.php';
//$user_id = $_SESSION["user_id"];
$user = $_GET['user'];
$month = $_GET['month'];

$wdaysinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM working_days WHERE working_days_id='$month'"));
$workingmonth = $wdaysinfo['year_and_month'];
$days = $wdaysinfo['days'];
$date = new DateTime($workingmonth . '-01'); 
$endDate = new DateTime($workingmonth . '-01'); 
$endDate->modify('last day of this month'); 

$interval = new DateInterval('P1D'); 
$period = new DatePeriod($date, $interval, $endDate->modify('+1 day')); 

// foreach ($period as $day) {
//     echo $day->format('Y-m-d') . "\n"; 
//}
$startOfMonth = $workingmonth . '-01 00:00:00';
$endOfMonth = date("Y-m-t 23:59:59", strtotime($startOfMonth));


$row = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileusers WHERE user_id='$user'"));
$c = 1;
       
                $userid = $row['user_id'];
                $basicsalary = $row['basicsalary'];
                $dailyworkinhours = $row['dailyworkinhours'];
                $othourrate = $row['othourrate'];
                $wh = $days * $dailyworkinhours;
                $hoursarlary = $basicsalary / $wh;
                $normalworkinghours = 0;
                $totalHours = 0;
                $totalWorkingSeconds = 0;
                $othours = 0;
                foreach ($period as $day) {
                         $dbdate = $day->format('Y-m-d'); 
                         $dailyhours = 0;
                         if(mysqli_num_rows(mysqli_query($conn, "SELECT * FROM in_out_time WHERE user_id='$userid' AND DATE(date_time)='$dbdate'"))==2){
                           $inoutinfo1 =  mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM in_out_time WHERE user_id='$userid' AND in_or_out='0' AND DATE(date_time)='$dbdate'"));
                           $inoutinfo2 =  mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM in_out_time WHERE user_id='$userid' AND in_or_out='1' AND DATE(date_time)='$dbdate'"));
                           $inTime = new DateTime($inoutinfo1['date_time']);
                           $outTime = new DateTime($inoutinfo2['date_time']);
                           $interval = $inTime->diff($outTime);

                          
                           $seconds = ($interval->h * 3600) + ($interval->i * 60) + $interval->s;
                           $dailyhours = floor($seconds / 3600);
                           if($dailyhours > $dailyworkinhours){
                                $othours += $dailyhours -  $dailyworkinhours;
                           }
                                $totalWorkingSeconds += $seconds;
                                $totalHours = floor($totalWorkingSeconds / 3600);
                           
                           
                         }
                }
        

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Invoice</title>
    <style>
    @page {
        size: 10.4cm 10cm landscape;
        font-size: 15px;
    }

    body {
        font-family: Arial, sans-serif;
        margin: 0;
        padding: 0;
    }

    .container {
        width: 80mm;
        margin: 0 auto;
        padding: 10px;
        box-sizing: border-box;
    }

    table {
        width: 100%;
        border-collapse: collapse;
    }

    tbody{
        td{
            font-size: 15px;
        }
    }
    tfoot{
        td{
            font-size: 15px;
        }
    }
    th,
    td {
        padding: 5px;
        text-align: left;
        font-size: 10px;

    }

    img {
        width: 80px;
        height: auto;
        margin-right: 10px;
    }

    p {
        font-size: 12px;
        margin: 0;
    }

    .center {
        text-align: center;
    }
    </style>
</head>

<body>
    <div class="container">
        <div style="width:100%;">
            <div style="text-align: center;">
                <b>
                    <p style="font-size:24px;">Payroll</p>
                    <p style="font-size:14px;">Mirigama/Balangoda</p>
                </b>
                <p style="margin: 0;">Tel : 071 513 2796</p>
            </div>
        </div>
       
        <p style="margin-top: 10px; margin-bottom:5px;"><strong>User: <?php echo $row['fullname']; ?></strong></p>
        <p style="margin-bottom: 10px;">Month: <?php echo $wdaysinfo['year_and_month']; ?></p>
       
        <?php 
           $Normalhours =  $totalHours - $othours;
           $payments = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount FROM payments_and_deductions WHERE payments_or_deductions='1' AND user_Id='$userid' AND added_date >= '$startOfMonth' AND added_date <= '$endOfMonth'")); 
           $deductions = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount FROM payments_and_deductions WHERE payments_or_deductions='2' AND user_Id='$userid' AND added_date >= '$startOfMonth' AND added_date <= '$endOfMonth'"));
           $ot = $othours*$row['othourrate'];
           $commiision = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(admin_commiision) as admin_commiision FROM mobileorders WHERE admin_id='$userid' AND create_at >= '$startOfMonth' AND create_at <= '$endOfMonth'"));
        
           $normalworkinghours = $totalHours - $othours;
           $sal =  $hoursarlary * $normalworkinghours ;
           $sal = $sal + $payments['amount'];
           $sal = $sal - $deductions['amount'];
           $sal = $sal + $commiision['admin_commiision'];
           
         
        ?>
        <table>
            <thead>
                
                
            </thead>
            <tbody>
            <tr><td colspan='4'><span style="text-align: center; font-size:14px; font-weight:600;">Working Hours:</span></td></tr>
                <?php
		                 echo "<tr>";
						echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>Normal Working Hours</td>";
						echo "<td style='text-align: right;'>". $Normalhours."</td>";
						echo "</tr>";
                        echo "<tr>";
						echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>OT Hours</td>";
						echo "<td style='text-align: right;'>".$othours."</td>";
						echo "</tr>";


                    

                        echo "<tr>";
                        echo "<td style='padding-left:25px;font-size: 12px;'></td>";
                        echo "<td style='padding-left:25px;font-size: 12px;' colspan='2'><strong>Total Working Hours</strong></td>";
                        echo "<td style='text-align: right;'>".$totalHours."</td>";
                        echo "</tr>";
                        echo "<tr>";
                        echo "<td style='padding-left:25px;font-size: 12px;'></td>";
                        echo "<td style='padding-left:25px;font-size: 12px;' colspan='2'><strong>OT</strong></td>";
                        echo "<td style='text-align: right;'>".number_format($ot, 2)."</td>";
                        echo "</tr>";
				?>

            <tr><td colspan='4'><span style="text-align: center; font-size:14px; font-weight:600;">Payments and Deductions:</span></td></tr>
            <?php
		                 echo "<tr>";
						echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>Payments</td>";
						echo "<td style='text-align: right;'>". number_format($payments['amount'], 2)."</td>";
						echo "</tr>";
                        echo "<tr>";
						echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>Deductions</td>";
						echo "<td style='text-align: right;'>".number_format($deductions['amount'], 2)."</td>";
						echo "</tr>";
                ?>
               <tr><td colspan='4'><span style="text-align: center; font-size:14px; font-weight:600;">Commiision:</span></td></tr>
               <?php
		                 echo "<tr>";
						echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>Commiision</td>";
						echo "<td style='text-align: right;'>". number_format($commiision['admin_commiision'], 2)."</td>";
						echo "</tr>";

              ?>
              <tr><td colspan='4'><span style="text-align: center; font-size:14px; font-weight:600;">Salary Total:</span></td></tr>
              <?php
		                 echo "<tr>";
						echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>Salary</td>";
						echo "<td style='text-align: right;'>".number_format($sal, 2)."</td>";
						echo "</tr>";

              ?>
            </tbody>
           
        </table>

        
    </div>
    <!-- JavaScript to trigger print functionality -->
    <script>
    window.onload = function() {
        window.print();
    };
    </script>
</body>

</html>

<?php
$conn->close();
?>
<script>
function redirectToPosPanel() {
    window.location.href = "genarate-salary-list.php";
}

window.onafterprint = redirectToPosPanel;
window.onload = function() {
    window.print();
    setTimeout(redirectToPosPanel, 2000);
};
</script>