Esempio n. 1
0
 public function getTaxedPromoPrice(Country $country, $virtualColumnName = 'price_PROMO_PRICE', $discount = 0)
 {
     $taxCalculator = new Calculator();
     return round($taxCalculator->load($this->getProduct(), $country)->getTaxedPrice($this->getPromoPrice($virtualColumnName, $discount)), 2);
 }
Esempio n. 2
0
 public function getTaxedPromoPrice(Country $country, $price)
 {
     $taxCalculator = new Calculator();
     return round($taxCalculator->load($this, $country)->getTaxedPrice($price), 2);
 }
 /**
  * Calculate taxed/untexted price for a product
  *
  * @param $price
  * @param $price_type
  * @param  Product $product
  * @param  bool    $convert
  * @return string
  */
 protected function computePrice($price, $price_type, Product $product, $convert = false)
 {
     $calc = new Calculator();
     $calc->load($product, Country::getShopLocation());
     if ($price_type == 'without_tax') {
         $return_price = $calc->getTaxedPrice($price);
     } elseif ($price_type == 'with_tax') {
         $return_price = $calc->getUntaxedPrice($price);
     } else {
         $return_price = $price;
     }
     if ($convert != 0) {
         $return_price = $price * Currency::getDefaultCurrency()->getRate();
     }
     // Format the number using '.', to perform further calculation
     return NumberFormat::getInstance($this->getRequest())->formatStandardNumber($return_price);
 }
Esempio n. 4
0
 /**
  * Calculate taxed/untexted price for a product
  *
  * @param $price
  * @param $price_type
  * @param  Product $product
  * @param  bool    $convert
  * @return string
  */
 protected function computePrice($price, $price_type, Product $product, $convert = false)
 {
     $calc = new Calculator();
     $calc->load($product, Country::getShopLocation());
     if ($price_type == 'without_tax') {
         $return_price = $calc->getTaxedPrice($price);
     } elseif ($price_type == 'with_tax') {
         $return_price = $calc->getUntaxedPrice($price);
     } else {
         $return_price = $price;
     }
     if ($convert != 0) {
         $return_price = $price * Currency::getDefaultCurrency()->getRate();
     }
     return floatval($return_price);
 }
Esempio n. 5
0
 /**
  * Update the promo status of the sale's selected products and combinations
  *
  * @param  ProductSaleStatusUpdateEvent              $event
  * @throws \RuntimeException
  * @throws \Exception
  * @throws \Propel\Runtime\Exception\PropelException
  */
 public function updateProductsSaleStatus(ProductSaleStatusUpdateEvent $event)
 {
     $taxCalculator = new Calculator();
     $sale = $event->getSale();
     // Get all selected product sale elements for this sale
     if (null !== ($saleProducts = SaleProductQuery::create()->filterBySale($sale)->orderByProductId())) {
         $saleOffsetByCurrency = $sale->getPriceOffsets();
         $offsetType = $sale->getPriceOffsetType();
         $con = Propel::getWriteConnection(SaleTableMap::DATABASE_NAME);
         $con->beginTransaction();
         try {
             /** @var SaleProduct $saleProduct */
             foreach ($saleProducts as $saleProduct) {
                 // Reset all sale status on product's PSE
                 ProductSaleElementsQuery::create()->filterByProductId($saleProduct->getProductId())->update(['Promo' => false], $con);
                 $taxCalculator->load($saleProduct->getProduct($con), CountryModel::getShopLocation());
                 $attributeAvId = $saleProduct->getAttributeAvId();
                 $pseRequest = ProductSaleElementsQuery::create()->filterByProductId($saleProduct->getProductId());
                 // If no attribute AV id is defined, consider ALL product combinations
                 if (!is_null($attributeAvId)) {
                     // Find PSE attached to combination containing this attribute av :
                     // SELECT * from product_sale_elements pse
                     // left join attribute_combination ac on ac.product_sale_elements_id = pse.id
                     // where pse.product_id=363
                     // and ac.attribute_av_id = 7
                     // group by pse.id
                     $pseRequest->useAttributeCombinationQuery(null, Criteria::LEFT_JOIN)->filterByAttributeAvId($attributeAvId)->endUse();
                 }
                 $pseList = $pseRequest->find();
                 if (null !== $pseList) {
                     $this->updateProductSaleElementsPrices($pseList, $sale->getActive(), $offsetType, $taxCalculator, $saleOffsetByCurrency, $con);
                 }
             }
             $con->commit();
         } catch (PropelException $e) {
             $con->rollback();
             throw $e;
         }
     }
 }
