<?php
namespace myretail\controllers;

use Yii;
use yii\web\Controller;
use myretail\models\ImportPayment;
use myretail\models\SyncConfig;

class ImportPaymentController extends Controller {
    public function actionIndex() {
        $model = new ImportPayment();
        $tipe_tabel = $model->tipe_tabel;
        $conn = SyncConfig::getConnection();
        $config = SyncConfig::find()->where(['type_con' => 'retail'])->one();
        $access_token = $config->api_key;

        if (isset($_POST['date_start'])) {$start = $_POST['date_start'];} else {$start = '0000-00-00';}
        if (isset($_POST['date_end'])) {$end = $_POST['date_end'];} else {$end = '0000-00-00';}

        $result = $model->ambilData("import", $tipe_tabel, $access_token, $start, $end);

        if ($result != NULL) {
            for ($i=0; $i < count($result['receive_payments']); $i++) {
                $id_lokal[$i] = $result['receive_payments'][$i]['receive_payment']['id'];
                unset($result['receive_payments'][$i]['receive_payment']['id']);
            }
            $pembulatan = $result['pembulatan'];
            $jurnal_entry = $result['jurnal'];
            unset($result['pembulatan']);
            unset($result['jurnal']);
            $response = $model->kirimData($result, $access_token, 'receive_payments/batch_create');
            
            // BEGIN - Proses mengirim data jurnal entry ke jurnal
            $hasil = $jurnal_entry - $pembulatan;
            if ($hasil < 2) {
                $model->kirimJurnalEntry($jurnal_entry, $result, $access_token);
            }
            // END - Proses mengirim data jurnal entry ke jurnal

            $simpan = $model->simpanStatus($response, $tipe_tabel, $id_lokal);
            $echo = $simpan;
            // END - proses ambil, kirim dan simpan data yang baru
        } else {
            // BEGIN - proses ambil, kirim dan simpan data yang gagal diimport
            $echo = "pay";
            /*$stmt = mysqli_query($conn, "SELECT * FROM sync_to_jurnal WHERE status_sinkron = 'proses' AND tipe_tabel = '$tipe_tabel'");
            if (mysqli_fetch_assoc($stmt) > 0) {
                $result = ambilData("reimport", $tipe_tabel, $access_token);
                echo json_encode($result);
                for ($i=0; $i < count($result['receive_payments']); $i++) { 
                    $id_lokal[$i] = $result['receive_payments'][$i]['receive_payment']['id'];
                    unset($result['receive_payments'][$i]['receive_payment']['id']);
                }
                $response = kirimData($result, $access_token, 'receive_payments/batch_create');
                $simpan = simpanStatus($response, $tipe_tabel, $id_lokal);
                echo $simpan;
            } else {
                mysqli_query($conn, "UPDATE sync_to_jurnal SET status_sinkron = 'proses' WHERE status_sinkron = 'failed' AND tipe_tabel = '$tipe_tabel'");

            }*/
            // END - proses ambil, kirim dan simpan data yang gagal diimport
        }

        return $echo;
    }
}