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;
14 use SL::DB::PurchaseInvoice;
15 use SL::DB::RecordLink;
16 use SL::DB::RequirementSpec;
17 use SL::DBUtils qw(like);
18 use SL::DB::ShopOrder;
20 use SL::Locale::String;
21 use SL::Presenter::Record qw(grouped_record_list);
23 use Rose::Object::MakeMethods::Generic
25 scalar => [ qw(object object_model object_id link_type link_direction link_type_desc) ],
28 __PACKAGE__->run_before('check_auth');
29 __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) ]);
30 __PACKAGE__->run_before('check_link_params', only => [ qw( ajax_add_list ajax_add_do) ]);
32 my %link_type_defaults = (
33 filter => 'type_filter',
34 project => 'globalproject',
35 description => 'transaction_description',
36 description_title => t8('Transaction description'),
40 my @link_type_specifics = (
41 { title => t8('Requirement spec'), type => 'requirement_spec', model => 'RequirementSpec', number => 'id', project => 'project', description => 'title', date => undef, filter => 'working_copy_filter', },
42 { title => t8('Shop Order'), type => 'shop_order', model => 'ShopOrder', number => 'shop_ordernumber', date => 'order_date', project => undef, },
43 { title => t8('Sales quotation'), type => 'sales_quotation', model => 'Order', number => 'quonumber', },
44 { title => t8('Sales Order'), type => 'sales_order', model => 'Order', number => 'ordnumber', },
45 { title => t8('Sales delivery order'), type => 'sales_delivery_order', model => 'DeliveryOrder', number => 'donumber', },
46 { title => t8('Sales Invoice'), type => 'invoice', model => 'Invoice', number => 'invnumber', },
47 { title => t8('Request for Quotation'), type => 'request_quotation', model => 'Order', number => 'quonumber', },
48 { title => t8('Purchase Order'), type => 'purchase_order', model => 'Order', number => 'ordnumber', },
49 { title => t8('Purchase delivery order'), type => 'purchase_delivery_order', model => 'DeliveryOrder', number => 'donumber', },
50 { title => t8('Purchase Invoice'), type => 'purchase_invoice', model => 'PurchaseInvoice', number => 'invnumber', },
51 { title => t8('Letter'), type => 'letter', model => 'Letter', number => 'letternumber', description => 'subject', description_title => t8('Subject'), date => 'date', project => undef },
52 { title => t8('Email'), type => 'email_journal', model => 'EmailJournal', number => 'id', description => 'subject', description_title => t8('Subject'), },
53 { title => t8('AR Transaction'), type => 'ar_transaction', model => 'Invoice', number => 'invnumber', },
54 { title => t8('AP Transaction'), type => 'ap_transaction', model => 'PurchaseInvoice', number => 'invnumber', },
57 my @link_types = map { +{ %link_type_defaults, %{ $_ } } } @link_type_specifics;
63 sub action_ajax_list {
67 my $linked_records = $self->object->linked_records(direction => 'both', recursive => 1, save_path => 1);
68 push @{ $linked_records }, $self->object->sepa_export_items if $self->object->can('sepa_export_items');
70 my $output = grouped_record_list(
72 with_columns => [ qw(record_link_direction) ],
73 edit_record_links => 1,
74 object_model => $self->object_model,
75 object_id => $self->object_id,
77 $self->render(\$output, { layout => 0, process => 0 });
81 $self->render('generic/error', { layout => 0 }, label_error => $@);
85 sub action_ajax_delete {
88 foreach my $str (@{ $::form->{record_links_delete} || [] }) {
89 my ($from_table, $from_id, $to_table, $to_id) = split m/__/, $str, 4;
93 next if !$from_table || !$from_id || !$to_table || !$to_id;
95 SL::DB::Manager::RecordLink->delete_all(where => [
96 from_table => $from_table,
98 to_table => $to_table,
103 $self->action_ajax_list;
106 sub action_ajax_add_filter {
109 my $presenter = $self->presenter;
111 my @link_type_select = map { [ $_->{type}, $_->{title} ] } @link_types;
112 my @projects = map { [ $_->id, $_->presenter->project(display => 'inline', style => 'both', no_link => 1) ] } @{ SL::DB::Manager::Project->get_all_sorted };
113 my $is_sales = $self->object->can('customer_id') && $self->object->customer_id;
116 'record_links/add_filter',
118 is_sales => $is_sales,
119 DEFAULT_LINK_TYPE => $is_sales ? 'sales_quotation' : 'request_quotation',
120 LINK_TYPES => \@link_type_select,
121 PROJECTS => \@projects,
125 sub action_ajax_add_list {
128 my $manager = 'SL::DB::Manager::' . $self->link_type_desc->{model};
129 my $vc = $self->link_type =~ m/shop|sales_|^invoice|requirement_spec|letter|^ar_/ ? 'customer' : 'vendor';
130 my $project = $self->link_type_desc->{project};
131 my $project_id = "${project}_id";
132 my $description = $self->link_type_desc->{description};
133 my $filter = $self->link_type_desc->{filter};
135 my @where = $filter && $manager->can($filter) ? $manager->$filter($self->link_type) : ();
136 push @where, ("${vc}.${vc}number" => { ilike => like($::form->{vc_number}) }) if $::form->{vc_number};
137 push @where, ("${vc}.name" => { ilike => like($::form->{vc_name}) }) if $::form->{vc_name};
138 push @where, ($description => { ilike => like($::form->{transaction_description}) }) if $::form->{transaction_description};
139 push @where, ($project_id => $::form->{globalproject_id}) if $::form->{globalproject_id} && $manager->can($project_id);
141 my @with_objects = ($vc);
142 push @with_objects, $project if $manager->can($project_id);
144 # show the newest records first (should be better for 80% of the cases TODO sortable click
145 my $objects = $manager->get_all_sorted(where => \@where, with_objects => \@with_objects, sort_by => 'itime', sort_dir => 'ASC');
146 my $output = $self->render(
147 'record_links/add_list',
151 number_column => $self->link_type_desc->{number},
152 description_column => $description,
153 description_title => $self->link_type_desc->{description_title},
154 project_column => $project,
155 date_column => $self->link_type_desc->{date},
158 my %result = ( count => scalar(@{ $objects }), html => $output );
160 $self->render(\to_json(\%result), { type => 'json', process => 0 });
163 sub action_ajax_add_do {
164 my ($self, %params) = @_;
166 my $object_side = $self->link_direction eq 'from' ? 'from' : 'to';
167 my $link_side = $object_side eq 'from' ? 'to' : 'from';
168 my $link_table = SL::DB::Helper::Mappings::get_table_for_package($self->link_type_desc->{model});
170 foreach my $link_id (@{ $::form->{link_id} || [] }) {
171 # Check for existing reverse connections in order to avoid loops.
173 "${link_side}_table" => $self->object->meta->table,
174 "${link_side}_id" => $self->object_id,
175 "${object_side}_table" => $link_table,
176 "${object_side}_id" => $link_id,
179 my $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
182 # Check for existing connections in order to avoid duplicates.
184 "${object_side}_table" => $self->object->meta->table,
185 "${object_side}_id" => $self->object_id,
186 "${link_side}_table" => $link_table,
187 "${link_side}_id" => $link_id,
190 $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
192 SL::DB::RecordLink->new(@props)->save if !$existing;
195 $self->action_ajax_list;
203 sub check_object_params {
206 my %models = map { ($_->{model} => 1 ) } @link_types;
208 $self->object_id( $::form->{object_id});
209 $self->object_model($::form->{object_model});
211 die "Invalid object_model or object_id" if !$self->object_id || !$models{$self->object_model};
213 my $model = 'SL::DB::' . $self->object_model;
214 $self->object($model->new(id => $self->object_id)->load || die "Record not found");
219 sub check_link_params {
222 $self->link_type( $::form->{link_type});
223 $self->link_type_desc((first { $_->{type} eq $::form->{link_type} } @link_types) || die "Invalid link_type");
224 $self->link_direction($::form->{link_direction} =~ m/^(?:from|to)$/ ? $::form->{link_direction} : die "Invalid link_direction");
230 $::auth->assert('record_links');