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