vendor/lm/engine-inventory-matrix/src/InventoryMatrix.php line 120

Open in your IDE?
  1. <?php
  2. namespace Lm\Engine\InventoryMatrix;
  3. use Lm\Engine\EC\Entity\GoodsWithRelated;
  4. use Lm\Engine\InventoryMatrix\Service\InventoryMatrixService;
  5. use Lm\Engine\InventoryMatrix\Models\GoodsSetPurchaseModel;
  6. use Lm\Engine\SokujitsuHassou\SokujitsuHassou;
  7. use Twig\Environment;
  8. use Twig\Loader\FilesystemLoader;
  9. class InventoryMatrix
  10. {
  11.     /**
  12.      * Format stock arrival date to a human-readable format
  13.      *
  14.      * @param string $date 入荷予定日 (YYYY-MM-DD 形式)
  15.      * @return string 変換された入荷予定日(例: "3月上旬")
  16.      * @throws \InvalidArgumentException 無効な日付形式の場合に例外をスロー
  17.      */
  18.     public function formatStockArrivalDate(string $date)
  19.     {
  20.         // YYYY-MM-DD 形式かどうかを確認
  21.         if (!preg_match('/^\d{4}-\d{2}-\d{2}$/'$date)) {
  22.             throw new \InvalidArgumentException('無効な日付形式です。YYYY-MM-DD 形式で入力してください。');
  23.         }
  24.         $timestamp strtotime($date);
  25.         if ($timestamp === false) {
  26.             throw new \InvalidArgumentException('無効な日付です。');
  27.         }
  28.         $month = (int)date('n'$timestamp);
  29.         $day = (int)date('j'$timestamp);
  30.         if ($day >= && $day <= 10) {
  31.             return "{$month}月上旬〜順次";
  32.         } elseif ($day >= 11 && $day <= 20) {
  33.             return "{$month}月中旬〜順次";
  34.         } else {
  35.             return "{$month}月下旬〜順次";
  36.         }
  37.     }
  38.     /**
  39.      * "上旬/中旬/下旬" だけを返却
  40.      *
  41.      * @param string $date
  42.      * @return string|null
  43.      */
  44.     private function formatStockArrivalPeriodOnly($date)
  45.     {
  46.         if (!is_string($date) || !preg_match('/^\d{4}-\d{2}-\d{2}$/'$date)) {
  47.             return null;
  48.         }
  49.         $timestamp strtotime($date);
  50.         if ($timestamp === false) {
  51.             return null;
  52.         }
  53.         $month = (int)date('n'$timestamp);
  54.         $day = (int)date('j'$timestamp);
  55.         if ($day >= && $day <= 10) {
  56.             return "{$month}月上旬";
  57.         } elseif ($day >= 11 && $day <= 20) {
  58.             return "{$month}月中旬";
  59.         }
  60.         return "{$month}月下旬";
  61.     }
  62.     /**
  63.      * @param string $date
  64.      * @return int|null 1=上旬, 2=中旬, 3=下旬
  65.      */
  66.     private function getJunBucket($date)
  67.     {
  68.         if (!is_string($date) || !preg_match('/^\d{4}-\d{2}-\d{2}$/'$date)) {
  69.             return null;
  70.         }
  71.         $timestamp strtotime($date);
  72.         if ($timestamp === false) {
  73.             return null;
  74.         }
  75.         $day = (int)date('j'$timestamp);
  76.         if ($day <= 10) {
  77.             return 1;
  78.         }
  79.         if ($day <= 20) {
  80.             return 2;
  81.         }
  82.         return 3;
  83.     }
  84.     /***
  85.      * Handle get Matrix Stock from GoodId
  86.      * @param GoodsWithRelated|int $goods
  87.      * @param bool|true $readOnly
  88.      * @param array|null &$color
  89.      * @param array|null &$size
  90.      * @return null|string
  91.      * @throws \Twig\Error\LoaderError
  92.      * @throws \Twig\Error\RuntimeError
  93.      * @throws \Twig\Error\SyntaxError
  94.      */
  95.     public function getMatrixStock($goods$readOnly true, &$color null, &$size null)
  96.     {
  97.         /**
  98.          * @var GoodsWithRelated $goods
  99.          */
  100.         $goods GoodsWithRelated::factory($goods);
  101.         $goodsId $goods->getGoodsId();
  102.         //
  103.         $model = new GoodsSetPurchaseModel();
  104.         $inventoryMatrixService = new InventoryMatrixService();
  105.         list($jan$janColor) = $inventoryMatrixService->getGoodsJanById($goodsId);
  106.         $color $inventoryMatrixService->getGoodsColorSelectSql($goodsId);
  107.         $size $inventoryMatrixService->getGoodsSizeListById($goodsId);
  108.         $sokujitsu $inventoryMatrixService->getGoodsSameDayShipping($goodsId1);
  109.         $yoyaku $inventoryMatrixService->getGoodsReservation($goodsId1);
  110.         $yoteibi $inventoryMatrixService->getGoodsStockDate($goodsId1);
  111.         list($yoteibiJanFrom$yoteibiJanTo) = $inventoryMatrixService->getGoodsJanStock3NyukaFromAndTo($goodsId);
  112.         $sellingColor $inventoryMatrixService->getSellingColorSql($goodsIdcount($color));
  113.         try {
  114.             $stock $inventoryMatrixService->getGoodsStock($goodsId1);
  115.         } catch (\Exception $e) {
  116.             // throw new NotFoundHttpException('お探しの商品が見つかりませんでした', $e);
  117.             return null;
  118.         }
  119.         $matrixData = [];
  120.         $matrixData_cnt = [];
  121.         $line 0;
  122.         $sizeMax 8;
  123.         $max 0;
  124.         foreach ($color as $v1) {
  125.             //
  126.             if (!isset($yoyaku[$v1["gcl_id"]])) {
  127.                 continue;
  128.             }
  129.             $line++;
  130.             $line2 0;
  131.             $no 1;
  132.             foreach ($size as $v2) {
  133.                 if ($line2 % ($sizeMax) === 0) {
  134.                     $no++;
  135.                 } else {
  136.                     $max $line2 % ($sizeMax);
  137.                 }
  138.                 $tmpYoteibi NULL;
  139.                 $tmpYoteibiTo NULL;
  140.                 if (!empty($yoteibi[$v1["gcl_id"]][$v2["gp_id"]]) and $sokujitsu[$v1["gcl_id"]][$v2["gp_id"]] <= SokujitsuHassou::FLG_NONE) {
  141.                     $tmpYoteibi $this->formatStockArrivalDate($yoteibi[$v1["gcl_id"]][$v2["gp_id"]]);
  142.                     // 既存の$tmpYoteibiは触らず、表示用の from / to を別変数で組み立てる
  143.                     $fromDate = isset($yoteibiJanFrom[$v1["gcl_id"]][$v2["gp_id"]]) ? $yoteibiJanFrom[$v1["gcl_id"]][$v2["gp_id"]] : null;
  144.                     if (empty($fromDate)) {
  145.                         $fromDate $yoteibi[$v1["gcl_id"]][$v2["gp_id"]];
  146.                     }
  147.                     $tmpYoteibiTo '順次';
  148.                     $toDate = isset($yoteibiJanTo[$v1["gcl_id"]][$v2["gp_id"]]) ? $yoteibiJanTo[$v1["gcl_id"]][$v2["gp_id"]] : null;
  149.                     $toLabel $this->formatStockArrivalPeriodOnly($toDate);
  150.                     // TOが有効で、FROMより後、かつ同一旬でないときだけ "From〜To"
  151.                     if (!empty($toLabel)) {
  152.                         $fromTs strtotime($fromDate);
  153.                         $toTs strtotime($toDate);
  154.                         $fromJun $this->getJunBucket($fromDate);
  155.                         $toJun $this->getJunBucket($toDate);
  156.                         $sameYearMonth = (date('Y-n'$fromTs) === date('Y-n'$toTs));
  157.                         if ($fromTs !== false && $toTs !== false && $toTs >= $fromTs && !($sameYearMonth && $fromJun !== null && $fromJun === $toJun)) {
  158.                             $tmpYoteibiTo $toLabel;
  159.                         }
  160.                     }
  161.                 }
  162.                 $children = [];
  163.                 foreach (['main''sub''other'] as $type)
  164.                     if (!empty($v1["{$type}_gcl_id"]) && !empty($v2["{$type}_gp_id"])) $children[] = [
  165.                         'gclId' => $v1["{$type}_gcl_id"],
  166.                         'gpId' => $v2["{$type}_gp_id"],
  167.                     ];
  168.                 $matrixData_cnt[$no] = $max;
  169.                 $matrixData[$no][$line][$line2] = [
  170.                     'line' => $line,
  171.                     'product_id' => $jan[$v1["gcl_id"]][$v2["gp_id"]],
  172.                     'shiire_color' => $janColor[$v1["gcl_id"]][$v2["gp_id"]],
  173.                     'color' => $v1,
  174.                     'size' => $v2,
  175.                     'yoyaku' => $yoyaku[$v1["gcl_id"]][$v2["gp_id"]],
  176.                     'yoteibi' => $tmpYoteibi,
  177.                     'yoteibi_to' => $tmpYoteibiTo,
  178.                     'stock' => $stock[$v1["gcl_id"]][$v2["gp_id"]],
  179.                     'sokujitsu' => $sokujitsu[$v1["gcl_id"]][$v2["gp_id"]],
  180.                     'children' => $children,
  181.                 ];
  182.                 $line2++;
  183.             }
  184.         }
  185.         // Get goods set purchase
  186.         $getAsGoodsSetPurchase $model->getAsGoodsSetPurchaseById($goodsId);
  187.         $mainGspName '';
  188.         if ($getAsGoodsSetPurchase) {
  189.             $flattened = [];
  190.             foreach ($getAsGoodsSetPurchase as $goods) {
  191.                 if ($type GoodsSetPurchaseModel::GOODS_SET_PURCHASE_TYPE_LIST[$goods['gsp_type']])
  192.                     foreach ($goods as $key => $value) {
  193.                         $flattened["{$type['name']}_{$key}"] = $value;
  194.                     }
  195.             }
  196.             if ($flattened) {
  197.                 $mainGspName $flattened['main_gsp_name'];
  198.             }
  199.         }
  200.         $viewsPath realpath(__DIR__) . '/View/';
  201.         $loader = new FilesystemLoader($viewsPath);
  202.         $twig = new Environment($loader);
  203.         $template $twig->load('matrix_stock.twig');
  204.         $html $template->render([
  205.             'matrixData' => $matrixData,
  206.             'goodsId' => $goodsId,
  207.             'sellingColor' => $sellingColor,
  208.             'readOnly' => $readOnly,
  209.             'getAsGoodsSetPurchase' => $getAsGoodsSetPurchase,
  210.             'mainGspName' => $mainGspName
  211.         ]);
  212.         return $html;
  213.     }
  214. }