<?php
class Kosik extends Super {

  protected $id_uziv;
  protected $koeficient_skupina;
  protected $produkty = array();
  public $doprava_zdarma = false;
  public $doprava = null;
  public $id_doprava = false;
  public $adresa_dodaci  = array();
  public $adresa_fakturacni = array();
  public $id_dodaci 		= null;
  public $id_fakturacni = null;
  public $celkovy_pocet  = 0;
  public $celkova_cena	 = 0;

  public function __construct() {
  	$registry = Registry::getInstance();
  	$this->id_uziv = $registry->user->id;
  	$this->koeficient_skupina = $registry->user->koeficient;
  }

  public function __destruct() {
		$_SESSION['kosik'] = serialize($this);
  }
  
  public function pridat($id_produktu, $mnozstvi) {
  	if ($mnozstvi > 0) {
    	if (key_exists($id_produktu, $this->produkty)) {
    		$this->produkty[$id_produktu]['mnozstvi'] += $mnozstvi;
    		$this->celkovy_pocet += $mnozstvi;
    		$this->celkova_cena += $mnozstvi * $this->produkty[$id_produktu]['celkova_cena'];
    		$ret = '<b>&nbsp;&nbsp;Produkt byl přidán.</b>';
    	}
    	else {
    		$result = dibi::query('SELECT DISTINCT E.ID, E.NAZEV, E.REWRITE_LINK, 
        	if(SLEVA_OD < curdate() AND SLEVA_DO > curdate(), CEIL(E.CENA *	(1-SLEVA_PROCENT)) * '.$this->koeficient_skupina.', E.CENA * '.$this->koeficient_skupina.') AS CENA,
  				if(SLEVA_OD < curdate() AND SLEVA_DO > curdate(), CEIL(E.CENA * D.KOEFICIENT * '.$this->koeficient_skupina.' *
      			(1-SLEVA_PROCENT) + E.RECYKLACNI_POPLATEK + E.AUTORSKY_POPLATEK),
      			CEIL(E.CENA * D.KOEFICIENT * '.$this->koeficient_skupina.' + E.RECYKLACNI_POPLATEK + E.AUTORSKY_POPLATEK)) AS CELK_CENA,
        	E.RECYKLACNI_POPLATEK, E.AUTORSKY_POPLATEK,E.DOPRAVA_ZDARMA, D.KOEFICIENT
      		FROM PRODUKT E
      		INNER JOIN DAN D ON D.ID = E.ID_DAN
      		LEFT JOIN OBRAZEK_PRODUKTU OP ON OP.ID_PROD = E.ID AND OP.PORADI = 1
      		LEFT JOIN VYROBCE V ON V.ID = E.ID_VYR
      		WHERE E.STAV IN (2,3,5,6) AND E.ID = %i ',$id_produktu);
    		
    		if (count($result) == 1) {
  				$rows = $result->fetchAll();
  				$row = $rows[0];
  
  				$item['id'] = $row['ID'];
  				$item['mnozstvi'] = $mnozstvi;
  				$item['cena'] = $row['CENA'];
  				$item['celkova_cena'] = $row['CELK_CENA'];
  				$item['recyklacni_poplatek'] = $row['RECYKLACNI_POPLATEK'];
  				$item['autorsky_poplatek'] = $row['AUTORSKY_POPLATEK'];
  				$item['dan_koef'] = $row['KOEFICIENT'];
  				if ($row['DOPRAVA_ZDARMA'] == 1) {
  					$this->doprava_zdarma = true;
  				}
  				$this->produkty[$id_produktu] = $item; 
  				$this->celkovy_pocet += $mnozstvi;
  				$this->celkova_cena  += ($mnozstvi * $item['celkova_cena']);
    			$ret = '<b>&nbsp;&nbsp;Produkt byl přidán.</b>';

    		}
    		else {
    			$ret = '<b>&nbsp;&nbsp;Produkt nelze přidat.</b>';
    		}
    	}
 			return $ret;
  	}
  }
  
  public function souhrn() {
  	switch ($this->celkovy_pocet) {
  		case 1:
  			$pocet = $this->celkovy_pocet.' položka';
  			break;

  		case 2:
  		case 3:
  		case 4:
  			$pocet = $this->celkovy_pocet.' položky';
  			break;
  			
  		default:
  			$pocet = $this->celkovy_pocet.' položek';
  			break;
  	}

  	$ret = '<p>Celkem položek:</p>
     <p class="light_text">'.$pocet.'</p>
     <p>Celková cena:</p>
     <p class="light_text">'.number_format($this->celkova_cena,0,',',' ').' Kč</p>';
  	
  	return $ret;
  }

  public function upravit($mnozstvi) {

  	foreach($mnozstvi as $key=>$value) {
  		if ($value > 0) {
  			$this->produkty[$key]['mnozstvi'] = $value;
  		}
  		else {
  			unset($this->produkty[$key]);
  		}
  	}

		$this->prepocitej();
  }

  public function prepocitej() {
  	
  	$registry = Registry::getInstance();
  	if ($registry->user->id != $this->id_uziv) {
  		$this->nactizDB();
  	}
  	else {
  		
  		$this->celkova_cena = 0;
  		$this->celkovy_pocet = 0;
  		foreach($this->produkty as $key=>$value) {

    		$this->celkova_cena  += $value['celkova_cena'] * $value['mnozstvi'];
    		$this->celkovy_pocet += $value['mnozstvi'];

  		}
  	}
  }

  public function nactizDB() {

  	if (count($this->produkty) > 0) {
    	$keys = array_keys($this->produkty);
    	$mnozstvi = array();
  		foreach($this->produkty as $key=>$value) {
  			$mnozstvi[$key] = $value['mnozstvi'];
  		}
  		$this->produkty = array();
			$this->id_doprava = false;
			$this->adresa_dodaci  = array();
			$this->adresa_fakturacni = array();
			$this->id_dodaci 		 = null;
			$this->id_fakturacni = null;
			$this->celkovy_pocet = 0;
			$this->celkova_cena	 = 0;

			$registry = Registry::getInstance();
  		$this->id_uziv = $registry->user->id;
  		$this->koeficient_skupina = $registry->user->koeficient;
  	
			$result = dibi::query('SELECT DISTINCT E.ID, E.NAZEV, E.REWRITE_LINK, 
        	if(SLEVA_OD < curdate() AND SLEVA_DO > curdate(), CEIL(E.CENA *	(1-SLEVA_PROCENT)), E.CENA) AS CENA,
  				if(SLEVA_OD < curdate() AND SLEVA_DO > curdate(), CEIL(E.CENA * D.KOEFICIENT * '.$this->koeficient_skupina.' *
      			(1-SLEVA_PROCENT) + E.RECYKLACNI_POPLATEK + E.AUTORSKY_POPLATEK),
      			CEIL(E.CENA * D.KOEFICIENT * '.$this->koeficient_skupina.' + E.RECYKLACNI_POPLATEK + E.AUTORSKY_POPLATEK)) AS CELK_CENA,
        	E.RECYKLACNI_POPLATEK, E.AUTORSKY_POPLATEK,E.DOPRAVA_ZDARMA, D.KOEFICIENT
      		FROM PRODUKT E
      		INNER JOIN DAN D ON D.ID = E.ID_DAN
      		LEFT JOIN OBRAZEK_PRODUKTU OP ON OP.ID_PROD = E.ID AND OP.PORADI = 1
      		LEFT JOIN VYROBCE V ON V.ID = E.ID_VYR
      		WHERE E.STAV IN (2,3,5,6) AND E.ID = %and ',$keys);
			
			foreach($result as $row) {
				  
				$item['id'] = $row['ID'];
				$item['mnozstvi'] = $mnozstvi[$row['ID']];
				$item['cena'] = $row['CENA'];
				$item['celkova_cena'] = $row['CELK_CENA'];
				$item['recyklacni_poplatek'] = $row['RECYKLACNI_POPLATEK'];
				$item['autorsky_poplatek'] = $row['AUTORSKY_POPLATEK'];
				$item['dan_koef'] = $row['KOEFICIENT'];
				if ($row['DOPRAVA_ZDARMA'] == 1) {
					$this->doprava_zdarma = true;
				}
				$this->produkty[$row['ID']] = $item;
				$this->celkovy_pocet += $mnozstvi[$row['ID']];
				$this->celkova_cena  += ($mnozstvi[$row['ID']] * $item['celkova_cena']);
			}
  	}
  }

  public function doObjednavky($id_obj) {
  	
  	$values = array();
  	foreach($this->produkty as $item) {
  		$values[] = ('('.$item['id'].', '.$id_obj.', '.$item['cena'].', '.$item['dan_koef'].',
  			'.$item['recyklacni_poplatek'].', '.$item['autorsky_poplatek'].', '.$item['mnozstvi'].', 1  )');
  	}

  	dibi::query('INSERT INTO PRODUKTY_OBJEDNAVKA (ID_PROD, ID_OBJ, CENA, DAN,
  		RECYKLACNI_POPLATEK, AUTORSKY_POPLATEK, MNOZSTVI_OBJ, STAV) VALUES %sql',implode(',', $values));

  }

  public function smazat() {

  	$this->celkova_cena = 0;
  	$this->celkovy_pocet = 0;
  	$this->doprava_zdarma = false;
  	$this->produkty = array();
		$this->id_doprava = null;
		$this->doprava = null;
		$this->adresa_dodaci  = array();
		$this->adresa_fakturacni = array();
		$this->id_dodaci 		 = null;
		$this->id_fakturacni = null;

  	$registry = Registry::getInstance();
  	$this->id_uziv = $registry->user->id;
  	$this->koeficient_skupina = $registry->user->koeficient;

  }


  public function getArray() {
  	return $this->produkty;
  }

  
  function __sleep() {
    //vytvoříme pole vlastností objektu
    $vlastnosti = get_object_vars($this);
    foreach ($vlastnosti as $klic => $hodnota) {
      $pole[] = $klic;
    }
    return $pole;

  }

  function __wakeup() {

  	$registry = Registry::getInstance();
  	if ($registry->user->id != $this->id_uziv) {
  		$this->nactizDB();
  	}
    Debug::addPoint( __CLASS__.' - '.__METHOD__, $this);
  }

}
?>