/**
  * Creates, flushes and returns a Purchasable
  *
  * @return PurchasableInterface
  */
 protected function createPurchasable()
 {
     /**
      * @var CurrencyInterface $currency
      */
     $currency = $this->getRepository('currency')->findOneBy(['iso' => 'USD']);
     /**
      * @var ValueInterface $variantOption
      */
     $variantOption = $this->find('attribute_value', 1);
     /**
      * @var ProductInterface $product
      */
     $product = $this->getFactory('product')->create()->setPrice(Money::create(1000, $currency))->setName('abc')->setSlug('abc')->setEnabled(true)->setWidth(10)->setHeight(10)->setDepth(10)->setWeight(10)->setStock(10);
     /**
      * @var VariantInterface $variant
      */
     $variant = $this->getFactory('product_variant')->create()->setProduct($product)->setPrice(Money::create(1200, $currency))->addOption($variantOption)->setWidth(10)->setHeight(10)->setDepth(10)->setWeight(10)->setEnabled(true)->setStock(20);
     $product->setPrincipalVariant($variant);
     $this->getObjectManager('product')->persist($product);
     $this->getObjectManager('product_variant')->persist($variant);
     $this->getObjectManager('product')->flush();
     $this->getObjectManager('product_variant')->flush();
     $this->variant = $variant;
     return $variant;
 }
 /**
  * When a cart goes below 0 (due to discounts), set the amount to 0.
  *
  * @param CartInterface $cart Cart
  */
 public function validateNegativeAmount(CartInterface $cart)
 {
     $amount = $cart->getAmount();
     if ($amount->getAmount() <= 0) {
         $cart->setAmount(Money::create(0, $amount->getCurrency()));
     }
 }
