This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,173 @@
<?php
namespace Stemcell\Checkout\Model\Cart\Totals;
use Magento\Framework\Api\AttributeValueFactory;
use Stemcell\Checkout\Api\Data\Quote\TotalsItemInterface;
use \Magento\Quote\Model\Quote\ItemFactory as QuoteItemFactory;
use \Magento\Quote\Model\Quote\Item as QuoteItem;
use Stemcell\Customer\Api\Data\SalespersonInterface;
use Stemcell\Customer\Model\LabRepository;
class Item extends \Magento\Quote\Model\Cart\Totals\Item implements TotalsItemInterface
{
protected $item = null;
private SalespersonInterface $salespersonInterface;
private LabRepository $labRepository;
private QuoteItemFactory $itemFactory;
public function __construct(
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
\Magento\Framework\Api\AttributeValueFactory $attributeValueFactory,
QuoteItemFactory $itemFactory,
SalespersonInterface $salespersonInterface,
LabRepository $labRepository,
$data = []
) {
$this->itemFactory = $itemFactory;
$this->salespersonInterface = $salespersonInterface;
$this->labRepository = $labRepository;
parent::__construct($extensionFactory, $attributeValueFactory, $data);
}
/**
* @return QuoteItem
*/
protected function getQuoteItem()
{
if (!$this->item) {
$this->item = $this->itemFactory->create()->load($this->getItemId());
}
return $this->item;
}
/**
* @inheritDoc
*/
public function getSku()
{
return $this->_get(self::SKU);
}
/**
* @inheritDoc
*/
public function setSku(string $sku)
{
return $this->setData(self::SKU, $this->getQuoteItem()->getSku());
}
/**
* @inheritDoc
*/
public function getSalespersonName()
{
return $this->_get(self::SALESPERSON_NAME);
}
/**
* @inheritDoc
*/
public function setSalespersonName(string | null $salespersonName)
{
return $this->setData(self::SALESPERSON_NAME, $salespersonName);
}
/**
* @inheritDoc
*/
public function getLabBookingAccount()
{
return $this->_get(self::LAB_BOOKING_ACCOUNT);
}
/**
* @inheritDoc
*/
public function setLabBookingAccount(string $labBookingAccount)
{
return $this->setData(self::LAB_BOOKING_ACCOUNT, $labBookingAccount);
}
/**
* @inheritDoc
*/
public function getCdCode()
{
return $this->_get(self::CD_CODE);
}
/**
* @inheritDoc
*/
public function setCdCode(string $cdCode)
{
return $this->setData(self::CD_CODE, $this->getQuoteItem()->getCd());
}
/**
* @inheritDoc
*/
public function getQuoteCode()
{
return $this->_get(self::QUOTE_CODE);
}
/**
* @inheritDoc
*/
public function setQuoteCode(string $quoteCode)
{
return $this->setData(self::QUOTE_CODE, $this->getQuoteItem()->getData('quote'));
}
/**
* @inheritDoc
*/
public function getPiSapId()
{
return $this->_get(self::PI_SAP_ID);
}
/**
* @inheritDoc
*/
public function setPiSapId(string $piSapId)
{
return $this->setData(self::PI_SAP_ID, $this->getQuoteItem()->getData(self::PI_SAP_ID));
}
/**
* @inheritDoc
*/
public function getCustomerDiscountPercent()
{
return $this->_get(self::CUSTOMER_DISCOUNT_PERCENT);
}
/**
* @inheritDoc
*/
public function setCustomerDiscountPercent(string $customerDiscountPercent)
{
return $this->setData(self::CUSTOMER_DISCOUNT_PERCENT, $customerDiscountPercent);
}
/**
* @inheritDoc
*/
public function getNote()
{
return $this->_get(self::NOTE);
}
/**
* @inheritDoc
*/
public function setNote(string $note)
{
return $this->setData(self::NOTE, $note);
}
}
@@ -0,0 +1,129 @@
<?php
namespace Stemcell\Checkout\Model\Cart\Totals;
use Magento\Catalog\Helper\Product\ConfigurationPool;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Api\ExtensibleDataInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\Exception\LocalizedException;
use Stemcell\Checkout\Api\Data\Quote\TotalsInterfaceFactory;
use Psr\Log\LoggerInterface;
use Stemcell\Customer\Api\SalespersonRepositoryInterface;
use Stemcell\Customer\Model\LabRepository;
class ItemConverter extends \Magento\Quote\Model\Cart\Totals\ItemConverter
{
private SalespersonRepositoryInterface $salespersonRepository;
private LabRepository $labRepository;
public function __construct(
private ConfigurationPool $configurationPool,
private EventManager $eventManager,
private \Stemcell\Checkout\Api\Data\Quote\TotalsItemInterfaceFactory $stemcelltotalsItemFactory,
private \Magento\Quote\Api\Data\TotalsItemInterfaceFactory $totalsItemFactory,
SalespersonRepositoryInterface $salespersonRepository,
LabRepository $labRepository,
private DataObjectHelper $dataObjectHelper,
private \Magento\Framework\Serialize\Serializer\Json $serializer
) {
parent::__construct($configurationPool, $eventManager, $totalsItemFactory, $dataObjectHelper, $serializer);
$this->salespersonRepository = $salespersonRepository;
$this->labRepository = $labRepository;
}
/**
* Retrieve formatted item options view
*
* @param \Magento\Quote\Api\Data\CartItemInterface $item
* @return string
*/
private function getFormattedOptionValue($item)
{
$optionsData = [];
/* @var $helper \Magento\Catalog\Helper\Product\Configuration */
$helper = $this->configurationPool->getByProductType('default');
$options = $this->configurationPool->getByProductType($item->getProductType())->getOptions($item);
foreach ($options as $index => $optionValue) {
$params = [
'max_length' => 55,
'cut_replacer' => ' <a href="#" class="dots tooltip toggle" onclick="return false">...</a>'
];
$option = $helper->getFormattedOptionValue($optionValue, $params);
$optionsData[$index] = $option;
$optionsData[$index]['label'] = $optionValue['label'];
}
return $this->serializer->serialize($optionsData);
}
protected function addAdditionalItemsData(\Magento\Quote\Model\Quote\Item $quoteItem)
{
$data = [];
$data['sku'] = $quoteItem->getSku();
if ($quoteItem->getSalespersonId()) {
$salesperson = $this->salespersonRepository->get($quoteItem->getSalespersonId());
$data['salesperson_name'] = $salesperson->getName();
} else {
$data['salesperson_name'] = '';
}
if ($quoteItem->getLabId()) {
try {
$lab = $this->labRepository->get($quoteItem->getLabId());
$data['lab_booking_account'] = $lab->getBookingAccount();
} catch (LocalizedException $e) {
}
}
$data['cd_code'] = $quoteItem->getCd();
$data['quote_code'] = $quoteItem->getData('quote');
$data['note'] = $quoteItem->getData('note');
$data['pi_sap_id'] = $quoteItem->getData('pi_sap_id');
if ($quoteItem->getData('customer_discount_percent')) {
$data['customer_discount_percent'] = number_format(
$quoteItem->getData('customer_discount_percent')*1.0,
2, '.', ''
).'%';
} else {
$data['customer_discount_percent'] = '';
}
if ($option = $quoteItem->getOptionByCode('simple_product')) {
$data['name'] = $option->getProduct()->getName();
}
return $data;
}
/**
* Converts a specified quote item model to a totals item data object.
*
* @param \Magento\Quote\Model\Quote\Item $item
* @return \Stemcell\Checkout\Api\Data\Quote\TotalsItemInterface
* @throws \Exception
*/
public function modelToDataObject($item)
{
$this->eventManager->dispatch('items_additional_data', ['item' => $item]);
$items = $item->toArray();
$items = array_merge($items, $this->addAdditionalItemsData($item));
$items['options'] = $this->getFormattedOptionValue($item);
// ObjectManager::getInstance()->get(LoggerInterface::class)->debug('modelToDataObject $items', $items);
unset($items[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
$itemsData = $this->stemcelltotalsItemFactory->create();
// $itemsData->setSalespersonName();
$itemsData->setItemId($item->getEntityId());
$this->dataObjectHelper->populateWithArray(
$itemsData,
$items,
\Stemcell\Checkout\Api\Data\Quote\TotalsItemInterface::class
);
return $itemsData;
}
}