d71caae9aa855d616712d371cf34ab5ea89fabad
[kivitendo-erp.git] / SL / DB / Order.pm
1 package SL::DB::Order;
2
3 use strict;
4
5 use SL::RecordLinks;
6
7 use SL::DB::MetaSetup::Order;
8 use SL::DB::Manager::Order;
9 use SL::DB::Invoice;
10
11 __PACKAGE__->attr_number($_, places => -2) for qw(amount netamount marge_total marge_percent);
12 __PACKAGE__->attr_date($_) for qw(transdate reqdate);
13 __PACKAGE__->attr_percent($_) for qw(marge_percent);
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 );
25
26 __PACKAGE__->meta->initialize;
27
28 # methods
29
30 sub type {
31   my $self = shift;
32
33   return 'sales_order'       if $self->customer_id && ! $self->quotation;
34   return 'purchase_order'    if $self->vendor_id   && ! $self->quotation;
35   return 'sales_quotation'   if $self->customer_id &&   $self->quotation;
36   return 'request_quotation' if $self->vendor_id   &&   $self->quotation;
37
38   return;
39 }
40
41 sub is_type {
42   return shift->type eq shift;
43 }
44
45 sub invoices {
46   my $self   = shift;
47   my %params = @_;
48
49   if ($self->quotation) {
50     return [];
51   } else {
52     return SL::DB::Manager::Invoice->get_all(
53       query => [
54         ordnumber => $self->ordnumber,
55         @{ $params{query} || [] },
56       ]
57     );
58   }
59 }
60
61 sub abschlag_invoices {
62   return shift()->invoices(query => [ abschlag => 1 ]);
63 }
64
65 sub end_invoice {
66   return shift()->invoices(query => [ abschlag => 0 ]);
67 }
68
69 1;
70
71 __END__
72
73 =head1 NAME
74
75 SL::DB::Order - Order Datenbank Objekt.
76
77 =head1 FUNCTIONS
78
79 =head2 type
80
81 Returns one of the following string types:
82
83 =over 4
84
85 =item saes_order
86
87 =item purchase_order
88
89 =item sales_quotation
90
91 =item request_quotation
92
93 =back
94
95 =head2 is_type TYPE
96
97 Rreturns true if the order is of the given type.
98
99 =head1 BUGS
100
101 Nothing here yet.
102
103 =head1 AUTHOR
104
105   Sven Schöling <s.schoeling@linet-services.de>
106
107 =cut