Example #3
0
 /**
  * Load data fixtures with the passed EntityManager
  *
  * @param ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     /**
      * Pack.
      *
      * @var CategoryInterface     $menCategory
      * @var ManufacturerInterface $manufacturer
      * @var CurrencyInterface     $currencyEur
      * @var ObjectDirector        $packDirector
      * @var EntityTranslatorInterface $entityTranslator
      */
     $menCategory = $this->getReference('category-men');
     $manufacturer = $this->getReference('manufacturer-levis');
     $currencyEur = $this->getReference('currency-EUR');
     $packDirector = $this->getDirector('purchasable_pack');
     $entityTranslator = $this->get('elcodi.entity_translator');
     // Id assigned = 9
     $pack4flavors = $packDirector->create()->setName('Pack 4 flavors')->setSlug('pack-4-flavors')->setDescription('Pack 4 flavors')->setShortDescription('Pack 4 flavors')->addCategory($menCategory)->setPrincipalCategory($menCategory)->setManufacturer($manufacturer)->addPurchasable($this->getReference('variant-ibiza-lips-white-small'))->addPurchasable($this->getReference('product-ibiza-banana'))->addPurchasable($this->getReference('product-i-was-there'))->addPurchasable($this->getReference('product-a-life-style'))->setStockType(ElcodiProductStock::INHERIT_STOCK)->setPrice(Money::create(3000, $currencyEur))->setSku('pack-sku-code-1')->setHeight(30)->setWidth(30)->setDepth(30)->setWeight(200)->setShowInHome(true)->setEnabled(true);
     $packDirector->save($pack4flavors);
     $this->addReference('pack-4-flavors', $pack4flavors);
     $entityTranslator->save($pack4flavors, ['en' => ['name' => 'Pack 4 flavors English', 'slug' => 'pack-4-flavors-en', 'description' => 'Sed venenatis mauris eros, sit amet dapibus turpis consectetur et.
             Etiam blandit erat libero. Integer a elit a tortor scelerisque
             bibendum quis eget tortor. Donec vitae tempor tellus.', 'metaTitle' => 'Pack 4 flavors English', 'metaDescription' => 'Pack 4 flqueavors English', 'metaKeywords' => 'Pack 4 flavors English'], 'es' => ['name' => 'Pack 4 flavors Spanish', 'slug' => 'pack-4-flavors-es', 'description' => 'Sed venenatis mauris eros, sit amet dapibus turpis consectetur et.
             Etiam blandit erat libero. Integer a elit a tortor scelerisque
             bibendum quis eget tortor. Donec vitae tempor tellus.', 'metaTitle' => 'Pack 4 flavors Spanish', 'metaDescription' => 'Pack 4 flavors Spanish', 'metaKeywords' => 'Pack 4 flavors Spanish'], 'fr' => ['name' => 'Pack 4 flavors Français', 'slug' => 'pack-4-flavors-fr', 'description' => 'Sed venenatis mauris eros, sit amet dapibus turpis consectetur et.
             Etiam blandit erat libero. Integer a elit a tortor scelerisque
             bibendum quis eget tortor. Donec vitae tempor tellus.', 'metaTitle' => 'Pack 4 flavors Français', 'metaDescription' => 'Pack 4 flavors Français', 'metaKeywords' => 'Pack 4 flavors Français'], 'ca' => ['name' => 'Pack 4 flavors Català', 'slug' => 'pack-4-flavors-ca', 'description' => 'Sed venenatis mauris eros, sit amet dapibus turpis consectetur et.
             Etiam blandit erat libero. Integer a elit a tortor scelerisque
             bibendum quis eget tortor. Donec vitae tempor tellus.', 'metaTitle' => 'Pack 4 flavors Català', 'metaDescription' => 'Pack 4 flavors Català', 'metaKeywords' => 'Pack 4 flavors Català']]);
     $this->storePurchasableImage($pack4flavors, 'pack-1.jpg');
     $packDirector->save($pack4flavors);
 }
Example #4
0
 /**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     /**
      * @var ObjectDirector            $carrierDirector
      * @var EntityTranslatorInterface $entityTranslator
      */
     $carrierDirector = $this->getDirector('carrier');
     $entityTranslator = $this->get('elcodi.entity_translator');
     $carrier = $carrierDirector->create()->setName('default')->setTax($this->getReference('tax-vat-21'))->setDescription('Default carrier')->setEnabled(true);
     $this->setReference('carrier-default', $carrier);
     $carrierDirector->save($carrier);
     $entityTranslator->save($carrier, ['en' => ['name' => 'Basic', 'description' => 'Our basic delivery system'], 'es' => ['name' => 'Básico', 'description' => 'Nuestro sistema de entrega básico'], 'fr' => ['name' => 'Minimale', 'description' => 'Notre système de livraison basique'], 'ca' => ['name' => 'Bàsic', 'description' => 'El nostre sistema d\'entrega bàsic']]);
     /**
      * @var CurrencyInterface $currencyEuro
      * @var ZoneInterface     $zoneSpain
      */
     $zoneSpain = $this->getReference('zone-spain');
     $currencyEuro = $this->getReference('currency-EUR');
     $shippingRangeDirector = $this->getDirector('shipping_range');
     $shippingPriceRange1 = $shippingRangeDirector->create()->setType(ElcodiShippingRangeTypes::TYPE_PRICE)->setCarrier($carrier)->setName('From 0€ to 10€')->setFromZone($zoneSpain)->setToZone($zoneSpain)->setFromPrice(Money::create(0, $currencyEuro))->setToPrice(Money::create(1000, $currencyEuro))->setPrice(Money::create(900, $currencyEuro))->setEnabled(true);
     $shippingPriceRange2 = $shippingRangeDirector->create()->setType(ElcodiShippingRangeTypes::TYPE_PRICE)->setCarrier($carrier)->setName('From 10€ to 20€')->setFromZone($zoneSpain)->setToZone($zoneSpain)->setFromPrice(Money::create(1000, $currencyEuro))->setToPrice(Money::create(2000, $currencyEuro))->setPrice(Money::create(500, $currencyEuro))->setEnabled(true);
     $shippingPriceRange3 = $shippingRangeDirector->create()->setType(ElcodiShippingRangeTypes::TYPE_PRICE)->setCarrier($carrier)->setName('Free for up to 20€')->setFromZone($zoneSpain)->setToZone($zoneSpain)->setFromPrice(Money::create(2000, $currencyEuro))->setToPrice(Money::create(999999999, $currencyEuro))->setPrice(Money::create(115, $currencyEuro))->setEnabled(true);
     $shippingRangeDirector->save($shippingPriceRange1);
     $shippingRangeDirector->save($shippingPriceRange2);
     $shippingRangeDirector->save($shippingPriceRange3);
 }
Example #5
0
 /**
  * Load data fixtures with the passed EntityManager.
  *
  * @param ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     /**
      * Pack.
      *
      * @var CategoryInterface     $category
      * @var ManufacturerInterface $manufacturer
      * @var CurrencyInterface     $currency
      * @var ObjectDirector        $packDirector
      */
     $category = $this->getReference('category');
     $manufacturer = $this->getReference('manufacturer');
     $product = $this->getReference('product');
     $productReduced = $this->getReference('product-reduced');
     $variant = $this->getReference('variant-red-small');
     $currency = $this->getReference('currency-dollar');
     $packDirector = $this->getDirector('purchasable_pack');
     // Id assigned = 9
     $pack = $packDirector->create()->setName('pack')->setSlug('pack')->setDescription('my pack description')->setShortDescription('my pack short description')->addCategory($category)->setPrincipalCategory($category)->setManufacturer($manufacturer)->addPurchasable($product)->addPurchasable($productReduced)->addPurchasable($variant)->setStockType(ElcodiProductStock::SPECIFIC_STOCK)->setStock(10)->setPrice(Money::create(5000, $currency))->setSku('pack-sku-code-1')->setHeight(30)->setWidth(30)->setDepth(30)->setWeight(200)->setEnabled(true);
     $packDirector->save($pack);
     $this->addReference('pack', $pack);
     // Id assigned = 10
     $packInherit = $packDirector->create()->setName('pack-inherit')->setSlug('pack-inherit')->setDescription('my pack inherit description')->setShortDescription('my pack inherit short description')->addCategory($category)->setPrincipalCategory($category)->setManufacturer($manufacturer)->addPurchasable($product)->addPurchasable($productReduced)->addPurchasable($variant)->setStockType(ElcodiProductStock::INHERIT_STOCK)->setPrice(Money::create(5000, $currency))->setSku('pack-inherit-sku-code-1')->setHeight(30)->setWidth(30)->setDepth(30)->setWeight(200)->setEnabled(true);
     $this->storeProductImage($packInherit, 'pack.jpg');
     $packDirector->save($packInherit);
     $this->addReference('pack-inherit', $packInherit);
 }
