Unnötige Relationship (ist schon FK)
[kivitendo-erp.git] / SL / DB / DeliveryOrder.pm
1 package SL::DB::DeliveryOrder;
2
3 use strict;
4
5 use Carp;
6
7 use SL::DB::MetaSetup::DeliveryOrder;
8 use SL::DB::Manager::DeliveryOrder;
9 use SL::DB::Helper::LinkedRecords;
10 use SL::DB::Helper::TransNumberGenerator;
11
12 use List::Util qw(first);
13
14 __PACKAGE__->meta->add_relationship(orderitems => { type         => 'one to many',
15                                                     class        => 'SL::DB::DeliveryOrderItem',
16                                                     column_map   => { id => 'delivery_order_id' },
17                                                     manager_args => { with_objects => [ 'part' ] }
18                                                   },
19                                    );
20
21 __PACKAGE__->meta->initialize;
22
23 # methods
24
25 sub items { goto &orderitems; }
26
27 sub items_sorted {
28   my ($self) = @_;
29
30   return [ sort {$a->id <=> $b->id } @{ $self->items } ];
31 }
32
33 sub sales_order {
34   my $self   = shift;
35   my %params = @_;
36
37
38   require SL::DB::Order;
39   my $orders = SL::DB::Manager::Order->get_all(
40     query => [
41       ordnumber => $self->ordnumber,
42       @{ $params{query} || [] },
43     ],
44   );
45
46   return first { $_->is_type('sales_order') } @{ $orders };
47 }
48
49 sub type {
50   return shift->customer_id ? 'sales_delivery_order' : 'purchase_delivery_order';
51 }
52
53 sub displayable_state {
54   my ($self) = @_;
55
56   return join '; ',
57     ($self->closed    ? $::locale->text('closed')    : $::locale->text('open')),
58     ($self->delivered ? $::locale->text('delivered') : $::locale->text('not delivered'));
59 }
60
61 sub date {
62   goto &transdate;
63 }
64
65 1;