<?php

class Lib_Basket
{
	public function update(){
		$db = Zend_Registry::get('db');
		$basket = new Zend_Session_Namespace('basket');
		$order = new Zend_Session_Namespace('order');

		foreach( $order->product as $key => $v ){
		
			$idProduct = $v['idProduct']; 
		
		/*	
			echo $idProduct.",<br />";			
			var_dump($v);echo'<hr />';
		*/	
		
			$Product = new Product();	
			$select = $Product->select();				
			$select->where('product_id = ?', $idProduct);			
			$row = $Product->fetchRow($select);
			
			Lib_Basket::updateProduct($row,$key);
		
		//	$basket->product[$idProduct]['price'] = rand(0,10000)/100;
		}
	/*	
		var_export($basket->product);
		echo'<hr />';
		var_export($order->product);
	*/
	/*
		foreach( $order->product as $key => $v ){
			$idProduct = $v['idProduct'];
			
			$order->product[$key]['price_update'] = 0;
			if( $order->product[$key]['price'] != $basket->product[$idProduct]['price'] ){
				$order->product[$key]['price_update'] = 1;
			}			
			
			$order->product[$key]['price'] = $basket->product[$idProduct]['price'];	
		}
	*/
	}	
	
	
	
	public function updateProduct($row,$idProduct){
	
	//	$basket = new Zend_Session_Namespace('basket');
		$basket = new Zend_Session_Namespace('order');

			$wysokosc = $basket->product[$idProduct]['product_height'];
			$szerokosc = $basket->product[$idProduct]['product_width'];
			$glebokosc = $basket->product[$idProduct]['product_depth'];
			$iloscpolek = $basket->product[$idProduct]['product_shelfamount'];
			
			$CENA_STALA = $row->product_price;
			
			
			$cena_j_korpusu = 0;	
			$cena_j_plecow = 0;		
			$cena_j_obrzeza = 0;			
			if( $row['product_body'] == 1 AND $basket->product[$idProduct]['product_body_id'] > 0 ){		
			
				$ProductBody = new ProductBody();
				
				$select = $ProductBody->select();				
				$select->where('product_body_id = ?', $basket->product[$idProduct]['product_body_id']);		
				$row2 = $ProductBody->fetchRow($select); 		
				$cena_j_korpusu = $row2->product_body_price;

				if( $row2->product_body_type == 0 ){
					// obrzeze
					if( $basket->product[$idProduct]['product_rim_id'] == 1 ) $cena_j_obrzeza = $row->product_rim1_price;
					if( $basket->product[$idProduct]['product_rim_id'] == 2 ) $cena_j_obrzeza = $row->product_rim2_price;
					if( $basket->product[$idProduct]['product_rim_id'] == 3 ) $cena_j_obrzeza = $row->product_rim3_price;
					if( $basket->product[$idProduct]['product_rim_id'] == 4 ) $cena_j_obrzeza = $row->product_rim4_price;
				}
				
				$select = $ProductBody->select();				
				$select->where('product_body_id = ?', $basket->product[$idProduct]['product_body2_id']);		
				$row2 = $ProductBody->fetchRow($select); 		
				$cena_j_plecow = $row2->product_body_price;	
				
			}
			
			$powierzchnia_plyty = (($wysokosc*$glebokosc)*2+($szerokosc*$glebokosc*2)+($szerokosc*$glebokosc*$iloscpolek))/1000000;
			$ilosc_obrzeza = ($wysokosc*2 + $szerokosc*3 + $szerokosc*$iloscpolek)/1000;
			$powierzchnia_plyty_plecowej = $wysokosc*$szerokosc/1000000;
			
			$CENA_KORPUSU = ($powierzchnia_plyty*$cena_j_korpusu) + ($ilosc_obrzeza*$cena_j_obrzeza) + ($powierzchnia_plyty_plecowej*$cena_j_plecow);
			
			
			
			if( $basket->product[$idProduct]['front_filtr'] != 'no' ){
			
				$cena_j_frontu = 0;			
				if( $row['product_front'] == 1 AND $basket->product[$idProduct]['product_front_id'] > 0 ){				
					$ProductFront = new ProductFront();
					$select = $ProductFront->select();				
					$select->where('product_front_id = ?', $basket->product[$idProduct]['product_front_id']);			
					$row2 = $ProductFront->fetchRow($select); 		
					$cena_j_frontu = $row2->product_front_price;

					$cena_j_obrzezaf = 0;
					if( $row2->product_front_type != 1 ){
						if( $basket->product[$idProduct]['product_rim2_id'] == 1 ) $cena_j_obrzezaf = $row->product_rim1_price;
						if( $basket->product[$idProduct]['product_rim2_id'] == 2 ) $cena_j_obrzezaf = $row->product_rim2_price;
						if( $basket->product[$idProduct]['product_rim2_id'] == 3 ) $cena_j_obrzezaf = $row->product_rim3_price;
						if( $basket->product[$idProduct]['product_rim2_id'] == 4 ) $cena_j_obrzezaf = $row->product_rim4_price;
					}				
				}
				
				$powierzchnia_plyty = (($wysokosc*$szerokosc))/1000000;
				$ilosc_obrzeza = ($wysokosc*2 + $szerokosc*2)/1000;
				
				$CENA_FRONTU = ($powierzchnia_plyty*$cena_j_frontu) + ($ilosc_obrzeza*$cena_j_obrzezaf);
			
			}
				
			if( $row['product_type'] == 1 AND $basket->product[$idProduct]['product_type_id'] > 0 ){				
				$ProductType = new ProductType();
				$select = $ProductType->select();				
				$select->where('product_type_id = ?', $basket->product[$idProduct]['product_type_id']);			
				$row2 = $ProductType->fetchRow($select); 		
				$CENA_AKCESORIOW = $CENA_AKCESORIOW + $row2->product_type_price;				
			}
			
				
			if( $row['product_accessories'] == 1 AND $basket->product[$idProduct]['product_accessories_id'] > 0 ){				
				$ProductAccessories = new ProductAccessories();
				$select = $ProductAccessories->select();				
				$select->where('product_accessories_id = ?', $basket->product[$idProduct]['product_accessories_id']);			
				$row2 = $ProductAccessories->fetchRow($select); 		
				$CENA_AKCESORIOW = $CENA_AKCESORIOW + $row2->product_accessories_price;				
			}
			
				
			if( $row['product_accessories'] == 1 AND $basket->product[$idProduct]['product_accessories_id2'] > 0 ){				
				$ProductAccessories = new ProductAccessories();
				$select = $ProductAccessories->select();				
				$select->where('product_accessories_id = ?', $basket->product[$idProduct]['product_accessories_id2']);			
				$row2 = $ProductAccessories->fetchRow($select); 		
				$CENA_AKCESORIOW = $CENA_AKCESORIOW + $row2->product_accessories_price;				
			}
			
				
			if( $row['product_accessories'] == 1 AND $basket->product[$idProduct]['product_accessories_id3'] > 0 ){				
				$ProductAccessories = new ProductAccessories();
				$select = $ProductAccessories->select();				
				$select->where('product_accessories_id = ?', $basket->product[$idProduct]['product_accessories_id3']);			
				$row2 = $ProductAccessories->fetchRow($select); 		
				$CENA_AKCESORIOW = $CENA_AKCESORIOW + $row2->product_accessories_price;				
			}
			
				
			if( $row['product_accessories'] == 1 AND $basket->product[$idProduct]['product_accessories_id4'] > 0 ){				
				$ProductAccessories = new ProductAccessories();
				$select = $ProductAccessories->select();				
				$select->where('product_accessories_id = ?', $basket->product[$idProduct]['product_accessories_id4']);			
				$row2 = $ProductAccessories->fetchRow($select); 		
				$CENA_AKCESORIOW = $CENA_AKCESORIOW + $row2->product_accessories_price;				
			}
			if( $row['product_legs'] == 1 AND $basket->product[$idProduct]['product_legs_id'] > 0 ){				
				$ProductLegs = new ProductLegs();
				$select = $ProductLegs->select();				
				$select->where('product_legs_id = ?', $basket->product[$idProduct]['product_legs_id']);			
				$row2 = $ProductLegs->fetchRow($select); 		
				$CENA_AKCESORIOW = $CENA_AKCESORIOW + $row2->product_legs_price;				
			}
			
				
			if( $row['product_handles'] == 1 AND $basket->product[$idProduct]['product_handles_id'] > 0 ){				
				$ProductHandles = new ProductHandles();
				$select = $ProductHandles->select();				
				$select->where('product_handles_id = ?', $basket->product[$idProduct]['product_handles_id']);			
				$row2 = $ProductHandles->fetchRow($select); 		
				$CENA_AKCESORIOW = $CENA_AKCESORIOW + $row2->product_handles_price;				
			}


			
			if( $row['product_options'] == 1 ){	
			
				$cenawysyn = 0;
				if( $basket->product[$idProduct]['product_options_1'] > 0 ) $cenawysyn = $cenawysyn + $row->product_options_1;
				if( $basket->product[$idProduct]['product_options_2'] > 0 ) $cenawysyn = $cenawysyn + $row->product_options_2;
				if( $basket->product[$idProduct]['product_options_3'] > 0 ) $cenawysyn = $cenawysyn + $row->product_options_3;
				if( $basket->product[$idProduct]['product_options_4'] > 0 ) $cenawysyn = $cenawysyn + $row->product_options_4;
			
				$CENA_AKCESORIOW = $CENA_AKCESORIOW + $cenawysyn;				
			}
			
			
			if( $row['product_options2'] == 1 ){	
				if( $basket->product[$idProduct]['product_options_id'] == 0 ) $cenawieniec = $row->product_options_ww0;
				if( $basket->product[$idProduct]['product_options_id'] == 1 ) $cenawieniec = $row->product_options_ww1;
						
				$CENA_AKCESORIOW = $CENA_AKCESORIOW + $cenawieniec;	
			}
			
			
			
			if( $row['product_price_scheme'] == 0 ){	
				$cena_calkowita = $CENA_STALA * 1.05 + $CENA_KORPUSU * 1.05 + $CENA_FRONTU * 1.4 + $CENA_AKCESORIOW * 1.27 + $CENA_DOD_USLUG;
			}
			if( $row['product_price_scheme'] == 1 ){	
				$cena_calkowita = $CENA_STALA * 1.05 + $CENA_KORPUSU * 1.05 + $CENA_FRONTU * 1.4 + $CENA_AKCESORIOW * 1.27 + $CENA_DOD_USLUG;
			}
			if( $row['product_price_scheme'] == 2 ){	
				$cena_calkowita = $CENA_STALA * 1.05 + $CENA_KORPUSU * 1.05 + $CENA_FRONTU * 1.4 + $CENA_AKCESORIOW * 1.27 + $CENA_DOD_USLUG;
			}
			if( $row['product_price_scheme'] == 3 ){	
				$cena_calkowita = $CENA_STALA * 1.05 + $CENA_KORPUSU * 1.05 + $CENA_FRONTU * 1.4 + $CENA_AKCESORIOW * 1.27 + $CENA_DOD_USLUG;
			}
			if( $row['product_price_scheme'] == 4 ){	
				$cena_calkowita = $CENA_STALA * 1.05 + $CENA_KORPUSU * 1.05 + $CENA_FRONTU * 1.4 + $CENA_AKCESORIOW * 1.27 + $CENA_DOD_USLUG;
			}
			//	$cena_calkowita = $cena_calkowita + rand(0,100);
			$basket->product[$idProduct]['price_update'] = 0;
			if( $basket->product[$idProduct]['price'] != $cena_calkowita ){
				$basket->product[$idProduct]['price_update'] = 1;
			}			
			$basket->product[$idProduct]['price'] = $cena_calkowita;	
	
	}
	
}
