<?php
//echo phpinfo();
//die();
include("api/soap.php");
include("lib/util.php");

$proxy = new SoapProxy();
$p = new stdClass();
$p->vendorID = "2";
$strXml = $proxy->webService->iSkhiya($p)->iSkhiyaResult;
$resarray = (json_decode(json_encode($strXml),true));
$key =  $resarray;

//$data = "action=DEBTORINFOR&bank_name=Standard+Bank&branch_code=78798&account_number=879879879&current_assets=3321&fixed_assets=7687&current_liabilities=7687&long_term_liabilities=686&previous_year_turnover=876&est_current_year_turnover=876&current_turnover_per_month=876&current_turnover_per_annum=876&number_of_employees=876&account_type=Transmission&company_id=&debtor_id=133&trade_ref1=ME&trade_ref_email1=sada%40fdsk.com&trade_ref2=dsdj&trade_ref_email2=sdjas%40jk.com&trade_ref3=xsj&trade_ref_email1=cdskj%40fsjdk.com&supplier_ref1=fdfe&supplier_ref_email1=ee%40sd.cm&supplier_ref2=er&supplier_ref_email2=ee%40sd.cm&supplier_ref3=trete&supplier_ref_email1=ee%40sd.cm";
//$data = encryptIt( $data,$key,true);
//echo "$data<br/>";
//die();
$serverName = "localhost\sqlexpress";

$connectionOptions = array("Database"=>"efficientdata","Uid"=>"sa","Pwd"=>"12_s5ydw3ll");

	/* Connect using Windows Authentication. */
global	$conn;	
global $folder;

$conn = sqlsrv_connect( $serverName, $connectionOptions);
if( $conn === false ){ 
	die( FormatErrors( sqlsrv_errors() ) ); 
}

//echo "Here";


extract($_GET);
extract($_POST);


$data = decryptIt( $data,$key);
//echo "$data<br/>";
$thedata = explode("&",$data);
foreach($thedata as $val)
{
	$pieces = explode("=",$val);
	$_GET[$pieces[0]] = $pieces[1];
}

var_dump($_GET);

extract($_GET);
extract($_POST);

if(!isset($action))
{
	die();
}

if(strtoupper($action) == "DEBIT")
{
	 //debit is where we deduct money from the client's account
	$sql = "";
}

if(strtoupper($action) == "DEBIT")
{
	
}

