Sub-Aliase 'items' auf 'orderitems' bzw. 'invoiceitems' zur späteren Verwendung eines...
[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 sub items { goto &invoiceitems; }
29
30 # it is assumed, that ordnumbers are unique here.
31 sub first_order_by_ordnumber {
32   my $self = shift;
33
34   my $orders = SL::DB::Manager::Order->get_all(
35     query => [
36       ordnumber => $self->ordnumber,
37
38     ],
39   );
40
41   return first { $_->is_type('sales_order') } @{ $orders };
42 }
43
44 sub abschlag_percentage {
45   my $self         = shift;
46   my $order        = $self->first_order_by_ordnumber or return;
47   my $order_amount = $order->netamount               or return;
48   return $self->abschlag
49     ? $self->netamount / $order_amount
50     : undef;
51 }
52
53 sub taxamount {
54   my $self = shift;
55   die 'not a setter method' if @_;
56
57   return $self->amount - $self->netamount;
58 }
59
60 __PACKAGE__->meta->make_attr_helpers(taxamount => 'numeric(15,5)');
61
62 1;