Invoice/EventListener/AbstractInvoiceListener.php
protected function updateCustomerBalance(InvoiceInterface $invoice)
    {
        // Abort if not credit
        if (!InvoiceTypes::isCredit($invoice)) {
            return;
        }

        $sale = $this->getSaleFromInvoice($invoice);

        // Abort if no customer
        if (null === $customer = $sale->getCustomer()) {
            return;
        }

        $methodCs = $this->persistenceHelper->getChangeSet($invoice, 'paymentMethod');
        $amountCs = $this->persistenceHelper->getChangeSet($invoice, 'grandTotal');

        // Debit grand total if invoice is removed
        // TODO Multiple call will credit too much !
        if ($this->persistenceHelper->isScheduledForRemove($invoice)) {
            $method = empty($methodCs) ? $invoice->getPaymentMethod() : $methodCs[0];
            $amount = empty($amountCs) ? $invoice->getGrandTotal(): $amountCs[0];

            if ($method && $method->isCredit() && 0 != Money::compare($amount, 0, $invoice->getCurrency())) {
                $this->customerUpdater->updateCreditBalance($customer, -$amount, true);
            }

            return;
        }

        // Abort if nothing has changed
        if (empty($methodCs) && empty($amountCs)) {
            return;
        }

        // Debit old method customer balance
        /** @var \Ekyna\Component\Commerce\Payment\Model\PaymentMethodInterface $method */
        if (!empty($methodCs) && null !== $method = $methodCs[0]) {
            $amount = empty($amountCs) ? $invoice->getGrandTotal(): $amountCs[0];

            if ($method->isCredit() && 0 != Money::compare($amount, 0, $invoice->getCurrency())) {
                $this->customerUpdater->updateCreditBalance($customer, -$amount, true);
            }
        }

        // Credit new method customer balance
        if (empty($methodCs)) {
            $method = $invoice->getPaymentMethod();
            $amount = empty($amountCs) ? $invoice->getGrandTotal(): $amountCs[1] - $amountCs[0];
        } else {
            /** @var \Ekyna\Component\Commerce\Payment\Model\PaymentMethodInterface $method */
            $method = $methodCs[1];
            $amount = empty($amountCs) ? $invoice->getGrandTotal(): $amountCs[1];
        }
        if ($method && $method->isCredit() && 0 != Money::compare($amount, 0, $invoice->getCurrency())) {
            $this->customerUpdater->updateCreditBalance($customer, $amount, true);
        }
    }
protected function updateCustomerBalance(InvoiceInterface $invoice)
    {
        // Abort if not credit
        if (!InvoiceTypes::isCredit($invoice)) {
            return;
        }

        $sale = $this->getSaleFromInvoice($invoice);

        // Abort if no customer
        if (null === $customer = $sale->getCustomer()) {
            return;
        }

        $methodCs = $this->persistenceHelper->getChangeSet($invoice, 'paymentMethod');
        $amountCs = $this->persistenceHelper->getChangeSet($invoice, 'grandTotal');

        // Debit grand total if invoice is removed
        // TODO Multiple call will credit too much !
        if ($this->persistenceHelper->isScheduledForRemove($invoice)) {
            $method = empty($methodCs) ? $invoice->getPaymentMethod() : $methodCs[0];
            $amount = empty($amountCs) ? $invoice->getGrandTotal(): $amountCs[0];

            if ($method && $method->isCredit() && 0 != Money::compare($amount, 0, $invoice->getCurrency())) {
                $this->customerUpdater->updateCreditBalance($customer, -$amount, true);
            }

            return;
        }

        // Abort if nothing has changed
        if (empty($methodCs) && empty($amountCs)) {
            return;
        }

        // Debit old method customer balance
        /** @var \Ekyna\Component\Commerce\Payment\Model\PaymentMethodInterface $method */
        if (!empty($methodCs) && null !== $method = $methodCs[0]) {
            $amount = empty($amountCs) ? $invoice->getGrandTotal(): $amountCs[0];

            if ($method->isCredit() && 0 != Money::compare($amount, 0, $invoice->getCurrency())) {
                $this->customerUpdater->updateCreditBalance($customer, -$amount, true);
            }
        }

        // Credit new method customer balance
        if (empty($methodCs)) {
            $method = $invoice->getPaymentMethod();
            $amount = empty($amountCs) ? $invoice->getGrandTotal(): $amountCs[1] - $amountCs[0];
        } else {
            /** @var \Ekyna\Component\Commerce\Payment\Model\PaymentMethodInterface $method */
            $method = $methodCs[1];
            $amount = empty($amountCs) ? $invoice->getGrandTotal(): $amountCs[1];
        }
        if ($method && $method->isCredit() && 0 != Money::compare($amount, 0, $invoice->getCurrency())) {
            $this->customerUpdater->updateCreditBalance($customer, $amount, true);
        }
    }
Updates the customer balance

@param InvoiceInterface $invoice