<?php 
if(file_exists('../../../'.$_REQUEST['tenant'].'/config.php')){require_once('../../../'.$_REQUEST['tenant'].'/config.php'); }
if(file_exists('/config.php')){require_once('/config.php'); }

$_REQUEST['content']=str_replace('<th class="remove" style="display: none;"></th>','',$_REQUEST['content']);
$body='';
$header='';
$footer='';
    if($_REQUEST['type']=="statement"){
    $insert="_".$_REQUEST['version']."_".date('d-m-Y');
    }

    function getAgentDetails($agent){
    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `agents` WHERE `agent_number`='".$agent."'")or die(mysqli_error($GLOBALS['con']));
    $stmt->execute();$s=$stmt->get_result();$stmt->close();
    $xd=mysqli_fetch_array($s);
    return $xd['agent_name'].'<br>'.$xd['address1'].'<br>'.$xd['address2'].'<br>'.$xd['town'].'<br>'.$xd['post'].'<br>';
    }

    function getBrokerDetails($broker){
    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `brokers` WHERE `broker_number`='".$broker."'")or die(mysqli_error($GLOBALS['con']));
    $stmt->execute();$s=$stmt->get_result();$stmt->close();
    $xd=mysqli_fetch_array($s);
    return $xd['broker_name'].'<br>'.$xd['address1'].'<br>'.$xd['address2'].'<br>'.$xd['town'].'<br>'.$xd['post'].'<br>';
    }

    function getClientDetails($client){
    $stmt = $GLOBALS['con']->prepare("SELECT * FROM `clientdetails` WHERE `clientnumber`='".$client."'")or die(mysqli_error($GLOBALS['con']));
    $stmt->execute();$s=$stmt->get_result();$stmt->close();
    $xd=mysqli_fetch_array($s);
    return $xd['clientname'].'<br>'.$xd['address1'].'<br>'.$xd['address2'].'<br>'.$xd['town'].'<br>'.$xd['postcode'].'<br>';
    }

    $header='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head> 
    <body bgcolor="#FFFFFF" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" style="font-size:10px!important">';
    $header.=file_get_contents('../../../'.TENANT_URL.'/engines/report_structure/header_view.php');
    ////////////////////////////////////
    $body.='<link href="packages/bootstrap/bootstrap.gen.css" rel="stylesheet">';
    $body.=file_get_contents('../../../'.TENANT_URL.'/engines/report_structure/style.php');
    $footer='';

    if(!empty($_REQUEST['addressBox'])){
    $body.='<table width="100%" style="font-family: verdana; font-size: 8pt; color: #2a3f54; font-weight: bold;">
    <tr><td align="right"><h3>STATEMENT OF ACCOUNT</h3></td></tr></table>';

        switch ($_REQUEST['addressBox']){
        case "client":
        $body.='<table width="100%" style="font-family: verdana; font-size: 8pt; color: #000000; border-bottom:1px solid #2a3f54">
        <tr>
        <td>'.getClientDetails($_REQUEST['clientnumber']).'</td>
        </tr>
        </table>'
       ;
        break;

        case "agent":
        $body.='<table width="100%" style="font-family: serif; font-size: 8pt; color: #000000; font-weight: bold; font-style: italic; border-bottom:1px solid #2a3f54">
        <tr>
        <td style="width:50%">'.getCompanyDetails('address').'</td>
        <td style="width:50%; text-align:right">'.getAgentDetails($_REQUEST['number']).'</td>
        </tr>
        </table>';
        break;
        case "broker":
            $body.='<table width="100%" style="font-family: serif; font-size: 8pt; color: #000000; font-weight: bold; font-style: italic; border-bottom:1px solid #2a3f54">
            <tr>
            <td style="width:50%">'.getCompanyDetails('address').'</td>
            <td style="width:50%; text-align:right">'.getBrokerDetails($_REQUEST['number']).'</td>
            </tr>
            </table>';
            break;

        }
        $footer.='
        <table width="100%" style="font-family: verdana; font-size: 8pt; color: #000000;">
        <tr style="background-color:#2a3f54">
        <td align="right" style="color:#fff; font-weight: bold;">Account current Balance: £'.$_REQUEST['total'].'</td>
        </tr>
        <tr><td align="center">-- Thank you for your business --</td></tr>
        <tr><td><p>Please make all cheques Payable to <span style="color:#2a3f54">[CNAME]</span></p>
        <p><span style="color:#2a3f54">Bank Name</span> [CBANK]. <span style="color:#2a3f54">Account Number</span> [CACNO]. <span style="color:#2a3f54">Sort Code</span> [CSCODE]</p>
        <p><small>When paying by BACS, please ensure that your company name and this invoice number is used as payment reference.</small></p></td></tr>
        </table>';

    }
    else{
        $footer.=file_get_contents('../../../'.TENANT_URL.'/engines/report_structure/footer_view.php');
    
    }
    $footer=str_replace('[WEB]',getCompanyDetails('web'),$footer);
    $footer=str_replace('[CNAME]',getCompanyDetails('name'),$footer);    
    $footer=str_replace('[CBANK]',getCompanyDetails('bankName'),$footer);
    $footer=str_replace('[CACNO]',getCompanyDetails('accountNo'),$footer);
    $footer=str_replace('[CSCODE]',getCompanyDetails('sortCode'),$footer);
    $footer=str_replace('[WEB]',getCompanyDetails('web'),$footer);

    $body.=$_REQUEST['content'];

    $body=str_replace('[REPORT_PATH]',getCompanyDetails('web'),$body);
        //// CONTENT REPLACEMENT
        if(file_exists('../../../'.TENANT_URL.'/images/company/logo.jpg')){$header=str_replace('[LGO]','<img src="../../../'.TENANT_URL.'/images/company/logo.jpg" height="50px" />',$header);}

        if(file_exists('../../../'.TENANT_URL.'/images/company/afl_1.jpg')){$header=str_replace('[AF1]','<td><img src="../../../'.TENANT_URL.'/images/company/afl_1.jpg" width="30px" /></td>',$header);}
        if(file_exists('../../../'.TENANT_URL.'/images/company/afl_2.jpg')){$header=str_replace('[AF2]','<td><img src="../../../'.TENANT_URL.'/images/company/afl_2.jpg" width="30px" /></td>',$header);}
        if(file_exists('../../../'.TENANT_URL.'/images/company/afl_3.jpg')){$header=str_replace('[AF3]','<td><img src="../../../'.TENANT_URL.'/images/company/afl_3.jpg" width="30px" /></td>',$header);}
        if(file_exists('../../../'.TENANT_URL.'/images/company/afl_4.jpg')){$header=str_replace('[AF4]','<td><img src="../../../'.TENANT_URL.'/images/company/afl_4.jpg" width="30px" /></td>',$header);}
        if(file_exists('../../../'.TENANT_URL.'/images/company/afl_5.jpg')){$header=str_replace('[AF5]','<td><img src="../../../'.TENANT_URL.'/images/company/afl_5.jpg" width="30px" /></td>',$header);}
        if(file_exists('../../../'.TENANT_URL.'/images/company/afl_6.jpg')){$header=str_replace('[AF6]','<td><img src="../../../'.TENANT_URL.'/images/company/afl_6.jpg" width="30px" /></td>',$header);}
        
        if($_REQUEST['type']!="statement"){
            $filestore="../../output/reps/".$_REQUEST['clientnumber']."_".$_REQUEST['type'].$insert.".pdf";
        }
        if($_REQUEST['type']=="statement"){
            $_REQUEST['name']=str_replace(" ","_",$_REQUEST['name']);
            $filestore='../../..'.TENANT_URL.'/output/reps/'.$_REQUEST['name'].'-'.$_REQUEST['type'].$insert.'.pdf';
            $return=$_REQUEST['name']."-".$_REQUEST['type'].$insert.".pdf";
        }



