<?php

session_start();

if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("function.inc.php");


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

$query_path="select setup_GCIExp from setup";
$resultpath=mysql_query($query_path,$connection) or die(mysql_error());
$row_path=mysql_fetch_array($resultpath);

$ourFileName=$row_path[0];


$ourFileName=$ourFileName."dgcidata1.txt";

$ourFileHandle = fopen($ourFileName, 'a') or die("can't open file");

set_time_limit ( 150 ) ;

$t1=time();

//$query_ledger="select ledger_number from clients";
//$resultdata=mysql_query($query_ledger,$connection) or die(mysql_error());




//while($row=mysql_fetch_array($resultdata)){

//$ledger_number=$row[0];



/// Appending in file 
$select_query1="select ledger_number,Client_GCIno,7 as de,1 as fs,
date_format(client_creationdate,'%Y'),4 as dsfd,3 as dfdf,
date_format(client_creationdate,'%m'),2 as dsfd,3 as dfdf,
date_format(client_creationdate,'%d'),2 as dsfd,3 as dfdf,
ledger_name,70 as de4,0 as xx,
mid(Ledger_name,1,30),30 as de5,0 as we,
mid(Ledger_Desc,1,30),30 as de6,0 as gfgf,
Client_CompOwnerTitle,1 as de7,0 as wew,
Client_CompOwner,30 as de8,0 as wwq,
'C' as c,0 as de99,0 as de999,
mid(Ledger_number,LENGTH(ledger_number)-5+1,5) as M,5 as de9,1  as wwq1,
'1' as dfd,0 as dfd,0 as fdsf,
date_format(client_creationdate,'%Y'),4 as dssfd,3 as dsfdf,
date_format(client_creationdate,'%m'),2 as dssfd,3 as dfsdf,
date_format(client_creationdate,'%d'),2 as dssfd,3 as dfsdf,
null,18 as fdf,1 as fwwe
from clients ";	



$result=mysql_query($select_query1, $connection);

