<?php
class objednavkyController extends baseController {

	function __construct() {

    $this->controller_name = 'Objednávky';
    $this->controller_fname = 'objednavky';
    $this->controller_info  = 'Objednávky';
    $this->entita_classname = 'Objednavka';
    parent::__construct();

  }

  public function seznam($args)	{

    $entita = new $this->entita_classname();
    $table_id = 'seznamAtribut';
    $filtr_query = array();

    $this->registry->jscript->addJqFunction('"#'.$table_id.'"', 'tablesorter','','','','','','document_ready');
/*
    $query = array();
    $query[] = 'SELECT E.*, S.NAZEV AS S_NAZEV,
     (SELECT COUNT(*) FROM [HODNOTA_ATRIBUTU] WHERE ID_ATR = S.ID) AS POCET_HODNOT FROM ['.$entita->table.'] AS E';
    array_push($query,'INNER JOIN [STAV] AS S ON S.STAV_ID = E.STAV AND S.ENTITA = %s',$entita->table);
*/
    $query = $entita->getAllQuery();
    $filtr_query = array();
    
	$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
    	),
      'S_NAZEV' => array(
      	'type' => 'stav',
    		'key'	 => 'STAV',
    		'sql'	 => 'in'
    	),
      'UPRAVIT' => array(
      	'type' => 'submit'
    	),
      'DETAIL' => array(
      	'type' => 'all'
    	)
    );
    
    $this->setFilter($filtr_query, $filtry, array('ID_UZIV' => $this->registry->user->id));
    $this->myOrder($filtr_query, $entita, array(array('name' => 'dt_vytvoren', 'ord' => 'DESC')));
    $strankovani = $this->addLimit($query, $filtr_query ,' FROM OBJEDNAVKA E');
		$query = array_merge($query,$filtr_query);
