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::TransNumberGenerator;
18 __PACKAGE__->meta->add_relationship(
20 type => 'one to many',
21 class => 'SL::DB::OrderItem',
22 column_map => { id => 'trans_id' },
24 with_objects => [ 'part' ]
27 periodic_invoices_config => {
29 class => 'SL::DB::PeriodicInvoicesConfig',
30 column_map => { id => 'oe_id' },
32 periodic_invoices => {
33 type => 'one to many',
34 class => 'SL::DB::PeriodicInvoice',
35 column_map => { id => 'oe_id' },
39 class => 'SL::DB::PaymentTerm',
40 column_map => { payment_id => 'id' },
44 __PACKAGE__->meta->initialize;
48 sub items { goto &orderitems; }
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;
62 return shift->type eq shift;
69 if ($self->quotation) {
72 return SL::DB::Manager::Invoice->get_all(
74 ordnumber => $self->ordnumber,
75 @{ $params{query} || [] },
81 sub abschlag_invoices {
82 return shift()->invoices(query => [ abschlag => 1 ]);
86 return shift()->invoices(query => [ abschlag => 0 ]);
89 sub convert_to_invoice {
90 my ($self, %params) = @_;
92 croak("Conversion to invoices is only supported for sales records") unless $self->customer_id;
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);
113 SL::DB::Order - Order Datenbank Objekt.
119 Returns one of the following string types:
127 =item sales_quotation
129 =item request_quotation
135 Rreturns true if the order is of the given type.
137 =item C<convert_to_invoice %params>
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.
144 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
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.
150 At the moment only sales quotations and sales orders can be converted.
152 =item C<create_sales_process>
154 Creates and saves a new sales process. Can only be called for sales
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>.
160 Returns the newly created process instance.
170 Sven Schöling <s.schoeling@linet-services.de>