Permalink
Browse files

the intended hierarchy model is that all item objects descendants of …

…the 'AbstractItem', instead of having a 'getInvoice', 'getRecurringInvoice', or 'getEstimate' methods have a "getInvoice" method that retrieves their associated invoice , or estimate, or recurring_invoice. This way we can put more code in the abstract classes and less in the inheritance ones.
  • Loading branch information...
1 parent 8a3ea92 commit 9b50d9e854408e0ed681063413eeb4758f3b5f03 @JoeZ99 JoeZ99 committed Dec 24, 2011
View
@@ -49,6 +49,7 @@ doctrine:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
+
# Swiftmailer Configuration
swiftmailer:
transport: %mailer_transport%
@@ -12,7 +12,7 @@
* TODO: Customer and Series relations. Timestampable and Taggable
*
* @ORM\MappedSuperclass
- * @ORM\HasLifecycleCallbacks()
+ * @ORM\HasLifecycleCallbacks
*/
class AbstractInvoice
{
@@ -498,6 +498,46 @@ public function getStatus()
/** ########### CUSTOM METHODS ################## */
+ /** ** RELATIONSHIPS ** */
+
+ /**
+ * addItem
+ * adds an item and recalculcates amounts
+ * it needs to use the 'addNewItem' of the descendant
+ *
+ * @param \Siwapp\CoreBUndle\Entity\AbstractItem $item
+ * @author JoeZ99 <jzarate@gmail.com>
+ */
+ public function addItem(\Siwapp\CoreBundle\Entity\AbstractItem $item)
+ {
+ $this->addNewItem($item);
+ if($item instanceof \Siwapp\InvoiceBundle\Entity\Item)
+ {
+ $item->setInvoice($this);
+ }
+ $this->setAmounts();
+ }
+
+ /**
+ * removeItem
+ * removes an item and recalculcates amounts
+ * it needs to use the 'removeThisItem' of the descendant
+ *
+ * @param mixed $mixed : can be an integer or an item instance
+ * - if an integer, removes the item with
+ * that position in the collection
+ * - if an instance, removes that item
+ * @author JoeZ99 <jzarate@gmail.com>
+ */
+ public function removeItem($mixed)
+ {
+ $this->removeThisItem($mixed);
+ $this->setAmounts();
+
+ }
+
+ /* ** OTHER ** */
+
private $decimals = null;
public function getRoundedAmount($concept = 'gross')
@@ -576,9 +616,10 @@ public function setAmounts()
/** *********** LIFECYCLE CALLBACKS ************* */
/**
+ * @ORM\PreUpdate
* @ORM\PrePersist
*/
- public function preSave()
+ public function preUpdate()
{
$this->checkStatus();
// TODO: check for customer matching and update it accordingly. (calling it's updateCustomer method)
@@ -13,6 +13,7 @@
*
* TODO: Custom methods
* @ORM\MappedSuperclass
+ * @ORM\HasLifecycleCallbacks
*/
class AbstractItem
{
@@ -147,6 +148,29 @@ public function setUnitaryCost($unitary_cost)
/** **************** CUSTOM METHODS ************* */
+ /**
+ * This function is to be implemented on its descendants
+ */
+ public function getInvoice()
+ {
+ }
+
+
+ /**
+ * reCalculate
+ * recalculate invoice's amounts
+ *
+ * @ORM\PreUpdate
+ * @author JoeZ99 <jzarate@gmail.com>
+ */
+ public function reCalculate()
+ {
+ if($this->getInvoice() instanceof \Siwapp\CoreBundle\Entity\AbstractInvoice)
+ {
+ $this->getInvoice()->setAmounts();
+ }
+ }
+
/**
* Get base amount
*
@@ -19,7 +19,6 @@ class AbstractInvoiceRepository extends EntityRepository
**/
public function updateTotals()
{
- echo $this->getEntityName();
$em = $this->getEntityManager();
foreach($em->createQuery('SELECT i from '.$this->getEntityName().' i')->getResult() as $entity)
{
@@ -14,6 +14,9 @@ class SiwappBaseTest extends \PHPUnit_Framework_TestCase
protected $_invoice_repo;
protected $_recurring_invoice_repo;
protected $_estimate_repo;
+ protected $_invoice_item_repo;
+ protected $_recurring_inovice_item_repo;
+ protected $_estimate_item_repo;
public function setUp()
{
@@ -70,6 +73,27 @@ protected function getRepo($repo_name)
}
return $this->_estimate_repo;
break;
+ case 'invoice_item':
+ if(!$this->_invoice_item_repo)
+ {
+ $this->_invoice_item_repo = $this->em->getRepository('SiwappInvoiceBundle:Item');
+ }
+ return $this->_invoice_item_repo;
+ break;
+ case 'recurring_invoice_item':
+ if(!$this->_recurring_invoice_item_repo)
+ {
+ $this->_recurring_invoice_item_repo = $this->em->getRepository('SiwappRecurringInvoiceBundle:Item');
+ }
+ return $this->_recurring_invoice_item_repo;
+ break;
+ case 'estimate_item':
+ if(!$this->_estimate_item_repo)
+ {
+ $this->_estimate_item_repo = $this->em->getRepository('SiwappEstimateBundle:Item');
+ }
+ return $this->_estimate_item_repo;
+ break;
}
}
@@ -234,10 +234,9 @@ public function getDueDate()
*
* @param Siwapp\InvoiceBundle\Entity\Item $item
*/
- public function addItem(\Siwapp\InvoiceBundle\Entity\Item $item)
+ public function addNewItem(\Siwapp\InvoiceBundle\Entity\Item $item)
{
$this->items[] = $item;
- $item->setInvoice($this);
}
/**
@@ -270,6 +269,26 @@ public function getPayments()
return $this->payments;
}
+ /** ***************** RELATIONSHIP METHODS ************* **/
+ public function removeThisItem($mixed = null)
+ {
+ if ($mixed instanceof \Siwapp\InvoiceBundle\Entity\Item)
+ {
+ foreach($this->items as $ref => $item)
+ {
+ if ($item === $mixed)
+ {
+ unset($this->items[$ref]);
+ break;
+ }
+ }
+ }
+ else if(is_int($mixed))
+ {
+ unset($this->items[$mixed]);
+ }
+ }
+
/** **************** CUSTOM METHODS AND PROPERTIES ************** */
public function __toString()
@@ -352,7 +371,7 @@ public function setSeriesId($value)
* @return Siwapp\InvoiceBundle\Invoice $this
*/
public function checkStatus()
- {
+ {
if($this->status == Invoice::DRAFT)
{
return $this;
@@ -1,6 +1,7 @@
<?php
use Siwapp\InvoiceBundle\Entity\Invoice;
+use Siwapp\InvoiceBundle\Entity\Item;
use Siwapp\CoreBundle\Tests\SiwappBaseTest;
@@ -20,27 +21,27 @@ public function testTrial()
$this->assertEquals($test_invoice->tax_amount_iva16, 862.64);
// TODO checks post save
- // deleting
+ // recalculate when deleting item
$items = $test_invoice->getItems();
- echo count($items);
- $this->em->remove($items[0]);
+ $test_invoice->removeItem($items[2]);
$this->em->flush();
- $this->em->detach($test_invoice);
- $t2_invoice = $this->getRepo('invoice')->find(1);
- echo count($t2_invoice->getItems());
-
-
-
-
- /*
+ $this->assertEquals(count($test_invoice->getItems()),4);
+ $this->assertEquals($test_invoice->getGrossAmount(), 8072.36);
+ // recalculate when adding item
+ $new_item = new Item();
+ $new_item->setDescription("test item");
+ $new_item->setUnitaryCost(100);
+ $new_item->setQuantity(1);
+ $new_item->setDiscount(0);
+ $this->em->persist($new_item);
+ $test_invoice->addItem($new_item);
+ $this->em->flush();
+ $this->assertEquals($test_invoice->getGrossAmount(),8172.36);
+ // recalculate when updating item
+ $item = $items[0];
+ $item->setQuantity($item->getQuantity*2);
+ $this->em->flush();
+ $this->assertEquals($test_invoice->getGrossAmount(),6903.01);
- $inv = new Invoice();
- $this->assertEquals(0,0);
- $repo = $this->em->getRepository('SiwappInvoiceBundle:Invoice');
- foreach($repo->findAll() as $inv)
- {
- echo $inv->getId().", ".$inv->getCustomerName()."\n";
- }
- */
}
}

0 comments on commit 9b50d9e

Please sign in to comment.