X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=includes%2Freferences.inc;h=0c11ebee3defacaa1d96bcdfb61a25a862f08160;hb=175a48c5bd73abe16e77fa2cb48ba522f00d1501;hp=eba249dcb477bfb9814ab53d1703a13c19d2dc8d;hpb=bafedd5d941782b5d35c56f0b37feb5f75b40b33;p=fa-stable.git diff --git a/includes/references.inc b/includes/references.inc index eba249dc..0c11ebee 100644 --- a/includes/references.inc +++ b/includes/references.inc @@ -10,69 +10,74 @@ See the License here . ***********************************************************************/ include_once($path_to_root . "/includes/db/references_db.inc"); +//--------------------------------------------------------------------------------------------- +// +// For now (2.3) the references system has somewhat inconsistent design scheme. +// Most transactions store references in respective table, but this is not the case +// for journal entries. All references regardless of type are stored also in refs table. +// Reference uniquness now can be checked with is_new_reference() for all transactions. +// In near future this should be fixed either with removing reference fields +// in transaction tables, or adding ref in bank transaction/journal and removing refs table. +// class references { - - function save($type, $id, $reference) - { - add_reference($type, $id, $reference); - if ($reference != 'auto') - $this->save_last($reference, $type); - } - + // + // Get reference from refs table for given transaction. + // Used for transactions which do not hold references (journal and bank). + // function get($type, $id) { return get_reference($type, $id); - } - - function delete($type, $id) - { - delete_reference($type, $id); - } - - function update($type, $id, $reference) - { - update_reference($type, $id, $reference); } - // check if reference is used for any non voided transaction (used for ST_JOURNALENTRY type) + // + // Check if reference is used for any non voided transaction (used for ST_JOURNALENTRY type) + // function exists($type, $reference) { return (find_reference($type, $reference) != null); } - - function save_last($reference, $type) - { - $next = $this->increment($reference); - save_next_reference($type, $next); - } - + // + // Get default reference on new transaction creation. + // function get_next($type) { return get_next_reference($type); } // - // Restore previous reference if voided trans ref was the last one. + // Check reference is valid before add/update transaction. // - function restore_last($type, $id) + function is_valid($reference) + { + return strlen(trim($reference)) > 0; + } + // + // Save reference (and prepare next) on write transaction. + // + function save($type, $id, $reference) { - $reference = $this->get($type, $id); - $last = $this->increment($this->get_next($type), true); //decrement - if ($reference==$last) { - save_next_reference($type, $last); + update_reference($type, $id, $reference); // store in refs table + if ($reference == $this->get_next($type)) { // if reference was not changed from default + $next = $this->_increment($reference); // increment default + save_next_reference($type, $next); } } - - //------------------------------------ - - function is_valid($reference) + // + // Restore previous reference (if possible) after voiding transaction. + // + function restore_last($type, $id) { - return strlen(trim($reference)) > 0; + $reference = get_reference($type, $id); + $prev = $this->_increment($this->get_next($type), true); //decrement + if ($reference==$prev) { + save_next_reference($type, $prev); + } } + //----------------------------------------------------------------------- // // Increments (or decrements if $back==true) reference template // - function increment($reference, $back=false) + function _increment($reference, $back=false) { // New method done by Pete. So f.i. WA036 will increment to WA037 and so on. // If $reference contains at least one group of digits, @@ -93,7 +98,6 @@ class references else return $reference; } - //------------------------------------ } //---------------------------------------------------------------------------- @@ -106,27 +110,33 @@ function is_new_reference($ref, $type) $db_trans = $db_info[2]; $db_ref = $db_info[3]; - if ($db_ref != null) - { - if ($db_type != null) { - $sql = "SELECT $db_ref FROM $db_name - LEFT JOIN ".TB_PREF."voided v ON - $db_name.$db_type=v.type AND $db_name.$db_trans=v.id - WHERE $db_name.$db_ref='$ref' AND ISNULL(v.id) - AND $db_name.$db_type=$type"; - } else { - $sql = "SELECT $db_ref FROM $db_name - LEFT JOIN ".TB_PREF."voided v ON - v.type=$type AND $db_name.$db_trans=v.id - WHERE $db_ref='$ref' AND ISNULL(v.id)"; - } - $result = db_query($sql, "could not test for unique reference"); - - return (db_num_rows($result) == 0); - } + $ref = db_escape(trim($ref)); + $type = db_escape($type); - // it's a type that doesn't use references - shouldn't be calling here, but say yes anyways - return true; + if ($db_ref == null) { // journal or bank trans store references in refs table + $db_ref = TB_PREF."refs"; + $db_type = 'type'; + $db_trans = 'id'; + $rb_ref = 'reference'; + } + + if ($db_type != null) { + $sql = "SELECT $db_ref FROM $db_name + LEFT JOIN ".TB_PREF."voided v ON + $db_name.$db_type=v.type AND $db_name.$db_trans=v.id + WHERE $db_name.$db_ref=$ref AND ISNULL(v.id) + AND $db_name.$db_type=$type"; + } else { + $sql = "SELECT $db_ref FROM $db_name + LEFT JOIN ".TB_PREF."voided v ON + v.type=$type AND $db_name.$db_trans=v.id + WHERE $db_ref=$ref AND ISNULL(v.id)"; + } + + $result = db_query($sql, "could not test for unique reference"); + + return (db_num_rows($result) == 0); + }