Example #6
0
 /**
  * Return the fields to test in entities.
  *
  * [
  *      [[
  *          "type" => $this::GETTER_SETTER,
  *          "getter" => "getValue",
  *          "setter" => "setValue",
  *          "value" => "Elcodi\Component\...\Interfaces\AnInterface"
  *          "nullable" => true
  *      ]],
  *      [[
  *          "type" => $this::ADDER_REMOVER|$this::ADDER_REMOVER,
  *          "getter" => "getValue",
  *          "setter" => "setValue",
  *          "adder" => "addValue",
  *          "removed" => "removeValue",
  *          "bag" => "collection", // can be array
  *          "value" => "Elcodi\Component\...\Interfaces\AnInterface"
  *      ]]
  * ]
  *
  * @return array Fields
  */
 public function getTestableFields()
 {
     $currency = new Currency();
     $currency->setIso('EUR');
     $currency->setSymbol('€');
     return [[['type' => $this::GETTER_SETTER, 'getter' => 'getCode', 'setter' => 'setCode', 'value' => 'discount', 'nullable' => false]], [['type' => $this::GETTER_SETTER, 'getter' => 'getName', 'setter' => 'setName', 'value' => 'Discount', 'nullable' => false]], [['type' => $this::GETTER_SETTER, 'getter' => 'getEnforcement', 'setter' => 'setEnforcement', 'value' => ElcodiCouponTypes::ENFORCEMENT_AUTOMATIC, 'nullable' => false]], [['type' => $this::GETTER_SETTER, 'getter' => 'getPrice', 'setter' => 'setPrice', 'value' => Money::create(1000, $currency), 'nullable' => false]], [['type' => $this::GETTER_SETTER, 'getter' => 'getDiscount', 'setter' => 'setDiscount', 'value' => 10, 'nullable' => false]], [['type' => $this::GETTER_SETTER, 'getter' => 'getAbsolutePrice', 'setter' => 'setAbsolutePrice', 'value' => Money::create(1000, $currency), 'nullable' => false]], [['type' => $this::GETTER_SETTER, 'getter' => 'getValue', 'setter' => 'setValue', 'value' => 'value', 'nullable' => true]], [['type' => $this::GETTER_SETTER, 'getter' => 'getCount', 'setter' => 'setCount', 'value' => 10, 'nullable' => false]], [['type' => $this::GETTER_SETTER, 'getter' => 'getUsed', 'setter' => 'setUsed', 'value' => 10, 'nullable' => false]], [['type' => $this::GETTER_SETTER, 'getter' => 'getPriority', 'setter' => 'setPriority', 'value' => 10, 'nullable' => false]], [['type' => $this::GETTER_SETTER, 'getter' => 'getMinimumPurchase', 'setter' => 'setMinimumPurchase', 'value' => Money::create(1000, $currency), 'nullable' => false]], [['type' => $this::GETTER_SETTER, 'getter' => 'getStackable', 'setter' => 'setStackable', 'value' => true, 'nullable' => false]], [['type' => $this::GETTER_SETTER, 'getter' => 'getRule', 'setter' => 'setRule', 'value' => new Rule(), 'nullable' => true]]];
 }
 /**
  * Calculate coupon absolute value.
  *
  * @param CartInterface   $cart   Cart
  * @param CouponInterface $coupon Coupon
  *
  * @return MoneyInterface|false Absolute value for this coupon in this cart
  */
 public function getCouponAbsoluteValue(CartInterface $cart, CouponInterface $coupon)
 {
     $currency = $this->currencyWrapper->get();
     $couponPrice = Money::create(0, $currency);
     $value = $coupon->getValue();
     preg_match($this->regexp(), $value, $match);
     $m = (int) $match[1];
     $n = (int) $match[2];
     $expressionValue = isset($match[3]) ? $match[3] : '';
     $modifiers = isset($match[4]) ? $match[4] : '';
     $freePerGroup = $m - $n;
     $freePerGroup = max($freePerGroup, 0);
     foreach ($cart->getCartLines() as $cartLine) {
         $moneys = [];
         $purchasable = $cartLine->getPurchasable();
         $expressionEvaluator = $this->getExpressionLanguageInstance();
         $expressionResult = (empty($expressionValue) || $expressionEvaluator->evaluate($expressionValue, ['purchasable' => $purchasable])) && $this->evaluatePurchasableType($purchasable, $modifiers);
         if (true === $expressionResult) {
             $partialElements = $cartLine->getQuantity();
             for ($i = 0; $i < $partialElements; ++$i) {
                 $partialPurchasable = $cartLine->getPurchasable();
                 $moneys[] = $partialPurchasable->getReducedPrice()->getAmount() > 0 ? $partialPurchasable->getReducedPrice() : $partialPurchasable->getPrice();
             }
             $groups = $partialElements / $m;
             if ($groups > 0) {
                 $nbMoneys = $groups * $freePerGroup;
                 $moneysToDiscount = array_slice($moneys, 0, $nbMoneys);
                 foreach ($moneysToDiscount as $moneyToDiscount) {
                     $couponPrice = $couponPrice->add($this->currencyConverter->convertMoney($moneyToDiscount, $currency));
                 }
             }
         }
     }
     return $couponPrice;
 }
Example #8
0
 /**
  * {@inheritdoc}
  */
 public function setDefaultOptions(OptionsResolverInterface $resolver)
 {
     /**
      * We set given Currency as default object to work with
      */
     $money = Money::create(0, $this->currencyWrapper->getDefaultCurrency());
     $resolver->setDefaults(array('data_class' => 'Elcodi\\Component\\Currency\\Entity\\Money', 'empty_data' => $money));
 }
 /**
  * When a cart goes below 0 (due to discounts), set the amount to 0.
  *
  * @param CartOnLoadEvent $event Event
  */
 public function limitCartAmount(CartOnLoadEvent $event)
 {
     $cart = $event->getCart();
     $amount = $cart->getAmount();
     if ($amount->getAmount() <= 0) {
         $cart->setAmount(Money::create(0, $amount->getCurrency()));
     }
 }
 /**
  * Test convert money.
  *
  * @param $isoFrom
  * @param $isoTo
  * @param $amount
  * @param $resultAmount
  *
  * @dataProvider dataConvertMoney
  */
 public function testConvertMoney($isoFrom, $isoTo, $amount, $resultAmount)
 {
     $currencyFrom = $this->createCurrency($isoFrom);
     $currencyTo = $this->createCurrency($isoTo);
     $money = Money::create($amount, $currencyFrom);
     $moneyResult = $this->currencyConverter->convertMoney($money, $currencyTo);
     $this->assertEquals($moneyResult->getAmount(), $resultAmount);
     $this->assertEquals($moneyResult->getCurrency(), $currencyTo);
 }
 /**
  * @expectedException \Exception
  */
 public function testCurrencyRateNotFoundThrowsException()
 {
     $localAdapter = $this->getMock('Elcodi\\Component\\Currency\\Adapter\\LocaleProvider\\Interfaces\\LocaleProviderAdapterInterface');
     $localAdapter->expects($this->any())->method('getLocaleIso')->will($this->returnValue('es_ES'));
     $priceExtension = new PrintMoneyExtension($this->getMock('Elcodi\\Component\\Currency\\Services\\CurrencyConverter', [], [], '', false), $this->getMock('Elcodi\\Component\\Currency\\Wrapper\\CurrencyWrapper', [], [], '', false), $localAdapter);
     $currencyFactory = new CurrencyFactory();
     $currencyFactory->setEntityNamespace('Elcodi\\Component\\Currency\\Entity\\Currency');
     $priceExtension->printMoney(Money::create(1000, $currencyFactory->create()->setIso('US1')), 'US1');
 }
