Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[kivitendo-erp.git] / bin / mozilla / ct.pl
index dbf8cdd..32fe324 100644 (file)
@@ -49,6 +49,7 @@ use POSIX qw(strftime);
 
 use SL::CT;
 use SL::CVar;
+use SL::Request qw(flatten);
 use SL::DB::Business;
 use SL::DB::Default;
 use SL::Helper::Flash;
@@ -110,6 +111,18 @@ sub search {
   $main::lxdebug->leave_sub();
 }
 
+sub search_contact {
+  $::lxdebug->enter_sub;
+  $::auth->assert('customer_vendor_edit');
+
+  $::form->{fokus}    = 'Form.name';
+
+  $::form->header;
+  print $::form->parse_html_template('ct/search_contact');
+
+  $::lxdebug->leave_sub;
+}
+
 sub list_names {
   $main::lxdebug->enter_sub();
 
@@ -268,6 +281,93 @@ sub list_names {
   $main::lxdebug->leave_sub();
 }
 
+sub list_contacts {
+  $::lxdebug->enter_sub;
+  $::auth->assert('customer_vendor_edit');
+
+  $::form->{sortdir} = 1 unless defined $::form->{sortdir};
+
+  my @contacts     = CT->search_contacts(
+    search_term => $::form->{search_term},
+    filter      => $::form->{filter},
+  );
+
+  my @columns      = qw(
+    cp_id vcname vcnumber cp_name cp_givenname cp_street cp_phone1 cp_phone2
+    cp_mobile1 cp_mobile2 cp_email cp_abteilung cp_birthday cp_gender
+  );
+
+  my @visible_columns;
+  if ($::form->{l}) {
+    @visible_columns = grep { $::form->{l}{$_} } @columns;
+    push @visible_columns, qw(cp_phone1 cp_phone2)   if $::form->{l}{cp_phone};
+    push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
+  } else {
+   @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
+  }
+
+  my %column_defs  = (
+    'cp_id'        => { 'text' => $::locale->text('ID'), },
+    'vcname'       => { 'text' => $::locale->text('Customer/Vendor'), },
+    'vcnumber'     => { 'text' => $::locale->text('Customer/Vendor Number'), },
+    'cp_name'      => { 'text' => $::locale->text('Name'), },
+    'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
+    'cp_street'    => { 'text' => $::locale->text('Street'), },
+    'cp_phone1'    => { 'text' => $::locale->text('Phone1'), },
+    'cp_phone2'    => { 'text' => $::locale->text('Phone2'), },
+    'cp_mobile1'   => { 'text' => $::locale->text('Mobile 1'), },
+    'cp_mobile2'   => { 'text' => $::locale->text('Mobile 2'), },
+    'cp_email'     => { 'text' => $::locale->text('E-mail'), },
+    'cp_abteilung' => { 'text' => $::locale->text('Department'), },
+    'cp_birthday'  => { 'text' => $::locale->text('Birthday'), },
+    'cp_gender'    => { 'text' => $::locale->text('Gender'), },
+  );
+
+  map { $column_defs{$_}->{visible} = 1 } @visible_columns;
+
+  my @hidden_variables  = (qw(search_term filter l));
+  my $hide_vars         = { map { $_ => $::form->{$_} } @hidden_variables };
+  my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
+  my $callback          = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
+  $::form->{callback}     = "$callback&sort=" . E($::form->{sort});
+
+  map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
+
+  $::form->{title} = $::locale->text('Contacts');
+
+  my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
+
+  my @options    = $::locale->text('Search term') . ': ' . $::form->{search_term};
+
+  $report->set_options('top_info_text'       => join("\n", @options),
+                       'output_format'       => 'HTML',
+                       'title'               => $::form->{title},
+                       'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
+    );
+  $report->set_options_from_form;
+
+  $report->set_columns(%column_defs);
+  $report->set_column_order(@columns);
+
+  $report->set_export_options('list_contacts', @hidden_variables);
+
+  $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
+
+  foreach my $ref (@contacts) {
+    my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
+
+    $row->{vcname}->{link}   = build_std_url('action=edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
+    $row->{vcnumber}->{link} = $row->{vcname}->{link};
+    $row->{cp_email}->{link} = 'mailto:' . E($ref->{cp_email});
+
+    $report->add_data($row);
+  }
+
+  $report->generate_with_headers;
+
+  $::lxdebug->leave_sub;
+}
+
 sub edit {
   $main::lxdebug->enter_sub();