itmes_sorted liefert immer eine Array-Referenz ...
[kivitendo-erp.git] / SL / DB / Order.pm
1 package SL::DB::Order;
2
3 use utf8;
4 use strict;
5
6 use Carp;
7 use DateTime;
8 use List::Util qw(max);
9
10 use SL::DB::MetaSetup::Order;
11 use SL::DB::Manager::Order;
12 use SL::DB::Invoice;
13 use SL::DB::Helper::FlattenToForm;
14 use SL::DB::Helper::LinkedRecords;
15 use SL::DB::Helper::PriceTaxCalculator;
16 use SL::DB::Helper::PriceUpdater;
17 use SL::DB::Helper::TransNumberGenerator;
18 use SL::RecordLinks;
19
20 __PACKAGE__->meta->add_relationship(
21   orderitems => {
22     type         => 'one to many',
23     class        => 'SL::DB::OrderItem',
24     column_map   => { id => 'trans_id' },
25     manager_args => {
26       with_objects => [ 'part' ]
27     }
28   },
29   periodic_invoices_config => {
30     type                   => 'one to one',
31     class                  => 'SL::DB::PeriodicInvoicesConfig',
32     column_map             => { id => 'oe_id' },
33   },
34 );
35
36 __PACKAGE__->meta->initialize;
37
38 # methods
39
40 sub items { goto &orderitems; }
41
42 sub items_sorted {
43   my ($self) = @_;
44
45   return [ sort {$a->id <=> $b->id } @{ $self->items } ];
46 }
47
48 sub type {
49   my $self = shift;
50
51   return 'sales_order'       if $self->customer_id && ! $self->quotation;
52   return 'purchase_order'    if $self->vendor_id   && ! $self->quotation;
53   return 'sales_quotation'   if $self->customer_id &&   $self->quotation;
54   return 'request_quotation' if $self->vendor_id   &&   $self->quotation;
55
56   return;
57 }
58
59 sub is_type {
60   return shift->type eq shift;
61 }
62
63 sub displayable_type {
64   my $type = shift->type;
65
66   return $::locale->text('Sales quotation')   if $type eq 'sales_quotation';
67   return $::locale->text('Request quotation') if $type eq 'request_quotation';
68   return $::locale->text('Sales Order')       if $type eq 'sales_order';
69   return $::locale->text('Purchase Order')    if $type eq 'purchase_order';
70
71   die 'invalid type';
72 }
73
74
75 sub is_sales {
76   croak 'not an accessor' if @_ > 1;
77   return !!shift->customer_id;
78 }
79
80 sub invoices {
81   my $self   = shift;
82   my %params = @_;
83
84   if ($self->quotation) {
85     return [];
86   } else {
87     return SL::DB::Manager::Invoice->get_all(
88       query => [
89         ordnumber => $self->ordnumber,
90         @{ $params{query} || [] },
91       ]
92     );
93   }
94 }
95
96 sub displayable_state {
97   my ($self) = @_;
98
99   return $self->closed ? $::locale->text('closed') : $::locale->text('open');
100 }
101
102 sub abschlag_invoices {
103   return shift()->invoices(query => [ abschlag => 1 ]);
104 }
105
106 sub end_invoice {
107   return shift()->invoices(query => [ abschlag => 0 ]);
108 }
109
110 sub convert_to_invoice {
111   my ($self, %params) = @_;
112
113   croak("Conversion to invoices is only supported for sales records") unless $self->customer_id;
114
115   my $invoice;
116   if (!$self->db->do_transaction(sub {
117     $invoice = SL::DB::Invoice->new_from($self)->post(%params) || die;
118     $self->link_to_record($invoice);
119     $self->update_attributes(closed => 1);
120     # die;
121   })) {
122     return undef;
123   }
124
125   return $invoice;
126 }
127
128 sub number {
129   my $self = shift;
130
131   my %number_method = (
132     sales_order       => 'ordnumber',
133     sales_quotation   => 'quonumber',
134     purchase_order    => 'ordnumber',
135     request_quotation => 'quonumber',
136   );
137
138   return $self->${ \ $number_method{$self->type} }(@_);
139 }
140
141 sub date {
142   goto &transdate;
143 }
144
145 1;
146
147 __END__
148
149 =head1 NAME
150
151 SL::DB::Order - Order Datenbank Objekt.
152
153 =head1 FUNCTIONS
154
155 =head2 C<type>
156
157 Returns one of the following string types:
158
159 =over 4
160
161 =item sales_order
162
163 =item purchase_order
164
165 =item sales_quotation
166
167 =item request_quotation
168
169 =back
170
171 =head2 C<is_type TYPE>
172
173 Returns true if the order is of the given type.
174
175 =head2 C<convert_to_invoice %params>
176
177 Creates a new invoice with C<$self> as the basis by calling
178 L<SL::DB::Invoice::new_from>. That invoice is posted, and C<$self> is
179 linked to the new invoice via L<SL::DB::RecordLink>. C<$self>'s
180 C<closed> attribute is set to C<true>, and C<$self> is saved.
181
182 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
183
184 Returns the new invoice instance on success and C<undef> on
185 failure. The whole process is run inside a transaction. On failure
186 nothing is created or changed in the database.
187
188 At the moment only sales quotations and sales orders can be converted.
189
190 =head2 C<create_sales_process>
191
192 Creates and saves a new sales process. Can only be called for sales
193 orders.
194
195 The newly created process will be linked bidirectionally to both
196 C<$self> and to all sales quotations that are linked to C<$self>.
197
198 Returns the newly created process instance.
199
200 =head1 BUGS
201
202 Nothing here yet.
203
204 =head1 AUTHOR
205
206 Sven Schöling <s.schoeling@linet-services.de>
207
208 =cut