compute difference of reserved amounts vs basket items

@param \OxidEsales\Eshop\Application\Model\Basket $oBasket basket object

@return array
176332
protected function _basketDifference(\OxidEsales\Eshop\Application\Model\Basket $oBasket)
    {
        $aDiff = $this->_getReservedItems();
        // refreshing history
        foreach ($oBasket->getContents() as $oItem) {
            $sProdId = $oItem->getProductId();
            if (!isset($aDiff[$sProdId])) {
                $aDiff[$sProdId] = -$oItem->getAmount();
            } else {
                $aDiff[$sProdId] -= $oItem->getAmount();
            }
        }

        return $aDiff;
    }
46230
public function getPayPalBasketVatValue()
    {
        $basketVatValue = 0;
        $basketVatValue += $this->getPayPalProductVat();
        $basketVatValue += $this->getPayPalWrappingVat();
        $basketVatValue += $this->getPayPalGiftCardVat();
        $basketVatValue += $this->getPayPalPayCostVat();

        if ($this->getDeliveryCosts() < round($this->getDeliveryCosts(), 2)) {
            return floor($basketVatValue * 100) / 100;
        }

        return $basketVatValue;
    }
779213
public function LoadFromBasket(TShopBasket &$oBasket)
    {
        $oShop = TdbShop::GetInstance();
        $oPortal = $this->getPortalDomainService()->getActivePortal();
        $oUser = TdbDataExtranetUser::GetInstance();
        $oBillingAdr = $oUser->GetBillingAddress();

        $sAffiliateCode = $oShop->GetAffilateCode();
        if (false === $sAffiliateCode) {
            $sAffiliateCode = '';
        }

        $pkg_shop_affiliate_id = '';
        $oAffiliatePartnerProgram = TdbPkgShopAffiliate::GetActiveInstance();
        if ($oAffiliatePartnerProgram) {
            if (!empty($oAffiliatePartnerProgram->sCode)) {
                $pkg_shop_affiliate_id = $oAffiliatePartnerProgram->id;
            }
        }
        $request = $this->getCurrentRequest();
        $aOrderData = array(
            'shop_id' => $oShop->id,
            'cms_portal_id' => $oPortal->id,
            'ordernumber' => '',
            'order_ident' => $oBasket->sBasketIdentifier,
            'data_extranet_user_id' => $oUser->id,
            'user_email' => $oUser->GetUserEMail(),
            'customer_number' => $oUser->GetCustomerNumber(),
            'vat_id' => $oUser->fieldVatId,
            'adr_billing_company' => $oBillingAdr->fieldCompany,
            'adr_billing_salutation_id' => $oBillingAdr->fieldDataExtranetSalutationId,
            'adr_billing_firstname' => $oBillingAdr->fieldFirstname,
            'adr_billing_lastname' => $oBillingAdr->fieldLastname,
            'adr_billing_street' => $oBillingAdr->fieldStreet,
            'adr_billing_streetnr' => $oBillingAdr->fieldStreetnr,
            'adr_billing_city' => $oBillingAdr->fieldCity,
            'adr_billing_postalcode' => $oBillingAdr->fieldPostalcode,
            'adr_billing_country_id' => $oBillingAdr->fieldDataCountryId,
            'adr_billing_telefon' => $oBillingAdr->fieldTelefon,
            'adr_billing_fax' => $oBillingAdr->fieldFax,
            'adr_shipping_use_billing' => '1',
            'adr_shipping_salutation_id' => '',
            'adr_shipping_company' => '',
            'adr_shipping_firstname' => '',
            'adr_shipping_lastname' => '',
            'adr_shipping_street' => '',
            'adr_shipping_streetnr' => '',
            'adr_shipping_city' => '',
            'adr_shipping_postalcode' => '',
            'adr_shipping_country_id' => '',
            'adr_shipping_telefon' => '',
            'adr_shipping_fax' => '',
            'shop_shipping_group_price' => $oBasket->dCostShipping,
            'shop_payment_method_price' => $oBasket->dCostPaymentMethodSurcharge,
            'value_article' => $oBasket->dCostArticlesTotal,
            'value_total' => $oBasket->dCostTotal,
            'value_wrapping' => $oBasket->dCostWrapping,
            'value_wrapping_card' => $oBasket->dCostWrappingCards,
            'value_vouchers' => $oBasket->dCostVouchers,
            'value_vouchers_not_sponsored' => $oBasket->dCostNoneSponsoredVouchers,
            'value_discounts' => $oBasket->dCostDiscounts,
            'value_vat_total' => $oBasket->dCostVAT,
            'count_articles' => $oBasket->dTotalNumberOfArticles,
            'count_unique_articles' => $oBasket->iTotalNumberOfUniqueArticles,
            'totalweight' => $oBasket->dTotalWeight,
            'totalvolume' => $oBasket->dTotalVolume,
            'affiliate_code' => $sAffiliateCode,
            'pkg_shop_affiliate_id' => $pkg_shop_affiliate_id,
            'cms_language_id' => self::getLanguageService()->getActiveLanguageId(),
            'user_ip' => null === $request ? '' : $request->getClientIp(),
        );
        if (property_exists($oBillingAdr, 'fieldAddressAdditionalInfo')) {
            $aOrderData['adr_billing_additional_info'] = $oBillingAdr->fieldAddressAdditionalInfo;
        }

        $oShippingGroup = $oBasket->GetActiveShippingGroup();
        if ($oShippingGroup) {
            $oShippingVat = $oShippingGroup->GetVat();
            $aOrderData['shop_shipping_group_id'] = $oShippingGroup->id;
            $aOrderData['shop_shipping_group_name'] = $oShippingGroup->fieldName;
            if ($oShippingVat) {
                $aOrderData['shop_shipping_group_vat_percent'] = $oShippingVat->fieldVatPercent;
            }
        }
        $oPayment = $oBasket->GetActivePaymentMethod();
        if ($oPayment) {
            $aOrderData['shop_payment_method_id'] = $oPayment->id;
            $aOrderData['shop_payment_method_name'] = $oPayment->fieldName;
            $oPaymentVat = $oPayment->GetVat();
            if ($oPaymentVat) {
                $aOrderData['shop_payment_method_vat_percent'] = $oPaymentVat->fieldVatPercent;
            }
        }

        if (!$oUser->ShipToBillingAddress()) {
            $oShippingAdr = $oUser->GetShippingAddress();
            $aOrderData['adr_shipping_use_billing'] = '0';
            $aOrderData['adr_shipping_salutation_id'] = $oShippingAdr->fieldDataExtranetSalutationId;
            $aOrderData['adr_shipping_company'] = $oShippingAdr->fieldCompany;
            $aOrderData['adr_shipping_firstname'] = $oShippingAdr->fieldFirstname;
            $aOrderData['adr_shipping_lastname'] = $oShippingAdr->fieldLastname;
            $aOrderData['adr_shipping_street'] = $oShippingAdr->fieldStreet;
            $aOrderData['adr_shipping_streetnr'] = $oShippingAdr->fieldStreetnr;
            $aOrderData['adr_shipping_city'] = $oShippingAdr->fieldCity;
            $aOrderData['adr_shipping_postalcode'] = $oShippingAdr->fieldPostalcode;
            $aOrderData['adr_shipping_country_id'] = $oShippingAdr->fieldDataCountryId;
            $aOrderData['adr_shipping_telefon'] = $oShippingAdr->fieldTelefon;
            $aOrderData['adr_shipping_fax'] = $oShippingAdr->fieldFax;
            if (property_exists($oShippingAdr, 'fieldAddressAdditionalInfo')) {
                $aOrderData['adr_shipping_additional_info'] = $oShippingAdr->fieldAddressAdditionalInfo;
            }
        }
        $this->LoadFromBasketPostProcessData($oBasket, $aOrderData);
        $this->LoadFromRow($aOrderData);
    }
