_methods)) return call_user_func_array(array($this->io,$method),$args); else throw HTTP_Exception::factory(501,'Unknown/unauthorised method :method',array(':method'=>$method)); } public function __construct(Model_Invoice $io=NULL) { $this->io = is_null($io) ? ORM::factory('Invoice') : $io; // Set our invoice as valid if (is_null($io)) $this->io->status = 1; } public static function instance(Model_Invoice $io=NULL) { return new Invoice($io); } /** * Add a Service to an Invoice * * @param $so Model_Servie */ public function add_service(Model_Service $so) { if ($this->io->loaded()) throw HTTP_Exception::factory(501,'Cannot use add service :service to an existing invoice :invoice ',array(':service'=>$so->id,':invoice'=>$this->io->id)); if (! $this->io->account_id) $this->io->account_id = $so->account_id; else if ($this->io->account_id != $so->account_id) throw HTTP_Exception::factory(501,'Cannot add service :service to invoice - it is for a different account',array(':service'=>$so->id)); // Set the invoice due date if (! $this->io->due_date OR $this->io->due_date > $this->io->min_due($so->date_next_invoice)) $this->io->due_date = $this->io->min_due($so->date_next_invoice); $pdata = Period::details($so->recur_schedule,$so->product->price_recurr_day,$so->invoiced_to()+86400,FALSE,$so->product->price_recurr_strict); $iio = ORM::factory('Invoice_Item'); $iio->service_id = $so->id; $iio->product_id = $so->product_id; $iio->quantity = $pdata['prorata']; $iio->price_base = $so->price(); $iio->recurring_schedule = $so->recur_schedule; $iio->date_start = $pdata['start_time']; $iio->date_stop = $pdata['end_time']; // Service Billing $iio->item_type = 0; $this->io->add_item($iio); // Check if there are any charges $c = ORM::factory('Charge') ->where('service_id','=',$so->id) ->where('void','is',NULL) ->where('processed','is',NULL); foreach ($c->find_all() as $co) { $iio = ORM::factory('Invoice_Item'); $iio->service_id = $co->service_id; $iio->product_id = $co->product_id; $iio->charge_id = $co->id; $iio->quantity = $co->quantity; $iio->price_base = $co->amount; $iio->date_start = $co->date_orig; $iio->date_stop = $co->date_orig; $iio->item_type = $co->type; $this->io->add_item($iio); } return $this; } public function render($type,$section,$args=array()) { switch ($type) { case 'html': switch ($section) { case 'body': return View::factory('invoice/user/view/body') ->set('show_id',(isset($args['noid']) AND $args['noid']) ? FALSE : TRUE) ->set('o',$this->io); break; default: throw HTTP_Exception::factory(501,'Unknown section type :section',array(':section'=>$section)); } break; default: throw HTTP_Exception::factory(501,'Unknown render type :type',array(':type'=>$type)); } } /** * Generate a PDF invoice */ public function pdf() { $invoice_class = Kohana::classname('Invoice_TCPDF_'.Kohana::$config->load('invoice')->driver); $pdf = new $invoice_class($this->io); if ($pdf->getTemplate()) { $pagecount = $pdf->setSourceFile($pdf->getTemplate()); $tplidx = $pdf->ImportPage(1); } $pdf->addPage(); # If we are using FPDI if (isset($tplidx)) $pdf->useTemplate($tplidx); $this->draw_summary_invoice($pdf); # If we get here, all is OK. return $pdf; } private function draw_summary_invoice($pdf) { // Draw Invoice Basics $pdf->drawCompanyLogo(); $pdf->drawCompanyAddress(); $pdf->drawInvoiceHeader(); // @todo Get news from DB $pdf->drawNews(''); $pdf->drawRemittenceStub(); $pdf->drawPaymentMethods(); if ($this->io->billing_status !=1 && $this->io->due_date <= time()) $pdf->drawInvoiceDueNotice(); elseif($this->io->billing_status == 1) $pdf->drawInvoicePaidNotice(); if ($this->io->account->invoices_due_total()) $pdf->drawSummaryInvoicesDue(); $pdf->drawSummaryLineItems(); // Next Page $pdf->drawDetailLineItems(); // Draw any Custom functions: $pdf->drawCustom(); } } ?>