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