From 6986a52271646bddcf1cf8ff18f499ff328b8c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20J=C4=99drzejewski?= Date: Sun, 3 Feb 2013 20:14:53 +0100 Subject: [PATCH] Introduce initial order entities * Add FOSUserBundle as dependency. * Add SyliusInventoryBundle as dependency. --- Entity/Order.php | 301 ++++++++++++++++++ Entity/Variant.php | 11 +- Form/Type/OrderType.php | 38 +++ Resources/config/doctrine/Order.orm.xml | 36 +++ Resources/config/validation.xml | 9 + composer.json | 22 +- .../Sylius/Bundle/CoreBundle/Entity/Order.php | 139 ++++++++ .../Bundle/CoreBundle/Entity/Variant.php | 15 + .../Bundle/CoreBundle/Form/Type/OrderType.php | 37 +++ .../Form/Type/ShippingMethodType.php | 2 +- .../Type/{Variant.php => VariantType.php} | 0 11 files changed, 598 insertions(+), 12 deletions(-) create mode 100644 Entity/Order.php create mode 100644 Form/Type/OrderType.php create mode 100644 Resources/config/doctrine/Order.orm.xml create mode 100644 spec/Sylius/Bundle/CoreBundle/Entity/Order.php create mode 100644 spec/Sylius/Bundle/CoreBundle/Form/Type/OrderType.php rename spec/Sylius/Bundle/CoreBundle/Form/Type/{Variant.php => VariantType.php} (100%) diff --git a/Entity/Order.php b/Entity/Order.php new file mode 100644 index 000000000..7382b78a4 --- /dev/null +++ b/Entity/Order.php @@ -0,0 +1,301 @@ + + */ +class Order extends BaseOrder +{ + // Labels for tax and shipping adjustments. + const TAX_ADJUSTMENT = 'Tax'; + const SHIPPING_ADJUSTMENT = 'Shipping'; + + /** + * User. + * + * @var UserInterface + */ + protected $user; + + /** + * Order shipping address. + * + * @var AddressInterface + */ + protected $shippingAddress; + + /** + * Order billing address. + * + * @var AddressInterface + */ + protected $billingAddress; + + /** + * Shipments for this order. + * + * @var Collection + */ + protected $shipments; + + /** + * Inventory units. + * + * @var Collection + */ + protected $inventoryUnits; + + /** + * Constructor. + */ + public function __construct() + { + parent::__construct(); + + $this->inventoryUnits = new ArrayCollection(); + $this->shipments = new ArrayCollection(); + } + + /** + * Get user. + * + * @return UserInterface + */ + public function getUser() + { + return $this->user; + } + + /** + * Set user. + * + * @param UserInterface $user + */ + public function setUser(UserInterface $user) + { + $this->user = $user; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getShippingAddress() + { + return $this->shippingAddress; + } + + /** + * {@inheritdoc} + */ + public function setShippingAddress(AddressInterface $address) + { + $this->shippingAddress = $address; + } + + /** + * {@inheritdoc} + */ + public function getBillingAddress() + { + return $this->billingAddress; + } + + /** + * {@inheritdoc} + */ + public function setBillingAddress(AddressInterface $address) + { + $this->billingAddress = $address; + } + + /** + * Get the tax total. + * + * @return float + */ + public function getTaxTotal() + { + $taxTotal = 0; + + foreach ($this->getTaxAdjustments() as $adjustment) { + $taxTotal += $adjustment->getAmount(); + } + + return $taxTotal; + } + + /** + * Get all tax adjustments. + * + * @return Collection + */ + public function getTaxAdjustments() + { + return $this->adjustments->filter(function (AdjustmentInterface $adjustment) { + return Order::TAX_ADJUSTMENT === $adjustment->getLabel(); + }); + } + + /** + * Remove all tax adjustments. + */ + public function removeTaxAdjustments() + { + foreach ($this->getTaxAdjustments() as $adjustment) { + $this->removeAdjustment($adjustment); + } + + return $this; + } + + /** + * Get shipping total. + * + * @return float + */ + public function getShippingTotal() + { + $shippingTotal = 0; + + foreach ($this->getShippingAdjustments() as $adjustment) { + $shippingTotal += $adjustment->getAmount(); + } + + return $shippingTotal; + } + + /** + * Get all shipping adjustments. + * + * @return Collection + */ + public function getShippingAdjustments() + { + return $this->adjustments->filter(function (AdjustmentInterface $adjustment) { + return Order::SHIPPING_ADJUSTMENT === $adjustment->getLabel(); + }); + } + + /** + * Remove all shipping adjustments. + */ + public function removeShippingAdjustments() + { + foreach ($this->getShippingAdjustments() as $adjustment) { + $this->removeAdjustment($adjustment); + } + + return $this; + } + + /** + * Get all inventory units. + * + * @return Collection + */ + public function getInventoryUnits() + { + return $this->inventoryUnits; + } + + /** + * Add inventory unit. + * + * @param InventoryUnitInterface $unit + */ + public function addInventoryUnit(InventoryUnitInterface $unit) + { + if (!$this->inventoryUnits->contains($unit)) { + $unit->setOrder($this); + $this->inventoryUnits->add($unit); + } + + return $this; + } + + /** + * Remove inventory unit. + * + * @param InventoryUnitInterface $unit + */ + public function removeInventoryUnit(InventoryUnitInterface $unit) + { + if ($this->inventoryUnits->contains($unit)) { + $unit->setOrder(null); + $this->inventoryUnits->removeElement($unit); + } + + return $this; + } + + /** + * Get all shipments associated with this order. + * + * @return Collection + */ + public function getShipments() + { + return $this->shipments; + } + + /** + * Add a shipment. + * + * @param ShipmentInterface $shipment + */ + public function addShipment(ShipmentInterface $shipment) + { + if (!$this->hasShipment($shipment)) { + $shipment->setOrder($this); + $this->shipments->add($shipment); + } + } + + /** + * Remove shipment. + * + * @param ShipmentInterface $shipment + */ + public function removeShipment(ShipmentInterface $shipment) + { + if ($this->hasShipment($shipment)) { + $shipment->setOrder(null); + $this->shipments->removeElement($shipment); + } + } + + /** + * Has shipment? + * + * @param ShipmentInterface $shipment + * + * @return Boolean + */ + public function hasShipment(ShipmentInterface $shipment) + { + return $this->shipments->contains($shipment); + } +} diff --git a/Entity/Variant.php b/Entity/Variant.php index 10df19b1a..4aa10ab62 100644 --- a/Entity/Variant.php +++ b/Entity/Variant.php @@ -13,6 +13,7 @@ use Sylius\Bundle\AssortmentBundle\Entity\Variant\Variant as BaseVariant; use Sylius\Bundle\AssortmentBundle\Model\Variant\VariantInterface; +use Sylius\Bundle\SalesBundle\Model\SellableInterface; use Sylius\Bundle\TaxationBundle\Model\TaxCategoryInterface; use Sylius\Bundle\TaxationBundle\Model\TaxableInterface; @@ -21,7 +22,7 @@ * * @author Paweł Jędrzejewski */ -class Variant extends BaseVariant +class Variant extends BaseVariant implements SellableInterface { /** * The variant price. @@ -61,4 +62,12 @@ public function setDefaults(VariantInterface $masterVariant) $this->setPrice($masterVariant->getPrice()); } + + /** + * {@inheritdoc} + */ + public function getSellableName() + { + return $this->product->getName(); + } } diff --git a/Form/Type/OrderType.php b/Form/Type/OrderType.php new file mode 100644 index 000000000..315e4544d --- /dev/null +++ b/Form/Type/OrderType.php @@ -0,0 +1,38 @@ + + */ +class OrderType extends BaseOrderType +{ + /** + * {@inheritdoc} + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + parent::buildForm($builder, $options); + + $builder + ->add('shippingAddress', 'sylius_address') + ->add('billingAddress', 'sylius_address') + ; + } +} + diff --git a/Resources/config/doctrine/Order.orm.xml b/Resources/config/doctrine/Order.orm.xml new file mode 100644 index 000000000..104ccb8c1 --- /dev/null +++ b/Resources/config/doctrine/Order.orm.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Resources/config/validation.xml b/Resources/config/validation.xml index 95b06ef3b..d42e6780e 100644 --- a/Resources/config/validation.xml +++ b/Resources/config/validation.xml @@ -16,6 +16,15 @@ xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd"> + + + + + + + + + diff --git a/composer.json b/composer.json index 417e84b10..26dbab6cb 100644 --- a/composer.json +++ b/composer.json @@ -17,17 +17,19 @@ ], "minimum-stability": "dev", "require": { - "php": ">=5.3.3", + "php": ">=5.3.3", - "symfony/framework-bundle": "~2.1", - "sylius/assortment-bundle": "0.1.*", - "sylius/taxation-bundle": "0.1.*", - "sylius/shipping-bundle": "0.1.*", - "sylius/addressing-bundle": "0.1.*", - "sylius/sales-bundle": "0.1.*", - "jms/serializer-bundle": "1.0.*", - "fsc/hateoas-bundle": "0.3.x-dev", - "fzaninotto/faker": "1.1.*" + "symfony/framework-bundle": ">=2.2,<2.3-dev", + "sylius/assortment-bundle": "0.1.*", + "sylius/taxation-bundle": "0.1.*", + "sylius/shipping-bundle": "0.1.*", + "sylius/addressing-bundle": "0.1.*", + "sylius/sales-bundle": "0.1.*", + "sylius/inventory-bundle": "0.1.*", + "jms/serializer-bundle": "1.0.*", + "friendsofsymfony/user-bundle": "1.3.1", + "fsc/hateoas-bundle": "0.3.x-dev", + "fzaninotto/faker": "1.1.*" }, "require-dev": { "phpspec/phpspec2": "dev-develop", diff --git a/spec/Sylius/Bundle/CoreBundle/Entity/Order.php b/spec/Sylius/Bundle/CoreBundle/Entity/Order.php new file mode 100644 index 000000000..5e115e6cd --- /dev/null +++ b/spec/Sylius/Bundle/CoreBundle/Entity/Order.php @@ -0,0 +1,139 @@ + + */ +class Order extends ObjectBehavior +{ + function it_should_be_initializable() + { + $this->shouldHaveType('Sylius\Bundle\CoreBundle\Entity\Order'); + } + + function it_should_implement_Sylius_order_interface() + { + $this->shouldImplement('Sylius\Bundle\SalesBundle\Model\OrderInterface'); + } + + function it_should_extend_Sylius_order_mapped_superclass() + { + $this->shouldHaveType('Sylius\Bundle\SalesBundle\Entity\Order'); + } + + function it_should_not_have_user_defined_by_default() + { + $this->getUser()->shouldReturn(null); + } + + /** + * @param FOS\UserBundle\Model\UserInterface $user + */ + function it_should_allow_defining_user($user) + { + $this->setUser($user); + $this->getUser()->shouldReturn($user); + } + + function it_should_not_have_shipping_address_by_default() + { + $this->getShippingAddress()->shouldReturn(null); + } + + /** + * @param Sylius\Bundle\AddressingBundle\Model\AddressInterface $address + */ + function it_should_allow_defining_shipping_address($address) + { + $this->setShippingAddress($address); + $this->getShippingAddress()->shouldReturn($address); + } + + function it_should_not_have_billing_address_by_default() + { + $this->getBillingAddress()->shouldReturn(null); + } + + /** + * @param Sylius\Bundle\AddressingBundle\Model\AddressInterface $address + */ + function it_should_allow_defining_billing_address($address) + { + $this->setBillingAddress($address); + $this->getBillingAddress()->shouldReturn($address); + } + + function it_should_initialize_inventory_units_collection_by_default() + { + $this->getInventoryUnits()->shouldHaveType('Doctrine\Common\Collections\Collection'); + } + + /** + * @param Sylius\Bundle\InventoryBundle\Model\InventoryUnitInterface $unit + */ + function it_should_add_inventory_units_properly($unit) + { + $unit->setOrder($this)->shouldBeCalled(); + $this->addInventoryUnit($unit); + } + + /** + * @param Sylius\Bundle\InventoryBundle\Model\InventoryUnitInterface $unit + */ + function it_should_remove_inventory_units_properly($unit) + { + $unit->setOrder($this)->shouldBeCalled(); + $this->addInventoryUnit($unit); + + $unit->setOrder(null)->shouldBeCalled(); + $this->removeInventoryUnit($unit); + } + + function it_should_initialize_shipments_collection_by_default() + { + $this->getShipments()->shouldHaveType('Doctrine\Common\Collections\Collection'); + } + + /** + * @param Sylius\Bundle\ShippingBundle\Model\ShipmentInterface $shipment + */ + function it_should_add_shipment_properly($shipment) + { + $this->hasShipment($shipment)->shouldReturn(false); + + $shipment->setOrder($this)->shouldBeCalled(); + $this->addShipment($shipment); + + $this->hasShipment($shipment)->shouldReturn(true); + } + + /** + * @param Sylius\Bundle\ShippingBundle\Model\ShipmentInterface $shipment + */ + function it_should_remove_shipment_properly($shipment) + { + $shipment->setOrder($this)->shouldBeCalled(); + $this->addShipment($shipment); + + $this->hasShipment($shipment)->shouldReturn(true); + + $shipment->setOrder(null)->shouldBeCalled(); + $this->removeShipment($shipment); + + $this->hasShipment($shipment)->shouldReturn(false); + } +} diff --git a/spec/Sylius/Bundle/CoreBundle/Entity/Variant.php b/spec/Sylius/Bundle/CoreBundle/Entity/Variant.php index 4726ce827..526437e67 100644 --- a/spec/Sylius/Bundle/CoreBundle/Entity/Variant.php +++ b/spec/Sylius/Bundle/CoreBundle/Entity/Variant.php @@ -20,6 +20,21 @@ function it_should_be_initializable() $this->shouldHaveType('Sylius\Bundle\CoreBundle\Entity\Variant'); } + function it_should_implement_Sylius_product_variant_interface() + { + $this->shouldImplement('Sylius\Bundle\AssortmentBundle\Model\Variant\VariantInterface'); + } + + function it_should_extend_Sylius_product_variant_mapped_superclass() + { + $this->shouldHaveType('Sylius\Bundle\AssortmentBundle\Entity\Variant\Variant'); + } + + function it_should_implement_Sylius_sellable_interface() + { + $this->shouldImplement('Sylius\Bundle\SalesBundle\Model\SellableInterface'); + } + function it_should_not_have_price_by_default() { $this->getPrice()->shouldReturn(null); diff --git a/spec/Sylius/Bundle/CoreBundle/Form/Type/OrderType.php b/spec/Sylius/Bundle/CoreBundle/Form/Type/OrderType.php new file mode 100644 index 000000000..bbee8548d --- /dev/null +++ b/spec/Sylius/Bundle/CoreBundle/Form/Type/OrderType.php @@ -0,0 +1,37 @@ +beConstructedWith('Order'); + } + + function it_should_be_initializable() + { + $this->shouldHaveType('Sylius\Bundle\CoreBundle\Form\Type\OrderType'); + } + + function it_should_be_a_form_type() + { + $this->shouldImplement('Symfony\Component\Form\FormTypeInterface'); + } + + function it_should_extend_Sylius_order_form_type() + { + $this->shouldHaveType('Sylius\Bundle\SalesBundle\Form\Type\OrderType'); + } +} diff --git a/spec/Sylius/Bundle/CoreBundle/Form/Type/ShippingMethodType.php b/spec/Sylius/Bundle/CoreBundle/Form/Type/ShippingMethodType.php index 227f984d4..c264c92ed 100644 --- a/spec/Sylius/Bundle/CoreBundle/Form/Type/ShippingMethodType.php +++ b/spec/Sylius/Bundle/CoreBundle/Form/Type/ShippingMethodType.php @@ -46,7 +46,7 @@ function it_should_build_form_with_zone_choice_field($builder, $factory) { $builder->getFormFactory()->willReturn($factory); - $builder->add('zone', 'sylius_addressing_zone_choice')->shouldBeCalled()->willReturn($builder); + $builder->add('zone', 'sylius_zone_choice')->shouldBeCalled()->willReturn($builder); $this->buildForm($builder, array()); } diff --git a/spec/Sylius/Bundle/CoreBundle/Form/Type/Variant.php b/spec/Sylius/Bundle/CoreBundle/Form/Type/VariantType.php similarity index 100% rename from spec/Sylius/Bundle/CoreBundle/Form/Type/Variant.php rename to spec/Sylius/Bundle/CoreBundle/Form/Type/VariantType.php