Skip to content

Commit

Permalink
#3 Siwapp\InvoiceBundle\Invoice custom methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeZ99 committed Dec 11, 2011
1 parent 782f06d commit 88eec61
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/Siwapp/CoreBundle/Entity/AbstractInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,14 @@ public function preSave()
// 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
*/
Expand Down
1 change: 1 addition & 0 deletions src/Siwapp/InvoiceBundle/DataFixtures/invoices.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Invoice:
Invoice_1:
id: 4432
draft: false
sent_by_email: false
Series: Series_2
Expand Down
154 changes: 149 additions & 5 deletions src/Siwapp/InvoiceBundle/Entity/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Util\Inflector;
use Siwapp\CoreBundle\Entity\AbstractInvoice;
use Symfony\Component\Validator\Constraints as Assert;

Expand Down Expand Up @@ -249,11 +250,6 @@ public function getItems()
return $this->items;
}

public function __toString()
{
return (string)$this->number;
}

/**
* Add payments
*
Expand All @@ -273,4 +269,152 @@ public function getPayments()
{
return $this->payments;
}

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

public function __toString()
{
return (string)$this->number;
}



const DRAFT = 0;
const CLOSED = 1;
const OPENED = 2;
const OVERDUE = 3;

private $series_changed = false;

public function getDueAmount()
{
if($this->status == self::DRAFT)
{
return null;
}
return $this->gross_amount - $this->paid_amount;
}

/**
* try to catch custom methods to be used in twig templates
*/
public function __get($name)
{
if($name == 'due_amount')
{
$m = Inflector::camelize("get_{$name}");
return $this->$m();
}
if(strpos($name, 'tax_amount_') === 0)
{
return $this->calculate($name, true);
}
return false;
}

public function __isset($name)
{
if($name == 'due_amount')
{
return true;
}
if(strpos($name, 'tax_amount_') === 0)
{
return true;
}
return parent::__isset($name);
}

/**
* When setting series id, we check if there has been a series change,
* because the invoice number will have to change later
*
* TODO: Reiew this method when series object are available
*
* @author JoeZ99 <jzarate@gmail.com>
*
*/
public function setSeriesId($value)
{
// we check numeric value to prevent loading series by name in the tests
if($this->getNumber() && $value != $this->series_id &&
is_numeric($this->series_id) && is_numeric($value))
{
$this->series_changed = true;
}
parent::setSeriesId($value);
}

/**
* checkStatus
* checks and sets the status
*
* @return Siwapp\InvoiceBundle\Invoice $this
*/
public function checkStatus()
{
if($this->closed || $this->due_amount == 0)
{
$this->setStatus(Invoice::CLOSED);
}
else
{
if($this->due_date > sfDate::getInstance()->format('Y-m-d'))
{
$this->setStatus(Invoice::OPENED);
}
else
{
$this->setStatus(Invoice::OVERDUE);
}
}
return $this;
}

public function getStatusString()
{
switch($this->status)
{
case Invoice::DRAFT;
$status = 'draft';
break;
case Invoice::CLOSED;
$status = 'closed';
break;
case Invoice::OPENED;
$status = 'opened';
break;
default:
$status = 'unknown';
break;
}
return $status;
}

public function setAmounts()
{
parent::setAmounts();
$this->setPaidAmount($this->calculate('paid_amount'));

return $this;
}



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

/**
* @ORM\PrePersist
*/
public function setNextNumber()
{
// compute the number of invoice
if( (!$this->number && $this->status!=self::DRAFT) ||
($this->series_changed && !$this->status!=self::DRAFT)
)
{
$this->series_changed = false;
$this->setNumber($this->id);
}
}
}

0 comments on commit 88eec61

Please sign in to comment.