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