+sub action_part_picker_search {
+ my ($self) = @_;
+
+ my $search_term = $self->models->filtered->laundered->{all_substr_multi__ilike};
+ $search_term ||= $self->models->filtered->laundered->{all_with_makemodel_substr_multi__ilike};
+ $search_term ||= $self->models->filtered->laundered->{all_with_customer_partnumber_substr_multi__ilike};
+
+ $_[0]->render('part/part_picker_search', { layout => 0 }, search_term => $search_term);
+}
+
+sub action_part_picker_result {
+ $_[0]->render('part/_part_picker_result', { layout => 0 }, parts => $_[0]->parts);
+}
+
+sub action_show {
+ my ($self) = @_;
+
+ if ($::request->type eq 'json') {
+ my $part_hash;
+ if (!$self->part) {
+ # TODO error
+ } else {
+ $part_hash = $self->part->as_tree;
+ $part_hash->{cvars} = $self->part->cvar_as_hashref;
+ }
+
+ $self->render(\ SL::JSON::to_json($part_hash), { layout => 0, type => 'json', process => 0 });
+ }
+}
+
+# helper functions
+sub validate_add_items {
+ scalar @{$::form->{add_items}};
+}
+
+sub prepare_assortment_render_vars {
+ my ($self) = @_;
+
+ my %vars = ( items_sellprice_sum => $self->part->items_sellprice_sum,
+ items_lastcost_sum => $self->part->items_lastcost_sum,
+ assortment_html => $self->render_assortment_items_to_html( \@{$self->part->items} ),
+ );
+ $vars{items_sum_diff} = $vars{items_sellprice_sum} - $vars{items_lastcost_sum};
+
+ return \%vars;
+}
+
+sub prepare_assembly_render_vars {
+ my ($self) = @_;
+
+ croak("Need assembly item(s) to create a 'save as new' assembly.") unless $self->part->items;
+
+ my %vars = ( items_sellprice_sum => $self->part->items_sellprice_sum,
+ items_lastcost_sum => $self->part->items_lastcost_sum,
+ assembly_html => $self->render_assembly_items_to_html( \@{ $self->part->items } ),
+ );
+ $vars{items_sum_diff} = $vars{items_sellprice_sum} - $vars{items_lastcost_sum};
+
+ return \%vars;
+}
+
+sub add {
+ my ($self) = @_;
+
+ check_has_valid_part_type($self->part->part_type);
+
+ $self->_set_javascript;
+ $self->_setup_form_action_bar;
+
+ my %title_hash = ( part => t8('Add Part'),
+ assembly => t8('Add Assembly'),
+ service => t8('Add Service'),
+ assortment => t8('Add Assortment'),
+ );
+
+ $self->render(
+ 'part/form',
+ title => $title_hash{$self->part->part_type},
+ );
+}
+
+
+sub _set_javascript {
+ my ($self) = @_;
+ $::request->layout->use_javascript("${_}.js") for qw(kivi.Part kivi.File kivi.PriceRule ckeditor/ckeditor ckeditor/adapters/jquery kivi.ShopPart kivi.Validator);
+ $::request->layout->add_javascripts_inline("\$(function(){kivi.PriceRule.load_price_rules_for_part(@{[ $self->part->id ]})});") if $self->part->id;
+}
+
+sub recalc_item_totals {
+ my ($self, %params) = @_;
+
+ if ( $params{part_type} eq 'assortment' ) {
+ return 0 unless scalar @{$self->assortment_items};
+ } elsif ( $params{part_type} eq 'assembly' ) {
+ return 0 unless scalar @{$self->assembly_items};
+ } else {
+ carp "can only calculate sum for assortments and assemblies";
+ };
+
+ my $part = SL::DB::Part->new(part_type => $params{part_type});
+ if ( $part->is_assortment ) {
+ $part->assortment_items( @{$self->assortment_items} );
+ if ( $params{price_type} eq 'lastcost' ) {
+ return $part->items_lastcost_sum;
+ } else {
+ if ( $params{pricegroup_id} ) {
+ return $part->items_sellprice_sum(pricegroup_id => $params{pricegroup_id});
+ } else {
+ return $part->items_sellprice_sum;
+ };
+ }
+ } elsif ( $part->is_assembly ) {
+ $part->assemblies( @{$self->assembly_items} );
+ if ( $params{price_type} eq 'weight' ) {
+ return $part->items_weight_sum;
+ } elsif ( $params{price_type} eq 'lastcost' ) {
+ return $part->items_lastcost_sum;
+ } else {
+ return $part->items_sellprice_sum;
+ }
+ }
+}
+
+sub check_part_not_modified {
+ my ($self) = @_;
+
+ return !($::form->{last_modification} && ($self->part->last_modification ne $::form->{last_modification}));
+
+}
+
+sub parse_form {
+ my ($self) = @_;
+
+ my $is_new = !$self->part->id;
+
+ my $params = delete($::form->{part}) || { };
+
+ delete $params->{id};
+ $self->part->assign_attributes(%{ $params});
+ $self->part->bin_id(undef) unless $self->part->warehouse_id;
+
+ $self->normalize_text_blocks;
+
+ # Only reset items ([]) and rewrite from form if $::form->{assortment_items} isn't empty. This
+ # will be the case for used assortments when saving, or when a used assortment
+ # is "used as new"
+ if ( $self->part->is_assortment and $::form->{assortment_items} and scalar @{$::form->{assortment_items}}) {
+ $self->part->assortment_items([]);
+ $self->part->add_assortment_items(@{$self->assortment_items}); # assortment_items has a get_set_init
+ };
+
+ if ( $self->part->is_assembly and $::form->{assembly_items} and @{$::form->{assembly_items}} ) {
+ $self->part->assemblies([]); # completely rewrite assortments each time
+ $self->part->add_assemblies( @{ $self->assembly_items } );
+ };
+
+ $self->part->translations([]);
+ $self->parse_form_translations;
+
+ $self->part->prices([]);
+ $self->parse_form_prices;
+
+ $self->parse_form_customerprices;
+ $self->parse_form_makemodels;
+}
+
+sub parse_form_prices {
+ my ($self) = @_;
+ # only save prices > 0
+ my $prices = delete($::form->{prices}) || [];
+ foreach my $price ( @{$prices} ) {
+ my $sellprice = $::form->parse_amount(\%::myconfig, $price->{price});
+ next unless $sellprice > 0; # skip negative prices as well
+ my $p = SL::DB::Price->new(parts_id => $self->part->id,
+ pricegroup_id => $price->{pricegroup_id},
+ price => $sellprice,
+ );
+ $self->part->add_prices($p);
+ };
+}
+
+sub parse_form_translations {
+ my ($self) = @_;
+ # don't add empty translations
+ my $translations = delete($::form->{translations}) || [];
+ foreach my $translation ( @{$translations} ) {
+ next unless $translation->{translation};
+ my $t = SL::DB::Translation->new( %{$translation} ) or die "Can't create translation";
+ $self->part->add_translations( $translation );
+ };
+}
+
+sub parse_form_makemodels {
+ my ($self) = @_;
+
+ my $makemodels_map;
+ if ( $self->part->makemodels ) { # check for new parts or parts without makemodels
+ $makemodels_map = { map { $_->id => Rose::DB::Object::Helpers::clone($_) } @{$self->part->makemodels} };
+ };
+
+ $self->part->makemodels([]);
+
+ my $position = 0;
+ my $makemodels = delete($::form->{makemodels}) || [];
+ foreach my $makemodel ( @{$makemodels} ) {
+ next unless $makemodel->{make};
+ $position++;
+ my $vendor = SL::DB::Manager::Vendor->find_by(id => $makemodel->{make}) || die "Can't find vendor from make";
+
+ my $id = $makemodels_map->{$makemodel->{id}} ? $makemodels_map->{$makemodel->{id}}->id : undef;
+ my $mm = SL::DB::MakeModel->new( # parts_id => $self->part->id, # will be assigned by row add_makemodels
+ id => $id,
+ make => $makemodel->{make},
+ model => $makemodel->{model} || '',
+ lastcost => $::form->parse_amount(\%::myconfig, $makemodel->{lastcost_as_number}),
+ sortorder => $position,
+ );
+ if ($makemodels_map->{$mm->id} && !$makemodels_map->{$mm->id}->lastupdate && $makemodels_map->{$mm->id}->lastcost == 0 && $mm->lastcost == 0) {
+ # lastupdate isn't set, original lastcost is 0 and new lastcost is 0
+ # don't change lastupdate
+ } elsif ( !$makemodels_map->{$mm->id} && $mm->lastcost == 0 ) {
+ # new makemodel, no lastcost entered, leave lastupdate empty
+ } elsif ($makemodels_map->{$mm->id} && $makemodels_map->{$mm->id}->lastcost == $mm->lastcost) {
+ # lastcost hasn't changed, use original lastupdate
+ $mm->lastupdate($makemodels_map->{$mm->id}->lastupdate);
+ } else {
+ $mm->lastupdate(DateTime->now);
+ };
+ $self->part->makemodel( scalar @{$self->part->makemodels} ? 1 : 0 ); # do we need this boolean anymore?
+ $self->part->add_makemodels($mm);
+ };
+}
+
+sub parse_form_customerprices {
+ my ($self) = @_;
+
+ my $customerprices_map;
+ if ( $self->part->customerprices ) { # check for new parts or parts without customerprices
+ $customerprices_map = { map { $_->id => Rose::DB::Object::Helpers::clone($_) } @{$self->part->customerprices} };
+ };
+
+ $self->part->customerprices([]);
+
+ my $position = 0;
+ my $customerprices = delete($::form->{customerprices}) || [];
+ foreach my $customerprice ( @{$customerprices} ) {
+ next unless $customerprice->{customer_id};
+ $position++;
+ my $customer = SL::DB::Manager::Customer->find_by(id => $customerprice->{customer_id}) || die "Can't find customer from id";
+
+ my $id = $customerprices_map->{$customerprice->{id}} ? $customerprices_map->{$customerprice->{id}}->id : undef;
+ my $cu = SL::DB::PartCustomerPrice->new( # parts_id => $self->part->id, # will be assigned by row add_customerprices
+ id => $id,
+ customer_id => $customerprice->{customer_id},
+ customer_partnumber => $customerprice->{customer_partnumber} || '',
+ price => $::form->parse_amount(\%::myconfig, $customerprice->{price_as_number}),
+ sortorder => $position,
+ );
+ if ($customerprices_map->{$cu->id} && !$customerprices_map->{$cu->id}->lastupdate && $customerprices_map->{$cu->id}->price == 0 && $cu->price == 0) {
+ # lastupdate isn't set, original price is 0 and new lastcost is 0
+ # don't change lastupdate
+ } elsif ( !$customerprices_map->{$cu->id} && $cu->price == 0 ) {
+ # new customerprice, no lastcost entered, leave lastupdate empty
+ } elsif ($customerprices_map->{$cu->id} && $customerprices_map->{$cu->id}->price == $cu->price) {
+ # price hasn't changed, use original lastupdate
+ $cu->lastupdate($customerprices_map->{$cu->id}->lastupdate);
+ } else {
+ $cu->lastupdate(DateTime->now);
+ };
+ $self->part->add_customerprices($cu);
+ };
+}
+
+sub build_bin_select {
+ select_tag('part.bin_id', [ @{ $_[0]->warehouse->bins_sorted } ],
+ title_key => 'description',
+ default => $_[0]->bin->id,
+ );
+}
+
+
+# get_set_inits for partpicker
+
+sub init_parts {
+ if ($::form->{no_paginate}) {
+ $_[0]->models->disable_plugin('paginated');
+ }
+
+ $_[0]->models->get;
+}
+
+# get_set_inits for part controller
+sub init_part {
+ my ($self) = @_;
+
+ # used by edit, save, delete and add
+
+ if ( $::form->{part}{id} ) {
+ return SL::DB::Part->new(id => $::form->{part}{id})->load(with => [ qw(makemodels customerprices prices translations partsgroup shop_parts shop_parts.shop) ]);
+ } elsif ( $::form->{id} ) {
+ return SL::DB::Part->new(id => $::form->{id})->load; # used by inventory tab
+ } else {
+ die "part_type missing" unless $::form->{part}{part_type};
+ return SL::DB::Part->new(part_type => $::form->{part}{part_type});
+ };
+}
+
+sub init_orphaned {
+ my ($self) = @_;
+ return $self->part->orphaned;
+}
+
+sub init_models {
+ my ($self) = @_;
+
+ SL::Controller::Helper::GetModels->new(
+ controller => $self,
+ sorted => {
+ _default => {
+ by => 'partnumber',
+ dir => 1,
+ },
+ partnumber => t8('Partnumber'),
+ description => t8('Description'),
+ },
+ with_objects => [ qw(unit_obj classification) ],
+ );
+}
+
+sub init_p {
+ SL::Presenter->get;
+}
+
+
+sub init_assortment_items {
+ # this init is used while saving and whenever assortments change dynamically
+ my ($self) = @_;
+ my $position = 0;
+ my @array;
+ my $assortment_items = delete($::form->{assortment_items}) || [];
+ foreach my $assortment_item ( @{$assortment_items} ) {
+ next unless $assortment_item->{parts_id};
+ $position++;
+ my $part = SL::DB::Manager::Part->find_by(id => $assortment_item->{parts_id}) || die "Can't determine item to be added";
+ my $ai = SL::DB::AssortmentItem->new( parts_id => $part->id,
+ qty => $::form->parse_amount(\%::myconfig, $assortment_item->{qty_as_number}),
+ charge => $assortment_item->{charge},
+ unit => $assortment_item->{unit} || $part->unit,
+ position => $position,
+ );
+
+ push(@array, $ai);
+ };
+ return \@array;
+}
+
+sub init_makemodels {
+ my ($self) = @_;
+
+ my $position = 0;
+ my @makemodel_array = ();
+ my $makemodels = delete($::form->{makemodels}) || [];
+
+ foreach my $makemodel ( @{$makemodels} ) {
+ next unless $makemodel->{make};
+ $position++;
+ my $mm = SL::DB::MakeModel->new( # parts_id => $self->part->id, # will be assigned by row add_makemodels
+ id => $makemodel->{id},
+ make => $makemodel->{make},
+ model => $makemodel->{model} || '',
+ lastcost => $::form->parse_amount(\%::myconfig, $makemodel->{lastcost_as_number} || 0),
+ sortorder => $position,
+ ) or die "Can't create mm";
+ # $mm->id($makemodel->{id}) if $makemodel->{id};
+ push(@makemodel_array, $mm);
+ };
+ return \@makemodel_array;
+}
+
+sub init_customerprices {
+ my ($self) = @_;
+
+ my $position = 0;
+ my @customerprice_array = ();
+ my $customerprices = delete($::form->{customerprices}) || [];
+
+ foreach my $customerprice ( @{$customerprices} ) {
+ next unless $customerprice->{customer_id};
+ $position++;
+ my $cu = SL::DB::PartCustomerPrice->new( # parts_id => $self->part->id, # will be assigned by row add_customerprices
+ id => $customerprice->{id},
+ customer_partnumber => $customerprice->{customer_partnumber},
+ customer_id => $customerprice->{customer_id} || '',
+ price => $::form->parse_amount(\%::myconfig, $customerprice->{price_as_number} || 0),
+ sortorder => $position,
+ ) or die "Can't create cu";
+ # $cu->id($customerprice->{id}) if $customerprice->{id};
+ push(@customerprice_array, $cu);
+ };
+ return \@customerprice_array;
+}
+
+sub init_assembly_items {
+ my ($self) = @_;
+ my $position = 0;
+ my @array;
+ my $assembly_items = delete($::form->{assembly_items}) || [];
+ foreach my $assembly_item ( @{$assembly_items} ) {
+ next unless $assembly_item->{parts_id};
+ $position++;
+ my $part = SL::DB::Manager::Part->find_by(id => $assembly_item->{parts_id}) || die "Can't determine item to be added";
+ my $ai = SL::DB::Assembly->new(parts_id => $part->id,
+ bom => $assembly_item->{bom},
+ qty => $::form->parse_amount(\%::myconfig, $assembly_item->{qty_as_number}),
+ position => $position,
+ );
+ push(@array, $ai);
+ };
+ return \@array;
+}
+
+sub init_all_warehouses {
+ my ($self) = @_;
+ SL::DB::Manager::Warehouse->get_all(query => [ or => [ invalid => 0, invalid => undef, id => $self->part->warehouse_id ] ]);
+}
+
+sub init_all_languages {
+ SL::DB::Manager::Language->get_all_sorted;
+}
+
+sub init_all_partsgroups {
+ my ($self) = @_;
+ SL::DB::Manager::PartsGroup->get_all_sorted(query => [ or => [ id => $self->part->partsgroup_id, obsolete => 0 ] ]);
+}
+
+sub init_all_buchungsgruppen {
+ my ($self) = @_;
+ if ( $self->part->orphaned ) {
+ return SL::DB::Manager::Buchungsgruppe->get_all_sorted;
+ } else {
+ return SL::DB::Manager::Buchungsgruppe->get_all_sorted(where => [ id => $self->part->buchungsgruppen_id ]);
+ }
+}
+
+sub init_shops_not_assigned {
+ my ($self) = @_;
+
+ my @used_shop_ids = map { $_->shop->id } @{ $self->part->shop_parts };
+ if ( @used_shop_ids ) {
+ return SL::DB::Manager::Shop->get_all( query => [ obsolete => 0, '!id' => \@used_shop_ids ], sort_by => 'sortkey' );
+ }
+ else {
+ return SL::DB::Manager::Shop->get_all( query => [ obsolete => 0 ], sort_by => 'sortkey' );
+ }
+}
+
+sub init_all_units {
+ my ($self) = @_;
+ if ( $self->part->orphaned ) {
+ return SL::DB::Manager::Unit->get_all_sorted;
+ } else {
+ return SL::DB::Manager::Unit->get_all(where => [ unit => $self->part->unit ]);
+ }
+}
+
+sub init_all_payment_terms {
+ my ($self) = @_;
+ SL::DB::Manager::PaymentTerm->get_all_sorted(query => [ or => [ id => $self->part->payment_id, obsolete => 0 ] ]);
+}
+
+sub init_all_price_factors {
+ SL::DB::Manager::PriceFactor->get_all_sorted;
+}
+
+sub init_all_pricegroups {
+ SL::DB::Manager::Pricegroup->get_all_sorted(query => [ obsolete => 0 ]);
+}
+
+# model used to filter/display the parts in the multi-items dialog
+sub init_multi_items_models {
+ SL::Controller::Helper::GetModels->new(
+ controller => $_[0],
+ model => 'Part',
+ with_objects => [ qw(unit_obj partsgroup classification) ],
+ disable_plugin => 'paginated',
+ source => $::form->{multi_items},
+ sorted => {
+ _default => {
+ by => 'partnumber',
+ dir => 1,
+ },
+ partnumber => t8('Partnumber'),
+ description => t8('Description')}
+ );
+}
+
+sub init_parts_classification_filter {
+ return [] unless $::form->{parts_classification_type};
+
+ return [ used_for_sale => 't' ] if $::form->{parts_classification_type} eq 'sales';
+ return [ used_for_purchase => 't' ] if $::form->{parts_classification_type} eq 'purchases';
+
+ die "no query rules for parts_classification_type " . $::form->{parts_classification_type};
+}
+
+# simple checks to run on $::form before saving
+
+sub form_check_part_description_exists {
+ my ($self) = @_;
+
+ return 1 if $::form->{part}{description};
+
+ $self->js->flash('error', t8('Part Description missing!'))
+ ->run('kivi.Part.set_tab_active_by_name', 'basic_data')
+ ->focus('#part_description');
+ return 0;
+}
+
+sub form_check_assortment_items_exist {
+ my ($self) = @_;
+
+ return 1 unless $::form->{part}{part_type} eq 'assortment';
+ # skip item check for existing assortments that have been used
+ return 1 if ($self->part->id and !$self->part->orphaned);
+
+ # new or orphaned parts must have items in $::form->{assortment_items}
+ unless ( $::form->{assortment_items} and scalar @{$::form->{assortment_items}} ) {
+ $self->js->run('kivi.Part.set_tab_active_by_name', 'assortment_tab')
+ ->focus('#add_assortment_item_name')
+ ->flash('error', t8('The assortment doesn\'t have any items.'));
+ return 0;
+ };
+ return 1;
+}
+
+sub form_check_assortment_items_unique {
+ my ($self) = @_;
+
+ return 1 unless $::form->{part}{part_type} eq 'assortment';
+
+ my %duplicate_elements;
+ my %count;
+ for (map { $_->{parts_id} } @{$::form->{assortment_items}}) {
+ $duplicate_elements{$_}++ if $count{$_}++;
+ };
+
+ if ( keys %duplicate_elements ) {
+ $self->js->run('kivi.Part.set_tab_active_by_name', 'assortment_tab')
+ ->flash('error', t8('There are duplicate assortment items'));
+ return 0;
+ };
+ return 1;
+}
+
+sub form_check_assembly_items_exist {
+ my ($self) = @_;
+
+ return 1 unless $::form->{part}->{part_type} eq 'assembly';
+
+ # skip item check for existing assembly that have been used
+ return 1 if ($self->part->id and !$self->part->orphaned);
+
+ unless ( $::form->{assembly_items} and scalar @{$::form->{assembly_items}} ) {
+ $self->js->run('kivi.Part.set_tab_active_by_name', 'assembly_tab')
+ ->focus('#add_assembly_item_name')
+ ->flash('error', t8('The assembly doesn\'t have any items.'));
+ return 0;
+ };
+ return 1;
+}
+
+sub form_check_partnumber_is_unique {
+ my ($self) = @_;
+
+ if ( !$::form->{part}{id} and $::form->{part}{partnumber} ) {
+ my $count = SL::DB::Manager::Part->get_all_count(where => [ partnumber => $::form->{part}{partnumber} ]);
+ if ( $count ) {
+ $self->js->flash('error', t8('The partnumber already exists!'))
+ ->focus('#part_description');
+ return 0;
+ };
+ };
+ return 1;
+}
+
+# general checking functions
+
+sub check_part_id {
+ die t8("Can't load item without a valid part.id") . "\n" unless $::form->{part}{id};
+}
+
+sub check_form {
+ my ($self) = @_;
+
+ $self->form_check_part_description_exists || return 0;
+ $self->form_check_assortment_items_exist || return 0;
+ $self->form_check_assortment_items_unique || return 0;
+ $self->form_check_assembly_items_exist || return 0;
+ $self->form_check_partnumber_is_unique || return 0;
+
+ return 1;
+}
+
+sub check_has_valid_part_type {
+ die "invalid part_type" unless $_[0] =~ /^(part|service|assembly|assortment)$/;
+}
+
+
+sub normalize_text_blocks {
+ my ($self) = @_;
+
+ # check if feature is enabled (select normalize_part_descriptions from defaults)
+ return unless ($::instance_conf->get_normalize_part_descriptions);
+
+ # text block
+ foreach (qw(description)) {
+ $self->part->{$_} =~ s/\s+$//s;
+ $self->part->{$_} =~ s/^\s+//s;
+ $self->part->{$_} =~ s/ {2,}/ /g;
+ }
+ # html block (caveat: can be circumvented by using bold or italics)
+ $self->part->{notes} =~ s/^<p>( )+\s+/<p>/s;
+ $self->part->{notes} =~ s/( )+<\/p>$/<\/p>/s;
+
+}
+
+sub render_assortment_items_to_html {
+ my ($self, $assortment_items, $number_of_items) = @_;
+
+ my $position = $number_of_items + 1;
+ my $html;
+ foreach my $ai (@$assortment_items) {
+ $html .= $self->p->render('part/_assortment_row',
+ PART => $self->part,
+ orphaned => $self->orphaned,
+ ITEM => $ai,
+ listrow => $position % 2 ? 1 : 0,
+ position => $position, # for legacy assemblies
+ );
+ $position++;
+ };
+ return $html;
+}
+
+sub render_assembly_items_to_html {
+ my ($self, $assembly_items, $number_of_items) = @_;
+
+ my $position = $number_of_items + 1;
+ my $html;
+ foreach my $ai (@{$assembly_items}) {
+ $html .= $self->p->render('part/_assembly_row',
+ PART => $self->part,
+ orphaned => $self->orphaned,
+ ITEM => $ai,
+ listrow => $position % 2 ? 1 : 0,
+ position => $position, # for legacy assemblies
+ );
+ $position++;
+ };
+ return $html;
+}
+
+sub parse_add_items_to_objects {
+ my ($self, %params) = @_;
+ my $part_type = $params{part_type};
+ die unless $params{part_type} =~ /^(assortment|assembly)$/;
+ my $position = $params{position} || 1;
+
+ my @add_items = grep { $_->{qty_as_number} } @{ $::form->{add_items} };
+
+ my @item_objects;
+ foreach my $item ( @add_items ) {
+ my $part = SL::DB::Manager::Part->find_by(id => $item->{parts_id}) || die "Can't load part";
+ my $ai;
+ if ( $part_type eq 'assortment' ) {
+ $ai = SL::DB::AssortmentItem->new(part => $part,
+ qty => $::form->parse_amount(\%::myconfig, $item->{qty_as_number}),
+ unit => $part->unit, # TODO: $item->{unit} || $part->unit
+ position => $position,
+ ) or die "Can't create AssortmentItem from item";
+ } elsif ( $part_type eq 'assembly' ) {
+ $ai = SL::DB::Assembly->new(parts_id => $part->id,
+ # id => $self->assembly->id, # will be set on save
+ qty => $::form->parse_amount(\%::myconfig, $item->{qty_as_number}),
+ bom => 0, # default when adding: no bom
+ position => $position,
+ );
+ } else {
+ die "part_type must be assortment or assembly";
+ }
+ push(@item_objects, $ai);
+ $position++;
+ };
+
+ return \@item_objects;
+}
+
+sub _setup_form_action_bar {
+ my ($self) = @_;
+
+ my $may_edit = $::auth->assert('part_service_assembly_edit', 'may fail');
+ my $used_in_pricerules = !!SL::DB::Manager::PriceRuleItem->get_all_count(where => [type => 'part', value_int => $self->part->id]);
+
+ for my $bar ($::request->layout->get('actionbar')) {
+ $bar->add(
+ combobox => [
+ action => [
+ t8('Save'),
+ call => [ 'kivi.Part.save' ],
+ disabled => !$may_edit ? t8('You do not have the permissions to access this function.') : undef,
+ checks => ['kivi.validate_form'],
+ ],
+ action => [
+ t8('Use as new'),
+ call => [ 'kivi.Part.use_as_new' ],
+ disabled => !$self->part->id ? t8('The object has not been saved yet.')
+ : !$may_edit ? t8('You do not have the permissions to access this function.')
+ : undef,
+ ],
+ ], # end of combobox "Save"
+
+ combobox => [
+ action => [ t8('Workflow') ],
+ action => [
+ t8('Save and Purchase Order'),
+ submit => [ '#ic', { action => "Part/save_and_purchase_order" } ],
+ checks => ['kivi.validate_form'],
+ disabled => !$self->part->id ? t8('The object has not been saved yet.')
+ : !$may_edit ? t8('You do not have the permissions to access this function.')
+ : !$::auth->assert('purchase_order_edit', 'may fail') ? t8('You do not have the permissions to access this function.')
+ : undef,
+ only_if => !$::form->{inline_create},
+ ],
+ ],
+
+ action => [
+ t8('Abort'),
+ submit => [ '#ic', { action => "Part/abort" } ],
+ only_if => !!$::form->{inline_create},
+ ],
+
+ action => [
+ t8('Delete'),
+ call => [ 'kivi.Part.delete' ],
+ confirm => t8('Do you really want to delete this object?'),
+ disabled => !$self->part->id ? t8('This object has not been saved yet.')
+ : !$may_edit ? t8('You do not have the permissions to access this function.')
+ : !$self->part->orphaned ? t8('This object has already been used.')
+ : $used_in_pricerules ? t8('This object is used in price rules.')
+ : undef,
+ ],
+
+ 'separator',
+
+ action => [
+ t8('History'),
+ call => [ 'kivi.Part.open_history_popup' ],
+ disabled => !$self->part->id ? t8('This object has not been saved yet.')
+ : !$may_edit ? t8('You do not have the permissions to access this function.')
+ : undef,
+ ],
+ );
+ }
+}