<commit_before><?php
/**
 * User: <NAME> <<EMAIL>>
 */

namespace Praxigento\Wallet\Decorate\Quote\Model\Quote;

/**
 * @SuppressWarnings(PHPMD.CamelCasePropertyName)
 */
class Relation
{
    /** @var \Praxigento\Wallet\Repo\Entity\Partial\IQuote */
    protected $_repoPartialQuote;

    public function __construct(
        \Praxigento\Wallet\Repo\Entity\Partial\IQuote $repoPartialQuote
    ) {
        $this->_repoPartialQuote = $repoPartialQuote;
    }

    /**
     * Process original realtions then save partial payment totals.
     *
     * @param \Magento\Quote\Model\Quote\Relation $subject
     * @param \Closure $proceed
     * @param \Magento\Framework\Model\AbstractModel $object
     *
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function aroundProcessRelation(
        \Magento\Quote\Model\Quote\Relation $subject,
        \Closure $proceed,
        \Magento\Framework\Model\AbstractModel $object
    ) {
        $proceed($object);
        assert($object instanceof \Magento\Quote\Model\Quote);
        $quoteId = $object->getId();
        /** @var \Magento\Quote\Model\Quote\Address $addrShipping */
        $addrShipping = $object->getShippingAddress();
        $total = $addrShipping->get(\Praxigento\Wallet\Model\Quote\Address\Total\Partial::CODE_TOTAL);
        $baseTotal = $addrShipping->get(\Praxigento\Wallet\Model\Quote\Address\Total\Partial::CODE_BASE_TOTAL);
        /* check if current total exist */
        /** @var \Praxigento\Wallet\Data\Entity\Partial\Quote $exist */
        $exist = $this->_repoPartialQuote->getById($quoteId);
        if ($exist) {
            /* there is record in registry */
            $baseTotalExist = $exist->getBasePartialAmount();
            if ($baseTotalExist == $baseTotal) {
                /* amount is equal to stored, do nothing */
            } elseif (abs($baseTotal) < 0.00001) {
                /* amount is zero, remove data from registry */
                $this->_repoPartialQuote->deleteById($quoteId);
            } else {
                /* update saved value */
                $exist->setPartialAmount($total);
                $exist->setBasePartialAmount($baseTotal);
                $this->_repoPartialQuote->updateById($quoteId, $exist);
            }
        } elseif (abs($baseTotal) > 0.00001) {
            /* create new record in registry */
            $data = new \Praxigento\Wallet\Data\Entity\Partial\Quote();
            $data->setQuoteRef($quoteId);
            $exist->setPartialAmount($total);
            $data->setBasePartialAmount($baseTotal);
            $this->_repoPartialQuote->create($data);
        }
    }
}<commit_msg>MOBI-528 - Wrong balance on eWallet<commit_after><?php
/**
 * User: <NAME> <<EMAIL>>
 */

namespace Praxigento\Wallet\Decorate\Quote\Model\Quote;

/**
 * @SuppressWarnings(PHPMD.CamelCasePropertyName)
 */
class Relation
{
    /** @var \Praxigento\Wallet\Repo\Entity\Partial\IQuote */
    protected $_repoPartialQuote;

    public function __construct(
        \Praxigento\Wallet\Repo\Entity\Partial\IQuote $repoPartialQuote
    ) {
        $this->_repoPartialQuote = $repoPartialQuote;
    }

    /**
     * Process original realtions then save partial payment totals.
     *
     * @param \Magento\Quote\Model\Quote\Relation $subject
     * @param \Closure $proceed
     * @param \Magento\Framework\Model\AbstractModel $object
     *
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function aroundProcessRelation(
        \Magento\Quote\Model\Quote\Relation $subject,
        \Closure $proceed,
        \Magento\Framework\Model\AbstractModel $object
    ) {
        $proceed($object);
        assert($object instanceof \Magento\Quote\Model\Quote);
        $quoteId = $object->getId();
        /** @var \Magento\Quote\Model\Quote\Address $addrShipping */
        $addrShipping = $object->getShippingAddress();
        $total = $addrShipping->getData(\Praxigento\Wallet\Model\Quote\Address\Total\Partial::CODE_TOTAL);
        $baseTotal = $addrShipping->getData(\Praxigento\Wallet\Model\Quote\Address\Total\Partial::CODE_BASE_TOTAL);
        /* check if current total exist */
        /** @var \Praxigento\Wallet\Data\Entity\Partial\Quote $exist */
        $exist = $this->_repoPartialQuote->getById($quoteId);
        if ($exist) {
            /* there is record in registry */
            $baseTotalExist = $exist->getBasePartialAmount();
            if ($baseTotalExist == $baseTotal) {
                /* amount is equal to stored, do nothing */
            } elseif (abs($baseTotal) < 0.00001) {
                /* amount is zero, remove data from registry */
                $this->_repoPartialQuote->deleteById($quoteId);
            } else {
                /* update saved value */
                $exist->setPartialAmount($total);
                $exist->setBasePartialAmount($baseTotal);
                $this->_repoPartialQuote->updateById($quoteId, $exist);
            }
        } elseif (abs($baseTotal) > 0.00001) {
            /* create new record in registry */
            $data = new \Praxigento\Wallet\Data\Entity\Partial\Quote();
            $data->setQuoteRef($quoteId);
            $exist->setPartialAmount($total);
            $data->setBasePartialAmount($baseTotal);
            $this->_repoPartialQuote->create($data);
        }
    }
}
{'fraction_non_alphanumeric': 0.13510479041916168, 'fraction_numerical': 0.005052395209580838, 'mean_word_length': 2.365869017632242, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '330', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1549, 'n_words': 254}