local/modules/TracabilitePP/Hook/TracabiliteHook.php line 80
<?php
namespace TracabilitePP\Hook;
use Propel\Runtime\ActiveQuery\Criteria;
use Thelia\Core\Event\Hook\HookRenderBlockEvent;
use Thelia\Core\Event\Hook\HookRenderEvent;
use Thelia\Core\Event\Image\ImageEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Hook\BaseHook;
use Thelia\Tools\URL;
use Thelia\log\Tlog;
use Thelia\Model\ModuleImageQuery;
use Thelia\Model\ConfigQuery;
use Thelia\Model\OrderProductQuery;
use Thelia\Model\Map\ProductTableMap;
use Thelia\Model\Map\OrderProductTableMap;
use TracabilitePP\TracabilitePP;
use TracabilitePP\Model\TracabiliteQuery;
use TracabilitePP\Model\Map\TracabiliteTableMap;
/**
* Class TracabiliteHook
* @package TracabilitePP\Hook
* @author François Carfantan <francois.carfantan@kmel.bzh>
*/
class TracabiliteHook extends BaseHook
{
public function onMainTopMenuTools(HookRenderBlockEvent $event)
{
$event->add(
[
'id' => 'tools_menu_tracabilite',
'class' => '',
'url' => URL::getInstance()->absoluteUrl('/admin/module/TracabilitePP'),
'title' => 'Tracabilite'
]
);
}
public function OnDeliveryOrderProduct(HookRenderEvent $event)
{
$passName = $this->translator->trans('Plant Passport', [], 'tracabilitepp.bo.default');
$orderProductId = $event->getArgument('order_product');
$hook_name = $event->getCode();
if($hook_name=="invoice.order-product"){
$nbcol = 6;
}
else{
$nbcol = 3;
}
$lotCode = $this->getLotCode($orderProductId);
$moduleId = $this->getModule()->getModuleId();
$imageUrl = $this->getImageUrl($moduleId);
$html="<tr class='table-2'>";
$html= $html."<td colspan='$nbcol'>";
$html= $html."<table style='border:none'>";
$html= $html."<tr>";
$html= $html."<td style='border:none'>";
$html= $html."<img style='width:40px; padding:3px' src='$imageUrl'/>";
$html= $html."</td>";
$html= $html."<td style='border:none'>";
$html= $html."<b>.$passName.</b>";
$html= $html."</td></tr>";
$html= $html."<tr><td colspan='2' style='border:none'>";
$html= $html.$this->getPassString($event->getArgument('order_product'), $lotCode);
$html= $html. "</td></tr></table></td></tr>";
$event->add($html);
}
public function OnOrderProductList(HookRenderEvent $event)
{
$orderProductId = $event->getArgument('order_product_id');
$title = $this->translator->trans('Lot reference', [], 'tracabilitepp.bo.default');
$lotCode = $this->getLotCode($orderProductId);
$event->add('<dl class="dl-horizontal"><dt>' . $title . '</dt><dd>' . $lotCode . '</dd></dl>');
}
private function getImageUrl($moduleId)
{
$imageFileName = ModuleImageQuery::create()->findOneByModuleId($moduleId)->getFile();
$payboxDisplay = new Tracabilitepp();
$uploadDir = $payboxDisplay->getUploadDir();
$event = new ImageEvent();
$event->setSourceFilepath(DS . $uploadDir . DS . $imageFileName)
->setCacheSubdirectory('tracabilitepp');
$this->dispatcher->dispatch($event,TheliaEvents::IMAGE_PROCESS);
return $event->getFileUrl();
}
private function getPassString($orderProductId, $lotCode)
{
$passStringArray = array();
$title = OrderProductQuery::create()->findPk($orderProductId)->getTitle();
$latinName = (explode(",", $title))[0];
array_push($passStringArray, ' <b>A</b> ' . $latinName);
array_push($passStringArray, '<b>B</b> ' . ConfigQuery::read('passport_ref'));
array_push($passStringArray, '<b>C</b> ' . $lotCode);
array_push($passStringArray, '<b>D</b> ' . ConfigQuery::read('plant_origin'));
return implode(" ", $passStringArray);
}
function getLotCode($orderProductId)
{
$lot = TracabiliteQuery::create()
->joinProduct()
->addJoin(ProductTableMap::REF, OrderProductTableMap::PRODUCT_REF, Criteria::RIGHT_JOIN)
->where(TracabiliteTableMap::START_DATE . '<=' . OrderProductTableMap::UPDATED_AT)
->_and()
->where(TracabiliteTableMap::END_DATE . '>=' . OrderProductTableMap::UPDATED_AT)
->_or()
->where(TracabiliteTableMap::END_DATE . ' IS NULL')
->where(OrderProductTableMap::ID . ' = ?', intval($orderProductId))
->select(TracabiliteTableMap::LOT_CODE)
->findOne();
if ($lot == null) {
return "";
}
return $lot;
}
}