<?php
function ID_OFFERS_GROUP($COMMAND,$ARG1_VAL) {
global $result,$thiss,$error,$conn;
 $field_name='ID_OFFERS_GROUP';
 
   //DEBUG_S(-3,'_POST',$_POST,__FILE__, __FUNCTION__, __LINE__);

 
 if($COMMAND=='POST_VERIFY') { 
 
  
 
 echo $_REQUEST['ID_OFFERS_GROUP']."***";
  DEBUG_S(-3,'_REQUEST',$_REQUEST,__FILE__, __FUNCTION__, __LINE__);
  
  
  
  		//mozliwosci modulu @2013-01-10
  	    //TYLKO NOWA UMOWA ==P_DEALNUMBER_OLD=''
  	    //
  	    
  	    
  	    	
  	
  
  		//jezeli jest zaznaczona flaga dodawania uslug powiazanych, to : 
 		if(!empty($_REQUEST['DEALS_TABLE_COLUMN__USERS2_OFFERS_GROUPS_OPCJE__ADD_TO_DEALS'])) {
  		
  		
  		 if(!empty($_REQUEST['P_DEALNUMBER_OLD'])) {
  		 	$error.= " MODUL NIE UMIE ROBIC ANEKSOW P_DEALNUMBER_OLD ";
  		 	die($error);
  		 }
  		
			settype($_REQUEST['ID_OFFERS_GROUP'], "integer");

  			if($_REQUEST['ID_OFFERS_GROUP']>0) {

  			
  			
  			if(!isset($_REQUEST['ID_OFFERS_GROUP_OPTIONS_WERE_SET'])) {
 		 			//wstrzymamy dodawanie uslugi - wyswietlimy opcje, jakie klient zamowil
 		 			
 		 			$SQL="select t2.ID as `key`, concat_ws(' ', t2.P_SERVICE ,t2.P_SERVICE_PARENT ,  t2.P_NORMAL_PRICE ,'pln ; ',  t2.S_ALIAS, t2.P_SERVICE_PARENT) as description ,t2.ID_PRODUKT as `group_key`		
 		 								from USERS2_OFFERS_GROUPS as t1 					    	left join USERS2_OFFERS as t2 on ((t2.ID=t1.ID_NET or 	
 		 								(t2.P_SERVICE='OPCJE' and t1.ID_NET>0 and t2.P_SERVICE_PARENT='NET') or 
 		 								t2.ID=t1.ID_TEL or 
 		 								(t2.P_SERVICE='OPCJE' and t1.ID_TEL>0 and t2.P_SERVICE_PARENT='TEL') or 
 		 								t2.ID=t1.ID_TV or
 		 								(t2.P_SERVICE='OPCJE' and t1.ID_TV>0 and t2.P_SERVICE_PARENT='TV') or
 		 								t2.ID=t1.ID_TVC or (t2.P_SERVICE='OPCJE' and t1.ID_TVC>0 and t2.P_SERVICE_PARENT='TVC') or 
 		 								t2.ID=t1.ID_TVCP or (t2.P_SERVICE='OPCJE' and t1.ID_TVCP>0 and t2.P_SERVICE_PARENT='TVCP')  					    		)) 					    	
 					  	where t1.ID='".addslashes($_REQUEST['ID_OFFERS_GROUP'])."' order by t2.ID_PRODUKT";
 		 			
// 		 			echo $SQL;
 		 			ZAP_SQL($SQL);
 		 			echo "\n<form method='POST'>";
 	//	 			echo "<script>
 //		 			
// 		 			</script>";

 		 			while($h=mysql_fetch_object($result)) {
 		 			
 		 				echo "\n<p class='GRUPA__".$h->group_key."'><input type=checkbox name='ID_OFFERS_GROUP_OPTIONS_WERE_SET_ID__".$h->key."'>(".$h->key.")<b> Grupa:".$h->group_key." </b>".$h->description."</p>";
 		 			}
 		 			//generujemy formularz z przekazanymi wczesniej zmiennymi 
 		 			//print_r($_REQUEST);
 		 			foreach($_REQUEST as $req_fld=>$req_val) {
 		 				echo "\n<input type=hidden name='".$req_fld."' value='".$req_val."'>";
 		 			
 		 			}
 		 			
 		 			
 		 			die("!!!<input type='submit' value='USTAL_OPCJE' name='ID_OFFERS_GROUP_OPTIONS_WERE_SET'></form>");
  			}
  					
  			
  			//zapierdalamy po po strukturze produktu, jezeli jest - tam jest ID_TEL, ID_TV, ID_NET
  			$sql="select ID_NET, ID_TEL, ID_TV, ID_TVC, ID_TVCP, ID_TVCP_1, ID_TVCP_2, ID_TVCP_3, ID_TVCP_4 from USERS2_OFFERS_GROUPS where ID='".addslashes($_REQUEST['ID_OFFERS_GROUP'])."'";
  			ZAP_SQL($sql);
  				while($h=mysql_fetch_object($result)) {
  					foreach($h as $s) {
  					   if($s)
  					   $id_offer_to_create[]=$s;
  					}
	  			}
	  		DEBUG_S(-3,'id_offer_to_create',$id_offer_to_create,__FILE__, __FUNCTION__, __LINE__);

	  			//teraz dla każdej z ofert budujemy wpisy do DEALS
  			
  			} else  { $error.=' ID_OFFERS_GROUP - brak komórki ';
					
				DEBUG_S(-3,'ID_OFFERS_GROUP',$_REQUEST['ID_OFFERS_GROUP'],__FILE__, __FUNCTION__, __LINE__);

  			}
  			
  			
  			//teraz lapiemy przekazane parametry z uslug dodatkowych, aby je dodac do ceny i wstawic odpowiednio w deals table
  			foreach($_REQUEST as $look_for_pars=>$val) {
  				$test=explode('__',$look_for_pars);
  				if($test[0]=='ID_OFFERS_GROUP_OPTIONS_WERE_SET_ID')
  					$id_opts_to_create[]=$test[1];
  			}
  		
  		
  			  	DEBUG_S(-3,'id_opts_to_create',$id_opts_to_create,__FILE__, __FUNCTION__, __LINE__);

  			  			echo "<hr>";
			print_r($_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID']);
  			//teraz pokolei jedziemy dodawanie do USERS2_DEALS na podstawie $id_offer_to_create
  			
  			foreach($id_offer_to_create as $USERS2_OFFERS_GROUPS) {
  				$sql="insert into USERS2_DEALS ( A_STATUS , A_STATUS_INFO , DATA_DOK, S_ADDRESS_STREET, S_OTHER_INFO, ID_BILLING_USERS_NEW , P_SERVICE_COMPANY, 
  							P_OFFER_ID_NEW, P_DEAL_DATE_NEW, P_DEALNUMBER_NEW, P_DEALTERM_NEW, MONTH_COST_NEW, INICJALY_HANDLOWCA , A_RECORD_CREATE_DATE,A_RECORD_CREATE_AUTHOR ,
  							P_SERVICE ) 
  				
  					select 'WAITING',  'wprowadzone automatycznie za pomoca kreatora sqix', dt.P_DEALDATE , dt.S_ADDRESS_STREET, dt.DEALDESC , dt.ID_BILLING_USERS , bu.BILLING_OWNER , 
  							of.ID ,  dt.P_DEALDATE , concat_ws('/',dt.ID,dt.P_DEALNUMBER), dt.P_DEALDATE_TERM, of.P_PROMO_MONTH_PRICE_NETTO , dt.P_DEALPREFIX , now(), '".$_SESSION['ADM_ACCOUNT']."' ,
							of.P_SERVICE
							from DEALS_TABLE as dt 
							left join BILLING_USERS as bu on bu.ID=dt.ID_BILLING_USERS
							 left join BILLING_USERS_ADD as bua on bua.id_users=bu.ID
							  left join USERS2_OFFERS as of on of.ID='".$USERS2_OFFERS_GROUPS."'
							  where dt.ID='".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID']."'
							  limit 1
  				 ";
  					mysql_query($sql);
  					$last_insert_id=mysql_insert_id(); //ostatni ID bedzie potrzebny aby zrobic P_ID w umowach
  					  DEBUG_S(-2,'LIID1 : '.$last_insert_id,$sql,__FILE__, __FUNCTION__, __LINE__);
  			//echo "\n<br> LID: ".$last_insert_id." for ".$sql;
  			
  				 foreach($id_opts_to_create as $USERS2_OFFERS_GROUPS) {
	
		  				$sql="insert into USERS2_DEALS ( A_STATUS , A_STATUS_INFO , DATA_DOK, S_ADDRESS_STREET, S_OTHER_INFO, ID_BILLING_USERS_NEW , P_SERVICE_COMPANY, 
  								P_OFFER_ID_NEW, P_DEAL_DATE_NEW, P_DEALNUMBER_NEW, P_DEALTERM_NEW, MONTH_COST_NEW, INICJALY_HANDLOWCA , A_RECORD_CREATE_DATE,A_RECORD_CREATE_AUTHOR ,
  							P_SERVICE ,P_ID) 
  				
  							select 'WAITING',  'wprowadzone automatycznie za pomoca kreatora sqix', dt.P_DEALDATE , dt.S_ADDRESS_STREET, dt.DEALDESC , dt.ID_BILLING_USERS , bu.BILLING_OWNER , 
  							of.ID ,  dt.P_DEALDATE , concat_ws('/',dt.ID,dt.P_DEALNUMBER), dt.P_DEALDATE_TERM, of.P_PROMO_MONTH_PRICE_NETTO , dt.P_DEALPREFIX , now(), '".$_SESSION['ADM_ACCOUNT']."' ,
							of.P_SERVICE , '".$last_insert_id."'
							from DEALS_TABLE as dt 
							left join BILLING_USERS as bu on bu.ID=dt.ID_BILLING_USERS
							 left join BILLING_USERS_ADD as bua on bua.id_users=bu.ID
							  left join USERS2_OFFERS as of on of.ID='".$USERS2_OFFERS_GROUPS."'
							   left join USERS2_DEALS as udp on udp.ID='".$last_insert_id."'
							   	where dt.ID='".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID']."' and of.P_SERVICE_PARENT=udp.P_SERVICE
								limit 1
		  				 ";

  					mysql_query($sql);
  				//	echo "\n<hr> LIID: ".$last_insert_id." for ".$sql;
  					DEBUG_S(-2,'LIID2: '.$last_insert_id,$sql,__FILE__, __FUNCTION__, __LINE__);
				}
  			  			
  			}
  			
  		}
  		
  //	foreach()
  
