Skip to content

Commit

Permalink
#3 Some custom methods in recurring invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeZ99 committed Dec 31, 2011
1 parent 927f02d commit 14b08d3
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 7 deletions.
7 changes: 2 additions & 5 deletions src/Siwapp/CoreBundle/Entity/AbstractInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,8 @@ public function getStatus()
*/
public function addItem(\Siwapp\CoreBundle\Entity\AbstractItem $item)
{
$this->addNewItem($item);
if($item instanceof \Siwapp\InvoiceBundle\Entity\Item)
{
$item->setInvoice($this);
}
$this->addNewItem($item);// this method is called from the descendant
$item->setInvoice($this);
$this->setAmounts();
}

Expand Down
3 changes: 3 additions & 0 deletions src/Siwapp/InvoiceBundle/Entity/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ public function removeThisItem($mixed = null)

/** **************** CUSTOM METHODS AND PROPERTIES ************** */

/**
* TODO: provide the series .
*/
public function __toString()
{
return (string)$this->number;
Expand Down
134 changes: 132 additions & 2 deletions src/Siwapp/RecurringInvoiceBundle/Entity/RecurringInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,18 @@ public function getLastExecutionDate()
return $this->last_execution_date;
}


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

/**
* Add items
* To be called from the ancestor's "addItem" method
*
* @param Siwapp\RecurringInvoiceBundle\Entity\Item $item
*/
public function addItem(\Siwapp\CoreBundle\Entity\AbstractItem $item)
public function addNewItem(\Siwapp\RecurringInvoiceBundle\Entity\Item $item)
{
$this->items[] = $item;
$item->setRecurringInvoice($this);
}

/**
Expand All @@ -300,4 +303,131 @@ 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 ************** **/
/**
* TODO: provide the series.
*/
public function __toString()
{
return (string) "Recurring Invoice: ".$this->id;
}

const INACTIVE = 0;
const FINISHED = 1;
const ACTIVE = 2;
const PENDING = 3;

/**
* get occurrences. get the number of invoices this recurring has generated
*
* @return integer the number of invoices generated
*/
public function getOccurrences()
{
return count($this->getInvoices());
}

/**
* Gets the number of the pending invoices to be generated
* by this recurring at the actual date
*
* @return integer the number of pending invoices
*/
public function countPendingInvoices()
{
return ($this->must_occurrences - $this->getOcurrences());
}

/**
* checks and sets the number of invoices that should have been
* generated until the day specified in args
*
* @param \DateTime $today
*/
public function checkMustOccurrences(\DateTime $today = null)
{
if(!$today) $today = new \DateTime();
$starting_date = $this->getStartingDate();
$finishing_date = $this->getFinishingDate();
// TODO : FINISH THIS METHODD!!!
if($today > $starting_date)
{
$check_date = $finishing_date ?
($today > $finishing_date ? $finishing_date: $today) : $today;

switch($this->period_type)
{
case 'year':
$unit = 'y';
break;
case 'month':
$unit = 'm';
break;
case 'week':
case 'day':
$unit = 'a';
break;
}

$difference = $check_date->diff($starting_date)->format($unit);

if($this->period_type == 'week')
{
$difference /= 7;
}
$must_occurrences = floor($difference / $this->period) +1;


// if there's already a must_occurreces and is greater
// then set this as must_occurences
if($this->must_occurrences &&
$must_occurrences > $this->must_occurrences)
{
$must_occurrences = $this->must_occurrences;
}
$this->must_occurrences = $must_occurrences;
}
else
{
$this->must_occurrences = 0;
}
}

/**
* checks and sets the status
*
* @return RecurringInvoice $this
*/
public function checkStatus()
{
$this->checkMustOccurrences();
//TODO : End this
}
}

0 comments on commit 14b08d3

Please sign in to comment.