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;
37 __PACKAGE__->before_save('_before_save_set_ord_quo_number');
41 sub _before_save_set_ord_quo_number {
44 # ordnumber is 'NOT NULL'. Therefore make sure it's always set to at
45 # least an empty string, even if we're saving a quotation.
46 $self->ordnumber('') if !$self->ordnumber;
48 my $field = $self->quotation ? 'quonumber' : 'ordnumber';
49 $self->create_trans_number if !$self->$field;
56 sub items { goto &orderitems; }
61 return [ sort {$a->id <=> $b->id } @{ $self->items } ];
67 return 'sales_order' if $self->customer_id && ! $self->quotation;
68 return 'purchase_order' if $self->vendor_id && ! $self->quotation;
69 return 'sales_quotation' if $self->customer_id && $self->quotation;
70 return 'request_quotation' if $self->vendor_id && $self->quotation;
76 return shift->type eq shift;
79 sub displayable_type {
80 my $type = shift->type;
82 return $::locale->text('Sales quotation') if $type eq 'sales_quotation';
83 return $::locale->text('Request quotation') if $type eq 'request_quotation';
84 return $::locale->text('Sales Order') if $type eq 'sales_order';
85 return $::locale->text('Purchase Order') if $type eq 'purchase_order';
92 croak 'not an accessor' if @_ > 1;
93 return !!shift->customer_id;
100 if ($self->quotation) {
103 require SL::DB::Invoice;
104 return SL::DB::Manager::Invoice->get_all(
106 ordnumber => $self->ordnumber,
107 @{ $params{query} || [] },
113 sub displayable_state {
116 return $self->closed ? $::locale->text('closed') : $::locale->text('open');
119 sub abschlag_invoices {
120 return shift()->invoices(query => [ abschlag => 1 ]);
124 return shift()->invoices(query => [ abschlag => 0 ]);
127 sub convert_to_invoice {
128 my ($self, %params) = @_;
130 croak("Conversion to invoices is only supported for sales records") unless $self->customer_id;
133 if (!$self->db->with_transaction(sub {
134 require SL::DB::Invoice;
135 $invoice = SL::DB::Invoice->new_from($self)->post(%params) || die;
136 $self->link_to_record($invoice);
137 $self->update_attributes(closed => 1);
146 sub convert_to_delivery_order {
147 my ($self, %params) = @_;
149 my ($delivery_order, $custom_shipto);
150 if (!$self->db->with_transaction(sub {
151 require SL::DB::DeliveryOrder;
152 ($delivery_order, $custom_shipto) = SL::DB::DeliveryOrder->new_from($self, %params);
153 $delivery_order->save;
154 $custom_shipto->save if $custom_shipto;
155 $self->link_to_record($delivery_order);
156 $self->update_attributes(delivered => 1);
159 return wantarray ? () : undef;
162 return wantarray ? ($delivery_order, $custom_shipto) : $delivery_order;
168 my %number_method = (
169 sales_order => 'ordnumber',
170 sales_quotation => 'quonumber',
171 purchase_order => 'ordnumber',
172 request_quotation => 'quonumber',
175 return $self->${ \ $number_method{$self->type} }(@_);
188 SL::DB::Order - Order Datenbank Objekt.
194 Returns one of the following string types:
202 =item sales_quotation
204 =item request_quotation
208 =head2 C<is_type TYPE>
210 Returns true if the order is of the given type.
212 =head2 C<convert_to_delivery_order %params>
214 Creates a new delivery order with C<$self> as the basis by calling
215 L<SL::DB::DeliveryOrder::new_from>. That delivery order is saved, and
216 C<$self> is linked to the new invoice via
217 L<SL::DB::RecordLink>. C<$self>'s C<delivered> attribute is set to
218 C<true>, and C<$self> is saved.
220 The arguments in C<%params> are passed to
221 L<SL::DB::DeliveryOrder::new_from>.
223 Returns C<undef> on failure. Otherwise the return value depends on the
224 context. In list context the new delivery order and a shipto instance
225 will be returned. In scalar instance only the delivery order instance
228 Custom shipto addresses (the ones specific to the sales/purchase
229 record and not to the customer/vendor) are only linked from C<shipto>
230 to C<delivery_orders>. Meaning C<delivery_orders.shipto_id> will not
231 be filled in that case. That's why a separate shipto object is created
234 =head2 C<convert_to_invoice %params>
236 Creates a new invoice with C<$self> as the basis by calling
237 L<SL::DB::Invoice::new_from>. That invoice is posted, and C<$self> is
238 linked to the new invoice via L<SL::DB::RecordLink>. C<$self>'s
239 C<closed> attribute is set to C<true>, and C<$self> is saved.
241 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
243 Returns the new invoice instance on success and C<undef> on
244 failure. The whole process is run inside a transaction. On failure
245 nothing is created or changed in the database.
247 At the moment only sales quotations and sales orders can be converted.
249 =head2 C<create_sales_process>
251 Creates and saves a new sales process. Can only be called for sales
254 The newly created process will be linked bidirectionally to both
255 C<$self> and to all sales quotations that are linked to C<$self>.
257 Returns the newly created process instance.
265 Sven Schöling <s.schoeling@linet-services.de>