2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 include_once 'class.data_set.inc';
19 class reflines_db extends data_set {
20 function __construct()
22 $this->set_validator('prefix:ui:_check_prefix', _("This prefix conflicts with another one already defined. Prefix have to be unambigous."));
23 $this->set_validator('prefix:ui:_check_template', _("Invalid template format."));
24 $this->set_validator('trans_type:ui:required', _("Transaction type cannot be empty."));
25 $this->set_validator('pattern:ui:required', _("Next reference cannot be empty."));
26 parent::__construct('reflines',
27 array('trans_type', 'prefix', 'description', 'default', 'pattern', 'id', 'inactive'),
32 Prefix cannot be ambigous.
34 function _check_prefix($data, $dummy_opt, $key)
36 $cond = "`id`<>".db_escape($key)." AND `trans_type`=".db_escape($data['trans_type']);
37 if ($data['prefix'] === '')
38 $cond .= " AND `prefix`='".$data['prefix']."'";
40 $cond .= "AND ((LOCATE('".$data['prefix']."', CONCAT(`prefix`,`pattern`))=1 OR (`prefix`<>'' AND LOCATE(`prefix`, '".$data['prefix'].$data['pattern']."')=1)))";
42 return db_num_rows($this->get_all($cond)) == 0;
45 function _check_template($data, $dummy_opt, $key)
47 global $refline_options, $refline_placeholders;
49 if (strpbrk($data['prefix'], '{}') !== false)
50 return $this->error(_("You cannot use placeholders in refline prefix."));
52 if (substr_count($data['pattern'], '{') != substr_count($data['pattern'], '}'))
53 return $this->error(_("Curly brackets does not balance."));
55 if (preg_match_all('/\{([^\}]*)\}/', $data['pattern'], $match)) // placeholders defind in template
58 foreach($match[1] as $ph) {
61 elseif (!isset($refline_placeholders[$ph]) || !@in_array($refline_placeholders[$ph], $refline_options[$data['trans_type']])) {
63 foreach($refline_placeholders as $id => $dt)
64 if (in_array($dt, $refline_options[$data['trans_type']]))
67 return $this->error(sprintf(_("Invalid placeholder '%s'. Placeholders allowed for this transaction type are: %s."),
68 $ph, implode(',', $allowed)));
73 return $this->error(_("Missing numeric placeholder. If you want to use template based references, you have to define numeric placeholder too."));
78 function is_used($prefix, $trans_type)
82 FROM (SELECT r.* FROM ".TB_PREF."refs r
83 LEFT JOIN ".TB_PREF."voided as v
84 ON r.id=v.id AND r.type=v.type
85 WHERE r.type=".db_escape($trans_type)." AND ISNULL(v.id)
87 LEFT JOIN ".TB_PREF."reflines line ON ref.type=line.trans_type AND substr(ref.reference,1, LENGTH(line.prefix))= line.prefix AND line.prefix<>''
88 WHERE ".($prefix == '' ? "ISNULL(prefix)" : "prefix=".db_escape($prefix));
90 $res = db_query($sql, "cannot check reference line");
92 return db_num_rows($res);
95 function delete_check($ref_id)
97 $rec = $this->get($ref_id);
99 return $this->error(_("Reference line which is default for any transaction type cannot be deleted."));
101 if ($this->is_used($rec['prefix'], $rec['trans_type']))
102 return $this->error(_("Reference line cannot be deleted because it is already in use."));
107 function _set_as_default($id, $type)
109 $sql = "UPDATE ".TB_PREF."reflines SET `default`=(`id`=".db_escape($id).")
110 WHERE `trans_type`=".db_escape($type);
111 return db_query($sql, "cannot update default refline");
114 function insert($data)
116 if (!parent::insert($data))
118 if (@$data['default'])
119 return $this->_set_as_default(db_insert_id(), $data['trans_type']);
123 function update($key, $data)
125 if (!parent::update($key, $data))
127 if (@$data['default'])
128 return $this->_set_as_default($key , $data['trans_type']);
132 function get_default($type)
134 $sql = "SELECT * FROM ".TB_PREF."reflines WHERE trans_type=".db_escape($type)." AND `default`";
135 $result = db_query($sql, "cannot retreive default refline for trnasaction type $type");
136 return db_fetch($result);
139 function count($type, $all=false)
141 $sql = "SELECT count(*) FROM ".TB_PREF."reflines WHERE trans_type=".db_escape($type);
144 $sql .= " AND !inactive";
145 $result = db_query($sql, "cannot retreive refline count for transaction type $type");
146 $rec = db_fetch($result);
147 return $rec ? $rec[0] : 0;
151 Recognize refline by reference prefix
153 function find_refline_id($reference, $type, $fallback=true)
155 $sql = "SELECT * FROM ".TB_PREF."reflines WHERE trans_type=".db_escape($type)
156 ." AND CHAR_LENGTH(`prefix`) AND LEFT(".db_escape($reference).", CHAR_LENGTH(`prefix`)) = `prefix`";
157 if ($fallback) // if not found return refline with empty prefix
158 $sql .= " UNION SELECT * FROM ".TB_PREF."reflines WHERE trans_type=".db_escape($type)." AND `prefix`=''";
159 $ret = db_query($sql, "cannot check reference line id");
160 $line = db_fetch($ret);
162 if (!$fallback && (db_num_rows($fallback) != 1)) // more than one record means ambigous reference line
165 return $line ? $line['id'] : null;
168 function save_next($type, $reference, $line=null)
170 $sql = "UPDATE ".TB_PREF."reflines SET pattern=SUBSTRING(" . db_escape(trim($reference)) .", LENGTH(`prefix`)+1)"
171 . " WHERE trans_type = ".db_escape($type)." AND ";
174 $sql .= "`id`=".db_escape($line);
178 return db_query($sql, "The next transaction ref for $type could not be updated");