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' },
36 class => 'SL::DB::PaymentTerm',
37 column_map => { payment_id => 'id' },
41 class => 'SL::DB::Contact',
42 column_map => { cp_id => 'cp_id' },
46 class => 'SL::DB::Shipto',
47 column_map => { shipto_id => 'shipto_id' },
51 class => 'SL::DB::Department',
52 column_map => { department_id => 'id' },
56 class => 'SL::DB::Language',
57 column_map => { language_id => 'id' },
61 __PACKAGE__->meta->initialize;
65 sub items { goto &orderitems; }
70 return 'sales_order' if $self->customer_id && ! $self->quotation;
71 return 'purchase_order' if $self->vendor_id && ! $self->quotation;
72 return 'sales_quotation' if $self->customer_id && $self->quotation;
73 return 'request_quotation' if $self->vendor_id && $self->quotation;
79 return shift->type eq shift;
82 sub displayable_type {
83 my $type = shift->type;
85 return $::locale->text('Sales quotation') if $type eq 'sales_quotation';
86 return $::locale->text('Request quotation') if $type eq 'request_quotation';
87 return $::locale->text('Sales Order') if $type eq 'sales_order';
88 return $::locale->text('Purchase Order') if $type eq 'purchase_order';
95 croak 'not an accessor' if @_ > 1;
96 return shift->customer_id;
103 if ($self->quotation) {
106 return SL::DB::Manager::Invoice->get_all(
108 ordnumber => $self->ordnumber,
109 @{ $params{query} || [] },
115 sub displayable_state {
118 return $self->closed ? $::locale->text('closed') : $::locale->text('open');
121 sub abschlag_invoices {
122 return shift()->invoices(query => [ abschlag => 1 ]);
126 return shift()->invoices(query => [ abschlag => 0 ]);
129 sub convert_to_invoice {
130 my ($self, %params) = @_;
132 croak("Conversion to invoices is only supported for sales records") unless $self->customer_id;
135 if (!$self->db->do_transaction(sub {
136 $invoice = SL::DB::Invoice->new_from($self)->post(%params) || die;
137 $self->link_to_record($invoice);
138 $self->update_attributes(closed => 1);
150 my %number_method = (
151 sales_order => 'ordnumber',
152 sales_quotation => 'quonumber',
153 purchase_order => 'ordnumber',
154 request_quotation => 'quonumber',
157 return $self->${ \ $number_method{$self->type} }(@_);
166 SL::DB::Order - Order Datenbank Objekt.
172 Returns one of the following string types:
180 =item sales_quotation
182 =item request_quotation
186 =head2 C<is_type TYPE>
188 Returns true if the order is of the given type.
190 =head2 C<convert_to_invoice %params>
192 Creates a new invoice with C<$self> as the basis by calling
193 L<SL::DB::Invoice::new_from>. That invoice is posted, and C<$self> is
194 linked to the new invoice via L<SL::DB::RecordLink>. C<$self>'s
195 C<closed> attribute is set to C<true>, and C<$self> is saved.
197 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
199 Returns the new invoice instance on success and C<undef> on
200 failure. The whole process is run inside a transaction. On failure
201 nothing is created or changed in the database.
203 At the moment only sales quotations and sales orders can be converted.
205 =head2 C<create_sales_process>
207 Creates and saves a new sales process. Can only be called for sales
210 The newly created process will be linked bidirectionally to both
211 C<$self> and to all sales quotations that are linked to C<$self>.
213 Returns the newly created process instance.
221 Sven Schöling <s.schoeling@linet-services.de>