<?php
namespace Lm\Engine\InventoryMatrix;
use Lm\Engine\EC\Entity\GoodsWithRelated;
use Lm\Engine\InventoryMatrix\Service\InventoryMatrixService;
use Lm\Engine\InventoryMatrix\Models\GoodsSetPurchaseModel;
use Lm\Engine\SokujitsuHassou\SokujitsuHassou;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class InventoryMatrix
{
/**
* Format stock arrival date to a human-readable format
*
* @param string $date 入荷予定日 (YYYY-MM-DD 形式)
* @return string 変換された入荷予定日(例: "3月上旬")
* @throws \InvalidArgumentException 無効な日付形式の場合に例外をスロー
*/
public function formatStockArrivalDate(string $date)
{
// YYYY-MM-DD 形式かどうかを確認
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
throw new \InvalidArgumentException('無効な日付形式です。YYYY-MM-DD 形式で入力してください。');
}
$timestamp = strtotime($date);
if ($timestamp === false) {
throw new \InvalidArgumentException('無効な日付です。');
}
$month = (int)date('n', $timestamp);
$day = (int)date('j', $timestamp);
if ($day >= 1 && $day <= 10) {
return "{$month}月上旬〜順次";
} elseif ($day >= 11 && $day <= 20) {
return "{$month}月中旬〜順次";
} else {
return "{$month}月下旬〜順次";
}
}
/***
* Handle get Matrix Stock from GoodId
* @param GoodsWithRelated|int $goods
* @param bool|true $readOnly
* @param array|null &$color
* @param array|null &$size
* @return null|string
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public function getMatrixStock($goods, $readOnly = true, &$color = null, &$size = null)
{
/**
* @var GoodsWithRelated $goods
*/
$goods = GoodsWithRelated::factory($goods);
$goodsId = $goods->getGoodsId();
//
$model = new GoodsSetPurchaseModel();
$inventoryMatrixService = new InventoryMatrixService();
list($jan, $janColor) = $inventoryMatrixService->getGoodsJanById($goodsId);
$color = $inventoryMatrixService->getGoodsColorSelectSql($goodsId);
$size = $inventoryMatrixService->getGoodsSizeListById($goodsId);
$sokujitsu = $inventoryMatrixService->getGoodsSameDayShipping($goodsId, 1);
$yoyaku = $inventoryMatrixService->getGoodsReservation($goodsId, 1);
$yoteibi = $inventoryMatrixService->getGoodsStockDate($goodsId, 1);
$sellingColor = $inventoryMatrixService->getSellingColorSql($goodsId, count($color));
try {
$stock = $inventoryMatrixService->getGoodsStock($goodsId, 1);
} catch (\Exception $e) {
// throw new NotFoundHttpException('お探しの商品が見つかりませんでした', $e);
return null;
}
$matrixData = [];
$matrixData_cnt = [];
$line = 0;
$sizeMax = 8;
$max = 0;
foreach ($color as $v1) {
//
if (!isset($yoyaku[$v1["gcl_id"]])) {
continue;
}
$line++;
$line2 = 0;
$no = 1;
foreach ($size as $v2) {
if ($line2 % ($sizeMax) === 0) {
$no++;
} else {
$max = $line2 % ($sizeMax);
}
$tmpYoteibi = NULL;
if (!empty($yoteibi[$v1["gcl_id"]][$v2["gp_id"]]) and $sokujitsu[$v1["gcl_id"]][$v2["gp_id"]] <= SokujitsuHassou::FLG_NONE) {
$tmpYoteibi = $this->formatStockArrivalDate($yoteibi[$v1["gcl_id"]][$v2["gp_id"]]);
}
$children = [];
foreach (['main', 'sub', 'other'] as $type)
if (!empty($v1["{$type}_gcl_id"]) && !empty($v2["{$type}_gp_id"])) $children[] = [
'gclId' => $v1["{$type}_gcl_id"],
'gpId' => $v2["{$type}_gp_id"],
];
$matrixData_cnt[$no] = $max;
$matrixData[$no][$line][$line2] = [
'line' => $line,
'product_id' => $jan[$v1["gcl_id"]][$v2["gp_id"]],
'shiire_color' => $janColor[$v1["gcl_id"]][$v2["gp_id"]],
'color' => $v1,
'size' => $v2,
'yoyaku' => $yoyaku[$v1["gcl_id"]][$v2["gp_id"]],
'yoteibi' => $tmpYoteibi,
'stock' => $stock[$v1["gcl_id"]][$v2["gp_id"]],
'sokujitsu' => $sokujitsu[$v1["gcl_id"]][$v2["gp_id"]],
'children' => $children,
];
$line2++;
}
}
// Get goods set purchase
$getAsGoodsSetPurchase = $model->getAsGoodsSetPurchaseById($goodsId);
$mainGspName = '';
if ($getAsGoodsSetPurchase) {
$flattened = [];
foreach ($getAsGoodsSetPurchase as $goods) {
if ($type = GoodsSetPurchaseModel::GOODS_SET_PURCHASE_TYPE_LIST[$goods['gsp_type']])
foreach ($goods as $key => $value) {
$flattened["{$type['name']}_{$key}"] = $value;
}
}
if ($flattened) {
$mainGspName = $flattened['main_gsp_name'];
}
}
$viewsPath = realpath(__DIR__) . '/View/';
$loader = new FilesystemLoader($viewsPath);
$twig = new Environment($loader);
$template = $twig->load('matrix_stock.twig');
$html = $template->render([
'matrixData' => $matrixData,
'goodsId' => $goodsId,
'sellingColor' => $sellingColor,
'readOnly' => $readOnly,
'getAsGoodsSetPurchase' => $getAsGoodsSetPurchase,
'mainGspName' => $mainGspName
]);
return $html;
}
}