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