/** * Convert data from Magento format to Odoo API format. Select additional data from DB. * * @param \Magento\Sales\Api\Data\OrderInterface $mageOrder * @return \Praxigento\Odoo\Data\Odoo\SaleOrder */ public function getSaleOrder(\Magento\Sales\Api\Data\OrderInterface $mageOrder) { $result = $this->_manObj->create(\Praxigento\Odoo\Data\Odoo\SaleOrder::class); /* Collect order data */ // id_mage $orderIdMage = (int) $mageOrder->getId(); // warehouse_id_odoo $warehouseIdOdoo = (int) $this->_extractWarehouseIdOdoo($mageOrder); // number $number = $mageOrder->getIncrementId(); // date (will be below) // customer $customer = $this->getSaleOrderCustomer($mageOrder); // addr_billing $addrBilling = $this->getAddressBilling($mageOrder); // addr_shipping $addrShipping = $this->getAddressShipping($mageOrder); // price_currency $priceCurrency = $mageOrder->getBaseCurrencyCode(); // pv_total (with date paid) $pvOrder = $this->_repoPvSale->getById($orderIdMage); $pvTotal = $this->_manFormat->toNumber($pvOrder->getTotal()); $datePaid = $pvOrder->getDatePaid(); // lines $lines = $this->getSaleOrderLines($mageOrder); // shipping $shipping = $this->getSaleOrderShipping($mageOrder); // payments $payments = $this->getSaleOrderPayments($mageOrder); /* calculate totals */ $totals = $this->_getLinesTotals($lines); // price_total $priceTotal = $totals[self::AMOUNT] + $shipping->getPriceAmountTotal(); $priceTotal = $this->_manFormat->toNumber($priceTotal); // $priceTotal = $this->_manFormat->toNumber($mageOrder->getBaseGrandTotal()); // price_tax $priceTax = $totals[self::TAX] + $shipping->getPriceTaxAmount(); $priceTax = $this->_manFormat->toNumber($priceTax); // $priceTax = $this->_manFormat->toNumber($mageOrder->getBaseTaxAmount()); // price_discount $priceDiscount = $totals[self::DISCOUNT] + $shipping->getPriceDiscount(); $priceDiscount = $this->_manFormat->toNumber($priceDiscount); /* populate Odoo Data Object */ $result->setIdMage($orderIdMage); $result->setWarehouseIdOdoo($warehouseIdOdoo); $result->setNumber($number); $result->setDatePaid($datePaid); $result->setCustomer($customer); $result->setAddrBilling($addrBilling); $result->setAddrShipping($addrShipping); $result->setPriceCurrency($priceCurrency); $result->setPriceTotal($priceTotal); $result->setPriceTax($priceTax); $result->setPriceDiscount($priceDiscount); $result->setPvTotal($pvTotal); $result->setLines($lines); $result->setShipping($shipping); $result->setPayments($payments); return $result; }
public function execute(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Sales\Model\Order\Invoice $invoice */ $invoice = $observer->getData(self::DATA_INVOICE); $state = $invoice->getState(); if ($state == \Magento\Sales\Model\Order\Invoice::STATE_PAID) { /* update date_paid in the PV registry */ /** @var \Magento\Sales\Model\Order $order */ $order = $invoice->getOrder(); $orderId = $order->getEntityId(); if ($orderId) { $datePaid = $this->_toolDate->getUtcNowForDb(); $this->_logger->debug("Update paid date in PV registry on sale order (#{$orderId}) is paid."); $data = [ESale::ATTR_DATE_PAID => $datePaid]; $this->_repoSale->updateById($orderId, $data); /* transfer PV to customer account */ $this->_subRegister->accountPv($order); } } }
public function execute(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Sales\Model\Order\Invoice $invoice */ $invoice = $observer->getData(self::DATA_INVOICE); $state = $invoice->getState(); if ($state == \Magento\Sales\Model\Order\Invoice::STATE_PAID) { try { /* update date_paid in the PV registry */ /** @var \Magento\Sales\Model\Order $order */ $order = $invoice->getOrder(); $orderId = $order->getEntityId(); $datePaid = $this->_toolDate->getUtcNowForDb(); $this->_logger->debug("Update paid date in PV registry on sale order (#{$orderId}) is paid."); $data = [ESale::ATTR_DATE_PAID => $datePaid]; $this->_repoSale->updateById($orderId, $data); /* transfer PV to customer account */ $this->_subRegister->accountPv($order); } catch (\Throwable $e) { /* catch all exceptions and steal them */ $msg = 'Some error is occurred on update of the paid date in PV register. Error: ' . $e->getMessage(); $this->_logger->error($msg); } } }
/** * Save PV data on sale order save. * * @param Request\Save $req * * @return Response\Save */ public function save(Request\Save $req) { $result = new Response\Save(); $orderId = $req->getSaleOrderId(); $datePaid = $req->getSaleOrderDatePaid(); $items = $req->getOrderItems(); $this->_logger->info("Save PV attributes for sale order #{$orderId}."); $def = $this->_manTrans->begin(); try { /* for all items get PV data by warehouse */ $orderTotal = 0; foreach ($items as $item) { $prodId = $item->getProductId(); $stockId = $item->getStockId(); $itemId = $item->getItemId(); $pv = $this->_repoStockItem->getPvByProductAndStock($prodId, $stockId); $qty = $item->getQuantity(); $total = $pv * $qty; $eItem = new \Praxigento\Pv\Data\Entity\Sale\Item(); $eItem->setSaleItemId($itemId); $eItem->setSubtotal($total); $eItem->setDiscount(0); $eItem->setTotal($total); $this->_repoSaleItem->replace($eItem); $orderTotal += $total; } /* save order data */ $eOrder = new \Praxigento\Pv\Data\Entity\Sale(); $eOrder->setSaleId($orderId); $eOrder->setSubtotal($orderTotal); $eOrder->setDiscount(0); $eOrder->setTotal($orderTotal); $eOrder->setDatePaid($datePaid); $this->_repoSale->replace($eOrder); $this->_manTrans->commit($def); $result->markSucceed(); $this->_logger->info("PV attributes for sale order #{$orderId} are saved."); } finally { $this->_manTrans->end($def); } return $result; }