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