if(strtoupper($action) == "DEBTORINFOR")
{
	$filename = "C:\EmailFiles\faq.html";
	$handle = fopen($filename,"r");
	$content = fread($handle,filesize($filename));
	fclose($handle);
	
	$sql = "select compid , c.name
		from ed_debtors d
		join ed_company c on c.id = compid
		where id = '$debtor_id'";
	$stmt = sqlsrv_query( $conn, $sql);
	$comp_id = 0;
	$company_name = "";
	while($row = sqlsrv_fetch_object( $stmt))
	{
		$comp_id = $row->compid;
		$company_name = $row->name;
	}
	
	$sql1 = "INSERT INTO ed_financialinfo2  (currentassets,fixedassets,currentliabilities,longtermliabilities
           ,previousyearturnover,estimatedcurrentyearturnover,currentturnoverpermonth,currentturnoverperannum,compid)
     VALUES
           ('$current_assets','$fixed_assets','$current_liabilities','$long_term_liabilities','$previous_year_turnover'
           ,'$est_current_year_turnover','$current_turnover_per_month','$current_turnover_per_annum','$comp_id')";
	 sqlsrv_query( $conn, $sql1);
	 
	 $sql1 = "INSERT INTO  ed_banker (banker,branchcode,accountnumber,comp_id,accounttype)
     VALUES ('$bank_name','$branch_code','$account_number','$comp_id','$account_type')";
	 sqlsrv_query( $conn, $sql1);
	 
	$sql2 = "INSERT INTO ed_suppliers (suppliername,contactperson,phonenumber,comp_id,active,supplieremail
           ,requestdate,filledtraderef,senttosupplier,isNotTradeRef)
     VALUES ('$trade_ref1' ,'$trade_ref_contact_person1','$trade_ref_telephone1','$comp_id'
           ,'1','$trade_ref_email1',getdate(),'0','0','0')";
	sqlsrv_query( $conn, $sql2);
	
	$email = str_replace(":Name","$trade_ref_contact_person1",$content);
	$email = str_replace(":Subject","Trade Reference Request",$email);
	$email = str_replace(":Message","Dear $trade_ref_contact_person1<br/><br/>Please complete a Trade/Credit Reference on behalf of one of your customers, $company_name",$email);
	$subject = "Request For Trade Reference from $company_name";
	$email_sql = "INSERT INTO ed_mail (mailto,mailsubject,mailmessage,sent,mailfiles,datecreated)
     VALUES ('$trade_ref_email1','$subject','$$email','0','',getdate())";
	sqlsrv_query( $conn, $email_sql);
	
	$sql2 = "INSERT INTO ed_suppliers (suppliername,contactperson,phonenumber,comp_id,active,supplieremail
           ,requestdate,filledtraderef,senttosupplier,isNotTradeRef)
     VALUES ('$trade_ref2' ,'$trade_ref_contact_person2','$trade_ref_telephone2','$comp_id'
           ,'1','$trade_ref_email2',getdate(),'0','0','0')";
	sqlsrv_query( $conn, $sql2);
	
	$email = str_replace(":Name","$trade_ref_contact_person2",$content);
	$email = str_replace(":Subject","Trade Reference Request",$email);
	$email = str_replace(":Message","Dear $trade_ref_contact_person2<br/><br/>Please complete a Trade/Credit Reference on behalf of one of your customers, $company_name",$email);
	$subject = "Request For Trade Reference from $company_name";
	$email_sql = "INSERT INTO ed_mail (mailto,mailsubject,mailmessage,sent,mailfiles,datecreated)
     VALUES ('$trade_ref_email2','$subject','$$email','0','',getdate())";
	sqlsrv_query( $conn, $email_sql);
	
	$sql2 = "INSERT INTO ed_suppliers (suppliername,contactperson,phonenumber,comp_id,active,supplieremail
           ,requestdate,filledtraderef,senttosupplier,isNotTradeRef)
     VALUES ('$trade_ref3' ,'$trade_ref_contact_person3','$trade_ref_telephone3','$comp_id'
           ,'1','$trade_ref_email3',getdate(),'0','0','0')";
	sqlsrv_query( $conn, $sql2);
	
	$email = str_replace(":Name","$trade_ref_contact_person3",$content);
	$email = str_replace(":Subject","Trade Reference Request",$email);
	$email = str_replace(":Message","Dear $trade_ref_contact_person3<br/><br/>Please complete a Trade/Credit Reference on behalf of one of your customers, $company_name",$email);
	$subject = "Request For Trade Reference from $company_name";
	$email_sql = "INSERT INTO ed_mail (mailto,mailsubject,mailmessage,sent,mailfiles,datecreated)
     VALUES ('$trade_ref_email3','$subject','$$email','0','',getdate())";
	sqlsrv_query( $conn, $email_sql);	
	
	$sql2 = "INSERT INTO ed_suppliers (suppliername,comp_id,active,supplieremail,requestdate,filledtraderef
           ,senttosupplier,isNotTradeRef)
     VALUES  ('$supplier_ref1','$comp_id','1','$supplier_ref_email1',getdate(),'0','0','1')";	
	sqlsrv_query( $conn, $sql2);
	
	$email = str_replace(":Name","$supplier_ref1",$content);
	$email = str_replace(":Subject","Trade Reference Request",$email);
	$email = str_replace(":Message","$supplier_ref1<br/><br/>Please complete a Supplier Reference on behalf of one of your customers, $company_name",$email);
	$subject = "Request For Trade Reference from $company_name";
	$email_sql = "INSERT INTO ed_mail (mailto,mailsubject,mailmessage,sent,mailfiles,datecreated)
     VALUES ('$supplier_ref_email1','$subject','$$email','0','',getdate())";
	sqlsrv_query( $conn, $email_sql);	
	
	
	$sql2 = "INSERT INTO ed_suppliers (suppliername,comp_id,active,supplieremail,requestdate,filledtraderef
           ,senttosupplier,isNotTradeRef)
     VALUES  ('$supplier_ref2','$comp_id','1','$supplier_ref_email2',getdate(),'0','0','1')";	
	sqlsrv_query( $conn, $sql2);
	
	$email = str_replace(":Name","$supplier_ref2",$content);
	$email = str_replace(":Subject","Trade Reference Request",$email);
	$email = str_replace(":Message","$supplier_ref2<br/><br/>Please complete a Supplier Reference on behalf of one of your customers, $company_name",$email);
	$subject = "Request For Trade Reference from $company_name";
	$email_sql = "INSERT INTO ed_mail (mailto,mailsubject,mailmessage,sent,mailfiles,datecreated)
     VALUES ('$supplier_ref_email2','$subject','$$email','0','',getdate())";
	sqlsrv_query( $conn, $email_sql);	
	
	$sql2 = "INSERT INTO ed_suppliers (suppliername,comp_id,active,supplieremail,requestdate,filledtraderef
           ,senttosupplier,isNotTradeRef)
     VALUES  ('$supplier_ref3','$comp_id','1','$supplier_ref_email3',getdate(),'0','0','1')";	
	sqlsrv_query( $conn, $sql2);
	
	$email = str_replace(":Name","$supplier_ref3",$content);
	$email = str_replace(":Subject","Trade Reference Request",$email);
	$email = str_replace(":Message","$supplier_ref3<br/><br/>Please complete a Supplier Reference on behalf of one of your customers, $company_name",$email);
	$subject = "Request For Trade Reference from $company_name";
	$email_sql = "INSERT INTO ed_mail (mailto,mailsubject,mailmessage,sent,mailfiles,datecreated)
     VALUES ('$supplier_ref_email3','$subject','$$email','0','',getdate())";
	sqlsrv_query( $conn, $email_sql);	
	 
}	

echo "HERE";

?>