0793a4459aac6627120b136d554db62dd4314e1a
[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',  'payment', 'invnumber', 'ordnumber',           'quonumber', 'salesman',
159     'country'
160   );
161
162   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
163   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
164   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
165
166   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
167
168   my %column_defs = (
169     'id'                => { 'text' => $locale->text('ID'), },
170     "$form->{db}number" => { 'text' => $locale->text('Number'), },
171     'name'              => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
172     'contact'           => { 'text' => $locale->text('Contact'), },
173     'phone'             => { 'text' => $locale->text('Phone'), },
174     'fax'               => { 'text' => $locale->text('Fax'), },
175     'email'             => { 'text' => $locale->text('E-mail'), },
176     'cc'                => { 'text' => $locale->text('Cc'), },
177     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
178     'business'          => { 'text' => $locale->text('Type of Business'), },
179     'invnumber'         => { 'text' => $locale->text('Invoice'), },
180     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
181     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
182     'street'            => { 'text' => $locale->text('Street'), },
183     'zipcode'           => { 'text' => $locale->text('Zipcode'), },
184     'city'              => { 'text' => $locale->text('City'), },
185     'country'           => { 'text' => $locale->text('Country'), },
186     'salesman'          => { 'text' => $locale->text('Salesman'), },
187     'discount'          => { 'text' => $locale->text('Discount'), },
188     'payment'           => { 'text' => $locale->text('Payment Terms'), },
189     %column_defs_cvars,
190   );
191
192   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
193
194   my @hidden_variables  = ( qw(
195       db status obsolete name contact email cp_name addr_street addr_zipcode
196       addr_city addr_country business_id
197     ), "$form->{db}number",
198     map({ "cvar_$_->{name}" } @searchable_custom_variables),
199     map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
200     map({ "l_$_" } @columns),
201   );
202
203   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
204   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
205   $form->{callback}     = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
206
207   foreach (@columns) {
208     my $sortdir              = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
209     $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
210   }
211
212   my ($ordertype, $quotationtype, $attachment_basename);
213   if ($form->{IS_CUSTOMER}) {
214     $form->{title}       = $locale->text('Customers');
215     $ordertype           = 'sales_order';
216     $quotationtype       = 'sales_quotation';
217     $attachment_basename = $locale->text('customer_list');
218
219   } else {
220     $form->{title}       = $locale->text('Vendors');
221     $ordertype           = 'purchase_order';
222     $quotationtype       = 'request_quotation';
223     $attachment_basename = $locale->text('vendor_list');
224   }
225
226   my $report = SL::ReportGenerator->new(\%myconfig, $form);
227
228   $report->set_options('top_info_text'         => join("\n", @options),
229                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
230                        'output_format'         => 'HTML',
231                        'title'                 => $form->{title},
232                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
233     );
234   $report->set_options_from_form();
235   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
236
237   $report->set_columns(%column_defs);
238   $report->set_column_order(@columns);
239
240   $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
241
242   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
243
244   CVar->add_custom_variables_to_report('module'         => 'CT',
245                                        'trans_id_field' => 'id',
246                                        'configs'        => $cvar_configs,
247                                        'column_defs'    => \%column_defs,
248                                        'data'           => $form->{CT});
249
250   my $previous_id;
251
252   foreach my $ref (@{ $form->{CT} }) {
253     my $row = { map { $_ => { 'data' => '' } } @columns };
254
255     if ($ref->{id} ne $previous_id) {
256       $previous_id = $ref->{id};
257       $ref->{discount} = $form->format_amount(\%myconfig, $ref->{discount} * 100.0, 2);
258       map { $row->{$_}->{data} = $ref->{$_} } @columns;
259
260       $row->{name}->{link}  = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
261       $row->{email}->{link} = 'mailto:' . E($ref->{email});
262     }
263
264     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
265     $row->{invnumber}->{link} = $base_url;
266     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
267     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
268     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
269     $row->{$column}->{data}   = $ref->{$column};
270
271     $report->add_data($row);
272   }
273
274   $report->generate_with_headers();
275
276   $main::lxdebug->leave_sub();
277 }
278
279 sub list_contacts {
280   $::lxdebug->enter_sub;
281   $::auth->assert('customer_vendor_edit');
282
283   $::form->{sortdir} = 1 unless defined $::form->{sortdir};
284
285   my @contacts     = CT->search_contacts(
286     search_term => $::form->{search_term},
287     filter      => $::form->{filter},
288   );
289
290   my $cvar_configs = CVar->get_configs('module' => 'Contacts');
291
292   my @columns      = qw(
293     cp_id vcname vcnumber cp_name cp_givenname cp_street cp_zipcode cp_city cp_phone1 cp_phone2 cp_privatphone
294     cp_mobile1 cp_mobile2 cp_fax cp_email cp_privatemail cp_abteilung cp_position cp_birthday cp_gender
295   );
296
297   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
298   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
299   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
300
301   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
302
303   my @visible_columns;
304   if ($::form->{l}) {
305     @visible_columns = grep { $::form->{l}{$_} } @columns;
306     push @visible_columns, qw(cp_phone1 cp_phone2)   if $::form->{l}{cp_phone};
307     push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
308   } else {
309    @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
310   }
311
312   my %column_defs  = (
313     'cp_id'        => { 'text' => $::locale->text('ID'), },
314     'vcname'       => { 'text' => $::locale->text('Customer/Vendor'), },
315     'vcnumber'     => { 'text' => $::locale->text('Customer/Vendor Number'), },
316     'cp_name'      => { 'text' => $::locale->text('Name'), },
317     'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
318     'cp_street'    => { 'text' => $::locale->text('Street'), },
319     'cp_zipcode'   => { 'text' => $::locale->text('Zipcode'), },
320     'cp_city'      => { 'text' => $::locale->text('City'), },
321     'cp_phone1'    => { 'text' => $::locale->text('Phone1'), },
322     'cp_phone2'    => { 'text' => $::locale->text('Phone2'), },
323     'cp_mobile1'   => { 'text' => $::locale->text('Mobile1'), },
324     'cp_mobile2'   => { 'text' => $::locale->text('Mobile2'), },
325     'cp_email'     => { 'text' => $::locale->text('E-mail'), },
326     'cp_abteilung' => { 'text' => $::locale->text('Department'), },
327     'cp_position'  => { 'text' => $::locale->text('Function/position'), },
328     'cp_birthday'  => { 'text' => $::locale->text('Birthday'), },
329     'cp_gender'    => { 'text' => $::locale->text('Gender'), },
330     'cp_fax'       => { 'text' => $::locale->text('Fax'), },
331     'cp_privatphone' => { 'text' => $::locale->text('Private Phone') },
332     'cp_privatemail' => { 'text' => $::locale->text('Private E-mail') },
333     %column_defs_cvars,
334   );
335
336   map { $column_defs{$_}->{visible} = 1 } @visible_columns;
337
338   my @hidden_variables  = (qw(search_term filter l));
339   my $hide_vars         = { map { $_ => $::form->{$_} } @hidden_variables };
340   my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
341   my $callback          = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
342   $::form->{callback}     = "$callback&sort=" . E($::form->{sort});
343
344   map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
345
346   $::form->{title} = $::locale->text('Contacts');
347
348   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
349
350   my @options;
351   push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
352   for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
353     push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
354   }
355   if ($::form->{filter}{status}) {
356     push @options, $::locale->text('Status') . ': ' . (
357       $::form->{filter}{status} =~ /active/   ? $::locale->text('Active')   :
358       $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
359       $::form->{filter}{status} =~ /all/      ? $::locale->text('All')      : ''
360     );
361   }
362
363
364   $report->set_options('top_info_text'       => join("\n", @options),
365                        'output_format'       => 'HTML',
366                        'title'               => $::form->{title},
367                        'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
368     );
369   $report->set_options_from_form;
370
371   $report->set_columns(%column_defs);
372   $report->set_column_order(@columns);
373
374   $report->set_export_options('list_contacts', @hidden_variables);
375
376   $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
377
378   CVar->add_custom_variables_to_report('module'         => 'Contacts',
379                                        'trans_id_field' => 'cp_id',
380                                        'configs'        => $cvar_configs,
381                                        'column_defs'    => \%column_defs,
382                                        'data'           => \@contacts);
383
384
385   foreach my $ref (@contacts) {
386     my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
387
388     $row->{vcname}->{link}   = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
389     $row->{vcnumber}->{link} = $row->{vcname}->{link};
390
391     for (qw(cp_email cp_privatemail)) {
392       $row->{$_}->{link} = 'mailto:' . E($ref->{$_}) if $ref->{$_};
393     }
394
395     $report->add_data($row);
396   }
397
398   $report->generate_with_headers;
399
400   $::lxdebug->leave_sub;
401 }
402
403 sub continue { call_sub($main::form->{nextsub}); }