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