<?php
/**
 *
 * Interchange letter send report
 *
 * @author Moises Heberle [moises@solis.coop.br]
 *
 * @version $Id$
 *
 * \b Maintainers: \n
 * Eduardo Bonfandini [eduardo@solis.coop.br]
 * Jamiel Spezia [jamiel@solis.coop.br]
 * Luiz Gregory Filho [luiz@solis.coop.br]
 * Moises Heberle [moises@solis.coop.br]
 * Sandro Roberto Weisheimer [sandrow@solis.coop.br]
 *
 * @since
 * Class created on 05/05/2009
 *
 * \b Organization: \n
 * SOLIS - Cooperativa de Solucoes Livres \n
 * The Gnuteca2 Development Team
 *
 * \b CopyLeft: \n
 * CopyLeft (L) 2009 SOLIS - Cooperativa de Solucoes Livres \n
 *
 * \b License: \n
 * Licensed under GPL (for further details read the COPYING file or http://www.gnu.org/copyleft/gpl.html )
 *
 * \b History: \n
 * See history in SVN repository: http://gnuteca.solis.coop.br
 *
 **/

class rptInterchangeLetterSend
{
    public $MIOLO;
    public $module;
    public $filename;
    public $pathFile;
    public $modelFile;
    public $args;
    public $supplier;
    public $supplierTypeAndLocation;


    public function __construct($args = null)
    {
        //parent::__construct();
        $this->MIOLO  = MIOLO::getInstance();
        $this->module = MIOLO::getCurrentModule();
        $this->args   = $args;
        $supplier     = $args->supplier;
        $this->supplier                 = $supplier;
        $this->supplierTypeAndLocation  = $args->supplierTypeAndLocation;

        //Modelo html que sera convertido para PDF
        $this->modelFile = $this->MIOLO->getModulePath($this->module, 'handlers/administration/interchange/univates/solicitacao_de_intercambio_folha_timbrada.html');
        
        //Cria o nome do arquivo e instancia GnutecaPDF
        $this->filename = 'interchange_' . date('Ymd') . '.pdf';
        $this->pathFile = $this->MIOLO->getModulePath($this->module, "html/files/{$this->filename}");
        
        //FIXME Verificar se ainda sera necessario a geracao abaixo via FPDF
        /*
        //Set config of document
        $this->setFilePath($this->pathFile);
        $this->SetLineWidth(0.3);
        $this->SetFont('Arial', NULL, 10);
        $this->AddPage();
        $this->MultiCell(170, 5, $args->content, 0, 'L');

        $this->AddPage();
        $y = $this->GetY();
        $this->Cell(20, 5, _M('Company name', $this->module) . ':', 0, 2);
        $this->Cell(20, 5, _M('Neighborhood', $this->module) . ':', 0, 2);
        $this->Cell(20, 5, _M('City', $this->module) . ':', 0, 2);
        $this->Cell(20, 5, _M('Zip code', $this->module) . ':', 0, 2);
        $this->Cell(20, 5, _M('Phone', $this->module) . ':', 0, 2);
        $this->Cell(20, 5, _M('E-mail', $this->module) . ':', 0, 2);
        $this->SetY($y);
        $this->SetX(50);
        $this->Cell(80, 5, $supplier->companyName, 0, 2);
        $this->Cell(80, 5, $supplier->neighborhood, 0, 2);
        $this->Cell(80, 5, $supplier->city, 0, 2);
        $this->Cell(80, 5, $supplier->zipCode, 0, 2);
        $this->Cell(80, 5, $supplier->phone, 0, 2);
        $this->Cell(80, 5, $supplier->email, 0, 2);
        */
    }

    public function generate()
    {
        $content = $this->args->reportContent;
        $content = str_replace($this->getImageURL(), $this->MIOLO->getModulePath($this->module, 'html/images/' . $this->getLogoFile()), $content);
        
        $dp = new MDOMPDF();
        $dp->dompdf->load_html($content);
        $dp->dompdf->render();
        file_put_contents($this->pathFile, $dp->dompdf->output());
        return file_exists($this->pathFile);
    }

    /**
     * Get download link of the report file
     *
     * @return String
     */
    public function getDownloadURL()
    {
        return "index.php/{$this->module}/files/{$this->filename}";
    }
    
    
    public function getModelFile()
    {
        return $this->modelFile;
    }
    
    public function readModelFile()
    {
        $content = file_get_contents($this->getModelFile());
        $content = utf8_decode($content);
        $content = strtr($content, array(
            '$IMG_LOGO'                 => $this->getImageURL(),
            '$PAGE_HEADER'              => str_replace("\n", '<br>', INTERCHANGE_LETTER_HEADER),
            '$PAGE_FOOTER'              => str_replace("\n", '<br>', INTERCHANGE_LETTER_FOOTER),
            '$SUPPLIER_NAME'            => $this->supplierTypeAndLocation->companyName,
            '$EMAIL_CONFIRM_RECEIPT'    => INTERCHANGE_LETTER_EMAIL_CONFIRM_RECEIPT
        ));
        return $content;
    }
    
    public function getImageURL()
    {
        return $this->MIOLO->getActionURL($this->module, 'images:' . $this->getLogoFile());
    }
    
    public function getLogoFile()
    {
        return 'univates_logo.jpg';
    }
}
?>
