Sonata\Component\Transformer\InvoiceTransformer::transformFromOrder PHP Method

transformFromOrder() public method

Transforms an order into an invoice.
public transformFromOrder ( Sonata\Component\Order\OrderInterface $order, Sonata\Component\Invoice\InvoiceInterface $invoice )
$order Sonata\Component\Order\OrderInterface
$invoice Sonata\Component\Invoice\InvoiceInterface
    public function transformFromOrder(OrderInterface $order, InvoiceInterface $invoice)
    {
        $event = new OrderTransformEvent($order);
        $this->eventDispatcher->dispatch(TransformerEvents::PRE_ORDER_TO_INVOICE_TRANSFORM, $event);
        $invoice->setName($order->getBillingName());
        $invoice->setAddress1($order->getBillingAddress1());
        $invoice->setAddress2($order->getBillingAddress2());
        $invoice->setAddress3($order->getBillingAddress3());
        $invoice->setCity($order->getBillingCity());
        $invoice->setCountry($order->getBillingCountryCode());
        $invoice->setPostcode($order->getBillingPostcode());
        $invoice->setEmail($order->getBillingEmail());
        $invoice->setFax($order->getBillingFax());
        $invoice->setMobile($order->getBillingMobile());
        $invoice->setPhone($order->getBillingPhone());
        $invoice->setReference($order->getReference());
        $invoice->setCurrency($order->getCurrency());
        $invoice->setCustomer($order->getCustomer());
        $invoice->setTotalExcl($order->getTotalExcl());
        $invoice->setTotalInc($order->getTotalInc());
        $invoice->setPaymentMethod($order->getPaymentMethod());
        $invoice->setLocale($order->getLocale());
        foreach ($order->getOrderElements() as $orderElement) {
            $invoiceElement = $this->createInvoiceElementFromOrderElement($orderElement);
            $invoiceElement->setInvoice($invoice);
            $invoice->addInvoiceElement($invoiceElement);
        }
        if ($order->getDeliveryCost() > 0) {
            $this->addDelivery($invoice, $order);
        }
        $invoice->setStatus(InvoiceInterface::STATUS_OPEN);
        $event = new InvoiceTransformEvent($invoice);
        $this->eventDispatcher->dispatch(TransformerEvents::POST_ORDER_TO_INVOICE_TRANSFORM, $event);
    }

Usage Example

 public function testTransformFromOrder()
 {
     $customer = $this->getMock('Sonata\\Component\\Customer\\CustomerInterface');
     $orderElement = $this->getMock('Sonata\\Component\\Order\\OrderElementInterface');
     $orderElement->expects($this->once())->method('getDescription');
     $orderElement->expects($this->once())->method('getDesignation');
     $orderElement->expects($this->once())->method('getPrice')->will($this->returnValue(42));
     $orderElement->expects($this->once())->method('getQuantity')->will($this->returnValue(3));
     $orderElement->expects($this->once())->method('getVatRate');
     $order = $this->getMock('Sonata\\Component\\Order\\OrderInterface');
     $order->expects($this->once())->method('getOrderElements')->will($this->returnValue(array($orderElement)));
     $order->expects($this->once())->method('getCustomer')->will($this->returnValue($customer));
     $order->expects($this->once())->method('getBillingAddress1');
     $order->expects($this->once())->method('getBillingAddress2');
     $order->expects($this->once())->method('getBillingAddress3');
     $order->expects($this->once())->method('getBillingCity');
     $order->expects($this->once())->method('getBillingCountryCode');
     $order->expects($this->once())->method('getBillingPostcode');
     $order->expects($this->once())->method('getBillingEmail');
     $order->expects($this->once())->method('getBillingMobile');
     $order->expects($this->once())->method('getBillingFax');
     $order->expects($this->once())->method('getBillingPhone');
     $order->expects($this->once())->method('getReference');
     $currency = new Currency();
     $currency->setLabel('EUR');
     $order->expects($this->once())->method('getCurrency')->will($this->returnValue($currency));
     $order->expects($this->once())->method('getTotalExcl');
     $order->expects($this->once())->method('getTotalInc');
     $invoice = $this->getMock('Sonata\\Component\\Invoice\\InvoiceInterface');
     $invoiceElement = $this->getMock('Sonata\\Component\\Invoice\\InvoiceElementInterface');
     $invoiceElementManager = $this->getMock('Sonata\\Component\\Invoice\\InvoiceElementManagerInterface');
     $invoiceElementManager->expects($this->once())->method('create')->will($this->returnValue($invoiceElement));
     $deliveryPool = new DeliveryPool();
     $eventDispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface');
     $invoiceTransformer = new InvoiceTransformer($invoiceElementManager, $deliveryPool, $eventDispatcher);
     $invoiceTransformer->transformFromOrder($order, $invoice);
 }