8 use List::Util qw(max);
10 use SL::DB::MetaSetup::Order;
11 use SL::DB::Manager::Order;
12 use SL::DB::Helper::FlattenToForm;
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' },
35 __PACKAGE__->meta->initialize;
39 sub items { goto &orderitems; }
44 return [ sort {$a->id <=> $b->id } @{ $self->items } ];
50 return 'sales_order' if $self->customer_id && ! $self->quotation;
51 return 'purchase_order' if $self->vendor_id && ! $self->quotation;
52 return 'sales_quotation' if $self->customer_id && $self->quotation;
53 return 'request_quotation' if $self->vendor_id && $self->quotation;
59 return shift->type eq shift;
62 sub displayable_type {
63 my $type = shift->type;
65 return $::locale->text('Sales quotation') if $type eq 'sales_quotation';
66 return $::locale->text('Request quotation') if $type eq 'request_quotation';
67 return $::locale->text('Sales Order') if $type eq 'sales_order';
68 return $::locale->text('Purchase Order') if $type eq 'purchase_order';
75 croak 'not an accessor' if @_ > 1;
76 return !!shift->customer_id;
83 if ($self->quotation) {
86 require SL::DB::Invoice;
87 return SL::DB::Manager::Invoice->get_all(
89 ordnumber => $self->ordnumber,
90 @{ $params{query} || [] },
96 sub displayable_state {
99 return $self->closed ? $::locale->text('closed') : $::locale->text('open');
102 sub abschlag_invoices {
103 return shift()->invoices(query => [ abschlag => 1 ]);
107 return shift()->invoices(query => [ abschlag => 0 ]);
110 sub convert_to_invoice {
111 my ($self, %params) = @_;
113 croak("Conversion to invoices is only supported for sales records") unless $self->customer_id;
116 if (!$self->db->do_transaction(sub {
117 $invoice = SL::DB::Invoice->new_from($self)->post(%params) || die;
118 $self->link_to_record($invoice);
119 $self->update_attributes(closed => 1);
131 my %number_method = (
132 sales_order => 'ordnumber',
133 sales_quotation => 'quonumber',
134 purchase_order => 'ordnumber',
135 request_quotation => 'quonumber',
138 return $self->${ \ $number_method{$self->type} }(@_);
151 SL::DB::Order - Order Datenbank Objekt.
157 Returns one of the following string types:
165 =item sales_quotation
167 =item request_quotation
171 =head2 C<is_type TYPE>
173 Returns true if the order is of the given type.
175 =head2 C<convert_to_invoice %params>
177 Creates a new invoice with C<$self> as the basis by calling
178 L<SL::DB::Invoice::new_from>. That invoice is posted, and C<$self> is
179 linked to the new invoice via L<SL::DB::RecordLink>. C<$self>'s
180 C<closed> attribute is set to C<true>, and C<$self> is saved.
182 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
184 Returns the new invoice instance on success and C<undef> on
185 failure. The whole process is run inside a transaction. On failure
186 nothing is created or changed in the database.
188 At the moment only sales quotations and sales orders can be converted.
190 =head2 C<create_sales_process>
192 Creates and saves a new sales process. Can only be called for sales
195 The newly created process will be linked bidirectionally to both
196 C<$self> and to all sales quotations that are linked to C<$self>.
198 Returns the newly created process instance.
206 Sven Schöling <s.schoeling@linet-services.de>