Merge branch 'master' of git@vc.linet-services.de:public/lx-office-erp
[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   periodic_invoices        => {
35     type                   => 'one to many',
36     class                  => 'SL::DB::PeriodicInvoice',
37     column_map             => { id => 'oe_id' },
38   },
39   payment_term => {
40     type       => 'one to one',
41     class      => 'SL::DB::PaymentTerm',
42     column_map => { payment_id => 'id' },
43   },
44   contact      => {
45     type       => 'one to one',
46     class      => 'SL::DB::Contact',
47     column_map => { cp_id => 'cp_id' },
48   },
49   shipto       => {
50     type       => 'one to one',
51     class      => 'SL::DB::Shipto',
52     column_map => { shipto_id => 'shipto_id' },
53   },
54   department   => {
55     type       => 'one to one',
56     class      => 'SL::DB::Department',
57     column_map => { department_id => 'id' },
58   },
59   language     => {
60     type       => 'one to one',
61     class      => 'SL::DB::Language',
62     column_map => { language_id => 'id' },
63   },
64 );
65
66 __PACKAGE__->meta->initialize;
67
68 # methods
69
70 sub items { goto &orderitems; }
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     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->do_transaction(sub {
141     $invoice = SL::DB::Invoice->new_from($self)->post(%params) || die;
142     $self->link_to_record($invoice);
143     $self->update_attributes(closed => 1);
144     # die;
145   })) {
146     return undef;
147   }
148
149   return $invoice;
150 }
151
152 1;
153
154 __END__
155
156 =head1 NAME
157
158 SL::DB::Order - Order Datenbank Objekt.
159
160 =head1 FUNCTIONS
161
162 =head2 C<type>
163
164 Returns one of the following string types:
165
166 =over 4
167
168 =item sales_order
169
170 =item purchase_order
171
172 =item sales_quotation
173
174 =item request_quotation
175
176 =back
177
178 =head2 C<is_type TYPE>
179
180 Returns true if the order is of the given type.
181
182 =head2 C<convert_to_invoice %params>
183
184 Creates a new invoice with C<$self> as the basis by calling
185 L<SL::DB::Invoice::new_from>. That invoice is posted, and C<$self> is
186 linked to the new invoice via L<SL::DB::RecordLink>. C<$self>'s
187 C<closed> attribute is set to C<true>, and C<$self> is saved.
188
189 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
190
191 Returns the new invoice instance on success and C<undef> on
192 failure. The whole process is run inside a transaction. On failure
193 nothing is created or changed in the database.
194
195 At the moment only sales quotations and sales orders can be converted.
196
197 =head2 C<create_sales_process>
198
199 Creates and saves a new sales process. Can only be called for sales
200 orders.
201
202 The newly created process will be linked bidirectionally to both
203 C<$self> and to all sales quotations that are linked to C<$self>.
204
205 Returns the newly created process instance.
206
207 =head1 BUGS
208
209 Nothing here yet.
210
211 =head1 AUTHOR
212
213 Sven Schöling <s.schoeling@linet-services.de>
214
215 =cut