is_sales methode für Order
[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 is_sales {
88   croak 'not an accessor' if @_ > 1;
89   return shift->customer_id;
90 }
91
92 sub invoices {
93   my $self   = shift;
94   my %params = @_;
95
96   if ($self->quotation) {
97     return [];
98   } else {
99     return SL::DB::Manager::Invoice->get_all(
100       query => [
101         ordnumber => $self->ordnumber,
102         @{ $params{query} || [] },
103       ]
104     );
105   }
106 }
107
108 sub abschlag_invoices {
109   return shift()->invoices(query => [ abschlag => 1 ]);
110 }
111
112 sub end_invoice {
113   return shift()->invoices(query => [ abschlag => 0 ]);
114 }
115
116 sub convert_to_invoice {
117   my ($self, %params) = @_;
118
119   croak("Conversion to invoices is only supported for sales records") unless $self->customer_id;
120
121   my $invoice;
122   if (!$self->db->do_transaction(sub {
123     $invoice = SL::DB::Invoice->new_from($self)->post(%params) || die;
124     $self->link_to_record($invoice);
125     $self->update_attributes(closed => 1);
126     # die;
127   })) {
128     return undef;
129   }
130
131   return $invoice;
132 }
133
134 1;
135
136 __END__
137
138 =head1 NAME
139
140 SL::DB::Order - Order Datenbank Objekt.
141
142 =head1 FUNCTIONS
143
144 =head2 C<type>
145
146 Returns one of the following string types:
147
148 =over 4
149
150 =item sales_order
151
152 =item purchase_order
153
154 =item sales_quotation
155
156 =item request_quotation
157
158 =back
159
160 =head2 C<is_type TYPE>
161
162 Returns true if the order is of the given type.
163
164 =head2 C<convert_to_invoice %params>
165
166 Creates a new invoice with C<$self> as the basis by calling
167 L<SL::DB::Invoice::new_from>. That invoice is posted, and C<$self> is
168 linked to the new invoice via L<SL::DB::RecordLink>. C<$self>'s
169 C<closed> attribute is set to C<true>, and C<$self> is saved.
170
171 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
172
173 Returns the new invoice instance on success and C<undef> on
174 failure. The whole process is run inside a transaction. On failure
175 nothing is created or changed in the database.
176
177 At the moment only sales quotations and sales orders can be converted.
178
179 =head2 C<create_sales_process>
180
181 Creates and saves a new sales process. Can only be called for sales
182 orders.
183
184 The newly created process will be linked bidirectionally to both
185 C<$self> and to all sales quotations that are linked to C<$self>.
186
187 Returns the newly created process instance.
188
189 =head1 BUGS
190
191 Nothing here yet.
192
193 =head1 AUTHOR
194
195 Sven Schöling <s.schoeling@linet-services.de>
196
197 =cut