<?php
//20160507 - Modify Manual Database switches to target BAT1 - Christiaan
//20161108 - PH2 Extra - Invoice/Order process merged - Christiaan
//20161110 - PH2 Extra - Invoice pdf filename structure changed - Christiaan
//20161221 - new licence query including whatever licence is tied to the invoice - christiaan
//20180322 - Update VAT to use SystemSettings[VAT] and change VAT from 14% to 15% for 20180401 countrywide VAT increase - CL
//20180326 - fix rounding issue on Invoice item totals on PDF - CL
//20180326 - Add gross total for more accurate Vat calculation - CL

include_once('../htmltopdf/html2fpdf.php');
include_once("_nemo.basic.cls.php");
class PDFTransactionFooter extends HTML2FPDF
{
   var $intPageCount = 0;
   var $blnLocked = 0;
   var $strClient = "";

   function __construct($blnLocked,$strClient){
      //HTML2FPDF::HTML2FPDF();
      parent::__construct(); //201811 - PHP7 Upgrade - pj
      $this->blnLocked = $blnLocked;
      $this->strClient = $strClient;
   }

   function AddPage($orientation='')
   {// Override parent Addpage to set Footers on all pages
      //Start a new page
      if($this->state==0) $this->Open();
      $family=$this->FontFamily;
      $style=$this->FontStyle.($this->underline ? 'U' : '');
      $size=$this->FontSizePt;
      $lw=$this->LineWidth;
      $dc=$this->DrawColor;
      $fc=$this->FillColor;
      $tc=$this->TextColor;
      $cf=$this->ColorFlag;
      if($this->page>0)
      {
         //Page footer
         $this->InFooter=true;

         if($this->page > 0)
         {
            $this->Footer();
         }

         //$this->Footer();

         $this->InFooter=false;
         //Close page
         $this->_endpage();
      }
      //Start new page
      $this->_beginpage($orientation);
      //Set line cap style to square
      $this->_out('2 J');
      //Set line width
      $this->LineWidth=$lw;
      $this->_out(sprintf('%.2f w',$lw*$this->k));
      //Set font

      if($family) $this->SetFont($family,$style,$size);
      //Set colors
      $this->DrawColor=$dc;
      if($dc!='0 G') $this->_out($dc);
      $this->FillColor=$fc;
      if($fc!='0 g') $this->_out($fc);
      $this->TextColor=$tc;
      $this->ColorFlag=$cf;
      //Page header
      $this->Header();
      //Restore line width
      if($this->LineWidth!=$lw)
      {
         $this->LineWidth=$lw;
         $this->_out(sprintf('%.2f w',$lw*$this->k));
      }
      //Restore font
      if($family) $this->SetFont($family,$style,$size);
      //Restore colors
      if($this->DrawColor!=$dc)
      {
         $this->DrawColor=$dc;
         $this->_out($dc);
      }
      if($this->FillColor!=$fc)
      {
         $this->FillColor=$fc;
         $this->_out($fc);
      }
      $this->TextColor=$tc;
      $this->ColorFlag=$cf;
   }


   function Footer(){
      if(!$this->_numberingFooter)
        return;

      //! @desc The footer is printed in every page!
      //Position at 1.0 cm from bottom
      $this->intPageCount++;
      $this->SetY(-24);
      $this->SetFont('Arial','',10);
      $this->SetTextColor(0,0,0);
      //$this->Cell(0,14,(($this->blnLocked == 0 )?'Order Confirmation':'Tax Invoice').' for '.$this->strClient.' as at '.date("j M Y").'',0,0,'L');
      $this->SetFont('Arial','',10);
      $this->SetTextColor(0,0,0);


      // $this->Cell(0,14,'www.knowmore.co.zax',0,0,'R');


      $this->Ln(6);
      $this->SetY($this->GetY()+5);

      $this->SetTextColor(100 , 100 , 100);
      $this->SetDrawColor(100 , 100 , 100);
      $this->Line(20 , 285  , 190 , 285);
      $this->SetTextColor(99 , 100, 102);
      //$this->Cell(0,11,'Copyright exists. Please do not reproduce this material in any form without the written approval of Knowmore',0,0,'L');
      //$this->Cell(0,11,'T 021 5244 668',0,0,'R');
      //$this->Image('images/layers/pdffooter.jpeg',10,$this->GetY() - 10  ,191);

      //Return Font to normal
      $this->SetFont('Arial','',9);


      if(!$this->_numbering)
         $this->_numberingFooter=false;
   }
}
class PDF_Invoice extends GenrateInvoice
{
   var $FilePath;
   var $FileName;

   function getFilePath(){ return $this->FilePath; }
   function setFilePath($FilePath){ $this->FilePath = $FilePath; }
   function getFileName(){ return $this->FileName; }
   function setFileName($FileName){ $this->FileName = $FileName; }

