Skip to content

Commit

Permalink
#3 set new methods for managing item relationship -adding & removing-
Browse files Browse the repository at this point in the history
   inside the core classes.
  • Loading branch information
JoeZ99 committed Feb 5, 2012
1 parent bb2023a commit 0239974
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 89 deletions.
49 changes: 21 additions & 28 deletions src/Siwapp/CoreBundle/Entity/AbstractInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,24 +501,23 @@ public function getStatus()
/** ** RELATIONSHIPS ** */

/**
* addItem
* addNewItem
* adds an item and recalculcates amounts
* it needs to use the 'addNewItem' of the descendant
* it needs to use the 'addItem' of the descendant
*
* @param \Siwapp\CoreBUndle\Entity\AbstractItem $item
* @param \Siwapp\CoreBundle\Entity\AbstractItem $item
* @author JoeZ99 <[email protected]>
*/
public function addItem(\Siwapp\CoreBundle\Entity\AbstractItem $item)
public function addNewItem(\Siwapp\CoreBundle\Entity\AbstractItem $item)
{
$this->addNewItem($item);// this method is called from the descendant
$item->setInvoice($this);
$this->addItem($item);// this method is called from the descendant
$item->setParent($this);// this is to ensure relation is established
$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
Expand All @@ -528,7 +527,21 @@ public function addItem(\Siwapp\CoreBundle\Entity\AbstractItem $item)
*/
public function removeItem($mixed)
{
$this->removeThisItem($mixed);
if($mixed instanceof \Siwapp\CoreBundle\Entity\AbstractItem)
{
foreach($this->getItems() as $ref => $item)
{
if($item === $mixed)
{
unset($this->items[$ref]);
break;
}
}
}
else if(is_int($mixed))
{
unset($this->items[$mixed]);
}
$this->setAmounts();

}
Expand Down Expand Up @@ -622,24 +635,4 @@ public function preUpdate()
// TODO: check for customer matching and update it accordingly. (calling it's updateCustomer method)
}

/**
* checkStatus is to be implemented by the classes than inherit
* from this
*/
protected function checkStatus()
{
}

/**
* @ORM\PostRemove
*/
public function postDelete()
{
foreach($this->items as $it)
{
$it->delete();
}
}


}
11 changes: 2 additions & 9 deletions src/Siwapp/CoreBundle/Entity/AbstractItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,8 @@ public function setUnitaryCost($unitary_cost)
$this->unitary_cost = $unitary_cost;
}


/** **************** CUSTOM METHODS ************* */

/**
* This function is to be implemented on its descendants
*/
public function getInvoice()
{
}

/** **************** CUSTOM METHODS ************* */


/**
Expand Down
11 changes: 9 additions & 2 deletions src/Siwapp/EstimateBundle/Entity/Estimate.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class Estimate extends AbstractInvoice
{
/**
* @ORM\OneToMany(targetEntity="Item", mappedBy="estimate")
* @ORM\OneToMany(targetEntity="Item", mappedBy="estimate", orphanRemoval=true, cascade={"all"})
*/
private $items;

Expand Down Expand Up @@ -119,7 +119,6 @@ public function getSentByEmail()
public function addItem(\Siwapp\CoreBundle\Entity\AbstractItem $item)
{
$this->items[] = $item;
$item->setEstimate($this);
}

/**
Expand All @@ -139,5 +138,13 @@ public function getItems()
const APPROVED = 2;
const REJECTED = 3;

public function checkStatus()
{
if($this->getDraft())
{
$this->setStatus(Estimate::DRAFT);
}
}


}
15 changes: 15 additions & 0 deletions src/Siwapp/EstimateBundle/Entity/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,19 @@ public function removeTax(\Siwapp\CoreBundle\Entity\Tax $tax)
{
$this->taxes->removeElement($tax);
}

/** *************** CUSTOM METHODS ************* **/

/** *************** RELATIONSHIP METHODS ********* **/

