1 package SL::Controller::RecordLinks;
5 use parent qw(SL::Controller::Base);
7 use List::Util qw(first);
9 use SL::DB::Helper::Mappings;
11 use SL::DB::DeliveryOrder;
13 use SL::DB::PurchaseInvoice;
14 use SL::DB::RecordLink;
16 use SL::Locale::String;
18 use Rose::Object::MakeMethods::Generic
20 scalar => [ qw(object object_model object_id link_type link_direction link_type_desc) ],
23 __PACKAGE__->run_before('check_object_params', only => [ qw(ajax_list ajax_delete ajax_add_select_type ajax_add_filter ajax_add_list ajax_add_do) ]);
24 __PACKAGE__->run_before('check_link_params', only => [ qw( ajax_add_list ajax_add_do) ]);
27 { title => t8('Sales quotation'), type => 'sales_quotation', model => 'Order', number => 'quonumber', },
28 { title => t8('Sales Order'), type => 'sales_order', model => 'Order', number => 'ordnumber', },
29 { title => t8('Sales delivery order'), type => 'sales_delivery_order', model => 'DeliveryOrder', number => 'donumber', },
30 { title => t8('Sales Invoice'), type => 'invoice', model => 'Invoice', number => 'invnumber', },
31 { title => t8('Request for Quotation'), type => 'request_quotation', model => 'Order', number => 'quonumber', },
32 { title => t8('Purchase Order'), type => 'purchase_order', model => 'Order', number => 'ordnumber', },
33 { title => t8('Purchase delivery order'), type => 'purchase_delivery_order', model => 'DeliveryOrder', number => 'donumber', },
34 { title => t8('Purchase Invoice'), type => 'purchase_invoice', model => 'PurchaseInvoice', number => 'invnumber', },
42 sub action_ajax_list {
46 my $linked_records = $self->object->linked_records(direction => 'both');
47 push @{ $linked_records }, $self->object->sepa_export_items if $self->object->can('sepa_export_items');
48 my $output = SL::Presenter->get->grouped_record_list(
50 with_columns => [ qw(record_link_direction) ],
51 edit_record_links => 1,
52 object_model => $self->object_model,
53 object_id => $self->object_id,
55 $self->render(\$output, { layout => 0, process => 0 });
59 $self->render('generic/error', { layout => 0 }, label_error => $@);
63 sub action_ajax_delete {
66 foreach my $str (@{ $::form->{record_links_delete} || [] }) {
67 my ($from_table, $from_id, $to_table, $to_id) = split m/__/, $str, 4;
71 next if !$from_table || !$from_id || !$to_table || !$to_id;
73 SL::DB::Manager::RecordLink->delete_all(where => [
74 from_table => $from_table,
76 to_table => $to_table,
81 $self->action_ajax_list;
84 sub action_ajax_add_filter {
87 my $presenter = $self->presenter;
89 my @link_type_select = map { [ $_->{type}, $_->{title} ] } @link_types;
90 my @projects = map { [ $_->id, $presenter->project($_, display => 'inline', style => 'both', no_link => 1) ] } @{ SL::DB::Manager::Project->get_all_sorted };
91 my $is_sales = $self->object->can('customer_id') && $self->object->customer_id;
94 'record_links/add_filter',
96 is_sales => $is_sales,
97 DEFAULT_LINK_TYPE => $is_sales ? 'sales_quotation' : 'request_quotation',
98 LINK_TYPES => \@link_type_select,
99 PROJECTS => \@projects,
103 sub action_ajax_add_list {
106 my $manager = 'SL::DB::Manager::' . $self->link_type_desc->{model};
107 my $vc = $self->link_type =~ m/sales_|^invoice$/ ? 'customer' : 'vendor';
109 my @where = $manager->type_filter($self->link_type);
110 push @where, ("${vc}.${vc}number" => { ilike => '%' . $::form->{vc_number} . '%' }) if $::form->{vc_number};
111 push @where, ("${vc}.name" => { ilike => '%' . $::form->{vc_name} . '%' }) if $::form->{vc_name};
112 push @where, (transaction_description => { ilike => '%' . $::form->{transaction_description} . '%' }) if $::form->{transaction_description};
113 push @where, (globalproject_id => $::form->{globalproject_id}) if $::form->{globalproject_id};
115 my $objects = $manager->get_all_sorted(where => \@where, with_objects => [ $vc, 'globalproject' ]);
116 my $output = $self->render(
117 'record_links/add_list',
121 number_column => $self->link_type_desc->{number},
124 my %result = ( count => scalar(@{ $objects }), html => $output );
126 $self->render(\to_json(\%result), { type => 'json', process => 0 });
129 sub action_ajax_add_do {
130 my ($self, %params) = @_;
132 my $object_side = $self->link_direction eq 'from' ? 'from' : 'to';
133 my $link_side = $object_side eq 'from' ? 'to' : 'from';
134 my $link_table = SL::DB::Helper::Mappings::get_table_for_package($self->link_type_desc->{model});
136 foreach my $link_id (@{ $::form->{link_id} || [] }) {
137 # Check for existing reverse connections in order to avoid loops.
139 "${link_side}_table" => $self->object->meta->table,
140 "${link_side}_id" => $self->object_id,
141 "${object_side}_table" => $link_table,
142 "${object_side}_id" => $link_id,
145 my $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
148 # Check for existing connections in order to avoid duplicates.
150 "${object_side}_table" => $self->object->meta->table,
151 "${object_side}_id" => $self->object_id,
152 "${link_side}_table" => $link_table,
153 "${link_side}_id" => $link_id,
156 $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
158 SL::DB::RecordLink->new(@props)->save if !$existing;
161 $self->action_ajax_list;
169 sub check_object_params {
172 my %models = map { ($_->{model} => 1 ) } @link_types;
174 $self->object_id( $::form->{object_id});
175 $self->object_model($::form->{object_model});
177 die "Invalid object_model or object_id" if !$self->object_id || !$models{$self->object_model};
179 my $model = 'SL::DB::' . $self->object_model;
180 $self->object($model->new(id => $self->object_id)->load || die "Record not found");
185 sub check_link_params {
188 $self->link_type( $::form->{link_type});
189 $self->link_type_desc((first { $_->{type} eq $::form->{link_type} } @link_types) || die "Invalid link_type");
190 $self->link_direction($::form->{link_direction} =~ m/^(?:from|to)$/ ? $::form->{link_direction} : die "Invalid link_direction");