Example #12
0
 /**
  * @expectedException \Exception
  */
 public function testCurrencyRateNotFoundThrowsException()
 {
     $locale = $this->getMock('Elcodi\\Component\\Language\\Entity\\Interfaces\\LocaleInterface');
     $locale->expects($this->any())->method('getIso')->willReturn('es_ES');
     $priceExtension = new MoneyPrinter($this->getMock('Elcodi\\Component\\Currency\\Services\\CurrencyConverter', [], [], '', false), $this->getMock('Elcodi\\Component\\Currency\\Wrapper\\CurrencyWrapper', [], [], '', false), Locale::create($locale));
     $currencyFactory = new CurrencyFactory();
     $currencyFactory->setEntityNamespace('Elcodi\\Component\\Currency\\Entity\\Currency');
     $currencyFactory->setDateTimeFactory(new DateTimeFactory());
     $priceExtension->printMoney(Money::create(1000, $currencyFactory->create()->setIso('US1')), 'US1');
 }
 /**
  * Creates, flushes and returns a Purchasable.
  *
  * @return mixed
  */
 protected function createPurchasable()
 {
     /**
      * @var CurrencyInterface $currency
      */
     $currency = $this->getRepository('currency')->findOneBy(['iso' => 'USD']);
     $product = $this->get('elcodi.factory.product')->create()->setPrice(Money::create(1000, $currency))->setName('abc')->setSlug('abc')->setWidth(10)->setHeight(10)->setDepth(10)->setWeight(10)->setEnabled(true)->setStock(10);
     $this->getObjectManager('product')->persist($product);
     $this->getObjectManager('product')->flush();
     return $product;
 }
 /**
  * Calculate coupon absolute value.
  *
  * @param CartInterface   $cart   Cart
  * @param CouponInterface $coupon Coupon
  *
  * @return MoneyInterface Absolute value for this coupon in this cart
  */
 public function getCouponAbsoluteValue(CartInterface $cart, CouponInterface $coupon)
 {
     $currency = $this->currencyWrapper->get();
     $couponPrice = Money::create(0, $currency);
     foreach ($this->cartCouponApplicators as $cartCouponApplicator) {
         if ($cartCouponApplicator->canBeApplied($cart, $coupon)) {
             return $cartCouponApplicator->getCouponAbsoluteValue($cart, $coupon);
         }
     }
     return $couponPrice;
 }
 /**
  * Calculates coupons price given actual Cart.
  *
  * @param CartInterface $cart Cart
  */
 public function refreshCouponAmount(CartInterface $cart)
 {
     $couponAmount = Money::create(0, $this->currencyWrapper->get());
     $coupons = $this->cartCouponManager->getCoupons($cart);
     foreach ($coupons as $coupon) {
         $currentCouponAmount = $this->cartCouponApplicatorCollector->getCouponAbsoluteValue($cart, $coupon);
         $coupon->setAbsolutePrice($currentCouponAmount);
         $couponAmount = $couponAmount->add($currentCouponAmount);
     }
     $cart->setCouponAmount($couponAmount);
 }
Example #16
0
 /**
  * Validates the value to be greater or equal than the constraint value.
  *
  * @param Money      $value      Value
  * @param Constraint $constraint Constraint
  *
  * @return null
  */
 public function validate($value, Constraint $constraint)
 {
     if (null === $value) {
         return null;
     }
     if (!$value instanceof Money) {
         throw new UnexpectedTypeException($value, 'Elcodi\\Component\\Currency\\Entity\\Money');
     }
     $minimumMoney = Money::create($constraint->value, $value->getCurrency());
     if ($value->isLessThan($minimumMoney)) {
         $this->context->buildViolation($constraint->message)->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))->setParameter('{{ compared_value }}', $this->formatValue($minimumMoney, self::OBJECT_TO_STRING))->setParameter('{{ compared_value_type }}', $this->formatTypeOf($minimumMoney))->addViolation();
     }
 }
