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 #======================================================================
 
  41   $main::lxdebug->enter_sub();
 
  43   my ($self, $myconfig, $form) = @_;
 
  45   my $dbh   = $form->dbconnect($myconfig);
 
  46   my $query = qq|SELECT ct.*, b.id AS business, s.*, cp.*
 
  48                  LEFT JOIN business b on ct.business_id = b.id
 
  49                  LEFT JOIN shipto s on ct.id = s.trans_id
 
  50                  LEFT JOIN contacts cp on ct.id = cp.cp_cv_id
 
  51                  WHERE ct.id = $form->{id}  order by cp.cp_id limit 1|;
 
  52   my $sth = $dbh->prepare($query);
 
  53   $sth->execute || $form->dberror($query);
 
  55   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
  57   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
  60   if ($form->{salesman_id}) {
 
  61     my $query = qq|SELECT ct.name AS salesman
 
  63                   WHERE ct.id = $form->{salesman_id}|;
 
  64     my $sth = $dbh->prepare($query);
 
  65     $sth->execute || $form->dberror($query);
 
  67     my ($ref) = $sth->fetchrow_array();
 
  69     $form->{salesman} = $ref;
 
  74   # check if it is orphaned
 
  75   my $arap = ($form->{db} eq 'customer') ? "ar" : "ap";
 
  76   $query = qq|SELECT a.id
 
  78               JOIN $form->{db} ct ON (a.$form->{db}_id = ct.id)
 
  79               WHERE ct.id = $form->{id}
 
  83               JOIN $form->{db} ct ON (a.$form->{db}_id = ct.id)
 
  84               WHERE ct.id = $form->{id}|;
 
  85   $sth = $dbh->prepare($query);
 
  86   $sth->execute || $form->dberror($query);
 
  88   unless ($sth->fetchrow_array) {
 
  89     $form->{status} = "orphaned";
 
  94   $query = qq|SELECT c.accno, c.description
 
  96               JOIN tax t ON (t.chart_id = c.id)
 
  97               WHERE c.link LIKE '%CT_tax%'
 
  99   $sth = $dbh->prepare($query);
 
 100   $sth->execute || $form->dberror($query);
 
 102   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 103     $form->{taxaccounts} .= "$ref->{accno} ";
 
 104     $form->{tax}{ $ref->{accno} }{description} = $ref->{description};
 
 107   chop $form->{taxaccounts};
 
 109   # get taxes for customer/vendor
 
 110   $query = qq|SELECT c.accno
 
 112               JOIN $form->{db}tax t ON (t.chart_id = c.id)
 
 113               WHERE t.$form->{db}_id = $form->{id}|;
 
 114   $sth = $dbh->prepare($query);
 
 115   $sth->execute || $form->dberror($query);
 
 117   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 118     $form->{tax}{ $ref->{accno} }{taxable} = 1;
 
 123   $query = qq|SELECT id, description
 
 126   $sth = $dbh->prepare($query);
 
 127   $sth->execute || $form->dberror($query);
 
 129   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 130     push @{ $form->{all_business} }, $ref;
 
 136   $main::lxdebug->leave_sub();
 
 140 sub query_titles_and_greetings {
 
 141   $main::lxdebug->enter_sub();
 
 143   my ($self, $myconfig, $form) = @_;
 
 146   my $dbh = $form->dbconnect($myconfig);
 
 149     "SELECT DISTINCT(c.cp_greeting) FROM contacts c WHERE c.cp_greeting LIKE '%'";
 
 150   $sth = $dbh->prepare($query);
 
 151   $sth->execute() || $form->dberror($query);
 
 152   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 153     next unless ($ref->{cp_greeting} =~ /[a-zA-Z]/);
 
 154     $tmp{ $ref->{cp_greeting} } = 1;
 
 158   @{ $form->{GREETINGS} } = sort(keys(%tmp));
 
 163     "SELECT DISTINCT(c.cp_title) FROM contacts c WHERE c.cp_title LIKE '%'";
 
 164   $sth = $dbh->prepare($query);
 
 165   $sth->execute() || $form->dberror($query);
 
 166   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 167     next unless ($ref->{cp_title} =~ /[a-zA-Z]/);
 
 168     $tmp{ $ref->{cp_title} } = 1;
 
 172   @{ $form->{TITLES} } = sort(keys(%tmp));
 
 175   $main::lxdebug->leave_sub();
 
 180   $main::lxdebug->enter_sub();
 
 182   my ($self, $myconfig, $form) = @_;
 
 184   my $dbh = $form->dbconnect($myconfig);
 
 187   my $query = qq|SELECT accno, description
 
 189                  WHERE c.link LIKE '%CT_tax%'
 
 190                  AND c.id = t.chart_id
 
 192   $sth = $dbh->prepare($query);
 
 193   $sth->execute || $form->dberror($query);
 
 196   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 197     $form->{taxaccounts} .= "$ref->{accno} ";
 
 198     $form->{tax}{ $ref->{accno} }{description} = $ref->{description};
 
 201   chop $form->{taxaccounts};
 
 203   # this is just for the selection for type of business
 
 204   $query = qq|SELECT id, description
 
 206   $sth = $dbh->prepare($query);
 
 207   $sth->execute || $form->dberror($query);
 
 209   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 210     push @{ $form->{all_business} }, $ref;
 
 216   $main::lxdebug->leave_sub();
 
 220   $main::lxdebug->enter_sub();
 
 222   my ($self, $myconfig, $form) = @_;
 
 224   # connect to database
 
 225   my $dbh = $form->dbconnect($myconfig);
 
 227   map({ $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 228           if ($form->{"selected_cp_${_}"});
 
 229   } qw(title greeting));
 
 232   map { $form->{$_} =~ s/\'/\'\'/g }
 
 233     qw(customernumber name street zipcode city country homepage contact notes cp_title cp_greeting language);
 
 235   # assign value discount, terms, creditlimit
 
 236   $form->{discount} = $form->parse_amount($myconfig, $form->{discount});
 
 237   $form->{discount} /= 100;
 
 239   $form->{taxincluded} *= 1;
 
 240   $form->{obsolete}    *= 1;
 
 241   $form->{business}    *= 1;
 
 242   $form->{salesman_id} *= 1;
 
 243   $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
 
 248     $query = qq|DELETE FROM customertax
 
 249                 WHERE customer_id = $form->{id}|;
 
 250     $dbh->do($query) || $form->dberror($query);
 
 252     $query = qq|DELETE FROM shipto
 
 253                 WHERE trans_id = $form->{id}|;
 
 254     $dbh->do($query) || $form->dberror($query);
 
 256     my $uid = rand() . time;
 
 258     $uid .= $form->{login};
 
 260     $uid = substr($uid, 2, 75);
 
 262     $query = qq|INSERT INTO customer (name)
 
 264     $dbh->do($query) || $form->dberror($query);
 
 266     $query = qq|SELECT c.id FROM customer c
 
 267                 WHERE c.name = '$uid'|;
 
 268     $sth = $dbh->prepare($query);
 
 269     $sth->execute || $form->dberror($query);
 
 271     ($form->{id}) = $sth->fetchrow_array;
 
 273     if (!$form->{customernumber} && $form->{business}) {
 
 274       $form->{customernumber} =
 
 275         $form->update_business($myconfig, $form->{business});
 
 277     if (!$form->{customernumber}) {
 
 278       $form->{customernumber} =
 
 279         $form->update_defaults($myconfig, "customernumber");
 
 284   $query = qq|UPDATE customer SET
 
 285               customernumber = '$form->{customernumber}',
 
 286               name = '$form->{name}',
 
 287               department_1 = '$form->{department_1}',
 
 288               department_2 = '$form->{department_2}',
 
 289               street = '$form->{street}',
 
 290               zipcode = '$form->{zipcode}',
 
 291               city = '$form->{city}',
 
 292               country = '$form->{country}',
 
 293               homepage = '$form->{homepage}',
 
 294               contact = '$form->{contact}',
 
 295               phone = '$form->{phone}',
 
 296               fax = '$form->{fax}',
 
 297               email = '$form->{email}',
 
 299               bcc = '$form->{bcc}',
 
 300               notes = '$form->{notes}',
 
 301               discount = $form->{discount},
 
 302               creditlimit = $form->{creditlimit},
 
 303               terms = $form->{terms},
 
 304               taxincluded = '$form->{taxincluded}',
 
 305               business_id = $form->{business},
 
 306               taxnumber = '$form->{taxnumber}',
 
 307               sic_code = '$form->{sic}',
 
 308               language = '$form->{language}',
 
 309               account_number = '$form->{account_number}',
 
 310               bank_code = '$form->{bank_code}',
 
 311               bank = '$form->{bank}',
 
 312               obsolete = '$form->{obsolete}',
 
 313               ustid = '$form->{ustid}',
 
 314               username = '$form->{username}',
 
 315               salesman_id = '$form->{salesman_id}',
 
 316               user_password = '$form->{user_password}',
 
 317               c_vendor_id = '$form->{c_vendor_id}'
 
 318               WHERE id = $form->{id}|;
 
 319   $dbh->do($query) || $form->dberror($query);
 
 321   if ($form->{cp_id}) {
 
 322     $query = qq|UPDATE contacts SET
 
 323                 cp_greeting = '$form->{cp_greeting}',
 
 324                 cp_title = '$form->{cp_title}',
 
 325                 cp_givenname = '$form->{cp_givenname}',
 
 326                 cp_name = '$form->{cp_name}',
 
 327                 cp_email = '$form->{cp_email}',
 
 328                 cp_phone1 = '$form->{cp_phone1}',
 
 329                 cp_phone2 = '$form->{cp_phone2}'
 
 330                 WHERE cp_id = $form->{cp_id}|;
 
 331   } elsif ($form->{cp_name} || $form->{cp_givenname}) {
 
 333       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2)
 
 334                   VALUES ($form->{id}, '$form->{cp_greeting}','$form->{cp_title}','$form->{cp_givenname}','$form->{cp_name}','$form->{cp_email}','$form->{cp_phone1}','$form->{cp_phone2}')|;
 
 336   $dbh->do($query) || $form->dberror($query);
 
 339   foreach $item (split / /, $form->{taxaccounts}) {
 
 340     if ($form->{"tax_$item"}) {
 
 341       $query = qq|INSERT INTO customertax (customer_id, chart_id)
 
 342                   VALUES ($form->{id}, (SELECT c.id
 
 344                                         WHERE c.accno = '$item'))|;
 
 345       $dbh->do($query) || $form->dberror($query);
 
 350   $form->add_shipto($dbh, $form->{id});
 
 354   $main::lxdebug->leave_sub();
 
 358   $main::lxdebug->enter_sub();
 
 360   my ($self, $myconfig, $form) = @_;
 
 362   # connect to database
 
 363   my $dbh = $form->dbconnect($myconfig);
 
 365   map({ $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 366           if ($form->{"selected_cp_${_}"});
 
 367   } qw(title greeting));
 
 370   map { $form->{$_} =~ s/\'/\'\'/g }
 
 371     qw(vendornumber name street zipcode city country homepage contact notes cp_title cp_greeting language);
 
 373   $form->{discount} = $form->parse_amount($myconfig, $form->{discount});
 
 374   $form->{discount} /= 100;
 
 376   $form->{taxincluded} *= 1;
 
 377   $form->{obsolete}    *= 1;
 
 378   $form->{business}    *= 1;
 
 379   $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
 
 384     $query = qq|DELETE FROM vendortax
 
 385                 WHERE vendor_id = $form->{id}|;
 
 386     $dbh->do($query) || $form->dberror($query);
 
 388     $query = qq|DELETE FROM shipto
 
 389                 WHERE trans_id = $form->{id}|;
 
 390     $dbh->do($query) || $form->dberror($query);
 
 393     $uid .= $form->{login};
 
 394     my $uid = rand() . time;
 
 395     $uid .= $form->{login};
 
 396     $uid = substr($uid, 2, 75);
 
 397     $query = qq|INSERT INTO vendor (name)
 
 399     $dbh->do($query) || $form->dberror($query);
 
 401     $query = qq|SELECT v.id FROM vendor v
 
 402                 WHERE v.name = '$uid'|;
 
 403     $sth = $dbh->prepare($query);
 
 404     $sth->execute || $form->dberror($query);
 
 406     ($form->{id}) = $sth->fetchrow_array;
 
 408     if (!$form->{vendornumber}) {
 
 409       $form->{vendornumber} =
 
 410         $form->update_defaults($myconfig, "vendornumber");
 
 416   $query = qq|UPDATE vendor SET
 
 417               vendornumber = '$form->{vendornumber}',
 
 418               name = '$form->{name}',
 
 419               department_1 = '$form->{department_1}',
 
 420               department_2 = '$form->{department_2}',
 
 421               street = '$form->{street}',
 
 422               zipcode = '$form->{zipcode}',
 
 423               city = '$form->{city}',
 
 424               country = '$form->{country}',
 
 425               homepage = '$form->{homepage}',
 
 426               contact = '$form->{contact}',
 
 427               phone = '$form->{phone}',
 
 428               fax = '$form->{fax}',
 
 429               email = '$form->{email}',
 
 431               bcc = '$form->{bcc}',
 
 432               notes = '$form->{notes}',
 
 433               terms = $form->{terms},
 
 434               discount = $form->{discount},
 
 435               creditlimit = $form->{creditlimit},
 
 436               taxincluded = '$form->{taxincluded}',
 
 437               gifi_accno = '$form->{gifi_accno}',
 
 438               business_id = $form->{business},
 
 439               taxnumber = '$form->{taxnumber}',
 
 440               sic_code = '$form->{sic}',
 
 441               language = '$form->{language}',
 
 442               account_number = '$form->{account_number}',
 
 443               bank_code = '$form->{bank_code}',
 
 444               bank = '$form->{bank}',
 
 445               obsolete = '$form->{obsolete}',
 
 446               ustid = '$form->{ustid}',
 
 447               username = '$form->{username}',
 
 448               user_password = '$form->{user_password}',
 
 449               v_customer_id = '$form->{v_customer_id}'
 
 450               WHERE id = $form->{id}|;
 
 451   $dbh->do($query) || $form->dberror($query);
 
 453   if ($form->{cp_id}) {
 
 454     $query = qq|UPDATE contacts SET
 
 455                 cp_greeting = '$form->{cp_greeting}',
 
 456                 cp_title = '$form->{cp_title}',
 
 457                 cp_givenname = '$form->{cp_givenname}',
 
 458                 cp_name = '$form->{cp_name}',
 
 459                 cp_email = '$form->{cp_email}',
 
 460                 cp_phone1 = '$form->{cp_phone1}',
 
 461                 cp_phone2 = '$form->{cp_phone2}'
 
 462                 WHERE cp_id = $form->{cp_id}|;
 
 463   } elsif ($form->{cp_name} || $form->{cp_givenname}) {
 
 465       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2)
 
 466                   VALUES ($form->{id}, '$form->{cp_greeting}','$form->{cp_title}','$form->{cp_givenname}','$form->{cp_name}','$form->{cp_email}','$form->{cp_phone1}','$form->{cp_phone2}')|;
 
 468   $dbh->do($query) || $form->dberror($query);
 
 471   foreach $item (split / /, $form->{taxaccounts}) {
 
 472     if ($form->{"tax_$item"}) {
 
 473       $query = qq|INSERT INTO vendortax (vendor_id, chart_id)
 
 474                   VALUES ($form->{id}, (SELECT c.id
 
 476                                         WHERE c.accno = '$item'))|;
 
 477       $dbh->do($query) || $form->dberror($query);
 
 482   $form->add_shipto($dbh, $form->{id});
 
 486   $main::lxdebug->leave_sub();
 
 490   $main::lxdebug->enter_sub();
 
 492   my ($self, $myconfig, $form) = @_;
 
 494   # connect to database
 
 495   my $dbh = $form->dbconnect($myconfig);
 
 498   my $query = qq|DELETE FROM $form->{db}
 
 499                  WHERE id = $form->{id}|;
 
 500   $dbh->do($query) || $form->dberror($query);
 
 504   $main::lxdebug->leave_sub();
 
 508   $main::lxdebug->enter_sub();
 
 510   my ($self, $myconfig, $form) = @_;
 
 512   # connect to database
 
 513   my $dbh = $form->dbconnect($myconfig);
 
 516   $form->{sort} = "name" unless ($form->{sort});
 
 518   if ($form->{"$form->{db}number"}) {
 
 519     my $companynumber = $form->like(lc $form->{"$form->{db}number"});
 
 520     $where .= " AND lower(ct.$form->{db}number) LIKE '$companynumber'";
 
 523     my $name = $form->like(lc $form->{name});
 
 524     $where .= " AND lower(ct.name) LIKE '$name'";
 
 526   if ($form->{contact}) {
 
 527     my $contact = $form->like(lc $form->{contact});
 
 528     $where .= " AND lower(ct.contact) LIKE '$contact'";
 
 530   if ($form->{email}) {
 
 531     my $email = $form->like(lc $form->{email});
 
 532     $where .= " AND lower(ct.email) LIKE '$email'";
 
 535   if ($form->{status} eq 'orphaned') {
 
 536     $where .= qq| AND ct.id NOT IN (SELECT o.$form->{db}_id
 
 537                                     FROM oe o, $form->{db} cv
 
 538                                     WHERE cv.id = o.$form->{db}_id)|;
 
 539     if ($form->{db} eq 'customer') {
 
 540       $where .= qq| AND ct.id NOT IN (SELECT a.customer_id
 
 541                                       FROM ar a, customer cv
 
 542                                       WHERE cv.id = a.customer_id)|;
 
 544     if ($form->{db} eq 'vendor') {
 
 545       $where .= qq| AND ct.id NOT IN (SELECT a.vendor_id
 
 547                                       WHERE cv.id = a.vendor_id)|;
 
 549     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
 
 552   my $query = qq|SELECT ct.*, b.description AS business
 
 554               LEFT JOIN business b ON (ct.business_id = b.id)
 
 557   # redo for invoices, orders and quotations
 
 558   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
 
 560     my ($ar, $union, $module);
 
 563     if ($form->{l_invnumber}) {
 
 564       $ar     = ($form->{db} eq 'customer') ? 'ar' : 'ap';
 
 565       $module = ($ar         eq 'ar')       ? 'is' : 'ir';
 
 567       $query = qq|SELECT ct.*, b.description AS business,
 
 568                   a.invnumber, a.ordnumber, a.quonumber, a.id AS invid,
 
 569                   '$module' AS module, 'invoice' AS formtype,
 
 570                   (a.amount = a.paid) AS closed
 
 572                 JOIN $ar a ON (a.$form->{db}_id = ct.id)
 
 573                 LEFT JOIN business b ON (ct.business_id = b.id)
 
 575                   AND a.invoice = '1'|;
 
 582     if ($form->{l_ordnumber}) {
 
 584                   SELECT ct.*, b.description AS business,
 
 585                   ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
 
 586                   'oe' AS module, 'order' AS formtype,
 
 589                 JOIN oe o ON (o.$form->{db}_id = ct.id)
 
 590                 LEFT JOIN business b ON (ct.business_id = b.id)
 
 592                   AND o.quotation = '0'|;
 
 598     if ($form->{l_quonumber}) {
 
 600                   SELECT ct.*, b.description AS business,
 
 601                   ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
 
 602                   'oe' AS module, 'quotation' AS formtype,
 
 605                 JOIN oe o ON (o.$form->{db}_id = ct.id)
 
 606                 LEFT JOIN business b ON (ct.business_id = b.id)
 
 608                   AND o.quotation = '1'|;
 
 614                  ORDER BY $form->{sort}|;
 
 616   my $sth = $dbh->prepare($query);
 
 617   $sth->execute || $form->dberror($query);
 
 619   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 620     $ref->{address} = "";
 
 621     map { $ref->{address} .= "$ref->{$_} "; } qw(street zipcode city country);
 
 622     push @{ $form->{CT} }, $ref;
 
 628   $main::lxdebug->leave_sub();