<?php
/**
* @version EC=CUBE4
* @copyright 株式会社 翔 kakeru.co.jp
* @author
* 2022年03月22日作成
*
* app/Customize/Service/CatalogService.php
*
*
* カタログサービス
* 2022/11/30
*
* C= C= C= ┌(;・_・)┘トコトコ
******************************************************/
namespace Customize\Service;
use Lm\Service\Db\SqlService;
use Customize\Service\CommonService;
use Customize\Service\CustomerService;
use Customize\Service\CartService;
use Lm\Entity\OrderHeader;
use Lm\Entity\Cart\CartHeader;
use Lm\Engine\SampleLimit\Catalog;
class CatalogService
{
const Table_limit = 'catalog_limit_table';
const Table_Catalog = 'catalog_item_table';
const Table_Category = 'category_table';
const Table_Goods = 'goods_table';
const DEVICE_NAME_TO_DEVICE_TYPE = [
'PC' => 1, // 1: パソコン
'SP' => 2, // 2: スマホ
'TB' => 3, // 3: タブレット
];
protected $CommonService;
protected $CustomerService;
protected $CartService;
public function __construct(
CommonService $CommonService
, CustomerService $CustomerService
, CartService $CartService
)
{
$this->CommonService = $CommonService;
$this->CustomerService = $CustomerService;
$this->CartService = $CartService;
}
/**
* カタログのリミットを取得する
*
* @return array $Limit
*/
public function GetLimit()
{
$Sql = new SqlService();
return $Sql->Table(self::Table_limit)
->Set('cl_device', $this->CommonService->GetDevice())
->Find();
}
public function getOrderCountOfToday($customerId)
{
$order = (new SqlService())
->Sql('
SELECT COUNT(*) AS order_count
FROM order_header_table
WHERE oh_customer = :customer_id
AND oh_catalog_id IS NOT NULL
AND oh_payment NOT IN (
:oh_payment_sample
)
AND oh_type NOT IN (
:oh_type_cancel
)
AND DATE(oh_date) = CURDATE()
')
->Params([
'customer_id' => $customerId,
'oh_payment_sample' => OrderHeader::PAYMENT_TYPE_SAMPLE,
'oh_type_cancel' => OrderHeader::TYPE_CANCEL,
])
->Fetch();
return !empty($order) ? $order['order_count'] : 0;
}
public function getOrderCountAfterPurchase($customerId)
{
$order = (new SqlService())
->Sql("
SELECT COUNT(oh_id) AS order_count
FROM order_header_table
WHERE oh_customer = :customer_id
AND oh_catalog_id IS NOT NULL
AND oh_payment IN (
:oh_payment_catalog
)
AND oh_type NOT IN (
:oh_type_cancel
)
AND DATE_FORMAT(oh_date, '%Y-%m-%d %H:%i:%s') > IFNULL(
(
SELECT DATE_FORMAT(oh_date, '%Y-%m-%d %H:%i:%s') AS latest_date
FROM order_header_table
WHERE oh_customer= :customer_id
AND oh_payment NOT IN (
:oh_payment_catalog
, :oh_payment_sample
)
AND oh_type NOT IN (
:oh_type_cancel
)
ORDER BY oh_date DESC LIMIT 1
),
'1970-01-01'
)
")
->Params([
'customer_id' => $customerId,
'oh_payment_catalog' => OrderHeader::PAYMENT_TYPE_CATALOG,
'oh_payment_sample' => OrderHeader::PAYMENT_TYPE_SAMPLE,
'oh_type_cancel' => OrderHeader::TYPE_CANCEL,
])
->Fetch();
return !empty($order) ? $order['order_count'] : 0;
}
/**
* カタログを取得する
*
* @param array param ['id',inids,'sihire']
* @return array $Limit
*/
public function GetCatalog($Param = null)
{
$Sql = new SqlService();
$LinkList = $this->CommonService->GetYaml('catalog_link_list.yaml');
$Sql->Table(self::Table_Catalog)
->Set('T.del_flg', 0)
->set('T.catalog_status', 1)
->Set('T.catalog_site_type', 'NULL')
->set('T.catalog_category', 'NOT NULL')
->Where('(T.iri * T.cases + T.hasu)>0');
if ($Id = $Param['id'] ?? null) {
return $Sql->Set('T.catalog_id', $Id)->find();
}
if ($Ids = $Param['InId'] ?? null) {
return $Sql->set('catalog_id', $Ids, 'IN')
->Order('catalog_category,catalog_display')
->FindAll();
}
if ($Shiire = $Param['shiire'] ?? null) {
return $Sql->set('shiiresaki_cd', $Shiire, 'IN')
->Order('catalog_category,catalog_display')
->FindAll();
}
$Datas = $Sql
->Order('T.catalog_category,catalog_display')
->FindAll();
$Re = [];
foreach ($Datas as $Data) {
$Data['link_list'][$Data['catalog_textlink_variation']] = $LinkList[$Data['catalog_textlink_variation']] ?? null;
$Data['link_list'][$Data['catalog_textlink_variation2']] = $LinkList[$Data['catalog_textlink_variation2']] ?? null;
$Data['link_list'][$Data['catalog_textlink_variation3']] = $LinkList[$Data['catalog_textlink_variation3']] ?? null;
$Re[$Data['catalog_category']][$Data['catalog_id']] = $Data;
}
return $Re;
}
public function CatalogValidation()
{
if (!in_array($this->CartService->getCartType(), [
CartService::CartTypeSample,
CartService::CartTypeCatalog,
])) {
return false;
}
$cartHeader = $this->createCardHeader();
$checkingResult = Catalog::checkCart($cartHeader);
if ($checkingResult['result'] == 0) {
return $checkingResult['message'];
}
return false;
}
/**
* 2022/04/21
* 選択したカタログDATAを取得する
*
* @return array()
*
*/
public function GetView()
{
if ($Ids = $this->CommonService->GetLmOrderOption(CommonService::Lm_Option_Catalog)){
return $this->GetCatalog(['InId' => $Ids]);
};
return [];
}
/**
* SHOPPING サンプル
*
*
*/
public function GetSampleCatalogs()
{
$Cart = $this->CartService->getCart();
$ProductIds = [];
foreach ($Cart->getItems() as $Item) {
if (!$Item->isProduct()) {
continue;
}
$Product = $Item->getProductClass()->getProduct();
$ProductIds[] = $Product->getId();
}
$Sql = new SqlService();
#商品の取得
$Goods = $Sql->Table(self::Table_Goods)
->Set('goods_id', $ProductIds, 'IN')
->FindAll();
$ShiireCd = [];
foreach ($Goods as $Good) {
$ShiireCd[] = $Good['goods_shiiresaki'];
}
return $this->GetCatalog(['shiire' => $ShiireCd]);
}
public function createCardHeader() {
$cartHeader = new CartHeader();
if ($Customer = $this->CommonService->getUser()) {
$customerId = $Customer->getLmCustomerId();
$cartHeader->setCustomerId($customerId);
}
$deviceName = $this->CommonService->GetDevice(true); // デバイス名(タブレットも判別するため
$device = $this->GetDeviceTypeByDeviceName($deviceName);
$cartHeader->setDevice($device);
return $cartHeader;
}
/**
* デバイス名よりデバイス種別を判別する
*
* @param int $deviceName
* @return string
*/
public function GetDeviceTypeByDeviceName($deviceName)
{
return self::DEVICE_NAME_TO_DEVICE_TYPE[$deviceName];
}
}