Example #17
0
 /**
  * Loads sample fixtures for product Variant entities
  *
  * @param ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     /**
      * @var ProductInterface  $productWithVariants
      * @var CurrencyInterface $currency
      */
     $currency = $this->getReference('currency-dollar');
     $productWithVariants = $this->getReference('product-with-variants');
     /**
      * @var $optionWhite Value
      * @var $optionRed   Value
      * @var $optionSmall Value
      * @var $optionLarge Value
      */
     $optionWhite = $this->getReference('value-color-white');
     $optionRed = $this->getReference('value-color-red');
     $optionSmall = $this->getReference('value-size-small');
     $optionLarge = $this->getReference('value-size-large');
     /**
      * Variant White-Small
      */
     $variantWhiteSmall = $this->container->get('elcodi.core.product.factory.variant')->create();
     $variantWhiteSmall->setSku('variant-white-small-sku')->setStock(100)->setProduct($productWithVariants)->addOption($optionWhite)->addOption($optionSmall)->setPrice(Money::create(1500, $currency))->setEnabled(true);
     $productWithVariants->setPrincipalVariant($variantWhiteSmall);
     $manager->persist($variantWhiteSmall);
     $this->addReference('variant-white-small', $productWithVariants);
     /**
      * Variant White-Large
      */
     $variantWhiteLarge = $this->container->get('elcodi.core.product.factory.variant')->create();
     $variantWhiteLarge->setSku('variant-white-large-sku')->setStock(100)->setProduct($productWithVariants)->addOption($optionWhite)->addOption($optionLarge)->setPrice(Money::create(1800, $currency))->setEnabled(true);
     $manager->persist($variantWhiteLarge);
     $this->addReference('variant-white-large', $productWithVariants);
     /**
      * Variant Red-Small
      */
     $variantRedSmall = $this->container->get('elcodi.core.product.factory.variant')->create();
     $variantRedSmall->setSku('variant-red-small-sku')->setStock(100)->setProduct($productWithVariants)->addOption($optionRed)->addOption($optionSmall)->setPrice(Money::create(1500, $currency))->setEnabled(true);
     $manager->persist($variantRedSmall);
     $this->addReference('variant-red-small', $productWithVariants);
     /**
      * Variant Red-Large
      */
     $variantRedLarge = $this->container->get('elcodi.core.product.factory.variant')->create();
     $variantRedLarge->setSku('variant-red-large-sku')->setStock(100)->setProduct($productWithVariants)->addOption($optionRed)->addOption($optionLarge)->setPrice(Money::create(1800, $currency))->setEnabled(true);
     $manager->persist($variantRedLarge);
     $this->addReference('variant-red-large', $productWithVariants);
     $manager->flush();
 }
Example #18
0
 /**
  * Return functions.
  *
  * @return ExpressionFunction[] An array of Function instances
  */
 public function getFunctions()
 {
     return [new ExpressionFunction('money', function () {
         throw new RuntimeException('Function "money" can\'t be compiled.');
     }, function (array $context, $amount, $currencyIso = null) {
         if ($currencyIso === null) {
             $currency = $this->defaultCurrencyWrapper->get();
         } else {
             /**
              * @var CurrencyInterface $currency
              */
             $currency = $this->currencyRepository->findOneBy(['iso' => $currencyIso]);
         }
         return Money::create($amount * 100, $currency);
     })];
 }
Example #19
0
 /**
  * Loads sample fixtures for product Variant entities.
  *
  * @param ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     /**
      * @var ProductInterface  $productWithVariants
      * @var CurrencyInterface $currency
      * @var ObjectDirector    $variantDirector
      */
     $currency = $this->getReference('currency-dollar');
     $productWithVariants = $this->getReference('product-with-variants');
     $variantDirector = $this->getDirector('product_variant');
     /**
      * @var ValueInterface $optionWhite
      * @var ValueInterface $optionRed
      * @var ValueInterface $optionSmall
      * @var ValueInterface $optionLarge
      */
     $optionWhite = $this->getReference('value-color-white');
     $optionRed = $this->getReference('value-color-red');
     $optionSmall = $this->getReference('value-size-small');
     $optionLarge = $this->getReference('value-size-large');
     /**
      * Variant White-Small.
      */
     $variantWhiteSmall = $variantDirector->create()->setSku('variant-white-small-sku')->setStock(100)->setProduct($productWithVariants)->addOption($optionWhite)->addOption($optionSmall)->setPrice(Money::create(1500, $currency))->setHeight(13)->setWidth(12)->setDepth(19)->setWeight(125)->setEnabled(true);
     $productWithVariants->setPrincipalVariant($variantWhiteSmall);
     $variantDirector->save($variantWhiteSmall);
     $this->addReference('variant-white-small', $variantWhiteSmall);
     /**
      * Variant White-Large.
      */
     $variantWhiteLarge = $variantDirector->create()->setSku('variant-white-large-sku')->setStock(100)->setProduct($productWithVariants)->addOption($optionWhite)->addOption($optionLarge)->setPrice(Money::create(1800, $currency))->setHeight(12)->setWidth(11)->setDepth(45)->setWeight(155)->setEnabled(true);
     $variantDirector->save($variantWhiteLarge);
     $this->addReference('variant-white-large', $variantWhiteLarge);
     /**
      * Variant Red-Small.
      */
     $variantRedSmall = $variantDirector->create()->setSku('variant-red-small-sku')->setStock(100)->setProduct($productWithVariants)->addOption($optionRed)->addOption($optionSmall)->setPrice(Money::create(1500, $currency))->setHeight(19)->setWidth(9)->setDepth(33)->setWeight(1000)->setEnabled(true);
     $this->storeProductImage($variantRedSmall, 'variant.jpg');
     $variantDirector->save($variantRedSmall);
     $this->addReference('variant-red-small', $variantRedSmall);
     /**
      * Variant Red-Large.
      */
     $variantRedLarge = $variantDirector->create()->setSku('variant-red-large-sku')->setStock(100)->setProduct($productWithVariants)->addOption($optionRed)->addOption($optionLarge)->setPrice(Money::create(1800, $currency))->setHeight(50)->setWidth(30)->setDepth(18)->setWeight(70)->setEnabled(true);
     $variantDirector->save($variantRedLarge);
     $this->addReference('variant-red-large', $variantRedLarge);
 }
 /**
  * Test testValidateCartCouponMinimumPrice
  *
  * @dataProvider dataValidateCartCouponMinimumPrice
  */
 public function testValidateCartCouponMinimumPrice($price, $currency, $throwException)
 {
     $cart = $this->getLoadedCart(2);
     $coupon = $this->getEnabledCoupon(3);
     $coupon->setMinimumPurchase(Money::create($price, $this->find('currency', $currency)));
     try {
         $this->get('elcodi.manager.cart_coupon')->addCoupon($cart, $coupon);
     } catch (CouponBelowMinimumPurchaseException $e) {
         if (!$throwException) {
             throw $e;
         }
     }
     /**
      * Clean operations to avoid restart scenario
      */
     $this->get('elcodi.manager.cart_coupon')->removeCoupon($cart, $coupon);
 }
