993920c01590f8fe0c46ab4c5e3e2a2de85df239
[kivitendo-erp.git] / SL / DB / Invoice.pm
1 # This file has been auto-generated only because it didn't exist.
2 # Feel free to modify it at will; it will not be overwritten automatically.
3
4 package SL::DB::Invoice;
5
6 use strict;
7
8 use List::Util qw(first);
9
10 use SL::DB::MetaSetup::Invoice;
11 use SL::DB::Manager::Invoice;
12
13 __PACKAGE__->meta->add_relationship(
14   invoiceitems => {
15     type         => 'one to many',
16     class        => 'SL::DB::InvoiceItem',
17     column_map   => { id => 'trans_id' },
18     manager_args => {
19       with_objects => [ 'part' ]
20     }
21   },
22 );
23
24 __PACKAGE__->meta->initialize;
25
26 # methods
27
28 # it is assumed, that ordnumbers are unique here.
29 sub first_order_by_ordnumber {
30   my $self = shift;
31
32   my $orders = SL::DB::Manager::Order->get_all(
33     query => [
34       ordnumber => $self->ordnumber,
35
36     ],
37   );
38
39   return first { $_->is_type('sales_order') } @{ $orders };
40 }
41
42 sub abschlag_percentage {
43   my $self         = shift;
44   my $order        = $self->first_order_by_ordnumber or return;
45   my $order_amount = $order->netamount               or return;
46   return $self->abschlag
47     ? $self->netamount / $order_amount
48     : undef;
49 }
50
51 sub taxamount {
52   my $self = shift;
53   die 'not a setter method' if @_;
54
55   return $self->amount - $self->netamount;
56 }
57
58 __PACKAGE__->meta->make_attr_helpers(taxamount => 'numeric(15,5)');
59
60 1;