<?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(100,100,100);
      //$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(100,100,100);
      $this->Cell(0,14,'www.knowmore.co.za',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(16);
      $pdf->SetTextColor(0 , 120, 95);

      //20161108 - PH2 Extra - Invoice/Order process merged - Christiaan
      $pdf->Cell(0,5,(($this->getLocked() == 0 )?"ORDER CONFIRMATION":"TAX INVOICE"),0,0,'R');

      //$pdf->setY($pdf->getY()-12);
      $pdf->fontsize = "9";
      $pdf->setY($pdf->getY()-8);
      //$pdf->WriteHTML($this->getTopInfo());

      $pdf->Ln(4);
      $pdf->SetTextColor(100, 100, 100);
      $pdf->SetFontSize(9);
      $pdf->SetX($pdf->GetX()+10);

      //20161108 - PH2 Extra - Invoice/Order process merged - Christiaan
      $pdf->Cell(25,5,(($this->getLocked() == 0 )?"Order":"Invoice")." No.:",0,0,'L');

      $pdf->Cell(25,5,$this->getTenDigitInvoiceNo(),0,0,'L');
      $pdf->Ln(5);
      $pdf->SetX($pdf->GetX()+10);
      $pdf->Cell(25,5,"Date:",0,0,'L');
      $pdf->Cell(25,5,date("j M Y",strtotime($this->getDate())),0,0,'L');

      $pdf->SetY($pdf->GetY()+17);
      $pdf->Ln(1);
      $pdf->SetFontSize(16);
      $pdf->Cell(95,5,"SUPPLIER",0,0,'L');
      $pdf->Cell(25,5,"RECIPIENT",0,0,'L');

      $pdf->setY($pdf->getY()+9);
      $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
                              FROM(
                              SELECT tblProduct.strProduct as strLicenceDesc, count( tblLicence.LicenceID) as intQuantity, SUM(tblLicence.dblAmountExcl) AS dblAmountExcl, SUM(tblLicence.dblAmountIncl) AS dblAmountIncl, tblLicence.LicenceID
                              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
                              FROM tblLicence
                              INNER JOIN tblCourse ON tblCourse.CourseID = tblLicence.refCourseID
                              WHERE refInvoiceID = $this->InvoiceID", 0);

      $SubTotal = 0;
      $GrossTotal = 0;
      $strDetailLines = "";
      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

         ## 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
         $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%'>5200028/00907</td>
                           <td width='30%'>$product</td>
                           <td width='15%' align='right'>".number_format($row->dblAmountExcl,2)."&nbsp;</td>
                           <td width='5%' align='right'>$row->intQuantity</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:#646464;'>
            <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>";
      $pdf->WriteHTML($strOutput);
      $pdf->SetDrawColor(100, 100, 100);
      $pdf->Line(10, $pdf->getY()-2.8, 200, $pdf->getY()-2.8);
      $pdf->setY($pdf->getY()-2);
      $strOutput = "
         <span style='color:#646464;'>
              <table border=0 cellpadding=0 cellspacing=0 width='100%'>$strDetailLines</table>
         </span>
         <span style='color:#646464;'>
              <table border=0 cellpadding=0 cellspacing=0 width='100%'><tr><td>$strPracticeConsultant</td></tr></table>
         </span>";
      $pdf->WriteHTML($strOutput);

      $pdf->SetDrawColor(100, 100, 100);
      $pdf->Line(10, 175, 200, 175);
      $pdf->setY(170);
      $pdf->fontsize = "9";
      $pdf->SetFont('Arial','',9);
      $pdf->WriteHTML($this->getPaymentDetails());
      $pdf->fontsize = "9";
      $pdf->SetFont('Arial','',9);

      /**
       * 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:#646464;'>
            <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:#646464;'>
               <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:#646464;'>
            <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:#646464;'>
              <table border=0 cellpadding=0 cellspacing=0 width='100%'>$strOutputDetail</table>
         </span>
         <span style='color:#646464;'>
              <table border=0 cellpadding=0 cellspacing=0 width='100%'><tr><td>$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%' align='right'>Sub Total</td>
                        <td align='right'>R ".number_format($SubTotal,2)."</td></tr>
                     <tr>
                        <td align='right'>VAT @".($SystemSettings[VAT]*100)."%</td>
                        <td align='right'>R ".number_format($VAT,2)."</td></tr>
                     <tr>
                        <td align='right'></td>
                        <td align='right'></tr>
                     <tr>
                        <td align='right' >Total</td>
                        <td align='right' >R ".number_format($AmountDue,2)."</td></tr>

                     ";

      if($this->blnLocked == 0)
      {
         $strOrder = "
         <BR><BR>
            <span style='color:#646464;'>
               <table>
                  <tr><td ><b>EFT Payments</b> - please find our bank details below:</td></tr>
               </table>
            </span>
            <span style='color:#646464;'>
               <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>
            <span style='color:#646464;'>
               <table>
                  <tr><td>Please scan proof of payment and email it to accounts@knowmore.co.za </td></tr>
               </table>
            </span>";
      }
      else
         $strOrder = "";
					//<a href='mailto:accounts@knowmore.co.za'>accounts@knowmore.co.za</a>

      $strOutput = "<BR><BR>
         <span style='color:#646464;'>
            <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>";

   }
*/
}
?>