8 use List::Util qw(max);
10 use SL::DB::MetaSetup::Order;
11 use SL::DB::Manager::Order;
12 use SL::DB::Helper::AttrHTML;
13 use SL::DB::Helper::AttrSorted;
14 use SL::DB::Helper::FlattenToForm;
15 use SL::DB::Helper::LinkedRecords;
16 use SL::DB::Helper::PriceTaxCalculator;
17 use SL::DB::Helper::PriceUpdater;
18 use SL::DB::Helper::TransNumberGenerator;
20 use Rose::DB::Object::Helpers qw(as_tree);
22 __PACKAGE__->meta->add_relationship(
24 type => 'one to many',
25 class => 'SL::DB::OrderItem',
26 column_map => { id => 'trans_id' },
28 with_objects => [ 'part' ]
31 periodic_invoices_config => {
33 class => 'SL::DB::PeriodicInvoicesConfig',
34 column_map => { id => 'oe_id' },
38 class => 'SL::DB::Shipto',
39 column_map => { id => 'trans_id' },
40 query_args => [ module => 'OE' ],
44 __PACKAGE__->meta->initialize;
46 __PACKAGE__->attr_html('notes');
47 __PACKAGE__->attr_sorted('items');
49 __PACKAGE__->before_save('_before_save_set_ord_quo_number');
53 sub _before_save_set_ord_quo_number {
56 # ordnumber is 'NOT NULL'. Therefore make sure it's always set to at
57 # least an empty string, even if we're saving a quotation.
58 $self->ordnumber('') if !$self->ordnumber;
60 my $field = $self->quotation ? 'quonumber' : 'ordnumber';
61 $self->create_trans_number if !$self->$field;
68 sub items { goto &orderitems; }
69 sub add_items { goto &add_orderitems; }
70 sub record_number { goto &number; }
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';
98 sub displayable_name {
99 join ' ', grep $_, map $_[0]->$_, qw(displayable_type record_number);
103 croak 'not an accessor' if @_ > 1;
104 return !!shift->customer_id;
111 if ($self->quotation) {
114 require SL::DB::Invoice;
115 return SL::DB::Manager::Invoice->get_all(
117 ordnumber => $self->ordnumber,
118 @{ $params{query} || [] },
124 sub displayable_state {
127 return $self->closed ? $::locale->text('closed') : $::locale->text('open');
130 sub abschlag_invoices {
131 return shift()->invoices(query => [ abschlag => 1 ]);
135 return shift()->invoices(query => [ abschlag => 0 ]);
138 sub convert_to_invoice {
139 my ($self, %params) = @_;
141 croak("Conversion to invoices is only supported for sales records") unless $self->customer_id;
144 if (!$self->db->with_transaction(sub {
145 require SL::DB::Invoice;
146 $invoice = SL::DB::Invoice->new_from($self)->post(%params) || die;
147 $self->link_to_record($invoice);
148 $self->update_attributes(closed => 1);
157 sub convert_to_delivery_order {
158 my ($self, @args) = @_;
160 my ($delivery_order, $custom_shipto);
161 if (!$self->db->with_transaction(sub {
162 require SL::DB::DeliveryOrder;
163 ($delivery_order, $custom_shipto) = SL::DB::DeliveryOrder->new_from($self, @args);
164 $delivery_order->save;
165 $custom_shipto->save if $custom_shipto;
166 $self->link_to_record($delivery_order);
167 # TODO extend link_to_record for items, otherwise long-term no d.r.y.
168 foreach my $item (@{ $delivery_order->items }) {
169 foreach (qw(orderitems)) { # expand if needed (delivery_order_items)
170 if ($item->{"converted_from_${_}_id"}) {
171 die unless $item->{id};
172 RecordLinks->create_links('mode' => 'ids',
174 'from_ids' => $item->{"converted_from_${_}_id"},
175 'to_table' => 'delivery_order_items',
176 'to_id' => $item->{id},
178 delete $item->{"converted_from_${_}_id"};
183 $self->update_attributes(delivered => 1);
186 return wantarray ? () : undef;
189 return wantarray ? ($delivery_order, $custom_shipto) : $delivery_order;
195 my %number_method = (
196 sales_order => 'ordnumber',
197 sales_quotation => 'quonumber',
198 purchase_order => 'ordnumber',
199 request_quotation => 'quonumber',
202 return $self->${ \ $number_method{$self->type} }(@_);
206 $_[0]->is_sales ? $_[0]->customer : $_[0]->vendor;
219 SL::DB::Order - Order Datenbank Objekt.
225 Returns one of the following string types:
233 =item sales_quotation
235 =item request_quotation
239 =head2 C<is_type TYPE>
241 Returns true if the order is of the given type.
243 =head2 C<convert_to_delivery_order %params>
245 Creates a new delivery order with C<$self> as the basis by calling
246 L<SL::DB::DeliveryOrder::new_from>. That delivery order is saved, and
247 C<$self> is linked to the new invoice via
248 L<SL::DB::RecordLink>. C<$self>'s C<delivered> attribute is set to
249 C<true>, and C<$self> is saved.
251 The arguments in C<%params> are passed to
252 L<SL::DB::DeliveryOrder::new_from>.
254 Returns C<undef> on failure. Otherwise the return value depends on the
255 context. In list context the new delivery order and a shipto instance
256 will be returned. In scalar instance only the delivery order instance
259 Custom shipto addresses (the ones specific to the sales/purchase
260 record and not to the customer/vendor) are only linked from C<shipto>
261 to C<delivery_orders>. Meaning C<delivery_orders.shipto_id> will not
262 be filled in that case. That's why a separate shipto object is created
265 =head2 C<convert_to_invoice %params>
267 Creates a new invoice with C<$self> as the basis by calling
268 L<SL::DB::Invoice::new_from>. That invoice is posted, and C<$self> is
269 linked to the new invoice via L<SL::DB::RecordLink>. C<$self>'s
270 C<closed> attribute is set to C<true>, and C<$self> is saved.
272 The arguments in C<%params> are passed to L<SL::DB::Invoice::post>.
274 Returns the new invoice instance on success and C<undef> on
275 failure. The whole process is run inside a transaction. On failure
276 nothing is created or changed in the database.
278 At the moment only sales quotations and sales orders can be converted.
280 =head2 C<create_sales_process>
282 Creates and saves a new sales process. Can only be called for sales
285 The newly created process will be linked bidirectionally to both
286 C<$self> and to all sales quotations that are linked to C<$self>.
288 Returns the newly created process instance.
296 Sven Schöling <s.schoeling@linet-services.de>