package FS::TicketSystem::RT_External;
use strict;
-use vars qw( $conf $default_queueid
+use vars qw( $DEBUG $me $conf $dbh $default_queueid $external_url
+ $priority_reverse
$priority_field $priority_field_queue $field
- $dbh $external_url );
+ );
use URI::Escape;
use FS::UID qw(dbh);
use FS::Record qw(qsearchs);
use FS::cust_main;
+$me = '[FS::TicketSystem::RT_External]';
+$DEBUG = 0;
+
FS::UID->install_callback( sub {
$conf = new FS::Conf;
$default_queueid = $conf->config('ticket_system-default_queueid');
+ $priority_reverse = $conf->exists('ticket_system-priority_reverse');
$priority_field =
$conf->config('ticket_system-custom_priority_field');
if ( $priority_field ) {
$priority_field_queue =
$conf->config('ticket_system-custom_priority_field_queue');
+
$field = $priority_field_queue
? $priority_field_queue. '.%7B'. $priority_field. '%7D'
: $priority_field;
$external_url = $conf->config('ticket_system-rt_external_url');
}
+ #kludge... should *use* the id... but good enough for now
+ if ( $priority_field_queue =~ /^(\d+)$/ ) {
+ my $id = $1;
+ my $sql = 'SELECT Name FROM Queues WHERE Id = ?';
+ my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
+ $sth->execute($id) or die $sth->errstr. " executing $sql";
+
+ $priority_field_queue = $sth->fetchrow_arrayref->[0];
+
+ }
+
} );
sub num_customer_tickets {
my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
my $sql = "SELECT COUNT(*) $from_sql";
+ warn "$me $sql (@param)" if $DEBUG;
my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
$sth->execute(@param) or die $sth->errstr. " executing $sql";
$limit ||= 0;
my( $from_sql, @param) = $self->_from_customer( $custnum, $priority );
- my $sql = "SELECT tickets.*, queues.name".
- ( length($priority) ? ", objectcustomfieldvalues.content" : '' ).
- " $from_sql ORDER BY priority, id DESC LIMIT $limit";
+ my $sql="SELECT Tickets.*, Queues.name, ".
+ "position(Tickets.status in 'newopenstalledresolvedrejecteddeleted')".
+ " AS svalue " .
+ ( length($priority) ? ", objectcustomfieldvalues.content" : '' ).
+ " $from_sql ".
+ " ORDER BY svalue, ".
+ " priority ". ( $priority_reverse ? 'ASC' : 'DESC' ). ", ".
+ " id DESC ".
+ " LIMIT $limit";
+ warn "$me $sql (@param)" if $DEBUG;
my $sth = $dbh->prepare($sql) or die $dbh->errstr. "preparing $sql";
$sth->execute(@param) or die $sth->errstr. "executing $sql";
my $where = '';
if ( defined($priority) ) {
- my $queue_sql = " ObjectCustomFields.ObjectId = ( SELECT id FROM queues
- WHERE queues.name = ? )
+ my $queue_sql = " ObjectCustomFields.ObjectId = ( SELECT id FROM Queues
+ WHERE Queues.name = ? )
OR ( ? = '' AND ObjectCustomFields.ObjectId = 0 )";
my $customfield_sql =
unshift @param, $priority;
$join = "JOIN ObjectCustomFieldValues
- ON ( tickets.id = ObjectCustomFieldValues.ObjectId )";
+ ON ( Tickets.id = ObjectCustomFieldValues.ObjectId )";
$where = " AND content = ?
AND ObjectCustomFieldValues.disabled != 1
$where =
"AND 0 = ( SELECT count(*) FROM ObjectCustomFieldValues
- WHERE ObjectId = tickets.id
+ WHERE ObjectId = Tickets.id
AND ObjectType = 'RT::Ticket'
AND $customfield_sql
)
}
my $sql = "
- FROM tickets
- JOIN queues ON ( tickets.queue = queues.id )
- JOIN links ON ( tickets.id = links.localbase )
+ FROM Tickets
+ JOIN Queues ON ( Tickets.queue = Queues.id )
+ JOIN Links ON ( Tickets.id = Links.localbase )
$join
WHERE ( ". join(' OR ', map "status = '$_'", $self->statuses ). " )
AND target = 'freeside://freeside/cust_main/$custnum'
sub queues {
my($self) = @_;
- my $sql = "SELECT id, name FROM queues WHERE disabled = 0";
+ my $sql = "SELECT id, name FROM Queues WHERE disabled = 0";
my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
$sth->execute() or die $sth->errstr. " executing $sql";
return '' unless $queueid;
- my $sql = "SELECT name FROM queues WHERE id = ?";
+ my $sql = "SELECT name FROM Queues WHERE id = ?";
my $sth = $dbh->prepare($sql) or die $dbh->errstr. " preparing $sql";
$sth->execute($queueid) or die $sth->errstr. " executing $sql";
- $sth->fetchrow_arrayref->[0];
+ my $rows = $sth->fetchrow_arrayref;
+ $rows ? $rows->[0] : '';
}
$external_url;
}
+sub _retrieve_single_value {
+ my( $self, $sql ) = @_;
+
+ warn "$me $sql" if $DEBUG;
+ my $sth = $dbh->prepare($sql) or die $dbh->errstr. "preparing $sql";
+ $sth->execute or die $sth->errstr. "executing $sql";
+
+ my $arrayref = $sth->fetchrow_arrayref;
+ $arrayref ? $arrayref->[0] : $arrayref;
+}
+
+sub transaction_creator {
+ my( $self, $transaction_id ) = @_;
+
+ my $sql = "SELECT name from transactions JOIN users ON ".
+ "transactions.creator=users.id WHERE transactions.id = ".
+ $transaction_id;
+
+ $self->_retrieve_single_value($sql);
+}
+
+sub transaction_ticketid {
+ my( $self, $transaction_id ) = @_;
+
+ my $sql = "SELECT objectid from transactions WHERE transactions.id = ".
+ $transaction_id;
+
+ $self->_retrieve_single_value($sql);
+}
+
+sub transaction_subject {
+ my( $self, $transaction_id ) = @_;
+
+ my $sql = "SELECT subject from Transactions JOIN Tickets ON objectid=".
+ "Tickets.id WHERE transactions.id = ". $transaction_id;
+
+ $self->_retrieve_single_value($sql);
+}
+
+sub transaction_status {
+ my( $self, $transaction_id ) = @_;
+
+ my $sql = "SELECT status from Transactions JOIN Tickets ON objectid=".
+ "Tickets.id WHERE transactions.id = ". $transaction_id;
+
+ $self->_retrieve_single_value($sql);
+}
+
1;