Skip to content

Commit

Permalink
- invoices
Browse files Browse the repository at this point in the history
- debug of order states
- events on order payment
- beginning of mail system
  • Loading branch information
philippe-levan committed Jan 12, 2012
1 parent a2754a5 commit 893dcdd
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 8 deletions.
1 change: 1 addition & 0 deletions Controller/OrderController.php
Expand Up @@ -89,6 +89,7 @@ public function displayOrderAction(
$orderHistory->setPriceIncludingVat($order->getPriceIncludingVat());
$orderHistory->setPriceWithoutVat($order->getPriceWithoutVat());
$order->addOrderHistory($orderHistory);
$em->flush(); // hack in order to have an id in the orderHistory...
$order->setStateFromHistory();

// build transaction
Expand Down
22 changes: 22 additions & 0 deletions Entity/Invoice.php
Expand Up @@ -9,6 +9,11 @@ class Invoice
*/
private $reference;

/**
* @var string $contentHtml
*/
private $contentHtml;

/**
* @var datetime $createdAt
*/
Expand Down Expand Up @@ -50,6 +55,22 @@ public function getReference()
return $this->reference;
}

/**
* @param string $contentHtml
*/
public function setContentHtml($contentHtml)
{
$this->contentHtml = $contentHtml;
}

/**
* @return string
*/
public function getContentHtml()
{
return $this->contentHtml;
}

/**
* Set createdAt
*
Expand Down Expand Up @@ -119,4 +140,5 @@ public function getOrder()
{
return $this->order;
}

}
13 changes: 8 additions & 5 deletions Entity/Order.php
Expand Up @@ -9,16 +9,19 @@ class Order
public function setStateFromHistory()
{
$historyList = $this->getOrderHistoryList();
$currentDate = null;
$currentId = null;
$currentState = null;
$currentDate = null;
foreach ($historyList as $history) {
if (is_null($currentDate)) {
$currentDate = $history->getStateDate();
if (is_null($currentId)) {
$currentId = $history->getId();
$currentState = $history->getState();
}
if ($currentDate < $history->getStateDate()) {
$currentDate = $history->getStateDate();
}
if ($currentId < $history->getId()) {
$currentId = $history->getId();
$currentState = $history->getState();
$currentDate = $history->getStateDate();
}
}
$this->setState($currentState);
Expand Down
34 changes: 34 additions & 0 deletions Event/AbstractEvent.php
@@ -0,0 +1,34 @@
<?php
namespace Kitpages\ShopBundle\Event;

use Symfony\Component\EventDispatcher\Event;

abstract class AbstractEvent extends Event
{
protected $data = array();
protected $isDefaultPrevented = false;

public function preventDefault()
{
$this->isDefaultPrevented = true;
}

public function isDefaultPrevented()
{
return $this->isDefaultPrevented;
}

public function set($key, $val)
{
$this->data[$key] = $val;
}

public function get($key)
{
if (!array_key_exists($key, $this->data)) {
return null;
}
return $this->data[$key];
}

}
10 changes: 10 additions & 0 deletions Event/ShopEvent.php
@@ -0,0 +1,10 @@
<?php

namespace Kitpages\ShopBundle\Event;

use Symfony\Component\EventDispatcher\Event;


class ShopEvent extends AbstractEvent
{
}
9 changes: 9 additions & 0 deletions KitpagesShopEvents.php
@@ -0,0 +1,9 @@
<?php

namespace Kitpages\ShopBundle;

final class KitpagesShopEvents
{
const AFTER_ORDER_PAYED = 'kitpages_shop.event.after_order_payed';
const AFTER_TRANSACTION_REFUSED = 'kitpages_shop.event.after_transaction_refused';
}
54 changes: 52 additions & 2 deletions Model/Cart/OrderManager.php
Expand Up @@ -5,14 +5,20 @@
use Kitpages\ShopBundle\Entity\OrderHistory;
use Kitpages\ShopBundle\Entity\OrderLine;
use Kitpages\ShopBundle\Entity\OrderUser;
use Kitpages\ShopBundle\Entity\Invoice;
use Kitpages\ShopBundle\Model\Cart\CartInterface;
use Kitpages\ShopBundle\Event\ShopEvent;
use Kitpages\ShopBundle\KitpagesShopEvents;

use Kitano\PaymentBundle\Event\PaymentEvent;
use Kitano\PaymentBundle\Model\Transaction;

use Symfony\Component\HttpFoundation\Session;
use Symfony\Bundle\DoctrineBundle\Registry;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Templating\EngineInterface;


class OrderManager
{
Expand All @@ -22,17 +28,27 @@ class OrderManager
/** @var null|LoggerInterface */
protected $logger = null;

