f4ca003a2d6cd61da17a5a3c30f7e08ccd3f0b93
[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   my @sorted =  sort {$a->id <=> $b->id } @{ $self->items };
46   return wantarray ? @sorted : \@sorted;
47 }
48
49 sub type {
50   my $self = shift;
51
52   return 'sales_order'       if $self->customer_id && ! $self->quotation;
53   return 'purchase_order'    if $self->vendor_id   && ! $self->quotation;
54   return 'sales_quotation'   if $self->customer_id &&   $self->quotation;
55   return 'request_quotation' if $self->vendor_id   &&   $self->quotation;
56
57   return;
58 }
59
60 sub is_type {
61   return shift->type eq shift;
62 }
63
64 sub displayable_type {
65   my $type = shift->type;
66
67   return $::locale->text('Sales quotation')   if $type eq 'sales_quotation';
68   return $::locale->text('Request quotation') if $type eq 'request_quotation';
69   return $::locale->text('Sales Order')       if $type eq 'sales_order';
70   return $::locale->text('Purchase Order')    if $type eq 'purchase_order';
71
72   die 'invalid type';
73 }
74
75
76 sub is_sales {
77   croak 'not an accessor' if @_ > 1;
78   return !!shift->customer_id;
79 }
80
81 sub invoices {
82   my $self   = shift;
83   my %params = @_;
84
85   if ($self->quotation) {
86     return [];
87   } else {
88     return SL::DB::Manager::Invoice->get_all(
89       query => [
90         ordnumber => $self->ordnumber,
91         @{ $params{query} || [] },
92       ]
93     );
94   }
95 }
96
97 sub displayable_state {
98   my ($self) = @_;
99
100   return $self->closed ? $::locale->text('closed') : $::locale->text('open');
101 }
102
103 sub abschlag_invoices {
104   return shift()->invoices(query => [ abschlag => 1 ]);
105 }
106
107 sub end_invoice {
108   return shift()->invoices(query => [ abschlag => 0 ]);
109 }
110
111 sub convert_to_invoice {
112   my ($self, %params) = @_;
113
114   croak("Conversion to invoices is only supported for sales records") unless $self->customer_id;
115
116   my $invoice;
117   if (!$self->db->do_transaction(sub {
118     $invoice = SL::DB::Invoice->new_from($self)->post(%params) || die;
119     $self->link_to_record($invoice);
120     $self->update_attributes(closed => 1);
121     # die;
122   })) {
123     return undef;
124   }
125
126   return $invoice;
127 }
128
129 sub number {
130   my $self = shift;
131
132   my %number_method = (
133     sales_order       => 'ordnumber',
134     sales_quotation   => 'quonumber',
135     purchase_order    => 'ordnumber',
136     request_quotation => 'quonumber',
137   );
138
139   return $self->${ \ $number_method{$self->type} }(@_);
140 }
141
142 sub date {
143   goto &transdate;
144 }
145
146 1;
147
148 __END__
149
150 =head1 NAME
151
152 SL::DB::Order - Order Datenbank Objekt.
153
154 =head1 FUNCTIONS
155
156 =head2 C<type>
157
158 Returns one of the following string types:
159
160 =over 4
161
162 =item sales_order
163
164 =item purchase_order
165
166 =item sales_quotation
167
168 =item request_quotation
169
170 =back
171
172 =head2 C<is_type TYPE>
173
174 Returns true if the order is of the given type.
175
176 =head2 C<convert_to_invoice %params>
177
178 Creates a new invoice with C<$self> as the basis by calling
179 L<SL::DB::Invoice::new_from>. That invoice is posted, and C<$self> is
180 linked to the new invoice via L<SL::DB::RecordLink>. C<$self>'s
181 C<closed> attribute is set to C<true>, and C<$self> is saved.
182
183 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
184
185 Returns the new invoice instance on success and C<undef> on
186 failure. The whole process is run inside a transaction. On failure
187 nothing is created or changed in the database.
188
189 At the moment only sales quotations and sales orders can be converted.
190
191 =head2 C<create_sales_process>
192
193 Creates and saves a new sales process. Can only be called for sales
194 orders.
195
196 The newly created process will be linked bidirectionally to both
197 C<$self> and to all sales quotations that are linked to C<$self>.
198
199 Returns the newly created process instance.
200
201 =head1 BUGS
202
203 Nothing here yet.
204
205 =head1 AUTHOR
206
207 Sven Schöling <s.schoeling@linet-services.de>
208
209 =cut