RDBO Support.
[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__->attr_number($_, places => -2) for qw(amount netamount paid  marge_total marge_percent taxamount);
14 __PACKAGE__->attr_date($_) for qw(transdate gldate datepaid duedate deliverydate orddate quodate);
15 __PACKAGE__->attr_percent($_) for qw(abschlag_percentage);
16
17 __PACKAGE__->meta->add_relationship(
18   invoiceitems => {
19     type         => 'one to many',
20     class        => 'SL::DB::InvoiceItem',
21     column_map   => { id => 'trans_id' },
22     manager_args => {
23       with_objects => [ 'part' ]
24     }
25   },
26 );
27
28 __PACKAGE__->meta->initialize;
29
30 # methods
31
32 # it is assumed, that ordnumbers are unique here.
33 sub first_order_by_ordnumber {
34   my $self = shift;
35
36   my $orders = SL::DB::Manager::Order->get_all(
37     query => [
38       ordnumber => $self->ordnumber,
39
40     ],
41   );
42
43   return first { $_->is_type('sales_order') } @{ $orders };
44 }
45
46 sub abschlag_percentage {
47   my $self         = shift;
48   my $order        = $self->first_order_by_ordnumber or return;
49   my $order_amount = $order->netamount               or return;
50   return $self->abschlag
51     ? $self->netamount / $order_amount
52     : undef;
53 }
54
55 sub taxamount {
56   my $self = shift;
57   die 'not a setter method' if @_;
58
59   return $self->amount - $self->netamount;
60 }
61
62 1;