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;
18 use Rose::DB::Object::Helpers qw(as_tree);
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::Shipto',
37 column_map => { id => 'trans_id' },
38 query_args => [ module => 'OE' ],
42 __PACKAGE__->meta->initialize;
44 __PACKAGE__->before_save('_before_save_set_ord_quo_number');
48 sub _before_save_set_ord_quo_number {
51 # ordnumber is 'NOT NULL'. Therefore make sure it's always set to at
52 # least an empty string, even if we're saving a quotation.
53 $self->ordnumber('') if !$self->ordnumber;
55 my $field = $self->quotation ? 'quonumber' : 'ordnumber';
56 $self->create_trans_number if !$self->$field;
63 sub items { goto &orderitems; }
64 sub add_items { goto &add_orderitems; }
69 return [ sort {$a->position <=> $b->position } @{ $self->items } ];
75 return 'sales_order' if $self->customer_id && ! $self->quotation;
76 return 'purchase_order' if $self->vendor_id && ! $self->quotation;
77 return 'sales_quotation' if $self->customer_id && $self->quotation;
78 return 'request_quotation' if $self->vendor_id && $self->quotation;
84 return shift->type eq shift;
87 sub displayable_type {
88 my $type = shift->type;
90 return $::locale->text('Sales quotation') if $type eq 'sales_quotation';
91 return $::locale->text('Request quotation') if $type eq 'request_quotation';
92 return $::locale->text('Sales Order') if $type eq 'sales_order';
93 return $::locale->text('Purchase Order') if $type eq 'purchase_order';
100 croak 'not an accessor' if @_ > 1;
101 return !!shift->customer_id;
108 if ($self->quotation) {
111 require SL::DB::Invoice;
112 return SL::DB::Manager::Invoice->get_all(
114 ordnumber => $self->ordnumber,
115 @{ $params{query} || [] },
121 sub displayable_state {
124 return $self->closed ? $::locale->text('closed') : $::locale->text('open');
127 sub abschlag_invoices {
128 return shift()->invoices(query => [ abschlag => 1 ]);
132 return shift()->invoices(query => [ abschlag => 0 ]);
135 sub convert_to_invoice {
136 my ($self, %params) = @_;
138 croak("Conversion to invoices is only supported for sales records") unless $self->customer_id;
141 if (!$self->db->with_transaction(sub {
142 require SL::DB::Invoice;
143 $invoice = SL::DB::Invoice->new_from($self)->post(%params) || die;
144 $self->link_to_record($invoice);
145 $self->update_attributes(closed => 1);
154 sub convert_to_delivery_order {
155 my ($self, @args) = @_;
157 my ($delivery_order, $custom_shipto);
158 if (!$self->db->with_transaction(sub {
159 require SL::DB::DeliveryOrder;
160 ($delivery_order, $custom_shipto) = SL::DB::DeliveryOrder->new_from($self, @args);
161 $delivery_order->save;
162 $custom_shipto->save if $custom_shipto;
163 $self->link_to_record($delivery_order);
164 $self->update_attributes(delivered => 1);
167 return wantarray ? () : undef;
170 return wantarray ? ($delivery_order, $custom_shipto) : $delivery_order;
176 my %number_method = (
177 sales_order => 'ordnumber',
178 sales_quotation => 'quonumber',
179 purchase_order => 'ordnumber',
180 request_quotation => 'quonumber',
183 return $self->${ \ $number_method{$self->type} }(@_);
187 $_[0]->is_sales ? $_[0]->customer : $_[0]->vendor;
200 SL::DB::Order - Order Datenbank Objekt.
206 Returns one of the following string types:
214 =item sales_quotation
216 =item request_quotation
220 =head2 C<is_type TYPE>
222 Returns true if the order is of the given type.
224 =head2 C<convert_to_delivery_order %params>
226 Creates a new delivery order with C<$self> as the basis by calling
227 L<SL::DB::DeliveryOrder::new_from>. That delivery order is saved, and
228 C<$self> is linked to the new invoice via
229 L<SL::DB::RecordLink>. C<$self>'s C<delivered> attribute is set to
230 C<true>, and C<$self> is saved.
232 The arguments in C<%params> are passed to
233 L<SL::DB::DeliveryOrder::new_from>.
235 Returns C<undef> on failure. Otherwise the return value depends on the
236 context. In list context the new delivery order and a shipto instance
237 will be returned. In scalar instance only the delivery order instance
240 Custom shipto addresses (the ones specific to the sales/purchase
241 record and not to the customer/vendor) are only linked from C<shipto>
242 to C<delivery_orders>. Meaning C<delivery_orders.shipto_id> will not
243 be filled in that case. That's why a separate shipto object is created
246 =head2 C<convert_to_invoice %params>
248 Creates a new invoice with C<$self> as the basis by calling
249 L<SL::DB::Invoice::new_from>. That invoice is posted, and C<$self> is
250 linked to the new invoice via L<SL::DB::RecordLink>. C<$self>'s
251 C<closed> attribute is set to C<true>, and C<$self> is saved.
253 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
255 Returns the new invoice instance on success and C<undef> on
256 failure. The whole process is run inside a transaction. On failure
257 nothing is created or changed in the database.
259 At the moment only sales quotations and sales orders can be converted.
261 =head2 C<create_sales_process>
263 Creates and saves a new sales process. Can only be called for sales
266 The newly created process will be linked bidirectionally to both
267 C<$self> and to all sales quotations that are linked to C<$self>.
269 Returns the newly created process instance.
277 Sven Schöling <s.schoeling@linet-services.de>