Jump to content

Custom PDF - Transferring data issues


rainbowfication

Recommended Posts

Hello all,

 

Im trying to create a new dynamic pdf doc, please note this is not a invoice.

 

I have based the code from pdf invoice and order slips and have been able to use a controller and class to generate a template pdf successfully with information in a TPL. Im having issues transferring data from the controller to smarty to use in the template.

 

From testing I think the issue is with the object that is sent across from the initial controller, i dont think it sending any currently but unsure how.

 

Any help will be appreciated showing me what Im doing wrong, even pointing me to some related documentation as i cant find much on how the pdf controller works.

 

I am not looking to buy a module as its not essential, Its more about learning how to do it.

 

Thanks

 

Tom

PdfVoucherController:

class PdfVoucherControllerCore extends FrontController
{
protected $display_header = false;
protected $display_footer = false;
protected $voucher_codes;
protected $code2;

public function postProcess()
{
 if (!$this->context->customer->isLogged())
  Tools::redirect('index.php?controller=authentication&back=order-follow');
 if (isset($_GET['id_code']) && Validate::isUnsignedId(Tools::getValue('id_code')) && Tools::getValue('code') == Code::getCodeByID(Tools::getValue('id_code')) )
  $this->voucher_codes = new Code(Tools::getValue('id_code'));
 if (isset($_GET['id_code']) && Validate::isUnsignedId(Tools::getValue('id_code')) && Tools::getValue('code') == Code::getCodeByID(Tools::getValue('id_code')) )
  $this->code2 = $_GET['id_code'];
 if (!isset($this->voucher_codes) || !Validate::isLoadedObject($this->voucher_codes))
  die(Tools::displayError('Order return not found.'));
}
public function display()
{
	$pdf = new PDF($this->voucher_codes, PDF::TEMPLATE_VOUCHER, $this->context->smarty, $this->code2);
	$pdf->render();
}
}

Code

class Code extends ObjectModel
{
public $customer;
public $code;
public $date_from;
public $date_to;
public $active = true;

public static $definition = array(
 'table' => 'voucher_codes',
 'primary' => 'id_code',
 'fields' => array(
  'code' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 64),
  'customer' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 64),
  'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
  'date_from' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 64),
  'date_to' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 64),
 ),
);

protected $webserviceParameters = array();

public static function getCodes($active = false)
{
 return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  SELECT *
  FROM `'._DB_PREFIX_.'voucher_codes`
  '.($active ? 'WHERE active = 1' : '').'
  ORDER BY `code` ASC
 ');
}
public function getCodesCollection()
{
 $order_codes = new Collection('OrderCodes');
 $order_codes->where('id_Code', '=', $this->id);
 return $order_codes;
}
public static function getIdByName($name)
{
 return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
  SELECT `id_code`
  FROM `'._DB_PREFIX_.'voucher_codes`
  WHERE `code` = \''.pSQL($name).'\'
 ');
}
public static function getCodeByID($id)
{
 return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
  SELECT `code`
  FROM `'._DB_PREFIX_.'voucher_codes`
  WHERE `id_code` = \''.pSQL($id).'\'
 ');
}
public static function getDateFromByID($id)
{
 return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
  SELECT `date_from`
  FROM `'._DB_PREFIX_.'voucher_codes`
  WHERE `id_code` = \''.pSQL($id).'\'
 ');
}
public static function getDateToByID($id)
{
 return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
  SELECT `date_to`
  FROM `'._DB_PREFIX_.'voucher_codes`
  WHERE `id_code` = \''.pSQL($id).'\'
 ');
}
public static function getCustomerByID($id)
{
 return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
  SELECT `customer`
  FROM `'._DB_PREFIX_.'voucher_codes`
  WHERE `id_code` = \''.pSQL($id).'\'
 ');
}
public function delete()
{
 if (parent::delete())
 {
 }
 return false;
}
}

HTMLTemplateVoucher

class HTMLTemplateVoucherCore extends HTMLTemplate
{
public $voucher;
public $voucher_codes;
public function __construct(Code $voucher_codes, $smarty)
{
 $this->voucher_codes = $voucher_codes;
 $this->voucher = new Code($voucher_codes->id_code);
 $this->smarty = $smarty;
}
public function getContent()
{
 $this->smarty->assign(array(
  'id_code' => $this->voucher->id_code,
  'code' => Code::getCodeByID($this->voucher_codes->id_code),
  'date_from' => Code::getDateFromByID($voucher_codes->id_code),
  'date_to' => Code::getDateToByID($voucher_codes->id_code)
 ));
 return $this->smarty->fetch($this->getTemplate('Voucher'));
}
public function getFilename()
{
 return sprintf('%06d', $this->voucher).'.pdf';
}
public function getBulkFilename()
{
 return 'Vouchers.pdf';
}
}

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...