$bodys = '
<table>
  <tr><td align="right"><h3>STATEMENT OF ACCOUNT</h3></td></tr>
</table>

<table border="0" style="margin-top: 5px; border-bottom: 1px solid #2a3f54;">
  <tr>
    <td>
      Acorns Lighting Services Ltd<br>
      Unit 11 Selby Business Park<br>
      Oakney Wood Road<br>
      Selby<br>
      YO8 8LZ<br>
    </td>
  </tr>
</table>

<table border="1" cellpadding="4" cellspacing="0" style="margin-top: 10px;">
  <thead>
    <tr>
      <th width="100">Invoice</th>
      <th>Client</th>
      <th>Status</th>
      <th>Date Issued</th>
      <th>Net Fee</th>
      <th>VAT</th>
      <th>Commission</th>
      <th>Total</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>000020</td>
      <td>Acorns Lighting Services Ltd</td>
      <td><span style="background-color:#ffc107; padding:2px 6px;">Unpaid</span></td>
      <td>15-06-2025</td>
      <td>£125.00</td>
      <td>£0.00</td>
      <td>£0.00</td>
      <td>£125.00</td>
    </tr>
    <tr>
      <td>000021</td>
      <td>Acorns Lighting Services Ltd</td>
      <td><span style="background-color:#ffc107; padding:2px 6px;">Unpaid</span></td>
      <td>18-06-2025</td>
      <td>£125.00</td>
      <td>£25.00</td>
      <td>£0.00</td>
      <td>£150.00</td>
    </tr>
  </tbody>
</table>

<table style="margin-top: 15px;">
  <tr>
    <td align="right" style="font-weight: bold;">Total Outstanding: £275.00</td>
  </tr>
</table>
';




        $orientation=$_REQUEST['format'];
        require_once '../../packages/mpd/vendor/autoload.php';


        $mpdf=new \Mpdf\Mpdf([
            'setAutoTopMargin' => 'stretch',
            'setAutoBottomMargin' => 'stretch',
            'mode' => 'utf-8',
            'format' => 'A4',
            'orientation'=>'L'
        ]);

        $header = mb_convert_encoding($header, 'UTF-8', 'UTF-8');
        $footer = mb_convert_encoding($footer, 'UTF-8', 'UTF-8');
        $body = mb_convert_encoding($body, 'UTF-8', 'UTF-8');
        $mpdf->shrink_tables_to_fit = 1;
        $mpdf->SetHTMLHeader($header,0,true); 
        $mpdf->SetHTMLFooter($footer,0,true); 
        $mpdf->WriteHTML($body);
        $mpdf->Output($filestore);


    if($_REQUEST['type']!="statement"){
        $str=explode("_",$_REQUEST['type']);
        $type=$str[0];
        $range=$str[1]."|".$str[2];
        $_REQUEST['content']=urlencode($_REQUEST['content']);
        $stmt = $GLOBALS['con']->prepare("INSERT INTO `finance_reports` VALUES ('NULL','".$_REQUEST['clientnumber']."','','".$type."','".$_REQUEST['content']."','".$_SESSION['user']."','".strtotime('now')."','".$range."','','1')")or die(mysqli_error($GLOBALS['con']));
        $stmt->execute();$stmt->close();
        echo  $body."_".$_REQUEST['clientnumber']."_".$_REQUEST['type']."_".$_REQUEST['format'];
    }
    else{echo $return;}
   
?>