<?php

SEF('INDEKS24_SQIX');


error_reporting(E_ALL);

echo "<pre>";

echo "// plan 
//	 1) robimy INDEKS24 z 20 ostatnich miesiecy
//	 2) szukamy projektow telewizyjnych dla typu TV__UMOWA ZAKUPU KANALU 
//   3) szukamy ich podprojektow, jezeli sa 
//	 4) robimy raport - z tabeli RAPORT_PROJECT_COSTS -> DATA_PROJ[ID][RAPORT][ROK]
//		a) roczny
//		b) inkrementalny - zmiany gora/dol
//		
//";
	
 //AD1
 $INDEX24=INDEKS24_SQIX(20,2); //20 w tyl , 2 w przod

 //AD2
  $ZAP_SQL="select * from IN7_MK_BAZA_DYSTRYBUCJI where M_DIST_TYPE='TV__UMOWA ZAKUPU KANALU' and A_STATUS!='DELETED' and ID=471 ";
	ZAP_SQL($ZAP_SQL) ;
	while($RAD2=mysql_fetch_object($result)) {		
		$DATA_PROJ[$RAD2->ID]['M_DIST_DESC']=$RAD2->M_DIST_DESC;
		$DATA_PROJ[$RAD2->ID]['TV_RAPORTOWANIE_EMAIL']=$RAD2->TV_RAPORTOWANIE_EMAIL;
	}
	
	
foreach($DATA_PROJ as $DPI=>$DPA) {
 //AD3
  $ZAP_SQL="select * from IN7_MK_BAZA_DYSTRYBUCJI where P_ID like '".$DPI."' and A_STATUS!='DELETED' ";
	ZAP_SQL($ZAP_SQL) ;
	while($RAD3=mysql_fetch_object($result)) {
		$DATA_PROJ[$DPI]['CHILDS'][$RAD3->ID]=$RAD3->M_DIST_DESC;	
	}
}



//AD4  select  
foreach($DATA_PROJ as $DPI=>$DPA) {
	//

	foreach($INDEX24 as $I24I=>$I24A) {
			$ZAP_SQL="select * from RAPORT_PROJECT_COSTS where ID_PROJECT='".$DPI."' and RPAD_DATA='".$I24A->DATA."' ";
//			$ZAP_SQL="select * from RAPORT_PROJECT_COSTS where ID_PROJECT='".$DPI."'  and RPAD_DATA='".$I24AA['ASDF']."'";

//			echo $ZAP_SQL." \n";
		    ZAP_SQL($ZAP_SQL);
			$i=2	; 
			flush();
			while($RAD4=mysql_fetch_object($result)){
				if(fmod($i,2)==0)
				$DATA_PROJ[$DPI]['RAPORT'][$I24A->ROK][$I24A->MIESIAC][$RAD4->ID_OFFER]['ILOSC_FAKTUR'][$RAD4->ILOSC_FAKTUR]=$RAD4->DATA_COUNT;
			    else 
				$DATA_PROJ[$DPI]['RAPORT'][$I24A->ROK][$I24A->MIESIAC][$RAD4->ID_OFFER]['COST_POWER'][$RAD4->COST_POWER]=$RAD4->DATA_COUNT;
				$i++;
			}

			//Tutaj powinnismy wydrukowac podsumowanie 
	
	}
				echo "<hr> Dla projektu ".$DPI." desc: ".$DPA['M_DIST_DESC']." nalezy zraportowac nastepujace ilosci licencji :<br>";
				
					echo "Roznice roczne year(NOW)-year(NOW-1) :<br>";
					unset($RAPORTER);
					$RAPORTER['TERAZROK']=date('Y');
					$RAPORTER['STARYROK']=$RAPORTER['TERAZROK']-1;

						foreach($DATA_PROJ[$DPI]['RAPORT'][$RAPORTER['STARYROK']] as $R72I=>$R72A) { //miesiace
						echo "\nJade miesiac:".$R72I;
							foreach($R72A as $R73I=>$R73A) {  //projekty
								foreach($R73A['ILOSC_FAKTUR'] as $R74I=>$R74A ) { //ILOSC_FAKTUR

									if($R74I>0) $DATA_PROJ[$DPI]['RAPORTER']['STARYROK_R74I_more_0']+=$R74A;
									
								}
							}
						}
						foreach($DATA_PROJ[$DPI]['RAPORT'][$RAPORTER['TERAZROK']] as $R72I=>$R72A) { //miesiace
						echo "\nJade miesiac:".$R72I;
							foreach($R72A as $R73I=>$R73A) {  //projekty
								foreach($R73A['ILOSC_FAKTUR'] as $R74I=>$R74A ) { //ILOSC_FAKTUR

									if($R74I>0) $DATA_PROJ[$DPI]['RAPORTER']['TERAZROK_R74I_more_0']+=$R74A;
									
								}
							}
						}						
						echo "Roznice miesieczne  :<br>";
						foreach($DATA_PROJ[$DPI]['RAPORT'][$RAPORTER['TERAZROK']] as $R72I=>$R72A) { //miesiace
						echo "\nJade miesiac:".$R72I;
							foreach($R72A as $R73I=>$R73A) {  //projekty
								foreach($R73A['ILOSC_FAKTUR'] as $R74I=>$R74A ) { //ILOSC_FAKTUR
								echo "\nJade ilosc_faktur:".$R74I." ".$R74A;

									if($R74I>0) $DATA_PROJ[$DPI]['RAPORTER']['TERAZMIESIAC_R74I_more_0'][$R72I]+=$R74A;
									
								}
							}
						}
						
						
					print_r($RAPORTER);
				print_r($DPA);
				flush();
			//	die();

}


print_r($DATA_PROJ);


print_r($INDEX24);
echo "</pre>";


?>
