+
+ function _legacy_line($refline)
+ {
+ return strpbrk($refline['pattern'], '{}') == false;
+ }
+
+ function _parse_next($type, $template, $context=null)
+ {
+ global $refline_options, $refline_placeholders;
+
+ // date based placeholders are always allowed, so default for non-array context is date
+ if (!isset($context))
+ $context = new_doc_date();
+
+ if (is_string($context))
+ $context = array('date' => $context);
+
+ $context['user'] = $_SESSION['wa_current_user']->user;
+ $context['pos'] = $_SESSION['wa_current_user']->pos;
+ $out = '';
+
+ while(($start = strpos($template, '{')) !==false) {
+
+ $out .= substr($template, 0, $start);
+ $stop = strpos($template, '}');
+ if ($stop === false) {
+ display_warning(_("Invalid refline template."));
+ $out .= $template; // debug
+ break;
+ }
+ $ph = substr($template, $start+1, $stop-$start-1);
+ $template = substr($template, $stop+1);
+
+ if (isset($refline_placeholders[$ph])) {
+ if (!isset($context[$refline_placeholders[$ph]]))
+ {
+ display_warning(sprintf(_("Missing refline context data: '%s'"), $refline_placeholders[$ph]));
+ $out .= $ph; // debug
+ } else {
+ switch ($ph)
+ {
+ case 'MM':
+ case 'YY':
+ case 'YYYY':
+ list($day, $month, $year) = explode_date_to_dmy($context['date']);
+ $out .= $ph == 'MM' ? sprintf('%02d', $month) : ($ph == 'YY' ? sprintf('%02d', $year%100): sprintf('%04d', $year));
+ break;
+ case 'C':
+ $out .= sprintf('%d', $context['customer']);
+ break;
+
+ case 'B':
+ $out .= sprintf('%d', $context['branch']);
+ break;
+
+ case 'S':
+ $out .= sprintf('%d', $context['supplier']);
+ break;
+
+ case 'L':
+ $out .= sprintf('%s', $context['location']);
+ break;
+
+ case 'P':
+ $out .= sprintf('%s', $context['pos']);
+ break;
+
+ case 'UU':
+ $out .= sprintf('%02d', $context['user']);
+ break;
+ }
+ }
+ } elseif (is_numeric($ph)) {
+ $out .= '{'.$ph.'}'; // index placeholder
+ }
+ }
+
+ $out .= $template; // add postfix
+ if (!preg_match('/^([^\{]*)?\{([^\}]*)\}(.*)/', $out, $match)) { // parse index
+ display_error(_("Missing numeric placeholder in refline definition."));
+ return $out;
+ }
+
+ $prefix = $match[1];
+ $postfix = $match[3];
+
+ $db_info = get_systype_db_info($type);
+ $trans_table = $db_info[0];
+ $type_fld = $db_info[1];
+ $tno_fld = $db_info[2];
+ $ref_fld = $db_info[3];
+
+ // retrieve last ref number in the refline from original transaction table
+ $sql = "SELECT MAX(CAST(SUBSTR($ref_fld, ".(strlen($prefix)+1).",LENGTH($ref_fld)-".(strlen($postfix)+strlen($prefix)).") AS UNSIGNED))"
+ ." FROM `$trans_table` tbl
+ LEFT JOIN ".TB_PREF."voided v ON tbl.`$tno_fld`=v.id AND v.type=$type"
+ ." WHERE ISNULL(v.id)"
+ .($type_fld ? " AND tbl.`$type_fld`=$type" : '')
+ ." AND `$ref_fld` REGEXP ".db_escape('^'.preg_quote($prefix).'[0-9]*'.preg_quote($postfix).'$');
+ $result = db_query($sql, 'cannot retrieve last reference');
+ $result = db_fetch_row($result);
+
+ // fill with zeros to the length of original index placeholder
+ return $prefix.sprintf('%0'.strlen($match[2]).'d', $result[0]+1).$postfix;
+ }
+