<?php

class CompuscanController extends BaseController {

    public function serveCompuscanPrincipal()
    {
        $email = Auth::user()->email;

        $user = DB::table('users')
            ->where('email','=',$email)
            ->first();

        return View::make('individual.compuscan.consumer-credit-report2')
            ->with('user',$user);
    }

    public function handleCompuscanPrincipal()
    {
        $url = "https://webservices-uat.compuscan.co.za/ConsumerCreditReportService?wsdl";

        $parameters= array("pUsername"=>"77802-1",
            "pPassword"=>"statusdev",
            "pMyOrigin"=>"StatusData",
            "pVersion" => "1.0",
            "pResultType" => "XML");


        $client = new SoapClient($url);
        $value = $client->getOnsellerProducts($parameters);
        $story = new SimpleXMLElement($value->TransactionReplyClass->returnData);
        //$productID = (int)$story->onsellerProducts->onsellerProduct->productId;
        $productID = $story->onsellerProducts->onsellerProduct->productId;

        $url = "https://webservices-uat.compuscan.co.za/ConsumerCreditReportService?wsdl";

        $parameters= array("pUsername"=>"77802-1",
            "pPassword"=>"statusdev",
            "pMyOrigin"=>"StatusData",
            "pVersion" => "1.0",
            "pInputType" => "xml",
            "pTransactionData" => base64_encode('
                <transactions>
                    <fullEnquiry> 
                    <Identity_number>7408285107080</Identity_number> 
                    <Surname>Goofy</Surname> 
                    <Forename>Just</Forename> 
                    <Enq_Purpose>12</Enq_Purpose> 
                    <ResultType>PDF</ResultType>
                    </fullEnquiry> 
                </transactions>') );


        $client = new SoapClient($url);
        $value = $client->doCCRFullEnquiry($parameters);

        $value2 = $value->TransactionReplyClass->returnData;

        $loadResult = simplexml_load_string($value2);

        $value3 = $loadResult->result->returnData;
        $decoded = base64_decode($value3);


        //$decoded = base64_decode($base64);
        $file = 'Consumer Credit Report.pdf';
        file_put_contents($file, $decoded);

        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($file).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            readfile($file);
            exit;
        }

        /*
        $params = new StdClass();
        $params->request = new StdClass();
        $params->request->pUsername = "77802-1";
        $params->request->pPassword = "statusdev";
        $params->request->pMyOrigin = "StatusData";
        $params->request->pVersion = "3.0";
        $params->request->pInputType = "XML";
        $params->request->pTransactionData = "<transactions>
                                                <fullEnquiry>
                                                    <Identity_number>7408285107080</Identity_number>
                                                    <Surname>Goofy</Surname>
                                                    <Forename>Just</Forename>
                                                    <Enq_Purpose>12</Enq_Purpose>
                                                    <ResultType>PDF</ResultType>
                                                </fullEnquiry>
                                              </transactions>";

        $options = array('trace' => 1, 'exceptions'  => 1);

        $wsdl = "https://webservices-uat.compuscan.co.za/ConsumerCreditReportService?wsdl";

        //Check if the PHP-SOAP module is installed
        if (!class_exists('SoapClient'))
        {
            die("Missing PHP-Soap module.");
        }
        //Create the webservice connection

        $webservice = new SoapClient($wsdl,$options);
        $response = $webservice->doCCRFullEnquiry($params);

        dd($params);

        $result=$response->TransReplyClass; */

    }

    public function serveCompuscanCommercial()
    {
        $email = Auth::user()->email;

        $user = DB::table('users')
            ->where('email','=',$email)
            ->first();
        return View::make('commercial.compuscan.commercial-report2')
            ->with('user',$user);
    }

    public function handleCompuscanCommercial()
    {
        $params = new StdClass();
        $params->request = new StdClass();
        $params->request->pUsrnme = "77802-1";
        $params->request->pPasswrd = "statusdev";
        $params->request->pOrigin = "StatusData";
        $params->request->pVersion = "1.0";
        $params->request->pOrigin_Version = "2.3";
        $params->request->pResultType = "XML";
        $params->request->pTransaction = "<Transactions>
                                                <Search_Criteria>
                                                    <Company_Name>COMPUSCAN MANUFACTURING</Company_Name>
                                                    <Reg_Number1>2011</Reg_Number1>
                                                    <Reg_Number2>028033</Reg_Number2>
                                                    <Reg_Number3>23</Reg_Number3>
                                                    <Tax_Number></Tax_Number>
                                                    <Vat_Number></Vat_Number>
                                                    <ResultType>XML</ResultType>
                                                </Search_Criteria>
                                            </Transactions>";

        $options = array('trace' => 1, 'exceptions'  => 1);

        $wsdl = "https://webservices-uat.compuscan.co.za/CompanySearchService?wsdl";

        //Check if the PHP-SOAP module is installed
        if (!class_exists('SoapClient'))
        {
            die("Missing PHP-Soap module.");
        }
        //Create the webservice connection

        $webservice = new SoapClient($wsdl,$options);
        $response = $webservice->CompanyPossibleSearch($params);

        $result=$response->TransReplyClass;
        $loadResult2 = simplexml_load_string($result->retData);

        //$companyID =(int)($loadResult2->POS_MATCHES->ROW[0]->COMPANY_ID);
        $companyID =$loadResult2->POS_MATCHES->ROW[0]->COMPANY_ID;
        $EnquiryID = (int)($loadResult2->ENQUIRY_ID[0]);

        $params = new StdClass();
        $params->request = new StdClass();
        $params->request->pUsrnme = "77802-1";
        $params->request->pPasswrd = "statusdev";
        $params->request->pOrigin = "StatusData";
        $params->request->pVersion = "1.0";
        $params->request->pOrigin_Version = "2.3";
        $params->request->pTransaction = "<Transactions>
                                            <Search_Criteria>
                                                <Company_Name>COMPUSCAN MANUFACTURING</Company_Name>
                                                <Reg_Number1>2011</Reg_Number1>
                                                <Reg_Number2>028033</Reg_Number2>
                                                <Reg_Number3>23</Reg_Number3>
                                                <Vat_Number></Vat_Number>
                                                <Tax_Number></Tax_Number>
                                                <Deeds_Data>Y</Deeds_Data>
                                                <ResultType>PDF</ResultType>
                                                <Run_CompanyScore>Y</Run_CompanyScore>
                                                <Run_Codix>Y</Run_Codix>
                                                <PosMatchEnq_Id>$EnquiryID</PosMatchEnq_Id>
                                                <CompanyId>$companyID</CompanyId>
                                            </Search_Criteria>
                                         </Transactions>";


        $options = array('trace' => 1, 'exceptions'  => 1);

        $wsdl = "https://webservices-uat.compuscan.co.za/CompanySearchService?wsdl";

        //Check if the PHP-SOAP module is installed
        if (!class_exists('SoapClient'))
        {
            die("Missing PHP-Soap module.");
        }
        //Create the webservice connection

        $webservice = new SoapClient($wsdl,$options);
        $response = $webservice->CompanyFullSearch($params);
        //dd($response);
        $value = $response->TransReplyClass->retData;

        $decoded = base64_decode($value);


        //$decoded = base64_decode($base64);
        $file = 'Business Credit Report.pdf';
        file_put_contents($file, $decoded);

        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($file).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            readfile($file);
            exit;
        }

       // echo $decoded.'.pdf';

        //var_dump($decoded.'.pdf');
        //dd($response);
    }

}
