<?php

/*
 * Demo: demo_getartiklicsv.php
 * 
 * Primer poziva API funkcije GETARTIKLI (cenovnik) nakon koga se preuyeti podaci konvertuju i prikayuju kao CSV
 * Uradjeno za BAKI
 *  
 */

header('Content-Type: text/html; charset=utf-8'); 

$m_image_web_url = "http:www.somedomain.com/images/";
 
include_once 'infoapi/infoapi.php';

$miapiHandler = new iapiHandler;
$miapiHandler->APIServerURL = SERVER_URL; // definisano u apitest/config.php

$miapiHandler->DebugLevel = DEBUG_LEVEL;
$miapiHandler->DebugSections = DEBUG_SECTIONS;


// naziv API funkcije koja se poziva
$mAPIFunctionName = 'GETARTIKLILIST';

$mQueryEnvelope = new iapiQueryEnvelope ($mAPIFunctionName);
$mQueryEnvelope->username = USERNAME; // definisano u apitest/config.php
$mQueryEnvelope->password = PASSWORD; // definisano u apitest/config.php

$mQuery = $mQueryEnvelope;

// moze se navesti lista polja koja treba vratiti u rezultatu
//$mQuery->data->fields = 'id_art, stanje, cena, def_mp_mar, pdv_st_art';

// po potrebi se može podesiti filter nad podacima
//$m_filter = new iapiFilter ('NE', 'cena', 0);   // artikli koji imaju cenu
//$m_filter = new iapiFilter ('GT', 'stanje', 0); // artikli kojih ima na stanju
//$mQuery->data->filter = $m_filter;

$mQuery->data->order = 'id_art';
$mQuery->data->result_database_format = 'SQLITE';

$mResponse  = $miapiHandler->Execute($mQuery);

$m_error = false;

if ($mResponse->error_state !='ok') {
  $m_error = true;
?>
Greska: <?php echo $mResponse->error_state; ?> - <?php echo $mResponse->error_message; ?>
<?php
} else {

  if ($mResponse->data->error_state !='ok') {
    $m_error = true;
    header("HTTP/1.0 550 API ERROR");
?>
Greska: <?php echo $mResponse->data->error_state; ?> - <?php echo $mResponse->data->error_message; ?>
<?php
  }

}

if (!$m_error) {
   
    $mdatabasedata = base64_decode ($mResponse->data->databaseattachment);

    $m_output_file = fopen('artikli.db', 'w');
    fwrite($m_output_file, $mdatabasedata);
    fclose($m_output_file);

    //echo 'data stored to artikli.db';
    
    echo '"id";"naziv";"barcode";"part_no";"kolicina";"cena";"preporucena_cena";"valuta";"kategorija";"putanja_do_slike"' . "\r\n";
    
    try {
      $db = new PDO('sqlite:artikli.db');

      $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      
      $data = $db->query('SELECT * FROM listaartikala');
      
      foreach($data as $row) {
        echo $row['id_art'] . ';';     // id
        echo $row['naziv_art'] . ';';   // naziv
        echo $row['barcode'] . ';';   //  barcode
        echo ';"';   // part_no
        echo $row['stanje'] . ';';   // kolicina
        echo $row['cena'] .';';   // cena
        echo ';';   // preporucena_cena
        echo ';';   // valuta
        echo ';';   // kategorija
        echo '"' . $m_image_web_url . $row['id_art'] . '.jpg"';   // putanja_do_slike
        
        echo "\r\n";
        
      }

      $db = NULL;
    }
    catch(PDOException $e) {
      print 'Exception : '.$e->getMessage();
    }

}
?>
<?php //include_once 'inc_sysinfo.php'; ?>