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 my $output = SL::Presenter->get->grouped_record_list(
49 with_columns => [ qw(record_link_direction) ],
50 edit_record_links => 1,
51 object_model => $self->object_model,
52 object_id => $self->object_id,
54 $self->render(\$output, { layout => 0, process => 0 });
58 $self->render('generic/error', { layout => 0 }, label_error => $@);
62 sub action_ajax_delete {
65 foreach my $str (@{ $::form->{record_links_delete} || [] }) {
66 my ($from_table, $from_id, $to_table, $to_id) = split m/__/, $str, 4;
70 next if !$from_table || !$from_id || !$to_table || !$to_id;
72 SL::DB::Manager::RecordLink->delete_all(where => [
73 from_table => $from_table,
75 to_table => $to_table,
80 $self->action_ajax_list;
83 sub action_ajax_add_filter {
86 my $presenter = $self->presenter;
88 my @link_type_select = map { [ $_->{type}, $_->{title} ] } @link_types;
89 my @projects = map { [ $_->id, $presenter->project($_, display => 'inline', style => 'both', no_link => 1) ] } @{ SL::DB::Manager::Project->get_all_sorted };
90 my $is_sales = $self->object->can('customer_id') && $self->object->customer_id;
93 'record_links/add_filter',
95 is_sales => $is_sales,
96 DEFAULT_LINK_TYPE => $is_sales ? 'sales_quotation' : 'request_quotation',
97 LINK_TYPES => \@link_type_select,
98 PROJECTS => \@projects,
102 sub action_ajax_add_list {
105 my $manager = 'SL::DB::Manager::' . $self->link_type_desc->{model};
106 my $vc = $self->link_type =~ m/sales_|^invoice$/ ? 'customer' : 'vendor';
108 my @where = $manager->type_filter($self->link_type);
109 push @where, ("${vc}.${vc}number" => { ilike => '%' . $::form->{vc_number} . '%' }) if $::form->{vc_number};
110 push @where, ("${vc}.name" => { ilike => '%' . $::form->{vc_name} . '%' }) if $::form->{vc_name};
111 push @where, (transaction_description => { ilike => '%' . $::form->{transaction_description} . '%' }) if $::form->{transaction_description};
112 push @where, (globalproject_id => $::form->{globalproject_id}) if $::form->{globalproject_id};
114 my $objects = $manager->get_all_sorted(where => \@where, with_objects => [ $vc, 'globalproject' ]);
115 my $output = $self->render(
116 'record_links/add_list',
120 number_column => $self->link_type_desc->{number},
123 my %result = ( count => scalar(@{ $objects }), html => $output );
125 $self->render(\to_json(\%result), { type => 'json', process => 0 });
128 sub action_ajax_add_do {
129 my ($self, %params) = @_;
131 my $object_side = $self->link_direction eq 'from' ? 'from' : 'to';
132 my $link_side = $object_side eq 'from' ? 'to' : 'from';
133 my $link_table = SL::DB::Helper::Mappings::get_table_for_package($self->link_type_desc->{model});
135 foreach my $link_id (@{ $::form->{link_id} || [] }) {
136 # Check for existing reverse connections in order to avoid loops.
138 "${link_side}_table" => $self->object->meta->table,
139 "${link_side}_id" => $self->object_id,
140 "${object_side}_table" => $link_table,
141 "${object_side}_id" => $link_id,
144 my $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
147 # Check for existing connections in order to avoid duplicates.
149 "${object_side}_table" => $self->object->meta->table,
150 "${object_side}_id" => $self->object_id,
151 "${link_side}_table" => $link_table,
152 "${link_side}_id" => $link_id,
155 $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
157 SL::DB::RecordLink->new(@props)->save if !$existing;
160 $self->action_ajax_list;
168 sub check_object_params {
171 my %models = map { ($_->{model} => 1 ) } @link_types;
173 $self->object_id( $::form->{object_id});
174 $self->object_model($::form->{object_model});
176 die "Invalid object_model or object_id" if !$self->object_id || !$models{$self->object_model};
178 my $model = 'SL::DB::' . $self->object_model;
179 $self->object($model->new(id => $self->object_id)->load || die "Record not found");
184 sub check_link_params {
187 $self->link_type( $::form->{link_type});
188 $self->link_type_desc((first { $_->{type} eq $::form->{link_type} } @link_types) || die "Invalid link_type");
189 $self->link_direction($::form->{link_direction} =~ m/^(?:from|to)$/ ? $::form->{link_direction} : die "Invalid link_direction");