757872
protected function isArticleParamsEqual($basketItem)
    {
        return ($basketItem->getProductId() == $this->getItemToValidate()->getArticleId() &&
                $basketItem->getPersParams() == $this->getItemToValidate()->getPersistParam() &&
                $basketItem->getSelList() == $this->getItemToValidate()->getSelectList());
    }
941915
protected function _markVouchers($oBasket, $oUser)
    {
        $this->_aVoucherList = $oBasket->getVouchers();

        if (is_array($this->_aVoucherList)) {
            foreach ($this->_aVoucherList as $sVoucherId => $oSimpleVoucher) {
                $oVoucher = oxNew(\OxidEsales\Eshop\Application\Model\Voucher::class);
                $oVoucher->load($sVoucherId);
                $oVoucher->markAsUsed($this->oxorder__oxid->value, $oUser->oxuser__oxid->value, $oSimpleVoucher->dVoucherdiscount);

                $this->_aVoucherList[$sVoucherId] = $oVoucher;
            }
        }
    }
165781
public function getBasketHash($sAmazonOrderReferenceId, $oBasket)
    {
        $aBasket = array(
            'amazonOrderReferenceId' => $sAmazonOrderReferenceId,
            'totalSum' => $oBasket->getBruttoSum(),
            'contents' => array()
        );

        /** @var oxBasketItem $oBasketItem */
        foreach ($oBasket->getContents() as $oBasketItem) {
            $sId = $oBasketItem->getArticle()->getId();
            $aBasket['contents'][$sId] = $oBasketItem->getAmount();
        }

        return md5(json_encode($aBasket));
    }
