Item-Positionen für Rechnungen in DB: items_sorted sortiert nach postition …
[kivitendo-erp.git] / SL / DB / Buchungsgruppe.pm
index 5f4d831..eb467c5 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 
 use SL::DB::MetaSetup::Buchungsgruppe;
 use SL::DB::Manager::Buchungsgruppe;
+use SL::DB::Helper::ActsAsList;
 
 __PACKAGE__->meta->add_relationship(
   inventory_account => {
@@ -11,81 +12,74 @@ __PACKAGE__->meta->add_relationship(
     class         => 'SL::DB::Chart',
     column_map    => { inventory_accno_id => 'id' },
   },
-  income_account_0 => {
-    type         => 'many to one',
-    class        => 'SL::DB::Chart',
-    column_map   => { income_accno_id_0 => 'id' },
-  },
-  income_account_1 => {
-    type         => 'many to one',
-    class        => 'SL::DB::Chart',
-    column_map   => { income_accno_id_1 => 'id' },
-  },
-  income_account_2 => {
-    type         => 'many to one',
-    class        => 'SL::DB::Chart',
-    column_map   => { income_accno_id_2 => 'id' },
-  },
-  income_account_3 => {
-    type         => 'many to one',
-    class        => 'SL::DB::Chart',
-    column_map   => { income_accno_id_3 => 'id' },
-  },
-  expense_account_0 => {
-    type         => 'many to one',
-    class        => 'SL::DB::Chart',
-    column_map   => { expense_accno_id_0 => 'id' },
-  },
-  expense_account_1 => {
-    type         => 'many to one',
-    class        => 'SL::DB::Chart',
-    column_map   => { expense_accno_id_1 => 'id' },
-  },
-  expense_account_2 => {
-    type         => 'many to one',
-    class        => 'SL::DB::Chart',
-    column_map   => { expense_accno_id_2 => 'id' },
-  },
-  expense_account_3 => {
-    type         => 'many to one',
-    class        => 'SL::DB::Chart',
-    column_map   => { expense_accno_id_3 => 'id' },
-  },
 );
 
 __PACKAGE__->meta->initialize;
 
+sub validate {
+  my ($self) = @_;
+
+  my @errors;
+  push @errors, $::locale->text('The description is missing.') if !$self->description;
+
+  return @errors;
+}
 
 sub income_accno_id {
   my ($self, $taxzone) = @_;
-  my $taxzone_id = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
-  my $method = 'income_accno_id_' . $taxzone_id;
 
-  return $self->$method;
+  require SL::DB::TaxZone;
+  require SL::DB::TaxzoneChart;
+
+  my $taxzone_id = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
+  my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by(taxzone_id => $taxzone_id, buchungsgruppen_id => $self->id);
+  return $taxzone_chart->income_accno_id if $taxzone_chart;
 }
 
 sub expense_accno_id {
   my ($self, $taxzone) = @_;
-  my $taxzone_id = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
-  my $method = 'expense_accno_id_' . $taxzone_id;
+  require SL::DB::TaxZone;
+  require SL::DB::TaxzoneChart;
 
-  return $self->$method;
+  my $taxzone_id = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
+  my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by(taxzone_id => $taxzone_id, buchungsgruppen_id => $self->id);
+  return $taxzone_chart->expense_accno_id if $taxzone_chart;
 }
 
 sub income_account {
   my ($self, $taxzone) = @_;
-  my $taxzone_id       = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
-  my $method           = 'income_account_' . $taxzone_id;
 
-  return $self->$method;
+  require SL::DB::TaxZone;
+  require SL::DB::TaxzoneChart;
+
+  my $taxzone_id       = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
+  my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by(taxzone_id => $taxzone_id, buchungsgruppen_id => $self->id);
+  return $taxzone_chart->income_accno if $taxzone_chart;
 }
 
 sub expense_account {
   my ($self, $taxzone) = @_;
+
+  require SL::DB::TaxZone;
+  require SL::DB::TaxzoneChart;
+
   my $taxzone_id       = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
-  my $method           = 'expense_account_' . $taxzone_id;
+  my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by(taxzone_id => $taxzone_id, buchungsgruppen_id => $self->id);
+  return $taxzone_chart->expense_accno if $taxzone_chart;
+}
 
-  return $self->$method;
+sub taxzonecharts {
+  my ($self) = @_;
+  return SL::DB::Manager::TaxzoneChart->get_all(where => [ buchungsgruppen_id => $self->id ]);
+}
+
+sub orphaned {
+  my ($self) = @_;
+  die 'not an accessor' if @_ > 1;
+
+  require SL::DB::Part;
+  return 0 if SL::DB::Manager::Part->get_all_count(query => [ buchungsgruppen_id => $self->id ]);
+  return 1;
 }
 
 1;
@@ -106,26 +100,29 @@ SL::DB::Buchungsgruppe - RDBO wrapper for the C<buchungsgruppen> table
 =item C<expense_accno_id $taxzone>
 
 Return the chart ID for the expense account for the given taxzone
-(either an integer between 0 and 3 inclusively or an instance of
-L<SL::DB::TaxZone>).
+(either the DB id or an instance of L<SL::DB::TaxZone>).
 
 =item C<expense_account>
 
 Return the chart (an instance of L<SL::DB::Chart>) for the expense
-account for the given taxzone (either an integer between 0 and 3
-inclusively or an instance of L<SL::DB::TaxZone>).
+account for the given taxzone (either the DB id or an instance of
+L<SL::DB::TaxZone>).
 
 =item C<income_accno_id>
 
 Return the chart ID for the income account for the given taxzone
-(either an integer between 0 and 3 inclusively or an instance of
+(either the DB id or an instance of L<SL::DB::TaxZone>).
 L<SL::DB::TaxZone>).
 
 =item C<income_account>
 
 Return the chart (an instance of L<SL::DB::Chart>) for the income
-account for the given taxzone (either an integer between 0 and 3
-inclusively or an instance of L<SL::DB::TaxZone>).
+account for the given taxzone (either the DB id or an instance of
+L<SL::DB::TaxZone>).
+
+=item C<orphaned>
+
+Checks whether this Buchungsgruppe is assigned to any parts.
 
 =back