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);
 
 105   $query = qq|SELECT id, description FROM tax_zones|;
 
 106   $form->{TAXZONE} = selectall_hashref_query($form, $dbh, $query);
 
 110     qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
 
 111     qq|FROM shipto WHERE (trans_id = ?) AND (module = 'CT')|;
 
 112   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
 
 115   $query  = qq|SELECT cp_id, cp_name FROM contacts WHERE cp_cv_id = ?|;
 
 116   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
 
 119   $query = qq|SELECT id, description FROM language ORDER BY id|;
 
 120   $form->{languages} = selectall_hashref_query($form, $dbh, $query);
 
 123   $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
 
 124   $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
 
 126   $dbh->disconnect() unless ($provided_dbh);
 
 128   $main::lxdebug->leave_sub();
 
 131 sub query_titles_and_greetings {
 
 132   $main::lxdebug->enter_sub();
 
 134   my ( $self, $myconfig, $form ) = @_;
 
 137   my $dbh = $form->dbconnect($myconfig);
 
 140     qq|SELECT DISTINCT(cp_greeting) | .
 
 142     qq|WHERE cp_greeting ~ '[a-zA-Z]' | .
 
 143     qq|ORDER BY cp_greeting|;
 
 144   $form->{GREETINGS} = [ selectall_array_query($form, $dbh, $query) ];
 
 147     qq|SELECT DISTINCT(greeting) | .
 
 149     qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 151     qq|SELECT DISTINCT(greeting) | .
 
 153     qq|WHERE greeting ~ '[a-zA-Z]' | .
 
 154     qq|ORDER BY greeting|;
 
 156   map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
 
 157   $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
 
 160     qq|SELECT DISTINCT(cp_title) | .
 
 162     qq|WHERE cp_title ~ '[a-zA-Z]'|;
 
 163   $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
 
 166     qq|SELECT DISTINCT(cp_abteilung) | .
 
 168     qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
 
 169   $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
 
 172   $main::lxdebug->leave_sub();
 
 176   $main::lxdebug->enter_sub();
 
 178   my ( $self, $myconfig, $form ) = @_;
 
 180   # set pricegroup to default
 
 181   $form->{klass} = 0 unless ($form->{klass});
 
 183   # connect to database
 
 184   my $dbh = $form->dbconnect_noauto($myconfig);
 
 187     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 188     if ( $form->{"selected_cp_${_}"} );
 
 189        } qw(title greeting abteilung) );
 
 190   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 191   if ( $form->{"selected_company_greeting"} );
 
 193   # assign value discount, terms, creditlimit
 
 194   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 195   $form->{discount} /= 100;
 
 196   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 198   my ( $query, $sth, $f_id );
 
 201     $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
 
 202     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
 
 204     if (($f_id ne $form->{id}) && ($f_id ne "")) {
 
 205       $main::lxdebug->leave_sub();
 
 210     if (!$form->{customernumber} && $form->{business}) {
 
 211       $form->{customernumber} =
 
 212         $form->update_business($myconfig, $form->{business}, $dbh);
 
 214     if (!$form->{customernumber}) {
 
 215       $form->{customernumber} =
 
 216         $form->update_defaults($myconfig, "customernumber", $dbh);
 
 219     $query  = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
 
 220     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
 
 222       $main::lxdebug->leave_sub();
 
 226     $query = qq|SELECT nextval('id')|;
 
 227     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 229     $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
 
 230     do_query($form, $dbh, $query, $form->{id});
 
 233   $query = qq|UPDATE customer SET | .
 
 234     qq|customernumber = ?, | .
 
 237     qq|department_1 = ?, | .
 
 238     qq|department_2 = ?, | .
 
 252     qq|creditlimit = ?, | .
 
 254     qq|business_id = ?, | .
 
 255     qq|taxnumber = ?, | .
 
 258     qq|account_number = ?, | .
 
 259     qq|bank_code = ?, | .
 
 264     qq|salesman_id = ?, | .
 
 265     qq|language_id = ?, | .
 
 266     qq|payment_id = ?, | .
 
 267     qq|taxzone_id = ?, | .
 
 268     qq|user_password = ?, | .
 
 269     qq|c_vendor_id = ?, | .
 
 273     $form->{customernumber},
 
 276     $form->{department_1},
 
 277     $form->{department_2},
 
 291     $form->{creditlimit},
 
 292     conv_i($form->{terms}),
 
 293     conv_i($form->{business}),
 
 297     $form->{account_number},
 
 300     $form->{obsolete} ? 't' : 'f',
 
 303     conv_i($form->{salesman_id}),
 
 304     conv_i($form->{language_id}),
 
 305     conv_i($form->{payment_id}),
 
 306     conv_i($form->{taxzone_id}),
 
 307     $form->{user_password},
 
 308     $form->{c_vendor_id},
 
 309     conv_i($form->{klass}),
 
 312   do_query( $form, $dbh, $query, @values );
 
 315   if ( $form->{cp_id} ) {
 
 316     $query = qq|UPDATE contacts SET | .
 
 317       qq|cp_greeting = ?, | .
 
 319       qq|cp_givenname = ?, | .
 
 322       qq|cp_phone1 = ?, | .
 
 323       qq|cp_phone2 = ?, | .
 
 324       qq|cp_abteilung = ?, | .
 
 326       qq|cp_mobile1 = ?, | .
 
 327       qq|cp_mobile2 = ?, | .
 
 328       qq|cp_satphone = ?, | .
 
 329       qq|cp_satfax = ?, | .
 
 330       qq|cp_project = ?, | .
 
 331       qq|cp_privatphone = ?, | .
 
 332       qq|cp_privatemail = ?, | .
 
 333       qq|cp_birthday = ? | .
 
 336       $form->{cp_greeting},
 
 338       $form->{cp_givenname},
 
 343       $form->{cp_abteilung},
 
 347       $form->{cp_satphone},
 
 350       $form->{cp_privatphone},
 
 351       $form->{cp_privatemail},
 
 352       $form->{cp_birthday},
 
 355   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 357       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname,  | .
 
 358       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
 
 359       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
 
 361       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 364       $form->{cp_greeting},
 
 366       $form->{cp_givenname},
 
 371       $form->{cp_abteilung},
 
 375       $form->{cp_satphone},
 
 378       $form->{cp_privatphone},
 
 379       $form->{cp_privatemail},
 
 383   do_query( $form, $dbh, $query, @values ) if ($query);
 
 386   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 388   $rc = $dbh->commit();
 
 391   $main::lxdebug->leave_sub();
 
 396   $main::lxdebug->enter_sub();
 
 398   my ( $self, $myconfig, $form ) = @_;
 
 400   $form->{taxzone_id} *= 1;
 
 401   # connect to database
 
 402   my $dbh = $form->dbconnect_noauto($myconfig);
 
 405     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 406     if ( $form->{"selected_cp_${_}"} );
 
 407        } qw(title greeting abteilung) );
 
 408   $form->{"greeting"} = $form->{"selected_company_greeting"}
 
 409   if ( $form->{"selected_company_greeting"} );
 
 411   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
 
 412   $form->{discount} /= 100;
 
 413   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
 
 418     $query = qq|DELETE FROM shipto WHERE (trans_id = ?) AND (module = 'CT')|;
 
 419     do_query($form, $dbh, $query, $form->{id});
 
 422     $query = qq|SELECT nextval('id')|;
 
 423     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 425     $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
 
 426     do_query($form, $dbh, $query, $form->{id});
 
 428     if ( !$form->{vendornumber} ) {
 
 429       $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
 
 434     qq|UPDATE vendor SET | .
 
 435     qq|  vendornumber = ?, | .
 
 437     qq|  greeting = ?, | .
 
 438     qq|  department_1 = ?, | .
 
 439     qq|  department_2 = ?, | .
 
 444     qq|  homepage = ?, | .
 
 453     qq|  discount = ?, | .
 
 454     qq|  creditlimit = ?, | .
 
 455     qq|  business_id = ?, | .
 
 456     qq|  taxnumber = ?, | .
 
 457     qq|  sic_code = ?, | .
 
 458     qq|  language = ?, | .
 
 459     qq|  account_number = ?, | .
 
 460     qq|  bank_code = ?, | .
 
 462     qq|  obsolete = ?, | .
 
 464     qq|  payment_id = ?, | .
 
 465     qq|  taxzone_id = ?, | .
 
 466     qq|  language_id = ?, | .
 
 467     qq|  username = ?, | .
 
 468     qq|  user_password = ?, | .
 
 469     qq|  v_customer_id = ? | .
 
 472     $form->{vendornumber},
 
 475     $form->{department_1},
 
 476     $form->{department_2},
 
 489     conv_i($form->{terms}),
 
 491     $form->{creditlimit},
 
 492     conv_i($form->{business}),
 
 496     $form->{account_number},
 
 499     $form->{obsolete} ? 't' : 'f',
 
 501     conv_i($form->{payment_id}),
 
 502     conv_i($form->{taxzone_id}),
 
 503     conv_i( $form->{language_id}),
 
 505     $form->{user_password},
 
 506     conv_i($form->{v_customer_id}),
 
 509   do_query($form, $dbh, $query, @values);
 
 512   if ( $form->{cp_id} ) {
 
 514       qq|UPDATE contacts SET | .
 
 515       qq|  cp_greeting = ?, | .
 
 516       qq|  cp_title = ?, | .
 
 517       qq|  cp_givenname = ?, | .
 
 519       qq|  cp_email = ?, | .
 
 520       qq|  cp_phone1 = ?, | .
 
 521       qq|  cp_phone2 = ? | .
 
 524       $form->{cp_greeting},
 
 526       $form->{cp_givenname},
 
 532   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
 
 534       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2) | .
 
 535       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?)|;
 
 538       $form->{cp_greeting},
 
 540       $form->{cp_givenname},
 
 546   do_query($form, $dbh, $query, @values) if ($query);
 
 549   $form->add_shipto( $dbh, $form->{id}, "CT" );
 
 551   $rc = $dbh->commit();
 
 554   $main::lxdebug->leave_sub();
 
 559   $main::lxdebug->enter_sub();
 
 561   my ( $self, $myconfig, $form ) = @_;
 
 562   # connect to database
 
 563   my $dbh = $form->dbconnect($myconfig);
 
 566   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 567   my $query = qq|DELETE FROM $cv WHERE id = ?|;
 
 568   do_query($form, $dbh, $query, $form->{id});
 
 572   $main::lxdebug->leave_sub();
 
 576   $main::lxdebug->enter_sub();
 
 578   my ( $self, $myconfig, $form ) = @_;
 
 580   # connect to database
 
 581   my $dbh = $form->dbconnect($myconfig);
 
 583   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
 
 588   my %allowed_sort_columns =
 
 589     map({ $_, 1 } qw(id customernumber name address contact phone fax email
 
 590                      taxnumber sic_code business invnumber ordnumber quonumber));
 
 592     $form->{sort} && $allowed_sort_columns{$form->{sort}} ?
 
 593     $form->{sort} : "name";
 
 594   $sortorder = "country,city,street" if ($sortorder eq "address");
 
 596   if ($form->{"${cv}number"}) {
 
 597     $where .= " AND ct.${cv}number ILIKE ?";
 
 598     push(@values, '%' . $form->{"${cv}number"} . '%');
 
 601   foreach my $key (qw(name contact email)) {
 
 603       $where .= " AND ct.$key ILIKE ?";
 
 604       push(@values, '%' . $form->{$key} . '%');
 
 608   if ( $form->{status} eq 'orphaned' ) {
 
 610       qq| AND ct.id NOT IN | .
 
 611       qq|   (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
 
 612     if ($cv eq 'customer') {
 
 614         qq| AND ct.id NOT IN | .
 
 615         qq| (SELECT a.customer_id FROM ar a, customer cv | .
 
 616         qq|  WHERE cv.id = a.customer_id)|;
 
 618     if ($cv eq 'vendor') {
 
 620         qq| AND ct.id NOT IN | .
 
 621         qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
 
 622         qq|  WHERE cv.id = a.vendor_id)|;
 
 624     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
 
 628     qq|SELECT ct.*, b.description AS business | .
 
 630     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 633   # redo for invoices, orders and quotations
 
 634   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
 
 635     my ($ar, $union, $module);
 
 638     if ($form->{l_invnumber}) {
 
 639       my $ar = $cv eq 'customer' ? 'ar' : 'ap';
 
 640       my $module = $ar eq 'ar' ? 'is' : 'ir';
 
 643         qq|SELECT ct.*, b.description AS business, | .
 
 644         qq|  a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
 
 645         qq|  '$module' AS module, 'invoice' AS formtype, | .
 
 646         qq|  (a.amount = a.paid) AS closed | .
 
 648         qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
 
 649         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 650         qq|WHERE $where AND (a.invoice = '1')|;
 
 655     if ( $form->{l_ordnumber} ) {
 
 658         qq|SELECT ct.*, b.description AS business,| .
 
 659         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 660         qq|  'oe' AS module, 'order' AS formtype, o.closed | .
 
 662         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 663         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 664         qq|WHERE $where AND (o.quotation = '0')|;
 
 669     if ( $form->{l_quonumber} ) {
 
 672         qq|SELECT ct.*, b.description AS business, | .
 
 673         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
 
 674         qq|  'oe' AS module, 'quotation' AS formtype, o.closed | .
 
 676         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
 
 677         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
 
 678         qq|WHERE $where AND (o.quotation = '1')|;
 
 682   $query .= qq| ORDER BY $sortorder|;
 
 684   $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
 
 685   map({ my $ref = $_; $ref->{address} = join(" ", map({ $ref->{$_} } qw(street zipcode city country))); }
 
 688   $main::lxdebug->leave_sub();
 
 692   $main::lxdebug->enter_sub();
 
 694   my ( $self, $myconfig, $form ) = @_;
 
 695   my $dbh   = $form->dbconnect($myconfig);
 
 697     qq|SELECT * FROM contacts c | .
 
 698     qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
 
 699   my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
 
 700   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 702   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 707   $main::lxdebug->leave_sub();
 
 711   $main::lxdebug->enter_sub();
 
 713   my ( $self, $myconfig, $form ) = @_;
 
 714   my $dbh   = $form->dbconnect($myconfig);
 
 715   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
 
 716   my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
 
 718   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 720   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 725   $main::lxdebug->leave_sub();
 
 729   $main::lxdebug->enter_sub();
 
 731   my ( $self, $myconfig, $form ) = @_;
 
 732   my $dbh = $form->dbconnect($myconfig);
 
 734   my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
 
 735   my $db = $form->{db} eq "customer" ? "customer" : "vendor";
 
 737   my $where = " WHERE 1=1 ";
 
 740   if ($form->{shipto_id} && ($arap eq "ar")) {
 
 741     $where .= "AND ${arap}.shipto_id = ?";
 
 742     push(@values, $form->{shipto_id});
 
 744     $where .= "AND ${arap}.${db}_id = ?";
 
 745     push(@values, $form->{id});
 
 749     $where .= "AND ${arap}.transdate >= ?";
 
 750     push(@values, conv_date($form->{from}));
 
 753     $where .= "AND ${arap}.transdate <= ?";
 
 754     push(@values, conv_date($form->{to}));
 
 757     qq|SELECT s.shiptoname, i.qty, | .
 
 758     qq|  ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
 
 759     qq|  invoice.description, invoice.unit | .
 
 761     qq|LEFT JOIN shipto s ON | .
 
 763      ? qq|(ar.shipto_id = s.shipto_id) |
 
 764      : qq|(ap.id = s.trans_id) |) .
 
 765     qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
 
 766     qq|LEFT join parts p ON (p.id = i.parts_id) | .
 
 768     qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
 
 770   $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
 
 774   $main::lxdebug->leave_sub();