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::Helper::Flash;
 
  56 use SL::ReportGenerator;
 
  58 require "bin/mozilla/common.pl";
 
  59 require "bin/mozilla/reportgenerator.pl";
 
  67   $main::lxdebug->enter_sub();
 
  69   $main::auth->assert('customer_vendor_edit');
 
  71   my $form     = $main::form;
 
  72   my %myconfig = %main::myconfig;
 
  74   $form->{title}    = "Add";
 
  75   $form->{callback} = "$form->{script}?action=add&db=$form->{db}" unless $form->{callback};
 
  77   CT->populate_drop_down_boxes(\%myconfig, \%$form);
 
  82   $main::lxdebug->leave_sub();
 
  86   $main::lxdebug->enter_sub();
 
  88   $main::auth->assert('customer_vendor_edit');
 
  90   my $form     = $main::form;
 
  91   my $locale   = $main::locale;
 
  93   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
 
  95   $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
 
  96   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
 
  98   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'CT');
 
  99   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
 
 100    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
 
 101                                                                            'include_prefix' => 'l_',
 
 102                                                                            'include_value'  => 'Y');
 
 104   $form->{title}    = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
 
 107   print $form->parse_html_template('ct/search');
 
 109   $main::lxdebug->leave_sub();
 
 113   $::lxdebug->enter_sub;
 
 114   $::auth->assert('customer_vendor_edit');
 
 116   $::form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'Contacts');
 
 117   ($::form->{CUSTOM_VARIABLES_FILTER_CODE},
 
 118    $::form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'    => $::form->{CUSTOM_VARIABLES},
 
 119                                                                            'include_prefix' => 'l.',
 
 120                                                                            'filter_prefix'  => 'filter.',
 
 121                                                                            'include_value'  => 'Y');
 
 123   $::form->{title} = $::locale->text('Search contacts');
 
 125   print $::form->parse_html_template('ct/search_contact');
 
 127   $::lxdebug->leave_sub;
 
 131   $main::lxdebug->enter_sub();
 
 133   $main::auth->assert('customer_vendor_edit');
 
 135   my $form     = $main::form;
 
 136   my %myconfig = %main::myconfig;
 
 137   my $locale   = $main::locale;
 
 139   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
 
 141   report_generator_set_default_sort('name', 1);
 
 143   CT->search(\%myconfig, \%$form);
 
 145   my $cvar_configs = CVar->get_configs('module' => 'CT');
 
 148   if ($form->{status} eq 'all') {
 
 149     push @options, $locale->text('All');
 
 150   } elsif ($form->{status} eq 'orphaned') {
 
 151     push @options, $locale->text('Orphaned');
 
 154   push @options, $locale->text('Name') . " : $form->{name}"                                    if $form->{name};
 
 155   push @options, $locale->text('Contact') . " : $form->{contact}"                              if $form->{contact};
 
 156   push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|                 if $form->{"$form->{db}number"};
 
 157   push @options, $locale->text('E-mail') . " : $form->{email}"                                 if $form->{email};
 
 158   push @options, $locale->text('Contact person (surname)')           . " : $form->{cp_name}"   if $form->{cp_name};
 
 159   push @options, $locale->text('Billing/shipping address (city)')    . " : $form->{addr_city}" if $form->{addr_city};
 
 160   push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{zipcode}"   if $form->{addr_zipcode};
 
 161   push @options, $locale->text('Billing/shipping address (street)')  . " : $form->{street}"    if $form->{addr_street};
 
 162   push @options, $locale->text('Billing/shipping address (country)') . " : $form->{country}"   if $form->{addr_country};
 
 164   if ($form->{business_id}) {
 
 165     my $business = SL::DB::Manager::Business->find_by(id => $form->{business_id});
 
 167       my $label = $form->{IS_CUSTOMER} ? $::locale->text('Customer type') : $::locale->text('Vendor type');
 
 168       push @options, $label . " : " . $business->description;
 
 173     'id',        'name',      "$form->{db}number",   'contact',   'phone',
 
 174     'fax',       'email',     'taxnumber',           'street',    'zipcode' , 'city',
 
 175     'business',  'invnumber', 'ordnumber',           'quonumber', 'salesman', 'country'
 
 178   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
 
 179   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
 
 180   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
 
 182   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
 
 185     'id'                => { 'text' => $locale->text('ID'), },
 
 186     "$form->{db}number" => { 'text' => $locale->text('Number'), },
 
 187     'name'              => { 'text' => $form->{IS_CUSTOMER} ? $::locale->text('Customer Name') : $::locale->text('Vendor Name'), },
 
 188     'contact'           => { 'text' => $locale->text('Contact'), },
 
 189     'phone'             => { 'text' => $locale->text('Phone'), },
 
 190     'fax'               => { 'text' => $locale->text('Fax'), },
 
 191     'email'             => { 'text' => $locale->text('E-mail'), },
 
 192     'cc'                => { 'text' => $locale->text('Cc'), },
 
 193     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
 
 194     'business'          => { 'text' => $locale->text('Type of Business'), },
 
 195     'invnumber'         => { 'text' => $locale->text('Invoice'), },
 
 196     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
 
 197     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
 
 198     'street'            => { 'text' => $locale->text('Street'), },
 
 199     'zipcode'           => { 'text' => $locale->text('Zipcode'), },
 
 200     'city'              => { 'text' => $locale->text('City'), },
 
 201     'country'           => { 'text' => $locale->text('Country'), },
 
 202     'salesman'          => { 'text' => $locale->text('Salesman'), },
 
 206   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
 
 208   my @hidden_variables  = ( qw(
 
 209       db status obsolete name contact email cp_name addr_street addr_zipcode
 
 210       addr_city addr_country business_id
 
 211     ), "$form->{db}number",
 
 212     map({ "cvar_$_->{name}" } @searchable_custom_variables),
 
 213     map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
 
 214     map({ "l_$_" } @columns),
 
 217   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
 
 218   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
 
 219   $form->{callback}     = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
 
 222     my $sortdir              = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
 
 223     $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
 
 226   my ($ordertype, $quotationtype, $attachment_basename);
 
 227   if ($form->{IS_CUSTOMER}) {
 
 228     $form->{title}       = $locale->text('Customers');
 
 229     $ordertype           = 'sales_order';
 
 230     $quotationtype       = 'sales_quotation';
 
 231     $attachment_basename = $locale->text('customer_list');
 
 234     $form->{title}       = $locale->text('Vendors');
 
 235     $ordertype           = 'purchase_order';
 
 236     $quotationtype       = 'request_quotation';
 
 237     $attachment_basename = $locale->text('vendor_list');
 
 240   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 242   $report->set_options('top_info_text'         => join("\n", @options),
 
 243                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
 
 244                        'output_format'         => 'HTML',
 
 245                        'title'                 => $form->{title},
 
 246                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
 248   $report->set_options_from_form();
 
 249   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 251   $report->set_columns(%column_defs);
 
 252   $report->set_column_order(@columns);
 
 254   $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
 
 256   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
 258   CVar->add_custom_variables_to_report('module'         => 'CT',
 
 259                                        'trans_id_field' => 'id',
 
 260                                        'configs'        => $cvar_configs,
 
 261                                        'column_defs'    => \%column_defs,
 
 262                                        'data'           => $form->{CT});
 
 266   foreach my $ref (@{ $form->{CT} }) {
 
 267     my $row = { map { $_ => { 'data' => '' } } @columns };
 
 269     if ($ref->{id} ne $previous_id) {
 
 270       $previous_id = $ref->{id};
 
 271       map { $row->{$_}->{data} = $ref->{$_} } @columns;
 
 273       $row->{name}->{link}  = build_std_url('script=controller.pl', 'action=CustomerVendor/edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
 
 274       $row->{email}->{link} = 'mailto:' . E($ref->{email});
 
 277     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
 
 278     $row->{invnumber}->{link} = $base_url;
 
 279     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
 
 280     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
 
 281     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
 
 282     $row->{$column}->{data}   = $ref->{$column};
 
 284     $report->add_data($row);
 
 287   $report->generate_with_headers();
 
 289   $main::lxdebug->leave_sub();
 
 293   $::lxdebug->enter_sub;
 
 294   $::auth->assert('customer_vendor_edit');
 
 296   $::form->{sortdir} = 1 unless defined $::form->{sortdir};
 
 298   my @contacts     = CT->search_contacts(
 
 299     search_term => $::form->{search_term},
 
 300     filter      => $::form->{filter},
 
 303   my $cvar_configs = CVar->get_configs('module' => 'Contacts');
 
 306     cp_id vcname vcnumber cp_name cp_givenname cp_street cp_zipcode cp_city cp_phone1 cp_phone2 cp_privatphone
 
 307     cp_mobile1 cp_mobile2 cp_fax cp_email cp_privatemail cp_abteilung cp_position cp_birthday cp_gender
 
 310   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
 
 311   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
 
 312   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
 
 314   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
 
 318     @visible_columns = grep { $::form->{l}{$_} } @columns;
 
 319     push @visible_columns, qw(cp_phone1 cp_phone2)   if $::form->{l}{cp_phone};
 
 320     push @visible_columns, qw(cp_mobile1 cp_mobile2) if $::form->{l}{cp_mobile};
 
 322    @visible_columns = qw(vcname vcnumber cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email);
 
 326     'cp_id'        => { 'text' => $::locale->text('ID'), },
 
 327     'vcname'       => { 'text' => $::locale->text('Customer/Vendor'), },
 
 328     'vcnumber'     => { 'text' => $::locale->text('Customer/Vendor Number'), },
 
 329     'cp_name'      => { 'text' => $::locale->text('Name'), },
 
 330     'cp_givenname' => { 'text' => $::locale->text('Given Name'), },
 
 331     'cp_street'    => { 'text' => $::locale->text('Street'), },
 
 332     'cp_zipcode'   => { 'text' => $::locale->text('Zipcode'), },
 
 333     'cp_city'      => { 'text' => $::locale->text('City'), },
 
 334     'cp_phone1'    => { 'text' => $::locale->text('Phone1'), },
 
 335     'cp_phone2'    => { 'text' => $::locale->text('Phone2'), },
 
 336     'cp_mobile1'   => { 'text' => $::locale->text('Mobile1'), },
 
 337     'cp_mobile2'   => { 'text' => $::locale->text('Mobile2'), },
 
 338     'cp_email'     => { 'text' => $::locale->text('E-mail'), },
 
 339     'cp_abteilung' => { 'text' => $::locale->text('Department'), },
 
 340     'cp_position'  => { 'text' => $::locale->text('Function/position'), },
 
 341     'cp_birthday'  => { 'text' => $::locale->text('Birthday'), },
 
 342     'cp_gender'    => { 'text' => $::locale->text('Gender'), },
 
 343     'cp_fax'       => { 'text' => $::locale->text('Fax'), },
 
 344     'cp_privatphone' => { 'text' => $::locale->text('Private Phone') },
 
 345     'cp_privatemail' => { 'text' => $::locale->text('Private E-mail') },
 
 349   map { $column_defs{$_}->{visible} = 1 } @visible_columns;
 
 351   my @hidden_variables  = (qw(search_term filter l));
 
 352   my $hide_vars         = { map { $_ => $::form->{$_} } @hidden_variables };
 
 353   my @hidden_nondefault = grep({ $::form->{$_} } @hidden_variables);
 
 354   my $callback          = build_std_url('action=list_contacts', join '&', map { E($_->[0]) . '=' . E($_->[1]) } @{ flatten($hide_vars) });
 
 355   $::form->{callback}     = "$callback&sort=" . E($::form->{sort});
 
 357   map { $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=" . ($::form->{sort} eq $_ ? 1 - $::form->{sortdir} : $::form->{sortdir}) } @columns;
 
 359   $::form->{title} = $::locale->text('Contacts');
 
 361   my $report     = SL::ReportGenerator->new(\%::myconfig, $::form);
 
 364   push @options, $::locale->text('Search term') . ': ' . $::form->{search_term} if $::form->{search_term};
 
 365   for (qw(cp_name cp_givenname cp_title cp_email cp_abteilung cp_project)) {
 
 366     push @options, $column_defs{$_}{text} . ': ' . $::form->{filter}{$_} if $::form->{filter}{$_};
 
 368   if ($::form->{filter}{status}) {
 
 369     push @options, $::locale->text('Status') . ': ' . (
 
 370       $::form->{filter}{status} =~ /active/   ? $::locale->text('Active')   :
 
 371       $::form->{filter}{status} =~ /orphaned/ ? $::locale->text('Orphaned') :
 
 372       $::form->{filter}{status} =~ /all/      ? $::locale->text('All')      : ''
 
 377   $report->set_options('top_info_text'       => join("\n", @options),
 
 378                        'output_format'       => 'HTML',
 
 379                        'title'               => $::form->{title},
 
 380                        'attachment_basename' => $::locale->text('contact_list') . strftime('_%Y%m%d', localtime time),
 
 382   $report->set_options_from_form;
 
 384   $report->set_columns(%column_defs);
 
 385   $report->set_column_order(@columns);
 
 387   $report->set_export_options('list_contacts', @hidden_variables);
 
 389   $report->set_sort_indicator($::form->{sort}, $::form->{sortdir});
 
 391   CVar->add_custom_variables_to_report('module'         => 'Contacts',
 
 392                                        'trans_id_field' => 'cp_id',
 
 393                                        'configs'        => $cvar_configs,
 
 394                                        'column_defs'    => \%column_defs,
 
 395                                        'data'           => \@contacts);
 
 398   foreach my $ref (@contacts) {
 
 399     my $row = { map { $_ => { 'data' => $ref->{$_} } } @columns };
 
 401     $row->{vcname}->{link}   = build_std_url('action=edit', 'id=' . E($ref->{vcid}), 'db=' . E($ref->{db}), 'callback', @hidden_nondefault);
 
 402     $row->{vcnumber}->{link} = $row->{vcname}->{link};
 
 404     for (qw(cp_email cp_privatemail)) {
 
 405       $row->{$_}->{link} = 'mailto:' . E($ref->{$_}) if $ref->{$_};
 
 408     $report->add_data($row);
 
 411   $report->generate_with_headers;
 
 413   $::lxdebug->leave_sub;
 
 417   $main::lxdebug->enter_sub();
 
 419   $main::auth->assert('customer_vendor_edit');
 
 421   my $form     = $main::form;
 
 422   my %myconfig = %main::myconfig;
 
 424   # show history button
 
 425   $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
 
 426   #/show hhistory button
 
 428   CT->get_tuple(\%myconfig, \%$form);
 
 429   CT->populate_drop_down_boxes(\%myconfig, \%$form);
 
 431   $form->{title} = "Edit";
 
 434   $form->{discount} *= 100;
 
 436   $form->{homepage} = 'http://' . $form->{homepage} unless ((!$form->{homepage}) || $form->{homepage} =~ m|^https?://|);
 
 441   $main::lxdebug->leave_sub();
 
 446   join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' '
 
 449 sub _contacts_label {
 
 450   join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname};
 
 454   $main::lxdebug->enter_sub();
 
 456   $main::auth->assert('customer_vendor_edit');
 
 458   my $form     = $main::form;
 
 459   my %myconfig = %main::myconfig;
 
 460   my $locale   = $main::locale;
 
 462   $form->get_lists(taxzones   => "ALL_TAXZONES",
 
 463                    currencies => "ALL_CURRENCIES");
 
 464   $form->get_pricegroup(\%myconfig, { all => 1 });
 
 466   $form->get_lists(customers => { key => "ALL_SALESMAN_CUSTOMERS", business_is_salesman => 1 }) if $::instance_conf->get_vertreter;
 
 467   $form->{ALL_EMPLOYEES}          = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{FU_created_for_user},  deleted => 0 ] ]);
 
 468   $form->{ALL_SALESMEN}           = SL::DB::Manager::Employee->get_all(query => [ or => [ id => $::form->{salesman_id},  deleted => 0 ] ]);
 
 469   $form->{USER}                   = SL::DB::Manager::Employee->current;
 
 471   $form->{taxincluded}    = ($form->{taxincluded}) ? "checked" : "";
 
 472   $form->{is_customer}    = $form->{db}     eq 'customer';
 
 473   $form->{shipto_label}   = \&_shipto_label;
 
 474   $form->{contacts_label} = \&_contacts_label;
 
 475   $form->{taxzone_id}     = 0                                                               if !$form->{id};
 
 476   $form->{SHIPTO_ALL}     = [ +{ shipto_id => '0', shiptoname => $::locale->text('All') }, @{ $form->{SHIPTO} } ];
 
 478   $form->{title} = $form->{title_save}
 
 479                 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
 
 481   CT->query_titles_and_greetings(\%myconfig, \%$form);
 
 482   map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(COMPANY_GREETINGS);
 
 484   $form->{NOTES} ||= [ ];
 
 486   if (!$form->{'language_id'}) {
 
 487     my $l_id = SL::DB::Default->get->{'language_id'};
 
 489       $form->{'default_language_id'} = $l_id;
 
 493   if (!$form->{'id'}) {
 
 494     $form->{'currency'} = $form->get_default_currency(\%myconfig);
 
 496     $form->{currency} = $form->{curr};
 
 499   $::form->{CUSTOM_VARIABLES} = { };
 
 500   my %specs = ( CT       => { field => 'id',    name_prefix => '',   },
 
 501                 Contacts => { field => 'cp_id', name_prefix => 'cp', },
 
 504   for my $module (keys %specs) {
 
 505     my $spec = $specs{$module};
 
 507     $::form->{CUSTOM_VARIABLES}->{$module} = CVar->get_custom_variables(module => $module, trans_id => $::form->{ $spec->{field} });
 
 508     CVar->render_inputs(variables => $::form->{CUSTOM_VARIABLES}->{$module}, name_prefix => $spec->{name_prefix})
 
 509       if scalar @{ $::form->{CUSTOM_VARIABLES}->{$module} };
 
 513   print $form->parse_html_template('ct/form_header');
 
 515   $main::lxdebug->leave_sub();
 
 519   $main::lxdebug->enter_sub();
 
 521   $main::auth->assert('customer_vendor_edit');
 
 523   my $form     = $main::form;
 
 525   print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
 
 526                                                        is_customer => $form->{db}     eq 'customer' });
 
 527   $main::lxdebug->leave_sub();
 
 531   $main::auth->assert('customer_vendor_edit');
 
 533   $::form->isblank("name", $::locale->text("Name missing!"));
 
 535   if ($::form->{new_salesman_id} && $::instance_conf->get_vertreter) {
 
 536     $::form->{salesman_id} = $::form->{new_salesman_id};
 
 537     delete $::form->{new_salesman_id};
 
 540   my $res = $::form->{db} eq 'customer' ? CT->save_customer(\%::myconfig, $::form) : CT->save_vendor(\%::myconfig, $::form);
 
 543     if ($::form->{"db"} eq "customer") {
 
 544       $::form->error($::locale->text('This customer number is already in use.'));
 
 546       $::form->error($::locale->text('This vendor number is already in use.'));
 
 551 sub add_transaction {
 
 552   $main::lxdebug->enter_sub();
 
 554   $main::auth->assert('customer_vendor_edit & ' .
 
 555                 '(general_ledger         | invoice_edit         | vendor_invoice_edit | ' .
 
 556                 ' request_quotation_edit | sales_quotation_edit | sales_order_edit    | purchase_order_edit)');
 
 558   my $form     = $main::form;
 
 559   my %myconfig = %main::myconfig;
 
 560   my $locale   = $main::locale;
 
 562 #  # saving the history
 
 563 #  if(!exists $form->{addition}) {
 
 564 #    $form->{addition} = "ADD TRANSACTION";
 
 565 #    $form->save_history;
 
 567 #  # /saving the history
 
 571   $form->{callback} = $form->escape($form->{callback}, 1);
 
 572   my $name = $form->escape("$form->{name}", 1);
 
 575     "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
 
 578   $main::lxdebug->leave_sub();
 
 581 sub save_and_ap_transaction {
 
 582   $main::lxdebug->enter_sub();
 
 584   $main::auth->assert('customer_vendor_edit & general_ledger');
 
 586   my $form     = $main::form;
 
 587   my %myconfig = %main::myconfig;
 
 589   $form->{script} = "ap.pl";
 
 591   if(!exists $form->{addition}) {
 
 592     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 593     $form->{addition} = "SAVED";
 
 596   # /saving the history
 
 598   $main::lxdebug->leave_sub();
 
 601 sub save_and_ar_transaction {
 
 602   $main::lxdebug->enter_sub();
 
 604   $main::auth->assert('customer_vendor_edit & general_ledger');
 
 606   my $form     = $main::form;
 
 607   my %myconfig = %main::myconfig;
 
 609   $form->{script} = "ar.pl";
 
 611   if(!exists $form->{addition}) {
 
 612     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 613     $form->{addition} = "SAVED";
 
 616   # /saving the history
 
 618   $main::lxdebug->leave_sub();
 
 621 sub save_and_invoice {
 
 622   $main::lxdebug->enter_sub();
 
 624   my $form     = $main::form;
 
 625   my %myconfig = %main::myconfig;
 
 627   if ($form->{db} eq 'customer') {
 
 628     $main::auth->assert('customer_vendor_edit & invoice_edit');
 
 630     $main::auth->assert('customer_vendor_edit & vendor_invoice_edit');
 
 633   $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
 
 634   $form->{type} = "invoice";
 
 636   if(!exists $form->{addition}) {
 
 637     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
 
 638     $form->{addition} = "SAVED";
 
 641   # /saving the history
 
 643   $main::lxdebug->leave_sub();
 
 647   $main::lxdebug->enter_sub();
 
 649   $main::auth->assert('customer_vendor_edit & request_quotation_edit');
 
 651   my $form     = $main::form;
 
 652   my %myconfig = %main::myconfig;
 
 654   $form->{script} = "oe.pl";
 
 655   $form->{type}   = "request_quotation";
 
 657   if(!exists $form->{addition}) {
 
 658     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
 659     $form->{addition} = "SAVED";
 
 662   # /saving the history
 
 664   $main::lxdebug->leave_sub();
 
 667 sub save_and_quotation {
 
 668   $main::lxdebug->enter_sub();
 
 670   $main::auth->assert('customer_vendor_edit & sales_quotation_edit');
 
 672   my $form     = $main::form;
 
 673   my %myconfig = %main::myconfig;
 
 675   $form->{script} = "oe.pl";
 
 676   $form->{type}   = "sales_quotation";
 
 678   if(!exists $form->{addition}) {
 
 679     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
 680     $form->{addition} = "SAVED";
 
 683   # /saving the history
 
 685   $main::lxdebug->leave_sub();
 
 689   $main::lxdebug->enter_sub();
 
 691   my $form     = $main::form;
 
 692   my %myconfig = %main::myconfig;
 
 694   if ($form->{db} eq 'customer') {
 
 695     $main::auth->assert('customer_vendor_edit & sales_order_edit');
 
 697     $main::auth->assert('customer_vendor_edit & purchase_order_edit');
 
 700   $form->{script} = "oe.pl";
 
 702     ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
 
 704   if(!exists $form->{addition}) {
 
 705     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
 706     $form->{addition} = "SAVED";
 
 709   # /saving the history
 
 711   $main::lxdebug->leave_sub();
 
 715   $main::lxdebug->enter_sub();
 
 717   $main::auth->assert('customer_vendor_edit');
 
 719   my $form     = $main::form;
 
 720   my %myconfig = %main::myconfig;
 
 721   my $locale   = $main::locale;
 
 723   my $msg = ucfirst $form->{db};
 
 729   if(!exists $form->{addition}) {
 
 730     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
 
 731     $form->{addition} = "SAVED";
 
 734   # /saving the history
 
 735   $form->redirect($locale->text($msg));
 
 737   $main::lxdebug->leave_sub();
 
 741   $main::lxdebug->enter_sub();
 
 743   $main::auth->assert('customer_vendor_edit');
 
 745   my $form     = $main::form;
 
 746   my %myconfig = %main::myconfig;
 
 747   my $locale   = $main::locale;
 
 749   my $msg = ucfirst $form->{db};
 
 755   if(!exists $form->{addition}) {
 
 756     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
 
 757     $form->{addition} = "SAVED";
 
 760   # /saving the history
 
 763   $main::lxdebug->leave_sub();
 
 768   $main::lxdebug->enter_sub();
 
 770   $main::auth->assert('customer_vendor_edit');
 
 772   my $form     = $main::form;
 
 773   my %myconfig = %main::myconfig;
 
 774   my $locale   = $main::locale;
 
 776   CT->delete(\%myconfig, \%$form);
 
 778   my $msg = ucfirst $form->{db};
 
 781   if(!exists $form->{addition}) {
 
 782     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
 
 783     $form->{addition} = "DELETED";
 
 786   # /saving the history
 
 787   $form->redirect($locale->text($msg));
 
 789   $main::lxdebug->leave_sub();
 
 793   $main::lxdebug->enter_sub();
 
 795   $main::auth->assert('customer_vendor_edit');
 
 797   my $form     = $main::form;
 
 802   $main::lxdebug->leave_sub();
 
 806   $main::lxdebug->enter_sub();
 
 808   $main::auth->assert('customer_vendor_edit');
 
 810   my $form     = $main::form;
 
 813   $main::lxdebug->leave_sub();
 
 817   $main::lxdebug->enter_sub();
 
 819   $main::auth->assert('customer_vendor_edit');
 
 821   CT->populate_drop_down_boxes(\%::myconfig, $::form);
 
 822   CT->query_titles_and_greetings(\%::myconfig, $::form);
 
 823   CT->get_contact(\%::myconfig, $::form) if $::form->{cp_id};
 
 825   $::form->{CUSTOM_VARIABLES}{Contacts} = CVar->get_custom_variables(module => 'Contacts', trans_id => $::form->{cp_id});
 
 826   CVar->render_inputs(variables => $::form->{CUSTOM_VARIABLES}{Contacts}, name_prefix => 'cp')
 
 827     if scalar @{ $::form->{CUSTOM_VARIABLES}->{Contacts} };
 
 829   $::form->{contacts_label} = \&_contacts_label;
 
 831   print $::form->ajax_response_header(), $::form->parse_html_template('ct/_contact');
 
 833   $main::lxdebug->leave_sub();
 
 837   $main::lxdebug->enter_sub();
 
 839   $main::auth->assert('customer_vendor_edit');
 
 841   CT->populate_drop_down_boxes(\%::myconfig, $::form);
 
 842   CT->get_shipto(\%::myconfig, $::form) if $::form->{shipto_id};
 
 844   $::form->{shipto_label} = \&_shipto_label;
 
 846   print $::form->ajax_response_header(), $::form->parse_html_template('ct/_shipto');
 
 848   $main::lxdebug->leave_sub();
 
 852   $::lxdebug->enter_sub;
 
 854   $::auth->assert('customer_vendor_edit');
 
 855   $::auth->assert('sales_all_edit');
 
 857   CT->get_delivery(\%::myconfig, $::form );
 
 859   print $::form->ajax_response_header,
 
 860         $::form->parse_html_template('ct/get_delivery', {
 
 861           is_customer =>  $::form->{db} eq 'customer',
 
 864   $::lxdebug->leave_sub;
 
 868   $::lxdebug->enter_sub;
 
 869   $::auth->assert('customer_vendor_edit');
 
 871   if (!$::form->{shipto_id}) {
 
 872     flash('error', $::locale->text('No shipto selected to delete'));
 
 875     CT->get_shipto(\%::myconfig, $::form);
 
 877     my $shipto = SL::DB::Manager::Shipto->find_by(shipto_id => $::form->{shipto_id});
 
 880       $shipto->detach->save;
 
 881       flash('info', $::locale->text('Shipto is in use and was flagged invalid.'));
 
 884       flash('info', $::locale->text('Shipto deleted.'));
 
 886     delete $::form->{$_} for grep /^shipto/, keys %$::form;
 
 891   $::lxdebug->leave_sub;
 
 895   $::lxdebug->enter_sub;
 
 896   $::auth->assert('customer_vendor_edit');
 
 898   if (!$::form->{cp_id}) {
 
 899     flash('error', $::locale->text('No contact selected to delete'));
 
 902     CT->get_contact(\%::myconfig, $::form);
 
 904     my $contact = SL::DB::Manager::Contact->find_by(cp_id => $::form->{cp_id});
 
 906     if ($contact->used) {
 
 907       $contact->detach->save;
 
 908       flash('info', $::locale->text('Contact is in use and was flagged invalid.'));
 
 911       flash('info', $::locale->text('Contact deleted.'));
 
 913     delete $::form->{$_} for grep /^cp_/, keys %$::form;
 
 918   $::lxdebug->leave_sub;
 
 921 sub ajax_autocomplete {
 
 922   $main::lxdebug->enter_sub();
 
 924   my $form     = $main::form;
 
 925   my %myconfig = %main::myconfig;
 
 927   $form->{column}          = 'name'     unless $form->{column} =~ /^name$/;
 
 928   $form->{vc}              = 'customer' unless $form->{vc} =~ /^customer|vendor$/;
 
 929   $form->{db}              = $form->{vc}; # CT expects this
 
 930   $form->{$form->{column}} = $form->{q}           || '';
 
 931   $form->{limit}           = ($form->{limit} * 1) || 10;
 
 932   $form->{searchitems}   ||= '';
 
 934   CT->search(\%myconfig, $form);
 
 936   print $form->ajax_response_header(),
 
 937         $form->parse_html_template('ct/ajax_autocomplete');
 
 939   $main::lxdebug->leave_sub();
 
 942 sub continue { call_sub($main::form->{nextsub}); }