8 use List::Util qw(max);
10 use SL::DB::MetaSetup::Order;
11 use SL::DB::Manager::Order;
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;
20 __PACKAGE__->meta->add_relationship(
22 type => 'one to many',
23 class => 'SL::DB::OrderItem',
24 column_map => { id => 'trans_id' },
26 with_objects => [ 'part' ]
29 periodic_invoices_config => {
31 class => 'SL::DB::PeriodicInvoicesConfig',
32 column_map => { id => 'oe_id' },
34 periodic_invoices => {
35 type => 'one to many',
36 class => 'SL::DB::PeriodicInvoice',
37 column_map => { id => 'oe_id' },
41 class => 'SL::DB::PaymentTerm',
42 column_map => { payment_id => 'id' },
46 __PACKAGE__->meta->initialize;
50 sub items { goto &orderitems; }
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;
64 return shift->type eq shift;
71 if ($self->quotation) {
74 return SL::DB::Manager::Invoice->get_all(
76 ordnumber => $self->ordnumber,
77 @{ $params{query} || [] },
83 sub abschlag_invoices {
84 return shift()->invoices(query => [ abschlag => 1 ]);
88 return shift()->invoices(query => [ abschlag => 0 ]);
91 sub convert_to_invoice {
92 my ($self, %params) = @_;
94 croak("Conversion to invoices is only supported for sales records") unless $self->customer_id;
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);
115 SL::DB::Order - Order Datenbank Objekt.
121 Returns one of the following string types:
129 =item sales_quotation
131 =item request_quotation
137 Rreturns true if the order is of the given type.
139 =item C<convert_to_invoice %params>
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.
146 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
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.
152 At the moment only sales quotations and sales orders can be converted.
154 =item C<create_sales_process>
156 Creates and saves a new sales process. Can only be called for sales
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>.
162 Returns the newly created process instance.
172 Sven Schöling <s.schoeling@linet-services.de>