Esempio n. 6
0
 public function processGetOrders(ApiCallEvent $event)
 {
     $api = $event->getApi();
     $response = $api->getResponse();
     if ($response->isInError()) {
         $this->logger->error($response->getFormattedError());
         throw new BadResponseException($response->getFormattedError());
     }
     $dispatcher = $event->getDispatcher();
     $orders = $response->getGroup("Orders");
     $validOrders = [];
     /** @var \Thelia\Model\Country $country */
     $shopCountry = CountryQuery::create()->findOneByShopCountry(true);
     $calculator = new Calculator();
     /**
      * Check if there is only one order: reformat the array in that case
      */
     if (array_key_exists("IdOrder", $orders["Order"])) {
         $orders = array("Order" => [$orders["Order"]]);
     }
     $notImportedOrders = [];
     /**
      * Then treat the orders
      */
     foreach ($orders["Order"] as $orderArray) {
         /**
          * I) create the addresses
          */
         /**
          * Get delivery address, and format empty fields
          */
         $deliveryAddressArray =& $orderArray["ShippingAddress"];
         $deliveryCountryId = CountryQuery::create()->findOneByIsoalpha2(strtolower($deliveryAddressArray["Country"]))->getId();
         foreach ($deliveryAddressArray as &$value) {
             if (is_array($value)) {
                 $value = "";
             }
         }
         /**
          * Same for invoice address
          */
         $invoiceAddressArray =& $orderArray["BillingAddress"];
         $invoiceCountry = CountryQuery::create()->findOneByIsoalpha2(strtolower($invoiceAddressArray["Country"]));
         $invoiceCountryId = $invoiceCountry->getId();
         foreach ($invoiceAddressArray as &$value) {
             if (is_array($value)) {
                 $value = "";
             }
         }
         $title = CustomerTitleQuery::create()->findOne()->getId();
         /**
          * Create the order addresses
          */
         $deliveryAddressEvent = new AddressCreateOrUpdateEvent("Delivery address", $title, $deliveryAddressArray["FirstName"], $deliveryAddressArray["LastName"], $deliveryAddressArray["Street"], $deliveryAddressArray["Street1"], $deliveryAddressArray["Street2"], $deliveryAddressArray["PostalCode"], $deliveryAddressArray["Town"], $deliveryCountryId, $deliveryAddressArray["PhoneMobile"], $deliveryAddressArray["Phone"], $deliveryAddressArray["Company"]);
         $deliveryAddressEvent->setCustomer($this->shoppingFluxCustomer);
         $dispatcher->dispatch(TheliaEvents::ADDRESS_CREATE, $deliveryAddressEvent);
         $invoiceAddressEvent = new AddressCreateOrUpdateEvent("Invoice address", $title, $invoiceAddressArray["FirstName"], $invoiceAddressArray["LastName"], $invoiceAddressArray["Street"], $invoiceAddressArray["Street1"], $invoiceAddressArray["Street2"], $invoiceAddressArray["PostalCode"], $invoiceAddressArray["Town"], $deliveryCountryId, $invoiceAddressArray["PhoneMobile"], $invoiceAddressArray["Phone"], $invoiceAddressArray["Company"]);
         $invoiceAddressEvent->setCustomer($this->shoppingFluxCustomer);
         $dispatcher->dispatch(TheliaEvents::ADDRESS_CREATE, $invoiceAddressEvent);
         /**
          * II) Add the products to a cart
          */
         /**
          * Format the products array
          */
         if ($orderArray["NumberOfProducts"] == "1") {
             $orderArray["Products"] = array("Product" => [$orderArray["Products"]["Product"]]);
         }
         $productsArray =& $orderArray["Products"]["Product"];
         /**
          * Create a fake cart
          */
         $cart = new Cart();
         /**
          * And fulfil it with the products
          */
         foreach ($productsArray as &$productArray) {
             $ids = explode("_", $productArray["SKU"]);
             $cartPse = ProductSaleElementsQuery::create()->findPk($ids[1]);
             $calculator->load($cartPse->getProduct(), $shopCountry);
             $price = $calculator->getUntaxedPrice((double) $productArray["Price"]);
             $cart->addCartItem((new CartItem())->setProductSaleElements($cartPse)->setProduct($cartPse->getProduct())->setQuantity($productArray["Quantity"])->setPrice($price)->setPromoPrice(0)->setPromo(0));
         }
         /**
          * III) Create/Save the order
          */
         /**
          * Construct order model
          */
         $lang = LangQuery::create()->findOneByLocale($invoiceCountry->getLocale());
         $currency = CurrencyQuery::create()->findOneByCode("EUR");
         $order = OrderQuery::create()->findOneByRef($orderArray["IdOrder"]);
         if ($order !== null) {
             $order->delete();
         }
         $order = new Order();
         $order->setPostage($orderArray["TotalShipping"])->setChoosenDeliveryAddress($deliveryAddressEvent->getAddress()->getId())->setChoosenInvoiceAddress($invoiceAddressEvent->getAddress()->getId())->setDeliveryModuleId(ShoppingFluxConfigQuery::getDeliveryModuleId())->setPaymentModuleId($this->shoppingFluxPaymentModuleId)->setTransactionRef($orderArray["Marketplace"]);
         /**
          * Construct event
          */
         $orderEvent = new OrderManualEvent($order, $currency, $lang, $cart, $this->shoppingFluxCustomer);
         $orderEvent->setDispatcher($dispatcher);
         $dispatcher->dispatch(TheliaEvents::ORDER_CREATE_MANUAL, $orderEvent);
         $placedOrder = $orderEvent->getPlacedOrder();
         $placedOrder->setRef($orderArray["IdOrder"])->setPaid();
         $validOrders[] = ["IdOrder" => $placedOrder->getRef(), "Marketplace" => $placedOrder->getTransactionRef()];
     }
     /**
      * IV) Valid the orders to Shopping Flux
      */
     $request = new Request("ValidOrders");
     foreach ($validOrders as $validOrder) {
         $request->addOrder($validOrder);
     }
     $validOrdersApi = new ValidOrders($response->getToken(), $response->getMode());
     $validOrdersApi->setRequest($request);
     $validOrdersResponse = $validOrdersApi->getResponse();
     if ($validOrdersResponse->isInError()) {
         $this->logger->error($response->getFormattedError());
     }
 }
Esempio n. 7
0
 public function getTaxedPromoPrice(Country $country)
 {
     $taxCalculator = new Calculator();
     return round($taxCalculator->load($this->getProduct(), $country)->getTaxedPrice($this->getPromoPrice()), 2);
 }
Esempio n. 8
0
 /**
  * @since Version 2.3
  * @param Country $country
  * @param State|null $state
  * @return float
  */
 public function getTotalTaxedPromoPrice(Country $country, State $state = null)
 {
     $taxCalculator = new Calculator();
     return $taxCalculator->load($this->getProduct(), $country, $state)->getTaxedPrice($this->getPromoPrice() * $this->getQuantity());
 }
Esempio n. 9
0
 /**
  * @expectedException \Thelia\Exception\TaxEngineException
  * @expectedExceptionCode 502
  */
 public function testLoadEmptyCountryException()
 {
     $calculator = new Calculator();
     $calculator->load(ProductQuery::create()->findOne(), new Country());
 }