Skip to content

Commit

Permalink
siwapp/siwapp#3 abstractinvoice custom methods and lifecycle callback…
Browse files Browse the repository at this point in the history
… schema
  • Loading branch information
JoeZ99 committed Dec 4, 2011
1 parent 5e7acd4 commit 5040d32
Showing 1 changed file with 75 additions and 23 deletions.
98 changes: 75 additions & 23 deletions src/Siwapp/CoreBundle/Entity/AbstractInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* TODO: Customer and Series relations. Timestampable and Taggable
*
* @ORM\MappedSuperclass
* @ORM\HasLifecycleCallbacks()
*/
class AbstractInvoice
{
Expand Down Expand Up @@ -144,17 +145,6 @@ class AbstractInvoice
*/
private $status;

private $decimals = null;

private function getDecimals()
{
if(!$this->decimals)
{
$this->decimals = 2;
}
return $this->decimals;
}

/**
* Get id
*
Expand Down Expand Up @@ -507,6 +497,28 @@ public function getStatus()

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


private $decimals = null;

public function getRoundedAmount($concept = 'gross')
{
if(!in_array($concept, array('base', 'discount', 'net', 'tax', 'gross')))
{
return 0;
}
return round(call_user_func(array($this, Inflector::camelize('get_'.$concept.'_amount'))),$this->getDecimals());
}

private function getDecimals()
{
if(!$this->decimals)
{
$this->decimals = 2;
}
return $this->decimals;
}


/**
* calculate values over items
*
Expand All @@ -523,26 +535,66 @@ public function calculate($field, $rounded=false)
switch($field)
{
case 'paid_amount':
foreach($this->getPayments() as $payment)
{
$val += $payment->getAmount();
}
break;
foreach($this->getPayments() as $payment)
{
$val += $payment->getAmount();
}
break;
default:
foreach($this->getItems() as $item)
{
$method = 'get'.Inflector::camelize($field);
$val += $item->method();
}
break;
foreach($this->getItems() as $item)
{
$method = 'get'.Inflector::camelize($field);
$val += $item->method();
}
break;
}

if($rounded)
{
return round($val, $this->getDecimals());
return round($val, $this->getDecimals());
}

return $val;
}

public function setAmounts()
{
$this->setBaseAmount($this->calculate('base_amount'));
$this->setDiscountAmount($this->calculate('discount_amount'));
$this->setNetAmount($this->getBaseAmount() - $this->getDiscountAmount());
$this->setTaxAmount($this->calculate('tax_amount'));
$rounded_gross = round(
$this->getNetAmount() + $this->getTaxAmount(),
PropertyTable::get('currency_decimals', 2)
);
$this->setGrossAmount($rounded_gross);

return $this;
}



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

/**
* @ORM\PrePersist
*/
public function preSave()
{
$this->checkStatus();
// TODO: check for customer matching and update it accordingly. (calling it's updateCustomer method)
}

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


}

0 comments on commit 5040d32

Please sign in to comment.