<?php       include_once('../config/db.php');

require '../../assets/vendor/autoload.php';
use Dompdf\Dompdf; 

// Data 

    $docs = $conn->prepare(" SELECT a.*  FROM adcompany a  ");
    $docs->execute(); 
    $doc = $docs->fetch(); 

    
    
    $path = '../admin/image/'.$doc["logo"].'';
    $type = pathinfo($path, PATHINFO_EXTENSION);
    $data = file_get_contents($path);
    $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

    $did =    htmlspecialchars ( strtoupper($_POST['printinvocie']));
  
    $invoices = $conn->prepare(" SELECT a.*  FROM acctransactions a where  a.id='$did'  ");
    $invoices->execute();  $invoice = $invoices->fetch(); 

    $partners = $conn->prepare(" SELECT a.partner,b.name,b.phone,b.email  FROM acctransactionsdetails a
                                LEFT JOIN bppartner b on a.partner=b.id where  a.transid='$did' and a.transmove=5  ");
    $partners->execute();  $partner = $partners->fetch(); 

    $invdetails = $conn->prepare(" SELECT a.*,b.name bname,b.code FROM acctransactionsdetails a
                                   LEFT JOIN item b on a.item=b.id where  a.transid='$did' and a.transmove=1  ");
    $invdetails->execute(); 
    $invdetail = $invdetails->fetchAll(); 

   // $subtotal = $conn->query(" SELECT  itemtotal from acctransactionsdetails a  where a.transid =  '".$details["id"]."' and a.transmove=3  ")->fetch();
    $subtotal = $conn->query(" SELECT  itemtotal from acctransactionsdetails a  where a.transid =  '$did' and a.transmove=3  ")->fetch();
    $tax = $conn->query(" SELECT  itemtotal from acctransactionsdetails a  where a.transid ='$did'  and a.transmove=4  ")->fetch();
    $total = $conn->query(" SELECT  itemtotal from acctransactionsdetails a  where a.transid ='$did'  and a.transmove=5  ")->fetch();
         
//



// Instantiate and use the dompdf class 
$dompdf = new Dompdf();

//include_once 'document.css';

$html='
        <style>
           h4   {
                    margin: 0;
                }
                .w-full {
                    width: 100%;
                }
                .w-logo {
                      width: 100%;
                      margin-left: auto;
                      margin-right: auto;
                      text-align: center;
                        }

                .w-companyname {
                      width: 100%;
                      text-align: center;
                      color: black;                      
                      padding:0px;
                      margin-bottom:-30px;
                        }
                .w-companyname {
                                 font-size:18px;
                                }
                .w-docname {
                      padding-top:0px;
                      width: 100%;
                      text-align: center;
                      color: #ffffff;                      
                      background-color: rgb(96 165 250);
                        }
                 .w-docname h2{
                        padding-top:7.5px;
                        padding-bottom:7.5px;
                        font-size:18px;
                               }
                               
                  .w-description {
                            padding-top:0px;
                            padding-bottom:0px;
                              }
                 .w-description h5 {
                            padding-top:0px;
                            padding-bottom:0px;
                              }
                 .w-header{
                             width: 100%;
                             padding:5px;
                          }
                    .w-half  {
                        width: 50%;
                    }
                    .w-half1 {
                        width: 35%;
                        font-size :0.875rem;
                    }
                    .w-half2 {
                        width: 35%;
                        font-size :0.875rem;
                    }
                    .w-half3 {
                        width: 30%;
                        font-size :0.875rem;
                    }

             
                .margin-top {
                    margin-top: 1.25rem;
                }
                .footer {
                    font-size: 0.875rem;
                    padding: 1rem;
                    background-color: rgb(241 245 249);
                }
                table {
                    width: 100%;
                    border-spacing: 0;
                }
                table.products {
                    font-size: 0.875rem;
                }
                table.products tr {
                    background-color: rgb(96 165 250);
                }
                table.products th {
                    color: #ffffff;
                    padding: 0.5rem;
                     text-align: left;
                    
                }
                table tr.items {
                    background-color: white;
                    
                    
                }
                table tr.items td {
                    padding: 0.4rem;
                    border:solid;
                    border-width:thin;
                }
                table .total {
                    text-align: right;
                    padding:10px;
                    margin-top: 0.1rem;
                    font-size: 15px;
                    background-color:white;
                }

                .total .tot{
                              border:solid;
                              border-width:thin;
                              border-color:black;
                         }
        </style>

            <div class="w-logo ">
                        <img src="'.$base64.'" height="70" width="180" />
            </div>
            <div class="w-companyname">
                        <h3> '.$doc["name"].' </h3>
            </div>
            <div class="w-docname">
                        <h2> INVOICE </h2>
            </div>
        
            <div class="margin-top">
                <table class="w-header">
                    <tr>
                        <td class="w-half1">
                            <div><h4>TO:</h4></div>
                            <div class="data" > '.$partner["name"].' </div>
                            <div class="data" > '.$partner["phone"].' </div>
                            <div class="data" > '.$partner["email"].' </div>
                        </td>
                        <td class="w-half2">
                            <div><h4>FROM:</h4></div>
                            <div class="data" > '.$doc["name"].' </div>
                            <div class="data" > '.$doc["phone"].' </div>
                            <div class="data" > '.$doc["email"].' </div>
                        </td>
                        <td class="w-half3">
                            <div><h4>Details:</h4></div>
                            <div class="data"> Invoice NO   :'.$invoice["invnumber"].' </div>
                            <div class="data"> Date Added     :     '.$invoice["date"].'</div>
                            <div class="data"> Due  Date      :     '.$invoice["enddate"].' </div>
                        </td>
                    </tr>
                </table>
            </div>
        
            <div class=" w-description ">
                 <h5> DESCRIPTION: '.$invoice["notes"].' </h5>
            </div>
            
            <div class="margin-top">
                <table class="products" style="table-layout:fixed;" >
                    <th>
                        <tr class="header" >
                            <th width="38%">Items </th>
                            <th width="15%">Quantity </th>
                            <th width="17%" >Price</th>
                            <th width="10%" >Tax</th>
                            <th width="20%" >Total</th>
                        </tr>
                    </th>
                    <tbody> ';
                      
                        foreach($invdetail as $invd){                  
                            $html.='<tr class="items">
                                      <td>  '.$invd["bname"].' </td>
                                      <td>  '.number_format($invd["itemunit"],2).' </td>
                                      <td>  '.number_format($invd["itemprice"],2).' </td>
                                      <td>  '.number_format($invd["itemtax"],2).' </td>
                                      <td>  '.number_format($invd["itemtotal"],2).' </td>
                                    </tr>';
                             
                                } 
                            
                        $html.= '
                    </tbody>
                    <tfoot class="total" >
                            <tr> 
                                       <td colspan="2">   </td>
                                       <td colspan="2" class="tot">  Sub Total  </td>
                                       <td class="tot" >    '.number_format($subtotal["itemtotal"],2).'   </td>
                           </tr>
                           <tr>      
                                       <td colspan="2">   </td>
                                       <td colspan="2" class="tot" >  Tax  </td>
                                       <td  class="tot" >'.number_format($tax["itemtotal"],2).'   </td>
                           </tr>
                           <tr> 
                                       <td colspan="2">   </td>
                                       <td colspan="2" class="tot" >  Total  </td>
                                       <td class="tot" >'.number_format($total["itemtotal"],2).'  </td>
                           </tr>
                    </tfoot>
                </table>
                
            </div>
           
';
// Load HTML content 
$dompdf->loadHtml($html);
 
 
// (Optional) Setup the paper size and orientation 
  $dompdf->setPaper('A4', 'portrait'); 
 // $dompdf->setPaper('A4', 'portrait'); 
 
// Render the HTML as PDF 
$dompdf->render(); 
 
// Output the generated PDF to Browser 
// $dompdf->download();
// $dompdf->stream();

$dompdf->stream("dompdf_out.pdf", array("Attachment" => false));

?>