<?php
require "../../config.php";

$baca = $db->query("SELECT * FROM akun_mutasi WHERE type='BCA'")->fetch_assoc();
$bca = new BCA($baca['nomor'],$baca['pin']);
$date = date("Y-m-d");
$dates = date("Y-m-d", strtotime("-7 days"));
$waktu = date("Y-m-d H:i:s");
$mut = $bca->getListTransaksi($dates,$date);
for($i = 0; $i <= count($mut['data']); $i++){
    $data = $mut['data'][$i];
    $desk = $data['desk'];
    $amount = $data['amount'];
    $p = str_replace(',','',$amount);
    $saldo = (int)$p;
     $dataa = $db->query("SELECT * FROM mutasi WHERE code = 'BCA' AND description='$desk' AND amount='$saldo'");
     if(mysqli_num_rows($dataa) !== 0) {
        echo "<br>Data Mutasi BCA Sudah Ada Di Database<br>";
    }else{
    if($data['flows'] == "CR"){
        $check = $db->query("INSERT INTO mutasi VALUES ('', '-', '$saldo','$desk', '$waktu', 'Unread','BCA')");
    }
    $data_deposit = $db->query("SELECT * FROM deposit WHERE metode='BANK BCA' AND amount='$saldo'");
    $data_order = $db->query("SELECT * FROM pembelian_guest WHERE total_harga='$saldo' status='Pending'");
    if($check == TRUE) {
        if(mysqli_num_rows($data_order) !== 0){
            $db->query("UPDATE pembelian_guest SET status='Waiting',sn='Transaksi Pending' WHERE metode='BANK BCA' AND status='Pending' total_harga='$saldo'");
        }else if(mysqli_num_rows($data_deposit) !== 0){
            $db->query("UPDATE deposit SET status='Success' WHERE metode='BANK BCA' AND amount='$saldo'");
            $db->query("UPDATE mutasi SET status='Read' WHERE code='BCA' AND amount='$saldo");
            $data_depo = $data_deposit->fetch_assoc();
            $user = $data_depo['username'];
            $db->query("UPDATE users SET saldo='saldo+$saldo WHERE username='$user'");
        }
            echo '<b><font color="green">SUKSES! -></font> DATA MUTASI BCA Rp. ('.$saldo.') TELAH DITAMBAHKAN KE DATABASE</b><br>';
    } else {
        echo '<b><font color="red">GAGAL! -></font> GAGAL MENAMPILKAN DATA MUTASI BCA ('.$saldo.')</b><br>';
        }
    }
}
?>