Example #21
0
 /**
  * Load data fixtures with the passed EntityManager.
  *
  * @param ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     /**
      * Product.
      *
      * @var CategoryInterface     $category
      * @var ManufacturerInterface $manufacturer
      * @var CurrencyInterface     $currency
      * @var ObjectDirector        $productDirector
      */
     $category = $this->getReference('category');
     $rootCategory = $this->getReference('rootCategory');
     $manufacturer = $this->getReference('manufacturer');
     $currency = $this->getReference('currency-dollar');
     $productDirector = $this->getDirector('product');
     $product = $productDirector->create()->setName('product')->setSlug('product')->setDescription('my product description')->setShortDescription('my product short description')->addCategory($category)->setPrincipalCategory($category)->setManufacturer($manufacturer)->setStock(10)->setPrice(Money::create(1000, $currency))->setSku('product-sku-code-1')->setHeight(10)->setWidth(15)->setDepth(20)->setWeight(100)->setEnabled(true);
     $productDirector->save($product);
     $this->addReference('product', $product);
     /**
      * Reduced Product.
      */
     $productReduced = $productDirector->create()->setName('product-reduced')->setSlug('product-reduced')->setDescription('my product-reduced description')->setShortDescription('my product-reduced short description')->setShowInHome(true)->setStock(5)->setPrice(Money::create(1000, $currency))->setReducedPrice(Money::create(500, $currency))->setHeight(25)->setWidth(30)->setDepth(35)->setWeight(200)->setEnabled(true);
     $this->storeProductImage($productReduced, 'product.jpg');
     $productDirector->save($productReduced);
     $this->addReference('product-reduced', $productReduced);
     /**
      * Product with variants.
      *
      * @var ProductInterface $productWithVariants
      */
     $productWithVariants = $productDirector->create()->setName('Product with variants')->setSku('product-sku-code-variant-1')->setSlug('product-with-variants')->setDescription('my product with variants description')->setShortDescription('my product with variants short description')->addCategory($category)->setPrincipalCategory($category)->setManufacturer($manufacturer)->setStock(10)->setPrice(Money::create(1000, $currency))->setHeight(40)->setWidth(45)->setDepth(50)->setWeight(500)->setEnabled(true);
     $productDirector->save($productWithVariants);
     $this->addReference('product-with-variants', $productWithVariants);
     /**
      * Root category product.
      *
      * @var ProductInterface $rootCategoryProduct
      */
     $rootCategoryProduct = $productDirector->create()->setName('Root category product')->setSlug('root-category-product')->setDescription('my product description')->setShortDescription('my product short description')->addCategory($rootCategory)->setPrincipalCategory($rootCategory)->setManufacturer($manufacturer)->setStock(10)->setPrice(Money::create(500, $currency))->setSku('product-sku-code-3')->setHeight(10)->setWidth(15)->setDepth(20)->setWeight(100)->setEnabled(true);
     $productDirector->save($rootCategoryProduct);
     $this->addReference('rootCategoryProduct', $rootCategoryProduct);
 }
Example #22
0
 /**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     /**
      * @var CurrencyInterface $currency
      * @var CouponFactory     $couponFactory
      */
     $couponFactory = $this->container->get('elcodi.core.coupon.factory.coupon');
     $currency = $this->getReference('currency-dollar');
     /**
      * Coupon with 12% of discount
      *
      * Valid from now without expire time
      *
      * Customer only can redeem it 5 times in all life
      *
      * Only 100 available
      *
      * @var CouponInterface $couponPercent
      */
     $couponPercent = $couponFactory->create();
     $couponPercent->setCode('percent')->setName('10 percent discount')->setType(ElcodiCouponTypes::TYPE_PERCENT)->setDiscount(12)->setCount(100)->setValidFrom(new DateTime())->setValidTo(new DateTime('next month'));
     $manager->persist($couponPercent);
     $this->addReference('coupon-percent', $couponPercent);
     /**
      * Coupon with 5 USD of discount.
      *
      * Valid from now without expire time
      *
      * Customer only can redeem it n times in all life
      *
      * Only 20 available
      *
      * Prices are stored in cents. @see \Elcodi\Component\Currency\Entity\Money
      *
      * @var CouponInterface $couponAmount
      */
     $couponAmount = $couponFactory->create();
     $couponAmount->setCode('amount')->setName('5 USD discount')->setType(ElcodiCouponTypes::TYPE_AMOUNT)->setPrice(Money::create(500, $currency))->setCount(20)->setValidFrom(new DateTime());
     $manager->persist($couponAmount);
     $this->addReference('coupon-amount', $couponAmount);
     $manager->flush();
 }
