array() ); protected $_has_many = array( 'invoice_item'=>array(), 'invoice_item_tax'=>array(), 'service'=>array('through'=>'invoice_item'), 'payment'=>array('through'=>'payment_item'), ); /** * @var array Filters to render values properly */ protected $_filters = array( // @todo This rounding should be a global configuration 'total_amt'=>array('round'=>array('2')), ); protected $_callbacks = array( 'id'=>array('get_next_id'), 'total_amt'=>array('calc_total'), 'tax_amt'=>array('calc_tax'), ); protected $_formats = array( 'date_orig'=>array('Config::date'=>array()), 'due_date'=>array('Config::date'=>array()), 'billed_amt'=>array('Currency::display'=>array()), 'credit_amt'=>array('Currency::display'=>array()), 'status'=>array('StaticList_YesNo::display'=>array()), 'total_amt'=>array('Currency::display'=>array()), ); /** * Display the Invoice Number */ public function invnum() { return sprintf('%06s',$this->id); } /** * Display the Invoice Reference Number */ public function refnum() { return sprintf('%02s-%04s-%06s',Config::siteid(),$this->account_id,$this->id); } /** * Display the amount due */ public function due($format=FALSE) { $result = 0; // If the invoice is active calculate the due amount if ($this->status) $result = $this->total_amt-$this->credit_amt-$this->billed_amt; if ($format) return Currency::display($result); else return $result; } public function subtotal($format=FALSE) { $result = 0; foreach ($this->items() as $item) $result += $item->subtotal(); if ($format) return Currency::display($result); else return $result; } public function total($format=FALSE) { $result = 0; foreach ($this->items() as $item) $result += $item->total(); // Reduce any credits $result -= $this->credit_amt; if ($format) return Currency::display($result); else return $result; } /** * Return a list of invoice items for this invoice. */ public function items() { // Get our invoice items for an existing invoice if ($this->id AND $this->loaded() AND ! $this->_changed) return $this->invoice_item->order_by('service_id,item_type,module_id')->find_all(); echo kohana::debug(array('BEFORE'=>$this->invoice_item)); if (! $this->invoice_items) $this->invoice_items = $this->invoice_item; echo kohana::debug(array('AFTER'=>$this->invoice_items));die(); return $this->invoice_items; } /** * Return a list of our main invoice items (item_id=0) */ public function items_main() { if ($this->id AND $this->loaded() AND ! $this->_changed) return $this->invoice_item->where('item_type','=',0)->find_all(); } /** * Return a list of our sub invoice items (item_id!=0) */ public function items_sub($sid) { if ($this->id AND $this->loaded() AND ! $this->_changed) return $this->invoice_item->where('service_id','=',$sid)->and_where('item_type','<>',0)->find_all(); } /** * Calculate the total for items for a service */ public function items_service_total($sid) { $total = 0; foreach ($this->invoice_item->where('service_id','=',$sid)->find_all() as $item) $total += $item->total(); return $total; } /** * Calculate the tax of items for a service */ public function items_service_tax($sid) { $total = 0; foreach ($this->invoice_item->where('service_id','=',$sid)->find_all() as $item) $total += $item->tax_amt; return $total; } /** * Return a list of items based on a sort criteria */ public function sorted_service_items($index) { $summary = array(); foreach ($this->items() as $item) { $key = $item->service->$index; if (! isset($summary[$key]['items'])) { $summary[$key]['items'] = array(); $summary[$key]['total'] = 0; } // Only record items with item_type=0 if ($item->item_type == 0) array_push($summary[$key]['items'],$item); $summary[$key]['total'] += $item->total(); } return $summary; } /** * Return a list of taxes used on this invoice */ public function tax_summary() { $summary = array(); foreach ($this->items() as $item) { foreach ($item->invoice_item_tax->find_all() as $item_tax) { if (! isset($summary[$item_tax->tax_id])) $summary[$item_tax->tax_id] = $item_tax->amount; else $summary[$item_tax->tax_id] += $item_tax->amount; } } return $summary; } public function add_item() { $c = count($this->invoice_items); $this->invoice_items[$c] = ORM::factory('invoice_item'); return $this->invoice_items[$c]; } public function save() { // Save the invoice parent::save(); // Need to save the associated items and their taxes if ($this->saved()) { $tax_amt = 0; $discount_amt = 0; foreach ($this->items() as $invoice_item) { $invoice_item->invoice_id = $this->id; if (! $invoice_item->check()) { // @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)); } $invoice_item->save(); if (! $invoice_item->saved()) { // @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?'); echo Kohana::debug(array('saved'=>$this)); } public function calc_total(Validate $array, $field) { $array[$field] = 0; // @todo Rounding here should come from a global config foreach ($this->invoice_items as $item) $array[$field] += round($item->price_base+$item->price_setup,2); $this->_changed[$field] = $field; } public function calc_tax(Validate $array, $field) { $array[$field] = 0; // @todo Rounding here should come from a global config // @todo tax should be evaluated per item // @todo tax parameters should come from user session foreach ($this->invoice_items as $item) $array[$field] += round(Tax::total(61,NULL,$item->price_base+$item->price_setup),2); $this->_changed[$field] = $field; } } ?>