Item-Positionen in DB: items_sorted sortiert nach postition …
[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::Helper::FlattenToForm;
13 use SL::DB::Helper::LinkedRecords;
14 use SL::DB::Helper::PriceTaxCalculator;
15 use SL::DB::Helper::PriceUpdater;
16 use SL::DB::Helper::TransNumberGenerator;
17 use SL::RecordLinks;
18 use Rose::DB::Object::Helpers qw(as_tree);
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   custom_shipto            => {
35     type                   => 'one to one',
36     class                  => 'SL::DB::Shipto',
37     column_map             => { id => 'trans_id' },
38     query_args             => [ module => 'OE' ],
39   },
40 );
41
42 __PACKAGE__->meta->initialize;
43
44 __PACKAGE__->before_save('_before_save_set_ord_quo_number');
45
46 # hooks
47
48 sub _before_save_set_ord_quo_number {
49   my ($self) = @_;
50
51   # ordnumber is 'NOT NULL'. Therefore make sure it's always set to at
52   # least an empty string, even if we're saving a quotation.
53   $self->ordnumber('') if !$self->ordnumber;
54
55   my $field = $self->quotation ? 'quonumber' : 'ordnumber';
56   $self->create_trans_number if !$self->$field;
57
58   return 1;
59 }
60
61 # methods
62
63 sub items { goto &orderitems; }
64 sub add_items { goto &add_orderitems; }
65
66 sub items_sorted {
67   my ($self) = @_;
68
69   return [ sort {$a->position <=> $b->position } @{ $self->items } ];
70 }
71
72 sub type {
73   my $self = shift;
74
75   return 'sales_order'       if $self->customer_id && ! $self->quotation;
76   return 'purchase_order'    if $self->vendor_id   && ! $self->quotation;
77   return 'sales_quotation'   if $self->customer_id &&   $self->quotation;
78   return 'request_quotation' if $self->vendor_id   &&   $self->quotation;
79
80   return;
81 }
82
83 sub is_type {
84   return shift->type eq shift;
85 }
86
87 sub displayable_type {
88   my $type = shift->type;
89
90   return $::locale->text('Sales quotation')   if $type eq 'sales_quotation';
91   return $::locale->text('Request quotation') if $type eq 'request_quotation';
92   return $::locale->text('Sales Order')       if $type eq 'sales_order';
93   return $::locale->text('Purchase Order')    if $type eq 'purchase_order';
94
95   die 'invalid type';
96 }
97
98
99 sub is_sales {
100   croak 'not an accessor' if @_ > 1;
101   return !!shift->customer_id;
102 }
103
104 sub invoices {
105   my $self   = shift;
106   my %params = @_;
107
108   if ($self->quotation) {
109     return [];
110   } else {
111     require SL::DB::Invoice;
112     return SL::DB::Manager::Invoice->get_all(
113       query => [
114         ordnumber => $self->ordnumber,
115         @{ $params{query} || [] },
116       ]
117     );
118   }
119 }
120
121 sub displayable_state {
122   my ($self) = @_;
123
124   return $self->closed ? $::locale->text('closed') : $::locale->text('open');
125 }
126
127 sub abschlag_invoices {
128   return shift()->invoices(query => [ abschlag => 1 ]);
129 }
130
131 sub end_invoice {
132   return shift()->invoices(query => [ abschlag => 0 ]);
133 }
134
135 sub convert_to_invoice {
136   my ($self, %params) = @_;
137
138   croak("Conversion to invoices is only supported for sales records") unless $self->customer_id;
139
140   my $invoice;
141   if (!$self->db->with_transaction(sub {
142     require SL::DB::Invoice;
143     $invoice = SL::DB::Invoice->new_from($self)->post(%params) || die;
144     $self->link_to_record($invoice);
145     $self->update_attributes(closed => 1);
146     1;
147   })) {
148     return undef;
149   }
150
151   return $invoice;
152 }
153
154 sub convert_to_delivery_order {
155   my ($self, @args) = @_;
156
157   my ($delivery_order, $custom_shipto);
158   if (!$self->db->with_transaction(sub {
159     require SL::DB::DeliveryOrder;
160     ($delivery_order, $custom_shipto) = SL::DB::DeliveryOrder->new_from($self, @args);
161     $delivery_order->save;
162     $custom_shipto->save if $custom_shipto;
163     $self->link_to_record($delivery_order);
164     $self->update_attributes(delivered => 1);
165     1;
166   })) {
167     return wantarray ? () : undef;
168   }
169
170   return wantarray ? ($delivery_order, $custom_shipto) : $delivery_order;
171 }
172
173 sub number {
174   my $self = shift;
175
176   my %number_method = (
177     sales_order       => 'ordnumber',
178     sales_quotation   => 'quonumber',
179     purchase_order    => 'ordnumber',
180     request_quotation => 'quonumber',
181   );
182
183   return $self->${ \ $number_method{$self->type} }(@_);
184 }
185
186 sub customervendor {
187   $_[0]->is_sales ? $_[0]->customer : $_[0]->vendor;
188 }
189
190 sub date {
191   goto &transdate;
192 }
193
194 1;
195
196 __END__
197
198 =head1 NAME
199
200 SL::DB::Order - Order Datenbank Objekt.
201
202 =head1 FUNCTIONS
203
204 =head2 C<type>
205
206 Returns one of the following string types:
207
208 =over 4
209
210 =item sales_order
211
212 =item purchase_order
213
214 =item sales_quotation
215
216 =item request_quotation
217
218 =back
219
220 =head2 C<is_type TYPE>
221
222 Returns true if the order is of the given type.
223
224 =head2 C<convert_to_delivery_order %params>
225
226 Creates a new delivery order with C<$self> as the basis by calling
227 L<SL::DB::DeliveryOrder::new_from>. That delivery order is saved, and
228 C<$self> is linked to the new invoice via
229 L<SL::DB::RecordLink>. C<$self>'s C<delivered> attribute is set to
230 C<true>, and C<$self> is saved.
231
232 The arguments in C<%params> are passed to
233 L<SL::DB::DeliveryOrder::new_from>.
234
235 Returns C<undef> on failure. Otherwise the return value depends on the
236 context. In list context the new delivery order and a shipto instance
237 will be returned. In scalar instance only the delivery order instance
238 is returned.
239
240 Custom shipto addresses (the ones specific to the sales/purchase
241 record and not to the customer/vendor) are only linked from C<shipto>
242 to C<delivery_orders>. Meaning C<delivery_orders.shipto_id> will not
243 be filled in that case. That's why a separate shipto object is created
244 and returned.
245
246 =head2 C<convert_to_invoice %params>
247
248 Creates a new invoice with C<$self> as the basis by calling
249 L<SL::DB::Invoice::new_from>. That invoice is posted, and C<$self> is
250 linked to the new invoice via L<SL::DB::RecordLink>. C<$self>'s
251 C<closed> attribute is set to C<true>, and C<$self> is saved.
252
253 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
254
255 Returns the new invoice instance on success and C<undef> on
256 failure. The whole process is run inside a transaction. On failure
257 nothing is created or changed in the database.
258
259 At the moment only sales quotations and sales orders can be converted.
260
261 =head2 C<create_sales_process>
262
263 Creates and saves a new sales process. Can only be called for sales
264 orders.
265
266 The newly created process will be linked bidirectionally to both
267 C<$self> and to all sales quotations that are linked to C<$self>.
268
269 Returns the newly created process instance.
270
271 =head1 BUGS
272
273 Nothing here yet.
274
275 =head1 AUTHOR
276
277 Sven Schöling <s.schoeling@linet-services.de>
278
279 =cut