Example #23
0
 /**
  * Test convert money
  *
  * @dataProvider dataConvertMoney
  */
 public function testConvertMoney($isoFrom, $isoTo, $amount, $resultAmount)
 {
     $currencyManager = $this->getMockBuilder('Elcodi\\Component\\Currency\\Services\\CurrencyManager')->setMethods(['getExchangeRateList'])->disableOriginalConstructor()->getMock();
     $currencyFactory = new CurrencyFactory();
     $currencyFactory->setEntityNamespace('Elcodi\\Component\\Currency\\Entity\\Currency');
     $currencyBase = 'USD';
     /**
      * @var CurrencyManager $currencyManager
      */
     $currencyManager->expects($this->any())->method('getExchangeRateList')->will($this->returnValue(['EUR' => ['rate' => '0.7365960000', 'currency' => $currencyFactory->create()], 'GBP' => ['rate' => '0.5887650000', 'currency' => $currencyFactory->create()], 'JPY' => ['rate' => '101.8226250000', 'currency' => $currencyFactory->create()]]));
     $currencyConverter = new CurrencyConverter($currencyManager, $currencyBase);
     $currencyFrom = $currencyFactory->create();
     $currencyFrom->setIso($isoFrom);
     $currencyTo = $currencyFactory->create();
     $currencyTo->setIso($isoTo);
     $money = Money::create($amount, $currencyFrom);
     $moneyResult = $currencyConverter->convertMoney($money, $currencyTo);
     $this->assertEquals($moneyResult->getAmount(), $resultAmount);
     $this->assertEquals($moneyResult->getCurrency(), $currencyTo);
 }
 /**
  * Set up.
  */
 public function setUp()
 {
     /**
      * @var CartEventDispatcher     $cartEventDispatcher
      * @var CartLineEventDispatcher $cartLineEventDispatcher
      * @var CartFactory             $cartFactory
      * @var CartLineFactory         $cartLineFactory
      * @var CartWrapper             $cartWrapper
      */
     $emptyMoneyWrapper = $this->getMock('Elcodi\\Component\\Currency\\Wrapper\\EmptyMoneyWrapper', [], [], '', false);
     $currency = new Currency();
     $currency->setIso('EUR');
     $emptyMoneyWrapper->expects($this->any())->method('get')->willReturn(Money::create(0, $currency));
     $cartEventDispatcher = $this->getMock('Elcodi\\Component\\Cart\\EventDispatcher\\CartEventDispatcher', [], [], '', false);
     $cartLineEventDispatcher = $this->getMock('Elcodi\\Component\\Cart\\EventDispatcher\\CartLineEventDispatcher', [], [], '', false);
     $cartFactory = $this->getMock('Elcodi\\Component\\Cart\\Factory\\CartFactory', ['create'], [$emptyMoneyWrapper]);
     $cartLineFactory = $this->getMock('Elcodi\\Component\\Cart\\Factory\\CartLineFactory', ['create'], [$emptyMoneyWrapper]);
     $this->cartManager = new CartManager($cartEventDispatcher, $cartLineEventDispatcher, $cartFactory, $cartLineFactory);
     $this->cartFactory = $cartFactory;
     $this->cartLineFactory = $cartLineFactory;
 }
 /**
  * Load cart purchasables prices.
  *
  * @param CartInterface $cart Cart
  */
 public function loadCartPurchasablesAmount(CartInterface $cart)
 {
     $currency = $this->currencyWrapper->get();
     $purchasableAmount = Money::create(0, $currency);
     /**
      * Calculate Amount and PurchasableAmount.
      */
     foreach ($cart->getCartLines() as $cartLine) {
         /**
          * @var CartLineInterface $cartLine
          */
         $cartLine = $this->loadCartLinePrices($cartLine);
         /**
          * @var MoneyInterface $purchasableAmount
          * @var MoneyInterface $totalAmount
          */
         $convertedPurchasableAmount = $this->currencyConverter->convertMoney($cartLine->getPurchasableAmount(), $currency);
         $purchasableAmount = $purchasableAmount->add($convertedPurchasableAmount->multiply($cartLine->getQuantity()));
     }
     $cart->setPurchasableAmount($purchasableAmount);
 }
Example #26
0
 /**
  * Load data fixtures with the passed EntityManager
  *
  * @param ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     /**
      * Product
      *
      * @var ProductInterface      $product
      * @var CategoryInterface     $category
      * @var ManufacturerInterface $manufacturer
      * @var CurrencyInterface     $currency
      */
     $product = $this->container->get('elcodi.core.product.factory.product')->create();
     $category = $this->getReference('category');
     $manufacturer = $this->getReference('manufacturer');
     $currency = $this->getReference('currency-dollar');
     $product->setName('product')->setSlug('product')->setDescription('my product description')->setShortDescription('my product short description')->addCategory($category)->setPrincipalCategory($category)->setManufacturer($manufacturer)->setStock(10)->setPrice(Money::create(1000, $currency))->setSku('product-sku-code-1')->setEnabled(true);
     $manager->persist($product);
     $this->addReference('product', $product);
     /**
      * Reduced Product
      *
      * @var ProductInterface $productReduced
      */
     $productReduced = $this->container->get('elcodi.core.product.factory.product')->create();
     $productReduced->setName('product-reduced')->setSlug('product-reduced')->setDescription('my product-reduced description')->setShortDescription('my product-reduced short description')->setShowInHome(true)->setStock(5)->setPrice(Money::create(1000, $currency))->setReducedPrice(Money::create(500, $currency))->setEnabled(true);
     $manager->persist($productReduced);
     $this->addReference('product-reduced', $productReduced);
     /**
      * Product with variants
      *
      * @var ProductInterface $productReduced
      */
     $productWithVariants = $this->container->get('elcodi.core.product.factory.product')->create();
     $productWithVariants->setName('Product with variants')->setSku('product-sku-code-variant-1')->setSlug('product-with-variants')->setDescription('my product with variants description')->setShortDescription('my product with variants short description')->addCategory($category)->setPrincipalCategory($category)->setManufacturer($manufacturer)->setStock(10)->setPrice(Money::create(1000, $currency))->setEnabled(true);
     $manager->persist($productWithVariants);
     $this->addReference('product-with-variants', $productWithVariants);
     $manager->flush();
 }