/** @var null|\Symfony\Component\Templating\EngineInterface */
protected $templating = null;

/** @var null|\Symfony\Component\EventDispatcher\EventDispatcherInterface */
protected $dispatcher = null;

public function __construct(
Registry $doctrine,
LoggerInterface $logger,
CartManagerInterface $cartManager,
$isCartIncludingVat
$isCartIncludingVat,
EngineInterface $templating,
EventDispatcherInterface $dispatcher
)
{
$this->doctrine = $doctrine;
$this->logger = $logger;
$this->cartManager = $cartManager;
$this->isCartIncludingVat = $isCartIncludingVat;
$this->templating = $templating;
$this->dispatcher = $dispatcher;
}


Expand Down Expand Up @@ -182,6 +198,9 @@ public function paymentListener(PaymentEvent $event)
if (! $order instanceof Order) {
throw new Exception("unknown order for transactionId=".$transaction->getId());
}
if ($order->getState() != OrderHistory::STATE_PAYED) {
$this->cartManager->getCart()->emptyCart();
}
if ($order->getState() != OrderHistory::STATE_READY_TO_PAY) {
$this->logger->info("orderId=".$order->getId()." not updated by payment process because state is not ready_to_pay");
return;
Expand All @@ -202,7 +221,31 @@ public function paymentListener(PaymentEvent $event)
$em->flush();
// empty cart
$this->cartManager->getCart()->emptyCart();
// TODO : generate invoice
// generate invoice, id generation
$invoice = new Invoice();
$invoice->setOrder($order);
$invoice->setReference(null);
$em->persist($invoice);
$em->flush();
// invoice generation step 2 (html and reference generation)
$invoice->setReference('I-'.$invoice->getId());
$invoiceHtmlContent = $this->templating->render(
"KitpagesShopBundle:Invoice:table.html.twig",
array(
"order" => $order,
"transaction" => $transaction,
"invoice" => $invoice
)
);
$invoice->setContentHtml($invoiceHtmlContent);
$em->flush();

$event = new ShopEvent();
$event->set("transaction", $transaction);
$event->set("order", $order);
$event->set("invoice", $invoice);
$event->set("orderHistory", $orderHistory);
$this->dispatcher->dispatch(KitpagesShopEvents::AFTER_ORDER_PAYED, $event);

return;
}
Expand All @@ -218,5 +261,12 @@ public function paymentListener(PaymentEvent $event)
$order->addOrderHistory($orderHistory);
$order->setStateFromHistory();
$em->flush();

$event = new ShopEvent();
$event->set("transaction", $transaction);
$event->set("order", $order);
$event->set("orderHistory", $orderHistory);
$this->dispatcher->dispatch(KitpagesShopEvents::AFTER_TRANSACTION_REFUSED, $event);

}
}
4 changes: 3 additions & 1 deletion Resources/config/doctrine/Invoice.orm.xml
Expand Up @@ -10,7 +10,9 @@
<generator strategy="AUTO" />
</id>

<field name="reference" column="reference" type="string" unique="true" length="250"/>
<field name="reference" column="reference" type="string" unique="true" length="250" nullable="true"/>

<field name="contentHtml" column="content_html" type="string" nullable="true"/>