848529
function itemsInfo()
    {
        $basketInfo = array();
        // Build a price summary for the items based on VAT.
        foreach ( $this->items() as $item )
        {
            $vatValue = $item['vat_value'];
            $itemCount = abs( $item['item_count'] );
            $priceExVAT = $item['price_ex_vat'];
            $priceIncVAT = $item['price_inc_vat'];
            $totalPriceExVAT = $item['total_price_ex_vat'];
            $totalPriceIncVAT = $item['total_price_inc_vat'];

            if ( !isset( $basketInfo['price_info'][$vatValue]['price_ex_vat'] ) )
            {
                $basketInfo['price_info'][$vatValue]['price_ex_vat'] = ( $priceExVAT * $itemCount );
                $basketInfo['price_info'][$vatValue]['price_inc_vat'] = ( $priceIncVAT * $itemCount );
                $basketInfo['price_info'][$vatValue]['price_vat'] = ( $priceIncVAT - $priceExVAT ) * $itemCount;

                $basketInfo['price_info'][$vatValue]['total_price_ex_vat'] = $totalPriceExVAT;
                $basketInfo['price_info'][$vatValue]['total_price_inc_vat'] = $totalPriceIncVAT;
                $basketInfo['price_info'][$vatValue]['total_price_vat'] = $totalPriceIncVAT - $totalPriceExVAT;
            }
            else
            {
                $basketInfo['price_info'][$vatValue]['price_ex_vat'] += ( $priceExVAT * $itemCount );
                $basketInfo['price_info'][$vatValue]['price_inc_vat'] += ( $priceIncVAT * $itemCount );
                $basketInfo['price_info'][$vatValue]['price_vat'] += ( $priceIncVAT - $priceExVAT ) * $itemCount;

                $basketInfo['price_info'][$vatValue]['total_price_ex_vat'] += $totalPriceExVAT;
                $basketInfo['price_info'][$vatValue]['total_price_inc_vat'] += $totalPriceIncVAT;
                $basketInfo['price_info'][$vatValue]['total_price_vat'] += ( $totalPriceIncVAT - $totalPriceExVAT );
            }

            if ( !isset( $basketInfo['total_price_info']['total_price_ex_vat'] ) )
            {
                $basketInfo['total_price_info']['price_ex_vat'] = ( $priceExVAT * $itemCount );
                $basketInfo['total_price_info']['price_inc_vat'] = ( $priceIncVAT * $itemCount );
                $basketInfo['total_price_info']['price_vat'] = ( ( $priceIncVAT - $priceExVAT ) * $itemCount );

                $basketInfo['total_price_info']['total_price_ex_vat'] = $totalPriceExVAT;
                $basketInfo['total_price_info']['total_price_inc_vat'] = $totalPriceIncVAT;
                $basketInfo['total_price_info']['total_price_vat'] = ( $totalPriceIncVAT - $totalPriceExVAT );
            }
            else
            {
                $basketInfo['total_price_info']['price_ex_vat'] += ( $priceExVAT * $itemCount );
                $basketInfo['total_price_info']['price_inc_vat'] += ( $priceIncVAT * $itemCount );
                $basketInfo['total_price_info']['price_vat'] += ( ( $priceIncVAT - $priceExVAT ) * $itemCount );

                $basketInfo['total_price_info']['total_price_ex_vat'] += $totalPriceExVAT;
                $basketInfo['total_price_info']['total_price_inc_vat'] += $totalPriceIncVAT;
                $basketInfo['total_price_info']['total_price_vat'] += ( $totalPriceIncVAT - $totalPriceExVAT );
            }
        }

        // Add shipping cost to the total items price and add / update additional price information.
        $productCollectionID = $this->attribute( 'productcollection_id' );

        // Add additional calculated information to the basketInfo array, that can be used in the template.
        $shippingUpdateStatus = eZShippingManager::updatePriceInfo( $productCollectionID, $basketInfo );

        ksort( $basketInfo['price_info'] );
        return $basketInfo;
    }
