1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
   9 # Copyright (c) 1998-2002
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  15 #  Contributors: Reed White <alta@alta-research.com>
 
  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.
 
  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 #======================================================================
 
  31 # customer/vendor module
 
  33 #======================================================================
 
  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!')
 
  48 use POSIX qw(strftime);
 
  53 use SL::Request qw(flatten);
 
  56 use SL::DB::DeliveryTerm;
 
  57 use SL::ReportGenerator;
 
  58 use SL::MoreCommon qw(uri_encode);
 
  60 require "bin/mozilla/common.pl";
 
  61 require "bin/mozilla/reportgenerator.pl";
 
  69   $main::lxdebug->enter_sub();
 
  71   $main::auth->assert('customer_vendor_edit');
 
  73   my $form     = $main::form;
 
  74   my $locale   = $main::locale;
 
  76   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
 
  78   $form->get_lists("business_types" => "ALL_BUSINESS_TYPES",
 
  79                    "salesmen"       => "ALL_SALESMEN");
 
  80   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
 
  82   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'CT');
 
  83   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
 
  84    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
 
  85                                                                            'include_prefix' => 'l_',
 
  86                                                                            'include_value'  => 'Y');
 
  88   $form->{title}    = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
 
  91   print $form->parse_html_template('ct/search');
 
  93   $main::lxdebug->leave_sub();
 
  97   $::lxdebug->enter_sub;
 
  98   $::auth->assert('customer_vendor_edit');
 
 100   $::form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'Contacts');
 
 101   ($::form->{CUSTOM_VARIABLES_FILTER_CODE},
 
 102    $::form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'    => $::form->{CUSTOM_VARIABLES},
 
 103                                                                            'include_prefix' => 'l.',
 
 104                                                                            'filter_prefix'  => 'filter.',
 
 105                                                                            'include_value'  => 'Y');
 
 107   $::form->{title} = $::locale->text('Search contacts');
 
 109   print $::form->parse_html_template('ct/search_contact');
 
 111   $::lxdebug->leave_sub;
 
 115   $main::lxdebug->enter_sub();
 
 117   $main::auth->assert('customer_vendor_edit');
 
 119   my $form     = $main::form;
 
 120   my %myconfig = %main::myconfig;
 
 121   my $locale   = $main::locale;
 
 123   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
 
 125   report_generator_set_default_sort('name', 1);
 
 127   CT->search(\%myconfig, \%$form);
 
 129   my $cvar_configs = CVar->get_configs('module' => 'CT');
 
 132   if ($form->{status} eq 'all') {
 
 133     push @options, $locale->text('All');
 
 134   } elsif ($form->{status} eq 'orphaned') {
 
 135     push @options, $locale->text('Orphaned');
 
 138   push @options, $locale->text('Name') . " : $form->{name}"                                       if $form->{name};
 
 139   push @options, $locale->text('Contact') . " : $form->{contact}"                                 if $form->{contact};
 
 140   push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|                    if $form->{"$form->{db}number"};
 
 141   push @options, $locale->text('E-mail') . " : $form->{email}"                                    if $form->{email};
 
 142   push @options, $locale->text('Contact person (surname)')           . " : $form->{cp_name}"      if $form->{cp_name};
 
 143   push @options, $locale->text('Billing/shipping address (city)')    . " : $form->{addr_city}"    if $form->{addr_city};
 
 144   push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{addr_zipcode}" if $form->{addr_zipcode};
 
 145   push @options, $locale->text('Billing/shipping address (street)')  . " : $form->{addr_street}"  if $form->{addr_street};
 
 146   push @options, $locale->text('Billing/shipping address (country)') . " : $form->{addr_country}" if $form->{addr_country};
 
 147   push @options, $locale->text('Billing/shipping address (GLN)')     . " : $form->{addr_gln}"     if $form->{addr_gln};
 
 148   push @options, $locale->text('Quick Search')                       . " : $form->{all}"          if $form->{all};
 
 150   if ($form->{business_id}) {
 
 151     my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
 
 153       my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
 
 154       push @options, $label . " : " . $business->description;
 
 157   if ($form->{salesman_id}) {
 
 158     my $salesman = SL::DB::Manager::Employee->find_by(id => $form->{salesman_id});
 
 160       push @options, $locale->text('Salesman') . " : " . $salesman->name;
 
 164   if ( $form->{insertdatefrom} or $form->{insertdateto} ) {
 
 165     push @options, $locale->text('Insert Date');
 
 166     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{insertdatefrom}, 1) if $form->{insertdatefrom};
 
 167     push @options, $locale->text('Bis')  . " " . $locale->date(\%myconfig, $form->{insertdateto},   1) if $form->{insertdateto};
 
 171     'id',        'name',    "$form->{db}number",   'contact',   'phone',    'discount',
 
 172     'fax',       'email',   'taxnumber',           'street',    'zipcode' , 'city',
 
 173     'business',  'payment', 'invnumber', 'ordnumber',           'quonumber', 'salesman',
 
 174     'country',   'gln',     'insertdate',           'pricegroup'
 
 177   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
 
 178   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
 
 179   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
 
 181   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
 
 184     'id'                => { 'text' => $locale->text('ID'), },
 
 185     "$form->{db}number" => { 'text' => $locale->text('Number'), },
 
 186     'name'              => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
 
 187     'contact'           => { 'text' => $locale->text('Contact'), },
 
 188     'phone'             => { 'text' => $locale->text('Phone'), },
 
 189     'fax'               => { 'text' => $locale->text('Fax'), },
 
 190     'email'             => { 'text' => $locale->text('E-mail'), },
 
 191     'cc'                => { 'text' => $locale->text('Cc'), },
 
 192     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
 
 193     'business'          => { 'text' => $locale->text('Type of Business'), },
 
 194     'invnumber'         => { 'text' => $locale->text('Invoice'), },
 
 195     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
 
 196     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
 
 197     'street'            => { 'text' => $locale->text('Street'), },
 
 198     'zipcode'           => { 'text' => $locale->text('Zipcode'), },
 
 199     'city'              => { 'text' => $locale->text('City'), },
 
 200     'country'           => { 'text' => $locale->text('Country'), },
 
 201     'gln'               => { 'text' => $locale->text('GLN'), },
 
 202     'salesman'          => { 'text' => $locale->text('Salesman'), },
 
 203     'discount'          => { 'text' => $locale->text('Discount'), },
 
 204     'payment'           => { 'text' => $locale->text('Payment Terms'), },
 
 205     'insertdate'        => { 'text' => $locale->text('Insert Date'), },
 
 206     'pricegroup'        => { 'text' => $locale->text('Pricegroup'), },
 
 210   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
 
 212   my @hidden_variables  = ( qw(
 
 213       db status obsolete name contact email cp_name addr_street addr_zipcode
 
 214       addr_city addr_country addr_gln business_id salesman_id insertdateto insertdatefrom all
 
 215     ), "$form->{db}number",
 
 216     map({ "cvar_$_->{name}" } @searchable_custom_variables),
 
 217     map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
 
 218     map({ "l_$_" } @columns),
 
 221   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
 
 222   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
 
 223   $form->{callback}     = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
 
 226     my $sortdir              = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
 
 227     $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
 
 230   my ($ordertype, $quotationtype, $attachment_basename);
 
 231   if ($form->{IS_CUSTOMER}) {
 
 232     $form->{title}       = $locale->text('Customers');
 
 233     $ordertype           = 'sales_order';
 
 234     $quotationtype       = 'sales_quotation';
 
 235     $attachment_basename = $locale->text('customer_list');
 
 238     $form->{title}       = $locale->text('Vendors');
 
 239     $ordertype           = 'purchase_order';
 
 240     $quotationtype       = 'request_quotation';
 
 241     $attachment_basename = $locale->text('vendor_list');
 
 244   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 246   $report->set_options('top_info_text'         => join("\n", @options),
 
 247                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
 
 248                        'output_format'         => 'HTML',
 
 249                        'title'                 => $form->{title},
 
 250                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
 252   $report->set_options_from_form();
 
 253   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 255   $report->set_columns(%column_defs);
 
 256   $report->set_column_order(@columns);
 
 258   $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
 
 260   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
 262   CVar->add_custom_variables_to_report('module'         => 'CT',
 
 263                                        'trans_id_field' => 'id',
 
 264                                        'configs'        => $cvar_configs,
 
 265                                        'column_defs'    => \%column_defs,
 
 266                                        'data'           => $form->{CT});
 
 270   foreach my $ref (@{ $form->{CT} }) {
 
 271     my $row = { map { $_ => { 'data' => '' } } @columns };
 
 273     if ($ref->{id} ne $previous_id) {
 
 274       $previous_id = $ref->{id};
 
 275       $ref->{discount} = $form->format_amount(\%myconfig, $ref->{discount} * 100.0, 2);
 
 276       map { $row->{$_}->{data} = $ref->{$_} } @columns;
 
 278       $row->{name}->{link}  = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
 
 279       $row->{email}->{link} = 'mailto:' . E($ref->{email});
 
 282     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
 
 283     $row->{invnumber}->{link} = $base_url;
 
 284     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
 
 285     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
 
 286     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
 
 287     $row->{$column}->{data}   = $ref->{$column};
 
 289     if (my $number = SL::CTI->sanitize_number(number => $ref->{phone})) {
 
 290       $row->{phone}->{link}       = SL::CTI->call_link(number => $number);
 
 291       $row->{phone}->{link_class} = 'cti_call_action';
 
 294     $report->add_data($row);
 
 297   $report->generate_with_headers();
 
 299   $main::lxdebug->leave_sub();
 
 303   $::lxdebug->enter_sub;
 
 304   $::auth->assert('customer_vendor_edit');
 
 306   $::form->{sortdir} = 1 unless defined $::form->{sortdir};
 
 308   my @contacts     = CT->search_contacts(
 
 309     search_term => $::form->{search_term},
 
 310     filter      => $::form->{filter},
 
 313   my $cvar_configs = CVar->get_configs('module' => 'Contacts');
 
 316     cp_id vcname vcnumber cp_name cp_givenname cp_street cp_zipcode cp_city cp_phone1 cp_phone2 cp_privatphone
 
 317     cp_mobile1 cp_mobile2 cp_fax cp_email cp_privatemail cp_abteilung cp_position cp_birthday cp_gender
 
 320   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
 
 321   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
 
 322   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
 
 324   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
 
 328     @visible_columns = grep { $::form->{l}{$_} } @columns;
 
 329     push @visible_columns, qw(cp_phone1 cp_phone2)   if $::form->{l}{cp_phone};
 
 330     push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
 
 332    @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
 
 336     'cp_id'        => { 'text' => $::locale->text('ID'), },
 
 337     'vcname'       => { 'text' => $::locale->text('Customer/Vendor'), },
 
 338     'vcnumber'     => { 'text' => $::locale->text('Customer/Vendor Number'), },
 
 339     'cp_name'      => { 'text' => $::locale->text('Name'), },
 
 340     'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
 
 341     'cp_street'    => { 'text' => $::locale->text('Street'), },
 
 342     'cp_zipcode'   => { 'text' => $::locale->text('Zipcode'), },
 
 343     'cp_city'      => { 'text' => $::locale->text('City'), },
 
 344     'cp_phone1'    => { 'text' => $::locale->text('Phone1'), },
 
 345     'cp_phone2'    => { 'text' => $::locale->text('Phone2'), },
 
 346     'cp_mobile1'   => { 'text' => $::locale->text('Mobile1'), },
 
 347     'cp_mobile2'   => { 'text' => $::locale->text('Mobile2'), },
 
 348     'cp_email'     => { 'text' => $::locale->text('E-mail'), },
 
 349     'cp_abteilung' => { 'text' => $::locale->text('Department'), },
 
 350     'cp_position'  => { 'text' => $::locale->text('Function/position'), },
 
 351     'cp_birthday'  => { 'text' => $::locale->text('Birthday'), },
 
 352     'cp_gender'    => { 'text' => $::locale->text('Gender'), },
 
 353     'cp_fax'       => { 'text' => $::locale->text('Fax'), },
 
 354     'cp_privatphone' => { 'text' => $::locale->text('Private Phone') },
 
 355     'cp_privatemail' => { 'text' => $::locale->text('Private E-mail') },
 
 359   map { $column_defs{$_}->{visible} = 1 } @visible_columns;
 
 361   my @hidden_variables  = (qw(search_term filter l));
 
 362   my $hide_vars         = { map { $_ => $::form->{$_} } @hidden_variables };
 
 363   my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
 
 364   my $callback          = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
 
 365   $::form->{callback}     = "$callback&sort=" . E($::form->{sort});
 
 367   map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
 
 369   $::form->{title} = $::locale->text('Contacts');
 
 371   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
 
 374   push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
 
 375   for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
 
 376     push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
 
 378   if ($::form->{filter}{status}) {
 
 379     push @options, $::locale->text('Status') . ': ' . (
 
 380       $::form->{filter}{status} =~ /active/   ? $::locale->text('Active')   :
 
 381       $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
 
 382       $::form->{filter}{status} =~ /all/      ? $::locale->text('All')      : ''
 
 387   $report->set_options('top_info_text'       => join("\n", @options),
 
 388                        'output_format'       => 'HTML',
 
 389                        'title'               => $::form->{title},
 
 390                        'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
 
 392   $report->set_options_from_form;
 
 394   $report->set_columns(%column_defs);
 
 395   $report->set_column_order(@columns);
 
 397   $report->set_export_options('list_contacts', @hidden_variables);
 
 399   $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
 
 401   CVar->add_custom_variables_to_report('module'         => 'Contacts',
 
 402                                        'trans_id_field' => 'cp_id',
 
 403                                        'configs'        => $cvar_configs,
 
 404                                        'column_defs'    => \%column_defs,
 
 405                                        'data'           => \@contacts);
 
 408   foreach my $ref (@contacts) {
 
 409     my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
 
 411     $row->{vcname}->{link}   = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
 
 412     $row->{vcnumber}->{link} = $row->{vcname}->{link};
 
 414     for (qw(cp_email cp_privatemail)) {
 
 415       $row->{$_}->{link} = 'mailto:' . E($ref->{$_}) if $ref->{$_};
 
 418     for (qw(cp_phone1 cp_phone2 cp_mobile1)) {
 
 419       next unless my $number = SL::CTI->sanitize_number(number => $ref->{$_});
 
 421       $row->{$_}->{link}       = SL::CTI->call_link(number => $number);
 
 422       $row->{$_}->{link_class} = 'cti_call_action';
 
 425     $report->add_data($row);
 
 428   $report->generate_with_headers;
 
 430   $::lxdebug->leave_sub;
 
 433 sub continue { call_sub($main::form->{nextsub}); }