while($row=mysql_fetch_array($result)){

$ledger_number=$row[0];
//echo$select_query1;


$ourFileHandle = fopen($ourFileName, 'a') or die("can't open file");



if ( '' == file_get_contents($ourFileName) )

{
echo"";
}
else fwrite($ourFileHandle,"\r\n");


$empty=export_alldata($row[1],$row[4],$row[7],$row[10],$ourFileHandle);


//1 st ledger 70 
$ledgerlen=strlen($row[13]);
fwrite($ourFileHandle,$row[13]);

$max=70-$ledgerlen;

for($i=1;$i<=$max;$i++){
fwrite($ourFileHandle,' ');
}

if($ledgerlen=='0')
for($i=1;$i<=70;$i++){
fwrite($ourFileHandle,' ');
}


//2nd Ledger 30 

$ledgerlen=strlen($row[16]);
fwrite($ourFileHandle,$row[16]);

$max=30-$ledgerlen;

for($i=1;$i<=$max;$i++){
fwrite($ourFileHandle,' ');
}

if($ledgerlen=='0')
for($i=1;$i<=30;$i++){
fwrite($ourFileHandle,' ');
}



// Ledger Desc

$ledgerlen=strlen($row[19]);


if($ledgerlen=='0'){
for($i=1;$i<=30;$i++){
fwrite($ourFileHandle,' ');
}
}

else{

fwrite($ourFileHandle,$row[19]);

$max=30-$ledgerlen;

for($i=1;$i<=$max;$i++){
fwrite($ourFileHandle,' ');
}

if($ledgerlen=='0')
for($i=1;$i<=30;$i++){
fwrite($ourFileHandle,' ');
}
}


//Client_CompOwnerTitle


if($ledgerlen=='0')
fwrite($ourFileHandle,' ');

else
fwrite($ourFileHandle,$row[22]);
$ledgerlen=strlen($row[22]);

if($ledgerlen=='0')
fwrite($ourFileHandle,' ');


/////Client_CompOwner

$ledgerlen=strlen($row[25]);

if($ledgerlen=='0'){
for($i=1;$i<=30;$i++){
fwrite($ourFileHandle,' ');
}
}
else{
fwrite($ourFileHandle,$row[25]);

$max=30-$ledgerlen;
for($i=1;$i<=$max;$i++){
fwrite($ourFileHandle,' ');
}
}



/// C 
fwrite($ourFileHandle,'C');


// Ledger number 
fwrite($ourFileHandle,$row[31]);

// Ledger number 
fwrite($ourFileHandle,'1');

// date
fwrite($ourFileHandle,$row[37]);
fwrite($ourFileHandle,$row[40]);
fwrite($ourFileHandle,$row[43]);

for($i=1;$i<=18;$i++){
fwrite($ourFileHandle,'0');
}


//$empty1=export_data($connection,'72',$select_query1,$ourFileName,$ourFileHandle,'0');

//if($empty1=='5'){

//$d=1;
//while($d<=179){
//fwrite($ourFileHandle,' ');
//$d=$d+1;
//}
//}

//total = 208 characteres;


//// 2nd set of variables

$select_query2="select 
TelephoneNo_ContactTitle,1 as len1,0 as i1,
Telephone_name,30 as len2,0 as i2,
Client_workTitle,25 as len3,0 as i3


from telephoneno
where client_type='C' and ledger_number='$ledger_number'";

//echo$select_query2;

$empty2=export_data($connection,'18',$select_query2,$ourFileName,$ourFileHandle,'0');

if($empty2=='5'){

$d=1;
while($d<=56){
fwrite($ourFileHandle,' ');
$d=$d+1;
}

while($d<=89){
fwrite($ourFileHandle,'0');
$d=$d+1;
}
}
//total 89

$select_query3="select 
Client_Telephone1,11 as dfd,1 as dsd,
Client_Telephone2,11 as dfdf,1 as dfdf,
null,11 as dffd,1 as dfdsf,
mid(client_pobox,1,9),9 as dfd,3 as dfd from clients where ledger_number='$ledger_number'";
$empty3=export_data($connection,'12',$select_query3,$ourFileName,$ourFileHandle,'0');

if($empty3=='5'){

$d=1;
while($d<=24){
fwrite($ourFileHandle,'0');
$d=$d+1;
}
}



///4 th set of variables 
$select_query4="select 
TelephoneNo_ContactTitle,'1' as len1,0 as t1,
Telephone_name,'30' as len2,0 as t2,
Client_workTitle,'25' as len3,0 as t3
from telephoneno
where client_type='A' and ledger_number='$ledger_number'";

//echo$select_query4;

$empty4=export_data($connection,'10',$select_query4,$ourFileName,$ourFileHandle,'3');

if($empty4=='5'){

$d=1;
while($d<=56){
fwrite($ourFileHandle,' ');
$d=$d+1;
}
}


//total 56
// Business sector value 
$business_query="select Client_BusinessSector from clients where ledger_number='$ledger_number'";

//echo$business_query;
$resultbusiness=mysql_query($business_query,$connection) or die(mysql_error());
$rowbusiness=mysql_fetch_array($resultbusiness);
$business_sector=$rowbusiness[0];

if($business_sector=='')
$business_sector='1';
//echo$business_sector;




$select_query5="select 
Client_Address,100 as fsdf,0 as sfsd,

Client_Address2,50 as fsdsf,0 as fgdf,

mid(Area_Code,length(Area_Code)-4+1,4),4 as fdsdsw,1 as fdss,
Client_ReferedByTitle,1 as fds,0 as sfsd,
Client_ReferedBy,30 as dfds,0 as dfds,
Client_SDINo,7 as ff,3 as dfsdf  ,
Client_DeliveryPhone,11 as fsd,1 asfs,
mid(Client_DeliveryAddress,1,50),50 as dfs ,0 as fsfsf, 
mid(Client_DeliveryAddress,51,50),50 as dfs ,0 as fsfsf, 
Client_DeliveryAddress2,50 as fsdfsww,0 as iiti,

mid(Client_DlvAreaCode,length(Client_DlvAreaCode)-4+1,4),4 as rtrd,1 as bxb,
Client_DlvAssistantTitle,1 as fssdvvf,0 as dsfsdf,
Client_DeliveryAssistant,30 as xxcvx,0 as dscsd,
Client_DlvWorkTitle,25 as fdsfsdc,0 as cbvbcb,
Client_DlvFax,11 as ytry,1 as uuy,
if(client_export=1,'E',''),1 as dfsd,0 as gfdgd,
if(client_local=1,'L',''),1 as dfsd,0 as gfdgd,
";





if($business_sector=='A')
$select_query5=$select_query5."'A' as a,0 as de99,0 as de999,
	 'A' as a1,0 as de919,0 as de1999,
	Client_BusinessSectorDesc,10 as ffwfw,0 as fwfwef,
	null,1 as fgdf,0 as rg,
	null,10 as fg,0 as rgdf,
	null,1 as fgsd,0 as fggfe,
	null,10 as ffwsfs,0 as fsfsf";
	
	
if($business_sector=='I')
$select_query5=$select_query5."'I' as iasd,0 as deai99,0 as de9ii99,
		null,1 as fgdf,0 as rg,
		null,10 as idsfsd,0 as fsdff,
		'I' as iaddassd,0 as deaiasd99,0 as de9iias99,
		Client_BusinessSectorDesc,10 as fsdfsd,0 as gfdsgf,
		null,1 as ffsdfgdf,0 as rfg,
		null,10 as ifsddsfsd,0 as fssdfdff";
		
if($business_sector=='O')
$select_query5=$select_query5."null,1 as fgdf,0 as rg,
		null,1 as idsfsd,0 as fsdff,
		null,10 as ifsddsfsd,0 as fssdfdff,
		null,1 as ffsdfgdf,0 as rfg,
		null,10 as ifds,0 as fssdfsdfdff,
		'o' as iasd,0 as deai99,0 as de9ii99,
	  Client_BusinessSectorDesc,10 as fsdfsd,0 as gfdsgf";
	  
if($business_sector=='1')
$select_query5=$select_query5."
		null,1 as idsfsd,0 as fsdff,
		null,10 as ifsddsfsd,0 as fssdfdff,
		null,1 as ffsdfgdf,0 as rfg,
		null,10 as ifds,0 as fssdfsdfdff,
		null,1 as ffasdsdfgdf,0 as rasdfg,
	  null,10 as fsdfsd,0 as gfdsgf";	


 

$select_query5=$select_query5." from clients where ledger_number='$ledger_number'";


//echo$select_query5;

$empty5=export_data($connection,'83',$select_query5,$ourFileName,$ourFileHandle,'0');
 


if($empty5=='5'){

$d=1;
while($d<=450){
fwrite($ourFileHandle,' ');
$d=$d+1;
}
}


$select_query6="select 

Client_BusinessType,50 as b11,0 as dsde,
Client_BType2,50 as bfd,0 as hgh,
Client_BType3,50 as begf,0 as dfd,
Client_TOBcode,4 as sdf,1 as bfd,
Client_EvaluationPoints,50 as bedd,0 as dfsd,
client_Weakness,50 as fdg,0 as gdfsg,
null,50 as dfsfsd,0 as sdf,																				
mid(Client_Tips,1,50),50 as fsd,0 as fsdf,		
mid(Client_Tips,51,50),50 as fsd,0 as fsdf,	
null,50 as bdf,0 as fsd,																		
null,4 as sd,0 as fsdf,                  													
mid(Salesmen_Code,length(Salesmen_Code)-4+1,4),4 as fsdf,0 as fsd,
null,3 as ds ,0 as sdf,																			
null,4 as sd,0 as dfsd, 																			
mid(Salesmen_Code,length(Salesmen_Code)-4+1,4),4 as fsdf,0 as fsd,

null,3 as fsdf,0 as sfd,																	
Client_CreditLimit,8 as dsfsd,1 as fdsf,
mid(Currency_Code,1,2),2 as fsdf,0 as fdsf,

Client_Rating,3 as fds,0 as fsd,
Client_PayTermType,1 as df,0 as dsfsd,
mid(Client_PaymentTerms,1,3),3 as dfds,1 as fsdf,
Client_FrequencyOfVisit,3 as dds,1 as fsf,
null,18 as fsdf,1 as fgdff,
'A',1 as fsdf,0 as fgdff,
null,2 as fsdf,1 as fgdff,
null,1 as fsdf,0 as fgdff,
Client_INVDLV,1 as ddsf,0 as dfsdf,
mid(Client_InvLanguage,1,1),1 as sdf ,0 as sdfsd,

if(client_invrounded=1,'R',''),1 as dedasd919,0 as drre1999,
null,13 as sfsd,1 as fsf,
null,13 as df,1 as dsfdsf,
null,10 as dfds,1 as sdfsdf,
null,10 as dfds,1 as fsdf,

Client_MotherCompany,5 as ds ,2 as fds,

null,30 as fsd,0 as ssdf,
User_Account,7 as fdsf,0 as fdsfds,
null,1 as fsd,0 as sfdd,
date_format(client_sysdate,'%Y'),4 as df,0 as dsfds,
date_format(client_sysdate,'%m'),2 as dffs,0 as dffds,
date_format(client_sysdate,'%d'),2 as sdfds,0 as fsd,

date_format(Client_SysTime,'%h%i%s%'),8 as fsf,0 as fsdf,
null,8 as fdsf,1 as fdsf,
null,4 as dfds,0 as ffdsfs 
  
from clients where ledger_number='$ledger_number'";




$empty6=export_data($connection,'130',$select_query6,$ourFileName,$ourFileHandle,'0');

if($empty6=='5'){

$d=1;
while($d<=638){
fwrite($ourFileHandle,' ');
$d=$d+1;
}
}
 



/// 7th set of variables 
$select_query7="select 
Competitor_Name,30 as fsdf,0 as dfsd,
Competitor_Ratio,5 as dfsd,2 as dsf,
Competitor_Salesman,30 as ds,0 as fsdf

from competitors where ledger_number='$ledger_number'";

//echo$select_query7;
$empty7=export_data($connection,'10',$select_query7,$ourFileName,$ourFileHandle,'3');

if($empty7=='5'){


for($p=1;$p<=3;$p++){
$d=1;
while($d<=30){
fwrite($ourFileHandle,' ');
$d=$d+1;
}

$d=1;
while($d<=5){
fwrite($ourFileHandle,'0');
$d=$d+1;
}

$d=1;
while($d<=30){
fwrite($ourFileHandle,' ');
$d=$d+1;
}
}
}


$select_query8="select 
12110,5 as fdfs,1 as sdfsd,
if(Client_MultipleSales='1','M',''),1 as fsf,0 as frsd
from clients where ledger_number='$ledger_number'";


$empty8=export_data($connection,'7',$select_query8,$ourFileName,$ourFileHandle,'0');

if($empty8=='5'){

$d=1;
while($d<=1){
fwrite($ourFileHandle,' ');
$d=$d+1;
}
}
}


$t2=time();
$t_lapsed=$t2-$t1;
echo "Total time lapsed = $t_lapsed";


mysql_close($connection);



print <<<HERE
</form>
</body>


HERE;


header ("Location: ./Form_Clients.php?ledger_number=$ledger_number&keepcompetitor=$keepcompetitor&keepactivation=$keepactivation&keepcontact=$keepcontact&mode=view");

