+sub get_taxkey {
+ my ($self, %params) = @_;
+
+ my $date = $params{date} || DateTime->today_local;
+ my $is_sales = !!$params{is_sales};
+ my $taxzone = $params{ defined($params{taxzone}) ? 'taxzone' : 'taxzone_id' } * 1;
+ my $tk_info = $::request->cache('get_taxkey');
+
+ $tk_info->{$self->id} //= {};
+ $tk_info->{$self->id}->{$taxzone} //= { };
+ my $cache = $tk_info->{$self->id}->{$taxzone}->{$is_sales} //= { };
+
+ if (!exists $cache->{$date}) {
+ $cache->{$date} =
+ $self->get_chart(type => $is_sales ? 'income' : 'expense', taxzone => $taxzone)
+ ->get_active_taxkey($date);
+ }
+
+ return $cache->{$date};
+}
+
+sub get_chart {
+ my ($self, %params) = @_;
+
+ my $type = (any { $_ eq $params{type} } qw(income expense inventory)) ? $params{type} : croak("Invalid 'type' parameter '$params{type}'");
+ my $taxzone = $params{ defined($params{taxzone}) ? 'taxzone' : 'taxzone_id' } * 1;
+
+ my $charts = $::request->cache('get_chart_id/by_part_id_and_taxzone')->{$self->id} //= {};
+ my $all_charts = $::request->cache('get_chart_id/by_id');
+
+ $charts->{$taxzone} ||= { };
+
+ if (!exists $charts->{$taxzone}->{$type}) {
+ require SL::DB::Buchungsgruppe;
+ my $bugru = SL::DB::Buchungsgruppe->load_cached($self->buchungsgruppen_id);
+ my $chart_id = ($type eq 'inventory') ? ($self->is_part ? $bugru->inventory_accno_id : undef)
+ : $bugru->call_sub("${type}_accno_id", $taxzone);
+
+ if ($chart_id) {
+ my $chart = $all_charts->{$chart_id} // SL::DB::Chart->load_cached($chart_id)->load;
+ $all_charts->{$chart_id} = $chart;
+ $charts->{$taxzone}->{$type} = $chart;
+ }
+ }
+
+ return $charts->{$taxzone}->{$type};
+}
+
+# this is designed to ignore chargenumbers, expiration dates and just give a list of how much <-> where
+sub get_simple_stock {
+ my ($self, %params) = @_;
+
+ return [] unless $self->id;
+
+ my $query = <<'';
+ SELECT sum(qty), warehouse_id, bin_id FROM inventory WHERE parts_id = ?
+ GROUP BY warehouse_id, bin_id
+
+ my $stock_info = selectall_hashref_query($::form, $::form->get_standard_dbh, $query, $self->id);
+ [ map { bless $_, 'SL::DB::Part::SimpleStock'} @$stock_info ];
+}
+# helper class to have bin/warehouse accessors in stock result
+{ package SL::DB::Part::SimpleStock;
+ sub warehouse { require SL::DB::Warehouse; SL::DB::Manager::Warehouse->find_by_or_create(id => $_[0]->{warehouse_id}) }
+ sub bin { require SL::DB::Bin; SL::DB::Manager::Bin ->find_by_or_create(id => $_[0]->{bin_id}) }
+}
+
+sub displayable_name {
+ join ' ', grep $_, map $_[0]->$_, qw(partnumber description);
+}
+
+sub clone_and_reset_deep {
+ my ($self) = @_;
+
+ my $clone = $self->clone_and_reset; # resets id and partnumber (primary key and unique constraint)
+ $clone->makemodels( map { $_->clone_and_reset } @{$self->makemodels} ) if @{$self->makemodels};
+ $clone->translations( map { $_->clone_and_reset } @{$self->translations} ) if @{$self->translations};
+
+ if ( $self->is_assortment ) {
+ # use clone rather than reset_and_clone because the unique constraint would also remove parts_id
+ $clone->assortment_items( map { $_->clone } @{$self->assortment_items} );
+ $_->assortment_id(undef) foreach @{ $clone->assortment_items }
+ };
+
+ if ( $self->is_assembly ) {
+ $clone->assemblies( map { $_->clone_and_reset } @{$self->assemblies});
+ };
+
+ if ( $self->prices ) {
+ $clone->prices( map { $_->clone } @{$self->prices}); # pricegroup_id gets reset here because it is part of a unique contraint
+ if ( $clone->prices ) {
+ foreach my $price ( @{$clone->prices} ) {
+ $price->id(undef);
+ $price->parts_id(undef);
+ };
+ };
+ };
+
+ return $clone;
+}
+
+sub item_diffs {
+ my ($self, $comparison_part) = @_;
+
+ die "item_diffs needs a part object" unless ref($comparison_part) eq 'SL::DB::Part';
+ die "part and comparison_part need to be of the same part_type" unless
+ ( $self->part_type eq 'assembly' or $self->part_type eq 'assortment' )
+ and ( $comparison_part->part_type eq 'assembly' or $comparison_part->part_type eq 'assortment' )
+ and $self->part_type eq $comparison_part->part_type;
+
+ # return [], [] if $self->items_checksum eq $comparison_part->items_checksum;
+ my @self_part_ids = map { $_->parts_id } $self->items;
+ my @comparison_part_ids = map { $_->parts_id } $comparison_part->items;
+
+ my %orig = map{ $_ => 1 } @self_part_ids;
+ my %comparison = map{ $_ => 1 } @comparison_part_ids;
+ my (@additions, @removals);
+ @additions = grep { !exists( $orig{$_} ) } @comparison_part_ids if @comparison_part_ids;
+ @removals = grep { !exists( $comparison{$_} ) } @self_part_ids if @self_part_ids;
+
+ return \@additions, \@removals;
+};
+
+sub items_sellprice_sum {
+ my ($self, %params) = @_;
+
+ return unless $self->is_assortment or $self->is_assembly;
+ return unless $self->items;
+
+ if ($self->is_assembly) {
+ return sum map { $_->linetotal_sellprice } @{$self->items};
+ } else {
+ return sum map { $_->linetotal_sellprice(%params) } grep { $_->charge } @{$self->items};
+ }
+}
+
+sub items_lastcost_sum {
+ my ($self) = @_;
+
+ return unless $self->is_assortment or $self->is_assembly;
+ return unless $self->items;
+ sum map { $_->linetotal_lastcost } @{$self->items};
+};
+
+sub assortment_lastcost_sum {
+ my ($self) = @_;
+
+ return unless $self->is_assortment;
+ sum map { $_->linetotal_lastcost } @{$self->assortment_items};
+};
+