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} .= '
  • ' . $source->description_as_stripped_html . '
  • '; + $entries->{$part_id}->{$date}->{date_obj} = $source->start_time; # for sorting + } + + my $h_unit = SL::DB::Manager::Unit->find_h_unit; + + my @keys = sort { $part_by_part_id{$a}->partnumber cmp $part_by_part_id{$b}->partnumber } keys %$entries; + foreach my $key (@keys) { + my $qty = 0; + my $longdescription = ''; + + my @dates = sort { $entries->{$key}->{$a}->{date_obj} <=> $entries->{$key}->{$b}->{date_obj} } keys %{$entries->{$key}}; + foreach my $date (@dates) { + my $entry = $entries->{$key}->{$date}; + + $qty += $entry->{duration}; + $longdescription .= $date . ' ' . _format_total($entry->{duration}) . ' h'; + $longdescription .= ''; + } + + my $item = SL::DB::DeliveryOrderItem->new( + parts_id => $part_by_part_id{$key}->id, + description => $part_by_part_id{$key}->description, + qty => $qty, + unit_obj => $h_unit, + sellprice => $part_by_part_id{$key}->sellprice, + longdescription => $longdescription, + ); + + $delivery_order->add_items($item); + } + + return $delivery_order; } sub customervendor { @@ -222,6 +302,15 @@ sub convert_to_invoice { return $invoice; } +sub digest { + my ($self) = @_; + + sprintf "%s %s (%s)", + $self->donumber, + $self->customervendor->name, + $self->date->to_kivitendo; +} + 1; __END__ @@ -267,18 +356,8 @@ quotations and purchase orders) are supported as sources. The conversion copies order items into delivery order items. Dates are copied as appropriate, e.g. the C field will be set to the current date. -Returns one or two objects depending on the context. In list context -the new delivery order instance 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. - -The objects returned are not saved. +Returns the new delivery order instance. The object returned is not +saved. C<%params> can include the following options: @@ -314,6 +393,31 @@ order. =back +=item C + +Creates a new C instace from the time recordings +given as C<$sources>. All time recording entries must belong to the same +customer. Time recordings are sorted by article and date. For each article +a new delivery order item is created. If no article is associated with an +entry, a default article will be used (hard coded). +Entries of the same date (for each article) are summed together and form a +list entry in the long description of the item. + +The created delivery order object will be returnd but not saved. + +C<$sources> must be an array reference of C instances. + +C<%params> can include the following options: + +=over 2 + +=item C + +An optional hash reference. If it exists then it is used to set +attributes of the newly created delivery order object. + +=back + =item C TODO: Describe sales_order