eb467c541b5791ed5aca4c235fd52b0a16d09e2a
[kivitendo-erp.git] / SL / DB / Buchungsgruppe.pm
1 package SL::DB::Buchungsgruppe;
2
3 use strict;
4
5 use SL::DB::MetaSetup::Buchungsgruppe;
6 use SL::DB::Manager::Buchungsgruppe;
7 use SL::DB::Helper::ActsAsList;
8
9 __PACKAGE__->meta->add_relationship(
10   inventory_account => {
11     type          => 'many to one',
12     class         => 'SL::DB::Chart',
13     column_map    => { inventory_accno_id => 'id' },
14   },
15 );
16
17 __PACKAGE__->meta->initialize;
18
19 sub validate {
20   my ($self) = @_;
21
22   my @errors;
23   push @errors, $::locale->text('The description is missing.') if !$self->description;
24
25   return @errors;
26 }
27
28 sub income_accno_id {
29   my ($self, $taxzone) = @_;
30
31   require SL::DB::TaxZone;
32   require SL::DB::TaxzoneChart;
33
34   my $taxzone_id = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
35   my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by(taxzone_id => $taxzone_id, buchungsgruppen_id => $self->id);
36   return $taxzone_chart->income_accno_id if $taxzone_chart;
37 }
38
39 sub expense_accno_id {
40   my ($self, $taxzone) = @_;
41   require SL::DB::TaxZone;
42   require SL::DB::TaxzoneChart;
43
44   my $taxzone_id = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
45   my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by(taxzone_id => $taxzone_id, buchungsgruppen_id => $self->id);
46   return $taxzone_chart->expense_accno_id if $taxzone_chart;
47 }
48
49 sub income_account {
50   my ($self, $taxzone) = @_;
51
52   require SL::DB::TaxZone;
53   require SL::DB::TaxzoneChart;
54
55   my $taxzone_id       = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
56   my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by(taxzone_id => $taxzone_id, buchungsgruppen_id => $self->id);
57   return $taxzone_chart->income_accno if $taxzone_chart;
58 }
59
60 sub expense_account {
61   my ($self, $taxzone) = @_;
62
63   require SL::DB::TaxZone;
64   require SL::DB::TaxzoneChart;
65
66   my $taxzone_id       = ref $taxzone && $taxzone->isa('SL::DB::TaxZone') ? $taxzone->id : $taxzone;
67   my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by(taxzone_id => $taxzone_id, buchungsgruppen_id => $self->id);
68   return $taxzone_chart->expense_accno if $taxzone_chart;
69 }
70
71 sub taxzonecharts {
72   my ($self) = @_;
73   return SL::DB::Manager::TaxzoneChart->get_all(where => [ buchungsgruppen_id => $self->id ]);
74 }
75
76 sub orphaned {
77   my ($self) = @_;
78   die 'not an accessor' if @_ > 1;
79
80   require SL::DB::Part;
81   return 0 if SL::DB::Manager::Part->get_all_count(query => [ buchungsgruppen_id => $self->id ]);
82   return 1;
83 }
84
85 1;
86 __END__
87
88 =pod
89
90 =encoding utf8
91
92 =head1 NAME
93
94 SL::DB::Buchungsgruppe - RDBO wrapper for the C<buchungsgruppen> table
95
96 =head1 FUNCTIONS
97
98 =over 4
99
100 =item C<expense_accno_id $taxzone>
101
102 Return the chart ID for the expense account for the given taxzone
103 (either the DB id or an instance of L<SL::DB::TaxZone>).
104
105 =item C<expense_account>
106
107 Return the chart (an instance of L<SL::DB::Chart>) for the expense
108 account for the given taxzone (either the DB id or an instance of
109 L<SL::DB::TaxZone>).
110
111 =item C<income_accno_id>
112
113 Return the chart ID for the income account for the given taxzone
114 (either the DB id or an instance of L<SL::DB::TaxZone>).
115 L<SL::DB::TaxZone>).
116
117 =item C<income_account>
118
119 Return the chart (an instance of L<SL::DB::Chart>) for the income
120 account for the given taxzone (either the DB id or an instance of
121 L<SL::DB::TaxZone>).
122
123 =item C<orphaned>
124
125 Checks whether this Buchungsgruppe is assigned to any parts.
126
127 =back
128
129 =head1 BUGS
130
131 Nothing here yet.
132
133 =head1 AUTHOR
134
135 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>,
136 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
137
138 =cut