11c81ee0daea1eb97936c1b79ce81c61624cbce0
[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., 51 Franklin Street, Fifth Floor, Boston,
29 # MA 02110-1335, USA.
30 #======================================================================
31 #
32 # customer/vendor module
33 #
34 #======================================================================
35
36 # $locale->text('Customers')
37 # $locale->text('Vendors')
38 # $locale->text('Add Customer')
39 # $locale->text('Add Vendor')
40 # $locale->text('Edit Customer')
41 # $locale->text('Edit Vendor')
42 # $locale->text('Customer saved!')
43 # $locale->text('Vendor saved!')
44 # $locale->text('Customer deleted!')
45 # $locale->text('Cannot delete customer!')
46 # $locale->text('Vendor deleted!')
47 # $locale->text('Cannot delete vendor!')
48
49 use POSIX qw(strftime);
50
51 use SL::CT;
52 use SL::CTI;
53 use SL::CVar;
54 use SL::Request qw(flatten);
55 use SL::DB::Business;
56 use SL::DB::Default;
57 use SL::DB::DeliveryTerm;
58 use SL::ReportGenerator;
59 use SL::Locale::String qw(t8);
60 use SL::MoreCommon qw(uri_encode);
61
62 require "bin/mozilla/common.pl";
63 require "bin/mozilla/reportgenerator.pl";
64
65 use strict;
66 1;
67
68 # end of main
69
70 sub search {
71   $main::lxdebug->enter_sub();
72
73   $main::auth->assert('customer_vendor_edit');
74
75   my $form     = $main::form;
76   my $locale   = $main::locale;
77
78   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
79
80   $form->get_lists("business_types" => "ALL_BUSINESS_TYPES",
81                    "salesmen"       => "ALL_SALESMEN");
82   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
83
84   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'CT');
85   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
86    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
87                                                                            'include_prefix' => 'l_',
88                                                                            'include_value'  => 'Y');
89
90   $form->{title}    = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
91
92   setup_ct_search_action_bar();
93
94   $form->header();
95   print $form->parse_html_template('ct/search');
96
97   $main::lxdebug->leave_sub();
98 }
99
100 sub search_contact {
101   $::lxdebug->enter_sub;
102   $::auth->assert('customer_vendor_edit');
103
104   $::form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'Contacts');
105   ($::form->{CUSTOM_VARIABLES_FILTER_CODE},
106    $::form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'    => $::form->{CUSTOM_VARIABLES},
107                                                                            'include_prefix' => 'l.',
108                                                                            'filter_prefix'  => 'filter.',
109                                                                            'include_value'  => 'Y');
110
111   $::form->{title} = $::locale->text('Search contacts');
112
113   setup_ct_search_contact_action_bar();
114   $::form->header;
115   print $::form->parse_html_template('ct/search_contact');
116
117   $::lxdebug->leave_sub;
118 }
119
120 sub list_names {
121   $main::lxdebug->enter_sub();
122
123   $main::auth->assert('customer_vendor_edit');
124
125   my $form     = $main::form;
126   my %myconfig = %main::myconfig;
127   my $locale   = $main::locale;
128
129   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
130
131   report_generator_set_default_sort('name', 1);
132
133   CT->search(\%myconfig, \%$form);
134
135   my $cvar_configs = CVar->get_configs('module' => 'CT');
136
137   my @options;
138   if ($form->{status} eq 'all') {
139     push @options, $locale->text('All');
140   } elsif ($form->{status} eq 'orphaned') {
141     push @options, $locale->text('Orphaned');
142   }
143
144   push @options, $locale->text('Name') . " : $form->{name}"                                       if $form->{name};
145   push @options, $locale->text('Contact') . " : $form->{contact}"                                 if $form->{contact};
146   push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|                    if $form->{"$form->{db}number"};
147   push @options, $locale->text('E-mail') . " : $form->{email}"                                    if $form->{email};
148   push @options, $locale->text('Contact person (surname)')           . " : $form->{cp_name}"      if $form->{cp_name};
149   push @options, $locale->text('Billing/shipping address (city)')    . " : $form->{addr_city}"    if $form->{addr_city};
150   push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{addr_zipcode}" if $form->{addr_zipcode};
151   push @options, $locale->text('Billing/shipping address (street)')  . " : $form->{addr_street}"  if $form->{addr_street};
152   push @options, $locale->text('Billing/shipping address (country)') . " : $form->{addr_country}" if $form->{addr_country};
153   push @options, $locale->text('Billing/shipping address (GLN)')     . " : $form->{addr_gln}"     if $form->{addr_gln};
154   push @options, $locale->text('Quick Search')                       . " : $form->{all}"          if $form->{all};
155
156   if ($form->{business_id}) {
157     my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
158     if ($business) {
159       my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
160       push @options, $label . " : " . $business->description;
161     }
162   }
163   if ($form->{salesman_id}) {
164     my $salesman = SL::DB::Manager::Employee->find_by(id => $form->{salesman_id});
165     if ($salesman) {
166       push @options, $locale->text('Salesman') . " : " . $salesman->name;
167     }
168   }
169
170   if ( $form->{insertdatefrom} or $form->{insertdateto} ) {
171     push @options, $locale->text('Insert Date');
172     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{insertdatefrom}, 1) if $form->{insertdatefrom};
173     push @options, $locale->text('Bis')  . " " . $locale->date(\%myconfig, $form->{insertdateto},   1) if $form->{insertdateto};
174   };
175
176   my @columns = (
177     'id',        'name',    "$form->{db}number",   'contact', 'main_contact_person',
178     'phone',    'discount',
179     'fax',       'email',   'taxnumber',           'street',    'zipcode' , 'city',
180     'business',  'payment', 'invnumber', 'ordnumber',           'quonumber', 'salesman',
181     'country',   'gln',     'insertdate',           'pricegroup', 'contact_origin', 'invoice_mail',
182     'creditlimit', 'ustid', 'commercial_court', 'delivery_order_mail'
183   );
184
185   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
186   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
187   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
188
189   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
190
191   my %column_defs = (
192     'id'                => { 'text' => $locale->text('ID'), },
193     "$form->{db}number" => { 'text' => $locale->text('Number'), },
194     'name'              => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
195     'contact'           => { 'text' => $locale->text('Contact'), },
196     'main_contact_person'  => { 'text' => $locale->text('Main Contact Person'), },
197     'phone'             => { 'text' => $locale->text('Phone'), },
198     'fax'               => { 'text' => $locale->text('Fax'), },
199     'email'             => { 'text' => $locale->text('E-mail'), },
200     'cc'                => { 'text' => $locale->text('Cc'), },
201     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
202     'business'          => { 'text' => $locale->text('Type of Business'), },
203     'invnumber'         => { 'text' => $locale->text('Invoice'), },
204     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
205     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
206     'street'            => { 'text' => $locale->text('Street'), },
207     'zipcode'           => { 'text' => $locale->text('Zipcode'), },
208     'city'              => { 'text' => $locale->text('City'), },
209     'country'           => { 'text' => $locale->text('Country'), },
210     'gln'               => { 'text' => $locale->text('GLN'), },
211     'salesman'          => { 'text' => $locale->text('Salesman'), },
212     'discount'          => { 'text' => $locale->text('Discount'), },
213     'payment'           => { 'text' => $locale->text('Payment Terms'), },
214     'insertdate'        => { 'text' => $locale->text('Insert Date'), },
215     'pricegroup'        => { 'text' => $locale->text('Pricegroup'), },
216     'invoice_mail'      => { 'text' => $locale->text('Email of the invoice recipient'), },
217     'delivery_order_mail' => { 'text' => $locale->text('Email of the delivery order recipient'), },
218     'contact_origin'    => { 'text' => $locale->text('Origin of personal data'), },
219     'creditlimit'       => { 'text' => $locale->text('Credit Limit'), },
220     'ustid'             => { 'text' => $locale->text('VAT ID'), },
221     'commercial_court'  => { 'text' => $locale->text('Commercial court'), },
222     %column_defs_cvars,
223   );
224
225   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
226
227   my @hidden_variables  = ( qw(
228       db status obsolete name contact email cp_name addr_street addr_zipcode
229       addr_city addr_country addr_gln business_id salesman_id insertdateto insertdatefrom all
230     ), "$form->{db}number",
231     map({ "cvar_$_->{name}" } @searchable_custom_variables),
232     map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
233     map({ "l_$_" } @columns),
234   );
235
236   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
237   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
238   $form->{callback}     = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
239
240   foreach (@columns) {
241     my $sortdir              = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
242     $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
243   }
244
245   my ($ordertype, $quotationtype, $attachment_basename);
246   if ($form->{IS_CUSTOMER}) {
247     $form->{title}       = $locale->text('Customers');
248     $ordertype           = 'sales_order';
249     $quotationtype       = 'sales_quotation';
250     $attachment_basename = $locale->text('customer_list');
251
252   } else {
253     $form->{title}       = $locale->text('Vendors');
254     $ordertype           = 'purchase_order';
255     $quotationtype       = 'request_quotation';
256     $attachment_basename = $locale->text('vendor_list');
257   }
258
259   my $report = SL::ReportGenerator->new(\%myconfig, $form);
260
261   $report->set_options('top_info_text'         => join("\n", @options),
262                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
263                        'output_format'         => 'HTML',
264                        'title'                 => $form->{title},
265                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
266     );
267   $report->set_options_from_form();
268   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
269
270   $report->set_columns(%column_defs);
271   $report->set_column_order(@columns);
272
273   $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
274
275   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
276
277   CVar->add_custom_variables_to_report('module'         => 'CT',
278                                        'trans_id_field' => 'id',
279                                        'configs'        => $cvar_configs,
280                                        'column_defs'    => \%column_defs,
281                                        'data'           => $form->{CT});
282
283   my $previous_id;
284
285   foreach my $ref (@{ $form->{CT} }) {
286     my $row = { map { $_ => { 'data' => '' } } @columns };
287
288     if ($ref->{id} ne $previous_id) {
289       $previous_id = $ref->{id};
290       $ref->{discount} = $form->format_amount(\%myconfig, $ref->{discount} * 100.0, 2);
291       $ref->{creditlimit} = $form->format_amount(\%myconfig, $ref->{creditlimit}, 2);
292       map { $row->{$_}->{data} = $ref->{$_} } @columns;
293
294       $row->{name}->{link}  = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
295       $row->{email}->{link} = 'mailto:' . E($ref->{email});
296     }
297
298     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
299     if ($::instance_conf->get_feature_experimental_order) {
300       if ('oe' eq $ref->{module}) {
301         $base_url             = build_std_url("script=controller.pl", 'action=Order/edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
302       }
303     }
304
305     $row->{invnumber}->{link} = $base_url;
306     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
307     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
308     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
309     $row->{$column}->{data}   = $ref->{$column};
310
311     if (my $number = SL::CTI->sanitize_number(number => $ref->{phone})) {
312       $row->{phone}->{link}       = SL::CTI->call_link(number => $number);
313       $row->{phone}->{link_class} = 'cti_call_action';
314     }
315
316     $report->add_data($row);
317   }
318
319   setup_ct_list_names_action_bar();
320   $report->generate_with_headers();
321
322   $main::lxdebug->leave_sub();
323 }
324
325 sub list_contacts {
326   $::lxdebug->enter_sub;
327   $::auth->assert('customer_vendor_edit');
328
329   $::form->{sortdir} = 1 unless defined $::form->{sortdir};
330
331   my @contacts     = CT->search_contacts(
332     search_term => $::form->{search_term},
333     filter      => $::form->{filter},
334   );
335
336   my $cvar_configs = CVar->get_configs('module' => 'Contacts');
337
338   my @columns      = qw(
339     cp_id vcname vcnumber cp_name cp_givenname cp_street cp_zipcode cp_city cp_phone1 cp_phone2 cp_privatphone
340     cp_mobile1 cp_mobile2 cp_fax cp_email cp_privatemail cp_abteilung cp_position cp_birthday cp_gender
341   );
342
343   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
344   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
345   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
346
347   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
348
349   my @visible_columns;
350   if ($::form->{l}) {
351     @visible_columns = grep { $::form->{l}{$_} } @columns;
352     push @visible_columns, qw(cp_phone1 cp_phone2)   if $::form->{l}{cp_phone};
353     push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
354   } else {
355    @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
356   }
357
358   my %column_defs  = (
359     'cp_id'        => { 'text' => $::locale->text('ID'), },
360     'vcname'       => { 'text' => $::locale->text('Customer/Vendor'), },
361     'vcnumber'     => { 'text' => $::locale->text('Customer/Vendor Number'), },
362     'cp_name'      => { 'text' => $::locale->text('Name'), },
363     'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
364     'cp_street'    => { 'text' => $::locale->text('Street'), },
365     'cp_zipcode'   => { 'text' => $::locale->text('Zipcode'), },
366     'cp_city'      => { 'text' => $::locale->text('City'), },
367     'cp_phone1'    => { 'text' => $::locale->text('Phone1'), },
368     'cp_phone2'    => { 'text' => $::locale->text('Phone2'), },
369     'cp_mobile1'   => { 'text' => $::locale->text('Mobile1'), },
370     'cp_mobile2'   => { 'text' => $::locale->text('Mobile2'), },
371     'cp_email'     => { 'text' => $::locale->text('E-mail'), },
372     'cp_abteilung' => { 'text' => $::locale->text('Department'), },
373     'cp_position'  => { 'text' => $::locale->text('Function/position'), },
374     'cp_birthday'  => { 'text' => $::locale->text('Birthday'), },
375     'cp_gender'    => { 'text' => $::locale->text('Gender'), },
376     'cp_fax'       => { 'text' => $::locale->text('Fax'), },
377     'cp_privatphone' => { 'text' => $::locale->text('Private Phone') },
378     'cp_privatemail' => { 'text' => $::locale->text('Private E-mail') },
379     %column_defs_cvars,
380   );
381
382   map { $column_defs{$_}->{visible} = 1 } @visible_columns;
383
384   my @hidden_variables  = (qw(search_term filter l));
385   my $hide_vars         = { map { $_ => $::form->{$_} } @hidden_variables };
386   my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
387   my $callback          = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
388   $::form->{callback}     = "$callback&sort=" . E($::form->{sort});
389
390   map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
391
392   $::form->{title} = $::locale->text('Contacts');
393
394   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
395
396   my @options;
397   push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
398   for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
399     push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
400   }
401   if ($::form->{filter}{status}) {
402     push @options, $::locale->text('Status') . ': ' . (
403       $::form->{filter}{status} =~ /active/   ? $::locale->text('Active')   :
404       $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
405       $::form->{filter}{status} =~ /all/      ? $::locale->text('All')      : ''
406     );
407   }
408
409
410   $report->set_options('top_info_text'       => join("\n", @options),
411                        'output_format'       => 'HTML',
412                        'title'               => $::form->{title},
413                        'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
414     );
415   $report->set_options_from_form;
416
417   $report->set_columns(%column_defs);
418   $report->set_column_order(@columns);
419
420   $report->set_export_options('list_contacts', @hidden_variables);
421
422   $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
423
424   CVar->add_custom_variables_to_report('module'         => 'Contacts',
425                                        'trans_id_field' => 'cp_id',
426                                        'configs'        => $cvar_configs,
427                                        'column_defs'    => \%column_defs,
428                                        'data'           => \@contacts);
429
430
431   foreach my $ref (@contacts) {
432     my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
433
434     $row->{vcname}->{link}   = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
435     $row->{vcnumber}->{link} = $row->{vcname}->{link};
436
437     for (qw(cp_email cp_privatemail)) {
438       $row->{$_}->{link} = 'mailto:' . E($ref->{$_}) if $ref->{$_};
439     }
440
441     for (qw(cp_phone1 cp_phone2 cp_mobile1)) {
442       next unless my $number = SL::CTI->sanitize_number(number => $ref->{$_});
443
444       $row->{$_}->{link}       = SL::CTI->call_link(number => $number);
445       $row->{$_}->{link_class} = 'cti_call_action';
446     }
447
448     $report->add_data($row);
449   }
450
451   $report->generate_with_headers();
452
453   $::lxdebug->leave_sub;
454 }
455
456 sub setup_ct_search_action_bar {
457   my %params = @_;
458
459   for my $bar ($::request->layout->get('actionbar')) {
460     $bar->add(
461       action => [
462         t8('Search'),
463         submit    => [ '#form', { action => 'list_names' } ],
464         accesskey => 'enter',
465       ],
466     );
467   }
468 }
469
470 sub setup_ct_list_names_action_bar {
471   my %params = @_;
472
473   for my $bar ($::request->layout->get('actionbar')) {
474     $bar->add(
475       action => [
476         t8('Add'),
477         submit    => [ '#new_form', { action => 'CustomerVendor/add' } ],
478         accesskey => 'enter',
479       ],
480     );
481   }
482 }
483
484 sub setup_ct_search_contact_action_bar {
485   my %params = @_;
486
487   for my $bar ($::request->layout->get('actionbar')) {
488     $bar->add(
489       action => [
490         t8('Search'),
491         submit    => [ '#form', { action => 'list_contacts' } ],
492         accesskey => 'enter',
493       ],
494     );
495   }
496 }
497
498 sub continue { call_sub($main::form->{nextsub}); }