/**
  * Initialize partial payment related totals (if exist).
  *
  * @return $this
  */
 public function initTotals()
 {
     /** @var \Magento\Sales\Block\Adminhtml\Order\Totals $parent */
     $parent = $this->getParentBlock();
     /** @var \Magento\Sales\Model\Order $order */
     $order = $parent->getOrder();
     $orderId = $order->getId();
     $found = $this->repoPartialSale->getById($orderId);
     if ($found) {
         $baseAmount = $found->getBasePartialAmount();
         $amount = $found->getPartialAmount();
         $total = new \Magento\Framework\DataObject(['code' => 'praxigento_wallet', 'strong' => true, 'base_value' => $baseAmount, 'value' => $amount, 'label' => __('Paid by eWallet'), 'area' => 'footer', 'is_formated' => false]);
         $parent->addTotal($total);
         /* MOBI-497: fix 'due' amount */
         $totalDue = $parent->getTotal('due');
         if ($totalDue) {
             $due = $totalDue->getData('value');
             $dueBase = $totalDue->getData('base_value');
             $dueFixed = $due - $amount;
             $dueFixedBase = $dueBase - $baseAmount;
             $totalDue->setData('value', $dueFixed);
             $totalDue->setData('base_value', $dueFixedBase);
         }
     }
     return $this;
 }
 public function collect(\Magento\Sales\Model\Order\Invoice $invoice)
 {
     parent::collect($invoice);
     $orderId = $invoice->getOrderId();
     $found = $this->repoPartialSale->getById($orderId);
     if ($found) {
         $partial = $found->getPartialAmount();
         $partialBase = $found->getBasePartialAmount();
         $grand = $invoice->getGrandTotal();
         $grandBase = $invoice->getBaseGrandTotal();
         $grandFixed = $grand - $partial;
         $grandFixedBase = $grandBase - $partialBase;
         $invoice->setGrandTotal($grandFixed);
         $invoice->setBaseGrandTotal($grandFixedBase);
         $invoice->setData(Cfg::CODE_TOTAL_PARTIAL_AMOUNT, $partial);
         $invoice->setData(Cfg::CODE_TOTAL_PARTIAL_AMOUNT_BASE, $partialBase);
     }
     return $this;
 }
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     /* Get base amount for partial payment from quote totals */
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $observer->getData(self::DATA_QUOTE);
     $basePartialAmount = $quote->getShippingAddress()->getData(\Praxigento\Wallet\Model\Quote\Address\Total\Partial::CODE_BASE_TOTAL);
     if ($basePartialAmount) {
         /* save amounts into order registry */
         $partialAmount = $quote->getShippingAddress()->getData(\Praxigento\Wallet\Model\Quote\Address\Total\Partial::CODE_TOTAL);
         /** @var \Magento\Sales\Model\Order $order */
         $order = $observer->getData(self::DATA_ORDER);
         $orderId = $order->getId();
         $data = new \Praxigento\Wallet\Data\Entity\Partial\Sale();
         $data->setPartialAmount($partialAmount);
         $data->setBasePartialAmount($basePartialAmount);
         $data->setSaleOrderRef($orderId);
         $this->_repoPartialSale->create($data);
         $this->_logger->debug("New partial payment by eWallet is registered for order #{$orderId} " . "(base: '{$basePartialAmount}', amount: '{$partialAmount}').");
     }
 }