This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/invoice/classes/model/invoice.php

455 lines
10 KiB
PHP
Raw Normal View History

2010-11-29 22:41:08 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides invoice capabilities.
*
* @package OSB
* @subpackage Invoice
* @category Models
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Invoice extends ORMOSB {
protected $_belongs_to = array(
'account'=>array()
);
protected $_has_many = array(
2011-07-13 22:59:32 +00:00
'invoice_item'=>array('far_key'=>'id'),
'invoice_item_tax'=>array('through'=>'invoice_item'),
2010-11-29 22:41:08 +00:00
'service'=>array('through'=>'invoice_item'),
'payment'=>array('through'=>'payment_item'),
'payment_item'=>array('far_key'=>'id'),
2010-11-29 22:41:08 +00:00
);
2011-08-02 06:20:11 +00:00
protected $_sorting = array(
'id'=>'DESC',
);
2011-07-14 09:09:03 +00:00
protected $_display_filters = array(
'date_orig'=>array(
array('Config::date',array(':value')),
),
'due_date'=>array(
array('Config::date',array(':value')),
),
'billed_amt'=>array(
array('Currency::display',array(':value')),
),
'credit_amt'=>array(
array('Currency::display',array(':value')),
),
'status'=>array(
array('StaticList_YesNo::display',array(':value')),
),
'total_amt'=>array(
array('Currency::display',array(':value')),
),
2010-11-29 22:41:08 +00:00
);
// Items belonging to an invoice
private $invoice_items = array();
2010-11-29 22:41:08 +00:00
/**
* Display the Invoice Number
*/
2011-07-14 09:09:03 +00:00
public function id() {
2010-11-29 22:41:08 +00:00
return sprintf('%06s',$this->id);
}
/**
* Display the Invoice Reference Number
*/
public function refnum() {
2011-05-02 12:28:17 +00:00
return sprintf('%s-%06s',$this->account->accnum(),$this->id);
2010-11-29 22:41:08 +00:00
}
/**
* Display the amount due
*/
public function due($format=FALSE) {
// If the invoice is active calculate the due amount
$result = $this->status ? round($this->total()-$this->payments_total(),2) : 0;
2011-07-14 09:09:03 +00:00
return $format ? Currency::display($result) : $result;
2011-05-02 12:28:17 +00:00
}
/**
2011-07-14 09:09:03 +00:00
* Return a list of invoice items for this invoice.
2011-05-02 12:28:17 +00:00
*/
2011-07-14 09:09:03 +00:00
public function items() {
// If we havent been changed, we'll load the records from the DB.
if ($this->loaded() AND ! $this->_changed)
return $this->invoice_item->order_by('service_id,item_type,module_id')->find_all()->as_array();
else
return $this->invoice_items;
2010-11-29 22:41:08 +00:00
}
2011-07-14 09:09:03 +00:00
/**
* Return the subtotal of all items
*/
2010-11-29 22:41:08 +00:00
public function subtotal($format=FALSE) {
$result = 0;
// @todo This rounding should be a system config.
2011-07-14 09:09:03 +00:00
foreach ($this->items() as $ito)
$result += round($ito->subtotal(),2);
2011-07-14 09:09:03 +00:00
return $format ? Currency::display($result) : $result;
}
public function discount($format=FALSE) {
2011-07-14 09:09:03 +00:00
$result = 0;
// @todo This rounding should be a system config.
2011-07-14 09:09:03 +00:00
foreach ($this->items() as $ito)
$result += round($ito->discount_amt,2);
return $format ? Currency::display($result) : $result;
}
public function tax($format=FALSE) {
$result = 0;
// @todo This rounding should be a system config.
foreach ($this->items() as $ito)
$result += round($ito->tax(),2);
2010-11-29 22:41:08 +00:00
2011-07-14 09:09:03 +00:00
return $format ? Currency::display($result) : $result;
2010-11-29 22:41:08 +00:00
}
2011-07-14 09:09:03 +00:00
/**
* Return the total of all items
*/
2010-11-29 22:41:08 +00:00
public function total($format=FALSE) {
$result = 0;
// @todo This rounding should be a system config.
2011-07-14 09:09:03 +00:00
foreach ($this->items() as $ito)
$result += round($ito->total(),2);
2010-11-29 22:41:08 +00:00
2011-07-14 09:09:03 +00:00
// Reduce by any credits
$result -= round($this->credit_amt,2);
2010-11-29 22:41:08 +00:00
2011-07-14 09:09:03 +00:00
return $format ? Currency::display($result) : $result;
2010-11-29 22:41:08 +00:00
}
2011-07-14 09:09:03 +00:00
public function payments() {
return ($this->loaded() AND ! $this->_changed) ? $this->payment_item->find_all() : NULL;
2011-07-14 09:09:03 +00:00
}
public function payments_total($format=FALSE) {
$result = 0;
2010-11-29 22:41:08 +00:00
2011-07-14 09:09:03 +00:00
foreach ($this->payments() as $po)
$result += $po->alloc_amt;
2010-11-29 22:41:08 +00:00
2011-07-14 09:09:03 +00:00
return $format ? Currency::display($result) : $result;
2010-11-29 22:41:08 +00:00
}
/**
* Get a list of services on an invoice
*
* We use this to list details by service on an invoice.
2010-11-29 22:41:08 +00:00
*/
public function items_services(array $items=array()) {
$result = array();
if (! $items)
$items = $this->items();
foreach ($items as $ito)
if ($ito->service_id AND empty($result[$ito->service_id]))
$result[$ito->service_id] = $ito->service;
return $result;
}
/**
* Return all invoice items for a service optionally by recurring schedule
*/
public function items_service($sid,$rs=NULL) {
$result = array();
$items = $this->items();
Sort::MAsort($items,'item_type');
foreach ($items as $ito)
if ($ito->service_id == $sid AND (is_null($rs) OR $ito->recurring_schedule == $rs))
array_push($result,$ito);
return $result;
2010-11-29 22:41:08 +00:00
}
/**
* Return a list of periods and services
2011-07-14 09:09:03 +00:00
*
* This is so that we can list items summarised by billing period
2010-11-29 22:41:08 +00:00
*/
public function items_service_periods() {
$result = array();
$c = array();
foreach ($this->items() as $ito)
if ($ito->service_id) {
// If we have already covered a service with no recurring_schedule
if (! $ito->recurring_schedule AND in_array($ito->service_id,$c))
continue;
array_push($c,$ito->service_id);
$result[$ito->recurring_schedule][] = $ito;
}
return $result;
2010-11-29 22:41:08 +00:00
}
2011-05-02 12:28:17 +00:00
/**
* Summarise the items on an invoice
*
* We summaries based on product.
2011-05-02 12:28:17 +00:00
*/
public function items_summary() {
2011-07-14 09:09:03 +00:00
$result = array();
2011-05-02 12:28:17 +00:00
foreach ($this->items() as $ito) {
// We conly summaries item_type=0
if (! $ito->item_type == 0)
continue;
$t = $ito->product_id;
2011-05-02 12:28:17 +00:00
2011-07-14 09:09:03 +00:00
if (! isset($result[$t])) {
$result[$t]['quantity'] = 0;
$result[$t]['subtotal'] = 0;
2011-05-02 12:28:17 +00:00
}
2011-07-14 09:09:03 +00:00
$result[$t]['quantity'] += $ito->quantity;
$result[$t]['subtotal'] += $ito->subtotal();
2011-05-02 12:28:17 +00:00
}
2011-07-14 09:09:03 +00:00
return $result;
}
2011-05-02 12:28:17 +00:00
2010-11-29 22:41:08 +00:00
/**
* Calculate the total for items for a service
*/
public function items_service_total($sid) {
$total = 0;
foreach ($this->items_service($sid) as $ito)
2011-07-14 09:09:03 +00:00
$total += $ito->total();
2010-11-29 22:41:08 +00:00
return $total;
}
/**
* Calculate the tax of items for a service
*/
public function items_service_tax($sid) {
$total = 0;
foreach ($this->items_service($sid) as $ito)
$total += $ito->tax();
2010-11-29 22:41:08 +00:00
return $total;
}
/**
* Calculate the discounts of items for a service
2010-11-29 22:41:08 +00:00
*/
public function items_service_discount($sid) {
$total = 0;
2010-11-29 22:41:08 +00:00
foreach ($this->items_service($sid) as $ito)
$total += $ito->discount();
2010-11-29 22:41:08 +00:00
return $total;
2010-11-29 22:41:08 +00:00
}
/**
* Return a list of taxes used on this invoice
* @todo Move some of this to invoice_item_tax.
2010-11-29 22:41:08 +00:00
*/
public function tax_summary() {
2011-05-02 12:28:17 +00:00
$summary = array();
2010-11-29 22:41:08 +00:00
2011-07-14 09:09:03 +00:00
foreach ($this->items() as $ito) {
foreach ($ito->invoice_item_tax->find_all() as $item_tax) {
2010-11-29 22:41:08 +00:00
if (! isset($summary[$item_tax->tax_id]))
$summary[$item_tax->tax_id] = $item_tax->amount;
else
$summary[$item_tax->tax_id] += $item_tax->amount;
}
}
// @todo This should be removed eventually
if (! $summary)
$summary[1] = $this->tax();
2011-05-02 12:28:17 +00:00
return $summary;
2010-11-29 22:41:08 +00:00
}
2011-07-14 09:09:03 +00:00
/**
* Add an item to an invoice
*/
2010-11-29 22:41:08 +00:00
public function add_item() {
if ($this->loaded() and ! $this->invoice_items)
throw new Kohana_Exception('Need to load invoice_items?');
2010-11-29 22:41:08 +00:00
$c = count($this->invoice_items);
$this->invoice_items[$c] = ORM::factory('invoice_item');
return $this->invoice_items[$c];
}
public function min_due($date) {
// @todo This should be configurable;
return ($date < time()) ? time() : $date;
}
2011-05-14 07:35:33 +00:00
public function save(Validation $validation = NULL) {
// Our items will be clobbered once we save the object, so we need to save it here.
$items = $this->items();
2010-11-29 22:41:08 +00:00
// Save the invoice
2011-05-14 07:35:33 +00:00
parent::save($validation);
2010-11-29 22:41:08 +00:00
// Need to save the associated items and their taxes
if ($this->saved()) {
foreach ($items as $iio) {
$iio->invoice_id = $this->id;
2010-11-29 22:41:08 +00:00
if (! $iio->check()) {
2010-11-29 22:41:08 +00:00
// @todo Mark invoice as cancelled and write a memo, then...
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed check()',array(':invoice'=>$invoice->id));
}
$iio->save();
2010-11-29 22:41:08 +00:00
if (! $iio->saved()) {
2010-11-29 22:41:08 +00:00
// @todo Mark invoice as cancelled and write a memo, then...
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed save()',array(':invoice'=>$invoice->id));
}
// @todo Need to save tax information
// @todo Need to save discount information
}
} else
throw new Kohana_Exception('Couldnt save invoice for some reason?');
}
/**
* Check the reminder value
*/
public function remind($key) {
if (! $this->loaded())
return NULL;
if (! trim($this->reminders))
return FALSE;
if (! preg_match('/^a:/',$this->reminders))
throw new Kohana_Exception('Reminder is not an array? (:reminder)',array(':remind',$this->reminders));
$remind = unserialize($this->reminders);
2011-09-27 11:22:13 +00:00
if (isset($remind[$key]))
return (is_array($remind[$key])) ? end($remind[$key]) : $remind[$key];
else
return FALSE;
}
public function set_remind($key,$value,$add=FALSE) {
if (! $this->loaded())
throw new Kohana_Exception('Cant call :method when a record not loaded.',array(':method',__METHOD__));
if (! trim($this->reminders)) {
$remind = array();
} else {
if (! preg_match('/^a:/',$this->reminders))
throw new Kohana_Exception('Reminder is not an array? (:reminder)',array(':remind',$this->reminders));
$remind = unserialize($this->reminders);
}
2011-09-27 11:22:13 +00:00
// If our value is null, we'll remove it.
if (is_null($value) AND isset($remind[$key]))
unset($remind[$key]);
elseif ($add)
$remind[$key][] = $value;
else
$remind[$key] = $value;
$this->reminders = serialize($remind);
$this->save();
return $this->saved();
}
2011-08-16 02:27:19 +00:00
/** LIST FUNCTIONS **/
private function _list_due() {
static $result = array();
if (! $result)
foreach (ORM::factory('invoice')->where('status','=',1)->find_all() as $io)
if ($io->due())
array_push($result,$io);
return $result;
}
2011-08-16 02:27:19 +00:00
/**
* Identify all the invoices that are due
*/
public function list_overdue($time=NULL) {
$result = array();
2011-08-16 02:27:19 +00:00
if (is_null($time))
$time = time();
foreach ($this->_list_due() as $io)
if ($io->due_date <= $time)
array_push($result,$io);
return $result;
2011-08-16 02:27:19 +00:00
}
2011-09-27 11:22:13 +00:00
/**
* Return a list of invoices that are over their due date with/without auto billing
*/
public function list_overdue_billing($time=NULL,$billing=FALSE) {
$return = array();
foreach ($this->list_overdue($time) as $io) {
$i = FALSE;
foreach ($io->service->find_all() as $so)
if (($billing AND $so->account_billing_id) OR (! $billing AND ! $so->account_billing_id)) {
array_push($return,$io);
break;
}
}
return $return;
}
2011-08-16 02:27:19 +00:00
/**
* Return a list of invoices that are due, excluding overdue.
*/
public function list_due($time=NULL) {
$result = array();
foreach ($this->_list_due() as $io)
if ($io->due_date > time())
if (is_null($time))
array_push($result,$io);
elseif ($this->due_date <= $time)
array_push($result,$io);
return $result;
2011-08-16 02:27:19 +00:00
}
2010-11-29 22:41:08 +00:00
}
?>