<!-- dates -->
<field name="createdAt" type="datetime">
Expand Down
8 changes: 8 additions & 0 deletions Resources/config/services.xml
Expand Up @@ -18,9 +18,17 @@
<argument type="service" id="logger"/>
<argument type="service" id="kitpages_shop.cartManager" />
<argument>%kitpages_shop.is_cart_including_vat%</argument>
<argument type="service" id="templating"/>
<argument type="service" id="event_dispatcher"/>

<tag name="kernel.event_listener" event="kitano_payment.event.after_payment_notification" method="paymentListener" />
<tag name="kernel.event_listener" event="kitano_payment.event.after_back_to_shop" method="paymentListener" />
</service>

<service id="kitpages_shop.twig.extension.shopExtension" class="Kitpages\ShopBundle\Twig\Extension\ShopExtension">
<tag name="twig.extension" />
</service>

</services>

</container>
59 changes: 59 additions & 0 deletions Resources/views/Invoice/table.html.twig
@@ -0,0 +1,59 @@
<table>
<thead>
<tr>
<td colspan="4">
<h1>{{ "Invoice" | trans }} {{invoice.reference}}</h1>
<span>{{ "Date" | trans}} : {{ order.stateDate | date("m/d/Y") }}</span><br/>
Company
</td>
</tr>
<tr>
<td colspan="4">
<h2>Client</h2>
{{order.invoiceUser.firstName}} {{order.invoiceUser.lastName}}<br/>
{{order.invoiceUser.email}}<br/>
{{order.invoiceUser.countryCode|kit_shop_country(app.session.locale)}}
</td>
</tr>
<tr>
<td colspan="4">
<h2>Invoice content</h2>
</td>
</tr>
</thead>
<tbody>
<tr>
<td>Ref</td>
<td>Name</td>
<td>Quantity</td>
<td>Price</td>
</tr>
</tbody>
<tbody>
{% for line in order.orderLineList %}
<tr>
<td>{{line.shopReference}}</td>
<td>
<h4>{{line.shopName}}</h4>
<div>{{line.shopDescription}}</div>
</td>
<td>{{line.quantity}}</td>
<td>{{ line.priceIncludingVat }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td colspan="3">Total</td>
<td>
{% if (order.priceIncludingVat == order.priceWithoutVat) %}
{{order.priceIncludingVat}}
{% else %}
Without VAT : {{order.priceWithoutVat}}<br/>
VAT : {{order.priceIncludingVat - order.priceWithoutVat}}<br/>
Including VAT : {{order.priceIncludingVat}}
{% endif %}
</td>
</tr>
</tfoot>
</table>
24 changes: 24 additions & 0 deletions Resources/views/Mail/layout.html.twig
@@ -0,0 +1,24 @@
<html>
<head>
<title>Email</title>
</head>
<body>
<table width="600" style="width: 600px">
<thead>
<tr>
<td>Mail header</td>
</tr>
</thead>
<tbody>
<tr>
<td>{% block body %}{% endblock %}</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Mail footer</td>
</tr>
</tfoot>
</table>
</body>
</html>
40 changes: 40 additions & 0 deletions Twig/Extension/ShopExtension.php
@@ -0,0 +1,40 @@
<?php
namespace Kitpages\ShopBundle\Twig\Extension;

use Symfony\Component\Locale\Locale;

class ShopExtension extends \Twig_Extension
{

public static function countryName($value, $locale)
{
$countryList = \Symfony\Component\Locale\Locale::getDisplayCountries($locale);
if (!in_array($value, $countryList)) {
return $countryList[$value];
} else {
return $value;
}
}

/**
* Returns a list of filters to add to the existing list.
*
* @return array An array of filters
*/
public function getFilters()
{
return array(
'kit_shop_country' => new \Twig_Filter_Function('Kitpages\ShopBundle\Twig\Extension\ShopExtension::countryName')
);
}

/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'kitpages_shop_shop';
}
}

0 comments on commit 893dcdd

Please sign in to comment.