Kunden/Lieferanten-Suche: Erfassen-Button überarbeitet
[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::CVar;
52 use SL::Request qw(flatten);
53 use SL::DB::Business;
54 use SL::DB::Default;
55 use SL::DB::DeliveryTerm;
56 use SL::Helper::Flash;
57 use SL::ReportGenerator;
58 use SL::MoreCommon qw(uri_encode);
59
60 require "bin/mozilla/common.pl";
61 require "bin/mozilla/reportgenerator.pl";
62
63 use strict;
64 1;
65
66 # end of main
67
68 sub search {
69   $main::lxdebug->enter_sub();
70
71   $main::auth->assert('customer_vendor_edit');
72
73   my $form     = $main::form;
74   my $locale   = $main::locale;
75
76   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
77
78   $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
79   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
80
81   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'CT');
82   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
83    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
84                                                                            'include_prefix' => 'l_',
85                                                                            'include_value'  => 'Y');
86
87   $form->{title}    = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
88
89   $form->header();
90   print $form->parse_html_template('ct/search');
91
92   $main::lxdebug->leave_sub();
93 }
94
95 sub search_contact {
96   $::lxdebug->enter_sub;
97   $::auth->assert('customer_vendor_edit');
98
99   $::form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'Contacts');
100   ($::form->{CUSTOM_VARIABLES_FILTER_CODE},
101    $::form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'    => $::form->{CUSTOM_VARIABLES},
102                                                                            'include_prefix' => 'l.',
103                                                                            'filter_prefix'  => 'filter.',
104                                                                            'include_value'  => 'Y');
105
106   $::form->{title} = $::locale->text('Search contacts');
107   $::form->header;
108   print $::form->parse_html_template('ct/search_contact');
109
110   $::lxdebug->leave_sub;
111 }
112
113 sub list_names {
114   $main::lxdebug->enter_sub();
115
116   $main::auth->assert('customer_vendor_edit');
117
118   my $form     = $main::form;
119   my %myconfig = %main::myconfig;
120   my $locale   = $main::locale;
121
122   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
123
124   report_generator_set_default_sort('name', 1);
125
126   CT->search(\%myconfig, \%$form);
127
128   my $cvar_configs = CVar->get_configs('module' => 'CT');
129
130   my @options;
131   if ($form->{status} eq 'all') {
132     push @options, $locale->text('All');
133   } elsif ($form->{status} eq 'orphaned') {
134     push @options, $locale->text('Orphaned');
135   }
136
137   push @options, $locale->text('Name') . " : $form->{name}"                                    if $form->{name};
138   push @options, $locale->text('Contact') . " : $form->{contact}"                              if $form->{contact};
139   push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|                 if $form->{"$form->{db}number"};
140   push @options, $locale->text('E-mail') . " : $form->{email}"                                 if $form->{email};
141   push @options, $locale->text('Contact person (surname)')           . " : $form->{cp_name}"   if $form->{cp_name};
142   push @options, $locale->text('Billing/shipping address (city)')    . " : $form->{addr_city}" if $form->{addr_city};
143   push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{zipcode}"   if $form->{addr_zipcode};
144   push @options, $locale->text('Billing/shipping address (street)')  . " : $form->{street}"    if $form->{addr_street};
145   push @options, $locale->text('Billing/shipping address (country)') . " : $form->{country}"   if $form->{addr_country};
146
147   if ($form->{business_id}) {
148     my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
149     if ($business) {
150       my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
151       push @options, $label . " : " . $business->description;
152     }
153   }
154
155   my @columns = (
156     'id',        'name',      "$form->{db}number",   'contact',   'phone',    'discount',
157     'fax',       'email',     'taxnumber',           'street',    'zipcode' , 'city',
158     'business',  'invnumber', 'ordnumber',           'quonumber', 'salesman', 'country'
159   );
160
161   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
162   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
163   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
164
165   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
166
167   my %column_defs = (
168     'id'                => { 'text' => $locale->text('ID'), },
169     "$form->{db}number" => { 'text' => $locale->text('Number'), },
170     'name'              => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
171     'contact'           => { 'text' => $locale->text('Contact'), },
172     'phone'             => { 'text' => $locale->text('Phone'), },
173     'fax'               => { 'text' => $locale->text('Fax'), },
174     'email'             => { 'text' => $locale->text('E-mail'), },
175     'cc'                => { 'text' => $locale->text('Cc'), },
176     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
177     'business'          => { 'text' => $locale->text('Type of Business'), },
178     'invnumber'         => { 'text' => $locale->text('Invoice'), },
179     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
180     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
181     'street'            => { 'text' => $locale->text('Street'), },
182     'zipcode'           => { 'text' => $locale->text('Zipcode'), },
183     'city'              => { 'text' => $locale->text('City'), },
184     'country'           => { 'text' => $locale->text('Country'), },
185     'salesman'          => { 'text' => $locale->text('Salesman'), },
186     'discount'          => { 'text' => $locale->text('Discount'), },
187     %column_defs_cvars,
188   );
189
190   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
191
192   my @hidden_variables  = ( qw(
193       db status obsolete name contact email cp_name addr_street addr_zipcode
194       addr_city addr_country business_id
195     ), "$form->{db}number",
196     map({ "cvar_$_->{name}" } @searchable_custom_variables),
197     map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
198     map({ "l_$_" } @columns),
199   );
200
201   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
202   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
203   $form->{callback}     = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
204
205   foreach (@columns) {
206     my $sortdir              = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
207     $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
208   }
209
210   my ($ordertype, $quotationtype, $attachment_basename);
211   if ($form->{IS_CUSTOMER}) {
212     $form->{title}       = $locale->text('Customers');
213     $ordertype           = 'sales_order';
214     $quotationtype       = 'sales_quotation';
215     $attachment_basename = $locale->text('customer_list');
216
217   } else {
218     $form->{title}       = $locale->text('Vendors');
219     $ordertype           = 'purchase_order';
220     $quotationtype       = 'request_quotation';
221     $attachment_basename = $locale->text('vendor_list');
222   }
223
224   my $report = SL::ReportGenerator->new(\%myconfig, $form);
225
226   $report->set_options('top_info_text'         => join("\n", @options),
227                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
228                        'output_format'         => 'HTML',
229                        'title'                 => $form->{title},
230                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
231     );
232   $report->set_options_from_form();
233   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
234
235   $report->set_columns(%column_defs);
236   $report->set_column_order(@columns);
237
238   $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
239
240   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
241
242   CVar->add_custom_variables_to_report('module'         => 'CT',
243                                        'trans_id_field' => 'id',
244                                        'configs'        => $cvar_configs,
245                                        'column_defs'    => \%column_defs,
246                                        'data'           => $form->{CT});
247
248   my $previous_id;
249
250   foreach my $ref (@{ $form->{CT} }) {
251     my $row = { map { $_ => { 'data' => '' } } @columns };
252
253     if ($ref->{id} ne $previous_id) {
254       $previous_id = $ref->{id};
255       $ref->{discount} = $form->format_amount(\%myconfig, $ref->{discount} * 100.0, 2);
256       map { $row->{$_}->{data} = $ref->{$_} } @columns;
257
258       $row->{name}->{link}  = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
259       $row->{email}->{link} = 'mailto:' . E($ref->{email});
260     }
261
262     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
263     $row->{invnumber}->{link} = $base_url;
264     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
265     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
266     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
267     $row->{$column}->{data}   = $ref->{$column};
268
269     $report->add_data($row);
270   }
271
272   $report->generate_with_headers();
273
274   $main::lxdebug->leave_sub();
275 }
276
277 sub list_contacts {
278   $::lxdebug->enter_sub;
279   $::auth->assert('customer_vendor_edit');
280
281   $::form->{sortdir} = 1 unless defined $::form->{sortdir};
282
283   my @contacts     = CT->search_contacts(
284     search_term => $::form->{search_term},
285     filter      => $::form->{filter},
286   );
287
288   my $cvar_configs = CVar->get_configs('module' => 'Contacts');
289
290   my @columns      = qw(
291     cp_id vcname vcnumber cp_name cp_givenname cp_street cp_zipcode cp_city cp_phone1 cp_phone2 cp_privatphone
292     cp_mobile1 cp_mobile2 cp_fax cp_email cp_privatemail cp_abteilung cp_position cp_birthday cp_gender
293   );
294
295   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
296   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
297   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
298
299   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
300
301   my @visible_columns;
302   if ($::form->{l}) {
303     @visible_columns = grep { $::form->{l}{$_} } @columns;
304     push @visible_columns, qw(cp_phone1 cp_phone2)   if $::form->{l}{cp_phone};
305     push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
306   } else {
307    @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
308   }
309
310   my %column_defs  = (
311     'cp_id'        => { 'text' => $::locale->text('ID'), },
312     'vcname'       => { 'text' => $::locale->text('Customer/Vendor'), },
313     'vcnumber'     => { 'text' => $::locale->text('Customer/Vendor Number'), },
314     'cp_name'      => { 'text' => $::locale->text('Name'), },
315     'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
316     'cp_street'    => { 'text' => $::locale->text('Street'), },
317     'cp_zipcode'   => { 'text' => $::locale->text('Zipcode'), },
318     'cp_city'      => { 'text' => $::locale->text('City'), },
319     'cp_phone1'    => { 'text' => $::locale->text('Phone1'), },
320     'cp_phone2'    => { 'text' => $::locale->text('Phone2'), },
321     'cp_mobile1'   => { 'text' => $::locale->text('Mobile1'), },
322     'cp_mobile2'   => { 'text' => $::locale->text('Mobile2'), },
323     'cp_email'     => { 'text' => $::locale->text('E-mail'), },
324     'cp_abteilung' => { 'text' => $::locale->text('Department'), },
325     'cp_position'  => { 'text' => $::locale->text('Function/position'), },
326     'cp_birthday'  => { 'text' => $::locale->text('Birthday'), },
327     'cp_gender'    => { 'text' => $::locale->text('Gender'), },
328     'cp_fax'       => { 'text' => $::locale->text('Fax'), },
329     'cp_privatphone' => { 'text' => $::locale->text('Private Phone') },
330     'cp_privatemail' => { 'text' => $::locale->text('Private E-mail') },
331     %column_defs_cvars,
332   );
333
334   map { $column_defs{$_}->{visible} = 1 } @visible_columns;
335
336   my @hidden_variables  = (qw(search_term filter l));
337   my $hide_vars         = { map { $_ => $::form->{$_} } @hidden_variables };
338   my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
339   my $callback          = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
340   $::form->{callback}     = "$callback&sort=" . E($::form->{sort});
341
342   map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
343
344   $::form->{title} = $::locale->text('Contacts');
345
346   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
347
348   my @options;
349   push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
350   for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
351     push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
352   }
353   if ($::form->{filter}{status}) {
354     push @options, $::locale->text('Status') . ': ' . (
355       $::form->{filter}{status} =~ /active/   ? $::locale->text('Active')   :
356       $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
357       $::form->{filter}{status} =~ /all/      ? $::locale->text('All')      : ''
358     );
359   }
360
361
362   $report->set_options('top_info_text'       => join("\n", @options),
363                        'output_format'       => 'HTML',
364                        'title'               => $::form->{title},
365                        'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
366     );
367   $report->set_options_from_form;
368
369   $report->set_columns(%column_defs);
370   $report->set_column_order(@columns);
371
372   $report->set_export_options('list_contacts', @hidden_variables);
373
374   $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
375
376   CVar->add_custom_variables_to_report('module'         => 'Contacts',
377                                        'trans_id_field' => 'cp_id',
378                                        'configs'        => $cvar_configs,
379                                        'column_defs'    => \%column_defs,
380                                        'data'           => \@contacts);
381
382
383   foreach my $ref (@contacts) {
384     my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
385
386     $row->{vcname}->{link}   = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
387     $row->{vcnumber}->{link} = $row->{vcname}->{link};
388
389     for (qw(cp_email cp_privatemail)) {
390       $row->{$_}->{link} = 'mailto:' . E($ref->{$_}) if $ref->{$_};
391     }
392
393     $report->add_data($row);
394   }
395
396   $report->generate_with_headers;
397
398   $::lxdebug->leave_sub;
399 }
400
401 sub continue { call_sub($main::form->{nextsub}); }