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