Preisupdater-Mixin in Order & Invoice includiert
[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::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   periodic_invoices        => {
34     type                   => 'one to many',
35     class                  => 'SL::DB::PeriodicInvoice',
36     column_map             => { id => 'oe_id' },
37   },
38   payment_term => {
39     type       => 'one to one',
40     class      => 'SL::DB::PaymentTerm',
41     column_map => { payment_id => 'id' },
42   },
43 );
44
45 __PACKAGE__->meta->initialize;
46
47 # methods
48
49 sub items { goto &orderitems; }
50
51 sub type {
52   my $self = shift;
53
54   return 'sales_order'       if $self->customer_id && ! $self->quotation;
55   return 'purchase_order'    if $self->vendor_id   && ! $self->quotation;
56   return 'sales_quotation'   if $self->customer_id &&   $self->quotation;
57   return 'request_quotation' if $self->vendor_id   &&   $self->quotation;
58
59   return;
60 }
61
62 sub is_type {
63   return shift->type eq shift;
64 }
65
66 sub invoices {
67   my $self   = shift;
68   my %params = @_;
69
70   if ($self->quotation) {
71     return [];
72   } else {
73     return SL::DB::Manager::Invoice->get_all(
74       query => [
75         ordnumber => $self->ordnumber,
76         @{ $params{query} || [] },
77       ]
78     );
79   }
80 }
81
82 sub abschlag_invoices {
83   return shift()->invoices(query => [ abschlag => 1 ]);
84 }
85
86 sub end_invoice {
87   return shift()->invoices(query => [ abschlag => 0 ]);
88 }
89
90 sub convert_to_invoice {
91   my ($self, %params) = @_;
92
93   croak("Conversion to invoices is only supported for sales records") unless $self->customer_id;
94
95   my $invoice;
96   if (!$self->db->do_transaction(sub {
97     $invoice = SL::DB::Invoice->new_from($self)->post(%params) || die;
98     $self->link_to_record($invoice);
99     $self->update_attributes(closed => 1);
100     # die;
101   })) {
102     return undef;
103   }
104
105   return $invoice;
106 }
107
108 1;
109
110 __END__
111
112 =head1 NAME
113
114 SL::DB::Order - Order Datenbank Objekt.
115
116 =head1 FUNCTIONS
117
118 =head2 type
119
120 Returns one of the following string types:
121
122 =over 4
123
124 =item saes_order
125
126 =item purchase_order
127
128 =item sales_quotation
129
130 =item request_quotation
131
132 =back
133
134 =head2 is_type TYPE
135
136 Rreturns true if the order is of the given type.
137
138 =item C<convert_to_invoice %params>
139
140 Creates a new invoice with C<$self> as the basis by calling
141 L<SL::DB::Invoice::new_from>. That invoice is posted, and C<$self> is
142 linked to the new invoice via L<SL::DB::RecordLink>. C<$self>'s
143 C<closed> attribute is set to C<true>, and C<$self> is saved.
144
145 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
146
147 Returns the new invoice instance on success and C<undef> on
148 failure. The whole process is run inside a transaction. On failure
149 nothing is created or changed in the database.
150
151 At the moment only sales quotations and sales orders can be converted.
152
153 =item C<create_sales_process>
154
155 Creates and saves a new sales process. Can only be called for sales
156 orders.
157
158 The newly created process will be linked bidirectionally to both
159 C<$self> and to all sales quotations that are linked to C<$self>.
160
161 Returns the newly created process instance.
162
163 =back
164
165 =head1 BUGS
166
167 Nothing here yet.
168
169 =head1 AUTHOR
170
171 Sven Schöling <s.schoeling@linet-services.de>
172
173 =cut