<?php
class Objednavka extends Super{

  protected $id;
  protected $id_uziv;
  protected $id_adr_doruc;
  protected $id_adr_fakt;
  protected $id_dop;
  protected $cena_dop;
  protected $dt_vytvoren;
  protected $dt_zmenen;
  protected $dt_vyrizen;
  protected $stav = 1;  // default stav 

  protected $getter = array('id', 'id_uziv', 'id_adr_doruc', 'id_adr_fakt',
   'id_dop', 'cena_dop', 'dt_vytvoren', 'dt_zmenen', 'dt_vyrizen', 'stav');
  protected $setter = array('id_uziv', 'id_adr_doruc', 'id_adr_fakt',
   'id_dop', 'cena_dop', 'dt_vytvoren', 'dt_zmenen', 'dt_vyrizen', 'stav');
  public $validate = array(
		'id_uziv' 		=> array('digits' => 'true'),
		'id_adr_doruc'=> array('required' => 'true','digits' => 'true'),
		'id_adr_fakt'	=> array('required' => 'true','digits' => 'true'),
    'id_dop'			=> array('required' => 'true','digits' => 'true'),
    'cena_dop'		=> array('required' => 'true'),
    'dt_vytvoren'	=> array(),
    'dt_zmenen'		=> array(),
    'dt_vyrizen'	=> array(),
  	'stav'				=> array()
  );

	public $label = array(
		'stav' => 'Stav objednávky'
  );
  public $valAll = array(
  	'id'     => true,
		'id_uziv' 		=> true,
		'id_adr_doruc'=> true,
		'id_adr_fakt'	=> true,
    'id_dop'			=> true,
    'cena_dop'		=> true,
    'dt_vytvoren'	=> true,
    'dt_zmenen'		=> true,
    'dt_vyrizen'	=> true,
  	'stav'				=> true
  );
	public $filtry = array(
      'ID' => array(
      	'type' => 'text',
				'key'	 => 'ID',
				'sql'	 => 'num',
    		'size' => 3
    	),
    	'ID_UZIV' => array(
      	'type' => 'text',
				'key'	 => 'ID_UZIV',
				'sql'	 => 'num',
    		'size' => 3
    	),
      'DT_VYTVORENA' => array( // dodělat
      	'type' => 'text',
    		'key'	 => 'DT_VYTVOREN',
    		'sql'	 => 'text',
    		'size' => 12
    	),
      'DT_VYTVOREN_OD' => array( // dodělat
      	'type' => 'text',
    		'key'	 => 'DT_VYTVOREN',
    		'sql'	 => 'gt'
    	),
      'DT_VYTVOREN_DO' => array( // dodělat
      	'type' => 'text',
    		'key'	 => 'DT_VYTVOREN',
    		'sql'	 => 'lt'
    	),
      'S_NAZEV' => array(
      	'type' => 'stav',
    		'key'	 => 'STAV',
    		'sql'	 => 'in'
    	),
      'UPRAVIT' => array(
      	'type' => 'submit'
    	),
      'DETAIL' => array(
      	'type' => 'all'
    	)
    );
  public $state_validate = array();

  function __construct() {
    /*
     * stavy:
1	Odeslaná
2	Neověřená
3	Potvrzená
4	Probíhá zpracování
5	Částečně vyřízená
6	Vyřízená
7	Storno
8	Odeslaná
     * 
     */
    $this->state_validate[1] = $this->valAll;
    $this->state_validate[2] = $this->valAll;
    $this->state_validate[3] = $this->valAll;
    $this->state_validate[4] = $this->valAll;
    $this->state_validate[5] = $this->valAll;
    $this->state_validate[6] = $this->valAll;
    $this->state_validate[7] = $this->valAll;
		$this->state_validate[8] = $this->valAll;
    $this->table = 'OBJEDNAVKA';

  }
  
/**
 * INSERT do DB
 * @param $key
 */

  public function insert() {
  	if (method_exists($this, 'beforeInsert')) {
  		$this->beforeInsert();
  	}
  	$res = dibi::query('INSERT INTO ['.$this->table.']', $this->toArray(true) );
  	$this->id = dibi::insertId();
  }

