local/modules/TracabilitePP/Hook/TracabiliteHook.php line 80

  1. <?php
  2. namespace TracabilitePP\Hook;
  3. use Propel\Runtime\ActiveQuery\Criteria;
  4. use Thelia\Core\Event\Hook\HookRenderBlockEvent;
  5. use Thelia\Core\Event\Hook\HookRenderEvent;
  6. use Thelia\Core\Event\Image\ImageEvent;
  7. use Thelia\Core\Event\TheliaEvents;
  8. use Thelia\Core\Hook\BaseHook;
  9. use Thelia\Tools\URL;
  10. use Thelia\log\Tlog;
  11. use Thelia\Model\ModuleImageQuery;
  12. use Thelia\Model\ConfigQuery;
  13. use Thelia\Model\OrderProductQuery;
  14. use Thelia\Model\Map\ProductTableMap;
  15. use Thelia\Model\Map\OrderProductTableMap;
  16. use TracabilitePP\TracabilitePP;
  17. use TracabilitePP\Model\TracabiliteQuery;
  18. use TracabilitePP\Model\Map\TracabiliteTableMap;
  19. /**
  20.  * Class TracabiliteHook
  21.  * @package TracabilitePP\Hook
  22.  * @author François Carfantan <francois.carfantan@kmel.bzh>
  23.  */
  24. class TracabiliteHook extends BaseHook
  25. {
  26.     public function onMainTopMenuTools(HookRenderBlockEvent $event)
  27.     {
  28.         $event->add(
  29.             [
  30.                 'id' => 'tools_menu_tracabilite',
  31.                 'class' => '',
  32.                 'url' =>  URL::getInstance()->absoluteUrl('/admin/module/TracabilitePP'),
  33.                 'title' => 'Tracabilite'
  34.             ]
  35.         );
  36.     }
  37.     public function OnDeliveryOrderProduct(HookRenderEvent $event)
  38.     {
  39.         $passName =  $this->translator->trans('Plant Passport', [], 'tracabilitepp.bo.default');
  40.         $orderProductId $event->getArgument('order_product');
  41.         $hook_name $event->getCode();
  42.     if($hook_name=="invoice.order-product"){
  43.         $nbcol 6;
  44.     }
  45.     else{
  46.         $nbcol 3;
  47.     }
  48.         $lotCode $this->getLotCode($orderProductId);
  49.         $moduleId $this->getModule()->getModuleId();
  50.         $imageUrl $this->getImageUrl($moduleId);
  51.         $html="<tr class='table-2'>";
  52.             $html$html."<td colspan='$nbcol'>";
  53.                 $html$html."<table  style='border:none'>";
  54.                     $html$html."<tr>";
  55.                         $html$html."<td  style='border:none'>";
  56.                                  $html$html."<img style='width:40px; padding:3px' src='$imageUrl'/>";
  57.                         $html$html."</td>";
  58.                         $html$html."<td style='border:none'>";
  59.                         $html$html."<b>.$passName.</b>";
  60.                     $html$html."</td></tr>";
  61.         $html$html."<tr><td colspan='2'  style='border:none'>";
  62.         $html$html.$this->getPassString($event->getArgument('order_product'), $lotCode);
  63.         $html$html"</td></tr></table></td></tr>";
  64.         $event->add($html);
  65.     }
  66.     public function OnOrderProductList(HookRenderEvent $event)
  67.     {
  68.         $orderProductId $event->getArgument('order_product_id');
  69.         $title =  $this->translator->trans('Lot reference', [], 'tracabilitepp.bo.default');
  70.         $lotCode $this->getLotCode($orderProductId);
  71.         $event->add('<dl class="dl-horizontal"><dt>' $title '</dt><dd>' $lotCode '</dd></dl>');
  72.     }
  73.     private function getImageUrl($moduleId)
  74.     {
  75.         $imageFileName ModuleImageQuery::create()->findOneByModuleId($moduleId)->getFile();
  76.         $payboxDisplay = new Tracabilitepp();
  77.         $uploadDir $payboxDisplay->getUploadDir();
  78.         $event = new ImageEvent();
  79.         $event->setSourceFilepath(DS $uploadDir DS $imageFileName)
  80.             ->setCacheSubdirectory('tracabilitepp');
  81.         $this->dispatcher->dispatch($event,TheliaEvents::IMAGE_PROCESS);
  82.         return $event->getFileUrl();
  83.     }
  84.     private function getPassString($orderProductId$lotCode)
  85.     {
  86.         $passStringArray = array();
  87.         $title OrderProductQuery::create()->findPk($orderProductId)->getTitle();
  88.         $latinName  = (explode(","$title))[0];
  89.         array_push($passStringArray' <b>A</b> ' $latinName);
  90.         array_push($passStringArray'<b>B</b> ' ConfigQuery::read('passport_ref'));
  91.         array_push($passStringArray'<b>C</b> ' $lotCode);
  92.         array_push($passStringArray'<b>D</b> ' ConfigQuery::read('plant_origin'));
  93.         return implode(" "$passStringArray);
  94.     }
  95.     function getLotCode($orderProductId)
  96.     {
  97.         $lot TracabiliteQuery::create()
  98.             ->joinProduct()
  99.             ->addJoin(ProductTableMap::REFOrderProductTableMap::PRODUCT_REFCriteria::RIGHT_JOIN)
  100.             ->where(TracabiliteTableMap::START_DATE '<=' OrderProductTableMap::UPDATED_AT)
  101.             ->_and()
  102.             ->where(TracabiliteTableMap::END_DATE '>=' OrderProductTableMap::UPDATED_AT)
  103.             ->_or()
  104.             ->where(TracabiliteTableMap::END_DATE ' IS NULL')
  105.             ->where(OrderProductTableMap::ID ' = ?'intval($orderProductId))
  106.             ->select(TracabiliteTableMap::LOT_CODE)
  107.             ->findOne();
  108.         if ($lot == null) {
  109.             return "";
  110.         }
  111.         return $lot;
  112.     }
  113. }