8 use List::Util qw(max);
10 use SL::DB::MetaSetup::Order;
11 use SL::DB::Manager::Order;
13 use SL::DB::Helper::LinkedRecords;
14 use SL::DB::Helper::PriceTaxCalculator;
15 use SL::DB::Helper::PriceUpdater;
16 use SL::DB::Helper::TransNumberGenerator;
19 __PACKAGE__->meta->add_relationship(
21 type => 'one to many',
22 class => 'SL::DB::OrderItem',
23 column_map => { id => 'trans_id' },
25 with_objects => [ 'part' ]
28 periodic_invoices_config => {
30 class => 'SL::DB::PeriodicInvoicesConfig',
31 column_map => { id => 'oe_id' },
33 periodic_invoices => {
34 type => 'one to many',
35 class => 'SL::DB::PeriodicInvoice',
36 column_map => { id => 'oe_id' },
40 class => 'SL::DB::PaymentTerm',
41 column_map => { payment_id => 'id' },
45 __PACKAGE__->meta->initialize;
49 sub items { goto &orderitems; }
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;
63 return shift->type eq shift;
70 if ($self->quotation) {
73 return SL::DB::Manager::Invoice->get_all(
75 ordnumber => $self->ordnumber,
76 @{ $params{query} || [] },
82 sub abschlag_invoices {
83 return shift()->invoices(query => [ abschlag => 1 ]);
87 return shift()->invoices(query => [ abschlag => 0 ]);
90 sub convert_to_invoice {
91 my ($self, %params) = @_;
93 croak("Conversion to invoices is only supported for sales records") unless $self->customer_id;
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);
114 SL::DB::Order - Order Datenbank Objekt.
120 Returns one of the following string types:
128 =item sales_quotation
130 =item request_quotation
136 Rreturns true if the order is of the given type.
138 =item C<convert_to_invoice %params>
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.
145 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
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.
151 At the moment only sales quotations and sales orders can be converted.
153 =item C<create_sales_process>
155 Creates and saves a new sales process. Can only be called for sales
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>.
161 Returns the newly created process instance.
171 Sven Schöling <s.schoeling@linet-services.de>