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";
 
 130     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
 
 131     qq|WHERE ct.id = ? | .
 
 135     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
 
 137   my ($dummy) = selectrow_query($form, $dbh, $query, $form->{id}, $form->{id});
 
 138   $form->{status} = "orphaned" unless ($dummy);
 
 142   $main::lxdebug->leave_sub();
 
 145 sub populate_drop_down_boxes {
 
 146   $main::lxdebug->enter_sub();
 
 148   my ($self, $myconfig, $form, $provided_dbh) = @_;
 
 150   my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
 
 153   $query = qq|SELECT id, description FROM business ORDER BY id|;
 
 154   $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
 
 158     qq|SELECT shipto_id, shiptoname, shiptodepartment_1, shiptostreet, shiptocity
 
 160        WHERE (trans_id = ?) AND (module = 'CT')|;
 
 161   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
 
 164   $query  = qq|SELECT cp_id, cp_name, cp_givenname FROM contacts WHERE cp_cv_id = ? ORDER BY cp_name|;
 
 165   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
 
 168   $query = qq|SELECT id, description FROM language ORDER BY id|;
 
 169   $form->{languages} = selectall_hashref_query($form, $dbh, $query);
 
 172   $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
 
 173   $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
 
 175   $dbh->disconnect() unless ($provided_dbh);
 
 177   $main::lxdebug->leave_sub();
 
 180 sub query_titles_and_greetings {
 
 181   $main::lxdebug->enter_sub();
 
 183   my ( $self, $myconfig, $form ) = @_;
 
 186   my $dbh = $form->dbconnect($myconfig);
 
 189     qq|SELECT DISTINCT(cp_greeting) | .
 
 191     qq|WHERE cp_greeting ~ '[a-zA-Z]' | .
 
 192     qq|ORDER BY cp_greeting|;
 
 193   $form->{GREETINGS} = [ selectall_array_query($form, $dbh, $query) ];
 
 196     qq|SELECT DISTINCT(greeting) | .
 
 198     qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 200     qq|SELECT DISTINCT(greeting) | .
 
 202     qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 203     qq|ORDER BY greeting|;
 
 205   map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
 
 206   $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
 
 209     qq|SELECT DISTINCT(cp_title) | .
 
 211     qq|WHERE cp_title ~ '[a-zA-Z]'|;
 
 212   $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
 
 215     qq|SELECT DISTINCT(cp_abteilung) | .
 
 217     qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
 
 218   $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
 
 221   $main::lxdebug->leave_sub();
 
 225   $main::lxdebug->enter_sub();
 
 227   my ( $self, $myconfig, $form ) = @_;
 
 229   # set pricegroup to default
 
 230   $form->{klass} = 0 unless ($form->{klass});
 
 232   # connect to database
 
 233   my $dbh = $form->dbconnect_noauto($myconfig);
 
 236     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 237     if ( $form->{"selected_cp_${_}"} );
 
 238        } qw(title greeting abteilung) );
 
 239   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 240   if ( $form->{"selected_company_greeting"} );
 
 242   # assign value discount, terms, creditlimit
 
 243   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 244   $form->{discount} /= 100;
 
 245   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 247   my ( $query, $sth, $f_id );
 
 250     $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
 
 251     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
 
 253     if (($f_id ne $form->{id}) && ($f_id ne "")) {
 
 254       $main::lxdebug->leave_sub();
 
 259     if (!$form->{customernumber} && $form->{business}) {
 
 260       $form->{customernumber} =
 
 261         $form->update_business($myconfig, $form->{business}, $dbh);
 
 263     if (!$form->{customernumber}) {
 
 264       $form->{customernumber} =
 
 265         $form->update_defaults($myconfig, "customernumber", $dbh);
 
 268     $query  = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
 
 269     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
 
 271       $main::lxdebug->leave_sub();
 
 275     $query = qq|SELECT nextval('id')|;
 
 276     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 278     $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
 
 279     do_query($form, $dbh, $query, $form->{id});
 
 282   $query = qq|UPDATE customer SET | .
 
 283     qq|customernumber = ?, | .
 
 286     qq|department_1 = ?, | .
 
 287     qq|department_2 = ?, | .
 
 301     qq|creditlimit = ?, | .
 
 303     qq|business_id = ?, | .
 
 304     qq|taxnumber = ?, | .
 
 307     qq|account_number = ?, | .
 
 308     qq|bank_code = ?, | .
 
 313     qq|salesman_id = ?, | .
 
 314     qq|language_id = ?, | .
 
 315     qq|payment_id = ?, | .
 
 316     qq|taxzone_id = ?, | .
 
 317     qq|user_password = ?, | .
 
 318     qq|c_vendor_id = ?, | .
 
 322     $form->{customernumber},
 
 325     $form->{department_1},
 
 326     $form->{department_2},
 
 340     $form->{creditlimit},
 
 341     conv_i($form->{terms}),
 
 342     conv_i($form->{business}),
 
 346     $form->{account_number},
 
 349     $form->{obsolete} ? 't' : 'f',
 
 352     conv_i($form->{salesman_id}),
 
 353     conv_i($form->{language_id}),
 
 354     conv_i($form->{payment_id}),
 
 355     conv_i($form->{taxzone_id}, 0),
 
 356     $form->{user_password},
 
 357     $form->{c_vendor_id},
 
 358     conv_i($form->{klass}),
 
 361   do_query( $form, $dbh, $query, @values );
 
 364   if ( $form->{cp_id} ) {
 
 365     $query = qq|UPDATE contacts SET | .
 
 366       qq|cp_greeting = ?, | .
 
 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 = ? | .
 
 385       $form->{cp_greeting},
 
 387       $form->{cp_givenname},
 
 392       $form->{cp_abteilung},
 
 396       $form->{cp_satphone},
 
 399       $form->{cp_privatphone},
 
 400       $form->{cp_privatemail},
 
 401       $form->{cp_birthday},
 
 404   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 406       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname,  | .
 
 407       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
 
 408       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
 
 410       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 413       $form->{cp_greeting},
 
 415       $form->{cp_givenname},
 
 420       $form->{cp_abteilung},
 
 424       $form->{cp_satphone},
 
 427       $form->{cp_privatphone},
 
 428       $form->{cp_privatemail},
 
 432   do_query( $form, $dbh, $query, @values ) if ($query);
 
 435   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 437   $self->_save_note('dbh' => $dbh);
 
 438   $self->_delete_selected_notes('dbh' => $dbh);
 
 440   CVar->save_custom_variables('dbh'       => $dbh,
 
 442                               'trans_id'  => $form->{id},
 
 443                               'variables' => $form);
 
 445   $rc = $dbh->commit();
 
 448   $main::lxdebug->leave_sub();
 
 453   $main::lxdebug->enter_sub();
 
 455   my ( $self, $myconfig, $form ) = @_;
 
 457   $form->{taxzone_id} *= 1;
 
 458   # connect to database
 
 459   my $dbh = $form->dbconnect_noauto($myconfig);
 
 462     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 463     if ( $form->{"selected_cp_${_}"} );
 
 464        } qw(title greeting abteilung) );
 
 465   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 466   if ( $form->{"selected_company_greeting"} );
 
 468   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 469   $form->{discount} /= 100;
 
 470   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 475     $query = qq|SELECT nextval('id')|;
 
 476     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 478     $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
 
 479     do_query($form, $dbh, $query, $form->{id});
 
 481     if ( !$form->{vendornumber} ) {
 
 482       $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
 
 487     qq|UPDATE vendor SET | .
 
 488     qq|  vendornumber = ?, | .
 
 490     qq|  greeting = ?, | .
 
 491     qq|  department_1 = ?, | .
 
 492     qq|  department_2 = ?, | .
 
 497     qq|  homepage = ?, | .
 
 506     qq|  discount = ?, | .
 
 507     qq|  creditlimit = ?, | .
 
 508     qq|  business_id = ?, | .
 
 509     qq|  taxnumber = ?, | .
 
 510     qq|  sic_code = ?, | .
 
 511     qq|  language = ?, | .
 
 512     qq|  account_number = ?, | .
 
 513     qq|  bank_code = ?, | .
 
 515     qq|  obsolete = ?, | .
 
 517     qq|  payment_id = ?, | .
 
 518     qq|  taxzone_id = ?, | .
 
 519     qq|  language_id = ?, | .
 
 520     qq|  username = ?, | .
 
 521     qq|  user_password = ?, | .
 
 522     qq|  v_customer_id = ? | .
 
 525     $form->{vendornumber},
 
 528     $form->{department_1},
 
 529     $form->{department_2},
 
 542     conv_i($form->{terms}),
 
 544     $form->{creditlimit},
 
 545     conv_i($form->{business}),
 
 549     $form->{account_number},
 
 552     $form->{obsolete} ? 't' : 'f',
 
 554     conv_i($form->{payment_id}),
 
 555     conv_i($form->{taxzone_id}, 0),
 
 556     conv_i( $form->{language_id}),
 
 558     $form->{user_password},
 
 559     $form->{v_customer_id},
 
 562   do_query($form, $dbh, $query, @values);
 
 565   if ( $form->{cp_id} ) {
 
 566     $query = qq|UPDATE contacts SET | .
 
 567       qq|cp_greeting = ?, | .
 
 569       qq|cp_givenname = ?, | .
 
 572       qq|cp_phone1 = ?, | .
 
 573       qq|cp_phone2 = ?, | .
 
 574       qq|cp_abteilung = ?, | .
 
 576       qq|cp_mobile1 = ?, | .
 
 577       qq|cp_mobile2 = ?, | .
 
 578       qq|cp_satphone = ?, | .
 
 579       qq|cp_satfax = ?, | .
 
 580       qq|cp_project = ?, | .
 
 581       qq|cp_privatphone = ?, | .
 
 582       qq|cp_privatemail = ?, | .
 
 583       qq|cp_birthday = ? | .
 
 586       $form->{cp_greeting},
 
 588       $form->{cp_givenname},
 
 593       $form->{cp_abteilung},
 
 597       $form->{cp_satphone},
 
 600       $form->{cp_privatphone},
 
 601       $form->{cp_privatemail},
 
 602       $form->{cp_birthday},
 
 605   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 607       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname,  | .
 
 608       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
 
 609       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
 
 611       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 614       $form->{cp_greeting},
 
 616       $form->{cp_givenname},
 
 621       $form->{cp_abteilung},
 
 625       $form->{cp_satphone},
 
 628       $form->{cp_privatphone},
 
 629       $form->{cp_privatemail},
 
 633   do_query($form, $dbh, $query, @values) if ($query);
 
 636   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 638   $self->_save_note('dbh' => $dbh);
 
 639   $self->_delete_selected_notes('dbh' => $dbh);
 
 641   CVar->save_custom_variables('dbh'       => $dbh,
 
 643                               'trans_id'  => $form->{id},
 
 644                               'variables' => $form);
 
 646   $rc = $dbh->commit();
 
 649   $main::lxdebug->leave_sub();
 
 654   $main::lxdebug->enter_sub();
 
 656   my ( $self, $myconfig, $form ) = @_;
 
 657   # connect to database
 
 658   my $dbh = $form->dbconnect($myconfig);
 
 661   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 662   my $query = qq|DELETE FROM $cv WHERE id = ?|;
 
 663   do_query($form, $dbh, $query, $form->{id});
 
 667   $main::lxdebug->leave_sub();
 
 671   $main::lxdebug->enter_sub();
 
 673   my ( $self, $myconfig, $form ) = @_;
 
 675   # connect to database
 
 676   my $dbh = $form->dbconnect($myconfig);
 
 678   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 683   my %allowed_sort_columns =
 
 684     map({ $_, 1 } qw(id customernumber vendornumber name address contact phone fax email
 
 685                      taxnumber sic_code business invnumber ordnumber quonumber));
 
 686   $sortorder    = $form->{sort} && $allowed_sort_columns{$form->{sort}} ? $form->{sort} : "name";
 
 687   $form->{sort} = $sortorder;
 
 688   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
 
 690   if ($sortorder eq "address") {
 
 691     $sortorder  = "lower(country) ${sortdir}, lower(city) ${sortdir}, lower(street) ${sortdir}";
 
 692   } elsif ($sortorder ne 'id') {
 
 693     $sortorder  = "lower($sortorder) ${sortdir}";
 
 695     $sortorder .= " ${sortdir}";
 
 698   if ($form->{"${cv}number"}) {
 
 699     $where .= " AND ct.${cv}number ILIKE ?";
 
 700     push(@values, '%' . $form->{"${cv}number"} . '%');
 
 703   foreach my $key (qw(name contact email)) {
 
 705       $where .= " AND ct.$key ILIKE ?";
 
 706       push(@values, '%' . $form->{$key} . '%');
 
 710   if ( $form->{status} eq 'orphaned' ) {
 
 712       qq| AND ct.id NOT IN | .
 
 713       qq|   (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
 
 714     if ($cv eq 'customer') {
 
 716         qq| AND ct.id NOT IN | .
 
 717         qq| (SELECT a.customer_id FROM ar a, customer cv | .
 
 718         qq|  WHERE cv.id = a.customer_id)|;
 
 720     if ($cv eq 'vendor') {
 
 722         qq| AND ct.id NOT IN | .
 
 723         qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
 
 724         qq|  WHERE cv.id = a.vendor_id)|;
 
 726     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
 
 729   if ($form->{obsolete} eq "Y") {
 
 730     $where .= qq| AND obsolete|;
 
 731   } elsif ($form->{obsolete} eq "N") {
 
 732     $where .= qq| AND NOT obsolete|;
 
 735   if ($form->{business_id}) {
 
 736     $where .= qq| AND (business_id = ?)|;
 
 737     push(@values, conv_i($form->{business_id}));
 
 740   my ($cvar_where, @cvar_values) = CVar->build_filter_query('module'         => 'CT',
 
 741                                                             'trans_id_field' => 'ct.id',
 
 745     $where .= qq| AND ($cvar_where)|;
 
 746     push @values, @cvar_values;
 
 750     qq|SELECT ct.*, b.description AS business | .
 
 752     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 755   my @saved_values = @values;
 
 756   # redo for invoices, orders and quotations
 
 757   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
 
 758     my ($ar, $union, $module);
 
 761     if ($form->{l_invnumber}) {
 
 762       my $ar = $cv eq 'customer' ? 'ar' : 'ap';
 
 763       my $module = $ar eq 'ar' ? 'is' : 'ir';
 
 766         qq|SELECT ct.*, b.description AS business, | .
 
 767         qq|  a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
 
 768         qq|  '$module' AS module, 'invoice' AS formtype, | .
 
 769         qq|  (a.amount = a.paid) AS closed | .
 
 771         qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
 
 772         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 773         qq|WHERE $where AND (a.invoice = '1')|;
 
 778     if ( $form->{l_ordnumber} ) {
 
 779       if ($union eq "UNION") {
 
 780         push(@values, @saved_values);
 
 784         qq|SELECT ct.*, b.description AS business,| .
 
 785         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 786         qq|  'oe' AS module, 'order' AS formtype, o.closed | .
 
 788         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 789         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 790         qq|WHERE $where AND (o.quotation = '0')|;
 
 795     if ( $form->{l_quonumber} ) {
 
 796       if ($union eq "UNION") {
 
 797         push(@values, @saved_values);
 
 801         qq|SELECT ct.*, b.description AS business, | .
 
 802         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 803         qq|  'oe' AS module, 'quotation' AS formtype, o.closed | .
 
 805         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 806         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 807         qq|WHERE $where AND (o.quotation = '1')|;
 
 811   $query .= qq| ORDER BY $sortorder|;
 
 813   $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
 
 814   map({ my $ref = $_; $ref->{address} = join(" ", map({ $ref->{$_} } qw(street zipcode city country))); }
 
 817   $main::lxdebug->leave_sub();
 
 821   $main::lxdebug->enter_sub();
 
 823   my ( $self, $myconfig, $form ) = @_;
 
 824   my $dbh   = $form->dbconnect($myconfig);
 
 826     qq|SELECT * FROM contacts c | .
 
 827     qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
 
 828   my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
 
 829   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 831   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 833   $query = qq|SELECT COUNT(cp_id) AS used FROM (
 
 834     SELECT cp_id FROM oe UNION
 
 835     SELECT cp_id FROM ar UNION
 
 836     SELECT cp_id FROM ap UNION
 
 837     SELECT cp_id FROM delivery_orders
 
 838   ) AS cpid WHERE cp_id = ? OR ? = 0|;
 
 839   ($form->{cp_used}) = selectfirst_array_query($form, $dbh, $query, ($form->{cp_id})x2);
 
 844   $main::lxdebug->leave_sub();
 
 848   $main::lxdebug->enter_sub();
 
 850   my ( $self, $myconfig, $form ) = @_;
 
 851   my $dbh   = $form->dbconnect($myconfig);
 
 852   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
 853   my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
 
 855   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 857   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 859   $query = qq|SELECT COUNT(shipto_id) AS used FROM (
 
 860     SELECT shipto_id FROM oe UNION
 
 861     SELECT shipto_id FROM ar UNION
 
 862     SELECT shipto_id FROM delivery_orders
 
 863   ) AS stid WHERE shipto_id = ? OR ? = 0|;
 
 864   ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
 
 869   $main::lxdebug->leave_sub();
 
 873   $main::lxdebug->enter_sub();
 
 875   my ( $self, $myconfig, $form ) = @_;
 
 876   my $dbh = $form->dbconnect($myconfig);
 
 878   my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
 
 879   my $db = $form->{db} eq "customer" ? "customer" : "vendor";
 
 881   my $where = " WHERE 1=1 ";
 
 884   if ($form->{shipto_id} && ($arap eq "ar")) {
 
 885     $where .= "AND ${arap}.shipto_id = ?";
 
 886     push(@values, $form->{shipto_id});
 
 888     $where .= "AND ${arap}.${db}_id = ?";
 
 889     push(@values, $form->{id});
 
 893     $where .= "AND ${arap}.transdate >= ?";
 
 894     push(@values, conv_date($form->{from}));
 
 897     $where .= "AND ${arap}.transdate <= ?";
 
 898     push(@values, conv_date($form->{to}));
 
 901     qq|SELECT s.shiptoname, i.qty, | .
 
 902     qq|  ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
 
 903     qq|  i.description, i.unit, i.sellprice | .
 
 905     qq|LEFT JOIN shipto s ON | .
 
 907      ? qq|(ar.shipto_id = s.shipto_id) |
 
 908      : qq|(ap.id = s.trans_id) |) .
 
 909     qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
 
 910     qq|LEFT join parts p ON (p.id = i.parts_id) | .
 
 912     qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
 
 914   $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
 
 918   $main::lxdebug->leave_sub();
 
 922   $main::lxdebug->enter_sub();
 
 927   my $form   = $main::form;
 
 929   Common::check_params(\%params, 'dbh');
 
 931   if (!$form->{NOTE_subject}) {
 
 932     $main::lxdebug->leave_sub();
 
 936   my $dbh = $params{dbh};
 
 940     'id'           => $form->{NOTE_id},
 
 941     'subject'      => $form->{NOTE_subject},
 
 942     'body'         => $form->{NOTE_body},
 
 943     'trans_id'     => $form->{id},
 
 944     'trans_module' => 'ct',
 
 947   $note{id} = Notes->save(%note);
 
 949   if ($form->{FU_date}) {
 
 951       'id'               => $form->{FU_id},
 
 952       'note_id'          => $note{id},
 
 953       'follow_up_date'   => $form->{FU_date},
 
 954       'created_for_user' => $form->{FU_created_for_user},
 
 955       'done'             => $form->{FU_done} ? 1 : 0,
 
 956       'subject'          => $form->{NOTE_subject},
 
 957       'body'             => $form->{NOTE_body},
 
 960           'trans_id'     => $form->{id},
 
 961           'trans_type'   => $form->{db} eq 'customer' ? 'customer' : 'vendor',
 
 962           'trans_info'   => $form->{name},
 
 967     $follow_up{id} = FU->save(%follow_up);
 
 969   } elsif ($form->{FU_id}) {
 
 970     do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
 
 971     do_query($form, $dbh, qq|DELETE FROM follow_ups      WHERE id = ?|,           conv_i($form->{FU_id}));
 
 974   delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
 
 976   $main::lxdebug->leave_sub();
 
 979 sub _delete_selected_notes {
 
 980   $main::lxdebug->enter_sub();
 
 985   Common::check_params(\%params, 'dbh');
 
 987   my $form = $main::form;
 
 988   my $dbh  = $params{dbh};
 
 990   foreach my $i (1 .. $form->{NOTES_rowcount}) {
 
 991     next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
 
 993     Notes->delete('dbh' => $params{dbh},
 
 994                   'id'  => $form->{"NOTE_id_$i"});
 
 997   $main::lxdebug->leave_sub();
 
1001   $main::lxdebug->enter_sub();
 
1004   my $shipto_id = shift;
 
1006   my $form      = $main::form;
 
1007   my %myconfig  = %main::myconfig;
 
1008   my $dbh       = $form->get_standard_dbh(\%myconfig);
 
1010   do_query($form, $dbh, qq|UPDATE shipto SET trans_id = NULL WHERE shipto_id = ?|, $shipto_id);
 
1014   $main::lxdebug->leave_sub();
 
1018   $main::lxdebug->enter_sub();
 
1021   my $shipto_id = shift;
 
1023   my $form      = $main::form;
 
1024   my %myconfig  = %main::myconfig;
 
1025   my $dbh       = $form->get_standard_dbh(\%myconfig);
 
1027   do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $shipto_id);
 
1031   $main::lxdebug->leave_sub();