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);
 
 198     qq|SELECT DISTINCT(greeting) | .
 
 200     qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 202     qq|SELECT DISTINCT(greeting) | .
 
 204     qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 205     qq|ORDER BY greeting|;
 
 207   map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
 
 208   $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
 
 211     qq|SELECT DISTINCT(cp_title) | .
 
 213     qq|WHERE cp_title ~ '[a-zA-Z]'|;
 
 214   $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
 
 217     qq|SELECT DISTINCT(cp_abteilung) | .
 
 219     qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
 
 220   $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
 
 223   $main::lxdebug->leave_sub();
 
 227   $main::lxdebug->enter_sub();
 
 229   my ( $self, $myconfig, $form ) = @_;
 
 231   # set pricegroup to default
 
 232   $form->{klass} = 0 unless ($form->{klass});
 
 234   # connect to database
 
 235   my $dbh = $form->dbconnect_noauto($myconfig);
 
 238     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 239     if ( $form->{"selected_cp_${_}"} );
 
 240        } qw(title greeting abteilung) );
 
 241   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 242   if ( $form->{"selected_company_greeting"} );
 
 244   # assign value discount, terms, creditlimit
 
 245   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 246   $form->{discount} /= 100;
 
 247   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 249   my ( $query, $sth, $f_id );
 
 252     $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
 
 253     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
 
 255     if (($f_id ne $form->{id}) && ($f_id ne "")) {
 
 256       $main::lxdebug->leave_sub();
 
 261     if (!$form->{customernumber} && $form->{business}) {
 
 262       $form->{customernumber} =
 
 263         $form->update_business($myconfig, $form->{business}, $dbh);
 
 265     if (!$form->{customernumber}) {
 
 266       $form->{customernumber} =
 
 267         $form->update_defaults($myconfig, "customernumber", $dbh);
 
 270     $query  = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
 
 271     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
 
 273       $main::lxdebug->leave_sub();
 
 277     $query = qq|SELECT nextval('id')|;
 
 278     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 280     $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
 
 281     do_query($form, $dbh, $query, $form->{id});
 
 284   $query = qq|UPDATE customer SET | .
 
 285     qq|customernumber = ?, | .
 
 288     qq|department_1 = ?, | .
 
 289     qq|department_2 = ?, | .
 
 303     qq|creditlimit = ?, | .
 
 305     qq|business_id = ?, | .
 
 306     qq|taxnumber = ?, | .
 
 308     qq|account_number = ?, | .
 
 309     qq|bank_code = ?, | .
 
 314     qq|direct_debit = ?, | .
 
 317     qq|salesman_id = ?, | .
 
 318     qq|language_id = ?, | .
 
 319     qq|payment_id = ?, | .
 
 320     qq|taxzone_id = ?, | .
 
 321     qq|user_password = ?, | .
 
 322     qq|c_vendor_id = ?, | .
 
 326     $form->{customernumber},
 
 329     $form->{department_1},
 
 330     $form->{department_2},
 
 344     $form->{creditlimit},
 
 345     conv_i($form->{terms}),
 
 346     conv_i($form->{business}),
 
 349     $form->{account_number},
 
 354     $form->{obsolete} ? 't' : 'f',
 
 355     $form->{direct_debit} ? 't' : 'f',
 
 358     conv_i($form->{salesman_id}),
 
 359     conv_i($form->{language_id}),
 
 360     conv_i($form->{payment_id}),
 
 361     conv_i($form->{taxzone_id}, 0),
 
 362     $form->{user_password},
 
 363     $form->{c_vendor_id},
 
 364     conv_i($form->{klass}),
 
 367   do_query( $form, $dbh, $query, @values );
 
 370   if ( $form->{cp_id} ) {
 
 371     $query = qq|UPDATE contacts SET | .
 
 373       qq|cp_givenname = ?, | .
 
 376       qq|cp_phone1 = ?, | .
 
 377       qq|cp_phone2 = ?, | .
 
 378       qq|cp_abteilung = ?, | .
 
 380       qq|cp_mobile1 = ?, | .
 
 381       qq|cp_mobile2 = ?, | .
 
 382       qq|cp_satphone = ?, | .
 
 383       qq|cp_satfax = ?, | .
 
 384       qq|cp_project = ?, | .
 
 385       qq|cp_privatphone = ?, | .
 
 386       qq|cp_privatemail = ?, | .
 
 387       qq|cp_birthday = ?, | .
 
 392       $form->{cp_givenname},
 
 397       $form->{cp_abteilung},
 
 401       $form->{cp_satphone},
 
 404       $form->{cp_privatphone},
 
 405       $form->{cp_privatemail},
 
 406       $form->{cp_birthday},
 
 407       $form->{cp_gender} eq 'f' ? 'f' : 'm',
 
 410   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 412       qq|INSERT INTO contacts ( cp_cv_id, cp_title, cp_givenname,  | .
 
 413       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
 
 414       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
 
 415       qq|  cp_birthday, cp_gender) | .
 
 416       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 420       $form->{cp_givenname},
 
 425       $form->{cp_abteilung},
 
 429       $form->{cp_satphone},
 
 432       $form->{cp_privatphone},
 
 433       $form->{cp_privatemail},
 
 434       $form->{cp_birthday},
 
 435       $form->{cp_gender} eq 'f' ? 'f' : 'm',
 
 438   do_query( $form, $dbh, $query, @values ) if ($query);
 
 441   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 443   $self->_save_note('dbh' => $dbh);
 
 444   $self->_delete_selected_notes('dbh' => $dbh);
 
 446   CVar->save_custom_variables('dbh'       => $dbh,
 
 448                               'trans_id'  => $form->{id},
 
 449                               'variables' => $form);
 
 451   $rc = $dbh->commit();
 
 454   $main::lxdebug->leave_sub();
 
 459   $main::lxdebug->enter_sub();
 
 461   my ( $self, $myconfig, $form ) = @_;
 
 463   $form->{taxzone_id} *= 1;
 
 464   # connect to database
 
 465   my $dbh = $form->dbconnect_noauto($myconfig);
 
 468     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 469     if ( $form->{"selected_cp_${_}"} );
 
 470        } qw(title greeting abteilung) );
 
 471   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 472   if ( $form->{"selected_company_greeting"} );
 
 474   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 475   $form->{discount} /= 100;
 
 476   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 481     $query = qq|SELECT nextval('id')|;
 
 482     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 484     $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
 
 485     do_query($form, $dbh, $query, $form->{id});
 
 487     if ( !$form->{vendornumber} ) {
 
 488       $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
 
 493     qq|UPDATE vendor SET | .
 
 494     qq|  vendornumber = ?, | .
 
 496     qq|  greeting = ?, | .
 
 497     qq|  department_1 = ?, | .
 
 498     qq|  department_2 = ?, | .
 
 503     qq|  homepage = ?, | .
 
 512     qq|  discount = ?, | .
 
 513     qq|  creditlimit = ?, | .
 
 514     qq|  business_id = ?, | .
 
 515     qq|  taxnumber = ?, | .
 
 516     qq|  language = ?, | .
 
 517     qq|  account_number = ?, | .
 
 518     qq|  bank_code = ?, | .
 
 522     qq|  obsolete = ?, | .
 
 523     qq|  direct_debit = ?, | .
 
 525     qq|  payment_id = ?, | .
 
 526     qq|  taxzone_id = ?, | .
 
 527     qq|  language_id = ?, | .
 
 528     qq|  username = ?, | .
 
 529     qq|  user_password = ?, | .
 
 530     qq|  v_customer_id = ? | .
 
 533     $form->{vendornumber},
 
 536     $form->{department_1},
 
 537     $form->{department_2},
 
 550     conv_i($form->{terms}),
 
 552     $form->{creditlimit},
 
 553     conv_i($form->{business}),
 
 556     $form->{account_number},
 
 561     $form->{obsolete} ? 't' : 'f',
 
 562     $form->{direct_debit} ? 't' : 'f',
 
 564     conv_i($form->{payment_id}),
 
 565     conv_i($form->{taxzone_id}, 0),
 
 566     conv_i( $form->{language_id}),
 
 568     $form->{user_password},
 
 569     $form->{v_customer_id},
 
 572   do_query($form, $dbh, $query, @values);
 
 575   if ( $form->{cp_id} ) {
 
 576     $query = qq|UPDATE contacts SET | .
 
 578       qq|cp_givenname = ?, | .
 
 581       qq|cp_phone1 = ?, | .
 
 582       qq|cp_phone2 = ?, | .
 
 583       qq|cp_abteilung = ?, | .
 
 585       qq|cp_mobile1 = ?, | .
 
 586       qq|cp_mobile2 = ?, | .
 
 587       qq|cp_satphone = ?, | .
 
 588       qq|cp_satfax = ?, | .
 
 589       qq|cp_project = ?, | .
 
 590       qq|cp_privatphone = ?, | .
 
 591       qq|cp_privatemail = ?, | .
 
 592       qq|cp_birthday = ?, | .
 
 597       $form->{cp_givenname},
 
 602       $form->{cp_abteilung},
 
 606       $form->{cp_satphone},
 
 609       $form->{cp_privatphone},
 
 610       $form->{cp_privatemail},
 
 611       $form->{cp_birthday},
 
 612       $form->{cp_gender} eq 'f' ? 'f' : 'm',
 
 615   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 617       qq|INSERT INTO contacts ( cp_cv_id, cp_title, cp_givenname,  | .
 
 618       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
 
 619       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
 
 620       qq|  cp_birthday, cp_gender) | .
 
 621       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 625       $form->{cp_givenname},
 
 630       $form->{cp_abteilung},
 
 634       $form->{cp_satphone},
 
 637       $form->{cp_privatphone},
 
 638       $form->{cp_privatemail},
 
 639       $form->{cp_birthday},
 
 643   do_query($form, $dbh, $query, @values) if ($query);
 
 646   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 648   $self->_save_note('dbh' => $dbh);
 
 649   $self->_delete_selected_notes('dbh' => $dbh);
 
 651   CVar->save_custom_variables('dbh'       => $dbh,
 
 653                               'trans_id'  => $form->{id},
 
 654                               'variables' => $form);
 
 656   $rc = $dbh->commit();
 
 659   $main::lxdebug->leave_sub();
 
 664   $main::lxdebug->enter_sub();
 
 666   my ( $self, $myconfig, $form ) = @_;
 
 667   # connect to database
 
 668   my $dbh = $form->dbconnect($myconfig);
 
 671   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 672   my $query = qq|DELETE FROM $cv WHERE id = ?|;
 
 673   do_query($form, $dbh, $query, $form->{id});
 
 677   $main::lxdebug->leave_sub();
 
 681   $main::lxdebug->enter_sub();
 
 683   my ( $self, $myconfig, $form ) = @_;
 
 685   # connect to database
 
 686   my $dbh = $form->dbconnect($myconfig);
 
 688   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 693   my %allowed_sort_columns =
 
 694     map({ $_, 1 } qw(id customernumber vendornumber name contact phone fax email
 
 695                      taxnumber business invnumber ordnumber quonumber));
 
 696   $sortorder    = $form->{sort} && $allowed_sort_columns{$form->{sort}} ? $form->{sort} : "name";
 
 697   $form->{sort} = $sortorder;
 
 698   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
 
 700   if ($sortorder ne 'id' && 1 >= scalar grep { $form->{$_} } qw(l_ordnumber l_quonumber l_invnumber)) {
 
 701     $sortorder  = "lower($sortorder) ${sortdir}";
 
 703     $sortorder .= " ${sortdir}";
 
 706   if ($form->{"${cv}number"}) {
 
 707     $where .= " AND ct.${cv}number ILIKE ?";
 
 708     push(@values, '%' . $form->{"${cv}number"} . '%');
 
 711   foreach my $key (qw(name contact email)) {
 
 713       $where .= " AND ct.$key ILIKE ?";
 
 714       push(@values, '%' . $form->{$key} . '%');
 
 718   if ($form->{cp_name}) {
 
 719     $where .= " AND ct.id IN (SELECT cp_cv_id FROM contacts WHERE lower(cp_name) LIKE lower(?))";
 
 720     push @values, '%' . $form->{cp_name} . '%';
 
 723   if ($form->{addr_city}) {
 
 724     $where .= " AND ((lower(ct.city) LIKE lower(?))
 
 729                         WHERE (module = 'CT')
 
 730                           AND (lower(shiptocity) LIKE lower(?))
 
 733     push @values, ('%' . $form->{addr_city} . '%') x 2;
 
 736   if ( $form->{status} eq 'orphaned' ) {
 
 738       qq| AND ct.id NOT IN | .
 
 739       qq|   (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
 
 740     if ($cv eq 'customer') {
 
 742         qq| AND ct.id NOT IN | .
 
 743         qq| (SELECT a.customer_id FROM ar a, customer cv | .
 
 744         qq|  WHERE cv.id = a.customer_id)|;
 
 746     if ($cv eq 'vendor') {
 
 748         qq| AND ct.id NOT IN | .
 
 749         qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
 
 750         qq|  WHERE cv.id = a.vendor_id)|;
 
 752     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
 
 755   if ($form->{obsolete} eq "Y") {
 
 756     $where .= qq| AND obsolete|;
 
 757   } elsif ($form->{obsolete} eq "N") {
 
 758     $where .= qq| AND NOT obsolete|;
 
 761   if ($form->{business_id}) {
 
 762     $where .= qq| AND (business_id = ?)|;
 
 763     push(@values, conv_i($form->{business_id}));
 
 766   my ($cvar_where, @cvar_values) = CVar->build_filter_query('module'         => 'CT',
 
 767                                                             'trans_id_field' => 'ct.id',
 
 771     $where .= qq| AND ($cvar_where)|;
 
 772     push @values, @cvar_values;
 
 775   if ($form->{addr_street}) {
 
 776     $where .= qq| AND (street ILIKE ?)|;
 
 777     push @values, '%' . $form->{addr_street} . '%';
 
 780   if ($form->{addr_zipcode}) {
 
 781     $where .= qq| AND (zipcode ILIKE ?)|;
 
 782     push @values, $form->{addr_zipcode} . '%';
 
 786     qq|SELECT ct.*, b.description AS business | .
 
 788     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 791   my @saved_values = @values;
 
 792   # redo for invoices, orders and quotations
 
 793   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
 
 794     my ($ar, $union, $module);
 
 797     if ($form->{l_invnumber}) {
 
 798       my $ar = $cv eq 'customer' ? 'ar' : 'ap';
 
 799       my $module = $ar eq 'ar' ? 'is' : 'ir';
 
 802         qq|SELECT ct.*, b.description AS business, | .
 
 803         qq|  a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
 
 804         qq|  '$module' AS module, 'invoice' AS formtype, | .
 
 805         qq|  (a.amount = a.paid) AS closed | .
 
 807         qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
 
 808         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 809         qq|WHERE $where AND (a.invoice = '1')|;
 
 814     if ( $form->{l_ordnumber} ) {
 
 815       if ($union eq "UNION") {
 
 816         push(@values, @saved_values);
 
 820         qq|SELECT ct.*, b.description AS business,| .
 
 821         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 822         qq|  'oe' AS module, 'order' AS formtype, o.closed | .
 
 824         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 825         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 826         qq|WHERE $where AND (o.quotation = '0')|;
 
 831     if ( $form->{l_quonumber} ) {
 
 832       if ($union eq "UNION") {
 
 833         push(@values, @saved_values);
 
 837         qq|SELECT ct.*, b.description AS business, | .
 
 838         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 839         qq|  'oe' AS module, 'quotation' AS formtype, o.closed | .
 
 841         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 842         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 843         qq|WHERE $where AND (o.quotation = '1')|;
 
 847   $query .= qq| ORDER BY $sortorder|;
 
 849   $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
 
 851   $main::lxdebug->leave_sub();
 
 855   $main::lxdebug->enter_sub();
 
 857   my ( $self, $myconfig, $form ) = @_;
 
 858   my $dbh   = $form->dbconnect($myconfig);
 
 860     qq|SELECT * FROM contacts c | .
 
 861     qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
 
 862   my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
 
 863   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 865   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 867   $query = qq|SELECT COUNT(cp_id) AS used FROM (
 
 868     SELECT cp_id FROM oe UNION
 
 869     SELECT cp_id FROM ar UNION
 
 870     SELECT cp_id FROM ap UNION
 
 871     SELECT cp_id FROM delivery_orders
 
 872   ) AS cpid WHERE cp_id = ? OR ? = 0|;
 
 873   ($form->{cp_used}) = selectfirst_array_query($form, $dbh, $query, ($form->{cp_id})x2);
 
 878   $main::lxdebug->leave_sub();
 
 882   $main::lxdebug->enter_sub();
 
 884   my ( $self, $myconfig, $form ) = @_;
 
 885   my $dbh   = $form->dbconnect($myconfig);
 
 886   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
 887   my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
 
 889   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 891   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 893   $query = qq|SELECT COUNT(shipto_id) AS used FROM (
 
 894     SELECT shipto_id FROM oe UNION
 
 895     SELECT shipto_id FROM ar UNION
 
 896     SELECT shipto_id FROM delivery_orders
 
 897   ) AS stid WHERE shipto_id = ? OR ? = 0|;
 
 898   ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
 
 903   $main::lxdebug->leave_sub();
 
 907   $main::lxdebug->enter_sub();
 
 909   my ( $self, $myconfig, $form ) = @_;
 
 910   my $dbh = $form->dbconnect($myconfig);
 
 912   my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
 
 913   my $db = $form->{db} eq "customer" ? "customer" : "vendor";
 
 914   my $qty_sign = $form->{db} eq 'vendor' ? ' * -1 AS qty' : '';
 
 916   my $where = " WHERE 1=1 ";
 
 919   if ($form->{shipto_id} && ($arap eq "ar")) {
 
 920     $where .= "AND ${arap}.shipto_id = ?";
 
 921     push(@values, $form->{shipto_id});
 
 923     $where .= "AND ${arap}.${db}_id = ?";
 
 924     push(@values, $form->{id});
 
 928     $where .= "AND ${arap}.transdate >= ?";
 
 929     push(@values, conv_date($form->{from}));
 
 932     $where .= "AND ${arap}.transdate <= ?";
 
 933     push(@values, conv_date($form->{to}));
 
 936     qq|SELECT s.shiptoname, i.qty $qty_sign, | .
 
 937     qq|  ${arap}.id, ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
 
 938     qq|  i.description, i.unit, i.sellprice, | .
 
 939     qq|  oe.id AS oe_id | .
 
 941     qq|LEFT JOIN shipto s ON | .
 
 943      ? qq|(ar.shipto_id = s.shipto_id) |
 
 944      : qq|(ap.id = s.trans_id) |) .
 
 945     qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
 
 946     qq|LEFT join parts p ON (p.id = i.parts_id) | .
 
 947     qq|LEFT JOIN oe ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '') | .
 
 949     qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
 
 951   $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
 
 955   $main::lxdebug->leave_sub();
 
 959   $main::lxdebug->enter_sub();
 
 964   my $form   = $main::form;
 
 966   Common::check_params(\%params, 'dbh');
 
 968   if (!$form->{NOTE_subject}) {
 
 969     $main::lxdebug->leave_sub();
 
 973   my $dbh = $params{dbh};
 
 977     'id'           => $form->{NOTE_id},
 
 978     'subject'      => $form->{NOTE_subject},
 
 979     'body'         => $form->{NOTE_body},
 
 980     'trans_id'     => $form->{id},
 
 981     'trans_module' => 'ct',
 
 984   $note{id} = Notes->save(%note);
 
 986   if ($form->{FU_date}) {
 
 988       'id'               => $form->{FU_id},
 
 989       'note_id'          => $note{id},
 
 990       'follow_up_date'   => $form->{FU_date},
 
 991       'created_for_user' => $form->{FU_created_for_user},
 
 992       'done'             => $form->{FU_done} ? 1 : 0,
 
 993       'subject'          => $form->{NOTE_subject},
 
 994       'body'             => $form->{NOTE_body},
 
 997           'trans_id'     => $form->{id},
 
 998           'trans_type'   => $form->{db} eq 'customer' ? 'customer' : 'vendor',
 
 999           'trans_info'   => $form->{name},
 
1004     $follow_up{id} = FU->save(%follow_up);
 
1006   } elsif ($form->{FU_id}) {
 
1007     do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
 
1008     do_query($form, $dbh, qq|DELETE FROM follow_ups      WHERE id = ?|,           conv_i($form->{FU_id}));
 
1011   delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
 
1013   $main::lxdebug->leave_sub();
 
1016 sub _delete_selected_notes {
 
1017   $main::lxdebug->enter_sub();
 
1022   Common::check_params(\%params, 'dbh');
 
1024   my $form = $main::form;
 
1025   my $dbh  = $params{dbh};
 
1027   foreach my $i (1 .. $form->{NOTES_rowcount}) {
 
1028     next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
 
1030     Notes->delete('dbh' => $params{dbh},
 
1031                   'id'  => $form->{"NOTE_id_$i"});
 
1034   $main::lxdebug->leave_sub();
 
1038   $main::lxdebug->enter_sub();
 
1041   my $shipto_id = shift;
 
1043   my $form      = $main::form;
 
1044   my %myconfig  = %main::myconfig;
 
1045   my $dbh       = $form->get_standard_dbh(\%myconfig);
 
1047   do_query($form, $dbh, qq|UPDATE shipto SET trans_id = NULL WHERE shipto_id = ?|, $shipto_id);
 
1051   $main::lxdebug->leave_sub();
 
1055   $main::lxdebug->enter_sub();
 
1058   my $shipto_id = shift;
 
1060   my $form      = $main::form;
 
1061   my %myconfig  = %main::myconfig;
 
1062   my $dbh       = $form->get_standard_dbh(\%myconfig);
 
1064   do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $shipto_id);
 
1068   $main::lxdebug->leave_sub();
 
1072   $main::lxdebug->enter_sub();
 
1077   Common::check_params(\%params, qw(vc id));
 
1079   my $myconfig = \%main::myconfig;
 
1080   my $form     = $main::form;
 
1082   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
1084   my $table        = $params{vc} eq 'customer' ? 'customer' : 'vendor';
 
1085   my @ids          = ref $params{id} eq 'ARRAY' ? @{ $params{id} } : ($params{id});
 
1086   my $placeholders = ('?') x scalar @ids;
 
1087   my $query        = qq|SELECT id, name, account_number, bank, bank_code, iban, bic
 
1089                         WHERE id IN (${placeholders})|;
 
1091   my $result       = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
 
1093   if (ref $params{id} eq 'ARRAY') {
 
1094     $result = { map { $_->{id} => $_ } @{ $result } };
 
1096     $result = $result->[0] || { 'id' => $params{id} };
 
1099   $main::lxdebug->leave_sub();