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., 51 Franklin Street, Fifth Floor, Boston,
 
  30 #======================================================================
 
  32 # customer/vendor module
 
  34 #======================================================================
 
  36 # $locale->text('Customers')
 
  37 # $locale->text('Vendors')
 
  38 # $locale->text('Add Customer')
 
  39 # $locale->text('Add Vendor')
 
  40 # $locale->text('Edit Customer')
 
  41 # $locale->text('Edit Vendor')
 
  42 # $locale->text('Customer saved!')
 
  43 # $locale->text('Vendor saved!')
 
  44 # $locale->text('Customer deleted!')
 
  45 # $locale->text('Cannot delete customer!')
 
  46 # $locale->text('Vendor deleted!')
 
  47 # $locale->text('Cannot delete vendor!')
 
  49 use POSIX qw(strftime);
 
  54 use SL::Request qw(flatten);
 
  57 use SL::DB::DeliveryTerm;
 
  58 use SL::ReportGenerator;
 
  59 use SL::Locale::String qw(t8);
 
  60 use SL::MoreCommon qw(uri_encode);
 
  62 require "bin/mozilla/common.pl";
 
  63 require "bin/mozilla/reportgenerator.pl";
 
  71   $main::lxdebug->enter_sub();
 
  73   $main::auth->assert('customer_vendor_edit');
 
  75   my $form     = $main::form;
 
  76   my $locale   = $main::locale;
 
  78   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
 
  80   $form->get_lists("business_types" => "ALL_BUSINESS_TYPES",
 
  81                    "salesmen"       => "ALL_SALESMEN");
 
  82   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
 
  84   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'CT');
 
  85   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
 
  86    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
 
  87                                                                            'include_prefix' => 'l_',
 
  88                                                                            'include_value'  => 'Y');
 
  90   $form->{title}    = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
 
  92   setup_ct_search_action_bar();
 
  95   print $form->parse_html_template('ct/search');
 
  97   $main::lxdebug->leave_sub();
 
 101   $::lxdebug->enter_sub;
 
 102   $::auth->assert('customer_vendor_edit');
 
 104   $::form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'Contacts');
 
 105   ($::form->{CUSTOM_VARIABLES_FILTER_CODE},
 
 106    $::form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'    => $::form->{CUSTOM_VARIABLES},
 
 107                                                                            'include_prefix' => 'l.',
 
 108                                                                            'filter_prefix'  => 'filter.',
 
 109                                                                            'include_value'  => 'Y');
 
 111   $::form->{title} = $::locale->text('Search contacts');
 
 113   setup_ct_search_contact_action_bar();
 
 115   print $::form->parse_html_template('ct/search_contact');
 
 117   $::lxdebug->leave_sub;
 
 121   $main::lxdebug->enter_sub();
 
 123   $main::auth->assert('customer_vendor_edit');
 
 125   my $form     = $main::form;
 
 126   my %myconfig = %main::myconfig;
 
 127   my $locale   = $main::locale;
 
 129   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
 
 131   report_generator_set_default_sort('name', 1);
 
 133   CT->search(\%myconfig, \%$form);
 
 135   my $cvar_configs = CVar->get_configs('module' => 'CT');
 
 138   if ($form->{status} eq 'all') {
 
 139     push @options, $locale->text('All');
 
 140   } elsif ($form->{status} eq 'orphaned') {
 
 141     push @options, $locale->text('Orphaned');
 
 144   push @options, $locale->text('Name') . " : $form->{name}"                                       if $form->{name};
 
 145   push @options, $locale->text('Contact') . " : $form->{contact}"                                 if $form->{contact};
 
 146   push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|                    if $form->{"$form->{db}number"};
 
 147   push @options, $locale->text('E-mail') . " : $form->{email}"                                    if $form->{email};
 
 148   push @options, $locale->text('Contact person (surname)')           . " : $form->{cp_name}"      if $form->{cp_name};
 
 149   push @options, $locale->text('Billing/shipping address (city)')    . " : $form->{addr_city}"    if $form->{addr_city};
 
 150   push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{addr_zipcode}" if $form->{addr_zipcode};
 
 151   push @options, $locale->text('Billing/shipping address (street)')  . " : $form->{addr_street}"  if $form->{addr_street};
 
 152   push @options, $locale->text('Billing/shipping address (country)') . " : $form->{addr_country}" if $form->{addr_country};
 
 153   push @options, $locale->text('Billing/shipping address (GLN)')     . " : $form->{addr_gln}"     if $form->{addr_gln};
 
 154   push @options, $locale->text('Quick Search')                       . " : $form->{all}"          if $form->{all};
 
 156   if ($form->{business_id}) {
 
 157     my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
 
 159       my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
 
 160       push @options, $label . " : " . $business->description;
 
 163   if ($form->{salesman_id}) {
 
 164     my $salesman = SL::DB::Manager::Employee->find_by(id => $form->{salesman_id});
 
 166       push @options, $locale->text('Salesman') . " : " . $salesman->name;
 
 170   if ( $form->{insertdatefrom} or $form->{insertdateto} ) {
 
 171     push @options, $locale->text('Insert Date');
 
 172     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{insertdatefrom}, 1) if $form->{insertdatefrom};
 
 173     push @options, $locale->text('Bis')  . " " . $locale->date(\%myconfig, $form->{insertdateto},   1) if $form->{insertdateto};
 
 177     'id',        'name',    "$form->{db}number",   'contact',   'phone',    'discount',
 
 178     'fax',       'email',   'taxnumber',           'street',    'zipcode' , 'city',
 
 179     'business',  'payment', 'invnumber', 'ordnumber',           'quonumber', 'salesman',
 
 180     'country',   'gln',     'insertdate',           'pricegroup'
 
 183   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
 
 184   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
 
 185   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
 
 187   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
 
 190     'id'                => { 'text' => $locale->text('ID'), },
 
 191     "$form->{db}number" => { 'text' => $locale->text('Number'), },
 
 192     'name'              => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
 
 193     'contact'           => { 'text' => $locale->text('Contact'), },
 
 194     'phone'             => { 'text' => $locale->text('Phone'), },
 
 195     'fax'               => { 'text' => $locale->text('Fax'), },
 
 196     'email'             => { 'text' => $locale->text('E-mail'), },
 
 197     'cc'                => { 'text' => $locale->text('Cc'), },
 
 198     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
 
 199     'business'          => { 'text' => $locale->text('Type of Business'), },
 
 200     'invnumber'         => { 'text' => $locale->text('Invoice'), },
 
 201     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
 
 202     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
 
 203     'street'            => { 'text' => $locale->text('Street'), },
 
 204     'zipcode'           => { 'text' => $locale->text('Zipcode'), },
 
 205     'city'              => { 'text' => $locale->text('City'), },
 
 206     'country'           => { 'text' => $locale->text('Country'), },
 
 207     'gln'               => { 'text' => $locale->text('GLN'), },
 
 208     'salesman'          => { 'text' => $locale->text('Salesman'), },
 
 209     'discount'          => { 'text' => $locale->text('Discount'), },
 
 210     'payment'           => { 'text' => $locale->text('Payment Terms'), },
 
 211     'insertdate'        => { 'text' => $locale->text('Insert Date'), },
 
 212     'pricegroup'        => { 'text' => $locale->text('Pricegroup'), },
 
 216   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
 
 218   my @hidden_variables  = ( qw(
 
 219       db status obsolete name contact email cp_name addr_street addr_zipcode
 
 220       addr_city addr_country addr_gln business_id salesman_id insertdateto insertdatefrom all
 
 221     ), "$form->{db}number",
 
 222     map({ "cvar_$_->{name}" } @searchable_custom_variables),
 
 223     map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
 
 224     map({ "l_$_" } @columns),
 
 227   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
 
 228   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
 
 229   $form->{callback}     = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
 
 232     my $sortdir              = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
 
 233     $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
 
 236   my ($ordertype, $quotationtype, $attachment_basename);
 
 237   if ($form->{IS_CUSTOMER}) {
 
 238     $form->{title}       = $locale->text('Customers');
 
 239     $ordertype           = 'sales_order';
 
 240     $quotationtype       = 'sales_quotation';
 
 241     $attachment_basename = $locale->text('customer_list');
 
 244     $form->{title}       = $locale->text('Vendors');
 
 245     $ordertype           = 'purchase_order';
 
 246     $quotationtype       = 'request_quotation';
 
 247     $attachment_basename = $locale->text('vendor_list');
 
 250   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 252   $report->set_options('top_info_text'         => join("\n", @options),
 
 253                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
 
 254                        'output_format'         => 'HTML',
 
 255                        'title'                 => $form->{title},
 
 256                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
 258   $report->set_options_from_form();
 
 259   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 261   $report->set_columns(%column_defs);
 
 262   $report->set_column_order(@columns);
 
 264   $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
 
 266   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
 268   CVar->add_custom_variables_to_report('module'         => 'CT',
 
 269                                        'trans_id_field' => 'id',
 
 270                                        'configs'        => $cvar_configs,
 
 271                                        'column_defs'    => \%column_defs,
 
 272                                        'data'           => $form->{CT});
 
 276   foreach my $ref (@{ $form->{CT} }) {
 
 277     my $row = { map { $_ => { 'data' => '' } } @columns };
 
 279     if ($ref->{id} ne $previous_id) {
 
 280       $previous_id = $ref->{id};
 
 281       $ref->{discount} = $form->format_amount(\%myconfig, $ref->{discount} * 100.0, 2);
 
 282       map { $row->{$_}->{data} = $ref->{$_} } @columns;
 
 284       $row->{name}->{link}  = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
 
 285       $row->{email}->{link} = 'mailto:' . E($ref->{email});
 
 288     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
 
 289     if ($::instance_conf->get_feature_experimental_order) {
 
 290       if ('oe' eq $ref->{module}) {
 
 291         $base_url             = build_std_url("script=controller.pl", 'action=Order/edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
 
 295     $row->{invnumber}->{link} = $base_url;
 
 296     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
 
 297     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
 
 298     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
 
 299     $row->{$column}->{data}   = $ref->{$column};
 
 301     if (my $number = SL::CTI->sanitize_number(number => $ref->{phone})) {
 
 302       $row->{phone}->{link}       = SL::CTI->call_link(number => $number);
 
 303       $row->{phone}->{link_class} = 'cti_call_action';
 
 306     $report->add_data($row);
 
 309   setup_ct_list_names_action_bar();
 
 310   $report->generate_with_headers();
 
 312   $main::lxdebug->leave_sub();
 
 316   $::lxdebug->enter_sub;
 
 317   $::auth->assert('customer_vendor_edit');
 
 319   $::form->{sortdir} = 1 unless defined $::form->{sortdir};
 
 321   my @contacts     = CT->search_contacts(
 
 322     search_term => $::form->{search_term},
 
 323     filter      => $::form->{filter},
 
 326   my $cvar_configs = CVar->get_configs('module' => 'Contacts');
 
 329     cp_id vcname vcnumber cp_name cp_givenname cp_street cp_zipcode cp_city cp_phone1 cp_phone2 cp_privatphone
 
 330     cp_mobile1 cp_mobile2 cp_fax cp_email cp_privatemail cp_abteilung cp_position cp_birthday cp_gender
 
 333   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
 
 334   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
 
 335   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
 
 337   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
 
 341     @visible_columns = grep { $::form->{l}{$_} } @columns;
 
 342     push @visible_columns, qw(cp_phone1 cp_phone2)   if $::form->{l}{cp_phone};
 
 343     push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
 
 345    @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
 
 349     'cp_id'        => { 'text' => $::locale->text('ID'), },
 
 350     'vcname'       => { 'text' => $::locale->text('Customer/Vendor'), },
 
 351     'vcnumber'     => { 'text' => $::locale->text('Customer/Vendor Number'), },
 
 352     'cp_name'      => { 'text' => $::locale->text('Name'), },
 
 353     'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
 
 354     'cp_street'    => { 'text' => $::locale->text('Street'), },
 
 355     'cp_zipcode'   => { 'text' => $::locale->text('Zipcode'), },
 
 356     'cp_city'      => { 'text' => $::locale->text('City'), },
 
 357     'cp_phone1'    => { 'text' => $::locale->text('Phone1'), },
 
 358     'cp_phone2'    => { 'text' => $::locale->text('Phone2'), },
 
 359     'cp_mobile1'   => { 'text' => $::locale->text('Mobile1'), },
 
 360     'cp_mobile2'   => { 'text' => $::locale->text('Mobile2'), },
 
 361     'cp_email'     => { 'text' => $::locale->text('E-mail'), },
 
 362     'cp_abteilung' => { 'text' => $::locale->text('Department'), },
 
 363     'cp_position'  => { 'text' => $::locale->text('Function/position'), },
 
 364     'cp_birthday'  => { 'text' => $::locale->text('Birthday'), },
 
 365     'cp_gender'    => { 'text' => $::locale->text('Gender'), },
 
 366     'cp_fax'       => { 'text' => $::locale->text('Fax'), },
 
 367     'cp_privatphone' => { 'text' => $::locale->text('Private Phone') },
 
 368     'cp_privatemail' => { 'text' => $::locale->text('Private E-mail') },
 
 372   map { $column_defs{$_}->{visible} = 1 } @visible_columns;
 
 374   my @hidden_variables  = (qw(search_term filter l));
 
 375   my $hide_vars         = { map { $_ => $::form->{$_} } @hidden_variables };
 
 376   my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
 
 377   my $callback          = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
 
 378   $::form->{callback}     = "$callback&sort=" . E($::form->{sort});
 
 380   map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
 
 382   $::form->{title} = $::locale->text('Contacts');
 
 384   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
 
 387   push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
 
 388   for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
 
 389     push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
 
 391   if ($::form->{filter}{status}) {
 
 392     push @options, $::locale->text('Status') . ': ' . (
 
 393       $::form->{filter}{status} =~ /active/   ? $::locale->text('Active')   :
 
 394       $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
 
 395       $::form->{filter}{status} =~ /all/      ? $::locale->text('All')      : ''
 
 400   $report->set_options('top_info_text'       => join("\n", @options),
 
 401                        'output_format'       => 'HTML',
 
 402                        'title'               => $::form->{title},
 
 403                        'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
 
 405   $report->set_options_from_form;
 
 407   $report->set_columns(%column_defs);
 
 408   $report->set_column_order(@columns);
 
 410   $report->set_export_options('list_contacts', @hidden_variables);
 
 412   $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
 
 414   CVar->add_custom_variables_to_report('module'         => 'Contacts',
 
 415                                        'trans_id_field' => 'cp_id',
 
 416                                        'configs'        => $cvar_configs,
 
 417                                        'column_defs'    => \%column_defs,
 
 418                                        'data'           => \@contacts);
 
 421   foreach my $ref (@contacts) {
 
 422     my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
 
 424     $row->{vcname}->{link}   = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
 
 425     $row->{vcnumber}->{link} = $row->{vcname}->{link};
 
 427     for (qw(cp_email cp_privatemail)) {
 
 428       $row->{$_}->{link} = 'mailto:' . E($ref->{$_}) if $ref->{$_};
 
 431     for (qw(cp_phone1 cp_phone2 cp_mobile1)) {
 
 432       next unless my $number = SL::CTI->sanitize_number(number => $ref->{$_});
 
 434       $row->{$_}->{link}       = SL::CTI->call_link(number => $number);
 
 435       $row->{$_}->{link_class} = 'cti_call_action';
 
 438     $report->add_data($row);
 
 441   $report->generate_with_headers();
 
 443   $::lxdebug->leave_sub;
 
 446 sub setup_ct_search_action_bar {
 
 449   for my $bar ($::request->layout->get('actionbar')) {
 
 453         submit    => [ '#form', { action => 'list_names' } ],
 
 454         accesskey => 'enter',
 
 460 sub setup_ct_list_names_action_bar {
 
 463   for my $bar ($::request->layout->get('actionbar')) {
 
 467         submit    => [ '#new_form', { action => 'CustomerVendor/add' } ],
 
 468         accesskey => 'enter',
 
 474 sub setup_ct_search_contact_action_bar {
 
 477   for my $bar ($::request->layout->get('actionbar')) {
 
 481         submit    => [ '#form', { action => 'list_contacts' } ],
 
 482         accesskey => 'enter',
 
 488 sub continue { call_sub($main::form->{nextsub}); }