protected function execute(InputInterface $input, OutputInterface $output) { $this->_em = $this->getContainer()->get('doctrine')->getManager(); $this->sportmaster = $this->_em->getRepository('CoreBundle:Customer')->findOneBy(['abbr' => $this->getContainer()->getParameter('sportmaster_abbr')]); if ($this->getContainer()->getParameter("kernel.environment") == 'prod') { $dsn = 'mysql:dbname=MOW-sportmaster;host=ch13x100.europe.panorg.com:3307'; $user = '******'; $password = '******'; } else { $dsn = 'mysql:dbname=MOW-sportmaster;host=mowns003:3306'; $user = '******'; $password = '******'; } $this->pdo = new \PDO($dsn, $user, $password); $cronLog = new CronLog(); $cronLog->setStartAt(new DateTime()); $cronLog->setDescription($this->getDescription()); $cronLog->setScript($this->getName()); $queryAll = "SELECT * FROM container c WHERE trim(Name) != '' AND ShippingLine != 18"; if ($input->getOption('type') == self::FETCH_TYPE_DAILY) { $queryAll .= $this->dailyCondition; } $stmt = $this->pdo->prepare($queryAll); $stmt->execute(); $mappingType = []; $mappingType[1] = $this->_em->getRepository('CoreBundle:ContainerType')->findOneBy(['isoCode' => '22G0']); $mappingType[2] = $this->_em->getRepository('CoreBundle:ContainerType')->findOneBy(['isoCode' => '42G0']); $mappingType[3] = $this->_em->getRepository('CoreBundle:ContainerType')->findOneBy(['isoCode' => '45G0']); $validator = new Validator\ValidatorContainerNumber(); $processed = []; $m = memory_get_usage(true); $i = 0; try { while (($row = $stmt->fetch(\PDO::FETCH_ASSOC)) == true) { if (!isset($processed[strtolower($row['Name'])])) { if ($validator->validate($row['Name'])) { $containerType = isset($mappingType[$row['ContainerType']]) ? $mappingType[$row['ContainerType']] : null; $container = $this->_em->getRepository('CoreBundle:Container')->findOneBy(['name' => $row['Name']]); if (!$container) { $container = new \App\CoreBundle\Entity\Container(); $container->setName($row['Name'])->setContainerType($containerType); $this->_em->persist($container); $i++; if ($i % self::BATCH_SIZE == 0) { $this->_em->flush(); // $this->_em->clear(); } } $processed[strtolower($container->getName())] = 1; } else { $this->messageService->addMessage("Container name = {$row['Name']} is invalid, skipping", Message::ERROR); } } } $output->writeln("New Containers: {$i}"); $this->_em->flush(); $this->syncContainerWithoutAta($input->getOption('type')); } catch (NonUniqueResultException $e) { $this->messageService->addMessage("Not unique entity {$e->getMessage()} {$e->getFile()} {$e->getLine()}", Message::ERROR); } catch (\Exception $e) { $this->messageService->addMessage("Fatal exception: {$e->getMessage()} {$e->getFile()} {$e->getLine()}", Message::ERROR); } if ($this->messageService->hasCategory(Message::ERROR)) { $message = \Swift_Message::newInstance()->setSubject('Sync container errors')->setFrom('*****@*****.**')->setTo(['*****@*****.**', '*****@*****.**'])->setBody($this->messageService->dumpAsHtml(), 'text/html'); $this->getContainer()->get('mailer')->send($message); $this->getContainer()->get('swiftmailer.command.spool_send')->run(new ArgvInput(array()), new ConsoleOutput()); } $cronLog->setEndAt(new DateTime()); $this->_em->persist($cronLog); $this->_em->flush(); $output->writeln('memory: ' . (memory_get_usage(true) - $m)); $output->writeln('Completed'); }
protected function execute(InputInterface $input, OutputInterface $output) { $this->_em = $this->getContainer()->get('doctrine')->getManager(); $cronLog = new CronLog(); $cronLog->setStartAt(new DateTime()); $cronLog->setDescription($this->getDescription()); $cronLog->setScript($this->getName()); $qb = $this->_em->getRepository('CoreBundle:ContainerFlow')->createQueryBuilder('cf'); /** * @var ContainerFlow[] $flows */ $flows = $qb->innerJoin('cf.subcontractor', 'sub')->innerJoin('cf.container', 'container')->where('sub.code IN (:maersk, :cma, :fesco, :hmm, :apl) AND cf.ata IS NULL AND (cf.customDate1 IS NULL OR cf.customDate1 != :now)')->setMaxResults(self::SINGLE_FETCH)->setParameter('maersk', self::MAERSK_CODE)->setParameter('cma', self::CMA_CODE)->setParameter('fesco', self::FESCO_CODE)->setParameter('hmm', self::HMM_CODE)->setParameter('apl', self::APL_CODE)->setParameter('now', date('Y-m-d'))->getQuery()->getResult(); foreach ($flows as $flow) { $parser = $this->getParserForCarrier($flow->getSubcontactor()->getCode()); $classParts = explode('\\', get_class($parser)); $service = array_pop($classParts); try { $result = $parser->getData($flow->getContainer()->getName()); //try to find by bill of landing for CmaCgm & HMM if (is_null($result) && in_array($service, ['CmaCgm', 'HMM', 'MaerskClassic'])) { $mbl = ''; foreach ($flow->getShipments() as $shipment) { $mbl = $shipment->getMbl(); if ($mbl) { $result = $parser->getData($mbl, ExternalTruckingAbstract::SEARCH_BOL); break; } } } if ($result) { if ($result->getBillOfLanding()) { $this->checkMBL($result->getBillOfLanding(), $flow); } if ($result->getEta()) { $flow->setETA(new DateTime($result->getEta())); } if ($result->getAta()) { $flow->setATA(new DateTime($result->getAta())); } if ($result->getLoadOnVesselAt()) { $flow->setLoadOnVesselAt(new DateTime($result->getLoadOnVesselAt())); } foreach ($result->getEvents() as $event) { $vehicle = $this->addVehicle($event->getVessel(), $flow->getSubcontactor()); $flowEvent = $this->_em->getRepository('CoreBundle:ContainerFlowEvent')->findOneBy(['containerFlow' => $flow->getId(), 'details' => $event->getStatus(), 'locationDetails' => $event->getLocation(), 'occurredAt' => new DateTime($event->getDate())]); if (!$flowEvent) { $flowEvent = new ContainerFlowEvent(); $flowEvent->setContainerFlow($flow); } $flowEvent->setDetails(strtolower($event->getStatus())); $flowEvent->setLocationDetails($event->getLocation()); $flowEvent->setOccurredAt(new DateTime($event->getDate())); $flowEvent->setVehicle($vehicle); $flowEvent->setVoyage($event->getVoyage()); $this->_em->persist($flowEvent); $this->_em->flush(); } } else { $this->messageService->addMessage("Service {$service}: Container data not found for {$flow->getContainer()->getName()}", Message::ERROR); } } catch (\Exception $e) { $this->messageService->addMessage("Service {$service}, container {$flow->getContainer()->getName()}: {$e->getMessage()}; file {$e->getFile()}; line {$e->getLine()}", Message::ERROR); } //set date of last check, next check will be tomorrow $flow->setCustomDate1(new DateTime('now')); $this->_em->persist($flow); $this->_em->flush(); } if ($this->messageService->hasCategory(Message::ERROR)) { $message = \Swift_Message::newInstance()->setSubject('Container flow external trucking update')->setFrom('*****@*****.**')->setTo(['*****@*****.**', '*****@*****.**'])->setBody($this->messageService->dumpAsHtml(), 'text/html'); $this->getContainer()->get('mailer')->send($message); $this->getContainer()->get('swiftmailer.command.spool_send')->run(new ArgvInput(array()), new ConsoleOutput()); } $cronLog->setEndAt(new DateTime()); $this->_em->persist($cronLog); $this->_em->flush(); $output->writeln('Completed'); }