X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/fc890e1040a469ee62b3661c5da2f3d2efb4cce9..2632ced6322ae7837105306b55d82448b750fe9d:/SL/DB/DeliveryOrder.pm diff --git a/SL/DB/DeliveryOrder.pm b/SL/DB/DeliveryOrder.pm index 284c15fc8..9bb76f2f4 100644 --- a/SL/DB/DeliveryOrder.pm +++ b/SL/DB/DeliveryOrder.pm @@ -14,7 +14,13 @@ use SL::DB::Helper::FlattenToForm; use SL::DB::Helper::LinkedRecords; use SL::DB::Helper::TransNumberGenerator; -use List::Util qw(first); +use SL::DB::Part; +use SL::DB::Unit; + +use SL::Helper::Number qw(_format_total _round_total); + +use List::Util qw(first notall); +use List::MoreUtils qw(any); __PACKAGE__->meta->add_relationship(orderitems => { type => 'one to many', class => 'SL::DB::DeliveryOrderItem', @@ -101,7 +107,7 @@ sub date { sub _clone_orderitem_cvar { my ($cvar) = @_; - my $cloned = Rose::DB::Object::Helpers::clone_and_reset($_); + my $cloned = $_->clone_and_reset; $cloned->sub_module('delivery_order_items'); return $cloned; @@ -130,22 +136,10 @@ sub new_from { # Custom shipto addresses (the ones specific to the sales/purchase # record and not to the customer/vendor) are only linked from - # shipto -> delivery_orders. Meaning delivery_orders.shipto_id - # will not be filled in that case. Therefore we have to return the - # new shipto object as a separate object so that the caller can - # save it, too. - my $custom_shipto; + # shipto â delivery_orders. Meaning delivery_orders.shipto_id + # will not be filled in that case. if (!$source->shipto_id && $source->id) { - my $old = $source->custom_shipto; - if ($old) { - $custom_shipto = SL::DB::Shipto->new( - map { +($_ => $old->$_) } - grep { !m{^ (?: itime | mtime | shipto_id | trans_id ) $}x } - map { $_->name } - @{ $old->meta->columns } - ); - $custom_shipto->module('DO'); - } + $args{custom_shipto} = $source->custom_shipto->clone($class) if $source->can('custom_shipto') && $source->custom_shipto; } else { $args{shipto_id} = $source->shipto_id; @@ -181,7 +175,93 @@ sub new_from { $delivery_order->items(\@items); - return ($delivery_order, $custom_shipto); + return $delivery_order; +} + +sub new_from_time_recordings { + my ($class, $sources, %params) = @_; + + croak("Unsupported object type in sources") if any { ref($_) ne 'SL::DB::TimeRecording' } @$sources; + croak("Cannot create delivery order from source records of different customers") if any { $_->customer_id != $sources->[0]->customer_id } @$sources; + + my %args = ( + is_sales => 1, + delivered => 1, + customer_id => $sources->[0]->customer_id, + taxzone_id => $sources->[0]->customer->taxzone_id, + currency_id => $sources->[0]->customer->currency_id, + employee_id => SL::DB::Manager::Employee->current->id, + salesman_id => SL::DB::Manager::Employee->current->id, + items => [], + ); + my $delivery_order = $class->new(%args); + $delivery_order->assign_attributes(%{ $params{attributes} }) if $params{attributes}; + + # - one item per part (article) + # - qty is sum of duration + # - description goes to item longdescription + # - ordered and summed by date + # - each description goes to an ordered list + # - (as time recording descriptions are formatted text by now, use stripped text) + # - merge same descriptions (todo) + # + + ### config + my $default_partnummer = 6; + my $default_part_id = SL::DB::Manager::Part->find_by(partnumber => $default_partnummer)->id; + + # check parts and collect entries + my %part_by_part_id; + my $entries; + foreach my $source (@$sources) { + my $part_id = $source->part_id ? $source->part_id + : $default_part_id ? $default_part_id + : undef; + + die 'article not found for entry "' . $source->displayable_times . '"' if !$part_id; + + if (!$part_by_part_id{$part_id}) { + $part_by_part_id{$part_id} = SL::DB::Part->new(id => $part_id)->load; + die 'article unit must be time based for entry "' . $source->displayable_times . '"' if !$part_by_part_id{$part_id}->unit_obj->is_time_based; + } + + my $date = $source->start_time->to_kivitendo; + $entries->{$part_id}->{$date}->{duration} += _round_total($source->duration_in_hours); + $entries->{$part_id}->{$date}->{content} .= '