Generische Unterstützung für CTI: Click-to-dial
[kivitendo-erp.git] / bin / mozilla / ct.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors: Reed White <alta@alta-research.com>
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
30 #
31 # customer/vendor module
32 #
33 #======================================================================
34
35 # $locale->text('Customers')
36 # $locale->text('Vendors')
37 # $locale->text('Add Customer')
38 # $locale->text('Add Vendor')
39 # $locale->text('Edit Customer')
40 # $locale->text('Edit Vendor')
41 # $locale->text('Customer saved!')
42 # $locale->text('Vendor saved!')
43 # $locale->text('Customer deleted!')
44 # $locale->text('Cannot delete customer!')
45 # $locale->text('Vendor deleted!')
46 # $locale->text('Cannot delete vendor!')
47
48 use POSIX qw(strftime);
49
50 use SL::CT;
51 use SL::CTI;
52 use SL::CVar;
53 use SL::Request qw(flatten);
54 use SL::DB::Business;
55 use SL::DB::Default;
56 use SL::DB::DeliveryTerm;
57 use SL::Helper::Flash;
58 use SL::ReportGenerator;
59 use SL::MoreCommon qw(uri_encode);
60
61 require "bin/mozilla/common.pl";
62 require "bin/mozilla/reportgenerator.pl";
63
64 use strict;
65 1;
66
67 # end of main
68
69 sub search {
70   $main::lxdebug->enter_sub();
71
72   $main::auth->assert('customer_vendor_edit');
73
74   my $form     = $main::form;
75   my $locale   = $main::locale;
76
77   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
78
79   $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
80   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
81
82   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'CT');
83   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
84    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
85                                                                            'include_prefix' => 'l_',
86                                                                            'include_value'  => 'Y');
87
88   $form->{title}    = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
89
90   $form->header();
91   print $form->parse_html_template('ct/search');
92
93   $main::lxdebug->leave_sub();
94 }
95
96 sub search_contact {
97   $::lxdebug->enter_sub;
98   $::auth->assert('customer_vendor_edit');
99
100   $::form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'Contacts');
101   ($::form->{CUSTOM_VARIABLES_FILTER_CODE},
102    $::form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'    => $::form->{CUSTOM_VARIABLES},
103                                                                            'include_prefix' => 'l.',
104                                                                            'filter_prefix'  => 'filter.',
105                                                                            'include_value'  => 'Y');
106
107   $::form->{title} = $::locale->text('Search contacts');
108   $::form->header;
109   print $::form->parse_html_template('ct/search_contact');
110
111   $::lxdebug->leave_sub;
112 }
113
114 sub list_names {
115   $main::lxdebug->enter_sub();
116
117   $main::auth->assert('customer_vendor_edit');
118
119   my $form     = $main::form;
120   my %myconfig = %main::myconfig;
121   my $locale   = $main::locale;
122
123   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
124
125   report_generator_set_default_sort('name', 1);
126
127   CT->search(\%myconfig, \%$form);
128
129   my $cvar_configs = CVar->get_configs('module' => 'CT');
130
131   my @options;
132   if ($form->{status} eq 'all') {
133     push @options, $locale->text('All');
134   } elsif ($form->{status} eq 'orphaned') {
135     push @options, $locale->text('Orphaned');
136   }
137
138   push @options, $locale->text('Name') . " : $form->{name}"                                    if $form->{name};
139   push @options, $locale->text('Contact') . " : $form->{contact}"                              if $form->{contact};
140   push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|                 if $form->{"$form->{db}number"};
141   push @options, $locale->text('E-mail') . " : $form->{email}"                                 if $form->{email};
142   push @options, $locale->text('Contact person (surname)')           . " : $form->{cp_name}"   if $form->{cp_name};
143   push @options, $locale->text('Billing/shipping address (city)')    . " : $form->{addr_city}" if $form->{addr_city};
144   push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{zipcode}"   if $form->{addr_zipcode};
145   push @options, $locale->text('Billing/shipping address (street)')  . " : $form->{street}"    if $form->{addr_street};
146   push @options, $locale->text('Billing/shipping address (country)') . " : $form->{country}"   if $form->{addr_country};
147
148   if ($form->{business_id}) {
149     my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
150     if ($business) {
151       my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
152       push @options, $label . " : " . $business->description;
153     }
154   }
155
156   my @columns = (
157     'id',        'name',    "$form->{db}number",   'contact',   'phone',    'discount',
158     'fax',       'email',   'taxnumber',           'street',    'zipcode' , 'city',
159     'business',  'payment', 'invnumber', 'ordnumber',           'quonumber', 'salesman',
160     'country'
161   );
162
163   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
164   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
165   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
166
167   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
168
169   my %column_defs = (
170     'id'                => { 'text' => $locale->text('ID'), },
171     "$form->{db}number" => { 'text' => $locale->text('Number'), },
172     'name'              => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
173     'contact'           => { 'text' => $locale->text('Contact'), },
174     'phone'             => { 'text' => $locale->text('Phone'), },
175     'fax'               => { 'text' => $locale->text('Fax'), },
176     'email'             => { 'text' => $locale->text('E-mail'), },
177     'cc'                => { 'text' => $locale->text('Cc'), },
178     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
179     'business'          => { 'text' => $locale->text('Type of Business'), },
180     'invnumber'         => { 'text' => $locale->text('Invoice'), },
181     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
182     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
183     'street'            => { 'text' => $locale->text('Street'), },
184     'zipcode'           => { 'text' => $locale->text('Zipcode'), },
185     'city'              => { 'text' => $locale->text('City'), },
186     'country'           => { 'text' => $locale->text('Country'), },
187     'salesman'          => { 'text' => $locale->text('Salesman'), },
188     'discount'          => { 'text' => $locale->text('Discount'), },
189     'payment'           => { 'text' => $locale->text('Payment Terms'), },
190     %column_defs_cvars,
191   );
192
193   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
194
195   my @hidden_variables  = ( qw(
196       db status obsolete name contact email cp_name addr_street addr_zipcode
197       addr_city addr_country business_id
198     ), "$form->{db}number",
199     map({ "cvar_$_->{name}" } @searchable_custom_variables),
200     map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
201     map({ "l_$_" } @columns),
202   );
203
204   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
205   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
206   $form->{callback}     = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
207
208   foreach (@columns) {
209     my $sortdir              = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
210     $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
211   }
212
213   my ($ordertype, $quotationtype, $attachment_basename);
214   if ($form->{IS_CUSTOMER}) {
215     $form->{title}       = $locale->text('Customers');
216     $ordertype           = 'sales_order';
217     $quotationtype       = 'sales_quotation';
218     $attachment_basename = $locale->text('customer_list');
219
220   } else {
221     $form->{title}       = $locale->text('Vendors');
222     $ordertype           = 'purchase_order';
223     $quotationtype       = 'request_quotation';
224     $attachment_basename = $locale->text('vendor_list');
225   }
226
227   my $report = SL::ReportGenerator->new(\%myconfig, $form);
228
229   $report->set_options('top_info_text'         => join("\n", @options),
230                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
231                        'output_format'         => 'HTML',
232                        'title'                 => $form->{title},
233                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
234     );
235   $report->set_options_from_form();
236   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
237
238   $report->set_columns(%column_defs);
239   $report->set_column_order(@columns);
240
241   $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
242
243   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
244
245   CVar->add_custom_variables_to_report('module'         => 'CT',
246                                        'trans_id_field' => 'id',
247                                        'configs'        => $cvar_configs,
248                                        'column_defs'    => \%column_defs,
249                                        'data'           => $form->{CT});
250
251   my $previous_id;
252
253   foreach my $ref (@{ $form->{CT} }) {
254     my $row = { map { $_ => { 'data' => '' } } @columns };
255
256     if ($ref->{id} ne $previous_id) {
257       $previous_id = $ref->{id};
258       $ref->{discount} = $form->format_amount(\%myconfig, $ref->{discount} * 100.0, 2);
259       map { $row->{$_}->{data} = $ref->{$_} } @columns;
260
261       $row->{name}->{link}  = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
262       $row->{email}->{link} = 'mailto:' . E($ref->{email});
263     }
264
265     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
266     $row->{invnumber}->{link} = $base_url;
267     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
268     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
269     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
270     $row->{$column}->{data}   = $ref->{$column};
271
272     if (my $number = SL::CTI->sanitize_number(number => $ref->{phone})) {
273       $row->{phone}->{link}       = SL::CTI->call_link(number => $number);
274       $row->{phone}->{link_class} = 'cti_call_action';
275     }
276
277     $report->add_data($row);
278   }
279
280   $report->generate_with_headers();
281
282   $main::lxdebug->leave_sub();
283 }
284
285 sub list_contacts {
286   $::lxdebug->enter_sub;
287   $::auth->assert('customer_vendor_edit');
288
289   $::form->{sortdir} = 1 unless defined $::form->{sortdir};
290
291   my @contacts     = CT->search_contacts(
292     search_term => $::form->{search_term},
293     filter      => $::form->{filter},
294   );
295
296   my $cvar_configs = CVar->get_configs('module' => 'Contacts');
297
298   my @columns      = qw(
299     cp_id vcname vcnumber cp_name cp_givenname cp_street cp_zipcode cp_city cp_phone1 cp_phone2 cp_privatphone
300     cp_mobile1 cp_mobile2 cp_fax cp_email cp_privatemail cp_abteilung cp_position cp_birthday cp_gender
301   );
302
303   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
304   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
305   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
306
307   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
308
309   my @visible_columns;
310   if ($::form->{l}) {
311     @visible_columns = grep { $::form->{l}{$_} } @columns;
312     push @visible_columns, qw(cp_phone1 cp_phone2)   if $::form->{l}{cp_phone};
313     push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
314   } else {
315    @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
316   }
317
318   my %column_defs  = (
319     'cp_id'        => { 'text' => $::locale->text('ID'), },
320     'vcname'       => { 'text' => $::locale->text('Customer/Vendor'), },
321     'vcnumber'     => { 'text' => $::locale->text('Customer/Vendor Number'), },
322     'cp_name'      => { 'text' => $::locale->text('Name'), },
323     'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
324     'cp_street'    => { 'text' => $::locale->text('Street'), },
325     'cp_zipcode'   => { 'text' => $::locale->text('Zipcode'), },
326     'cp_city'      => { 'text' => $::locale->text('City'), },
327     'cp_phone1'    => { 'text' => $::locale->text('Phone1'), },
328     'cp_phone2'    => { 'text' => $::locale->text('Phone2'), },
329     'cp_mobile1'   => { 'text' => $::locale->text('Mobile1'), },
330     'cp_mobile2'   => { 'text' => $::locale->text('Mobile2'), },
331     'cp_email'     => { 'text' => $::locale->text('E-mail'), },
332     'cp_abteilung' => { 'text' => $::locale->text('Department'), },
333     'cp_position'  => { 'text' => $::locale->text('Function/position'), },
334     'cp_birthday'  => { 'text' => $::locale->text('Birthday'), },
335     'cp_gender'    => { 'text' => $::locale->text('Gender'), },
336     'cp_fax'       => { 'text' => $::locale->text('Fax'), },
337     'cp_privatphone' => { 'text' => $::locale->text('Private Phone') },
338     'cp_privatemail' => { 'text' => $::locale->text('Private E-mail') },
339     %column_defs_cvars,
340   );
341
342   map { $column_defs{$_}->{visible} = 1 } @visible_columns;
343
344   my @hidden_variables  = (qw(search_term filter l));
345   my $hide_vars         = { map { $_ => $::form->{$_} } @hidden_variables };
346   my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
347   my $callback          = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
348   $::form->{callback}     = "$callback&sort=" . E($::form->{sort});
349
350   map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
351
352   $::form->{title} = $::locale->text('Contacts');
353
354   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
355
356   my @options;
357   push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
358   for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
359     push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
360   }
361   if ($::form->{filter}{status}) {
362     push @options, $::locale->text('Status') . ': ' . (
363       $::form->{filter}{status} =~ /active/   ? $::locale->text('Active')   :
364       $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
365       $::form->{filter}{status} =~ /all/      ? $::locale->text('All')      : ''
366     );
367   }
368
369
370   $report->set_options('top_info_text'       => join("\n", @options),
371                        'output_format'       => 'HTML',
372                        'title'               => $::form->{title},
373                        'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
374     );
375   $report->set_options_from_form;
376
377   $report->set_columns(%column_defs);
378   $report->set_column_order(@columns);
379
380   $report->set_export_options('list_contacts', @hidden_variables);
381
382   $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
383
384   CVar->add_custom_variables_to_report('module'         => 'Contacts',
385                                        'trans_id_field' => 'cp_id',
386                                        'configs'        => $cvar_configs,
387                                        'column_defs'    => \%column_defs,
388                                        'data'           => \@contacts);
389
390
391   foreach my $ref (@contacts) {
392     my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
393
394     $row->{vcname}->{link}   = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
395     $row->{vcnumber}->{link} = $row->{vcname}->{link};
396
397     for (qw(cp_email cp_privatemail)) {
398       $row->{$_}->{link} = 'mailto:' . E($ref->{$_}) if $ref->{$_};
399     }
400
401     for (qw(cp_phone1 cp_phone2 cp_mobile1)) {
402       next unless my $number = SL::CTI->sanitize_number(number => $ref->{$_});
403
404       $row->{$_}->{link}       = SL::CTI->call_link(number => $number);
405       $row->{$_}->{link_class} = 'cti_call_action';
406     }
407
408     $report->add_data($row);
409   }
410
411   $report->generate_with_headers;
412
413   $::lxdebug->leave_sub;
414 }
415
416 sub continue { call_sub($main::form->{nextsub}); }