Newer
Older
* This file contains QUI\ERP\Order\Basket\BasketOrder
*/
namespace QUI\ERP\Order\Basket;
use QUI;
* This is a helper to represent an already send order for the order process
*
* @package QUI\ERP\Order\Basket
*/
class BasketOrder
{
/**
* List of products
*
protected ?QUI\Interfaces\Users\User $User = null;
protected ?QUI\ERP\Order\AbstractOrder $Order = null;
protected ?QUI\ERP\Comments $FrontendMessages = null;
* @param ?QUI\Interfaces\Users\User $User
*
* @throws Exception
* @throws QUI\Exception
*/
public function __construct(string $orderHash, null | QUI\Interfaces\Users\User $User = null)
{
if (!$User) {
$User = QUI::getUserBySession();
}
if (!QUI::getUsers()->isUser($User) || $User->getType() == QUI\Users\Nobody::class) {

Henning Leutz
committed
throw new Exception([
'quiqqer/order',
'exception.basket.not.found'

Henning Leutz
committed
], 404);
$this->User = $User;
$this->hash = $orderHash;
$this->FrontendMessages = new QUI\ERP\Comments();
$this->readOrder();
// get basket id
try {
$Basket = QUI\ERP\Order\Handler::getInstance()->getBasketByHash(
$this->User
);
$this->id = $Basket->getId();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
}
/**
* refresh the basket
* - import the order stuff to the basket
*/
{
try {
$this->readOrder();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
}
/**
* imports the order data into the basket
*
* @throws QUI\ERP\Exception
* @throws QUI\ERP\Order\Exception
* @throws QUI\Exception
*/
{
$this->Order = QUI\ERP\Order\Handler::getInstance()->getOrderByHash($this->hash);
$this->Order->refresh();
$data = $this->Order->getArticles()->toArray();
$priceFactors = $this->Order->getArticles()->getPriceFactors()->toArray();
$this->List = new ProductList();
$this->List->setOrder($this->Order);
$this->List->setCurrency($this->Order->getCurrency());
$this->List->getPriceFactors()->importList([
'end' => $priceFactors
]);
// note, import do a cleanup
$this->import($articles);
}
/**
* Return the order ID
*
public function getId(): ?int
return $this->id;
* This method clears only the order articles
* if you want to clear the order, you must use Order->clear()
$this->getOrder()->getArticles()->clear();
public function count(): int
{
return $this->List->count();
}
//region product handling
/**
* Return the product list
*
{
return $this->List;
}
/**
* Add a product to the basket
*
* @param Product $Product
public function addProduct(Product $Product): void
$Package = QUI::getPackage('quiqqer/order');
$Config = $Package->getConfig();
$merge = boolval($Config->getValue('orderProcess', 'mergeSameProducts'));
if (!$merge) {
$this->List->addProduct($Product);
if ($this->hasOrder()) {
$this->updateOrder();
$Products = $this->List->getProducts();
if (
!method_exists($Product, 'toArray')
|| !method_exists($Product, 'getQuantity')
|| !method_exists($P, 'toArray')
|| !method_exists($P, 'getQuantity')
|| !method_exists($P, 'setQuantity')
) {
continue;
}
$p1 = OrderProductUtils::getCompareProductArray($Product->toArray());
$p2 = OrderProductUtils::getCompareProductArray($P->toArray());
if ($p1 == $p2) {
$foundProduct = true;
$quantity = $P->getQuantity();
$quantity = $quantity + $Product->getQuantity();
$P->setQuantity($quantity);
break;
if ($foundProduct === false) {
$this->List->addProduct($Product);
}
if ($this->hasOrder()) {
$this->updateOrder();
/**
* Removes a position from the products
*
* @param integer $pos
*
* @throws QUI\ERP\Exception
* @throws QUI\ERP\Order\Exception
* @throws QUI\Exception
* @throws QUI\Permissions\Exception
*/
if (!$this->hasOrder()) {
return;
}
$this->getOrder()->removeArticle($pos);
//endregion
/**
* Import the products to the basket
*
* @param array $products
public function import(array $products = []): void

Henning Leutz
committed
$products = [];
$Package = QUI::getPackage('quiqqer/order');
$Config = $Package->getConfig();
$merge = boolval($Config->getValue('orderProcess', 'mergeSameProducts'));
$products = OrderProductUtils::getMergedProductList($products);
QUI::getEvents()->fireEvent(
'quiqqerOrderBasketToOrderBegin',
[$this, $this->Order, &$products]
);

Henning Leutz
committed
$this->List = QUI\ERP\Order\Utils\Utils::importProductsToBasketList(
$this->List,

Henning Leutz
committed
);
try {
QUI::getEvents()->fireEvent(
'quiqqerOrderBasketToOrder',
[$this, $this->Order, $this->List]
);
QUI::getEvents()->fireEvent(
'quiqqerOrderBasketToOrderEnd',
[$this, $this->Order, $this->List]
);

Henning Leutz
committed
$this->List->calc();
$this->save();
} catch (\Exception $Exception) {
QUI\System\Log::addDebug($Exception->getMessage());
QUI::getEvents()->fireEvent(
'quiqqerBasketImport',
[$this, $this->List]
);
* Save the basket -> order

Henning Leutz
committed
*
* @throws QUI\Exception
$this->updateOrder();

Henning Leutz
committed
/**
* @throws Exception
* @throws ExceptionBasketNotFound
* @throws QUI\Database\Exception
*/

Henning Leutz
committed
{
$data = $this->toArray();
$Basket = QUI\ERP\Order\Handler::getInstance()->getBasketFromUser($this->User);
$Basket->import($data['products']);
}
/**
* Return the basket as array
*
* @return array
*/
public function toArray(): array
{
$Products = $this->getProducts();
$products = $Products->getProducts();

Henning Leutz
committed
$fields = [];
foreach ($Product->getFields() as $Field) {
if (!$Field->isPublic()) {
continue;
}
if (!$Field->isCustomField()) {
continue;
}
$fields[$Field->getId()] = $Field->getValue();
}
'id' => $Product->getId(),
'quantity' => method_exists($Product, 'getQuantity') ? $Product->getQuantity() : 1,

Henning Leutz
committed
];
$result[] = $attributes;
// calc data
$calculations = [];
try {
$data = $Products->getFrontendView()->toArray();
unset($data['attributes']);
unset($data['products']);
$calculations = $data;
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}

Henning Leutz
committed
return [
'id' => $this->getId(),
'products' => $result,
'priceFactors' => $Products->getPriceFactors()->toArray(),
'calculations' => $calculations

Henning Leutz
committed
];
}
//region hash & orders
/**
* Set the process number
* - Vorgangsnummer
*
{
$this->hash = $hash;
}
/**
* Return the process number
*
{
return $this->hash;
}
/**
* Does the basket have an assigned order?
*
* @return bool
*/
public function hasOrder(): bool
{
return true;
}
/**
* Return the assigned order from the basket
*
public function getOrder(): ?QUI\ERP\Order\AbstractOrder

Henning Leutz
committed
/**
* placeholder for api

Henning Leutz
committed
*
* @throws QUI\Exception

Henning Leutz
committed
*/

Henning Leutz
committed
{
$this->Order->getArticles()->clear();
$products = $this->List->getProducts();
foreach ($products as $Product) {
try {
if (method_exists($Product, 'toArticle')) {
$this->Order->addArticle($Product->toArticle(null, false));
}
} catch (QUI\Users\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
}
$Articles = $this->Order->getArticles();
$OrderCurrency = $this->Order->getCurrency();
$PriceFactors = $this->List->getPriceFactors();
$PriceFactors->setCurrency($OrderCurrency);
$Articles->setCurrency($OrderCurrency);
$Articles->importPriceFactors($PriceFactors->toErpPriceFactorList());
$Articles->recalculate();

Henning Leutz
committed
}
/**
* @throws QUI\Exception
*/
{
$this->updateOrder();
}
//region frontend message
/**
* Add a frontend message
*
* @param string $message
*/
public function addFrontendMessage(string $message): void
{
$this->FrontendMessages->addComment($message);
}
/**
* Return the frontend message object
*
* @return null|QUI\ERP\Comments
*/
public function getFrontendMessages(): ?QUI\ERP\Comments
{
return $this->FrontendMessages;
}
/**
* Clears the messages and save this status to the database
*/
{
$this->FrontendMessages->clear();
}
//endregion