//  echo ($_SESSION['ADM_ACCOUNT']);
 // die();
	
  } else {
 if(($_SESSION['CURRENT_MENU']=="NEW_RECORD")||($_SESSION['CURRENT_MENU']==$thiss->DETECT_EDIT_FUNCTION)) {


  	if(strstr($_SESSION[$thiss->DETECT_TABLE_COLUMN]['PERMEDIT'][$field_name],"W")) {
		ZAP_SQL("select 1+1;");
		
		
		
		//najpierw sprawdzamy czy umowy juz nie ma w DEALS
		if(($_SESSION['CURRENT_MENU']=="NEW_RECORD")) $SQL="select ID,P_DEALNUMBER_NEW,P_SERVICE  from USERS2_DEALS where P_DEALNUMBER_NEW='".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['NEW_RECORD']['ID']."' and P_DEALNUMBER_NEW>0";
		else $SQL="select ID,P_DEALNUMBER_NEW,P_SERVICE from USERS2_DEALS  where P_DEALNUMBER_NEW='".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID']."' and P_DEALDATE_NEW>'2013-01-01'"; //
	//	echo $SQL;
		$res1=mysql_query($SQL);
		if(mysql_num_rows($res1)>0) echo "<h2>W systemie jest juz zarejestrowana powiazana struktura umow w tabeli USERS2_DEALS, pod numerami ";
		while($h=mysql_fetch_object($res1)) {
				echo " ".$h->ID." ".$h->P_SERVICE."(id deals:".$h->P_DEALNUMBER_NEW.") , ";
		}			
		if(mysql_num_rows($res1)>0) echo "</h2>";
		
		if(mysql_num_rows($res1)>0) { //bedziemy szukac czy sie daja aktywowac
			if($_SESSION['CURRENT_MENU']==$thiss->DETECT_EDIT_FUNCTION) {
				$SQL="select ID,P_DEALNUMBER_NEW,P_SERVICE from USERS2_DEALS where P_DEALNUMBER_NEW='".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID']."' and P_DEALNUMBER_NEW>0 and A_STATUS='WAITING' ";
						$res1=mysql_query($SQL);
						if(mysql_num_rows($res1)>0) echo "<h3>Strukture danych do uruchomienia mozna wprowadzic do URUCHOMIENIA oraz ROZLICZANIA w module @2013-01-10 TODO ";
						while($h=mysql_fetch_object($res1)) {
						echo " ".$h->ID." ".$h->P_SERVICE."(id deals:".$h->P_DEALNUMBER_NEW.") , ";
					}			
					if(mysql_num_rows($res1)>0) echo "</h2>";
		
			
			
			
			}
		}
	
		
			if(mysql_num_rows($res1)==0) {
			
					if(!empty($_REQUEST['JAVA_VALUE'])) {
					

						//if($_REQUEST['JAVA_FIELD']=='P_DEALNUMBER_OLD__JAVA_FILTR_ID_BILLING_USERS') {	
					   //  $where=" ID_BILLING_USERS='".$_REQUEST['JAVA_VALUE']."'";	
						// $_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD']['P_DEALNUMBER_OLD__JAVA_FILTR_ID_BILLING_USERS']=$_REQUEST['JAVA_VALUE'];
						//} else 
						if($_REQUEST['JAVA_FIELD']==$field_name.'__JAVA') {
					    $_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD'][$field_name.'__JAVA']=addslashes($_REQUEST['JAVA_VALUE']);
							//dodatkowy warunek odpalany wyzwoleniem z javy
						} else if($_REQUEST['JAVA_FIELD']==$field_name.'_OPTIONS__JAVA') {
							$_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD'][$field_name.'_OPTIONS__JAVA']=addslashes($_REQUEST['JAVA_VALUE']);
 					    }
					
					

					}
					
				if(!empty($_REQUEST['JAVA_GET_SELECT_TABLE_SELECT'])) {
				
					if($_REQUEST['JAVA_FIELD']==$field_name.'__JAVA') {
						$where=" and ID like '%".addslashes($_REQUEST['JAVA_VALUE'])."%' or S_ALIAS like '%".$_REQUEST['JAVA_VALUE']."%' or P_PROMOTION_NAME like '%".$_REQUEST['JAVA_VALUE']."%' ";
						$SQL="select  ID as `key`, concat_ws(' ',S_ALIAS,P_PROMOTION_NAME,P_OFFER_TERM_BEGIN,P_OFFER_TERM,P_SERVICE_COMPANY,M_REWIR) as description   from USERS2_OFFERS_GROUPS where ( P_SERVICE_COMPANY='BN'  or P_SERVICE_COMPANY='NETDAY' ) ".$where." order by ID DESC limit 25";
					} else if($_REQUEST['JAVA_FIELD']==$field_name.'_OPTIONS__JAVA') {
					//@2013-01 old sql : mialy sie dodawac opcje z javy, ale nie udalo sie
					//	$SQL="select t2.ID as `key`, concat_ws(' ', t2.P_SERVICE ,t2.P_SERVICE_PARENT ,  t2.P_NORMAL_PRICE ,  t2.S_ALIAS, t2.P_SERVICE_PARENT) as description ,t2.ID_PRODUKT as `group_key`							from USERS2_OFFERS_GROUPS as t1 					    	left join USERS2_OFFERS as t2 on ((t2.ID=t1.ID_NET or 					    			(t2.P_SERVICE='OPCJE' and t1.ID_NET>0 and t2.P_SERVICE_PARENT='NET') or 					    		t2.ID=t1.ID_TEL or 					    			(t2.P_SERVICE='OPCJE' and t1.ID_TEL>0 and t2.P_SERVICE_PARENT='TEL') or 					    		t2.ID=t1.ID_TV or					    			(t2.P_SERVICE='OPCJE' and t1.ID_TV>0 and t2.P_SERVICE_PARENT='TV') or 					    		 t2.ID=t1.ID_TVC or 					    			(t2.P_SERVICE='OPCJE' and t1.ID_TVC>0 and t2.P_SERVICE_PARENT='TVC') or 					    		 t2.ID=t1.ID_TVCP or					    		 	(t2.P_SERVICE='OPCJE' and t1.ID_TVCP>0 and t2.P_SERVICE_PARENT='TVCP')  					    		)) 					    	
					//  	where t1.ID='".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD'][$field_name.'_OPTIONS__JAVA']."'
					//	 ";
						$SQL="select t2.ID as `key`, concat_ws(' ', t2.P_SERVICE ,t2.P_SERVICE_PARENT ,  t2.P_NORMAL_PRICE ,  t2.S_ALIAS, t2.P_SERVICE_PARENT) as description ,t2.ID_PRODUKT as `group_key`
							from USERS2_OFFERS_GROUPS as t1 
					    	
					    	where t1.ID='".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD'][$field_name.'_OPTIONS__JAVA']."'
							 ";
							// $SQL="select '101' as `key` , 'test_desc' as description ";
							 //echo $SQL;
					}

					  ZAP_SQL($SQL);
					    		$i=0;
						while ($r = mysql_fetch_object( $result )) {
							$rows[$i]->key=$r->key;
							$rows[$i]->group_key=$r->group_key;
							$rows[$i]->description=addslashes(iconv("UTF-8", "ISO-8859-1//TRANSLIT", $r->description));
							$i++;
						}
						
					if(empty($rows)) {
					$rows[0]->key='';
					$rows[0]->description='';
					$rows[0]->description2='';
					$rows[0]->description3='';
					}

 						print json_encode($rows);


  				} else { //else java 
  
				
					  echo "\n a) wybierz pakiet (filtr) : <input type=text name=".$field_name."__JAVA id=\"".$field_name."__JAVA\" onkeyup=JAVA_GET_SELECT_TABLE_SELECT_FROM_FIELD('".$field_name."','".session_id()."','".$field_name."__JAVA','".$field_name."__JAVA_P');>";

					 echo "<br>b) potwierdz wybor <select id=".$field_name."__JAVA_P name=".$field_name."__JAVA_P ";
							//5.2::@2012-12-20 sqix: przekazanie argumentu z selecta do zmiennej do zlapania w php
							echo  " onchange=\"set_FORM_NAME('".$field_name."__JAVA_P','".$field_name."')\" ";

					  echo ">";
					  echo "</select>";
					  //echo " <br>c) wybierz pakiety : ";
					 echo "\n  <input type=text name=".$field_name." id=\"".$field_name."\" value='".$ARG1_VAL."' >";
					//	echo "<div id=".$field_name."_OPTIONS__JAVA></div>";
					//  echo "<select id=".$field_name."_OPTIONS__JAVA name=".$field_name."_OPTIONS__JAVA ";
						//if($thiss->DETECT_TABLE_COLUMN=='DEALS_TABLE_COLUMN') //jezeli w deals table, to ustawiony platnik sugeruje umowy z tego klienta
							//5.1::@2012-12-20 sqix: mozliwosc przekazania zawartosci do innego pola formularza w JAVA -> //EOF:5.1::
							//echo  "onchange=\"set_FORM_NAME('".$field_name."__JAVA_P',$field_name.'__JAVA_ID_OFFERS_GROUP')\" ";
							//5.2::@2012-12-20 sqix: przekazanie argumentu z selecta do zmiennej do zlapania w php
//							echo  "onchange=\"JAVA_GET_SELECT_TABLE_SELECT_FROM_SELECT('".$field_name."','".session_id()."','".$field_name."_OPTIONS_JAVA')\" ";

						//					echo  "onchange=\"set_FORM_NAME('ID_BILLING_USERS_W_DEALS_TABLE','".$field_name."__JAVA_P\"')\" ";
						 //EOF:5.1::
					 // echo ">";

					  //echo "</select>";
					  					
						if($_SESSION['CURRENT_MENU']==$thiss->DETECT_EDIT_FUNCTION)
					    	echo "\n<br><input type=\"checkbox\" name=".$thiss->DETECT_TABLE_COLUMN."__USERS2_OFFERS_GROUPS_OPCJE__ADD_TO_DEALS> <b>Zaznacz ten przycisk, jezeli chcesz aby po kliknieciu zapisz system wprowadzil informacje do tabeli DEALS, zgodnie z wybranymi opcjami</b>";
						else echo "<br>Należy wejsc w edycje rekordu aby uzyc funkcji przetwarzania rekordu do USERS2_DEALS!!";
		//			echo "\n<br><input id='checkbox' type='checkbox' name='ismemeber_xxx' /> test costam";
				
				//	DEBUG_S(-2,'sql',$SQL,__FILE__, __FUNCTION__, __LINE__);
					/*
					    while($h=mysql_fetch_object($res3)) {
					     echo "\n<br> SKLADNIK OFERTY Numer:".$h->ID.", Opis:".$h->S_ALIAS.", Podrzedna do:".$h->P_SERVICE_PARENT." :";
					    		//petla, gdzie bedziemy zaznaczac opcje kupione przez klienta
					    		$SQL4="select t1.ID, t1.P_SERVICE , t1.P_NORMAL_PRICE ,  t1.S_ALIAS, t1.P_SERVICE_PARENT from USERS2_OFFERS as t1 
					    	      where t1.P_SERVICE='OPCJE' and t1.P_SERVICE_PARENT='".$h->P_SERVICE."'";
					    		$res4=mysql_query($SQL4);
					    		while($h4=mysql_fetch_object($res4)) {
					
					    			echo "\n<br><input type=\"checkbox\" name=".$thiss->DETECT_TABLE_COLUMN."__USERS2_OFFERS_GROUPS_OPCJE__".$h->ID."__".$h4->ID."> SKLADNIK OFERTY Numer:".$h4->ID.", Opis:".$h4->S_ALIAS.", Podrzedna do:".$h4->P_SERVICE_PARENT;
					    		
					    		}
					
					    }
					    */
					    echo "\n";
					} //echo json
					
				} //eof mysql_numrows==0
  
  } else     echo $ARG1_VAL."";
 }



}

}

?>
