public function onShippingMethodCostBeforeSave(LifecycleEventArgs $args) { $range = $args->getObject(); if ($range instanceof ShippingMethodCostInterface) { $shippingMethod = $range->getShippingMethod(); $cost = $range->getCost(); $grossAmount = $cost->getGrossAmount(); $taxRate = $shippingMethod->getTax()->getValue(); $netAmount = TaxHelper::calculateNetPrice($grossAmount, $taxRate); $cost->setTaxRate($taxRate); $cost->setTaxAmount($grossAmount - $netAmount); $cost->setNetAmount($netAmount); $cost->setCurrency($shippingMethod->getCurrency()->getCode()); } }
public function onShippingMethodCostBeforeSave(LifecycleEventArgs $args) { $entity = $args->getObject(); if ($entity instanceof ShippingMethodCostInterface) { $shippingMethod = $entity->getShippingMethod(); $cost = $entity->getCost(); $grossAmount = $cost->getGrossAmount(); $taxRate = $shippingMethod->getTax()->getValue(); $netAmount = TaxHelper::calculateNetPrice($grossAmount, $taxRate); $cost->setTaxRate($taxRate); $cost->setTaxAmount($grossAmount - $netAmount); $cost->setNetAmount($netAmount); $cost->setCurrency($shippingMethod->getCurrency()->getCode()); } if ($entity instanceof ShippingMethodInterface) { $availableCountries = $this->countryRepository->all(); $countries = array_filter($entity->getCountries(), function ($k) use($availableCountries) { return array_key_exists($k, $availableCountries); }, ARRAY_FILTER_USE_KEY); $entity->setCountries($countries); } }
/** * Recalculates buy prices for product * * @param ProductInterface $product */ protected function refreshProductBuyPrices(ProductInterface $product) { $buyPrice = $product->getBuyPrice(); $grossAmount = $buyPrice->getGrossAmount(); $taxRate = $product->getBuyPriceTax()->getValue(); $netAmount = TaxHelper::calculateNetPrice($grossAmount, $taxRate); $buyPrice->setTaxRate($taxRate); $buyPrice->setTaxAmount($grossAmount - $netAmount); $buyPrice->setNetAmount($netAmount); }
/** * Creates an instance of order product * * @param array $productValues * @param OrderInterface $order * * @return \WellCommerce\Bundle\OrderBundle\Entity\OrderProductInterface */ protected function createOrderProduct(array $productValues, OrderInterface $order) { $productId = (int) $productValues['product_id']; $product = $this->productRepository->find($productId); if (!$product instanceof ProductInterface) { throw new \InvalidArgumentException(sprintf('Cannot add product to order. ID "%s" does not exists.', $productId)); } $orderProduct = $this->factory->create(); $orderProduct->setBuyPrice($product->getBuyPrice()); $orderProduct->setOrder($order); $orderProduct->setProduct($product); $orderProduct->setProductAttribute(null); $orderProduct->setQuantity($productValues['quantity']); $orderProduct->setWeight($productValues['weight']); $sellPrice = new Price(); $grossAmount = $productValues['gross_amount']; $taxRate = $product->getSellPriceTax()->getValue(); $netAmount = TaxHelper::calculateNetPrice($grossAmount, $taxRate); $sellPrice->setTaxRate($taxRate); $sellPrice->setTaxAmount($grossAmount - $netAmount); $sellPrice->setNetAmount($netAmount); $sellPrice->setGrossAmount($grossAmount); $sellPrice->setCurrency($order->getCurrency()); $orderProduct->setSellPrice($sellPrice); return $orderProduct; }
/** * Recalculates the net and tax amount * * @param int|float $grossAmount * @param int|float $taxRate */ public function recalculate() { $this->netAmount = TaxHelper::calculateNetPrice($this->grossAmount, $this->taxRate); $this->taxAmount = $this->grossAmount - $this->netAmount; }