<?php
if(!isset($_SESSION)) {  session_start(); }
ob_start();
if(!isset($_SESSION["user_id"])){header("location:../logout.php");}
$father = $_SESSION["user_id"];
require_once "../op/vat.php";
require_once "../magic/DataBase.php";
   $dataBase = new DataBase();
   $conn = $dataBase->getConnection();

if(isset($_POST['myday'])){
    $myday      = $_POST['myday'] ; 
    $first_day  = date('Y-m-01', strtotime($myday));
	$last_day 	= date('Y-m-t', strtotime($myday));
    $vat        = new VAT($conn);

    $sql = $conn->prepare("SELECT contract_id ,date_in , time_in FROM `contracts`
    WHERE date_in BETWEEN ? and ? ORDER BY date_in, time_in");
    $sql->bindParam(1, $first_day);
    $sql->bindParam(2, $last_day);
    $sql->execute();
    if ($sql->rowCount() > 0) {
        $no = 0;
    while ($row = $sql->fetch()) {
            $contract_id = $row['contract_id'];
            $end         =  $row['date_in'];
            //last vat invoice end
       $vatSQL = $conn->prepare("SELECT max(end) as vatEnd from vat where contract_id = ?");
       $vatSQL->bindParam(1,$contract_id);
       $vatSQL->execute();
    if($vatSQL->rowCount() > 0){
        $lastVat    = $vatSQL->fetch(PDO::FETCH_ASSOC);
        $vatEnd     = $lastVat["vatEnd"];
        if(empty($vatEnd)){
            $vat->createVat($contract_id,$end,$father);
            $no = $no + 1;
        }else {
                $vatEnd     = new DateTime($vatEnd);
                $end1       = new DateTime($end);
            if($vatEnd < $end1){
                $vat->createVat($contract_id,$end,$father);
                $no = $no + 1;
                }
        }
       }
    }
        echo "<div class='alert alert-success'>
        <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
        <b> تم إصدار عدد $no فاتورة ضريبية بنجاح </b>
    </div>";
    }else{
        echo "<div class='alert alert-danger'>
                  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                  <b>لا يوجد عقود بدون فواتير ضريبية</b>
              </div>";
    }
}
//asaf vat invoice
if(isset($_POST['asaf'])){
    $myday      = $_POST['asaf'] ; 
    $end 	    = date('Y-m-t', strtotime($myday));
    $first_day  = date('Y-m-01', strtotime($myday));
	$last_day 	= date('Y-m-t', strtotime($myday));
    $vat        = new VAT($conn);
    $msg = "";
    $sql = $conn->prepare("SELECT contract_id ,date_in , time_in FROM `contracts`
    WHERE date_in BETWEEN ? and ?
    AND contract_id not In(SELECT DISTINCT contract_id from vat where end  BETWEEN ? AND ? ) ORDER BY date_in, time_in");
    $sql->bindParam(1, $first_day);
    $sql->bindParam(2, $last_day);
    $sql->bindParam(3, $first_day);
    $sql->bindParam(4, $last_day);
    $sql->execute();
    if ($sql->rowCount() > 0) {
        $no = 1;
        while ($row = $sql->fetch()) {
            $contract_id = $row['contract_id'];
            $end =  $row['date_in'];
            $vat->createVat($contract_id,$end,$father);
            $no = $no + 1;
        }
        $msg .= "<div class='alert alert-success'>
        <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
        <b> تم إصدار عدد $no فاتورة ضريبية بنجاح </b>
    </div>";
    }
        // $cust_id = 8007; 
        $sql = $conn->prepare("SELECT contract_id  FROM `contracts`  
        WHERE cust_id != 15859 AND date_out <= ?
        AND branch = 5 and cont_status = 1 ORDER BY contract_id");
        $sql->bindParam(1, $last_day);
        $sql->execute();
        if ($sql->rowCount() > 0) {
            $no = 1;
            while ($row = $sql->fetch()) {
            $contract_id = $row['contract_id'];
            $vat->createVat($contract_id,$end,$father);
            $no = $no + 1;
            }
        $msg .= "<div class='alert alert-success'>
            <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
            <b> تم إصدار عدد $no   فاتورة ضريبية بنجاح للشركات </b>
        </div>";
    }
    echo $msg ;
}

?>