[CS-Project-svn_notify] SF.net SVN: cs-project: [698] branches/invoices/lib
Brought to you by:
crazedsanity
From: <cra...@us...> - 2007-12-18 02:56:15
|
Revision: 698 http://cs-project.svn.sourceforge.net/cs-project/?rev=698&view=rev Author: crazedsanity Date: 2007-12-17 18:56:12 -0800 (Mon, 17 Dec 2007) Log Message: ----------- New methods for creating invoice & items. Yay. Modified Paths: -------------- branches/invoices/lib/invoice.class.php branches/invoices/lib/invoiceItem.class.php Modified: branches/invoices/lib/invoice.class.php =================================================================== --- branches/invoices/lib/invoice.class.php 2007-12-18 02:24:03 UTC (rev 697) +++ branches/invoices/lib/invoice.class.php 2007-12-18 02:56:12 UTC (rev 698) @@ -16,6 +16,7 @@ //TODO: log everything! + class invoice extends dbAbstract { protected $gfObj; @@ -29,9 +30,6 @@ const mainTable = 'invoice_table'; const mainTableSeq = 'invoice_table_invoice_id_seq'; - const itemTable = 'invoice_item_table'; - const itemTableSeq = 'invoice_item_table_invoice_item_id_seq'; - const transTable = 'invoice_transaction_table'; const transTableSeq = 'invoice_transaction_table_invoice_transaction_id_seq'; @@ -86,9 +84,18 @@ //========================================================================= /** - * Add a line item to the invoice; should be an instance of invoiceItem{}. + * Add a line item to the invoice. */ - public function add_item(invoiceItem $item) { + public function add_item(array $data) { + if(is_numeric($this->invoiceId)) { + $invoiceItem = new invoiceItem($this->db, $this->invoiceId); + $retval = $invoiceItem->create_item($data); + } + else { + throw new exception(__METHOD__ .': no invoice created!'); + } + + return($retval); }//end add_item() //========================================================================= @@ -108,6 +115,8 @@ throw new exception(__METHOD__ .": failed to retrieve last invoice_id: ". $this->lastError); } + $this->invoiceId = $retval; + return($retval); }//end get_inserted_invoice_id() //========================================================================= Modified: branches/invoices/lib/invoiceItem.class.php =================================================================== --- branches/invoices/lib/invoiceItem.class.php 2007-12-18 02:24:03 UTC (rev 697) +++ branches/invoices/lib/invoiceItem.class.php 2007-12-18 02:56:12 UTC (rev 698) @@ -19,20 +19,93 @@ protected $gfObj; protected $logsObj; + const itemTable = 'invoice_item_table'; + const itemTableSeq = 'invoice_item_table_invoice_item_id_seq'; + + private $invoiceId; + //========================================================================= - public function __construct(cs_phpDB $db) { + public function __construct(cs_phpDB $db, $invoiceId=NULL) { $this->db = $db; $this->gfObj = new cs_globalFunctions; $this->gfObj->debugPrintOpt = DEBUGPRINTOPT; $this->logsObj = new logsClass($this->db, 'Invoice Item'); + + $this->set_invoice_id($invoiceId); }//end __construct() //========================================================================= //========================================================================= - public function create_item(array $data) { - }//end create_item() + /** + * Set value of internal invoiceId. + */ + protected function set_invoice_id($invoiceId) { + if(is_numeric($invoiceId)) { + $this->invoiceId = $invoiceId; + } + else { + throw new exception(__METHOD__ .': invalid data ('. $invoiceId .')'); + } + }//end set_invoice_id() //========================================================================= + + + + //========================================================================= + /** + * Insert item into the database. + */ + public function insert_item(array $data) { + if(is_numeric($this->invoiceId)) { + $fields = array( + 'description' => 'sql', + 'unit_price' => 'float', + 'quantity' => 'int' + ); + + foreach($fields as $name=>$cleanType) { + if(isset($data[$name]) && strlen($this->gfObj->cleanString($data[$name], $cleanType))) { + $insertArr[$name] = $this->gfObj->cleanString($data[$name], $cleanType); + } + else { + throw new exception(__METHOD__ .': invalid data for '. $name .': ('. $data['name'] .')'); + } + } + + $insertArr['invoice_id'] = $this->invoiceId; + $sql = 'INSERT INTO '. $this->itemTable .' '. $this->gfObj->string_from_array($insertArr, 'insert', NULL, 'sql'); + if($this->run_sql($sql) && !strlen($this->lastError)) { + $retval = $this->get_last_inserted_item(); + } + else { + throw new exception(__METHOD__ .': failed to insert item: '. $this->lastError); + } + } + else { + throw new exception(__METHOD__ .': no invoiceId set!'); + } + + return($retval); + }//end insert_item() + //========================================================================= + + + + //========================================================================= + private function get_last_inserted_item() { + $sql = "SELECT currval('". $this->itemTableSeq ."'::text)"; + if($this->run_sql($sql)) { + $data = $this->db->farray(); + $retval = $data[0]; + } + else { + throw new exception(__METHOD__ .': failed to retrieve last invoice_item_id: '. $this->lastError); + } + + return($retval); + }//end get_last_inserted_item() + //========================================================================= } ?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |