+ if (!isset($line))
+ $line = $this->reflines->find_refline_id($reference, $type, true);
+
+ if (!isset($line))
+ return false;
+
+ $refline = $this->reflines->get($line);
+
+ if ($this->_legacy_line($refline)) //legacy non-templated line
+ return strlen(trim($reference)) > 0;
+
+ $regex = preg_quote($refline['prefix'].$refline['pattern']);
+ if (!is_array($context))
+ $context = array('date'=>$context);
+
+ $context['pos'] = $_SESSION["wa_current_user"]->pos;
+
+ if (is_date(@$context['date']))
+ {
+ list($year4, $month, $day) = explode("-", date2sql($context['date']));
+ $year2 = substr($year4, 2);
+ $f_year = explode("-", date2sql(get_fiscalyear_begin_for_date($context['date'])));
+ $f_year2 = substr($f_year[0], 2);
+ $f_year4 = $f_year[0];
+ } else
+ {
+ $month = '\d{2,}';
+ $year2 = '\d{2,}';
+ $year4 = '\d{4,}';
+ $f_year2 = '\d{2,}';
+ $f_year4 = '\d{4,}';
+ }
+ $cust = @$context['customer'] ? $context['customer'] : '\d+';
+ $supp = @$context['supplier'] ? $context['supplier'] : '\d+';
+ $branch = @$context['branch'] ? $context['branch'] : '\d+';
+ $location = @$context['location'] ? $context['location'] : '[a-z0-9]+';
+ $pos = @$context['pos'] ? $context['pos'] : '\d+';
+ $user = sprintf("%02d", $_SESSION['wa_current_user']->user);
+
+ $regex = preg_replace(
+ array(
+ '/\\\{/', // unquote placeholders
+ '/\\\}/',
+ '/\{MM\}/',
+ '/\{YY\}/',
+ '/\{YYYY\}/',
+ '/\{FF\}/',
+ '/\{FFFF\}/',
+ '/\{C\}/',
+ '/\{B\}/',
+ '/\{S\}/',
+ '/\{L\}/',
+ '/\{UU\}/',
+ '/\{P\}/',
+ '/\{\d+}/',
+ ),
+ array(
+ '{',
+ '}',
+ $month,
+ $year2,
+ $year4,
+ $f_year2,
+ $f_year4,
+ $cust,
+ $branch,
+ $supp,
+ $location,
+ $user,
+ $pos,
+ '\d+',
+ ), $regex);
+
+ $regex = '"^'.$regex.'"i';
+
+ return preg_match($regex, $reference, $match) ? 1 : 0;
+ }
+
+ //
+ // Save reference (and prepare next) on write transaction.
+ //
+ function save($type, $id, $reference, $line = null)
+ {
+ if ($reference == 'auto')
+ return;
+
+ $sql = "REPLACE ".TB_PREF."refs SET reference=".db_escape($reference)
+ .", type=".db_escape($type).", id=".db_escape($id);
+ db_query($sql, "could not update reference entry");
+
+ if (!isset($line))
+ {
+ $line = $this->reflines->find_refline_id($reference, $type);
+ }
+
+ $refline = $this->reflines->get($line);
+ // legacy code used with simple templates
+ if ($this->_legacy_line($refline) && ($reference == $this->get_next($type, $line))) { // if reference was not changed from default
+ $next = $this->_increment($reference); // increment default
+ $this->reflines->save_next($type, $next, $line);
+ }
+ }
+ //
+ // Restore previous reference (if possible) after voiding transaction.
+ //
+ function restore_last($type, $id)
+ {
+ // get refline for removed document reference
+ $reference = get_reference($type, $id);
+ $line = $this->reflines->find_refline_id($reference, $type);
+ $refline = $this->reflines->get($line);
+
+ if ($this->_legacy_line($refline)) // legacy code used with simple templates
+ {
+ $last = $this->_increment($this->get_next($type, $line), true); // find last reference used in this line
+ if ($reference == $last)
+ {
+ // save last reference as next
+ $sql = "UPDATE ".TB_PREF."reflines SET pattern=SUBSTRING(" . db_escape(trim($last)) .", LENGTH(`prefix`)+1)"
+ . " WHERE trans_type = ".db_escape($type) . " AND `id`=".db_escape($line);
+
+ db_query($sql, "The next transaction ref for $type could not be updated");
+ }
+ }
+ }
+
+ //-----------------------------------------------------------------------
+ //
+ // Increments (or decrements if $back==true) reference template
+ //
+ function _increment($reference, $back=false)
+ {
+ // Legacy code used when no palceholder is in use:
+ // WA036 will increment to WA037 and so on.
+ // If $reference contains at least one group of digits,
+ // extract first didgits group and add 1, then put all together.
+ // NB. preg_match returns 1 if the regex matches completely
+ // also $result[0] holds entire string, 1 the first captured, 2 the 2nd etc.
+ //
+ if (preg_match('/^(\D*?)(\d+)(.*)/', $reference, $result) == 1)
+ {
+ list($all, $prefix, $number, $postfix) = $result;
+ $dig_count = strlen($number); // How many digits? eg. 0003 = 4
+ $fmt = '%0' . $dig_count . 'd'; // Make a format string - leading zeroes
+ $val = intval($number + ($back ? ($number<1 ? 0 : -1) : 1));
+ $nextval = sprintf($fmt, $val); // Add one on, and put prefix back on
+
+ return $prefix.$nextval.$postfix;
+ }