bb-legacy/app/Tools.php line 1568

Open in your IDE?
  1.                     } else {
  2.                         $additionalDaysToSkip--;
  3.                     }
  4.                 }
  5.             } else {
  6.                 if (!$isHoliday && !$isWeekend) {
  7.                     if ($additionalDaysToSkip === 0) {
  8.                         return $dateTime->getTimestamp();
  9.                     } else {
  10.                         $additionalDaysToSkip--;
  11.                     }
  12.                 }
  13.             }
  14.             $dateTime->modify('+1 day');
  15.         }
  16.     }
  17.     /**
  18.      * @return int
  19.      *
  20.      * @throws Exception
  21.      *
  22.      * @var
  23.      */
  24.     public static function getExpeditionTimestampByDefault(?int $customerId$supplierId): int
  25.     {
  26.         return self::calculateExpeditionTimestamp($customerIdShippingDateService::DEFAULT_EXPEDITION_MESSAGE_TIME$supplierId00false);
  27.     }
  28.     /**
  29.      * @param int $idSupplier
  30.      * @param int|null $timestampToCompare
  31.      *
  32.      * @return int
  33.      *
  34.      * @throws Exception
  35.      */
  36.     public static function getExpeditionTimestampBySupplier(?int $customerIdint $idSupplier, ?int $timestampToComparebool $stock_3_5): int
  37.     {
  38.         $supplierExpeditionConfig plazo_aprovisionamiento_proveedor::getExpeditionInformation($idSupplier);
  39.         $cutTimeString $supplierExpeditionConfig['expedition_message_time'];
  40.         $expeditionAdditionalDaysDelay = (int)$supplierExpeditionConfig['expedition_days'];
  41.         if ($stock_3_5) {
  42.             $expeditionAdditionalDaysDelay = (int)$supplierExpeditionConfig['expedition_days_3_5'];
  43.         }
  44.         return self::calculateExpeditionTimestamp($customerId$cutTimeString$idSupplier$expeditionAdditionalDaysDelay$timestampToComparetrue);
  45.     }
  46.     /**
  47.      * @param array $product
  48.      *
  49.      * @return int
  50.      *
  51.      * @throws Exception
  52.      */
  53.     public static function getExpeditionTimestampForFutureStock(array $product): int
  54.     {
  55.         $daysUntilStockWillBeHere = \stock_venideros::calculateDaysTransit($product['id_product'], $product['id_product_attribute']);
  56.         if (!$daysUntilStockWillBeHere) {
  57.             $daysUntilStockWillBeHere = (int)self::jsonDecode(Configuration::get('STOCK_VENIDERO_DIAS_CORTESIA'), true);
  58.         }
  59.         $transitDays self::calculateDaysTransit($daysUntilStockWillBeHere);
  60.         $datetimeHelper = new DatetimeHelper();
  61.         $tmpExpeditionTimestamp $datetimeHelper->getCurrentTimestamp();
  62.         $tmpExpeditionTimestamp += $transitDays DatetimeHelper::SECONDS_A_DAY;
  63.         return self::getClosestNonHolidayTimestampInFuture($tmpExpeditionTimestampnull);
  64.     }
  65.     /**
  66.      * @param string $cutTimeString
  67.      * @param int $supplierId
  68.      * @param int $supplierAdditionalDaysDelay
  69.      * @param int|null $timestampToCompare
  70.      *
  71.      * @return int
  72.      *
  73.      * @throws Exception Calculates expedition timestamp taking into account:
  74.      *                   1. Customer specific delays (Ex: Workhuman 3 hour delay)
  75.      *                   2. Whether cut time has passed or not
  76.      *                   3. Supplier specific delays
  77.      *                   4. Skip holidays and weekends
  78.      *
  79.      * Accepts a $timestampToCompare argument that will be returned if its greater than the value calculated by the function
  80.      */
  81.     private static function calculateExpeditionTimestamp(
  82.         ?int $customerId,
  83.         string $cutTimeString,
  84.         int $supplierId,
  85.         int $supplierAdditionalDaysDelay,
  86.         ?int $timestampToCompare,
  87.         bool $includeSupplierHolidays
  88.     ): int {
  89.         $datetimeHelper = new DatetimeHelper();
  90.         $currentTimestamp $datetimeHelper->getCurrentTimestamp();
  91.         // Init to current timestamp
  92.         $tmpExpeditionTimestamp $currentTimestamp;
  93.         // Apply customer-defined delay
  94.         if (!empty($customerId)) {
  95.             $tmpExpeditionTimestamp self::addCustomerSpecificDelay($tmpExpeditionTimestamp$customerId);
  96.         }
  97.         // If the temporary expedition timestamp exceeds the cut time, add a day
  98.         if (self::timestampExceedsCutTime($tmpExpeditionTimestamp$cutTimeString)) {
  99.             $tmpExpeditionTimestamp $tmpExpeditionTimestamp DatetimeHelper::SECONDS_A_DAY;
  100.         }
  101.         // if $tmpExpeditionTimestamp is not a working day, get closest one
  102.         $closestNonHolidayTimestampInFuture self::getClosestNonHolidayTimestampInFuture($tmpExpeditionTimestamp$includeSupplierHolidays $supplierId null0);
  103.         // Add as many more days as 'expedition_days' value and get closest working day again
  104.         if ($supplierAdditionalDaysDelay 0) {
  105.             $closestNonHolidayTimestampInFuture self::getClosestNonHolidayTimestampInFuture($tmpExpeditionTimestamp$includeSupplierHolidays $supplierId null$supplierAdditionalDaysDelay);
  106.         }
  107.         if ($timestampToCompare $closestNonHolidayTimestampInFuture) {
  108.             return $timestampToCompare;
  109.         }
  110.         return $closestNonHolidayTimestampInFuture;
  111.     }
  112.     public static function replaceByAbsoluteURL($matches)
  113.     {
  114.         if (array_key_exists(1$matches) && array_key_exists(2$matches)) {
  115.             if (!preg_match('/^(?:https?:)?\/\//iUs'$matches[2])) {
  116.                 return $matches[1].BASE_URL.'public/css/'.$matches[2];
  117.             }
  118.             return $matches[0];
  119.         }
  120.         return false;
  121.     }
  122.     /**
  123.      * @param int $tmpExpeditionTimestamp
  124.      * @param string $cutTimeString
  125.      *
  126.      * @return bool
  127.      */
  128.     private static function timestampExceedsCutTime(int $tmpExpeditionTimestampstring $cutTimeString): bool
  129.     {
  130.         $datetimeHelper = new DatetimeHelper();
  131.         $tmpDatetime $datetimeHelper->getDatetimeFromTimestamp($tmpExpeditionTimestamp);
  132.         $cutTimeComponents explode(':'$cutTimeString);
  133.         $cutTimeHours = (int)trim($cutTimeComponents[0]);
  134.         $cutTimeMinutes = (int)trim($cutTimeComponents[1]);
  135.         $cutTimeSeconds = (int)trim($cutTimeComponents[2]);
  136.         $cutDatetime $datetimeHelper->getCurrentDateTime()->setTime($cutTimeHours$cutTimeMinutes$cutTimeSeconds);
  137.         return $tmpDatetime $cutDatetime;
  138.     }
  139.     /**
  140.      * Retornamos un array con la lista de todos los productos tienda disponibles
  141.      *
  142.      * @return array Listado de productos tienda
  143.      */
  144.     public static function getProductShopListArray()
  145.     {
  146.         $prestashopList explode(','Configuration::get('ID_SHOPS'));
  147.         return array_merge(\App\Entity\System\Product::SHOPIFY_PRODUCTS_IDS$prestashopList);
  148.     }
  149.     public static function getPackName($packName$subscriptionName '')
  150.     {
  151.         if ($packName == $subscriptionName) {
  152.             return $subscriptionName;
  153.         }
  154.         return $packName.' '.$subscriptionName;
  155.     }
  156.     public static function getGenericServerError($errorCode)
  157.     {
  158.         return sprintf(
  159.             self::l('Ha habido una incidencia y no ha podido completarse su pedido. Vaya a la sección %sContacto > Soporte técnico%s, seleccione Web BigBuy e indíquenos el código de la incidencia %s para poder ayudarle.''subscriptionController'),
  160.             '<a href="'.Link::getFrontLink('contact#tabpanel3').'" class="link color-blue">',
  161.             '</a>',
  162.             $errorCode
  163.         );
  164.     }
  165.     /**
  166.      * Clean Url of <script> tags
  167.      * This method use HTMLPurifier lib from composer
  168.      *
  169.      * @param string $url
  170.      */
  171.     public static function htmlPurifier($url): string
  172.     {
  173.         $config HTMLPurifier_Config::createDefault();
  174.         $config->set('Cache.DefinitionImpl'null);
  175.         $purifier = new HTMLPurifier($config);
  176.         $newParam $purifier->purify($url); // this convert
  177.         $searchParams = ['&lt;script&gt;''&lt;/script&gt;'];
  178.         $replaceParams '';
  179.         $newParam str_ireplace($searchParams$replaceParams$newParam);
  180.         $newParam trim($newParam);
  181.         return $newParam;
  182.     }
  183.     /**
  184.      * @template T
  185.      *
  186.      * @param class-string<T> $serviceFqn
  187.      *
  188.      * @return T
  189.      */
  190.     public static function getSfService(string $serviceFqn)
  191.     {
  192.         /** @var ContainerInterface $container */
  193.         $container $GLOBALS['kernel']->getContainer();
  194.         return $container->get($serviceFqn);
  195.     }
  196.     public static function getSfParameter(string $parameterName)
  197.     {
  198.         /** @var ContainerInterface $container */
  199.         $container $GLOBALS['kernel']->getContainer();
  200.         return $container->getParameter($parameterName);
  201.     }
  202.     /**
  203.      * @throws Exception
  204.      */
  205.     public static function getExpeditionDateTextForAvailableStock(?int $customerId$supplierId$languageId): string
  206.     {
  207.         $expeditionTimestamp self::getExpeditionTimestampByDefault($customerId$supplierId);
  208.         /** @var \App\Application\Service\Helper\ShippingDateService $shippingDateService */
  209.         $shippingDateService self::getSfService(ShippingDateService::class);
  210.         return $shippingDateService->getBuyItNowAndWeSendItOnDateText($expeditionTimestampfalse$languageId);
  211.     }
  212.     /**
  213.      * @throws Exception
  214.      */
  215.     public static function getExpeditionDateTextForSupplierStock($customerId$idSupplier$dateToCompare$stockSupplier_3_5$languageId): string
  216.     {
  217.         $expeditionTimestamp self::getExpeditionTimestampBySupplier($customerId$idSupplier$dateToCompare$stockSupplier_3_5);
  218.         $shippingDateService self::getSfService(ShippingDateService::class);
  219.         return $shippingDateService->getBuyItNowAndWeSendItOnDateText($expeditionTimestampfalse$languageId);
  220.     }
  221.     /**
  222.      * @param string $data
  223.      *
  224.      * @return string|null
  225.      */
  226.     public static function cleanNonPrintableCharacters(string $data): ?string
  227.     {
  228.         return preg_replace('/[[:^print:]]/'''$data);
  229.     }
  230.     /**
  231.      * @deprecated use Symfony translator instead
  232.      */
  233.     public static function trans(string $key, ?array $parameters = [], ?string $domain 'messages'): ?string
  234.     {
  235.         return Tools::getSfService('translator')->trans($key$parameters$domain);
  236.     }
  237.     /**
  238.      * @param int $tmpExpeditionTimestamp
  239.      * @param int $customerId
  240.      * @return float|int
  241.      */
  242.     protected static function addCustomerSpecificDelay(int $tmpExpeditionTimestampint $customerId): int
  243.     {
  244.         // If customer order confirmation delay exists, add to the timestamp
  245.         $customerManager self::getSfService(CustomerManager::class);
  246.         $customer $customerManager->findOneById($customerId);
  247.         if (!empty($customer->getOrderConfirmationDelay())) {
  248.             $tmpExpeditionTimestamp $tmpExpeditionTimestamp + ($customer->getOrderConfirmationDelay() * DatetimeHelper::SECONDS_AN_HOUR);
  249.         }
  250.         return $tmpExpeditionTimestamp;
  251.     }
  252. }