1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  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 # backend code for customers and vendors
 
  34 #   DS. 2000-07-04  Created
 
  36 #======================================================================
 
  52   $main::lxdebug->enter_sub();
 
  54   my ( $self, $myconfig, $form ) = @_;
 
  56   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
  58   my $dbh   = $form->dbconnect($myconfig);
 
  60     qq|SELECT ct.*, b.id AS business, cp.* | .
 
  62     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
  63     qq|LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id) | .
 
  64     qq|WHERE (ct.id = ?) | .
 
  65     qq|ORDER BY cp.cp_id LIMIT 1|;
 
  66   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
 
  68   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
  70   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
  73   if ( $form->{salesman_id} ) {
 
  75       qq|SELECT ct.name AS salesman | .
 
  79       selectrow_query($form, $dbh, $query, $form->{salesman_id});
 
  82   my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
 
  84     qq|SELECT n.*, n.itime::DATE AS created_on,
 
  85          e.name AS created_by_name, e.login AS created_by_login
 
  87        LEFT JOIN employee e ON (n.created_by = e.id)
 
  88        WHERE (n.trans_id = ?) AND (n.trans_module = 'ct')|;
 
  89   $form->{NOTES} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
  92     qq|SELECT fu.follow_up_date, fu.done AS follow_up_done, e.name AS created_for_name, e.name AS created_for_login
 
  94        LEFT JOIN employee e ON (fu.created_for_user = e.id)
 
  95        WHERE (fu.note_id = ?)
 
  96          AND NOT COALESCE(fu.done, FALSE)
 
  97          AND (   (fu.created_by = ?)
 
  98               OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?)))|;
 
  99   $sth = prepare_query($form, $dbh, $query);
 
 101   foreach my $note (@{ $form->{NOTES} }) {
 
 102     do_statement($form, $sth, $query, conv_i($note->{id}), conv_i($note->{created_by}), conv_i($employee_id));
 
 103     $ref = $sth->fetchrow_hashref();
 
 105     map { $note->{$_} = $ref->{$_} } keys %{ $ref } if ($ref);
 
 110   if ($form->{edit_note_id}) {
 
 112       qq|SELECT n.id AS NOTE_id, n.subject AS NOTE_subject, n.body AS NOTE_body,
 
 113            fu.id AS FU_id, fu.follow_up_date AS FU_date, fu.done AS FU_done, fu.created_for_user AS FU_created_for_user
 
 115          LEFT JOIN follow_ups fu ON ((n.id = fu.note_id) AND NOT COALESCE(fu.done, FALSE))
 
 117     $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{edit_note_id}));
 
 120       foreach my $key (keys %{ $ref }) {
 
 122         $new_key          =~ s/^([^_]+)/\U$1\E/;
 
 123         $form->{$new_key} =  $ref->{$key};
 
 128   # check if it is orphaned
 
 129   my $arap      = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
 
 132   if ($form->{db} eq 'vendor') {
 
 133     $makemodel = qq| UNION SELECT 1 FROM makemodel mm WHERE mm.make = ?|;
 
 140     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
 
 141     qq|WHERE ct.id = ? | .
 
 145     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
 
 148   my ($dummy) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x $num_args);
 
 150   $form->{status} = "orphaned" unless ($dummy);
 
 154   $main::lxdebug->leave_sub();
 
 157 sub populate_drop_down_boxes {
 
 158   $main::lxdebug->enter_sub();
 
 160   my ($self, $myconfig, $form, $provided_dbh) = @_;
 
 163   my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
 
 166   $query = qq|SELECT id, description FROM business ORDER BY id|;
 
 167   $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
 
 171     qq|SELECT shipto_id, shiptoname, shiptodepartment_1, shiptostreet, shiptocity
 
 173        WHERE (trans_id = ?) AND (module = 'CT')|;
 
 174   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
 
 177   $query  = qq|SELECT cp_id, cp_name, cp_givenname FROM contacts WHERE cp_cv_id = ? ORDER BY cp_name|;
 
 178   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
 
 181   $query = qq|SELECT id, description FROM language ORDER BY id|;
 
 182   $form->{languages} = selectall_hashref_query($form, $dbh, $query);
 
 185   $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
 
 186   $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
 
 188   $dbh->disconnect() unless ($provided_dbh);
 
 190   $main::lxdebug->leave_sub();
 
 193 sub query_titles_and_greetings {
 
 194   $main::lxdebug->enter_sub();
 
 196   my ( $self, $myconfig, $form ) = @_;
 
 197   my ( %tmp,  $ref, $query );
 
 199   my $dbh = $form->dbconnect($myconfig);
 
 202     qq|SELECT DISTINCT(greeting) | .
 
 204     qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 206     qq|SELECT DISTINCT(greeting) | .
 
 208     qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 209     qq|ORDER BY greeting|;
 
 211   map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
 
 212   $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
 
 215     qq|SELECT DISTINCT(cp_title) | .
 
 217     qq|WHERE cp_title ~ '[a-zA-Z]'|;
 
 218   $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
 
 221     qq|SELECT DISTINCT(cp_abteilung) | .
 
 223     qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
 
 224   $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
 
 227   $main::lxdebug->leave_sub();
 
 231   $main::lxdebug->enter_sub();
 
 233   my ( $self, $myconfig, $form ) = @_;
 
 235   # set pricegroup to default
 
 236   $form->{klass} = 0 unless ($form->{klass});
 
 238   # connect to database
 
 239   my $dbh = $form->dbconnect_noauto($myconfig);
 
 242     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 243     if ( $form->{"selected_cp_${_}"} );
 
 244        } qw(title greeting abteilung) );
 
 245   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 246   if ( $form->{"selected_company_greeting"} );
 
 248   # assign value discount, terms, creditlimit
 
 249   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 250   $form->{discount} /= 100;
 
 251   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 253   my ( $query, $sth, $f_id );
 
 256     $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
 
 257     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
 
 259     if (($f_id ne $form->{id}) && ($f_id ne "")) {
 
 260       $main::lxdebug->leave_sub();
 
 265     my $customernumber      = SL::TransNumber->new(type        => 'customer',
 
 267                                                    number      => $form->{customernumber},
 
 268                                                    business_id => $form->{business},
 
 270     $form->{customernumber} = $customernumber->create_unique unless $customernumber->is_unique;
 
 272     $query = qq|SELECT nextval('id')|;
 
 273     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 275     $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
 
 276     do_query($form, $dbh, $query, $form->{id});
 
 279   $query = qq|UPDATE customer SET | .
 
 280     qq|customernumber = ?, | .
 
 283     qq|department_1 = ?, | .
 
 284     qq|department_2 = ?, | .
 
 298     qq|creditlimit = ?, | .
 
 300     qq|business_id = ?, | .
 
 301     qq|taxnumber = ?, | .
 
 303     qq|account_number = ?, | .
 
 304     qq|bank_code = ?, | .
 
 309     qq|direct_debit = ?, | .
 
 312     qq|salesman_id = ?, | .
 
 313     qq|language_id = ?, | .
 
 314     qq|payment_id = ?, | .
 
 315     qq|taxzone_id = ?, | .
 
 316     qq|user_password = ?, | .
 
 317     qq|c_vendor_id = ?, | .
 
 321     $form->{customernumber},
 
 324     $form->{department_1},
 
 325     $form->{department_2},
 
 339     $form->{creditlimit},
 
 340     conv_i($form->{terms}),
 
 341     conv_i($form->{business}),
 
 344     $form->{account_number},
 
 349     $form->{obsolete} ? 't' : 'f',
 
 350     $form->{direct_debit} ? 't' : 'f',
 
 353     conv_i($form->{salesman_id}),
 
 354     conv_i($form->{language_id}),
 
 355     conv_i($form->{payment_id}),
 
 356     conv_i($form->{taxzone_id}, 0),
 
 357     $form->{user_password},
 
 358     $form->{c_vendor_id},
 
 359     conv_i($form->{klass}),
 
 362   do_query( $form, $dbh, $query, @values );
 
 365   if ( $form->{cp_id} ) {
 
 366     $query = qq|UPDATE contacts SET | .
 
 368       qq|cp_givenname = ?, | .
 
 371       qq|cp_phone1 = ?, | .
 
 372       qq|cp_phone2 = ?, | .
 
 373       qq|cp_abteilung = ?, | .
 
 375       qq|cp_mobile1 = ?, | .
 
 376       qq|cp_mobile2 = ?, | .
 
 377       qq|cp_satphone = ?, | .
 
 378       qq|cp_satfax = ?, | .
 
 379       qq|cp_project = ?, | .
 
 380       qq|cp_privatphone = ?, | .
 
 381       qq|cp_privatemail = ?, | .
 
 382       qq|cp_birthday = ?, | .
 
 387       $form->{cp_givenname},
 
 392       $form->{cp_abteilung},
 
 396       $form->{cp_satphone},
 
 399       $form->{cp_privatphone},
 
 400       $form->{cp_privatemail},
 
 401       $form->{cp_birthday},
 
 402       $form->{cp_gender} eq 'f' ? 'f' : 'm',
 
 405   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 407       qq|INSERT INTO contacts ( cp_cv_id, cp_title, cp_givenname,  | .
 
 408       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
 
 409       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
 
 410       qq|  cp_birthday, cp_gender) | .
 
 411       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 415       $form->{cp_givenname},
 
 420       $form->{cp_abteilung},
 
 424       $form->{cp_satphone},
 
 427       $form->{cp_privatphone},
 
 428       $form->{cp_privatemail},
 
 429       $form->{cp_birthday},
 
 430       $form->{cp_gender} eq 'f' ? 'f' : 'm',
 
 433   do_query( $form, $dbh, $query, @values ) if ($query);
 
 436   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 438   $self->_save_note('dbh' => $dbh);
 
 439   $self->_delete_selected_notes('dbh' => $dbh);
 
 441   CVar->save_custom_variables('dbh'       => $dbh,
 
 443                               'trans_id'  => $form->{id},
 
 444                               'variables' => $form,
 
 445                               'always_valid' => 1);
 
 447   my $rc = $dbh->commit();
 
 450   $main::lxdebug->leave_sub();
 
 455   $main::lxdebug->enter_sub();
 
 457   my ( $self, $myconfig, $form ) = @_;
 
 459   $form->{taxzone_id} *= 1;
 
 460   # connect to database
 
 461   my $dbh = $form->dbconnect_noauto($myconfig);
 
 464     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 465     if ( $form->{"selected_cp_${_}"} );
 
 466        } qw(title greeting abteilung) );
 
 467   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 468   if ( $form->{"selected_company_greeting"} );
 
 470   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 471   $form->{discount} /= 100;
 
 472   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 477     $query = qq|SELECT nextval('id')|;
 
 478     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 480     $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
 
 481     do_query($form, $dbh, $query, $form->{id});
 
 483     my $vendornumber      = SL::TransNumber->new(type   => 'vendor',
 
 485                                                  number => $form->{vendornumber},
 
 487     $form->{vendornumber} = $vendornumber->create_unique unless $vendornumber->is_unique;
 
 491     qq|UPDATE vendor SET | .
 
 492     qq|  vendornumber = ?, | .
 
 494     qq|  greeting = ?, | .
 
 495     qq|  department_1 = ?, | .
 
 496     qq|  department_2 = ?, | .
 
 501     qq|  homepage = ?, | .
 
 510     qq|  discount = ?, | .
 
 511     qq|  creditlimit = ?, | .
 
 512     qq|  business_id = ?, | .
 
 513     qq|  taxnumber = ?, | .
 
 514     qq|  language = ?, | .
 
 515     qq|  account_number = ?, | .
 
 516     qq|  bank_code = ?, | .
 
 520     qq|  obsolete = ?, | .
 
 521     qq|  direct_debit = ?, | .
 
 523     qq|  payment_id = ?, | .
 
 524     qq|  taxzone_id = ?, | .
 
 525     qq|  language_id = ?, | .
 
 526     qq|  username = ?, | .
 
 527     qq|  user_password = ?, | .
 
 528     qq|  v_customer_id = ? | .
 
 531     $form->{vendornumber},
 
 534     $form->{department_1},
 
 535     $form->{department_2},
 
 548     conv_i($form->{terms}),
 
 550     $form->{creditlimit},
 
 551     conv_i($form->{business}),
 
 554     $form->{account_number},
 
 559     $form->{obsolete} ? 't' : 'f',
 
 560     $form->{direct_debit} ? 't' : 'f',
 
 562     conv_i($form->{payment_id}),
 
 563     conv_i($form->{taxzone_id}, 0),
 
 564     conv_i( $form->{language_id}),
 
 566     $form->{user_password},
 
 567     $form->{v_customer_id},
 
 570   do_query($form, $dbh, $query, @values);
 
 573   if ( $form->{cp_id} ) {
 
 574     $query = qq|UPDATE contacts SET | .
 
 576       qq|cp_givenname = ?, | .
 
 579       qq|cp_phone1 = ?, | .
 
 580       qq|cp_phone2 = ?, | .
 
 581       qq|cp_abteilung = ?, | .
 
 583       qq|cp_mobile1 = ?, | .
 
 584       qq|cp_mobile2 = ?, | .
 
 585       qq|cp_satphone = ?, | .
 
 586       qq|cp_satfax = ?, | .
 
 587       qq|cp_project = ?, | .
 
 588       qq|cp_privatphone = ?, | .
 
 589       qq|cp_privatemail = ?, | .
 
 590       qq|cp_birthday = ?, | .
 
 595       $form->{cp_givenname},
 
 600       $form->{cp_abteilung},
 
 604       $form->{cp_satphone},
 
 607       $form->{cp_privatphone},
 
 608       $form->{cp_privatemail},
 
 609       $form->{cp_birthday},
 
 610       $form->{cp_gender} eq 'f' ? 'f' : 'm',
 
 613   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 615       qq|INSERT INTO contacts ( cp_cv_id, cp_title, cp_givenname,  | .
 
 616       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
 
 617       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
 
 618       qq|  cp_birthday, cp_gender) | .
 
 619       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 623       $form->{cp_givenname},
 
 628       $form->{cp_abteilung},
 
 632       $form->{cp_satphone},
 
 635       $form->{cp_privatphone},
 
 636       $form->{cp_privatemail},
 
 637       $form->{cp_birthday},
 
 641   do_query($form, $dbh, $query, @values) if ($query);
 
 644   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 646   $self->_save_note('dbh' => $dbh);
 
 647   $self->_delete_selected_notes('dbh' => $dbh);
 
 649   CVar->save_custom_variables('dbh'       => $dbh,
 
 651                               'trans_id'  => $form->{id},
 
 652                               'variables' => $form,
 
 653                               'always_valid' => 1);
 
 655   my $rc = $dbh->commit();
 
 658   $main::lxdebug->leave_sub();
 
 663   $main::lxdebug->enter_sub();
 
 665   my ( $self, $myconfig, $form ) = @_;
 
 666   # connect to database
 
 667   my $dbh = $form->dbconnect($myconfig);
 
 670   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 671   my $query = qq|DELETE FROM $cv WHERE id = ?|;
 
 672   do_query($form, $dbh, $query, $form->{id});
 
 676   $main::lxdebug->leave_sub();
 
 680   $main::lxdebug->enter_sub();
 
 682   my ( $self, $myconfig, $form ) = @_;
 
 684   # connect to database
 
 685   my $dbh = $form->dbconnect($myconfig);
 
 687   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 692   my %allowed_sort_columns =
 
 694       id customernumber vendornumber name contact phone fax email street
 
 695       taxnumber business invnumber ordnumber quonumber zipcode city
 
 697   my $sortorder    = $form->{sort} && $allowed_sort_columns{$form->{sort}} ? $form->{sort} : "name";
 
 698   $form->{sort} = $sortorder;
 
 699   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
 
 701   if ($sortorder ne 'id' && 1 >= scalar grep { $form->{$_} } qw(l_ordnumber l_quonumber l_invnumber)) {
 
 702     $sortorder  = "lower($sortorder) ${sortdir}";
 
 704     $sortorder .= " ${sortdir}";
 
 707   if ($form->{"${cv}number"}) {
 
 708     $where .= " AND ct.${cv}number ILIKE ?";
 
 709     push(@values, '%' . $form->{"${cv}number"} . '%');
 
 712   foreach my $key (qw(name contact email)) {
 
 714       $where .= " AND ct.$key ILIKE ?";
 
 715       push(@values, '%' . $form->{$key} . '%');
 
 719   if ($form->{cp_name}) {
 
 720     $where .= " AND ct.id IN (SELECT cp_cv_id FROM contacts WHERE lower(cp_name) LIKE lower(?))";
 
 721     push @values, '%' . $form->{cp_name} . '%';
 
 724   if ($form->{addr_city}) {
 
 725     $where .= " AND ((lower(ct.city) LIKE lower(?))
 
 730                         WHERE (module = 'CT')
 
 731                           AND (lower(shiptocity) LIKE lower(?))
 
 734     push @values, ('%' . $form->{addr_city} . '%') x 2;
 
 737   if ( $form->{status} eq 'orphaned' ) {
 
 739       qq| AND ct.id NOT IN | .
 
 740       qq|   (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
 
 741     if ($cv eq 'customer') {
 
 743         qq| AND ct.id NOT IN | .
 
 744         qq| (SELECT a.customer_id FROM ar a, customer cv | .
 
 745         qq|  WHERE cv.id = a.customer_id)|;
 
 747     if ($cv eq 'vendor') {
 
 749         qq| AND ct.id NOT IN | .
 
 750         qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
 
 751         qq|  WHERE cv.id = a.vendor_id)|;
 
 753     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
 
 756   if ($form->{obsolete} eq "Y") {
 
 757     $where .= qq| AND obsolete|;
 
 758   } elsif ($form->{obsolete} eq "N") {
 
 759     $where .= qq| AND NOT obsolete|;
 
 762   if ($form->{business_id}) {
 
 763     $where .= qq| AND (business_id = ?)|;
 
 764     push(@values, conv_i($form->{business_id}));
 
 767   my ($cvar_where, @cvar_values) = CVar->build_filter_query('module'         => 'CT',
 
 768                                                             'trans_id_field' => 'ct.id',
 
 772     $where .= qq| AND ($cvar_where)|;
 
 773     push @values, @cvar_values;
 
 776   if ($form->{addr_street}) {
 
 777     $where .= qq| AND (street ILIKE ?)|;
 
 778     push @values, '%' . $form->{addr_street} . '%';
 
 781   if ($form->{addr_zipcode}) {
 
 782     $where .= qq| AND (zipcode ILIKE ?)|;
 
 783     push @values, $form->{addr_zipcode} . '%';
 
 787     qq|SELECT ct.*, b.description AS business | .
 
 789     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 792   my @saved_values = @values;
 
 793   # redo for invoices, orders and quotations
 
 794   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
 
 795     my ($ar, $union, $module);
 
 798     if ($form->{l_invnumber}) {
 
 799       my $ar = $cv eq 'customer' ? 'ar' : 'ap';
 
 800       my $module = $ar eq 'ar' ? 'is' : 'ir';
 
 803         qq|SELECT ct.*, b.description AS business, | .
 
 804         qq|  a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
 
 805         qq|  '$module' AS module, 'invoice' AS formtype, | .
 
 806         qq|  (a.amount = a.paid) AS closed | .
 
 808         qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
 
 809         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 810         qq|WHERE $where AND (a.invoice = '1')|;
 
 815     if ( $form->{l_ordnumber} ) {
 
 816       if ($union eq "UNION") {
 
 817         push(@values, @saved_values);
 
 821         qq|SELECT ct.*, b.description AS business,| .
 
 822         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 823         qq|  'oe' AS module, 'order' AS formtype, o.closed | .
 
 825         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 826         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 827         qq|WHERE $where AND (o.quotation = '0')|;
 
 832     if ( $form->{l_quonumber} ) {
 
 833       if ($union eq "UNION") {
 
 834         push(@values, @saved_values);
 
 838         qq|SELECT ct.*, b.description AS business, | .
 
 839         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 840         qq|  'oe' AS module, 'quotation' AS formtype, o.closed | .
 
 842         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 843         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 844         qq|WHERE $where AND (o.quotation = '1')|;
 
 848   $query .= qq| ORDER BY $sortorder|;
 
 850   $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
 
 852   $main::lxdebug->leave_sub();
 
 856   $main::lxdebug->enter_sub();
 
 858   my ( $self, $myconfig, $form ) = @_;
 
 859   my $dbh   = $form->dbconnect($myconfig);
 
 861     qq|SELECT * FROM contacts c | .
 
 862     qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
 
 863   my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
 
 864   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 866   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 868   $query = qq|SELECT COUNT(cp_id) AS used FROM (
 
 869     SELECT cp_id FROM oe UNION
 
 870     SELECT cp_id FROM ar UNION
 
 871     SELECT cp_id FROM ap UNION
 
 872     SELECT cp_id FROM delivery_orders
 
 873   ) AS cpid WHERE cp_id = ? OR ? = 0|;
 
 874   ($form->{cp_used}) = selectfirst_array_query($form, $dbh, $query, ($form->{cp_id})x2);
 
 879   $main::lxdebug->leave_sub();
 
 883   $main::lxdebug->enter_sub();
 
 885   my ( $self, $myconfig, $form ) = @_;
 
 886   my $dbh   = $form->dbconnect($myconfig);
 
 887   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
 888   my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
 
 890   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 892   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 894   $query = qq|SELECT COUNT(shipto_id) AS used FROM (
 
 895     SELECT shipto_id FROM oe UNION
 
 896     SELECT shipto_id FROM ar UNION
 
 897     SELECT shipto_id FROM delivery_orders
 
 898   ) AS stid WHERE shipto_id = ? OR ? = 0|;
 
 899   ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
 
 904   $main::lxdebug->leave_sub();
 
 908   $main::lxdebug->enter_sub();
 
 910   my ( $self, $myconfig, $form ) = @_;
 
 911   my $dbh = $form->dbconnect($myconfig);
 
 913   my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
 
 914   my $db = $form->{db} eq "customer" ? "customer" : "vendor";
 
 915   my $qty_sign = $form->{db} eq 'vendor' ? ' * -1 AS qty' : '';
 
 917   my $where = " WHERE 1=1 ";
 
 920   if ($form->{shipto_id} && ($arap eq "ar")) {
 
 921     $where .= "AND ${arap}.shipto_id = ?";
 
 922     push(@values, $form->{shipto_id});
 
 924     $where .= "AND ${arap}.${db}_id = ?";
 
 925     push(@values, $form->{id});
 
 929     $where .= "AND ${arap}.transdate >= ?";
 
 930     push(@values, conv_date($form->{from}));
 
 933     $where .= "AND ${arap}.transdate <= ?";
 
 934     push(@values, conv_date($form->{to}));
 
 937     qq|SELECT s.shiptoname, i.qty $qty_sign, | .
 
 938     qq|  ${arap}.id, ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
 
 939     qq|  i.description, i.unit, i.sellprice, | .
 
 940     qq|  oe.id AS oe_id, invoice | .
 
 942     qq|LEFT JOIN shipto s ON | .
 
 944      ? qq|(ar.shipto_id = s.shipto_id) |
 
 945      : qq|(ap.id = s.trans_id) |) .
 
 946     qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
 
 947     qq|LEFT join parts p ON (p.id = i.parts_id) | .
 
 948     qq|LEFT JOIN oe ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '') | .
 
 950     qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
 
 952   $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
 
 956   $main::lxdebug->leave_sub();
 
 960   $main::lxdebug->enter_sub();
 
 965   my $form   = $main::form;
 
 967   Common::check_params(\%params, 'dbh');
 
 969   if (!$form->{NOTE_subject}) {
 
 970     $main::lxdebug->leave_sub();
 
 974   my $dbh = $params{dbh};
 
 978     'id'           => $form->{NOTE_id},
 
 979     'subject'      => $form->{NOTE_subject},
 
 980     'body'         => $form->{NOTE_body},
 
 981     'trans_id'     => $form->{id},
 
 982     'trans_module' => 'ct',
 
 985   $note{id} = Notes->save(%note);
 
 987   if ($form->{FU_date}) {
 
 989       'id'               => $form->{FU_id},
 
 990       'note_id'          => $note{id},
 
 991       'follow_up_date'   => $form->{FU_date},
 
 992       'created_for_user' => $form->{FU_created_for_user},
 
 993       'done'             => $form->{FU_done} ? 1 : 0,
 
 994       'subject'          => $form->{NOTE_subject},
 
 995       'body'             => $form->{NOTE_body},
 
 998           'trans_id'     => $form->{id},
 
 999           'trans_type'   => $form->{db} eq 'customer' ? 'customer' : 'vendor',
 
1000           'trans_info'   => $form->{name},
 
1005     $follow_up{id} = FU->save(%follow_up);
 
1007   } elsif ($form->{FU_id}) {
 
1008     do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
 
1009     do_query($form, $dbh, qq|DELETE FROM follow_ups      WHERE id = ?|,           conv_i($form->{FU_id}));
 
1012   delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
 
1014   $main::lxdebug->leave_sub();
 
1017 sub _delete_selected_notes {
 
1018   $main::lxdebug->enter_sub();
 
1023   Common::check_params(\%params, 'dbh');
 
1025   my $form = $main::form;
 
1026   my $dbh  = $params{dbh};
 
1028   foreach my $i (1 .. $form->{NOTES_rowcount}) {
 
1029     next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
 
1031     Notes->delete('dbh' => $params{dbh},
 
1032                   'id'  => $form->{"NOTE_id_$i"});
 
1035   $main::lxdebug->leave_sub();
 
1039   $main::lxdebug->enter_sub();
 
1042   my $shipto_id = shift;
 
1044   my $form      = $main::form;
 
1045   my %myconfig  = %main::myconfig;
 
1046   my $dbh       = $form->get_standard_dbh(\%myconfig);
 
1048   do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $shipto_id);
 
1052   $main::lxdebug->leave_sub();
 
1056   $main::lxdebug->enter_sub();
 
1061   Common::check_params(\%params, qw(vc id));
 
1063   my $myconfig = \%main::myconfig;
 
1064   my $form     = $main::form;
 
1066   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1068   my $table        = $params{vc} eq 'customer' ? 'customer' : 'vendor';
 
1069   my @ids          = ref $params{id} eq 'ARRAY' ? @{ $params{id} } : ($params{id});
 
1070   my $placeholders = join ", ", ('?') x scalar @ids;
 
1071   my $query        = qq|SELECT id, name, account_number, bank, bank_code, iban, bic
 
1073                         WHERE id IN (${placeholders})|;
 
1075   my $result       = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
 
1077   if (ref $params{id} eq 'ARRAY') {
 
1078     $result = { map { $_->{id} => $_ } @{ $result } };
 
1080     $result = $result->[0] || { 'id' => $params{id} };
 
1083   $main::lxdebug->leave_sub();
 
1088 sub parse_excel_file {
 
1089   $main::lxdebug->enter_sub();
 
1091   my ($self, $myconfig, $form) = @_;
 
1092   my $locale = $main::locale;
 
1094   $form->{formname}   = 'sales_quotation';
 
1095   $form->{type}   = 'sales_quotation';
 
1096   $form->{format} = 'excel';
 
1097   $form->{media}  = 'screen';
 
1098   $form->{quonumber} = 1;
 
1101   # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
 
1102   $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
 
1106   $form->{"${inv}date"} = $form->{transdate};
 
1107   $form->{label}        = $locale->text('Quotation');
 
1108   my $numberfld            = "sqnumber";
 
1112   $form->{what_done} = $form->{formname};
 
1114   map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
 
1116   my $output_dateformat = $myconfig->{"dateformat"};
 
1117   my $output_numberformat = $myconfig->{"numberformat"};
 
1118   my $output_longdates = 1;
 
1120   # map login user variables
 
1121   map { $form->{"login_$_"} = $myconfig->{$_} } ("name", "email", "fax", "tel", "company");
 
1124   for my $field (qw(transdate_oe deliverydate_oe)) {
 
1126       $form->{$field}[$_] = $locale->date($myconfig, $form->{$field}[$_], 1);
 
1127     } 0 .. $#{ $form->{$field} };
 
1130   if ($form->{shipto_id}) {
 
1131     $form->get_shipto($myconfig);
 
1134   $form->{notes} =~ s/^\s+//g;
 
1136   $form->{templates} = $myconfig->{templates};
 
1138   delete $form->{printer_command};
 
1140   $form->get_employee_info($myconfig);
 
1142   my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
 
1144   if (scalar @{ $cvar_date_fields }) {
 
1145     format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
 
1148   while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
 
1149     reformat_numbers($output_numberformat, $precision, @{ $field_list });
 
1153   my $extension            = 'xls';
 
1155   $form->{IN}         = "$form->{formname}.${extension}";
 
1157   delete $form->{OUT};
 
1159   $form->parse_template($myconfig);
 
1161   $main::lxdebug->leave_sub();