<?php
namespace myretail\controllers;

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

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

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

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

        return $echo;
    }
}