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 #======================================================================
 
  43   $main::lxdebug->enter_sub();
 
  45   my ( $self, $myconfig, $form ) = @_;
 
  47   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
  49   my $dbh   = $form->dbconnect($myconfig);
 
  51     qq|SELECT ct.*, b.id AS business, cp.* | .
 
  53     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
  54     qq|LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id) | .
 
  55     qq|WHERE (ct.id = ?) | .
 
  56     qq|ORDER BY cp.cp_id LIMIT 1|;
 
  57   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
 
  59   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
  61   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
  64   if ( $form->{salesman_id} ) {
 
  66       qq|SELECT ct.name AS salesman | .
 
  70       selectrow_query($form, $dbh, $query, $form->{salesman_id});
 
  73   # check if it is orphaned
 
  74   my $arap = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
 
  78     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
 
  79     qq|WHERE ct.id = ? | .
 
  83     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
 
  85   my ($dummy) = selectrow_query($form, $dbh, $query, $form->{id}, $form->{id});
 
  86   $form->{status} = "orphaned" unless ($dummy);
 
  90   $main::lxdebug->leave_sub();
 
  93 sub populate_drop_down_boxes {
 
  94   $main::lxdebug->enter_sub();
 
  96   my ($self, $myconfig, $form, $provided_dbh) = @_;
 
  98   my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
 
 101   $query = qq|SELECT id, description FROM business ORDER BY id|;
 
 102   $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
 
 106     qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
 
 107     qq|FROM shipto WHERE (trans_id = ?) AND (module = 'CT')|;
 
 108   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
 
 111   $query  = qq|SELECT cp_id, cp_name FROM contacts WHERE cp_cv_id = ?|;
 
 112   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
 
 115   $query = qq|SELECT id, description FROM language ORDER BY id|;
 
 116   $form->{languages} = selectall_hashref_query($form, $dbh, $query);
 
 119   $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
 
 120   $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
 
 122   $dbh->disconnect() unless ($provided_dbh);
 
 124   $main::lxdebug->leave_sub();
 
 127 sub query_titles_and_greetings {
 
 128   $main::lxdebug->enter_sub();
 
 130   my ( $self, $myconfig, $form ) = @_;
 
 133   my $dbh = $form->dbconnect($myconfig);
 
 136     qq|SELECT DISTINCT(cp_greeting) | .
 
 138     qq|WHERE cp_greeting ~ '[a-zA-Z]' | .
 
 139     qq|ORDER BY cp_greeting|;
 
 140   $form->{GREETINGS} = [ selectall_array_query($form, $dbh, $query) ];
 
 143     qq|SELECT DISTINCT(greeting) | .
 
 145     qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 147     qq|SELECT DISTINCT(greeting) | .
 
 149     qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 150     qq|ORDER BY greeting|;
 
 152   map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
 
 153   $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
 
 156     qq|SELECT DISTINCT(cp_title) | .
 
 158     qq|WHERE cp_title ~ '[a-zA-Z]'|;
 
 159   $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
 
 162     qq|SELECT DISTINCT(cp_abteilung) | .
 
 164     qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
 
 165   $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
 
 168   $main::lxdebug->leave_sub();
 
 172   $main::lxdebug->enter_sub();
 
 174   my ( $self, $myconfig, $form ) = @_;
 
 176   # set pricegroup to default
 
 177   $form->{klass} = 0 unless ($form->{klass});
 
 179   # connect to database
 
 180   my $dbh = $form->dbconnect_noauto($myconfig);
 
 183     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 184     if ( $form->{"selected_cp_${_}"} );
 
 185        } qw(title greeting abteilung) );
 
 186   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 187   if ( $form->{"selected_company_greeting"} );
 
 189   # assign value discount, terms, creditlimit
 
 190   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 191   $form->{discount} /= 100;
 
 192   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 194   my ( $query, $sth, $f_id );
 
 197     $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
 
 198     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
 
 200     if (($f_id ne $form->{id}) && ($f_id ne "")) {
 
 201       $main::lxdebug->leave_sub();
 
 206     if (!$form->{customernumber} && $form->{business}) {
 
 207       $form->{customernumber} =
 
 208         $form->update_business($myconfig, $form->{business}, $dbh);
 
 210     if (!$form->{customernumber}) {
 
 211       $form->{customernumber} =
 
 212         $form->update_defaults($myconfig, "customernumber", $dbh);
 
 215     $query  = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
 
 216     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
 
 218       $main::lxdebug->leave_sub();
 
 222     $query = qq|SELECT nextval('id')|;
 
 223     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 225     $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
 
 226     do_query($form, $dbh, $query, $form->{id});
 
 229   $query = qq|UPDATE customer SET | .
 
 230     qq|customernumber = ?, | .
 
 233     qq|department_1 = ?, | .
 
 234     qq|department_2 = ?, | .
 
 248     qq|creditlimit = ?, | .
 
 250     qq|business_id = ?, | .
 
 251     qq|taxnumber = ?, | .
 
 254     qq|account_number = ?, | .
 
 255     qq|bank_code = ?, | .
 
 260     qq|salesman_id = ?, | .
 
 261     qq|language_id = ?, | .
 
 262     qq|payment_id = ?, | .
 
 263     qq|taxzone_id = ?, | .
 
 264     qq|user_password = ?, | .
 
 265     qq|c_vendor_id = ?, | .
 
 269     $form->{customernumber},
 
 272     $form->{department_1},
 
 273     $form->{department_2},
 
 287     $form->{creditlimit},
 
 288     conv_i($form->{terms}),
 
 289     conv_i($form->{business}),
 
 293     $form->{account_number},
 
 296     $form->{obsolete} ? 't' : 'f',
 
 299     conv_i($form->{salesman_id}),
 
 300     conv_i($form->{language_id}),
 
 301     conv_i($form->{payment_id}),
 
 302     conv_i($form->{taxzone_id}, 0),
 
 303     $form->{user_password},
 
 304     $form->{c_vendor_id},
 
 305     conv_i($form->{klass}),
 
 308   do_query( $form, $dbh, $query, @values );
 
 311   if ( $form->{cp_id} ) {
 
 312     $query = qq|UPDATE contacts SET | .
 
 313       qq|cp_greeting = ?, | .
 
 315       qq|cp_givenname = ?, | .
 
 318       qq|cp_phone1 = ?, | .
 
 319       qq|cp_phone2 = ?, | .
 
 320       qq|cp_abteilung = ?, | .
 
 322       qq|cp_mobile1 = ?, | .
 
 323       qq|cp_mobile2 = ?, | .
 
 324       qq|cp_satphone = ?, | .
 
 325       qq|cp_satfax = ?, | .
 
 326       qq|cp_project = ?, | .
 
 327       qq|cp_privatphone = ?, | .
 
 328       qq|cp_privatemail = ?, | .
 
 329       qq|cp_birthday = ? | .
 
 332       $form->{cp_greeting},
 
 334       $form->{cp_givenname},
 
 339       $form->{cp_abteilung},
 
 343       $form->{cp_satphone},
 
 346       $form->{cp_privatphone},
 
 347       $form->{cp_privatemail},
 
 348       $form->{cp_birthday},
 
 351   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 353       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname,  | .
 
 354       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
 
 355       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
 
 357       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 360       $form->{cp_greeting},
 
 362       $form->{cp_givenname},
 
 367       $form->{cp_abteilung},
 
 371       $form->{cp_satphone},
 
 374       $form->{cp_privatphone},
 
 375       $form->{cp_privatemail},
 
 379   do_query( $form, $dbh, $query, @values ) if ($query);
 
 382   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 384   $rc = $dbh->commit();
 
 387   $main::lxdebug->leave_sub();
 
 392   $main::lxdebug->enter_sub();
 
 394   my ( $self, $myconfig, $form ) = @_;
 
 396   $form->{taxzone_id} *= 1;
 
 397   # connect to database
 
 398   my $dbh = $form->dbconnect_noauto($myconfig);
 
 401     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 402     if ( $form->{"selected_cp_${_}"} );
 
 403        } qw(title greeting abteilung) );
 
 404   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 405   if ( $form->{"selected_company_greeting"} );
 
 407   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 408   $form->{discount} /= 100;
 
 409   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 414     $query = qq|DELETE FROM shipto WHERE (trans_id = ?) AND (module = 'CT')|;
 
 415     do_query($form, $dbh, $query, $form->{id});
 
 418     $query = qq|SELECT nextval('id')|;
 
 419     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 421     $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
 
 422     do_query($form, $dbh, $query, $form->{id});
 
 424     if ( !$form->{vendornumber} ) {
 
 425       $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
 
 430     qq|UPDATE vendor SET | .
 
 431     qq|  vendornumber = ?, | .
 
 433     qq|  greeting = ?, | .
 
 434     qq|  department_1 = ?, | .
 
 435     qq|  department_2 = ?, | .
 
 440     qq|  homepage = ?, | .
 
 449     qq|  discount = ?, | .
 
 450     qq|  creditlimit = ?, | .
 
 451     qq|  business_id = ?, | .
 
 452     qq|  taxnumber = ?, | .
 
 453     qq|  sic_code = ?, | .
 
 454     qq|  language = ?, | .
 
 455     qq|  account_number = ?, | .
 
 456     qq|  bank_code = ?, | .
 
 458     qq|  obsolete = ?, | .
 
 460     qq|  payment_id = ?, | .
 
 461     qq|  taxzone_id = ?, | .
 
 462     qq|  language_id = ?, | .
 
 463     qq|  username = ?, | .
 
 464     qq|  user_password = ?, | .
 
 465     qq|  v_customer_id = ? | .
 
 468     $form->{vendornumber},
 
 471     $form->{department_1},
 
 472     $form->{department_2},
 
 485     conv_i($form->{terms}),
 
 487     $form->{creditlimit},
 
 488     conv_i($form->{business}),
 
 492     $form->{account_number},
 
 495     $form->{obsolete} ? 't' : 'f',
 
 497     conv_i($form->{payment_id}),
 
 498     conv_i($form->{taxzone_id}, 0),
 
 499     conv_i( $form->{language_id}),
 
 501     $form->{user_password},
 
 502     conv_i($form->{v_customer_id}),
 
 505   do_query($form, $dbh, $query, @values);
 
 508   if ( $form->{cp_id} ) {
 
 510       qq|UPDATE contacts SET | .
 
 511       qq|  cp_greeting = ?, | .
 
 512       qq|  cp_title = ?, | .
 
 513       qq|  cp_givenname = ?, | .
 
 515       qq|  cp_email = ?, | .
 
 516       qq|  cp_phone1 = ?, | .
 
 517       qq|  cp_phone2 = ? | .
 
 520       $form->{cp_greeting},
 
 522       $form->{cp_givenname},
 
 528   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 530       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2) | .
 
 531       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?)|;
 
 534       $form->{cp_greeting},
 
 536       $form->{cp_givenname},
 
 542   do_query($form, $dbh, $query, @values) if ($query);
 
 545   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 547   $rc = $dbh->commit();
 
 550   $main::lxdebug->leave_sub();
 
 555   $main::lxdebug->enter_sub();
 
 557   my ( $self, $myconfig, $form ) = @_;
 
 558   # connect to database
 
 559   my $dbh = $form->dbconnect($myconfig);
 
 562   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 563   my $query = qq|DELETE FROM $cv WHERE id = ?|;
 
 564   do_query($form, $dbh, $query, $form->{id});
 
 568   $main::lxdebug->leave_sub();
 
 572   $main::lxdebug->enter_sub();
 
 574   my ( $self, $myconfig, $form ) = @_;
 
 576   # connect to database
 
 577   my $dbh = $form->dbconnect($myconfig);
 
 579   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 584   my %allowed_sort_columns =
 
 585     map({ $_, 1 } qw(id customernumber name address contact phone fax email
 
 586                      taxnumber sic_code business invnumber ordnumber quonumber));
 
 588     $form->{sort} && $allowed_sort_columns{$form->{sort}} ?
 
 589     $form->{sort} : "name";
 
 590   $form->{sort} = $sortorder;
 
 591   $sortorder = "country,city,street" if ($sortorder eq "address");
 
 593   if ($form->{"${cv}number"}) {
 
 594     $where .= " AND ct.${cv}number ILIKE ?";
 
 595     push(@values, '%' . $form->{"${cv}number"} . '%');
 
 598   foreach my $key (qw(name contact email)) {
 
 600       $where .= " AND ct.$key ILIKE ?";
 
 601       push(@values, '%' . $form->{$key} . '%');
 
 605   if ( $form->{status} eq 'orphaned' ) {
 
 607       qq| AND ct.id NOT IN | .
 
 608       qq|   (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
 
 609     if ($cv eq 'customer') {
 
 611         qq| AND ct.id NOT IN | .
 
 612         qq| (SELECT a.customer_id FROM ar a, customer cv | .
 
 613         qq|  WHERE cv.id = a.customer_id)|;
 
 615     if ($cv eq 'vendor') {
 
 617         qq| AND ct.id NOT IN | .
 
 618         qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
 
 619         qq|  WHERE cv.id = a.vendor_id)|;
 
 621     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
 
 624   if ($form->{obsolete} eq "Y") {
 
 625     $where .= qq| AND obsolete|;
 
 626   } elsif ($form->{obsolete} eq "N") {
 
 627     $where .= qq| AND NOT obsolete|;
 
 630   if ($form->{business_id}) {
 
 631     $where .= qq| AND (business_id = ?)|;
 
 632     push(@values, conv_i($form->{business_id}));
 
 636     qq|SELECT ct.*, b.description AS business | .
 
 638     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 641   # redo for invoices, orders and quotations
 
 642   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
 
 643     my ($ar, $union, $module);
 
 646     if ($form->{l_invnumber}) {
 
 647       my $ar = $cv eq 'customer' ? 'ar' : 'ap';
 
 648       my $module = $ar eq 'ar' ? 'is' : 'ir';
 
 651         qq|SELECT ct.*, b.description AS business, | .
 
 652         qq|  a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
 
 653         qq|  '$module' AS module, 'invoice' AS formtype, | .
 
 654         qq|  (a.amount = a.paid) AS closed | .
 
 656         qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
 
 657         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 658         qq|WHERE $where AND (a.invoice = '1')|;
 
 663     if ( $form->{l_ordnumber} ) {
 
 666         qq|SELECT ct.*, b.description AS business,| .
 
 667         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 668         qq|  'oe' AS module, 'order' AS formtype, o.closed | .
 
 670         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 671         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 672         qq|WHERE $where AND (o.quotation = '0')|;
 
 677     if ( $form->{l_quonumber} ) {
 
 680         qq|SELECT ct.*, b.description AS business, | .
 
 681         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 682         qq|  'oe' AS module, 'quotation' AS formtype, o.closed | .
 
 684         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 685         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 686         qq|WHERE $where AND (o.quotation = '1')|;
 
 690   $query .= qq| ORDER BY $sortorder|;
 
 692   $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
 
 693   map({ my $ref = $_; $ref->{address} = join(" ", map({ $ref->{$_} } qw(street zipcode city country))); }
 
 696   $main::lxdebug->leave_sub();
 
 700   $main::lxdebug->enter_sub();
 
 702   my ( $self, $myconfig, $form ) = @_;
 
 703   my $dbh   = $form->dbconnect($myconfig);
 
 705     qq|SELECT * FROM contacts c | .
 
 706     qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
 
 707   my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
 
 708   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 710   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 715   $main::lxdebug->leave_sub();
 
 719   $main::lxdebug->enter_sub();
 
 721   my ( $self, $myconfig, $form ) = @_;
 
 722   my $dbh   = $form->dbconnect($myconfig);
 
 723   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
 724   my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
 
 726   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 728   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 733   $main::lxdebug->leave_sub();
 
 737   $main::lxdebug->enter_sub();
 
 739   my ( $self, $myconfig, $form ) = @_;
 
 740   my $dbh = $form->dbconnect($myconfig);
 
 742   my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
 
 743   my $db = $form->{db} eq "customer" ? "customer" : "vendor";
 
 745   my $where = " WHERE 1=1 ";
 
 748   if ($form->{shipto_id} && ($arap eq "ar")) {
 
 749     $where .= "AND ${arap}.shipto_id = ?";
 
 750     push(@values, $form->{shipto_id});
 
 752     $where .= "AND ${arap}.${db}_id = ?";
 
 753     push(@values, $form->{id});
 
 757     $where .= "AND ${arap}.transdate >= ?";
 
 758     push(@values, conv_date($form->{from}));
 
 761     $where .= "AND ${arap}.transdate <= ?";
 
 762     push(@values, conv_date($form->{to}));
 
 765     qq|SELECT s.shiptoname, i.qty, | .
 
 766     qq|  ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
 
 767     qq|  i.description, i.unit, i.sellprice | .
 
 769     qq|LEFT JOIN shipto s ON | .
 
 771      ? qq|(ar.shipto_id = s.shipto_id) |
 
 772      : qq|(ap.id = s.trans_id) |) .
 
 773     qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
 
 774     qq|LEFT join parts p ON (p.id = i.parts_id) | .
 
 776     qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
 
 778   $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
 
 782   $main::lxdebug->leave_sub();