X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/0c227fb25b4f1547bf3b2be5e5711f0bffe591d1..2851ec9a444d93086dc5356122b7143711d955c0:/SL/DB/DeliveryOrder.pm diff --git a/SL/DB/DeliveryOrder.pm b/SL/DB/DeliveryOrder.pm index 15eeee0c5..fef0ee5a3 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', @@ -172,6 +178,96 @@ sub new_from { 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 => 0, + 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); + # add content if not already in description + my $new_description = $source->description_as_stripped_html; + $entries->{$part_id}->{$date}->{content} .= '