
<?php 
class Account{


  public function return(){
        global $database;
        
        if(isset($_GET['id']) && !empty($_GET['id'])){
            $this->id = $_GET['id'];
            
            
            $sql1 = $database->query("SELECT * FROM transacoes WHERE id='$this->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'");
        $this->count = mysqli_num_rows($buscaConta);
        if($this->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')")){
         $transactions = $database->query("INSERT INTO transacoes(descricao,entrada,saida,saldo,caixa,vd,data,hora,user)
        VALUES('Saldo incial','$saldo','0','$saldo','$nome_conta','N/A','$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'];

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

public function incomes(){
    global $database;

    if(isset($_POST['submitIncomes'])){
        $descricao = $_POST['descricao'];
        $valor = $_POST['valor'];
        $conta = $_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 conta='$conta'");
        $fetch = $database->fetch_assoc($searchAcount);
        $saldo_actual = $fetch['saldo'];

        $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>";
        }
        
        if($insere = $database->query("INSERT INTO transacoes(descricao,entrada,saida,saldo,caixa,vd,data,hora,user)
        VALUES('$descricao','$valor','0','$saldo_novo','$conta','N/A','$siku','$hora','$user')")){
            $update = $database->query("UPDATE contas SET saldo='$saldo_novo' WHERE conta ='$conta'");
           
           $sql = $database->query("INSERT INTO caixa(descricao,tipo,valor,data)
           VALUES('$descricao','entrada','$valor','$siku')");

            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 = $_POST['conta'];
        $vd = $_POST['vd'];
         $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 conta='$conta'");
        $fetch = $database->fetch_assoc($searchAcount);
        $saldo_actual = $fetch['saldo'];

        $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>";            
        }
        
        if($insere = $database->query("INSERT INTO transacoes(descricao,entrada,saida,saldo,caixa,vd,data,hora,user)
        VALUES('$descricao','0','$valor','$saldo_novo','$conta','$vd','$siku','$hora','$user')")){
      
           $update = $database->query("UPDATE contas SET saldo='$saldo_novo' WHERE conta ='$conta'");
            
           $sql = $database->query("INSERT INTO caixa(descricao,tipo,valor,data)
           VALUES('$descricao','saida','$valor','$siku')");

           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'])){
        $this->nome_conta = $_POST['nome_conta'];
        $this->id = $_POST['id'];

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

    if($update = $database->query("UPDATE contas SET conta='$this->nome_conta' WHERE id='$this->id'")){
        $transactions = $database->query("UPDATE transacoes SET conta='$this->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'])){

        $this->id = $_GET['id_delete'];

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

        if($this->sql = $database->query("DELETE FROM contas WHERE id='$this->id'")){
            $this->transactions = $database->query("DELETE FROM transacoes WHERE caixa='$contaApagar'");
            
            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();

?>