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