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   # set pricegroup to default
 
 225   if ($form->{klass}) { }
 
 226   else { $form->{klass} = 0; }
 
 228   # connect to database
 
 229   my $dbh = $form->dbconnect($myconfig);
 
 232       $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 233         if ($form->{"selected_cp_${_}"});
 
 234   } qw(title greeting));
 
 237   map { $form->{$_} =~ s/\'/\'\'/g }
 
 238     qw(customernumber name street zipcode city country homepage contact notes cp_title cp_greeting language pricegroup);
 
 240   # assign value discount, terms, creditlimit
 
 241   $form->{discount} = $form->parse_amount($myconfig, $form->{discount});
 
 242   $form->{discount} /= 100;
 
 244   $form->{taxincluded} *= 1;
 
 245   $form->{obsolete}    *= 1;
 
 246   $form->{business}    *= 1;
 
 247   $form->{salesman_id} *= 1;
 
 248   $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
 
 253     $query = qq|DELETE FROM customertax
 
 254                 WHERE customer_id = $form->{id}|;
 
 255     $dbh->do($query) || $form->dberror($query);
 
 257     $query = qq|DELETE FROM shipto
 
 258                 WHERE trans_id = $form->{id}|;
 
 259     $dbh->do($query) || $form->dberror($query);
 
 261     my $uid = rand() . time;
 
 263     $uid .= $form->{login};
 
 265     $uid = substr($uid, 2, 75);
 
 267     $query = qq|INSERT INTO customer (name)
 
 269     $dbh->do($query) || $form->dberror($query);
 
 271     $query = qq|SELECT c.id FROM customer c
 
 272                 WHERE c.name = '$uid'|;
 
 273     $sth = $dbh->prepare($query);
 
 274     $sth->execute || $form->dberror($query);
 
 276     ($form->{id}) = $sth->fetchrow_array;
 
 278     if (!$form->{customernumber} && $form->{business}) {
 
 279       $form->{customernumber} =
 
 280         $form->update_business($myconfig, $form->{business});
 
 282     if (!$form->{customernumber}) {
 
 283       $form->{customernumber} =
 
 284         $form->update_defaults($myconfig, "customernumber");
 
 289   $query = qq|UPDATE customer SET
 
 290               customernumber = '$form->{customernumber}',
 
 291               name = '$form->{name}',
 
 292               department_1 = '$form->{department_1}',
 
 293               department_2 = '$form->{department_2}',
 
 294               street = '$form->{street}',
 
 295               zipcode = '$form->{zipcode}',
 
 296               city = '$form->{city}',
 
 297               country = '$form->{country}',
 
 298               homepage = '$form->{homepage}',
 
 299               contact = '$form->{contact}',
 
 300               phone = '$form->{phone}',
 
 301               fax = '$form->{fax}',
 
 302               email = '$form->{email}',
 
 304               bcc = '$form->{bcc}',
 
 305               notes = '$form->{notes}',
 
 306               discount = $form->{discount},
 
 307               creditlimit = $form->{creditlimit},
 
 308               terms = $form->{terms},
 
 309               taxincluded = '$form->{taxincluded}',
 
 310               business_id = $form->{business},
 
 311               taxnumber = '$form->{taxnumber}',
 
 312               sic_code = '$form->{sic}',
 
 313               language = '$form->{language}',
 
 314               account_number = '$form->{account_number}',
 
 315               bank_code = '$form->{bank_code}',
 
 316               bank = '$form->{bank}',
 
 317               obsolete = '$form->{obsolete}',
 
 318               ustid = '$form->{ustid}',
 
 319               username = '$form->{username}',
 
 320               salesman_id = '$form->{salesman_id}',
 
 321               user_password = '$form->{user_password}',
 
 322               c_vendor_id = '$form->{c_vendor_id}',
 
 323               klass = '$form->{klass}'
 
 324               WHERE id = $form->{id}|;
 
 325   $dbh->do($query) || $form->dberror($query);
 
 327   if ($form->{cp_id}) {
 
 328     $query = qq|UPDATE contacts SET
 
 329                 cp_greeting = '$form->{cp_greeting}',
 
 330                 cp_title = '$form->{cp_title}',
 
 331                 cp_givenname = '$form->{cp_givenname}',
 
 332                 cp_name = '$form->{cp_name}',
 
 333                 cp_email = '$form->{cp_email}',
 
 334                 cp_phone1 = '$form->{cp_phone1}',
 
 335                 cp_phone2 = '$form->{cp_phone2}'
 
 336                 WHERE cp_id = $form->{cp_id}|;
 
 337   } elsif ($form->{cp_name} || $form->{cp_givenname}) {
 
 339       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2)
 
 340                   VALUES ($form->{id}, '$form->{cp_greeting}','$form->{cp_title}','$form->{cp_givenname}','$form->{cp_name}','$form->{cp_email}','$form->{cp_phone1}','$form->{cp_phone2}')|;
 
 342   $dbh->do($query) || $form->dberror($query);
 
 345   foreach $item (split / /, $form->{taxaccounts}) {
 
 346     if ($form->{"tax_$item"}) {
 
 347       $query = qq|INSERT INTO customertax (customer_id, chart_id)
 
 348                   VALUES ($form->{id}, (SELECT c.id
 
 350                                         WHERE c.accno = '$item'))|;
 
 351       $dbh->do($query) || $form->dberror($query);
 
 356   $form->add_shipto($dbh, $form->{id});
 
 360   $main::lxdebug->leave_sub();
 
 364   $main::lxdebug->enter_sub();
 
 366   my ($self, $myconfig, $form) = @_;
 
 368   # connect to database
 
 369   my $dbh = $form->dbconnect($myconfig);
 
 372       $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
 
 373         if ($form->{"selected_cp_${_}"});
 
 374   } qw(title greeting));
 
 377   map { $form->{$_} =~ s/\'/\'\'/g }
 
 378     qw(vendornumber name street zipcode city country homepage contact notes cp_title cp_greeting language);
 
 380   $form->{discount} = $form->parse_amount($myconfig, $form->{discount});
 
 381   $form->{discount} /= 100;
 
 383   $form->{taxincluded} *= 1;
 
 384   $form->{obsolete}    *= 1;
 
 385   $form->{business}    *= 1;
 
 386   $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
 
 391     $query = qq|DELETE FROM vendortax
 
 392                 WHERE vendor_id = $form->{id}|;
 
 393     $dbh->do($query) || $form->dberror($query);
 
 395     $query = qq|DELETE FROM shipto
 
 396                 WHERE trans_id = $form->{id}|;
 
 397     $dbh->do($query) || $form->dberror($query);
 
 400     $uid .= $form->{login};
 
 401     my $uid = rand() . time;
 
 402     $uid .= $form->{login};
 
 403     $uid = substr($uid, 2, 75);
 
 404     $query = qq|INSERT INTO vendor (name)
 
 406     $dbh->do($query) || $form->dberror($query);
 
 408     $query = qq|SELECT v.id FROM vendor v
 
 409                 WHERE v.name = '$uid'|;
 
 410     $sth = $dbh->prepare($query);
 
 411     $sth->execute || $form->dberror($query);
 
 413     ($form->{id}) = $sth->fetchrow_array;
 
 415     if (!$form->{vendornumber}) {
 
 416       $form->{vendornumber} =
 
 417         $form->update_defaults($myconfig, "vendornumber");
 
 423   $query = qq|UPDATE vendor SET
 
 424               vendornumber = '$form->{vendornumber}',
 
 425               name = '$form->{name}',
 
 426               department_1 = '$form->{department_1}',
 
 427               department_2 = '$form->{department_2}',
 
 428               street = '$form->{street}',
 
 429               zipcode = '$form->{zipcode}',
 
 430               city = '$form->{city}',
 
 431               country = '$form->{country}',
 
 432               homepage = '$form->{homepage}',
 
 433               contact = '$form->{contact}',
 
 434               phone = '$form->{phone}',
 
 435               fax = '$form->{fax}',
 
 436               email = '$form->{email}',
 
 438               bcc = '$form->{bcc}',
 
 439               notes = '$form->{notes}',
 
 440               terms = $form->{terms},
 
 441               discount = $form->{discount},
 
 442               creditlimit = $form->{creditlimit},
 
 443               taxincluded = '$form->{taxincluded}',
 
 444               gifi_accno = '$form->{gifi_accno}',
 
 445               business_id = $form->{business},
 
 446               taxnumber = '$form->{taxnumber}',
 
 447               sic_code = '$form->{sic}',
 
 448               language = '$form->{language}',
 
 449               account_number = '$form->{account_number}',
 
 450               bank_code = '$form->{bank_code}',
 
 451               bank = '$form->{bank}',
 
 452               obsolete = '$form->{obsolete}',
 
 453               ustid = '$form->{ustid}',
 
 454               username = '$form->{username}',
 
 455               user_password = '$form->{user_password}',
 
 456               v_customer_id = '$form->{v_customer_id}'
 
 457               WHERE id = $form->{id}|;
 
 458   $dbh->do($query) || $form->dberror($query);
 
 460   if ($form->{cp_id}) {
 
 461     $query = qq|UPDATE contacts SET
 
 462                 cp_greeting = '$form->{cp_greeting}',
 
 463                 cp_title = '$form->{cp_title}',
 
 464                 cp_givenname = '$form->{cp_givenname}',
 
 465                 cp_name = '$form->{cp_name}',
 
 466                 cp_email = '$form->{cp_email}',
 
 467                 cp_phone1 = '$form->{cp_phone1}',
 
 468                 cp_phone2 = '$form->{cp_phone2}'
 
 469                 WHERE cp_id = $form->{cp_id}|;
 
 470   } elsif ($form->{cp_name} || $form->{cp_givenname}) {
 
 472       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2)
 
 473                   VALUES ($form->{id}, '$form->{cp_greeting}','$form->{cp_title}','$form->{cp_givenname}','$form->{cp_name}','$form->{cp_email}','$form->{cp_phone1}','$form->{cp_phone2}')|;
 
 475   $dbh->do($query) || $form->dberror($query);
 
 478   foreach $item (split / /, $form->{taxaccounts}) {
 
 479     if ($form->{"tax_$item"}) {
 
 480       $query = qq|INSERT INTO vendortax (vendor_id, chart_id)
 
 481                   VALUES ($form->{id}, (SELECT c.id
 
 483                                         WHERE c.accno = '$item'))|;
 
 484       $dbh->do($query) || $form->dberror($query);
 
 489   $form->add_shipto($dbh, $form->{id});
 
 493   $main::lxdebug->leave_sub();
 
 497   $main::lxdebug->enter_sub();
 
 499   my ($self, $myconfig, $form) = @_;
 
 501   # connect to database
 
 502   my $dbh = $form->dbconnect($myconfig);
 
 505   my $query = qq|DELETE FROM $form->{db}
 
 506                  WHERE id = $form->{id}|;
 
 507   $dbh->do($query) || $form->dberror($query);
 
 511   $main::lxdebug->leave_sub();
 
 515   $main::lxdebug->enter_sub();
 
 517   my ($self, $myconfig, $form) = @_;
 
 519   # connect to database
 
 520   my $dbh = $form->dbconnect($myconfig);
 
 523   $form->{sort} = "name" unless ($form->{sort});
 
 525   if ($form->{"$form->{db}number"}) {
 
 526     my $companynumber = $form->like(lc $form->{"$form->{db}number"});
 
 527     $where .= " AND lower(ct.$form->{db}number) LIKE '$companynumber'";
 
 530     my $name = $form->like(lc $form->{name});
 
 531     $where .= " AND lower(ct.name) LIKE '$name'";
 
 533   if ($form->{contact}) {
 
 534     my $contact = $form->like(lc $form->{contact});
 
 535     $where .= " AND lower(ct.contact) LIKE '$contact'";
 
 537   if ($form->{email}) {
 
 538     my $email = $form->like(lc $form->{email});
 
 539     $where .= " AND lower(ct.email) LIKE '$email'";
 
 542   if ($form->{status} eq 'orphaned') {
 
 543     $where .= qq| AND ct.id NOT IN (SELECT o.$form->{db}_id
 
 544                                     FROM oe o, $form->{db} cv
 
 545                                     WHERE cv.id = o.$form->{db}_id)|;
 
 546     if ($form->{db} eq 'customer') {
 
 547       $where .= qq| AND ct.id NOT IN (SELECT a.customer_id
 
 548                                       FROM ar a, customer cv
 
 549                                       WHERE cv.id = a.customer_id)|;
 
 551     if ($form->{db} eq 'vendor') {
 
 552       $where .= qq| AND ct.id NOT IN (SELECT a.vendor_id
 
 554                                       WHERE cv.id = a.vendor_id)|;
 
 556     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
 
 559   my $query = qq|SELECT ct.*, b.description AS business
 
 561               LEFT JOIN business b ON (ct.business_id = b.id)
 
 564   # redo for invoices, orders and quotations
 
 565   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
 
 567     my ($ar, $union, $module);
 
 570     if ($form->{l_invnumber}) {
 
 571       $ar     = ($form->{db} eq 'customer') ? 'ar' : 'ap';
 
 572       $module = ($ar         eq 'ar')       ? 'is' : 'ir';
 
 574       $query = qq|SELECT ct.*, b.description AS business,
 
 575                   a.invnumber, a.ordnumber, a.quonumber, a.id AS invid,
 
 576                   '$module' AS module, 'invoice' AS formtype,
 
 577                   (a.amount = a.paid) AS closed
 
 579                 JOIN $ar a ON (a.$form->{db}_id = ct.id)
 
 580                 LEFT JOIN business b ON (ct.business_id = b.id)
 
 582                   AND a.invoice = '1'|;
 
 589     if ($form->{l_ordnumber}) {
 
 591                   SELECT ct.*, b.description AS business,
 
 592                   ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
 
 593                   'oe' AS module, 'order' AS formtype,
 
 596                 JOIN oe o ON (o.$form->{db}_id = ct.id)
 
 597                 LEFT JOIN business b ON (ct.business_id = b.id)
 
 599                   AND o.quotation = '0'|;
 
 605     if ($form->{l_quonumber}) {
 
 607                   SELECT ct.*, b.description AS business,
 
 608                   ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
 
 609                   'oe' AS module, 'quotation' AS formtype,
 
 612                 JOIN oe o ON (o.$form->{db}_id = ct.id)
 
 613                 LEFT JOIN business b ON (ct.business_id = b.id)
 
 615                   AND o.quotation = '1'|;
 
 621                  ORDER BY $form->{sort}|;
 
 623   my $sth = $dbh->prepare($query);
 
 624   $sth->execute || $form->dberror($query);
 
 626   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 627     $ref->{address} = "";
 
 628     map { $ref->{address} .= "$ref->{$_} "; } qw(street zipcode city country);
 
 629     push @{ $form->{CT} }, $ref;
 
 635   $main::lxdebug->leave_sub();