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