+sub action_ajax_add_filter {
+ my ($self) = @_;
+
+ my $presenter = $self->presenter;
+
+ my @link_type_select = map { [ $_->{type}, $_->{title} ] } @link_types;
+ my @projects = map { [ $_->id, $presenter->project($_, display => 'inline', style => 'both', no_link => 1) ] } @{ SL::DB::Manager::Project->get_all_sorted };
+ my $is_sales = $self->object->can('customer_id') && $self->object->customer_id;
+
+ $self->render(
+ 'record_links/add_filter',
+ { layout => 0 },
+ is_sales => $is_sales,
+ DEFAULT_LINK_TYPE => $is_sales ? 'sales_quotation' : 'request_quotation',
+ LINK_TYPES => \@link_type_select,
+ PROJECTS => \@projects,
+ );
+}
+
+sub action_ajax_add_list {
+ my ($self) = @_;
+
+ my $manager = 'SL::DB::Manager::' . $self->link_type_desc->{model};
+ my $vc = $self->link_type =~ m/sales_|^invoice$/ ? 'customer' : 'vendor';
+
+ my @where = $manager->type_filter($self->link_type);
+ push @where, ("${vc}.${vc}number" => { ilike => '%' . $::form->{vc_number} . '%' }) if $::form->{vc_number};
+ push @where, ("${vc}.name" => { ilike => '%' . $::form->{vc_name} . '%' }) if $::form->{vc_name};
+ push @where, (transaction_description => { ilike => '%' . $::form->{transaction_description} . '%' }) if $::form->{transaction_description};
+ push @where, (globalproject_id => $::form->{globalproject_id}) if $::form->{globalproject_id};
+
+ my $objects = $manager->get_all_sorted(where => \@where, with_objects => [ $vc, 'globalproject' ]);
+ my $output = $self->render(
+ 'record_links/add_list',
+ { output => 0 },
+ OBJECTS => $objects,
+ vc => $vc,
+ number_column => $self->link_type_desc->{number},
+ );
+
+ my %result = ( count => scalar(@{ $objects }), html => $output );
+
+ $self->render(\to_json(\%result), { type => 'json', process => 0 });
+}
+
+sub action_ajax_add_do {
+ my ($self, %params) = @_;
+
+ my $object_side = $self->link_direction eq 'from' ? 'from' : 'to';
+ my $link_side = $object_side eq 'from' ? 'to' : 'from';
+ my $link_table = SL::DB::Helper::Mappings::get_table_for_package($self->link_type_desc->{model});
+
+ foreach my $link_id (@{ $::form->{link_id} || [] }) {
+ # Check for existing reverse connections in order to avoid loops.
+ my @props = (
+ "${link_side}_table" => $self->object->meta->table,
+ "${link_side}_id" => $self->object_id,
+ "${object_side}_table" => $link_table,
+ "${object_side}_id" => $link_id,
+ );
+
+ my $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
+ next if $existing;
+
+ # Check for existing connections in order to avoid duplicates.
+ @props = (
+ "${object_side}_table" => $self->object->meta->table,
+ "${object_side}_id" => $self->object_id,
+ "${link_side}_table" => $link_table,
+ "${link_side}_id" => $link_id,
+ );
+
+ $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
+
+ SL::DB::RecordLink->new(@props)->save if !$existing;
+ }
+
+ $self->action_ajax_list;
+}
+
+