Update Dokumentation
[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 __PACKAGE__->before_save('_before_save_set_donumber');
24
25 # hooks
26
27 sub _before_save_set_donumber {
28   my ($self) = @_;
29
30   $self->create_trans_number if !$self->donumber;
31
32   return 1;
33 }
34
35 # methods
36
37 sub items { goto &orderitems; }
38
39 sub items_sorted {
40   my ($self) = @_;
41
42   return [ sort {$a->id <=> $b->id } @{ $self->items } ];
43 }
44
45 sub sales_order {
46   my $self   = shift;
47   my %params = @_;
48
49
50   require SL::DB::Order;
51   my $orders = SL::DB::Manager::Order->get_all(
52     query => [
53       ordnumber => $self->ordnumber,
54       @{ $params{query} || [] },
55     ],
56   );
57
58   return first { $_->is_type('sales_order') } @{ $orders };
59 }
60
61 sub type {
62   return shift->customer_id ? 'sales_delivery_order' : 'purchase_delivery_order';
63 }
64
65 sub displayable_state {
66   my ($self) = @_;
67
68   return join '; ',
69     ($self->closed    ? $::locale->text('closed')    : $::locale->text('open')),
70     ($self->delivered ? $::locale->text('delivered') : $::locale->text('not delivered'));
71 }
72
73 sub date {
74   goto &transdate;
75 }
76
77 1;