X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FOrder.pm;h=9e781734485e01648dd4fc09ebdf1b98ee908e79;hb=5e96049428deceda2ef857930bbf67311deccbcb;hp=8212a647e6bf3fd992b909c2a1d651ffcdddd9b1;hpb=3a4ddae030f1f4542ca9641c2807b549b583a61e;p=kivitendo-erp.git diff --git a/SL/DB/Order.pm b/SL/DB/Order.pm index 8212a647e..9e7817344 100644 --- a/SL/DB/Order.pm +++ b/SL/DB/Order.pm @@ -9,7 +9,6 @@ use List::Util qw(max); use SL::DB::MetaSetup::Order; use SL::DB::Manager::Order; -use SL::DB::Invoice; use SL::DB::Helper::FlattenToForm; use SL::DB::Helper::LinkedRecords; use SL::DB::Helper::PriceTaxCalculator; @@ -31,43 +30,43 @@ __PACKAGE__->meta->add_relationship( class => 'SL::DB::PeriodicInvoicesConfig', column_map => { id => 'oe_id' }, }, - periodic_invoices => { - type => 'one to many', - class => 'SL::DB::PeriodicInvoice', - column_map => { id => 'oe_id' }, - }, - payment_term => { - type => 'one to one', - class => 'SL::DB::PaymentTerm', - column_map => { payment_id => 'id' }, - }, - contact => { - type => 'one to one', - class => 'SL::DB::Contact', - column_map => { cp_id => 'cp_id' }, - }, - shipto => { - type => 'one to one', - class => 'SL::DB::Shipto', - column_map => { shipto_id => 'shipto_id' }, - }, - department => { - type => 'one to one', - class => 'SL::DB::Department', - column_map => { department_id => 'id' }, - }, - language => { - type => 'one to one', - class => 'SL::DB::Language', - column_map => { language_id => 'id' }, + custom_shipto => { + type => 'one to one', + class => 'SL::DB::Shipto', + column_map => { id => 'trans_id' }, + query_args => [ module => 'OE' ], }, ); __PACKAGE__->meta->initialize; +__PACKAGE__->before_save('_before_save_set_ord_quo_number'); + +# hooks + +sub _before_save_set_ord_quo_number { + my ($self) = @_; + + # ordnumber is 'NOT NULL'. Therefore make sure it's always set to at + # least an empty string, even if we're saving a quotation. + $self->ordnumber('') if !$self->ordnumber; + + my $field = $self->quotation ? 'quonumber' : 'ordnumber'; + $self->create_trans_number if !$self->$field; + + return 1; +} + # methods sub items { goto &orderitems; } +sub add_items { goto &add_orderitems; } + +sub items_sorted { + my ($self) = @_; + + return [ sort {$a->id <=> $b->id } @{ $self->items } ]; +} sub type { my $self = shift; @@ -98,7 +97,7 @@ sub displayable_type { sub is_sales { croak 'not an accessor' if @_ > 1; - return shift->customer_id; + return !!shift->customer_id; } sub invoices { @@ -108,6 +107,7 @@ sub invoices { if ($self->quotation) { return []; } else { + require SL::DB::Invoice; return SL::DB::Manager::Invoice->get_all( query => [ ordnumber => $self->ordnumber, @@ -137,11 +137,12 @@ sub convert_to_invoice { croak("Conversion to invoices is only supported for sales records") unless $self->customer_id; my $invoice; - if (!$self->db->do_transaction(sub { + if (!$self->db->with_transaction(sub { + require SL::DB::Invoice; $invoice = SL::DB::Invoice->new_from($self)->post(%params) || die; $self->link_to_record($invoice); $self->update_attributes(closed => 1); - # die; + 1; })) { return undef; } @@ -149,19 +150,42 @@ sub convert_to_invoice { return $invoice; } +sub convert_to_delivery_order { + my ($self, @args) = @_; + + my ($delivery_order, $custom_shipto); + if (!$self->db->with_transaction(sub { + require SL::DB::DeliveryOrder; + ($delivery_order, $custom_shipto) = SL::DB::DeliveryOrder->new_from($self, @args); + $delivery_order->save; + $custom_shipto->save if $custom_shipto; + $self->link_to_record($delivery_order); + $self->update_attributes(delivered => 1); + 1; + })) { + return wantarray ? () : undef; + } + + return wantarray ? ($delivery_order, $custom_shipto) : $delivery_order; +} + sub number { my $self = shift; my %number_method = ( sales_order => 'ordnumber', sales_quotation => 'quonumber', - puchase_order => 'ordnumber', + purchase_order => 'ordnumber', request_quotation => 'quonumber', ); return $self->${ \ $number_method{$self->type} }(@_); } +sub date { + goto &transdate; +} + 1; __END__ @@ -192,6 +216,28 @@ Returns one of the following string types: Returns true if the order is of the given type. +=head2 C + +Creates a new delivery order with C<$self> as the basis by calling +L. That delivery order is saved, and +C<$self> is linked to the new invoice via +L. C<$self>'s C attribute is set to +C, and C<$self> is saved. + +The arguments in C<%params> are passed to +L. + +Returns C on failure. Otherwise the return value depends on the +context. In list context the new delivery order and a shipto instance +will be returned. In scalar instance only the delivery order instance +is returned. + +Custom shipto addresses (the ones specific to the sales/purchase +record and not to the customer/vendor) are only linked from C +to C. Meaning C will not +be filled in that case. That's why a separate shipto object is created +and returned. + =head2 C Creates a new invoice with C<$self> as the basis by calling