+require SL::DB::GLTransaction;
+require SL::DB::Invoice;
+require SL::DB::PurchaseInvoice;
+
+__PACKAGE__->meta->add_relationship(
+ ar => {
+ type => 'many to one',
+ class => 'SL::DB::Invoice',
+ column_map => { trans_id => 'id' },
+ },
+ ap => {
+ type => 'many to one',
+ class => 'SL::DB::PurchaseInvoice',
+ column_map => { trans_id => 'id' },
+ },
+ gl => {
+ type => 'many to one',
+ class => 'SL::DB::GLTransaction',
+ column_map => { trans_id => 'id' },
+ },
+);
+
+__PACKAGE__->meta->initialize;
+
+sub record {
+ my ($self) = @_;
+
+ my @classes = qw(Invoice PurchaseInvoice GLTransaction);
+
+ foreach my $class ( @classes ) {
+ $class = 'SL::DB::' . $class;
+ my $record = $class->new(id => $self->trans_id);
+ return $record if $record->load(speculative => 1);
+ };
+
+};
+
+sub get_type {
+ my $self = shift;
+
+ my $ref = ref $self->record;
+
+ return "ar" if $ref->isa('SL::DB::Invoice');
+ return "ap" if $ref->isa('SL::DB::PurchaseInvoice');
+ return "gl" if $ref->isa('SL::DB::GLTransaction');
+
+ die "Can't find trans_id " . $self->trans_id . " in ar, ap or gl" unless $ref;
+
+};
+
+sub transaction_name {
+ my $self = shift;
+
+ my $ref = ref $self->record;
+ my $name = "trans_id: " . $self->trans_id;
+ if ( $self->get_type eq 'ar' ) {
+ $name .= " (" . $self->record->abbreviation . " " . t8("AR") . ") " . t8("Invoice Number") . ": " . $self->record->invnumber;
+ } elsif ( $self->get_type eq 'ap' ) {
+ $name .= " (" . $self->record->abbreviation . " " . t8("AP") . ") " . t8("Invoice Number") . ": " . $self->record->invnumber;
+ } elsif ( $self->get_type eq 'gl' ) {
+ $name = "trans_id: " . $self->trans_id . " (" . $self->record->abbreviation . ") " . $self->record->reference . " - " . $self->record->description;
+ } else {
+ die "can't determine type of acc_trans line with trans_id " . $self->trans_id;
+ };
+
+ $name .= " " . t8("Date") . ": " . $self->transdate->to_kivitendo;
+
+ return $name;
+
+};