421368
protected function _getSessionBasketItems($oDiscount = null)
    {
        if (is_null($oDiscount)) {
            $oDiscount = $this->_getSerieDiscount();
        }

        $oBasket = $this->getSession()->getBasket();
        $aItems = [];
        $iCount = 0;

        foreach ($oBasket->getContents() as $oBasketItem) {
            if (!$oBasketItem->isDiscountArticle() && ($oArticle = $oBasketItem->getArticle()) && !$oArticle->skipDiscounts() && $oDiscount->isForBasketItem($oArticle)) {
                $aItems[$iCount] = [
                    'oxid'     => $oArticle->getId(),
                    'price'    => $oArticle->getBasketPrice($oBasketItem->getAmount(), $oBasketItem->getSelList(), $oBasket)->getPrice(),
                    'discount' => $oDiscount->getAbsValue($oArticle->getBasketPrice($oBasketItem->getAmount(), $oBasketItem->getSelList(), $oBasket)->getPrice()),
                    'amount'   => $oBasketItem->getAmount(),
                ];

                $iCount++;
            }
        }

        return $aItems;
    }
486289
public function isForBasketAmount($oBasket)
    {
        $dAmount = 0;
        $aBasketItems = $oBasket->getContents();
        foreach ($aBasketItems as $oBasketItem) {
            $oBasketArticle = $oBasketItem->getArticle(false);

            if ($this->oxdiscount__oxaddsumtype->value != 'itm') {
                $blForBasketItem = $this->isForBasketItem($oBasketArticle);
            } else {
                $blForBasketItem = $this->isForBundleItem($oBasketArticle);
            }

            if ($blForBasketItem) {
                $dRate = $oBasket->getBasketCurrency()->rate;
                if ($this->oxdiscount__oxprice->value) {
                    if (($oPrice = $oBasketArticle->getPrice())) {
                        $dAmount += ($oPrice->getPrice() * $oBasketItem->getAmount()) / $dRate;
                    }
                } elseif ($this->oxdiscount__oxamount->value) {
                    $dAmount += $oBasketItem->getAmount();
                }
            }
        }

        return $this->isForAmount($dAmount);
    }
662523
public function isForBasket($oBasket)
    {
        // initial configuration check
        if ($this->oxdiscount__oxamount->value == 0 && $this->oxdiscount__oxprice->value == 0) {
            return false;
        }

        $oSummary = $oBasket->getBasketSummary();
        // amounts check
        if ($this->oxdiscount__oxamount->value && ($oSummary->iArticleCount < $this->oxdiscount__oxamount->value || $oSummary->iArticleCount > $this->oxdiscount__oxamountto->value)) {
            return false;
            // price check
        } elseif ($this->oxdiscount__oxprice->value) {
            $dRate = $oBasket->getBasketCurrency()->rate;
            if ($oSummary->dArticleDiscountablePrice < $this->oxdiscount__oxprice->value * $dRate || $oSummary->dArticleDiscountablePrice > $this->oxdiscount__oxpriceto->value * $dRate) {
                return false;
            }
        }

        // oxobject2discount configuration check
        $oDb = \OxidEsales\Eshop\Core\DatabaseProvider::getDb();
        $sQ = 'select 1 from oxobject2discount where oxdiscountid = ' . $oDb->quote($this->oxdiscount__oxid->value) . ' and oxtype in ("oxarticles", "oxcategories" ) ';

        return !((bool) $oDb->getOne($sQ));
    }
1274458
protected function _loadReservations($sBasketId)
    {
        $oReservations = oxNew(\OxidEsales\Eshop\Application\Model\UserBasket::class);
        $aWhere = ['oxuserbaskets.oxuserid' => $sBasketId, 'oxuserbaskets.oxtitle' => 'reservations'];

        // creating if it does not exist
        if (!$oReservations->assignRecord($oReservations->buildSelectString($aWhere))) {
            $oReservations->oxuserbaskets__oxtitle = new \OxidEsales\Eshop\Core\Field('reservations');
            $oReservations->oxuserbaskets__oxuserid = new \OxidEsales\Eshop\Core\Field($sBasketId);
            // marking basket as new (it will not be saved in DB yet)
            $oReservations->setIsNewBasket();
        }

        return $oReservations;
    }