//$result = dibi::test($query);
    $result = dibi::query($query);
    $zaznamy = $result->fetchAll();
    
    foreach($zaznamy as $key=>$value) {
    	$zaznamy[$key]['DT_VYTVOREN'] = Tools::dateToCZ($value['DT_VYTVOREN']);
    	$zaznamy[$key]['CENA'] = Tools::numberFromDb($value['CENA']).' Kč';  
    }
    unset($result);

    array_walk($zaznamy,
      'Tools::addLink',
      array(
				'DETAIL' => array(
          'link' => Tools::controllerlink($this->controller_fname, 'detail'),
          'addindex' => 'ID',
          'text'	 => ICO_NAHLED
        )
      )
    );    
    
    $hlavicka = array(
      'ID' => 'Id',
      'DT_VYTVOREN' => 'Vytvořena',
      'S_NAZEV' => 'Stav',
      'CENA' => 'Cena celkem',
      'DETAIL' => ''
    );
     
    $obsah_css = array(
      'ID' => 'a_c',
      'S_NAZEV' => 'a_c',
    	'CENA' => 'a_r',
    	'DETAIL' => 'a_c'
    );
    
    $hlavicka_css = array(
      'UPRAVIT' => 'nosort'
    );
    $this->thCss($hlavicka_css);
    

    $this->html = $this->getTable($table_id, $hlavicka, $zaznamy,
      array('class' => 'seznam', 'summary' => $this->controller_name ),
      $obsah_css, array('ID', 'DETAIL'), $hlavicka_css, '', array(), false
    );

		$this->html .= '<div class="strankovani">'.$strankovani.'</div></form>';
    $this->setView();
  }

  public function detail($args) {

  	$id = @$args[0];
    $form_id = 'DetailObjednavky';
		$entita = new $this->entita_classname();

    if ($this->registry->form_send) {
    	$_POST = array_merge($entita->loadById($id),$_POST);

    	if (isset($_POST['uloz'])) {
    		$_POST['stav'] = 3;
    		$akce_text = 'potvrzena';
    	}
    	elseif (isset($_POST['storno'])) {
    		$_POST['stav'] = 7;
    		$akce_text = 'stronována';
    	}

      if (Validate::entita($entita) !== false) {

        $entita->upravit($id,$form_id);
        Tools::setMessage('Objednávka byla '.$akce_text.'.', '', 1);
        Tools::redirect(Tools::controllerlink($this->controller_fname, 'seznamnove'));


      }
      else {
        $valid_err = Validate::error();
        Tools::setMessage('Špatně vyplněné údaje'.$valid_err, CSS_ERR);
      }
    }
    
    $item = $entita->getInfo($id, true);
		$this->html .= '<h1>Objednávka id: '.$item['ID'].'<br /> Stav: '.$entita->my_stav.'</h1>';
		$this->html .= '<div id="adresy"><div class="adresa">
  					<h2> Fakturační adresa</h2>
  					<div class="adr_box">
        		<h3 class="a_l">'.@$item['FA_TITUL'].' '.$item['FA_JMENO'].' '.$item['FA_PRIJMENI'].'</h3>
        		<b>Ulice a č.p.:</b> '.$item['FA_ULICE'].' '.$item['FA_CISLO_POPISNE'].'<br />
        		<b>Město:</b> '.$item['FA_OBEC'].'<br />
        		<b>PSČ:</b> '.$item['FA_PSC'].'<br />
        		<br />'.(empty($item['FA_NAZEV_FIRMY'])? '' : '<b>Firma:</b> '.$item['FA_NAZEV_FIRMY'].'<br />
        		<b>IČ:</b> '.$item['FA_ICO'].'<br /><b>DIČ:</b> '.$item['FA_DIC'].'<br /><br />').'        		
        		<b>Kontaktní e-mail:</b><br />'.$item['FA_KONTAKT_MAIL'].'<br />
        		<b>Mobil:</b> '.$item['FA_KONTAKT_TELEFON'].'<br /><br />
        		</div>
					</div>';

		$this->html .= '<div class="adresa">
  					<h2> Dodací adresa</h2>
  					<div class="adr_box">
        		<h3 class="a_l">'.@$item['DA_TITUL'].' '.$item['DA_JMENO'].' '.$item['DA_PRIJMENI'].'</h3>
        		<b>Ulice a č.p.:</b> '.$item['DA_ULICE'].' '.$item['DA_CISLO_POPISNE'].'<br />
        		<b>Město:</b> '.$item['DA_OBEC'].'<br />
        		<b>PSČ:</b> '.$item['DA_PSC'].'<br />
        		<br />'.(empty($item['DA_NAZEV_FIRMY'])? '' : '<b>Firma:</b> '.$item['DA_NAZEV_FIRMY'].'<br />
        		<b>IČ:</b> '.$item['DA_ICO'].'<br /><b>DIČ:</b> '.$item['DA_DIC'].'<br /><br />').'        		
        		<b>Kontaktní e-mail:</b><br />'.$item['DA_KONTAKT_MAIL'].'<br />
        		<b>Mobil:</b> '.$item['DA_KONTAKT_TELEFON'].'<br /><br />
        		</div>
					</div>
					<hr class="cleaner_left">
					</div>';
		//<th>Katalog. č.</th>
		$this->html .= '<table summary="Objednavkaprehled" class="seznam" cellspacing="0">
      <thead><tr>
      	<th class="a_c ui-corner-tl">Id</th>
        <th>Produkt</th>
        <th>Množství</th>
        <th>Bez daně</th>
        <th>Daň</th>
        <th>Cena/kus</th>
        <th class="ui-corner-tr">Celkem</th>
      </tr></thead>';
		
		$result = dibi::query('SELECT PO.MNOZSTVI_OBJ AS MNOZSTVI,
			PO.CENA AS CENA_BEZ_DPH, PO.RECYKLACNI_POPLATEK, PO.AUTORSKY_POPLATEK, ((PO.DAN -1)*100) AS DAN_PROCENT,
			(PO.CENA * (PO.DAN -1)) AS DAN_CENA,
			CEIL(PO.CENA*PO.DAN + PO.RECYKLACNI_POPLATEK + PO.AUTORSKY_POPLATEK) AS MY_CENA,
			(CEIL(PO.CENA*PO.DAN + PO.RECYKLACNI_POPLATEK + PO.AUTORSKY_POPLATEK)*PO.MNOZSTVI_OBJ) AS CENA_CELKEM,
			P.ID, P.NAZEV, P.KATALOG_CISLO, P.REWRITE_LINK
			FROM produkty_objednavka PO
			INNER JOIN PRODUKT P ON P.ID = PO.ID_PROD
			WHERE ID_OBJ = %i', $entita->id);
		
		$celkova_cena = 0;
		$celkova_bezdane = 0;
		$celkovy_pocet = 0;
		//<td>'.$row['KATALOG_CISLO'].' </td>
		foreach($result as $row) {
			$this->html .= '<tr>
					<td class="a_c">'.$row['ID'].' </td>
          <td><a href="'.Tools::productLink($row['REWRITE_LINK']).'">'.$row['NAZEV'].'</a></td>
          <td class="a_c">'.$row['MNOZSTVI'].' Ks</td>
          <td class="a_r">'.number_format($row['CENA_BEZ_DPH'],2,',', ' ').' Kč<br />
          '.($row['RECYKLACNI_POPLATEK'] != 0 ? 'PHE : '.number_format($row['RECYKLACNI_POPLATEK'],2,',', ' ').'<br />' : '').'
          '.($row['AUTORSKY_POPLATEK'] != 0 ? 'ATP : '.number_format($row['AUTORSKY_POPLATEK'],2,',', ' ').'<br />' : '').'
          </td>
          <td class="a_r">'.number_format($row['DAN_PROCENT'],2,',', ' ').'%<br />
          '.number_format($row['DAN_CENA'],2,',', ' ').' Kč</td>
        
          <td class="a_r">'.number_format($row['MY_CENA'],2,',', ' ').' Kč</td>
          <td class="a_r">'.number_format($row['CENA_CELKEM'],2,',', ' ').' Kč</td>
        </tr>';
			$celkova_cena += $row['CENA_CELKEM'];
			$celkovy_pocet += $row['MNOZSTVI'];
			$celkova_bezdane += ($row['CENA_BEZ_DPH'] + $row['RECYKLACNI_POPLATEK'] + $row['AUTORSKY_POPLATEK']) * $row['MNOZSTVI'];
		}
		
		$this->html .= '
		<tr class="celkem">
					<td>Celkem: </td>
          <td> </td>
          <td class="a_c">'.$celkovy_pocet.' Ks</td>
          <td class="a_r">'.number_format($celkova_bezdane,2,',', ' ').' Kč</td>
          <td></td>
          <td></td>
          <td class="a_r">'.number_format($celkova_cena,2,',', ' ').' Kč</td>
        </tr>
        <tr class="celkem">
					<td>Doprava: </td>
          <td>'.$item['D_NAZEV'].'</td>
          <td></td>
          <td></td>
          <td></td>
          <td class="a_r">'.number_format($entita->cena_dop,2,',', ' ').' Kč</td>
          <td class="a_r">'.number_format($celkova_cena + $entita->cena_dop,2,',', ' ').' Kč</td>
        </tr>
        </table>';    
    $this->setView();

  }

  public function treeNav() {

    $this->registry->template->set('is_tree_nav', true, true);
    $this->registry->template->set('akt_nazev', 'Moje objednávky');
//Debug::getVar($this->registry->template);
  }
}
?>