/**
  * 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;
 }
Ejemplo n.º 2
0
 /**
  * Account PV on sale done.
  *
  * @param Request\AccountPv $request
  *
  * @return Response\AccountPv
  */
 public function accountPv(Request\AccountPv $request)
 {
     $result = new Response\AccountPv();
     $saleId = $request->getSaleOrderId();
     $customerId = $request->getCustomerId();
     $dateApplied = $request->getDateApplied();
     $this->_logger->info("PV accounting operation for sale order #{$saleId} is started.");
     $sale = $this->_repoSale->getById($saleId);
     $pvTotal = $sale->getTotal();
     /* get customer for sale order */
     if (is_null($customerId)) {
         $this->_logger->info("There is no customer ID in request, select customer ID from sale order data.");
         $customerId = $this->_repoMod->getSaleOrderCustomerId($saleId);
         $this->_logger->info("Order #{$saleId} is created by customer #{$customerId}.");
     }
     /* get PV account data for customer */
     $reqGetAccCust = new GetAccountRequest();
     $reqGetAccCust->setCustomerId($customerId);
     $reqGetAccCust->setAssetTypeCode(Cfg::CODE_TYPE_ASSET_PV);
     $reqGetAccCust->setCreateNewAccountIfMissed(true);
     $respGetAccCust = $this->_callAccount->get($reqGetAccCust);
     /* get PV account data for representative */
     $reqGetAccRepres = new GetAccountRepresentativeRequest();
     $reqGetAccRepres->setAssetTypeCode(Cfg::CODE_TYPE_ASSET_PV);
     $respGetAccRepres = $this->_callAccount->getRepresentative($reqGetAccRepres);
     /* create one operation with one transaction */
     $reqAddOper = new AddOperationRequest();
     $reqAddOper->setOperationTypeCode(Cfg::CODE_TYPE_OPER_PV_SALE_PAID);
     $trans = [Transaction::ATTR_DEBIT_ACC_ID => $respGetAccRepres->getId(), Transaction::ATTR_CREDIT_ACC_ID => $respGetAccCust->getId(), Transaction::ATTR_VALUE => $pvTotal, Transaction::ATTR_DATE_APPLIED => $dateApplied];
     $reqAddOper->setTransactions([$trans]);
     $respAddOper = $this->_callOperation->add($reqAddOper);
     $operId = $respAddOper->getOperationId();
     $result->setOperationId($operId);
     $result->markSucceed();
     $this->_logger->info("PV accounting operation for sale order #{$saleId} is completed.");
     return $result;
 }