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