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 #======================================================================
 
  49   $main::lxdebug->enter_sub();
 
  51   my ( $self, $myconfig, $form ) = @_;
 
  53   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
  55   my $dbh   = $form->dbconnect($myconfig);
 
  57     qq|SELECT ct.*, b.id AS business, cp.* | .
 
  59     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
  60     qq|LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id) | .
 
  61     qq|WHERE (ct.id = ?) | .
 
  62     qq|ORDER BY cp.cp_id LIMIT 1|;
 
  63   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
 
  65   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
  67   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
  70   if ( $form->{salesman_id} ) {
 
  72       qq|SELECT ct.name AS salesman | .
 
  76       selectrow_query($form, $dbh, $query, $form->{salesman_id});
 
  79   my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
 
  81     qq|SELECT n.*, n.itime::DATE AS created_on,
 
  82          e.name AS created_by_name, e.login AS created_by_login
 
  84        LEFT JOIN employee e ON (n.created_by = e.id)
 
  85        WHERE (n.trans_id = ?) AND (n.trans_module = 'ct')|;
 
  86   $form->{NOTES} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
  89     qq|SELECT fu.follow_up_date, fu.done AS follow_up_done, e.name AS created_for_name, e.name AS created_for_login
 
  91        LEFT JOIN employee e ON (fu.created_for_user = e.id)
 
  92        WHERE (fu.note_id = ?)
 
  93          AND NOT COALESCE(fu.done, FALSE)
 
  94          AND (   (fu.created_by = ?)
 
  95               OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?)))|;
 
  96   $sth = prepare_query($form, $dbh, $query);
 
  98   foreach my $note (@{ $form->{NOTES} }) {
 
  99     do_statement($form, $sth, $query, conv_i($note->{id}), conv_i($note->{created_by}), conv_i($employee_id));
 
 100     $ref = $sth->fetchrow_hashref();
 
 102     map { $note->{$_} = $ref->{$_} } keys %{ $ref } if ($ref);
 
 107   if ($form->{edit_note_id}) {
 
 109       qq|SELECT n.id AS NOTE_id, n.subject AS NOTE_subject, n.body AS NOTE_body,
 
 110            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
 
 112          LEFT JOIN follow_ups fu ON ((n.id = fu.note_id) AND NOT COALESCE(fu.done, FALSE))
 
 114     $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{edit_note_id}));
 
 117       foreach my $key (keys %{ $ref }) {
 
 119         $new_key          =~ s/^([^_]+)/\U\1\E/;
 
 120         $form->{$new_key} =  $ref->{$key};
 
 125   # check if it is orphaned
 
 126   my $arap      = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
 
 129   if ($form->{db} eq 'vendor') {
 
 130     $makemodel = qq| UNION SELECT 1 FROM makemodel mm WHERE mm.make = ?|;
 
 137     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
 
 138     qq|WHERE ct.id = ? | .
 
 142     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
 
 145   my ($dummy) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x $num_args);
 
 147   $form->{status} = "orphaned" unless ($dummy);
 
 151   $main::lxdebug->leave_sub();
 
 154 sub populate_drop_down_boxes {
 
 155   $main::lxdebug->enter_sub();
 
 157   my ($self, $myconfig, $form, $provided_dbh) = @_;
 
 159   my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
 
 162   $query = qq|SELECT id, description FROM business ORDER BY id|;
 
 163   $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
 
 167     qq|SELECT shipto_id, shiptoname, shiptodepartment_1, shiptostreet, shiptocity
 
 169        WHERE (trans_id = ?) AND (module = 'CT')|;
 
 170   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
 
 173   $query  = qq|SELECT cp_id, cp_name, cp_givenname FROM contacts WHERE cp_cv_id = ? ORDER BY cp_name|;
 
 174   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
 
 177   $query = qq|SELECT id, description FROM language ORDER BY id|;
 
 178   $form->{languages} = selectall_hashref_query($form, $dbh, $query);
 
 181   $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
 
 182   $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
 
 184   $dbh->disconnect() unless ($provided_dbh);
 
 186   $main::lxdebug->leave_sub();
 
 189 sub query_titles_and_greetings {
 
 190   $main::lxdebug->enter_sub();
 
 192   my ( $self, $myconfig, $form ) = @_;
 
 195   my $dbh = $form->dbconnect($myconfig);
 
 197 #  edit: cp_greeting wurde entfernt, wird durch cp_gender ersetzt
 
 199 #    qq|SELECT DISTINCT(cp_greeting) | .
 
 200 #    qq|FROM contacts | .
 
 201 #    qq|WHERE cp_greeting ~ '[a-zA-Z]' | .
 
 202 #    qq|ORDER BY cp_greeting|;
 
 203 #  $form->{GREETINGS} = [ selectall_array_query($form, $dbh, $query) ];
 
 206 #    qq|SELECT DISTINCT(greeting) | .
 
 207 #    qq|FROM customer | .
 
 208 #    qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 210 #    qq|SELECT DISTINCT(greeting) | .
 
 212 #    qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 213 #    qq|ORDER BY greeting|;
 
 215 #  map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
 
 216 #  $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
 
 219     qq|SELECT DISTINCT(cp_title) | .
 
 221     qq|WHERE cp_title ~ '[a-zA-Z]'|;
 
 222   $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
 
 225     qq|SELECT DISTINCT(cp_abteilung) | .
 
 227     qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
 
 228   $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
 
 231   $main::lxdebug->leave_sub();
 
 235   $main::lxdebug->enter_sub();
 
 237   my ( $self, $myconfig, $form ) = @_;
 
 239   # set pricegroup to default
 
 240   $form->{klass} = 0 unless ($form->{klass});
 
 242   # connect to database
 
 243   my $dbh = $form->dbconnect_noauto($myconfig);
 
 246     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 247     if ( $form->{"selected_cp_${_}"} );
 
 248        } qw(title greeting abteilung) );
 
 249   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 250   if ( $form->{"selected_company_greeting"} );
 
 252   # assign value discount, terms, creditlimit
 
 253   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 254   $form->{discount} /= 100;
 
 255   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 257   my ( $query, $sth, $f_id );
 
 260     $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
 
 261     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
 
 263     if (($f_id ne $form->{id}) && ($f_id ne "")) {
 
 264       $main::lxdebug->leave_sub();
 
 269     if (!$form->{customernumber} && $form->{business}) {
 
 270       $form->{customernumber} =
 
 271         $form->update_business($myconfig, $form->{business}, $dbh);
 
 273     if (!$form->{customernumber}) {
 
 274       $form->{customernumber} =
 
 275         $form->update_defaults($myconfig, "customernumber", $dbh);
 
 278     $query  = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
 
 279     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
 
 281       $main::lxdebug->leave_sub();
 
 285     $query = qq|SELECT nextval('id')|;
 
 286     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 288     $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
 
 289     do_query($form, $dbh, $query, $form->{id});
 
 292   $query = qq|UPDATE customer SET | .
 
 293     qq|customernumber = ?, | .
 
 296     qq|department_1 = ?, | .
 
 297     qq|department_2 = ?, | .
 
 311     qq|creditlimit = ?, | .
 
 313     qq|business_id = ?, | .
 
 314     qq|taxnumber = ?, | .
 
 316     qq|account_number = ?, | .
 
 317     qq|bank_code = ?, | .
 
 322     qq|direct_debit = ?, | .
 
 325     qq|salesman_id = ?, | .
 
 326     qq|language_id = ?, | .
 
 327     qq|payment_id = ?, | .
 
 328     qq|taxzone_id = ?, | .
 
 329     qq|user_password = ?, | .
 
 330     qq|c_vendor_id = ?, | .
 
 334     $form->{customernumber},
 
 337     $form->{department_1},
 
 338     $form->{department_2},
 
 352     $form->{creditlimit},
 
 353     conv_i($form->{terms}),
 
 354     conv_i($form->{business}),
 
 357     $form->{account_number},
 
 362     $form->{obsolete} ? 't' : 'f',
 
 363     $form->{direct_debit} ? 't' : 'f',
 
 366     conv_i($form->{salesman_id}),
 
 367     conv_i($form->{language_id}),
 
 368     conv_i($form->{payment_id}),
 
 369     conv_i($form->{taxzone_id}, 0),
 
 370     $form->{user_password},
 
 371     $form->{c_vendor_id},
 
 372     conv_i($form->{klass}),
 
 375   do_query( $form, $dbh, $query, @values );
 
 378   if ( $form->{cp_id} ) {
 
 379     $query = qq|UPDATE contacts SET | .
 
 380       qq|cp_greeting = ?, | .
 
 382       qq|cp_givenname = ?, | .
 
 385       qq|cp_phone1 = ?, | .
 
 386       qq|cp_phone2 = ?, | .
 
 387       qq|cp_abteilung = ?, | .
 
 389       qq|cp_mobile1 = ?, | .
 
 390       qq|cp_mobile2 = ?, | .
 
 391       qq|cp_satphone = ?, | .
 
 392       qq|cp_satfax = ?, | .
 
 393       qq|cp_project = ?, | .
 
 394       qq|cp_privatphone = ?, | .
 
 395       qq|cp_privatemail = ?, | .
 
 396       qq|cp_birthday = ?, | .
 
 400       $form->{cp_greeting},
 
 402       $form->{cp_givenname},
 
 407       $form->{cp_abteilung},
 
 411       $form->{cp_satphone},
 
 414       $form->{cp_privatphone},
 
 415       $form->{cp_privatemail},
 
 416       $form->{cp_birthday},
 
 417       $form->{cp_gender} eq 'f' ? 'f' : 'm',
 
 420   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 422       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname,  | .
 
 423       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
 
 424       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
 
 425       qq|  cp_birthday, cp_gender) | .
 
 426       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 429       $form->{cp_greeting},
 
 431       $form->{cp_givenname},
 
 436       $form->{cp_abteilung},
 
 440       $form->{cp_satphone},
 
 443       $form->{cp_privatphone},
 
 444       $form->{cp_privatemail},
 
 445       $form->{cp_birthday},
 
 446       $form->{cp_gender} eq 'f' ? 'f' : 'm',
 
 449   do_query( $form, $dbh, $query, @values ) if ($query);
 
 452   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 454   $self->_save_note('dbh' => $dbh);
 
 455   $self->_delete_selected_notes('dbh' => $dbh);
 
 457   CVar->save_custom_variables('dbh'       => $dbh,
 
 459                               'trans_id'  => $form->{id},
 
 460                               'variables' => $form);
 
 462   $rc = $dbh->commit();
 
 465   $main::lxdebug->leave_sub();
 
 470   $main::lxdebug->enter_sub();
 
 472   my ( $self, $myconfig, $form ) = @_;
 
 474   $form->{taxzone_id} *= 1;
 
 475   # connect to database
 
 476   my $dbh = $form->dbconnect_noauto($myconfig);
 
 479     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 480     if ( $form->{"selected_cp_${_}"} );
 
 481        } qw(title greeting abteilung) );
 
 482   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 483   if ( $form->{"selected_company_greeting"} );
 
 485   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 486   $form->{discount} /= 100;
 
 487   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 492     $query = qq|SELECT nextval('id')|;
 
 493     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 495     $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
 
 496     do_query($form, $dbh, $query, $form->{id});
 
 498     if ( !$form->{vendornumber} ) {
 
 499       $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
 
 504     qq|UPDATE vendor SET | .
 
 505     qq|  vendornumber = ?, | .
 
 507     qq|  greeting = ?, | .
 
 508     qq|  department_1 = ?, | .
 
 509     qq|  department_2 = ?, | .
 
 514     qq|  homepage = ?, | .
 
 523     qq|  discount = ?, | .
 
 524     qq|  creditlimit = ?, | .
 
 525     qq|  business_id = ?, | .
 
 526     qq|  taxnumber = ?, | .
 
 527     qq|  language = ?, | .
 
 528     qq|  account_number = ?, | .
 
 529     qq|  bank_code = ?, | .
 
 533     qq|  obsolete = ?, | .
 
 534     qq|  direct_debit = ?, | .
 
 536     qq|  payment_id = ?, | .
 
 537     qq|  taxzone_id = ?, | .
 
 538     qq|  language_id = ?, | .
 
 539     qq|  username = ?, | .
 
 540     qq|  user_password = ?, | .
 
 541     qq|  v_customer_id = ? | .
 
 544     $form->{vendornumber},
 
 547     $form->{department_1},
 
 548     $form->{department_2},
 
 561     conv_i($form->{terms}),
 
 563     $form->{creditlimit},
 
 564     conv_i($form->{business}),
 
 567     $form->{account_number},
 
 572     $form->{obsolete} ? 't' : 'f',
 
 573     $form->{direct_debit} ? 't' : 'f',
 
 575     conv_i($form->{payment_id}),
 
 576     conv_i($form->{taxzone_id}, 0),
 
 577     conv_i( $form->{language_id}),
 
 579     $form->{user_password},
 
 580     $form->{v_customer_id},
 
 583   do_query($form, $dbh, $query, @values);
 
 586   if ( $form->{cp_id} ) {
 
 587     $query = qq|UPDATE contacts SET | .
 
 588       qq|cp_greeting = ?, | .
 
 590       qq|cp_givenname = ?, | .
 
 593       qq|cp_phone1 = ?, | .
 
 594       qq|cp_phone2 = ?, | .
 
 595       qq|cp_abteilung = ?, | .
 
 597       qq|cp_mobile1 = ?, | .
 
 598       qq|cp_mobile2 = ?, | .
 
 599       qq|cp_satphone = ?, | .
 
 600       qq|cp_satfax = ?, | .
 
 601       qq|cp_project = ?, | .
 
 602       qq|cp_privatphone = ?, | .
 
 603       qq|cp_privatemail = ?, | .
 
 604       qq|cp_birthday = ? | .
 
 608       $form->{cp_greeting},
 
 610       $form->{cp_givenname},
 
 615       $form->{cp_abteilung},
 
 619       $form->{cp_satphone},
 
 622       $form->{cp_privatphone},
 
 623       $form->{cp_privatemail},
 
 624       $form->{cp_birthday},
 
 625       $form->{cp_gender} eq 'f' ? 'f' : 'm',
 
 628   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 630       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname,  | .
 
 631       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
 
 632       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
 
 633       qq|  cp_birthday, cp_gender) | .
 
 634       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 637       $form->{cp_greeting},
 
 639       $form->{cp_givenname},
 
 644       $form->{cp_abteilung},
 
 648       $form->{cp_satphone},
 
 651       $form->{cp_privatphone},
 
 652       $form->{cp_privatemail},
 
 656   do_query($form, $dbh, $query, @values) if ($query);
 
 659   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 661   $self->_save_note('dbh' => $dbh);
 
 662   $self->_delete_selected_notes('dbh' => $dbh);
 
 664   CVar->save_custom_variables('dbh'       => $dbh,
 
 666                               'trans_id'  => $form->{id},
 
 667                               'variables' => $form);
 
 669   $rc = $dbh->commit();
 
 672   $main::lxdebug->leave_sub();
 
 677   $main::lxdebug->enter_sub();
 
 679   my ( $self, $myconfig, $form ) = @_;
 
 680   # connect to database
 
 681   my $dbh = $form->dbconnect($myconfig);
 
 684   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 685   my $query = qq|DELETE FROM $cv WHERE id = ?|;
 
 686   do_query($form, $dbh, $query, $form->{id});
 
 690   $main::lxdebug->leave_sub();
 
 694   $main::lxdebug->enter_sub();
 
 696   my ( $self, $myconfig, $form ) = @_;
 
 698   # connect to database
 
 699   my $dbh = $form->dbconnect($myconfig);
 
 701   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 706   my %allowed_sort_columns =
 
 707     map({ $_, 1 } qw(id customernumber vendornumber name contact phone fax email
 
 708                      taxnumber business invnumber ordnumber quonumber));
 
 709   $sortorder    = $form->{sort} && $allowed_sort_columns{$form->{sort}} ? $form->{sort} : "name";
 
 710   $form->{sort} = $sortorder;
 
 711   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
 
 713   if ($sortorder ne 'id' && 1 >= scalar grep { $form->{$_} } qw(l_ordnumber l_quonumber l_invnumber)) {
 
 714     $sortorder  = "lower($sortorder) ${sortdir}";
 
 716     $sortorder .= " ${sortdir}";
 
 719   if ($form->{"${cv}number"}) {
 
 720     $where .= " AND ct.${cv}number ILIKE ?";
 
 721     push(@values, '%' . $form->{"${cv}number"} . '%');
 
 724   foreach my $key (qw(name contact email)) {
 
 726       $where .= " AND ct.$key ILIKE ?";
 
 727       push(@values, '%' . $form->{$key} . '%');
 
 731   if ($form->{cp_name}) {
 
 732     $where .= " AND ct.id IN (SELECT cp_cv_id FROM contacts WHERE lower(cp_name) LIKE lower(?))";
 
 733     push @values, '%' . $form->{cp_name} . '%';
 
 736   if ($form->{addr_city}) {
 
 737     $where .= " AND ((lower(ct.city) LIKE lower(?))
 
 742                         WHERE (module = 'CT')
 
 743                           AND (lower(shiptocity) LIKE lower(?))
 
 746     push @values, ('%' . $form->{addr_city} . '%') x 2;
 
 749   if ( $form->{status} eq 'orphaned' ) {
 
 751       qq| AND ct.id NOT IN | .
 
 752       qq|   (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
 
 753     if ($cv eq 'customer') {
 
 755         qq| AND ct.id NOT IN | .
 
 756         qq| (SELECT a.customer_id FROM ar a, customer cv | .
 
 757         qq|  WHERE cv.id = a.customer_id)|;
 
 759     if ($cv eq 'vendor') {
 
 761         qq| AND ct.id NOT IN | .
 
 762         qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
 
 763         qq|  WHERE cv.id = a.vendor_id)|;
 
 765     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
 
 768   if ($form->{obsolete} eq "Y") {
 
 769     $where .= qq| AND obsolete|;
 
 770   } elsif ($form->{obsolete} eq "N") {
 
 771     $where .= qq| AND NOT obsolete|;
 
 774   if ($form->{business_id}) {
 
 775     $where .= qq| AND (business_id = ?)|;
 
 776     push(@values, conv_i($form->{business_id}));
 
 779   my ($cvar_where, @cvar_values) = CVar->build_filter_query('module'         => 'CT',
 
 780                                                             'trans_id_field' => 'ct.id',
 
 784     $where .= qq| AND ($cvar_where)|;
 
 785     push @values, @cvar_values;
 
 788   if ($form->{addr_street}) {
 
 789     $where .= qq| AND (street ILIKE ?)|;
 
 790     push @values, '%' . $form->{addr_street} . '%';
 
 793   if ($form->{addr_zipcode}) {
 
 794     $where .= qq| AND (zipcode ILIKE ?)|;
 
 795     push @values, $form->{addr_zipcode} . '%';
 
 799     qq|SELECT ct.*, b.description AS business | .
 
 801     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 804   my @saved_values = @values;
 
 805   # redo for invoices, orders and quotations
 
 806   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
 
 807     my ($ar, $union, $module);
 
 810     if ($form->{l_invnumber}) {
 
 811       my $ar = $cv eq 'customer' ? 'ar' : 'ap';
 
 812       my $module = $ar eq 'ar' ? 'is' : 'ir';
 
 815         qq|SELECT ct.*, b.description AS business, | .
 
 816         qq|  a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
 
 817         qq|  '$module' AS module, 'invoice' AS formtype, | .
 
 818         qq|  (a.amount = a.paid) AS closed | .
 
 820         qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
 
 821         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 822         qq|WHERE $where AND (a.invoice = '1')|;
 
 827     if ( $form->{l_ordnumber} ) {
 
 828       if ($union eq "UNION") {
 
 829         push(@values, @saved_values);
 
 833         qq|SELECT ct.*, b.description AS business,| .
 
 834         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 835         qq|  'oe' AS module, 'order' AS formtype, o.closed | .
 
 837         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 838         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 839         qq|WHERE $where AND (o.quotation = '0')|;
 
 844     if ( $form->{l_quonumber} ) {
 
 845       if ($union eq "UNION") {
 
 846         push(@values, @saved_values);
 
 850         qq|SELECT ct.*, b.description AS business, | .
 
 851         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 852         qq|  'oe' AS module, 'quotation' AS formtype, o.closed | .
 
 854         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 855         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 856         qq|WHERE $where AND (o.quotation = '1')|;
 
 860   $query .= qq| ORDER BY $sortorder|;
 
 862   $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
 
 864   $main::lxdebug->leave_sub();
 
 868   $main::lxdebug->enter_sub();
 
 870   my ( $self, $myconfig, $form ) = @_;
 
 871   my $dbh   = $form->dbconnect($myconfig);
 
 873     qq|SELECT * FROM contacts c | .
 
 874     qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
 
 875   my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
 
 876   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 878   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 880   $query = qq|SELECT COUNT(cp_id) AS used FROM (
 
 881     SELECT cp_id FROM oe UNION
 
 882     SELECT cp_id FROM ar UNION
 
 883     SELECT cp_id FROM ap UNION
 
 884     SELECT cp_id FROM delivery_orders
 
 885   ) AS cpid WHERE cp_id = ? OR ? = 0|;
 
 886   ($form->{cp_used}) = selectfirst_array_query($form, $dbh, $query, ($form->{cp_id})x2);
 
 891   $main::lxdebug->leave_sub();
 
 895   $main::lxdebug->enter_sub();
 
 897   my ( $self, $myconfig, $form ) = @_;
 
 898   my $dbh   = $form->dbconnect($myconfig);
 
 899   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
 900   my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
 
 902   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 904   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 906   $query = qq|SELECT COUNT(shipto_id) AS used FROM (
 
 907     SELECT shipto_id FROM oe UNION
 
 908     SELECT shipto_id FROM ar UNION
 
 909     SELECT shipto_id FROM delivery_orders
 
 910   ) AS stid WHERE shipto_id = ? OR ? = 0|;
 
 911   ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
 
 916   $main::lxdebug->leave_sub();
 
 920   $main::lxdebug->enter_sub();
 
 922   my ( $self, $myconfig, $form ) = @_;
 
 923   my $dbh = $form->dbconnect($myconfig);
 
 925   my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
 
 926   my $db = $form->{db} eq "customer" ? "customer" : "vendor";
 
 927   my $qty_sign = $form->{db} eq 'vendor' ? ' * -1 AS qty' : '';
 
 929   my $where = " WHERE 1=1 ";
 
 932   if ($form->{shipto_id} && ($arap eq "ar")) {
 
 933     $where .= "AND ${arap}.shipto_id = ?";
 
 934     push(@values, $form->{shipto_id});
 
 936     $where .= "AND ${arap}.${db}_id = ?";
 
 937     push(@values, $form->{id});
 
 941     $where .= "AND ${arap}.transdate >= ?";
 
 942     push(@values, conv_date($form->{from}));
 
 945     $where .= "AND ${arap}.transdate <= ?";
 
 946     push(@values, conv_date($form->{to}));
 
 949     qq|SELECT s.shiptoname, i.qty $qty_sign, | .
 
 950     qq|  ${arap}.id, ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
 
 951     qq|  i.description, i.unit, i.sellprice, | .
 
 952     qq|  oe.id AS oe_id | .
 
 954     qq|LEFT JOIN shipto s ON | .
 
 956      ? qq|(ar.shipto_id = s.shipto_id) |
 
 957      : qq|(ap.id = s.trans_id) |) .
 
 958     qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
 
 959     qq|LEFT join parts p ON (p.id = i.parts_id) | .
 
 960     qq|LEFT JOIN oe ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '') | .
 
 962     qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
 
 964   $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
 
 968   $main::lxdebug->leave_sub();
 
 972   $main::lxdebug->enter_sub();
 
 977   my $form   = $main::form;
 
 979   Common::check_params(\%params, 'dbh');
 
 981   if (!$form->{NOTE_subject}) {
 
 982     $main::lxdebug->leave_sub();
 
 986   my $dbh = $params{dbh};
 
 990     'id'           => $form->{NOTE_id},
 
 991     'subject'      => $form->{NOTE_subject},
 
 992     'body'         => $form->{NOTE_body},
 
 993     'trans_id'     => $form->{id},
 
 994     'trans_module' => 'ct',
 
 997   $note{id} = Notes->save(%note);
 
 999   if ($form->{FU_date}) {
 
1001       'id'               => $form->{FU_id},
 
1002       'note_id'          => $note{id},
 
1003       'follow_up_date'   => $form->{FU_date},
 
1004       'created_for_user' => $form->{FU_created_for_user},
 
1005       'done'             => $form->{FU_done} ? 1 : 0,
 
1006       'subject'          => $form->{NOTE_subject},
 
1007       'body'             => $form->{NOTE_body},
 
1010           'trans_id'     => $form->{id},
 
1011           'trans_type'   => $form->{db} eq 'customer' ? 'customer' : 'vendor',
 
1012           'trans_info'   => $form->{name},
 
1017     $follow_up{id} = FU->save(%follow_up);
 
1019   } elsif ($form->{FU_id}) {
 
1020     do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
 
1021     do_query($form, $dbh, qq|DELETE FROM follow_ups      WHERE id = ?|,           conv_i($form->{FU_id}));
 
1024   delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
 
1026   $main::lxdebug->leave_sub();
 
1029 sub _delete_selected_notes {
 
1030   $main::lxdebug->enter_sub();
 
1035   Common::check_params(\%params, 'dbh');
 
1037   my $form = $main::form;
 
1038   my $dbh  = $params{dbh};
 
1040   foreach my $i (1 .. $form->{NOTES_rowcount}) {
 
1041     next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
 
1043     Notes->delete('dbh' => $params{dbh},
 
1044                   'id'  => $form->{"NOTE_id_$i"});
 
1047   $main::lxdebug->leave_sub();
 
1051   $main::lxdebug->enter_sub();
 
1054   my $shipto_id = shift;
 
1056   my $form      = $main::form;
 
1057   my %myconfig  = %main::myconfig;
 
1058   my $dbh       = $form->get_standard_dbh(\%myconfig);
 
1060   do_query($form, $dbh, qq|UPDATE shipto SET trans_id = NULL WHERE shipto_id = ?|, $shipto_id);
 
1064   $main::lxdebug->leave_sub();
 
1068   $main::lxdebug->enter_sub();
 
1071   my $shipto_id = shift;
 
1073   my $form      = $main::form;
 
1074   my %myconfig  = %main::myconfig;
 
1075   my $dbh       = $form->get_standard_dbh(\%myconfig);
 
1077   do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $shipto_id);
 
1081   $main::lxdebug->leave_sub();
 
1085   $main::lxdebug->enter_sub();
 
1090   Common::check_params(\%params, qw(vc id));
 
1092   my $myconfig = \%main::myconfig;
 
1093   my $form     = $main::form;
 
1095   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1097   my $table        = $params{vc} eq 'customer' ? 'customer' : 'vendor';
 
1098   my @ids          = ref $params{id} eq 'ARRAY' ? @{ $params{id} } : ($params{id});
 
1099   my $placeholders = ('?') x scalar @ids;
 
1100   my $query        = qq|SELECT id, name, account_number, bank, bank_code, iban, bic
 
1102                         WHERE id IN (${placeholders})|;
 
1104   my $result       = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
 
1106   if (ref $params{id} eq 'ARRAY') {
 
1107     $result = { map { $_->{id} => $_ } @{ $result } };
 
1109     $result = $result->[0] || { 'id' => $params{id} };
 
1112   $main::lxdebug->leave_sub();