<?php
require '../../config.php';
$dt = $db->query("SELECT * FROM akun_mutasi WHERE type='OVO'")->fetch_assoc();
$ovo = new Ovo($dt['token']);
$mut = json_decode($ovo->transactionHistory(), true)['data']['orders'][0];
for($indeks = 0; $indeks <= count($mut['complete'][0])-1; $indeks++) {
    $data        = $mut['complete'][$indeks];
    $merchant    = $data['merchant_name'].' '.$data['merchant_invoice'];
    $date_time   = $data['transaction_date'].' '.$data['transaction_time'];
    $trx_amount  = $data['transaction_amount'];
    $desc_1      = $data['desc1'].' '.$data['desc2'];
    $data_ovo = $db->query("SELECT * FROM mutasi WHERE description = '$merchant'");
     if($data_ovo->num_rows > 0) {
        echo "<br>Data Mutasi OVO Sudah Ada Di Database<br>";
     } else {
    
    $i = $db->query("INSERT INTO mutasi VALUES ('', '-', '$trx_amount','$merchant', '$date_time', 'Unread','OVO')");
    if($i == TRUE) {
        $data_deposit = $db->query("SELECT * FROM deposit WHERE metode='OVO' AND amount='$trx_amount'");
        $data_order = $db->query("SELECT * FROM pembelian_guest WHERE total_harga='$trx_amount'");
        if($data_order->num_rows !== 0){
        $db->query("UPDATE pembelian_guest SET status='Waiting',sn='Transaksi Pending' WHERE metode='OVO' AND total_harga='$trx_amount'");
        }else if($data_deposit->num_rows !== 0){
            $db->query("UPDATE deposit SET status='Success' WHERE metode='OVO' AND amount='$trx_amount'");
            $data_depo = $data_deposit->fetch_assoc();
            $user = $data_depo['username'];
            $db->query("UPDATE users SET saldo=saldo+$trx_amount WHERE username='$user'");
        }
        echo '<b><font color="green">SUKSES! -></font> DATA MUTASI OVO TELAH DITAMBAHKAN KE DATABASE</b><br>';
    } else {
        echo '<b><font color="red">GAGAL! -></font> GAGAL MENAMPILKAN DATA MUTASI OVO</b><br>';
        }
    }
    
}
$dtt = $db->query("SELECT * FROM akun_mutasi WHERE type = 'GOPAY'")->fetch_assoc();
$Gopay = new GojekPay($dtt['token']);
$mutt = json_decode($Gopay->getTransactionHistory(), true);

for($indeks = 0; $indeks <= count($mutt['data']['success'])-1; $indeks++) {
    $data       = $mutt['data']['success'][$indeks];
    $type       = $data['type'];
    $saldo      = $data['amount']['value'];
    $deskripsi  = $data['description'];
    $waktu      = $data['transacted_at'];
    $ref_trx    = $data['transaction_ref'];
    
     $dataGopay = $db->query("SELECT * FROM mutasi WHERE code='GOPAY' AND description = '$deskripsi'");
     if($dataGopay->num_rows > 0) {
        echo "<br>Data Mutasi GOPAY Sudah Ada Di Database<br>";
     } else {
    
    $i = $db->query("INSERT INTO mutasi VALUES ('', '-', '$saldo','$deskripsi', '$waktu', 'Unread','GOPAY')");
    if($i == TRUE) {
        $data_deposit = $db->query("SELECT * FROM deposit WHERE metode='GOPAY' AND amount='$saldo'");
        $data_order = $db->query("SELECT * FROM pembelian_guest WHERE total_harga='$saldo'");
        if($data_order->num_rows !== 0){
        $db->query("UPDATE pembelian_guest SET status='Waiting',sn='Transaksi Pending' WHERE metode='GOPAY' AND total_harga='$saldo'");
        }else if($data_deposit->num_rows !== 0){
            $db->query("UPDATE deposit SET status='Success' WHERE metode='GOPAY' 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 GOPAY TELAH DITAMBAHKAN KE DATABASE</b><br>';
    } else {
        echo '<b><font color="red">GAGAL! -></font> GAGAL MENAMPILKAN DATA MUTASI GOPAY</b><br>';
        }
    }
}