In Helper kein use SL::DB::Object
[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 use SL::DB::Order;
12
13 use List::Util qw(first);
14
15 __PACKAGE__->meta->add_relationship(orderitems => { type         => 'one to many',
16                                                     class        => 'SL::DB::DeliveryOrderItem',
17                                                     column_map   => { id => 'delivery_order_id' },
18                                                     manager_args => { with_objects => [ 'part' ] }
19                                                   },
20                                    );
21
22 __PACKAGE__->meta->initialize;
23
24 # methods
25
26 sub items { goto &orderitems; }
27
28 sub items_sorted {
29   my ($self) = @_;
30
31   return [ sort {$a->id <=> $b->id } @{ $self->items } ];
32 }
33
34 sub sales_order {
35   my $self   = shift;
36   my %params = @_;
37
38   my $orders = SL::DB::Manager::Order->get_all(
39     query => [
40       ordnumber => $self->ordnumber,
41       @{ $params{query} || [] },
42     ],
43   );
44
45   return first { $_->is_type('sales_order') } @{ $orders };
46 }
47
48 sub type {
49   return shift->customer_id ? 'sales_delivery_order' : 'purchase_delivery_order';
50 }
51
52 sub displayable_state {
53   my ($self) = @_;
54
55   return join '; ',
56     ($self->closed    ? $::locale->text('closed')    : $::locale->text('open')),
57     ($self->delivered ? $::locale->text('delivered') : $::locale->text('not delivered'));
58 }
59
60 sub date {
61   goto &transdate;
62 }
63
64 1;