/**
* setParent
*
* @param \Siwapp\EstimateBundle\Entity\Estimate $est
* @author JoeZ99 <[email protected]>
*/
protected function setParent(\Siwapp\EstimateBundle\Entity\Estimate $est)
{
$this->setEstimate($est);
}
}
25 changes: 3 additions & 22 deletions src/Siwapp/InvoiceBundle/Entity/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ public function getDueDate()

/**
* Add items
*
*
* @param Siwapp\InvoiceBundle\Entity\Item $item
*/
public function addNewItem(\Siwapp\InvoiceBundle\Entity\Item $item)
public function addItem(\Siwapp\InvoiceBundle\Entity\Item $item)
{
$this->items[] = $item;
}
Expand Down Expand Up @@ -269,26 +269,6 @@ 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 ************** */

/**
Expand Down Expand Up @@ -445,4 +425,5 @@ public function setNextNumber()
$this->setNumber($this->id);
}
}

}
17 changes: 17 additions & 0 deletions src/Siwapp/InvoiceBundle/Entity/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,21 @@ public function removeTax(\Siwapp\CoreBundle\Entity\Tax $tax)
{
$this->taxes->removeElement($tax);
}

/** ********** CUSTOM METHODS ********* **/

/** ********** RELATIONSHIP METHODS *** **/

/**
* setParent
* Generic method to set the 'owner' of this item
* it can be either an invoice, recurriong invoice , estimate..
*
* @param \Siwapp\InvoiceBundle\Entity\Invoice $invoice
* @author JoeZ99 <[email protected]>
*/
protected function setParent(\Siwapp\InvoiceBundle\Entity\Invoice $invoice)
{
$this->setInvoice($invoice);
}
}
15 changes: 15 additions & 0 deletions src/Siwapp/RecurringInvoiceBundle/Entity/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,19 @@ public function removeTax(\Siwapp\CoreBundle\Entity\Tax $tax)
{
$this->taxes->removeElement($tax);
}

/** ************* CUSTOM METHODS ********** **/

/** ************* RELATIONSHIP METHODS *********** **/

/**
* setParent
*
* @param \Siwapp\RecurringInvoiceBundle\Entity\RecurringInvoice $rinv
* @author JoeZ99 <[email protected]>
*/
protected function setParent(\Siwapp\RecurringInvoiceBundle\Entity\RecurringInvoice $rinv)
{
$this->setRecurringInvoice($rinv);
}
}
31 changes: 3 additions & 28 deletions src/Siwapp/RecurringInvoiceBundle/Entity/RecurringInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class RecurringInvoice extends AbstractInvoice
{
/**
* @ORM\OneToMany(targetEntity="Item", mappedBy="recurring_invoice")
* @ORM\OneToMany(targetEntity="Item", mappedBy="recurring_invoice", orphanRemoval=true, cascade={"all"})
*/
private $items;

Expand Down Expand Up @@ -285,11 +285,10 @@ public function getLastExecutionDate()

/**
* Add items
* To be called from the ancestor's "addItem" method
*
* @param Siwapp\RecurringInvoiceBundle\Entity\Item $item
*/
public function addNewItem(\Siwapp\RecurringInvoiceBundle\Entity\Item $item)
public function addItem(\Siwapp\RecurringInvoiceBundle\Entity\Item $item)
{
$this->items[] = $item;
}
Expand All @@ -304,31 +303,6 @@ public function getItems()
return $this->items;
}

/**
* Remove an item from the item collection
* To be called from ancestor's removeItem method
*
* @param mixed $mixed \Siwapp\RecurringInvoiceBundle\Entity\Item or integer
*/

public function removeThisItem($mixed = null)
{
if ($mixed instanceof \Siwapp\RecurringInvoiceBundle\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 ************** **/
/**
Expand Down Expand Up @@ -430,4 +404,5 @@ public function checkStatus()
$this->checkMustOccurrences();
//TODO : End this
}

}

0 comments on commit 0239974

Please sign in to comment.