2017-12-20 13:44:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This Software is the property of Data Development and is protected
|
|
|
|
* by copyright law - it is NOT Freeware.
|
|
|
|
*
|
|
|
|
* Any unauthorized use of this software without a valid license
|
|
|
|
* is a violation of the license agreement and will be prosecuted by
|
|
|
|
* civil and criminal law.
|
|
|
|
*
|
|
|
|
* http://www.shopmodule.com
|
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
|
|
|
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
|
|
|
* @link http://www.oxidmodule.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace D3\Ordermanager\Modules\Application\Model;
|
|
|
|
|
2019-05-15 11:31:56 +02:00
|
|
|
use Exception;
|
|
|
|
use OxidEsales\Eshop\Application\Model\Basket;
|
2019-07-09 10:28:15 +02:00
|
|
|
use OxidEsales\Eshop\Application\Model\BasketReservation;
|
2017-12-20 13:44:53 +01:00
|
|
|
use OxidEsales\Eshop\Application\Model\OrderArticle;
|
|
|
|
use OxidEsales\Eshop\Application\Model\Order;
|
|
|
|
use OxidEsales\Eshop\Application\Model\BasketItem;
|
|
|
|
use OxidEsales\Eshop\Application\Model\DiscountList;
|
|
|
|
use OxidEsales\Eshop\Application\Model\Discount;
|
2019-05-15 11:31:56 +02:00
|
|
|
use OxidEsales\Eshop\Core\Config;
|
2018-02-20 09:37:52 +01:00
|
|
|
use OxidEsales\Eshop\Core\Exception\ArticleException;
|
|
|
|
use OxidEsales\Eshop\Core\Exception\ArticleInputException;
|
2017-12-20 13:44:53 +01:00
|
|
|
use OxidEsales\Eshop\Core\Price;
|
|
|
|
use OxidEsales\Eshop\Core\PriceList;
|
2019-05-15 11:31:56 +02:00
|
|
|
use OxidEsales\Eshop\Core\Session;
|
|
|
|
use stdClass;
|
2017-12-20 13:44:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class d3_oxbasket_ordermanager
|
|
|
|
* @package D3\Ordermanager\Modules\Models
|
2019-05-15 11:31:56 +02:00
|
|
|
* @mixin Basket
|
2017-12-20 13:44:53 +01:00
|
|
|
*/
|
|
|
|
class d3_oxbasket_ordermanager extends d3_oxbasket_ordermanager_parent
|
|
|
|
{
|
2019-05-15 11:31:56 +02:00
|
|
|
/**
|
|
|
|
* @return d3_oxbasketitem_ordermanager
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function getBasketItemInstance()
|
|
|
|
{
|
|
|
|
return d3GetModCfgDIC()->get('d3ox.ordermanager.'.BasketItem::class);
|
|
|
|
}
|
|
|
|
|
2017-12-20 13:44:53 +01:00
|
|
|
/**
|
|
|
|
* Adds order article to basket (method normally used while recalculating order)
|
|
|
|
*
|
|
|
|
* @param OrderArticle $oOrderArticle order article to store in basket
|
2019-05-15 11:31:56 +02:00
|
|
|
* @throws Exception
|
2017-12-20 13:44:53 +01:00
|
|
|
*/
|
|
|
|
public function addOrderArticleToBasket4OrderManager($oOrderArticle)
|
|
|
|
{
|
|
|
|
// adding only if amount > 0
|
|
|
|
if ($oOrderArticle->getFieldData('oxamount') > 0) {
|
|
|
|
$sItemId = $oOrderArticle->getId();
|
|
|
|
//inserting new
|
2019-05-15 11:31:56 +02:00
|
|
|
$oBasketItem = $this->getBasketItemInstance();
|
2017-12-20 13:44:53 +01:00
|
|
|
$oBasketItem->setStockCheckStatus(false);
|
|
|
|
$oBasketItem->initFromOrderArticle($oOrderArticle);
|
|
|
|
$oBasketItem->setPrice($oOrderArticle->getPrice());
|
|
|
|
$oBasketItem->setWrapping($oOrderArticle->getFieldData('oxwrapid'));
|
|
|
|
$oBasketItem->setBundle($oOrderArticle->isBundle());
|
|
|
|
$oBasketItem->d3ChangeOrderArticle2RealArticle();
|
|
|
|
|
|
|
|
$this->_aBasketContents[$sItemId] = $oBasketItem;
|
|
|
|
|
|
|
|
//calling update method
|
|
|
|
$this->onUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 11:31:56 +02:00
|
|
|
/**
|
|
|
|
* @return Config
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function d3GetConfig()
|
|
|
|
{
|
|
|
|
return d3GetModCfgDIC()->get('d3ox.ordermanager.'.Config::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Session
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function d3GetSession()
|
|
|
|
{
|
|
|
|
return d3GetModCfgDIC()->get('d3ox.ordermanager.'.Session::class);
|
|
|
|
}
|
|
|
|
|
2017-12-20 13:44:53 +01:00
|
|
|
/**
|
|
|
|
* @param bool $blForceUpdate
|
|
|
|
* @param $oOrder
|
2018-02-20 09:37:52 +01:00
|
|
|
* @throws ArticleException
|
|
|
|
* @throws ArticleInputException
|
2019-05-15 11:31:56 +02:00
|
|
|
* @throws Exception
|
2017-12-20 13:44:53 +01:00
|
|
|
*/
|
2019-05-15 11:31:56 +02:00
|
|
|
public function calculateBasket4OrderManager($blForceUpdate, $oOrder)
|
2017-12-20 13:44:53 +01:00
|
|
|
{
|
|
|
|
if (!$this->isEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false == $this->_blUpdateNeeded && false == $blForceUpdate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_aCosts = array();
|
|
|
|
|
2019-05-15 11:31:56 +02:00
|
|
|
$this->_oPrice = $this->d3GetPrice();
|
2017-12-20 13:44:53 +01:00
|
|
|
$this->_oPrice->setBruttoPriceMode();
|
|
|
|
|
|
|
|
// 1. saving basket to the database
|
|
|
|
$this->_save();
|
|
|
|
|
|
|
|
// 2. remove all bundles
|
|
|
|
$this->_clearBundles();
|
|
|
|
|
|
|
|
// 3. generate bundle items
|
|
|
|
$this->_addBundles();
|
|
|
|
|
|
|
|
// reserve active basket
|
2019-05-15 11:31:56 +02:00
|
|
|
if ($this->d3GetConfig()->getConfigParam('blPsBasketReservationEnabled')) {
|
2019-07-09 10:28:15 +02:00
|
|
|
/** @var BasketReservation $oBasketReservation */
|
|
|
|
$oBasketReservation = $this->d3GetSession()->getBasketReservations();
|
|
|
|
$oBasketReservation->reserveBasket($this);
|
2017-12-20 13:44:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 4. calculating item prices
|
|
|
|
$this->_calcItemsPrice4OrderManager();
|
|
|
|
|
|
|
|
// 5. calculating/applying discounts
|
|
|
|
$this->_calcBasketDiscount4OrderManager($oOrder);
|
|
|
|
|
|
|
|
// 6. calculating basket total discount
|
|
|
|
$this->_calcBasketTotalDiscount();
|
|
|
|
|
|
|
|
// 7. check for vouchers
|
|
|
|
$this->_calcVoucherDiscount();
|
|
|
|
|
|
|
|
// 8. applies all discounts to pricelist
|
|
|
|
$this->_applyDiscounts();
|
|
|
|
|
|
|
|
// 9. calculating additional costs:
|
|
|
|
// 9.1: delivery
|
|
|
|
$this->setCost('oxdelivery', $this->_calcDeliveryCost());
|
|
|
|
|
|
|
|
// 9.2: adding wrapping costs
|
|
|
|
$this->setCost('oxwrapping', $this->_calcBasketWrapping());
|
|
|
|
|
|
|
|
// 9.3: adding payment cost
|
|
|
|
$this->setCost('oxpayment', $this->_calcPaymentCost4OrderManager($oOrder));
|
|
|
|
|
|
|
|
// 10. calculate total price
|
|
|
|
$this->_calcTotalPrice();
|
|
|
|
|
|
|
|
// 11. formating discounts
|
|
|
|
$this->formatDiscount();
|
|
|
|
|
|
|
|
// 12.setting to up-to-date status
|
|
|
|
$this->afterUpdate();
|
|
|
|
}
|
|
|
|
|
2018-02-20 09:37:52 +01:00
|
|
|
/**
|
2019-05-15 11:31:56 +02:00
|
|
|
* @return PriceList
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function d3getPriceList()
|
|
|
|
{
|
|
|
|
return d3GetModCfgDIC()->get('d3ox.ordermanager.'.PriceList::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return DiscountList
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function d3GetDiscountList()
|
|
|
|
{
|
|
|
|
return d3GetModCfgDIC()->get('d3ox.ordermanager.'.DiscountList::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception
|
2018-02-20 09:37:52 +01:00
|
|
|
*/
|
2017-12-20 13:44:53 +01:00
|
|
|
protected function _calcItemsPrice4OrderManager()
|
|
|
|
{
|
|
|
|
// resetting
|
|
|
|
$this->setSkipDiscounts(false);
|
|
|
|
|
|
|
|
// resetting
|
|
|
|
$this->_aItemDiscounts = array();
|
|
|
|
|
2019-05-15 11:31:56 +02:00
|
|
|
$this->_oProductsPriceList = $this->d3getPriceList();
|
|
|
|
$this->_oDiscountProductsPriceList = $this->d3getPriceList();
|
|
|
|
$this->_oNotDiscountedProductsPriceList = $this->d3getPriceList();
|
2017-12-20 13:44:53 +01:00
|
|
|
|
2019-05-15 11:31:56 +02:00
|
|
|
$oDiscountList = $this->d3GetDiscountList();
|
2017-12-20 13:44:53 +01:00
|
|
|
|
|
|
|
/** @var $oBasketItem BasketItem */
|
|
|
|
foreach ($this->_aBasketContents as $oBasketItem) {
|
|
|
|
if (!$oBasketItem->isDiscountArticle() && ( $oArticle = $oBasketItem->getArticle())) {
|
|
|
|
$oBasketPrice = $oBasketItem->getUnitPrice();
|
|
|
|
$oBasketItem->setRegularUnitPrice(clone $oBasketPrice);
|
|
|
|
$oBasketItem->setPrice($oBasketPrice);
|
|
|
|
//P adding product price
|
|
|
|
$this->_oProductsPriceList->addToPriceList($oBasketItem->getPrice());
|
|
|
|
|
|
|
|
$oBasketPrice->setBruttoPriceMode();
|
|
|
|
if (!$oArticle->skipDiscounts() && $this->canCalcDiscounts()) {
|
|
|
|
// apply basket type discounts for item
|
|
|
|
$aDiscounts = $oDiscountList->getBasketItemDiscounts($oArticle, $this, $this->getBasketUser());
|
|
|
|
reset($aDiscounts);
|
|
|
|
/** @var Discount $oDiscount */
|
|
|
|
foreach ($aDiscounts as $oDiscount) {
|
|
|
|
$oBasketPrice->setDiscount($oDiscount->getAddSum(), $oDiscount->getAddSumType());
|
|
|
|
}
|
|
|
|
$oBasketPrice->calculateDiscount();
|
|
|
|
} else {
|
|
|
|
$oBasketItem->setSkipDiscounts(true);
|
|
|
|
$this->setSkipDiscounts(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$oBasketPrice->multiply($oBasketItem->getAmount());
|
|
|
|
|
|
|
|
//P collect discount values for basket items which are discountable
|
|
|
|
if (!$oArticle->skipDiscounts()) {
|
|
|
|
$this->_oDiscountProductsPriceList->addToPriceList($oBasketPrice);
|
|
|
|
$this->_aDiscountedVats[$oBasketPrice->getVat()] += $oBasketPrice->getVatValue();
|
|
|
|
} else {
|
|
|
|
$this->_oNotDiscountedProductsPriceList->addToPriceList($oBasketPrice);
|
|
|
|
$oBasketItem->setSkipDiscounts(true);
|
|
|
|
$this->setSkipDiscounts(true);
|
|
|
|
}
|
|
|
|
} elseif ($oBasketItem->isBundle()) {
|
|
|
|
// if bundles price is set to zero
|
2019-05-15 11:31:56 +02:00
|
|
|
$oPrice = $this->d3GetPrice();
|
2017-12-20 13:44:53 +01:00
|
|
|
$oBasketItem->setPrice($oPrice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 11:31:56 +02:00
|
|
|
/**
|
|
|
|
* @return Price
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function d3GetPrice()
|
|
|
|
{
|
|
|
|
return d3GetModCfgDIC()->get('d3ox.ordermanager.'.Price::class);
|
|
|
|
}
|
|
|
|
|
2017-12-20 13:44:53 +01:00
|
|
|
/**
|
|
|
|
* @param Order $oOrder
|
|
|
|
* @return Price
|
2019-05-15 11:31:56 +02:00
|
|
|
* @throws Exception
|
2017-12-20 13:44:53 +01:00
|
|
|
*/
|
|
|
|
protected function _calcPaymentCost4OrderManager($oOrder)
|
|
|
|
{
|
2019-05-15 11:31:56 +02:00
|
|
|
$oPaymentPrice = $this->d3GetPrice();
|
2017-12-20 13:44:53 +01:00
|
|
|
$oPaymentPrice->setBruttoPriceMode();
|
|
|
|
$oPaymentPrice->setPrice($oOrder->getFieldData('oxpaycost'));
|
|
|
|
|
|
|
|
return $oPaymentPrice;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Order $oOrder
|
|
|
|
*/
|
|
|
|
protected function _calcBasketDiscount4OrderManager($oOrder)
|
|
|
|
{
|
2019-05-15 11:31:56 +02:00
|
|
|
$oDiscount = new stdClass();
|
2017-12-20 13:44:53 +01:00
|
|
|
$oDiscount->sOXID = 'stdDiscount';
|
|
|
|
$oDiscount->sDiscount = 'RabattText';
|
|
|
|
$oDiscount->sType = 'abs';
|
|
|
|
$oDiscount->dDiscount = $oOrder->getFieldData('oxdiscount');
|
|
|
|
$oDiscount->fDiscount = $oOrder->getFieldData('oxdiscount');
|
|
|
|
|
|
|
|
$this->_aDiscounts = array($oDiscount);
|
|
|
|
|
|
|
|
if (false == is_array($this->_aDiscountedVats)) {
|
|
|
|
$this->_aDiscountedVats = array(
|
|
|
|
'0' => 0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 11:31:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function d3GetDiscounts()
|
|
|
|
{
|
|
|
|
return $this->_aDiscounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function d3GetDiscountedVats()
|
|
|
|
{
|
|
|
|
return $this->_aDiscountedVats;
|
|
|
|
}
|
2017-12-20 13:44:53 +01:00
|
|
|
}
|