
<?php 
class Account{


  public function return(){
        global $database;
        
        if(isset($_GET['id']) && !empty($_GET['id'])){
            $id = $_GET['id'];
            
            
            $sql1 = $database->query("SELECT * FROM transacoes WHERE id='$id'");
            $assoc = $database->fetch_assoc($sql1);
            
            $conta = $assoc['caixa'];
            $valorSaida = $assoc['saida'];
            $valorEntrada = $assoc['entrada'];
            
        
            
            $contas = $database->query("SELECT * FROM contas WHERE conta='$conta'");
            $ass = $database->fetch_assoc($contas);
            
            $saldo = $ass['saldo'];
            
            
            
            if($valorSaida == 0){
            $valor = $valorEntrada;
            $saldoActual = $saldo - $valor;
            }
            if($valorEntrada == 0){
            $valor = $valorSaida;
            $saldoActual = $saldo + $valor;
            }
            
            
            $update = $database->query("UPDATE contas SET saldo='$saldoActual' WHERE conta='$conta'");
            
            $sql = $database->query("DELETE FROM transacoes WHERE id='$this->id'");
            if($sql){
                echo "<script>alert('lancamento cancelado com sucesso')</script>";
                echo "<script>window.open('contas.php','_self')</script>";
            }else{
                echo "ERORORRR";
            }
        }
    }



public function registerAcount(){

    global $database;

    if(isset($_POST['submitAccount'])){
        $nome_conta = $_POST['nome_conta'];
        $saldo = $_POST['saldo'];
        $url = "contas.php";
        $message = "$nome_conta ja foi cadastrada";
        $message2 = "$nome_conta cadastrada com sucesso";
        $user = $_SESSION['nome'];
        $data = date('Y-m-d');
        $hora = date("H:i", mktime(gmdate("H")+2, gmdate("i"), gmdate("s"), gmdate("m"), gmdate("d"), gmdate("Y")));
    
         

        $buscaConta = $database->query("SELECT  * FROM contas WHERE conta='$nome_conta'");
        $count = mysqli_num_rows($buscaConta);
        if($count == 1){
            echo "<script>alert('Esta conta ja existe')</script>";
            echo "<script>window.open('contas.php','_self')</script>";
        }
        
     if($sql = $database->query("INSERT INTO contas(conta,numero_conta,moeda,nib,iban,swift,saldo)VALUES('$nome_conta','0','MZN','00','00','00','$saldo')")){
        
     $conta_id = $database->the_insert_id();

         $transactions = $database->query("INSERT INTO transacoes(descricao,entrada,saida,saldo,caixa,id_cliente,data,hora,user)
        VALUES('Saldo incial','$saldo','0','$saldo','$conta_id','0','$data','$hora','$user')");
         echo "<script>alert('Conta adicionada com sucesso')</script>";
         echo "<script>window.open('contas.php','_self')</script>";
     }else{
    echo "<script>alert('Falha no cadastro')</script>";
     echo "<script>window.open('contas.php','_self')</script>";
}
    }
}  



public function fetchAccount(){
    global $database;

    $sql = $database->query("SELECT * FROM contas");
    while($row = $database->fetch_assoc($sql)){
        $account = $row['conta'];
        $id = $row['id'];

        echo "<option value='$id'>$account</option>";
    }
}

public function fetchAccountName(){
    global $database;

    $sql = $database->query("SELECT * FROM contas");
    while($row = $database->fetch_assoc($sql)){
        $account = $row['conta'];

        echo "<option>$account</option>";
    }
}

public function incomes(){
    global $database;

    if(isset($_POST['submitIncomes'])){
        $descricao = $_POST['descricao'];
        $valor = $_POST['valor'];
        $conta_id = $_POST['conta'];
        $siku = date('Y-m-d');

        $user = $_SESSION['nome'];
        $data = date('Y-m-d');
        $hora = date("H:i", mktime(gmdate("H")+2, gmdate("i"), gmdate("s"), gmdate("m"), gmdate("d"), gmdate("Y")));
        

        $searchAcount = $database->query("SELECT * FROM contas WHERE id='$conta_id'");
        $fetch = $database->fetch_assoc($searchAcount);
        $saldo_actual = $fetch['saldo'];
        $conta = $fetch['conta'];


        $saldo_novo = $saldo_actual + $valor;
        
        if($valor < 0){
             echo "<script>alert('O valor nao pode ser inferior a zero')</script>";
             echo "<script>window.open('contas.php','_self')</script>";
             exit;
        }
        
        if($insere = $database->query("INSERT INTO transacoes(descricao,entrada,saida,saldo,caixa,id_cliente,data,hora,user)
        VALUES('$descricao','$valor','0','$saldo_novo','$conta_id','0','$siku','$hora','$user')")){
            $update = $database->query("UPDATE contas SET saldo='$saldo_novo' WHERE id ='$conta_id'");
            echo "<script>alert('Entrada efetuada com sucesso')</script>";
            echo "<script>window.open('contas.php','_self')</script>";
        }else{
            echo "<script>alert('Ocorreu um erro')</script>";
            echo "<script>window.open('contas.php','_self')</script>";
        }
    
}

}


public function Expensives(){
    global $database;

    if(isset($_POST['submitExpensives'])){
        $descricao = $_POST['descricao'];
        $valor = $_POST['valor'];
        $conta_id = $_POST['conta'];
         $siku = date('Y-m-d');
        
        
        $user = $_SESSION['nome'];
        $data = date('Y-m-d');
        $hora = date("H:i", mktime(gmdate("H")+2, gmdate("i"), gmdate("s"), gmdate("m"), gmdate("d"), gmdate("Y")));
    

        $searchAcount = $database->query("SELECT * FROM contas WHERE id='$conta_id'");
        $fetch = $database->fetch_assoc($searchAcount);
        $saldo_actual = $fetch['saldo'];
        $conta = $fetch['conta'];

        $saldo_novo = $saldo_actual - $valor;

        if($valor > $saldo_actual){
            echo "<script>alert('Operacao recusada,valor superior ao saldo disponivel de $saldo_actual')</script>";
            echo "<script>window.open('contas.php','_self')</script>";
            exit;            
        }
        
        if($insere = $database->query("INSERT INTO transacoes(descricao,entrada,saida,saldo,caixa,id_cliente,data,hora,user)
        VALUES('$descricao','0','$valor','$saldo_novo','$conta_id','0','$siku','$hora','$user')")){
      
           $update = $database->query("UPDATE contas SET saldo='$saldo_novo' WHERE id ='$conta_id'");
            echo "<script>alert('Saida efetuada com sucesso')</script>";
            echo "<script>window.open('contas.php','_self')</script>";
        }else{
            echo "<script>alert('Ocorreu um erro')</script>";
            echo "<script>window.open('contas.php','_self')</script>";
        }
    
}

}


public function updateAccount(){
    global $database;
    if(isset($_POST['subUp'])){
        $nome_conta = $_POST['nome_conta'];
        $id = $_POST['id'];

        $take = $database->query("SELECT * FROM contas WHERE id='$id'");
        $fetch = $database->fetch_assoc($take);
        $contaNova = $fetch['conta'];

    if($update = $database->query("UPDATE contas SET conta='$nome_conta' WHERE id='$id'")){
        $transactions = $database->query("UPDATE transacoes SET conta='$nome_conta' WHERE caixa='$contaNova'");
        echo "<script>alert('conta actualizada com sucesso')</script>";
        echo "<script>window.open('contas.php','_self')</script>";
   
    }else{
        echo "<script>alert('Ocorreu um erro')</script>";
        echo "<script>window.open('contas.php','_self')</script>";
    }
    }
}

public function deleteAccount(){
    global $database;

    if(isset($_GET['id_delete']) && !empty($_GET['id_delete'])){

        $id = $_GET['id_delete'];

        if($sql = $database->query("DELETE FROM contas WHERE id='$id'")){
            $transactions = $database->query("DELETE FROM transacoes WHERE caixa='$id'");
            
            echo "<script>alert('Transacoes e conta eliminadas com sucesso')</script>";
            echo "<script>window.open('contas.php','_self')</script>";
        }else{
            echo "<script>alert('Ocorreu um erro')</script>";
            echo "<script>window.open('contas.php','_self')</script>";
        }

        
    }
}



}












$account = new Account();

?>