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);
 
  52 use SL::Request qw(flatten);
 
  55 use SL::DB::DeliveryTerm;
 
  56 use SL::Helper::Flash;
 
  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   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
 
  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');
 
  87   $form->{title}    = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
 
  90   print $form->parse_html_template('ct/search');
 
  92   $main::lxdebug->leave_sub();
 
  96   $::lxdebug->enter_sub;
 
  97   $::auth->assert('customer_vendor_edit');
 
  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');
 
 106   $::form->{title} = $::locale->text('Search contacts');
 
 108   print $::form->parse_html_template('ct/search_contact');
 
 110   $::lxdebug->leave_sub;
 
 114   $main::lxdebug->enter_sub();
 
 116   $main::auth->assert('customer_vendor_edit');
 
 118   my $form     = $main::form;
 
 119   my %myconfig = %main::myconfig;
 
 120   my $locale   = $main::locale;
 
 122   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
 
 124   report_generator_set_default_sort('name', 1);
 
 126   CT->search(\%myconfig, \%$form);
 
 128   my $cvar_configs = CVar->get_configs('module' => 'CT');
 
 131   if ($form->{status} eq 'all') {
 
 132     push @options, $locale->text('All');
 
 133   } elsif ($form->{status} eq 'orphaned') {
 
 134     push @options, $locale->text('Orphaned');
 
 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};
 
 147   if ($form->{business_id}) {
 
 148     my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
 
 150       my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
 
 151       push @options, $label . " : " . $business->description;
 
 156     'id',        'name',      "$form->{db}number",   'contact',   'phone',    'discount',
 
 157     'fax',       'email',     'taxnumber',           'street',    'zipcode' , 'city',
 
 158     'business',  'invnumber', 'ordnumber',           'quonumber', 'salesman', 'country'
 
 161   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
 
 162   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
 
 163   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
 
 165   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
 
 168     'id'                => { 'text' => $locale->text('ID'), },
 
 169     "$form->{db}number" => { 'text' => $locale->text('Number'), },
 
 170     'name'              => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
 
 171     'contact'           => { 'text' => $locale->text('Contact'), },
 
 172     'phone'             => { 'text' => $locale->text('Phone'), },
 
 173     'fax'               => { 'text' => $locale->text('Fax'), },
 
 174     'email'             => { 'text' => $locale->text('E-mail'), },
 
 175     'cc'                => { 'text' => $locale->text('Cc'), },
 
 176     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
 
 177     'business'          => { 'text' => $locale->text('Type of Business'), },
 
 178     'invnumber'         => { 'text' => $locale->text('Invoice'), },
 
 179     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
 
 180     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
 
 181     'street'            => { 'text' => $locale->text('Street'), },
 
 182     'zipcode'           => { 'text' => $locale->text('Zipcode'), },
 
 183     'city'              => { 'text' => $locale->text('City'), },
 
 184     'country'           => { 'text' => $locale->text('Country'), },
 
 185     'salesman'          => { 'text' => $locale->text('Salesman'), },
 
 186     'discount'          => { 'text' => $locale->text('Discount'), },
 
 190   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
 
 192   my @hidden_variables  = ( qw(
 
 193       db status obsolete name contact email cp_name addr_street addr_zipcode
 
 194       addr_city addr_country business_id
 
 195     ), "$form->{db}number",
 
 196     map({ "cvar_$_->{name}" } @searchable_custom_variables),
 
 197     map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
 
 198     map({ "l_$_" } @columns),
 
 201   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
 
 202   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
 
 203   $form->{callback}     = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
 
 206     my $sortdir              = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
 
 207     $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
 
 210   my ($ordertype, $quotationtype, $attachment_basename);
 
 211   if ($form->{IS_CUSTOMER}) {
 
 212     $form->{title}       = $locale->text('Customers');
 
 213     $ordertype           = 'sales_order';
 
 214     $quotationtype       = 'sales_quotation';
 
 215     $attachment_basename = $locale->text('customer_list');
 
 218     $form->{title}       = $locale->text('Vendors');
 
 219     $ordertype           = 'purchase_order';
 
 220     $quotationtype       = 'request_quotation';
 
 221     $attachment_basename = $locale->text('vendor_list');
 
 224   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 226   $report->set_options('top_info_text'         => join("\n", @options),
 
 227                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
 
 228                        'output_format'         => 'HTML',
 
 229                        'title'                 => $form->{title},
 
 230                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
 232   $report->set_options_from_form();
 
 233   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 235   $report->set_columns(%column_defs);
 
 236   $report->set_column_order(@columns);
 
 238   $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
 
 240   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
 242   CVar->add_custom_variables_to_report('module'         => 'CT',
 
 243                                        'trans_id_field' => 'id',
 
 244                                        'configs'        => $cvar_configs,
 
 245                                        'column_defs'    => \%column_defs,
 
 246                                        'data'           => $form->{CT});
 
 250   foreach my $ref (@{ $form->{CT} }) {
 
 251     my $row = { map { $_ => { 'data' => '' } } @columns };
 
 253     if ($ref->{id} ne $previous_id) {
 
 254       $previous_id = $ref->{id};
 
 255       $ref->{discount} = $form->format_amount(\%myconfig, $ref->{discount} * 100.0, 2);
 
 256       map { $row->{$_}->{data} = $ref->{$_} } @columns;
 
 258       $row->{name}->{link}  = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
 
 259       $row->{email}->{link} = 'mailto:' . E($ref->{email});
 
 262     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
 
 263     $row->{invnumber}->{link} = $base_url;
 
 264     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
 
 265     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
 
 266     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
 
 267     $row->{$column}->{data}   = $ref->{$column};
 
 269     $report->add_data($row);
 
 272   $report->generate_with_headers();
 
 274   $main::lxdebug->leave_sub();
 
 278   $::lxdebug->enter_sub;
 
 279   $::auth->assert('customer_vendor_edit');
 
 281   $::form->{sortdir} = 1 unless defined $::form->{sortdir};
 
 283   my @contacts     = CT->search_contacts(
 
 284     search_term => $::form->{search_term},
 
 285     filter      => $::form->{filter},
 
 288   my $cvar_configs = CVar->get_configs('module' => 'Contacts');
 
 291     cp_id vcname vcnumber cp_name cp_givenname cp_street cp_zipcode cp_city cp_phone1 cp_phone2 cp_privatphone
 
 292     cp_mobile1 cp_mobile2 cp_fax cp_email cp_privatemail cp_abteilung cp_position cp_birthday cp_gender
 
 295   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
 
 296   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
 
 297   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
 
 299   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
 
 303     @visible_columns = grep { $::form->{l}{$_} } @columns;
 
 304     push @visible_columns, qw(cp_phone1 cp_phone2)   if $::form->{l}{cp_phone};
 
 305     push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
 
 307    @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
 
 311     'cp_id'        => { 'text' => $::locale->text('ID'), },
 
 312     'vcname'       => { 'text' => $::locale->text('Customer/Vendor'), },
 
 313     'vcnumber'     => { 'text' => $::locale->text('Customer/Vendor Number'), },
 
 314     'cp_name'      => { 'text' => $::locale->text('Name'), },
 
 315     'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
 
 316     'cp_street'    => { 'text' => $::locale->text('Street'), },
 
 317     'cp_zipcode'   => { 'text' => $::locale->text('Zipcode'), },
 
 318     'cp_city'      => { 'text' => $::locale->text('City'), },
 
 319     'cp_phone1'    => { 'text' => $::locale->text('Phone1'), },
 
 320     'cp_phone2'    => { 'text' => $::locale->text('Phone2'), },
 
 321     'cp_mobile1'   => { 'text' => $::locale->text('Mobile1'), },
 
 322     'cp_mobile2'   => { 'text' => $::locale->text('Mobile2'), },
 
 323     'cp_email'     => { 'text' => $::locale->text('E-mail'), },
 
 324     'cp_abteilung' => { 'text' => $::locale->text('Department'), },
 
 325     'cp_position'  => { 'text' => $::locale->text('Function/position'), },
 
 326     'cp_birthday'  => { 'text' => $::locale->text('Birthday'), },
 
 327     'cp_gender'    => { 'text' => $::locale->text('Gender'), },
 
 328     'cp_fax'       => { 'text' => $::locale->text('Fax'), },
 
 329     'cp_privatphone' => { 'text' => $::locale->text('Private Phone') },
 
 330     'cp_privatemail' => { 'text' => $::locale->text('Private E-mail') },
 
 334   map { $column_defs{$_}->{visible} = 1 } @visible_columns;
 
 336   my @hidden_variables  = (qw(search_term filter l));
 
 337   my $hide_vars         = { map { $_ => $::form->{$_} } @hidden_variables };
 
 338   my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
 
 339   my $callback          = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
 
 340   $::form->{callback}     = "$callback&sort=" . E($::form->{sort});
 
 342   map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
 
 344   $::form->{title} = $::locale->text('Contacts');
 
 346   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
 
 349   push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
 
 350   for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
 
 351     push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
 
 353   if ($::form->{filter}{status}) {
 
 354     push @options, $::locale->text('Status') . ': ' . (
 
 355       $::form->{filter}{status} =~ /active/   ? $::locale->text('Active')   :
 
 356       $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
 
 357       $::form->{filter}{status} =~ /all/      ? $::locale->text('All')      : ''
 
 362   $report->set_options('top_info_text'       => join("\n", @options),
 
 363                        'output_format'       => 'HTML',
 
 364                        'title'               => $::form->{title},
 
 365                        'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
 
 367   $report->set_options_from_form;
 
 369   $report->set_columns(%column_defs);
 
 370   $report->set_column_order(@columns);
 
 372   $report->set_export_options('list_contacts', @hidden_variables);
 
 374   $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
 
 376   CVar->add_custom_variables_to_report('module'         => 'Contacts',
 
 377                                        'trans_id_field' => 'cp_id',
 
 378                                        'configs'        => $cvar_configs,
 
 379                                        'column_defs'    => \%column_defs,
 
 380                                        'data'           => \@contacts);
 
 383   foreach my $ref (@contacts) {
 
 384     my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
 
 386     $row->{vcname}->{link}   = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
 
 387     $row->{vcnumber}->{link} = $row->{vcname}->{link};
 
 389     for (qw(cp_email cp_privatemail)) {
 
 390       $row->{$_}->{link} = 'mailto:' . E($ref->{$_}) if $ref->{$_};
 
 393     $report->add_data($row);
 
 396   $report->generate_with_headers;
 
 398   $::lxdebug->leave_sub;
 
 401 sub continue { call_sub($main::form->{nextsub}); }