8d321661d90868064b8b35e57138cb11d0245c15
[kivitendo-erp.git] / SL / DB / Order.pm
1 package SL::DB::Order;
2
3 use utf8;
4 use strict;
5
6 use SL::RecordLinks;
7
8 use SL::DB::MetaSetup::Order;
9 use SL::DB::Manager::Order;
10 use SL::DB::Invoice;
11
12 __PACKAGE__->meta->add_relationship(
13   orderitems => {
14     type         => 'one to many',
15     class        => 'SL::DB::OrderItem',
16     column_map   => { id => 'trans_id' },
17     manager_args => {
18       with_objects => [ 'part' ]
19     }
20   },
21   periodic_invoices_config => {
22     type                   => 'one to one',
23     class                  => 'SL::DB::PeriodicInvoicesConfig',
24     column_map             => { id => 'oe_id' },
25   },
26   periodic_invoices        => {
27     type                   => 'one to many',
28     class                  => 'SL::DB::PeriodicInvoice',
29     column_map             => { id => 'oe_id' },
30   },
31 );
32
33 __PACKAGE__->meta->initialize;
34
35 # methods
36
37 sub type {
38   my $self = shift;
39
40   return 'sales_order'       if $self->customer_id && ! $self->quotation;
41   return 'purchase_order'    if $self->vendor_id   && ! $self->quotation;
42   return 'sales_quotation'   if $self->customer_id &&   $self->quotation;
43   return 'request_quotation' if $self->vendor_id   &&   $self->quotation;
44
45   return;
46 }
47
48 sub is_type {
49   return shift->type eq shift;
50 }
51
52 sub invoices {
53   my $self   = shift;
54   my %params = @_;
55
56   if ($self->quotation) {
57     return [];
58   } else {
59     return SL::DB::Manager::Invoice->get_all(
60       query => [
61         ordnumber => $self->ordnumber,
62         @{ $params{query} || [] },
63       ]
64     );
65   }
66 }
67
68 sub abschlag_invoices {
69   return shift()->invoices(query => [ abschlag => 1 ]);
70 }
71
72 sub end_invoice {
73   return shift()->invoices(query => [ abschlag => 0 ]);
74 }
75
76 1;
77
78 __END__
79
80 =head1 NAME
81
82 SL::DB::Order - Order Datenbank Objekt.
83
84 =head1 FUNCTIONS
85
86 =head2 type
87
88 Returns one of the following string types:
89
90 =over 4
91
92 =item saes_order
93
94 =item purchase_order
95
96 =item sales_quotation
97
98 =item request_quotation
99
100 =back
101
102 =head2 is_type TYPE
103
104 Rreturns true if the order is of the given type.
105
106 =head1 BUGS
107
108 Nothing here yet.
109
110 =head1 AUTHOR
111
112 Sven Schöling <s.schoeling@linet-services.de>
113
114 =cut