  public function loadById($id, $hash = false, $postRestore = false) {
    $result = dibi::query('SELECT  E.*, S.NAZEV AS S_NAZEV, S.POPIS AS S_POPIS FROM ['.$this->table.'] AS E 
    	INNER JOIN STAV AS S ON S.STAV_ID = E.STAV AND ENTITA = %s
    	WHERE E.ID = %i;', $this->table, $id);

    if (count($result) == 0) {
      throw new Appliexception(1, 'Požadovaný záznam neexistuje', 'seznam', __METHOD__.' Neexistující záznam '.$id );
    }

    $row = $result->fetch()->getArrayCopy();
    $this->setFromRow($row);
    $this->my_stav = '<div title="'.$row['S_POPIS'].'" class="block_state"> '.$row['STAV'].' - '.$row['S_NAZEV'].'</div><br />';

    if ($hash) {
      $this->hash  = $this->getHash();
    } 

    $this->myStates();

    if ($postRestore) {
      $this->setFromPost();
    }
    return $this->toArray();
  }

  public function getInfo($id, $hash = false, $postRestore = false) {
    $result = dibi::query('SELECT E.*, S.NAZEV AS S_NAZEV, S.POPIS AS S_POPIS,
		A1.TITUL AS FA_TITUL, A1.JMENO AS FA_JMENO, A1.PRIJMENI AS FA_PRIJMENI,A1.NAZEV_FIRMY AS FA_NAZEV_FIRMY, A1.ICO AS FA_ICO,
		A1.DIC AS FA_DIC, A1.KONTAKT_MAIL AS FA_KONTAKT_MAIL, A1.KONTAKT_TELEFON AS FA_KONTAKT_TELEFON, A1.ULICE AS FA_ULICE,
		A1.CISLO_POPISNE AS FA_CISLO_POPISNE, A1.OBEC AS FA_OBEC, A1.PSC AS FA_PSC,
		A2.TITUL AS DA_TITUL, A2.JMENO AS DA_JMENO, A2.PRIJMENI AS DA_PRIJMENI, A2.NAZEV_FIRMY AS DA_NAZEV_FIRMY, A2.ICO AS DA_ICO,
		A2.DIC AS DA_DIC, A2.KONTAKT_MAIL AS DA_KONTAKT_MAIL, A2.KONTAKT_TELEFON AS DA_KONTAKT_TELEFON, A2.ULICE AS DA_ULICE,
		A2.CISLO_POPISNE AS DA_CISLO_POPISNE, A2.OBEC AS DA_OBEC, A2.PSC AS DA_PSC,
		CONCAT(U.JMENO, " ", U.PRIJMENI) AS UZIVATEL,
		D.NAZEV AS D_NAZEV
		FROM `OBJEDNAVKA` AS E
		INNER JOIN ADRESA A1 ON A1.ID = E.ID_ADR_FAKT
		INNER JOIN ADRESA A2 ON A2.ID = E.ID_ADR_DORUC
		INNER JOIN UZIVATEL U ON U.ID = E.ID_UZIV
		INNER JOIN DOPRAVA D ON D.ID = E.ID_DOP
		INNER JOIN STAV AS S ON S.STAV_ID = E.STAV AND ENTITA = %s
		WHERE E.ID = %i;', $this->table, $id);
    
    $row = $result->fetch()->getArrayCopy();
    $this->setFromRow($row);
    $this->my_stav = $row['S_NAZEV'].'<br />';

    if ($hash) {
      $this->hash  = $this->getHash();
      $row[HASH]  = $this->getHash();
    } 

    $this->myStates();

    if ($postRestore) {
      $this->setFromPost();
    }
    return $row;
  }

	public function beforeInsert() {
		if ($this->id_uziv == 1) {
			$this->stav = 2;
		}
		else {
			$this->stav = 1;
		}
		$this->dt_vytvoren = date('Y-m-d H:m:s');

	}

	public function beforeUpdate() {

	}
	
	public function overLink() {
		$result = dibi::query('SELECT SHA1(CONCAT(ID,"-",ID_UZIV,"-",DT_VYTVOREN,"OVERENI")) AS HASH FROM OBJEDNAVKA WHERE STAV = 2 AND ID = %i',$this->id);
		if (count($result) == 1) {
			return $result->fetchSingle();
		}
		else {
			return false;
		}
	}
	
  public function getAllQuery() {

  	$query = array();
  	$query[] = 'SELECT E.*, S.NAZEV AS S_NAZEV, 
  		((SELECT SUM(CEIL(CENA * DAN + RECYKLACNI_POPLATEK + AUTORSKY_POPLATEK) * MNOZSTVI_OBJ) FROM PRODUKTY_OBJEDNAVKA WHERE ID_OBJ = E.ID) + CENA_DOP) AS CENA,
  		(SELECT SUM(MNOZSTVI_OBJ) FROM PRODUKTY_OBJEDNAVKA WHERE ID_OBJ = E.ID) AS POCET,
  		CONCAT(U.JMENO, " ", U.PRIJMENI) AS UZIVATEL
		FROM ['.$this->table.'] AS E';
  	array_push($query, 'INNER JOIN [STAV] S ON S.STAV_ID = E.STAV AND S.ENTITA = %s',$this->table );
  	array_push($query, 'INNER JOIN [UZIVATEL] U ON U.ID = E.ID_UZIV' );
  	return $query;

  }

}
?>