697693
public function GetValueForBasketValue($dBasketValueApplicableForDiscount)
    {
        $dValue = $this->fieldValue;
        $oBasket = TShopBasket::GetInstance();
        if ('prozent' == $this->fieldValueType) {
            $dValue = round($dBasketValueApplicableForDiscount * ($dValue / 100), 2);
        }
        // now if the discount is worth more than the current basket, we need to use only the part that we can
        if ($dBasketValueApplicableForDiscount < $dValue) {
            $dValue = $dBasketValueApplicableForDiscount;
        }

        return $dValue;
    }
212555
private function listBasket(Basket $basket)
    {
        $ret = [
            'basket_id' => $basket->getId(),
            'owner' => $this->listUser($basket->getUser()),
            'created_on' => $basket->getCreated()->format(DATE_ATOM),
            'description' => (string) $basket->getDescription(),
            'name' => $basket->getName(),
            'pusher_usr_id'     => $basket->getPusher() ? $basket->getPusher()->getId() : null,
            'pusher'            => $basket->getPusher() ? $this->listUser($basket->getPusher()) : null,
            'updated_on'        => $basket->getUpdated()->format(DATE_ATOM),
            'unread'            => !$basket->isRead(),
            'validation_basket' => !!$basket->getValidation(),
        ];

        if ($basket->getValidation()) {
            $users = array_map(function (ValidationParticipant $participant) {
                $user = $participant->getUser();

                return [
                    'usr_id' => $user->getId(),
                    'usr_name' => $user->getDisplayName(),
                    'confirmed' => $participant->getIsConfirmed(),
                    'can_agree' => $participant->getCanAgree(),
                    'can_see_others' => $participant->getCanSeeOthers(),
                    'readonly' => $user->getId() != $this->getAuthenticatedUser()->getId(),
                    'user' => $this->listUser($user),
                ];
            }, iterator_to_array($basket->getValidation()->getParticipants()));

            $expires_on_atom = NullableDateTime::format($basket->getValidation()->getExpires());

            $ret = array_merge([
                'validation_users'          => $users,
                'expires_on'                => $expires_on_atom,
                'validation_infos'          => $basket->getValidation()
                    ->getValidationString($this->app, $this->getAuthenticatedUser()),
                'validation_confirmed'      => $basket->getValidation()
                    ->getParticipant($this->getAuthenticatedUser())
                    ->getIsConfirmed(),
                'validation_initiator'      => $basket->getValidation()
                    ->isInitiator($this->getAuthenticatedUser()),
                'validation_initiator_user' => $this->listUser($basket->getValidation()->getInitiator()),
            ], $ret);
        }

        return $ret;
    }
1126153
public function getItemsCount()
    {
        $itemsCount = 0;

        foreach ($this->_aBasketContents as $oBasketItem) {
            $itemsCount += $oBasketItem->getAmount();
        }

        return $itemsCount;
    }
199843
public function GetValidShippingTypesForBasketArticleListAndCountry($oBasketArticleList, $sDataCountryId)
    {
        $oValidShippingTypes = new TIterator();
        /** @var $oValidShippingTypes TIterator* */
        $oShippingTypes = $this->GetFieldShopShippingTypeList('position');
        while ($oShippingType = $oShippingTypes->Next()) {
            $bIsValid = $oShippingType->IsActive();
            $bIsValid = $bIsValid && $oShippingType->isValidForCurrentPortal();
            $bIsValid = $bIsValid && $oShippingType->IsValidForCurrentUser(false);
            $bIsValid = $bIsValid && $oShippingType->IsValidForCountry($sDataCountryId);
            if ($bIsValid) {
                $oAffectedArticles = $oBasketArticleList->GetArticlesAffectedByShippingType($oShippingType);
                $bIsValid = ($oAffectedArticles->Length() > 0);
            }
            if ($bIsValid) {
                $oValidShippingTypes->AddItem($oShippingType);
            }
        }

        return $oValidShippingTypes;
    }
1154716
public function render()
    {
        // recalculating
        if ($oBasket = $this->getSession()->getBasket()) {
            $oBasket->calculateBasket(false);
        }

        parent::render();

        return $oBasket;
    }
694005
public function restoreQuickCheckoutBasket()
    {
        $oSession = $this->getSession();
        $sBasket = $oSession->getVariable(self::BESTITAMAZONPAY_TEMP_BASKET);

        if ($sBasket !== null) {
            //init oxbasketitem class first #1746
            $this->getObjectFactory()->createOxidObject('oxBasketItem');

            $oBasket = unserialize($sBasket);
            $this->_validateBasket($oBasket);

            //Reset old basket
            $oSession->setBasket($oBasket);
        }
    }