<?php

//3.5.3 AKORD ILOSCI PODPISANYCH UMÓW = $BUDZET['AKORD']['HANDL-UMOWY']['NOWE']

	//SZUKAMY PODPISANYCH UMÓW W DANYM OKRESIE
		//SPRAWDZAMY ICH RENTOWNOŚĆ
		//SPRAWDZAMY Z JAKIEGO SĄ REJONU- CZY NOWE CZY STARE?
		
	   //Analiuzjemy arraya i liczymy nowa sprzedaz na starych oraz nowych rewirach 

	$SQL="select t1.P_DEAL_DATE_NEW , t1.P_DEALTERM_NEW, t1.MONTH_COST_NEW , t1.P_DEALTERM_OLD , t1.MONTH_COST_OLD, t1.S_ADDRESS_STREET,
		if(t1.P_DEALTERM_OLD>t1.P_DEAL_DATE_NEW,PERIOD_DIFF(DATE_FORMAT(t1.P_DEALTERM_OLD,'%Y%m'),DATE_FORMAT(t1.P_DEAL_DATE_NEW,'%Y%m'))  ,'0') as DIFF_STRATA , 
		PERIOD_DIFF(DATE_FORMAT(t1.P_DEALTERM_NEW,'%Y%m'),DATE_FORMAT(t1.P_DEAL_DATE_NEW,'%Y%m')) as DIFF_ZYSK , 
		if(t3.T_WHEN_CONNECTED like '%-%-%',
		 PERIOD_DIFF(
			DATE_FORMAT(now(),'%Y%m'),DATE_FORMAT(t3.T_WHEN_CONNECTED,'%Y%m')
		 )
		,'100') as T3_T_WHEN_CONNECTED , t1.P_SERVICE 
		from USERS2_DEALS as t1 
		left join USERS2_MARKETING as t2 on t1.S_ADDRESS_STREET=t2.S_ADDRESS_STREET
		left join BUILDINGS as t3 on t2.T_TELBOX_BUILDING_IN=t3.S_ADDRESS_STREET
		
		 where t1.P_DEAL_DATE_NEW like '".$DATA."' and t1.A_STATUS!='DELETED' and t1.P_DEALTERM_NEW!='0000-00-00' and t1.P_DEALTERM_NEW>0 
		and t1.MONTH_COST_NEW>0 ";
	echo $SQL."\n ";
	$res=mysql_query($SQL);
	$i=0;
	while($h=mysql_fetch_array($res)) {
		$BUDZET['AKORD']['HANDL-UMOWY']['NOWE'][$i]['ADRES']=$h['S_ADDRESS_STREET'];
		$BUDZET['AKORD']['HANDL-UMOWY']['NOWE'][$i]['ZYSK']=$h['DIFF_ZYSK']*$h['MONTH_COST_NEW'];
		$BUDZET['AKORD']['HANDL-UMOWY']['NOWE'][$i]['STRATA']=$h['DIFF_STRATA']*$h['MONTH_COST_OLD'];
		$BUDZET['AKORD']['HANDL-UMOWY']['NOWE'][$i]['DNI_OD_ZASIEDLENIA']=$h['T3_T_WHEN_CONNECTED'];
		$BUDZET['AKORD']['HANDL-UMOWY']['NOWE'][$i]['P_SERVICE']=$h['P_SERVICE'];
		$BUDZET['AKORD']['HANDL-UMOWY']['NOWE'][$i]['S_ADDRESS_STREET']=$h['S_ADDRESS_STREET'];
		$BUDZET['AKORD']['HANDL-UMOWY']['NOWE'][$i]['P_DEALTERM_OLD']=$h['P_DEALTERM_OLD'];
		$BUDZET['AKORD']['HANDL-UMOWY']['NOWE'][$i]['P_DEALTERM_NEW']=$h['P_DEALTERM_NEW'];
		$BUDZET['AKORD']['HANDL-UMOWY']['NOWE'][$i]['MONTH_COST_OLD']=$h['MONTH_COST_OLD'];

//		$BUDZET['AKORD']['HANDL-UMOWY']['NOWE'][$i]['STRING']=$h['P_DEALTERM_OLD']."--".$h['P_DEAL_DATE_NEW'];

		$i++;
	}

	foreach( $BUDZET['AKORD']['HANDL-UMOWY']['NOWE'] as $VA) {
		if($VA['DNI_OD_ZASIEDLENIA'] > 6) {
			$GRUPA="stare zasoby OK";
		} else {
			$GRUPA="nowe zasoby NIE OK";
		}
		if($VA['P_DEALTERM_OLD'] != '0000-00-00') $ANEKS=" ANEKS";
		else $ANEKS=" NOWE";
		$GRUPA.=$ANEKS;

		 	$BUDZET['AKORD']['HANDL-UMOWY']['RAPORT'][$VA['P_SERVICE']][$GRUPA]['ZYSK']+=$VA['ZYSK'];
		 	$BUDZET['AKORD']['HANDL-UMOWY']['RAPORT'][$VA['P_SERVICE']][$GRUPA]['STRATA']+=$VA['STRATA'];
		 	$BUDZET['AKORD']['HANDL-UMOWY']['RAPORT'][$VA['P_SERVICE']][$GRUPA]['COUNT']++;
		 	$BUDZET['AKORD']['HANDL-UMOWY']['RAPORT'][$VA['P_SERVICE']][$GRUPA]['STRING'].=$VA['S_ADDRESS_STREET'].", ";

	}
if(!$_SESSION['DEBUG'])	unset( $BUDZET['AKORD']['HANDL-UMOWY']['NOWE'] );


print_r($BUDZET['AKORD']['HANDL-UMOWY']);


?>
