<?php
namespace myresto\controllers;

use Yii;
use yii\web\Controller;
use myresto\models\UpdateSupplier;
use myresto\models\SyncConfig;

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

        $idHeaderLog = $_POST['idHeaderLog'];

        $result = $model->ambilData($tipe_tabel);

        if ($result != NULL) {
            $id_lokal = $result['vendor']['id'];
            $id_jurnal = $result['vendor']['id_jurnal'];
            unset($result['vendor']['id']);
            unset($result['vendor']['id_jurnal']);
            $response = $model->kirimData($result, $access_token, $id_jurnal);
            $simpan = $model->simpanStatus($response, $tipe_tabel, $id_lokal, $idHeaderLog);
            $echo = $simpan;
            // END - proses ambil, kirim dan simpan data yang baru
        } else {
            $echo = "upsupp";
        }

        return $echo;
    }
}