Example #27
0
 /**
  * Testing that when I add a product with id 1 and a variant with the same
  * id (1), new variant is added into the cart, instead of incrementing first
  * mentioned product.
  */
 public function testAddProductAndVariantSameId()
 {
     /**
      * @var CartEventDispatcher     $cartEventDispatcher
      * @var CartLineEventDispatcher $cartLineEventDispatcher
      * @var CartFactory             $cartFactory
      * @var CartLineFactory         $cartLineFactory
      * @var CartWrapper             $cartWrapper
      */
     $emptyMoneyWrapper = $this->getMock('Elcodi\\Component\\Currency\\Wrapper\\EmptyMoneyWrapper', [], [], '', false);
     $currency = new Currency();
     $currency->setIso('EUR');
     $emptyMoneyWrapper->expects($this->any())->method('get')->willReturn(Money::create(0, $currency));
     $cartEventDispatcher = $this->getMock('Elcodi\\Component\\Cart\\EventDispatcher\\CartEventDispatcher', [], [], '', false);
     $cartLineEventDispatcher = $this->getMock('Elcodi\\Component\\Cart\\EventDispatcher\\CartLineEventDispatcher', [], [], '', false);
     $cartFactory = $this->getMock('Elcodi\\Component\\Cart\\Factory\\CartFactory', ['create'], [$emptyMoneyWrapper]);
     $cartLineFactory = $this->getMock('Elcodi\\Component\\Cart\\Factory\\CartLineFactory', ['create'], [$emptyMoneyWrapper]);
     $cartLineFactory->expects($this->any())->method('create')->willReturn(new CartLine());
     $cartManager = $this->getMockBuilder('Elcodi\\Component\\Cart\\Services\\CartManager')->setMethods(['increaseCartLineQuantity'])->setConstructorArgs([$cartEventDispatcher, $cartLineEventDispatcher, $cartFactory, $cartLineFactory])->getMock();
     $cart = $this->getMock('Elcodi\\Component\\Cart\\Entity\\Interfaces\\CartInterface');
     $cartLine = $this->getMock('Elcodi\\Component\\Cart\\Entity\\Interfaces\\CartLineInterface');
     $product = $this->getMock('Elcodi\\Component\\Product\\Entity\\Interfaces\\ProductInterface');
     $variant = $this->getMock('Elcodi\\Component\\Product\\Entity\\Interfaces\\VariantInterface');
     $product->expects($this->any())->method('getId')->willReturn(1);
     $variant->expects($this->any())->method('getId')->willReturn(1);
     $variant->expects($this->any())->method('getProduct')->willReturn($this->getMock('Elcodi\\Component\\Product\\Entity\\Interfaces\\ProductInterface'));
     $cartLine->expects($this->any())->method('getPurchasable')->willReturn($product);
     $cart->expects($this->any())->method('getCartLines')->willReturn(new ArrayCollection([$cartLine]));
     $cartManager->expects($this->never())->method('increaseCartLineQuantity');
     $cartManager->addPurchasable($cart, $variant, 1);
 }
Example #28
0
 /**
  * Configures the options for this type.
  *
  * @param OptionsResolver $resolver The resolver for the options.
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     $resolver->setDefaults(['data_class' => 'Elcodi\\Component\\Currency\\Entity\\Money', 'empty_data' => Money::create(0, $this->defaultCurrencyWrapper->get())]);
 }
Example #29
0
 /**
  * Return a formatted price given the price in an integer format.
  *
  * Takes the currency from CurrencyWrapper
  *
  * @param int $value Value
  *
  * @return string The formatted price
  */
 public function printMoneyFromValue($value)
 {
     $targetCurrency = $this->currencyWrapper->get();
     $money = Money::create($value, $targetCurrency);
     return $this->printMoney($money);
 }
Example #30
0
 /**
  * {@inheritDoc}
  */
 public function load(ObjectManager $manager)
 {
     /**
      * @var CouponFactory     $couponFactory
      * @var CurrencyInterface $currencyDollar
      * @var CurrencyInterface $currencyEuro
      */
     $couponFactory = $this->getFactory('coupon');
     $currencyDollar = $this->getReference('currency-USD');
     $currencyEuro = $this->getReference('currency-EUR');
     /**
      * Coupon with 12% of discount
      *
      * Valid from now, no expiration time
      *
      * Customer only can redeem it 5 times in all life
      *
      * @var CouponInterface $couponPercent
      */
     $couponPercent = $couponFactory->create();
     $couponPercent->setCode('percent')->setName('12 percent discount')->setType(ElcodiCouponTypes::TYPE_PERCENT)->setDiscount(12)->setCount(5)->setEnabled(true);
     $manager->persist($couponPercent);
     $this->addReference('coupon-percent', $couponPercent);
     /**
      * Coupon with 5 euros of discount
      *
      * Valid from now, no expiration time
      *
      * Can be redeemed many times
      *
      * @var CouponInterface $couponAmountEuro
      */
     $couponAmountEuro = $couponFactory->create();
     $couponAmountEuro->setCode('5euros')->setName('5 euros discount')->setType(ElcodiCouponTypes::TYPE_AMOUNT)->setPrice(Money::create(500, $currencyEuro))->setEnabled(true);
     $manager->persist($couponAmountEuro);
     $this->addReference('coupon-amount-euro', $couponAmountEuro);
     /**
      * Coupon with 5 euros of discount
      *
      * Valid from now, no expiration time
      *
      * Only 20 available
      *
      * @var CouponInterface $couponAmountDollar
      */
     $couponAmountDollar = $couponFactory->create();
     $couponAmountDollar->setCode('10dollars')->setName('10 dollars discount')->setType(ElcodiCouponTypes::TYPE_AMOUNT)->setPrice(Money::create(1000, $currencyDollar))->setCount(20)->setEnabled(true);
     $manager->persist($couponAmountDollar);
     $this->addReference('coupon-amount-dollar', $couponAmountDollar);
     /**
      * Automatic coupon applies 50% to big spenders
      *
      * Valid from now, no expiration time
      *
      * @var CouponInterface $couponBigSpenders
      */
     $couponBigSpenders = $couponFactory->create();
     /**
      * @var RuleInterface $ruleBigSpenders
      */
     $ruleBigSpenders = $this->getReference('rule-big-spender');
     $couponBigSpenders->setCode('bigspender')->setName('50% discount')->setType(ElcodiCouponTypes::TYPE_PERCENT)->setDiscount(50)->setRule($ruleBigSpenders)->setEnforcement(ElcodiCouponTypes::ENFORCEMENT_AUTOMATIC)->setEnabled(true);
     $manager->persist($couponBigSpenders);
     $this->addReference('coupon-big-spender', $couponBigSpenders);
     $manager->flush();
 }