Skip to content

Commit

Permalink
#3 Added AbstractInvoiceRepository, added doctrine extensions.
Browse files Browse the repository at this point in the history
    - Added 'updateTotals()' method to abstractinvoicerepository.
  • Loading branch information
JoeZ99 committed Dec 11, 2011
1 parent 9c2e188 commit 6bc261b
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function registerBundles()
new Siwapp\ProductBundle\SiwappProductBundle(),
new Siwapp\ConfigBundle\SiwappConfigBundle(),
new Siwapp\CoreBundle\SiwappCoreBundle(),
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
);

if (in_array($this->getEnvironment(), array('dev', 'test'))) {
Expand Down
3 changes: 3 additions & 0 deletions app/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
'Monolog' => __DIR__.'/../vendor/monolog/src',
'Assetic' => __DIR__.'/../vendor/assetic/src',
'Metadata' => __DIR__.'/../vendor/metadata/src',
'Stof' => __DIR__.'/../vendor/bundles',
'Gedmo' => __DIR__.'/../vendor/gedmo-doctrine-extensions/lib',

));
$loader->registerPrefixes(array(
'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib',
Expand Down
5 changes: 5 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@ swiftmailer:
jms_security_extra:
secure_controllers: true
secure_all_services: false

stof_doctrine_extensions:
orm:
default:
sluggable: true
10 changes: 9 additions & 1 deletion deps
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@
[DoctrineFixturesBundle]
git=http://github.com/symfony/DoctrineFixturesBundle.git
target=/bundles/Symfony/Bundle/DoctrineFixturesBundle


[gedmo-doctrine-extensions]
git=git://github.com/l3pp4rd/DoctrineExtensions.git
version=1.0.0

[DoctrineExtensionsBundle]
git=git://github.com/stof/StofDoctrineExtensionsBundle.git
version=1.0.0
target=/bundles/Stof/DoctrineExtensionsBundle
7 changes: 3 additions & 4 deletions src/Siwapp/CoreBundle/Entity/AbstractInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,8 @@ public function calculate($field, $rounded=false)
default:
foreach($this->getItems() as $item)
{
$method = 'get'.Inflector::camelize($field);
$val += $item->method();
$method = Inflector::camelize('get_'.$field);
$val += $item->$method();
}
break;
}
Expand All @@ -565,14 +565,13 @@ public function setAmounts()
$this->setTaxAmount($this->calculate('tax_amount'));
$rounded_gross = round(
$this->getNetAmount() + $this->getTaxAmount(),
PropertyTable::get('currency_decimals', 2)
$this->getDecimals()
);
$this->setGrossAmount($rounded_gross);

return $this;
}



/** *********** LIFECYCLE CALLBACKS ************* */

Expand Down
123 changes: 123 additions & 0 deletions src/Siwapp/CoreBundle/Entity/AbstractItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Util\Inflector;
use Gedmo\Sluggable\Util\Urlizer as Urlizer;
use Symfony\Component\Validator\Constraints as Assert;

/**
Expand Down Expand Up @@ -162,4 +164,125 @@ public function getTaxes()
{
return $this->taxes;
}

/**
* Get base amount
*
* @return float gross price of the item (times quantity)
*/
public function getBaseAmount()
{
return $this->unitary_cost*$this->quantity;
}

/**
* Get net amount
*
* @return float price with discount
*/
public function getNetAmount()
{
return $this->getBaseAmount() - $this->getDiscountAmount();
}

/**
* Get discount amount
*
* @return float amount to discount
*/
public function getDiscountAmount()
{
return $this->getBaseAmount() * $this->discount / 100;
}

/**
* Get tax amount
*
* @param array tax_names. if present, returns the amount for those taxes
* @return float amount to tax
*/
public function getTaxAmount($tax_names = null)
{
return $this->getNetAmount() * $this->getTaxesPercent($tax_names) / 100;
}

/**
* Get gross amount
*
* @return float amount to pay after discount and taxes
*/
public function getGrossAmount()
{
return $this->getNetAmount() + $this->getTaxAmount();
}

/**
* Get taxes percent
*
* @param tax_names array if present shows only percent of those taxes
* @return integer total percent of taxes to apply
*/
public function getTaxesPercent($tax_names = null)
{
$tax_names = $tax_names ?
(is_array($tax_names) ?
array_map(array('Urlizer', 'urlize'), $tax_names):
array(Urlizer::urlize($tax_names))) :
null;

$total = 0;
foreach($this->getTaxes() as $tax){
if(!$tax_names ||
in_array(Urlizer::urlize($tax->getName()), $tax_names))
{
$total += $tax->getValue();
}
}
return $total;
}

/**
* Try to capture a "getTaxAmountTAXNAME" method.
* This is to be able to use 'invoice.tax_amount_TAXNAME' in the templates
*
* @author JoeZ99 <jzarate@gmail.com>
*/
public function __call($data, $argument)
{
if(strpos($data, 'getTaxAmount') === 0 && strlen($data)>12)
{
$tax_name = substr($data, 12);
return $this->getTaxAmount($tax_name);
}
return false;
}

/**
* Again, capture hipothetical {{invoice.base_amount}} and the like
*
*/
public function __get($name)
{
if(in_array($name, array('base_amount', 'discount_amount', 'net_amount', 'tax_amount', 'gross_amount')))
{
$m = Inflector::camelize("get_{$name}");
return $this->$m();
}
return false;
}

/**
* Twig template system needs this to answer true for the specified properties
*/
public function __isset($name)
{
if(in_array($name, array('base_amount', 'discount_amount', 'net_amount', 'tax_amount', 'gross_amount')))
{
return true;
}
return false;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class AbstractInvoiceRepository extends EntityRepository
* @param ArrayCollection of entities
* @return AbstractInvoiceRepository
**/
public static function updateTotals(ArrayCollection $entities = new ArrayCollection())
public function updateTotals()
{
echo $this->getEntityName();
$em = $this->getEntityManager();
foreach($entity in $entities)
foreach($em->createQuery('SELECT i from '.$this->getEntityName().' i')->getResult() as $entity)
{
$entity->setAmounts();
$em->persist($entity);
Expand Down
5 changes: 2 additions & 3 deletions src/Siwapp/InvoiceBundle/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ class DefaultController extends Controller
public function sillyAction()
{
$repo = $this->getDoctrine()->getEntityManager()
->getRepository('SiwappCoreBundle:AbstractInvoice');
print_r($repo);
echo $repo->findByUpdateTotals();
->getRepository('SiwappInvoiceBundle:Invoice');
$repo->updateTotals();
return array();
}
/**
Expand Down
2 changes: 1 addition & 1 deletion src/Siwapp/InvoiceBundle/Entity/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @ORM\index(name="cstml_idx", columns={"customer_email"}),
* @ORM\index(name="cntct_idx", columns={"contact_person"})
* })
* @ORM\Entity(repositoryClass="Siwapp\InvoiceBundle\Entity\InvoiceRepository")
* @ORM\Entity(repositoryClass="Siwapp\InvoiceBundle\Repository\InvoiceRepository")
*/
class Invoice extends AbstractInvoice
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php

namespace Siwapp\InvoiceBundle\Entity;
namespace Siwapp\InvoiceBundle\Repository;

use Doctrine\ORM\EntityRepository;

use Siwapp\CoreBundle\Repository\AbstractInvoiceRepository;

/**
* InvoiceRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class InvoiceRepository extends EntityRepository
class InvoiceRepository extends AbstractInvoiceRepository
{
}

0 comments on commit 6bc261b

Please sign in to comment.