   function __construct($InvoiceID, $BS=0)
   {

      parent::setInvoiceID($InvoiceID);
      parent::setDate(date("Ymd"));
      parent::setLocked($blnLocked);
      $this->__autoload();
      // Determine if locked
      $idb = new NemoDatabase("tblInvoice", 0, null,0);

      //20161110 - PH2 Extra - Invoice pdf filename structure changed - Christiaan
      $row = $idb->getRowSQL("SELECT tblInvoice.blnLocked, strInvoiceNumber
                                 FROM tblInvoice
                              WHERE (((tblInvoice.InvoiceID)=". $idb->qs($InvoiceID) ."));");
      $this->setLocked($row->blnLocked);
      $this->setInvoiceNumber($row->strInvoiceNumber);
      $this->generateInvoice($BS);
   }

   function __autoload()
   {
      include_once('../htmltopdf/html2fpdf.php');
   }

   function generateInvoice($BS=0)
   {
      global $xdb, $SystemSettings, $DATABASE_SETTINGS, $DATABASE_SETTINGS2, $MYSQLI, $db;

      $this->getMidInfo();
      $pdf = new PDFTransactionFooter($this->getLocked(),$this->strClient);

      //. $pdf->setFooter("test");
      $pdf->startPageNums();
      $pdf->SetTopMargin(15);
      $pdf->AddPage();
      $pdf->AliasNbPages();
      $pdf->setY($pdf->getY()+6);
      $pdf->SetFontSize(18);

      // $pdf->SetTextColor(0 , 120, 95);
      $pdf->SetTextColor(0 , 0, 0);


      //$this->Image('images/layers/pdffooter.jpeg',10,$this->GetY() - 10  ,191);
      // $pdf->Image('images/Advanced PDF.jpg',10,$pdf->GetY()-10  ,190);
      $pdf->Image("images/Advanced PDF.jpg",0,0,210);

      //20161108 - PH2 Extra - Invoice/Order process merged - Christiaan
      $pdf->SetFont('Arial','B',18);
      $pdf->Cell(0,15,(($this->getLocked() == 0 )?"ORDER CONFIRMATION":"TAX INVOICE"),0,0,'R');

      //$pdf->setY($pdf->getY()-12);
      $pdf->fontsize = "13";
      $pdf->setY($pdf->getY()-8);
      //$pdf->WriteHTML($this->getTopInfo());

      $pdf->Ln(4);
      // $pdf->SetTextColor(100, 100, 100);
      $pdf->SetTextColor(0, 0, 0);

      $pdf->SetFontSize(13);
      $pdf->SetX($pdf->GetX());

      //20161108 - PH2 Extra - Invoice/Order process merged - Christiaan
      $pdf->SetFont('Arial','B',13);
      $pdf->Cell(30,35,(($this->getLocked() == 0 )?"ORDER":"INVOICE")." NO:",0,0,'L');

      $pdf->SetFont('Arial','',13);
      $pdf->Cell(30,35,$this->getTenDigitInvoiceNo(),0,0,'L');
      $pdf->Ln(5);
      $pdf->SetX($pdf->GetX());
      $pdf->SetFont('Arial','B',13);
      $pdf->Cell(30,35,"DATE:",0,0,'L');
      $pdf->SetFont('Arial','',13);
      $pdf->Cell(30,35,date("j M Y",strtotime($this->getDate())),0,0,'L');

      $pdf->SetY($pdf->GetY()+12);
      $pdf->Ln(1);
      $pdf->SetFontSize(16);
      $pdf->SetFont('Arial','B',16);
      $pdf->Cell(95,35,"SUPPLIER",0,0,'L');
      $pdf->Cell(25,35,"RECIPIENT",0,0,'L');

      $pdf->setY($pdf->getY()+25);
      $pdf->fontsize = "9";
      $pdf->SetFont('Arial','',9);
      $pdf->WriteHTML($this->getMidInfo($BS));
      $pdf->fontsize = "9";
      $pdf->SetFont('Arial','',9);
      //$pdf->WriteHTML($this->getProductInfo());

      $xdbProduct = new NemoDatabase("tblLicence", 0, null,0);
      $strPracticeConsultant = "";
      // *Note: A Practice Consultant will contact you within 2 working days of purchase.
      /*$rst = $xdbProduct->doQuery("SELECT tblProduct.strProduct,tblLicence.intUnits,tblLicence.intQuantum, tblProduct.strProductCode, Count(tblProduct.ProductID) AS Quantity,tblProduct.intHours, tblProduct.dblPrice,
                                   Sum(tblLicence.dblAmountIncl) AS AmountIncl, Sum(tblLicence.dblAmountExcl) AS AmountExcl
                              FROM tblProduct INNER JOIN (tblLicence INNER JOIN tblInvoice ON tblLicence.refInvoiceID = tblInvoice.InvoiceID) ON tblProduct.ProductID = tblLicence.refProductID
                              WHERE tblInvoice.InvoiceID = ".$this->InvoiceID."
                              GROUP BY tblProduct.strProduct, tblProduct.strProductCode, tblProduct.dblPrice,tblLicence.intQuantity,tblLicence.dblAmountExcl,tblLicence.intUnits");*/

      ## USING TRAINING SIDE PDF BUILDER FOR TOOLS AFTER PAYMENT GATEWAY :: 20150917 :: JACQUES
      // old licence query
      // if($BS == 1)
      // {
      //    $db =
      //    $MYSQLI = mysqli_connect($DATABASE_SETTINGS2[$SystemSettings[SERVER_NAME]]->hostname
      //                         , $DATABASE_SETTINGS2[$SystemSettings[SERVER_NAME]]->username
      //                         , $DATABASE_SETTINGS2[$SystemSettings[SERVER_NAME]]->password
      //                         , $DATABASE_SETTINGS2[$SystemSettings[SERVER_NAME]]->database);


      //    $rst = $xdbProduct->doQuery(" SELECT * FROM tblLicence
      //                                  INNER JOIN tblProduct ON tblProduct.ProductID = tblLicence.refProductID
      //                                  WHERE refInvoiceID = ".$this->InvoiceID, 0);

      //    $db =
      //    $MYSQLI = mysqli_connect($DATABASE_SETTINGS[$SystemSettings[SERVER_NAME]]->hostname
      //                      , $DATABASE_SETTINGS[$SystemSettings[SERVER_NAME]]->username
      //                      , $DATABASE_SETTINGS[$SystemSettings[SERVER_NAME]]->password
      //                      , $DATABASE_SETTINGS[$SystemSettings[SERVER_NAME]]->database);
      // }
      // else
      // {
      //    $rst = $xdbProduct->doQuery("SELECT tblCourse.strCourse, tblLicence.dblAmountExcl, dblAmountIncl, LicenceID
      //                             FROM tblLicence
      //                             INNER JOIN tblCourse ON tblCourse.CourseID = tblLicence.refCourseID
      //                             WHERE refInvoiceID = ".$this->InvoiceID);
      // }

      //20161221 - new licence query including whatever licence is tied to the invoice - christiaan
      $rst = $xdb->doQuery("SELECT tblProductLicence.strLicenceDesc, tblProductLicence.intQuantity, tblProductLicence.dblAmountExcl, tblProductLicence.dblAmountIncl, tblProductLicence.LicenceID, tblProductLicence.refModuleID
                              FROM(
                              SELECT tblProduct.strProduct as strLicenceDesc, count( tblLicence.LicenceID) as intQuantity, SUM(tblLicence.dblAmountExcl) AS dblAmountExcl, SUM(tblLicence.dblAmountIncl) AS dblAmountIncl, tblLicence.LicenceID, tblLicence.refModuleID
                              FROM tblLicence
                              INNER JOIN tblProduct ON tblProduct.ProductID = tblLicence.refProductID
                              WHERE refInvoiceID = $this->InvoiceID
                              GROUP BY ProductID
                              ) tblProductLicence
                              WHERE 1=1
                              UNION ALL
                              SELECT tblCourse.strCourse as strLicenceDesc, 1 as intQuantity, tblLicence.dblAmountExcl, tblLicence.dblAmountIncl, tblLicence.LicenceID, tblLicence.refModuleID
                              FROM tblLicence
                              INNER JOIN tblCourse ON tblCourse.CourseID = tblLicence.refCourseID
                              WHERE refInvoiceID = $this->InvoiceID", 0);

      $SubTotal = 0;
      $GrossTotal = 0;
      $strDetailLines = "";
      $counter = 1;
      $tdColorx="#f3d8e0";
      while($row = $xdbProduct->fetch_object($rst))
      {
         
         if($counter % 2 == 0) {
            $tdColor="#f3d8e0";
         }else{
            $tdColor="#ffffff";
         }
         $VAT = 0;
         $VAT = $row->dblAmountIncl - $row->dblAmountExcl;
         $SubTotal += $row->dblAmountExcl;
         $GrossTotal += $row->dblAmountIncl; //20180327 - Add gross total for more accurate Vat calculation - CL

         ## USING TRAINING SIDE PDF BUILDER FOR TOOLS AFTER PAYMENT GATEWAY :: 20150917 :: JACQUES
         //old licence condition statement
         // if($BS == 1)
         // {
         //    $product = $row->strProduct;
         // }
         // else
         // {
         //    $product = $row->strCourse;
         // }
         //20161221 - new licence query including whatever licence is tied to the invoice - christiaan
         if ($row->refModuleID != 0) {
            $rowModule = $xdb->getRowSQL("SELECT * FROM modModule WHERE ModuleID ='".$row->refModuleID."'", 0);
            $product = $row->strLicenceDesc . " - ". $rowModule->strModuleName ;
         }else{
            $product = $row->strLicenceDesc;
         }
         // $product = $row->strLicenceDesc;

         //if($row->dblAmountExcl << 0)
         //   $row->strCourse .= " Discount";

         $this->totalCost = $this->totalCost + $row->AmountExcl;
         //20180326 - fix rounding issue on Invoice item totals on PDF - CL
         $strDetailLines .= "<tr>
                           <td width='15%' bgcolor='$tdColor'>5200028/00907</td>
                           <td width='30%' bgcolor='$tdColor'>$product</td>
                           <td width='15%' bgcolor='$tdColor' align='right'>".number_format($row->dblAmountExcl,2)."&nbsp;</td>
                           <td width='5%' bgcolor='$tdColor' align='right'>$row->intQuantity</td>
                           <td width='10%' bgcolor='$tdColor' align='right'>".number_format($VAT,2)."</td>
                           <td width='15%' bgcolor='$tdColor' align='right'>".number_format(round($row->dblAmountIncl,2),2)."</td>
                           </tr>
                           <tr><td bgcolor='#ffffff'></td></tr>
                           ";

         $counter += 1;
      }
      $this->setSubTotal($SubTotal);
      //20180326 - Add gross total for more accurate Vat calculation - CL
      $this->setGrossTotal($GrossTotal);

      $strOutput = "
         <span style='color:#ffffff;background-color:#990000;'>
            <table border=0 cellpadding=0 cellspacing=0 width='100%'>
               <tr style='color:#ffffff;'>
                  <td width='15%' bgcolor='#b57f00' style='color:#ffffff;' ><B>ACCOUNT CODE</B></td>
                  <td width='30%' bgcolor='#b57f00' style='color:#ffffff;' ><B>DESCRIPTION</B></td>
                  <td width='15%' bgcolor='#b57f00' style='color:#ffffff;' align='right'><B>UNIT PRICE</B></td>
                  <td width='5%' bgcolor='#b57f00' style='color:#ffffff;'  align='right'><B>QTY</B></td>
                  <td width='10%' bgcolor='#b57f00' style='color:#ffffff;' align='right'><B>VAT</B></td>
                  <td width='15%' bgcolor='#b57f00' align='right'><B>TOTAL</B></td>
               </tr>
             </table>
         </span>";
      $pdf->WriteHTML($strOutput);
      // $pdf->SetDrawColor(0, 0, 0);
      // $pdf->Line(10, $pdf->getY()-2.8, 200, $pdf->getY()-2.8);
      $pdf->setY($pdf->getY()-2);
      $strOutput = "
         <span style='color:#000000;'>
              <table border=0 cellpadding=0 cellspacing=0 width='100%'>$strDetailLines</table>
         </span>
";
      $pdf->WriteHTML($strOutput);

      // $pdf->SetDrawColor(0, 0, 0);
      // $pdf->Line(10, 175, 200, 175);
      $pdf->setY($pdf->GetY()+1);
      $pdf->fontsize = "9";
      $pdf->SetFont('Arial','',9);
      $pdf->WriteHTML($this->getPaymentDetails());
      $pdf->fontsize = "9";
      $pdf->SetFont('Arial','',9);


      $height_of_cell = 60; // mm
      $page_height = 286.93; // mm (portrait letter)
      $page_max_height = $page_height - ($page_height/3);

      if(file_exists("images/KM-Transparent.jpg")){
         // if($page_max_height < $pdf->GetY())
         // {
         //    $pdf->AddPage();
         // }
         $pdf->Image("images/KM-Transparent.jpg",152,255.5,50);
      }



      /**
       * blnLocked
       * 0 = order
       * 1 = invoice
       **/

      //$pdf->AddPage();
      //$pdf->fontsize = "9";
      //$pdf->SetFont('Arial','',9);
      //$pdf->WriteHTML($this->displayLicences());

      ## USING TRAINING SIDE PDF BUILDER FOR TOOLS AFTER PAYMENT GATEWAY :: 20150917 :: JACQUES
      
      //commented out $BS condition as we are getting whatever licences are in the invoice rather - christiaan
      // if($BS == 1)
      // {
      //    //20161110 - PH2 Extra - Invoice pdf filename structure changed - Christiaan
      //    // $pdf->Output($SystemSettings[InvoicePdfDirAdmin].(($this->getLocked() == 0 )?"Order Confirmation":"Invoice")."-".$this->getInvoiceID().".".date("Ymd").".pdf", 'F');
      //    // $this->setFilePath($SystemSettings[InvoicePdfDirAdmin].(($this->getLocked() == 0 )?"Order Confirmation":"Invoice")."-".$this->getInvoiceID().".".date("Ymd").".pdf");
      //    // $this->setFileName((($this->getLocked() == 0 )?"Order Confirmation":"Invoice")."-".$this->getInvoiceID().".".date("Ymd").".pdf");
      //    $pdf->Output($SystemSettings[InvoicePdfDirAdmin]."Order Confirmation-".$this->getInvoiceNumber().".".date("Ymd").".pdf", 'F');
      //    $this->setFilePath($SystemSettings[InvoicePdfDirAdmin]."Order Confirmation-".$this->getInvoiceNumber().".".date("Ymd").".pdf");
      //    $this->setFileName("Order Confirmation-".$this->getInvoiceID().".".date("Ymd").".pdf");

      // }
      // else
      // {
         //20161221 - new licence query including whatever licence is tied to the invoice - christiaan
         $rowInvoice = $xdb->getRowSQL("SELECT strInvoiceNumber FROM tblInvoice WHERE InvoiceID = ". $xdb->qs($this->getInvoiceID()));

         //20161110 - PH2 Extra - Invoice pdf filename structure changed - Christiaan
         // $pdf->Output($SystemSettings[InvoicePdfDirAdmin].(($this->getLocked() == 0 )?"Order Confirmation":"Invoice")."-".$rowInvoice->strInvoiceNumber.".".date("Ymd").".pdf", 'F');
         // $this->setFilePath($SystemSettings[InvoicePdfDirAdmin].(($this->getLocked() == 0 )?"Order Confirmation":"Invoice")."-".$rowInvoice->strInvoiceNumber.".".date("Ymd").".pdf");
         // $this->setFileName((($this->getLocked() == 0 )?"Order Confirmation":"Invoice")."-".$rowInvoice->strInvoiceNumber.".".date("Ymd").".pdf");
         $pdf->Output($SystemSettings[InvoicePdfDirAdmin].( ($this->getLocked() == 0 )?"Order Confirmation":"Invoice")."-".$rowInvoice->strInvoiceNumber.".".date("Ymd").".pdf", 'F');
         $this->setFilePath($SystemSettings[InvoicePdfDirAdmin].( ($this->getLocked() == 0 )?"Order Confirmation":"Invoice")."-".$rowInvoice->strInvoiceNumber.".".date("Ymd").".pdf");
         $this->setFileName(( ($this->getLocked() == 0 )?"Order Confirmation":"Invoice")."-".$rowInvoice->strInvoiceNumber.".".date("Ymd").".pdf");
      //}
      //echo $SystemSettings[InvoicePdfDirAdmin].$rowInvoice->strInvoiceNumber.".".date("Ymd").".pdf";
   }
}

class GenrateInvoice
{
   var $InvoiceID;
   var $strInvoiceNumber;
   var $Date;
   var $arrSupplier = array();
   var $SubTotal;
   var $GrossTotal;
   var $blnLocked;
   var $strClient = "";

   function getInvoiceID(){ return $this->InvoiceID; }
   function setInvoiceID($InvoiceID){ $this->InvoiceID = $InvoiceID; }
   //20161110 - PH2 Extra - Invoice pdf filename structure changed - Christiaan
   function getInvoiceNumber(){ return $this->strInvoiceNumber; }
   function setInvoiceNumber($strInvoiceNumber){ $this->strInvoiceNumber = $strInvoiceNumber; }
   function getDate(){ return $this->Date; }
   function setDate($Date){ $this->Date = $Date; }
   function getSubTotal(){ return $this->SubTotal; }
   function setSubTotal($SubTotal){ $this->SubTotal = $SubTotal; }
   //20180326 - Add gross total for more accurate Vat calculation - CL
   function getGrossTotal(){ return $this->GrossTotal; }
   function setGrossTotal($GrossTotal){ $this->GrossTotal = $GrossTotal; }
   function getLocked(){return $this->blnLocked; }
   function setLocked($blnLocked){ $this->blnLocked = $blnLocked; }

   function __construct($InvoiceID)
   {
      $this->__autoload();
      $this->InvoiceID = $InvoiceID;
      $this->setDate(date("Ymd"));

      // Determine if locked
      $xdb = new sqlObject("tblInvoice",0);
      //20161110 - PH2 Extra - Invoice pdf filename structure changed - Christiaan
      $row = $xdb->getRowSQL("SELECT tblInvoice.blnLocked, strInvoiceNumber
                                 FROM tblInvoice
                              WHERE (((tblInvoice.InvoiceID)=$InvoiceID));");
      $this->setLocked($row->blnLocked);
      $this->setInvoiceNumber($row->strInvoiceNumber);
   }

   function getTenDigitInvoiceNo()
   {#20100426
      $TenDigitInvoiceNo = "";
      // Return the current InvoiceID with leading zeros;
      //$len = 10 - strlen($this->InvoiceID);
      $len = 8 - strlen($this->strInvoiceNumber);
      for($n=0 ; $n < $len  ;$n++ )
      {
         $TenDigitInvoiceNo .= "0";
      }
      //20161108 - PH2 Extra - Invoice/Order process merged - Christiaan
      //return $TenDigitInvoiceNo.$this->InvoiceID;
      return $TenDigitInvoiceNo.$this->strInvoiceNumber;
   }

   function getTopInfo()
   {
      // (($this->getLocked() == 0 )?"ORDER CONFIRMATION":"TAX INVOICE")
      $strOutput = "
         <span style='color:#00785F;'>
            <table cellspacing=0 cellpadding=0 border=0 width='100%'>
               <tr>
                  <td colspan='100%' align='right'>
                  </td>
               </tr>
            </table>
         </span>
         <span style='color:#000000;'>
            <table cellspacing=0 cellpadding=0 border=0 width='100%'>
               <tr><td colspan='100%' align='center'><td colspan='100%' align='right'>".(($this->getLocked() == 0 )?"Order Confirmation":"Tax Invoice")."</td></td></tr>
               <tr>
                  <td width='10%' align='left'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ".(($this->getLocked() == 0 )?"Order":"Invoice")." No.:</td>
                  <td align='left'>".$this->getTenDigitInvoiceNo()."</td>
               </tr>
               <tr>
                  <td align='left'>Date:</td>
                  <td align='left'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ".date("j M Y",strtotime($this->getDate()))."
                  </td>
               </tr>
            </table>
         </span>";

      //echo $strOutput;
      return $strOutput;
   }

   function getMidInfo($BS=0)
   {
      global $xdb, $SystemSettings, $DATABASE_SETTINGS, $DATABASE_SETTINGS2, $MYSQLI, $db;
      // Load Recipient info

      if($BS == 1)
      {
         $db =
         $MYSQLI = mysqli_connect($DATABASE_SETTINGS2[$SystemSettings[SERVER_NAME]]->hostname
                              , $DATABASE_SETTINGS2[$SystemSettings[SERVER_NAME]]->username
                              , $DATABASE_SETTINGS2[$SystemSettings[SERVER_NAME]]->password
                              , $DATABASE_SETTINGS2[$SystemSettings[SERVER_NAME]]->database);


         $row = $xdb->getRowSQL("SELECT *
                             FROM tblInvoice
                             INNER JOIN tblPractice ON tblInvoice.refPracticeID = tblPractice.PracticeID
                             WHERE tblInvoice.InvoiceID = ".$this->InvoiceID);

         $Company = $row->strPractice;
         $Client = $row->strContact;
         $Address1 = $row->strAddressPostal1;
         $Address2 = $row->strAddressPostal2;
         $Address3 = $row->strAddressPostal3;
         $Code = $row->strAddressPostal4;
         $VatNumber = $row->strVat;

         $db =
         $MYSQLI = mysqli_connect($DATABASE_SETTINGS[$SystemSettings[SERVER_NAME]]->hostname
                           , $DATABASE_SETTINGS[$SystemSettings[SERVER_NAME]]->username
                           , $DATABASE_SETTINGS[$SystemSettings[SERVER_NAME]]->password
                           , $DATABASE_SETTINGS[$SystemSettings[SERVER_NAME]]->database);
      }
      else
      {
         $xdbLearner = new NemoDatabase("tblLearner", 0, null,0);
         $row = $xdbLearner->getRowSQL("SELECT tblLearner.*
                             FROM tblInvoice INNER JOIN tblLearner ON tblInvoice.refLearnerID = tblLearner.LearnerID
                             WHERE tblInvoice.InvoiceID = ".$this->InvoiceID);

         $Company = $row->strCompany;
         $Client = $row->strName . " " . $row->strSurname;
         $Address1 = $row->strAddress1;
         $Address2 = $row->strAddress2;
         $Address3 = $row->strAddress3;
         $Code = $row->strCode;
         $VatNumber = $row->strVatNumber;
      }


      /*if($row->strClient == "Private")
      {
         $row->strClient = $row->strRegisteredName;
         $row->strContactPerson = $row->strContact;
         $row->strAddressPostal1 = $row->P1;
         $row->strAddressPostal2 = $row->P2;
         $row->strAddressPostal3 = $row->P3;
         $row->strAddressPostal4 = $row->P4;
         $row->strVat = $row->PVat;
      }else{//20101201 - pj
         $row->strClient = $row->strClientLong;
      }
      */

      // Load Supplier info

      $strOutputDetail = "<tr>
                        <td width='10%' align='left'><b>Company:</b></td>
                        <td width='40%'>Old Mutual Wealth Services Company (Pty) Ltd</td>
                        <td align='left' width='10%'><b>Company :</b></td>
                        <td>$Company</td></tr>
                     <tr>
                        <td align='left'><b>Address :</b></td>
                        <td>The Estuaries </td>
                        <td align='left'><b>Name :</b></td>
                        <td>$Client</td></tr>
                     <tr>
                        <td align='left'><b> </b></td>
                        <td>2 Oxbow Crescent, Century City</td>
                        <td align='left'><b>Address :</b></td>
                        <td>$Address1</td></tr>
                     <tr>
                        <td></td>
                        <td>7441</td>
                        <td></td>
                        <td>$Address2</td></tr>
                     <tr>
                        <td align='left'><b>Tel:</b></td>
                        <td>021 524 4400 </td>
                        <td></td>
                        <td>$Address3</td></tr>
                     <tr>
                        <td align='left'><b>Email: </b></td>
                        <td>accounts@knowmore.co.za</td>
                        <td></td>
                        <td>$Code</td>
                     </tr>
                     <tr>
                        <td align='left'><b>Reg no.:</b></td>
                        <td colspan=100%>2012/100073/07</td>
                     </tr>
                     <tr>
                        <td align='left'><b>VAT No.: </b></td>
                        <td> 4890267778</td>
                        <td><b>VAT No.: </b></td>
                        <td>$VatNumber</td>
                     </tr>
                     <tr>
                        <td align='left' colspan='2'></td>
                        <td align='left' colspan='2'></td>
                     </tr>
                  ";

      $strOutput = "
            <span style='color:#000000;'>
               <table border=0 cellpadding=0 cellspacing=0 width='100%'>$strOutputDetail</table>
            </span>
               ";
      //echo $strOutput;
      return $strOutput;

   }

   function getProductInfo()
   {//Business Decision: 20100528 : Pj and nadia phone call
      $xdbProduct = new NemoDatabase("tblLicence", 0, null,0);
      $strPracticeConsultant = "";
      // *Note: A Practice Consultant will contact you within 2 working days of purchase.
      /*$rst = $xdbProduct->doQuery("SELECT tblProduct.strProduct,tblLicence.intUnits,tblLicence.intQuantum, tblProduct.strProductCode, Count(tblProduct.ProductID) AS Quantity,tblProduct.intHours, tblProduct.dblPrice,
                                   Sum(tblLicence.dblAmountIncl) AS AmountIncl, Sum(tblLicence.dblAmountExcl) AS AmountExcl
                              FROM tblProduct INNER JOIN (tblLicence INNER JOIN tblInvoice ON tblLicence.refInvoiceID = tblInvoice.InvoiceID) ON tblProduct.ProductID = tblLicence.refProductID
                              WHERE tblInvoice.InvoiceID = ".$this->InvoiceID."
                              GROUP BY tblProduct.strProduct, tblProduct.strProductCode, tblProduct.dblPrice,tblLicence.intQuantity,tblLicence.dblAmountExcl,tblLicence.intUnits");*/

      $rst = $xdbProduct->doQuery("SELECT tblCourse.strCourse, tblLicence.dblAmountExcl, dblAmountIncl, tblLicence.LicanceID
                                  FROM tblLicence
                                  INNER JOIN tblCourse ON tblCourse.CourseID = tblLicence.refCourseID
                                  WHERE refInvoiceID = ".$this->InvoiceID);

      $SubTotal = 0;
      $GrossTotal = 0;
      while($row = $xdbProduct->fetch_object($rst))
      {
         $VAT = 0;
         $VAT = $row->dblAmountIncl - $row->dblAmountExcl;
         $SubTotal += $row->dblAmountExcl;
         $GrossTotal += $row->dblAmountIncl;//20180327 - Add gross total for more accurate Vat calculation - CL

         $this->totalCost = $this->totalCost + $row->AmountExcl;
         //20180326 - fix rounding issue on Invoice item totals on PDF - CL
         $strOutputDetail .= "<tr>
                           <td width='15%'>Unknown</td>
                           <td width='30%'>$row->strCourse ($row->LicenceID)</td>
                           <td width='15%' align='right'>".number_format($row->dblAmountExcl,2)."&nbsp;</td>
                           <td width='5%' align='right'>1</td>
                           <td width='10%' align='right'>".number_format($VAT,2)."</td>
                           <td width='15%' align='right'>".number_format(round($row->dblAmountIncl,2),2)."</td>
                           </tr>";
      }
      $this->setSubTotal($SubTotal);
      //20180326 - Add gross total for more accurate Vat calculation - CL
      $this->setGrossTotal($GrossTotal);

      $strOutput = "
         <span style='color:#000000;'>
            <table border=0 cellpadding=0 cellspacing=0 width='100%'>
               <tr>
                  <td width='15%' ><B>ACCOUNT CODE</B></td>
                  <td width='30%' ><B>DESCRIPTION</B></td>
                  <td width='15%' align='right'><B>UNIT PRICE</B></td>
                  <td width='5%'  align='right'><B>QTY</B></td>
                  <td width='10%' align='right'><B>VAT</B></td>
                  <td width='15%' align='right'><B>TOTAL</B></td>
               </tr>
             </table>
         </span>
         <span style='color:#000000;'>
              <table border=0 cellpadding=0 cellspacing=0 width='100%'>$strOutputDetail</table>
         </span>
         <span style='color:#000000;'>
              <table border=0 cellpadding=0 cellspacing=0 width='100%'><tr><td bgcolor='#ffffff'>$strPracticeConsultant</td></tr></table>
         </span>";
      //echo $strOutput;
      return $strOutput;
   }

   function getPaymentDetails()
   {
      global $SystemSettings;
      //20180322 - Update VAT to use SystemSettings[VAT] and change VAT from 14% to 15% for 20180401 countrywide VAT increase - CL
      //20180326 - Add gross total for more accurate Vat calculation - CL
      $SubTotal = round($this->getSubTotal(),2);
      $AmountDue = round($this->getGrossTotal(),2);
      $VAT = round($AmountDue - $SubTotal,2);

      $strOutputDetail .= "<tr>
                        <td width='88%' bgcolor='#ffffff' align='right'><B>SUB TOTAL</B></td>
                        <td bgcolor='#ffffff' align='right'>R ".number_format($SubTotal,2)."</td></tr>
                     <tr>
                        <td bgcolor='#ffffff' align='right'><B>VAT @".($SystemSettings[VAT]*100)."%</B></td>
                        <td align='right'>R ".number_format($VAT,2)."</td></tr>
                     <tr>
                        <td bgcolor='#ffffff' align='right'></td>
                        <td bgcolor='#ffffff' align='right'></tr>
                     <tr>
                        <td bgcolor='#ffffff' align='right' ><B>TOTAL</B></td>
                        <td bgcolor='#ffffff' align='right' >R ".number_format($AmountDue,2)."</td></tr>

                     ";

      if($this->blnLocked == 0)
      {
         $strOrder = "
               <table style='color:#000000;'>
                  <tr><td ><b>EFT Payments</b> - please find our bank details below:</td></tr>
               </table>
               <table>
                  <tr><td width='16%' align='left'><b>Account Name:</b></td><td width='84%'>Old Mutual Wealth Services Company (PTY) Limited - 2012/100073/07</td></tr>
                  <tr><td align='left'><b>Bank Name:</b></td><td>Nedbank</td></tr>
                  <tr><td align='left'><b>Branch Name:</b></td><td>Cape Town</td></tr>
                  <tr><td align='left'><b>Branch Code:</b></td><td>198765</td></tr>
                  <tr><td align='left'><b>Account Number:</b></td><td>1452069980</td></tr>
                  <tr><td align='left'><b>Reference:</b></td><td>Please use your Order number (".$this->getTenDigitInvoiceNo().") as reference</td></tr>
               </table>
            
            <span style='color:#000000;'>
               <table>
                  <tr><td width='82%'>Please scan proof of payment and email it to accounts@knowmore.co.za </td><td align='right'><a href='www.knowmore.co.za'>www.knowmore.co.za</a></td></tr>
               </table>
            </span>
            <span></span>";
      }
      else
         $strOrder = "";
               //<a href='mailto:accounts@knowmore.co.za'>accounts@knowmore.co.za</a>

      $strOutput = "<BR><BR>
         <span style='color:#000000;'>
            <table  border=0 celspacing=0 celpadding=0 width='100%'>$strOutputDetail</table>
         </span>
              $strOrder";
      //echo $strOutput;
      return $strOutput;
   }

/*
   function displayLicences()
   {
      $xdbLicence = new NemoDatabase("tblLicence", 0, null,0);

      $rst = $xdb->doQuery("SELECT tblLicence.LicenceID AS LicenceCode, tblProduct.strProduct, tblLicence.strDateActivated, tblLicence.strDateExpiry
                              FROM tblProduct RIGHT JOIN tblLicence ON tblProduct.ProductID = tblLicence.refProductID
                              WHERE (((tblLicence.refInvoiceID)=".$this->getInvoiceID()."))
                              ORDER BY tblProduct.strProduct, tblLicence.strDateActivated, tblLicence.strDateExpiry;");

      $rst = $xdbLicence->doQuery("SELECT *
                              FROM tblLicence
                              WHERE tblLicence.refInvoiceID = ".$this->InvoiceID);
      while($row = $xdbLicence->fetch_object($rst))
      {
         $strOutput .=" <tr>
                           <td  width='25%' >$row->LicenceCode</td>
                           <td  width='25%' >$row->strProduct</td>
                           <td  width='25%' align='right' >".DisplayReportDate($row->strDateActivated)."</td>
                           <td  width='25%' align='right' >".DisplayReportDate($row->strDateExpiry)."</td>
                           </tr>";
      }
      return "<table bgcolor=#329998 border=1 cellpadding=0 cellspacing=0 width='100%' >
               <tr>
                  <td width='25%' align='center' ><b>Licence Code</b></td>
                  <td width='25%' align='center' ><b>Product</b></td>
                  <td width='25%' align='center' ><b>Activation Date</b></td>
                  <td width='25%' align='center' ><b>Expiry Date</b></td>
               </tr>
               </table>
              <table border=0 width='100%' >

               $strOutput</table>";

   }
*/
}
?>