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