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 #======================================================================
42 $main::lxdebug->enter_sub();
44 my ($self, $myconfig, $form) = @_;
46 my $dbh = $form->dbconnect($myconfig);
47 my $query = qq|SELECT ct.*, b.id AS business, s.*, cp.*
49 LEFT JOIN business b on ct.business_id = b.id
50 LEFT JOIN shipto s on ct.id = s.trans_id
51 LEFT JOIN contacts cp on ct.id = cp.cp_cv_id
52 WHERE ct.id = $form->{id} order by cp.cp_id limit 1|;
53 my $sth = $dbh->prepare($query);
54 $sth->execute || $form->dberror($query);
56 my $ref = $sth->fetchrow_hashref(NAME_lc);
58 map { $form->{$_} = $ref->{$_} } keys %$ref;
61 if ($form->{salesman_id}) {
62 my $query = qq|SELECT ct.name AS salesman
64 WHERE ct.id = $form->{salesman_id}|;
65 my $sth = $dbh->prepare($query);
66 $sth->execute || $form->dberror($query);
68 my ($ref) = $sth->fetchrow_array();
70 $form->{salesman} = $ref;
75 # check if it is orphaned
76 my $arap = ($form->{db} eq 'customer') ? "ar" : "ap";
77 $query = qq|SELECT a.id
79 JOIN $form->{db} ct ON (a.$form->{db}_id = ct.id)
80 WHERE ct.id = $form->{id}
84 JOIN $form->{db} ct ON (a.$form->{db}_id = ct.id)
85 WHERE ct.id = $form->{id}|;
86 $sth = $dbh->prepare($query);
87 $sth->execute || $form->dberror($query);
89 unless ($sth->fetchrow_array) {
90 $form->{status} = "orphaned";
96 $query = qq|SELECT c.accno, c.description
98 JOIN tax t ON (t.chart_id = c.id)
99 WHERE c.link LIKE '%CT_tax%'
101 $sth = $dbh->prepare($query);
102 $sth->execute || $form->dberror($query);
104 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
105 $form->{taxaccounts} .= "$ref->{accno} ";
106 $form->{tax}{$ref->{accno}}{description} = $ref->{description};
109 chop $form->{taxaccounts};
111 # get taxes for customer/vendor
112 $query = qq|SELECT c.accno
114 JOIN $form->{db}tax t ON (t.chart_id = c.id)
115 WHERE t.$form->{db}_id = $form->{id}|;
116 $sth = $dbh->prepare($query);
117 $sth->execute || $form->dberror($query);
119 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
120 $form->{tax}{$ref->{accno}}{taxable} = 1;
125 $query = qq|SELECT id, description
128 $sth = $dbh->prepare($query);
129 $sth->execute || $form->dberror($query);
131 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
132 push @{ $form->{all_business} }, $ref;
138 $main::lxdebug->leave_sub();
142 sub query_titles_and_greetings {
143 $main::lxdebug->enter_sub();
145 my ($self, $myconfig, $form) = @_;
148 my $dbh = $form->dbconnect($myconfig);
150 $query = "SELECT DISTINCT(c.cp_greeting) FROM contacts c WHERE c.cp_greeting LIKE '%'";
151 $sth = $dbh->prepare($query);
152 $sth->execute() || $form->dberror($query);
153 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
154 next unless ($ref->{cp_greeting} =~ /[a-zA-Z]/);
155 $tmp{$ref->{cp_greeting}} = 1;
159 @{$form->{GREETINGS}} = sort(keys(%tmp));
163 $query = "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();
221 $main::lxdebug->enter_sub();
224 my ($self, $myconfig, $form) = @_;
226 # connect to database
227 my $dbh = $form->dbconnect($myconfig);
229 map({ $form->{"cp_${_}"} = $form->{"selected_cp_${_}"} if ($form->{"selected_cp_${_}"}); } qw(title greeting));
231 map { $form->{$_} =~ s/\'/\'\'/g } qw(customernumber name street zipcode city country homepage contact notes cp_title cp_greeting language);
233 # assign value discount, terms, creditlimit
234 $form->{discount} = $form->parse_amount($myconfig, $form->{discount});
235 $form->{discount} /= 100;
237 $form->{taxincluded} *= 1;
238 $form->{obsolete} *= 1;
239 $form->{business} *= 1;
240 $form->{salesman_id} *= 1;
241 $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
246 $query = qq|DELETE FROM customertax
247 WHERE customer_id = $form->{id}|;
248 $dbh->do($query) || $form->dberror($query);
250 $query = qq|DELETE FROM shipto
251 WHERE trans_id = $form->{id}|;
252 $dbh->do($query) || $form->dberror($query);
254 my $uid = rand().time;
256 $uid .= $form->{login};
258 $uid = substr($uid,2,75);
260 $query = qq|INSERT INTO customer (name)
262 $dbh->do($query) || $form->dberror($query);
264 $query = qq|SELECT c.id FROM customer c
265 WHERE c.name = '$uid'|;
266 $sth = $dbh->prepare($query);
267 $sth->execute || $form->dberror($query);
269 ($form->{id}) = $sth->fetchrow_array;
271 if (!$form->{customernumber} && $form->{business}) {
272 $form->{customernumber} = $form->update_business($myconfig, $form->{business});
274 if (!$form->{customernumber}) {
275 $form->{customernumber} = $form->update_defaults($myconfig, "customernumber");
281 $query = qq|UPDATE customer SET
282 customernumber = '$form->{customernumber}',
283 name = '$form->{name}',
284 department_1 = '$form->{department_1}',
285 department_2 = '$form->{department_2}',
286 street = '$form->{street}',
287 zipcode = '$form->{zipcode}',
288 city = '$form->{city}',
289 country = '$form->{country}',
290 homepage = '$form->{homepage}',
291 contact = '$form->{contact}',
292 phone = '$form->{phone}',
293 fax = '$form->{fax}',
294 email = '$form->{email}',
296 bcc = '$form->{bcc}',
297 notes = '$form->{notes}',
298 discount = $form->{discount},
299 creditlimit = $form->{creditlimit},
300 terms = $form->{terms},
301 taxincluded = '$form->{taxincluded}',
302 business_id = $form->{business},
303 taxnumber = '$form->{taxnumber}',
304 sic_code = '$form->{sic}',
305 language = '$form->{language}',
306 account_number = '$form->{account_number}',
307 bank_code = '$form->{bank_code}',
308 bank = '$form->{bank}',
309 obsolete = '$form->{obsolete}',
310 ustid = '$form->{ustid}',
311 username = '$form->{username}',
312 salesman_id = '$form->{salesman_id}',
313 user_password = '$form->{user_password}',
314 c_vendor_id = '$form->{c_vendor_id}'
315 WHERE id = $form->{id}|;
316 $dbh->do($query) || $form->dberror($query);
318 if ($form->{cp_id}) {
319 $query = qq|UPDATE contacts SET
320 cp_greeting = '$form->{cp_greeting}',
321 cp_title = '$form->{cp_title}',
322 cp_givenname = '$form->{cp_givenname}',
323 cp_name = '$form->{cp_name}',
324 cp_email = '$form->{cp_email}',
325 cp_phone1 = '$form->{cp_phone1}',
326 cp_phone2 = '$form->{cp_phone2}'
327 WHERE cp_id = $form->{cp_id}|;
328 } elsif ($form->{cp_name} || $form->{cp_givenname}) {
329 $query = qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2)
330 VALUES ($form->{id}, '$form->{cp_greeting}','$form->{cp_title}','$form->{cp_givenname}','$form->{cp_name}','$form->{cp_email}','$form->{cp_phone1}','$form->{cp_phone2}')|;
332 $dbh->do($query) || $form->dberror($query);
335 foreach $item (split / /, $form->{taxaccounts}) {
336 if ($form->{"tax_$item"}) {
337 $query = qq|INSERT INTO customertax (customer_id, chart_id)
338 VALUES ($form->{id}, (SELECT c.id
340 WHERE c.accno = '$item'))|;
341 $dbh->do($query) || $form->dberror($query);
346 $form->add_shipto($dbh, $form->{id});
350 $main::lxdebug->leave_sub();
355 $main::lxdebug->enter_sub();
357 my ($self, $myconfig, $form) = @_;
359 # connect to database
360 my $dbh = $form->dbconnect($myconfig);
362 map({ $form->{"cp_${_}"} = $form->{"selected_cp_${_}"} if ($form->{"selected_cp_${_}"}); } qw(title greeting));
364 map { $form->{$_} =~ s/\'/\'\'/g } qw(vendornumber name street zipcode city country homepage contact notes cp_title cp_greeting language);
366 $form->{discount} = $form->parse_amount($myconfig, $form->{discount});
367 $form->{discount} /= 100;
369 $form->{taxincluded} *= 1;
370 $form->{obsolete} *= 1;
371 $form->{business} *= 1;
372 $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
377 $query = qq|DELETE FROM vendortax
378 WHERE vendor_id = $form->{id}|;
379 $dbh->do($query) || $form->dberror($query);
381 $query = qq|DELETE FROM shipto
382 WHERE trans_id = $form->{id}|;
383 $dbh->do($query) || $form->dberror($query);
386 $uid .= $form->{login};
388 $query = qq|INSERT INTO vendor (name)
390 $dbh->do($query) || $form->dberror($query);
392 $query = qq|SELECT v.id FROM vendor v
393 WHERE v.name = '$uid'|;
394 $sth = $dbh->prepare($query);
395 $sth->execute || $form->dberror($query);
397 ($form->{id}) = $sth->fetchrow_array;
399 if (!$form->{vendornumber}) {
400 $form->{vendornumber} = $form->update_defaults($myconfig, "vendornumber");
406 $query = qq|UPDATE vendor SET
407 vendornumber = '$form->{vendornumber}',
408 name = '$form->{name}',
409 department_1 = '$form->{department_1}',
410 department_2 = '$form->{department_2}',
411 street = '$form->{street}',
412 zipcode = '$form->{zipcode}',
413 city = '$form->{city}',
414 country = '$form->{country}',
415 homepage = '$form->{homepage}',
416 contact = '$form->{contact}',
417 phone = '$form->{phone}',
418 fax = '$form->{fax}',
419 email = '$form->{email}',
421 bcc = '$form->{bcc}',
422 notes = '$form->{notes}',
423 terms = $form->{terms},
424 discount = $form->{discount},
425 creditlimit = $form->{creditlimit},
426 taxincluded = '$form->{taxincluded}',
427 gifi_accno = '$form->{gifi_accno}',
428 business_id = $form->{business},
429 taxnumber = '$form->{taxnumber}',
430 sic_code = '$form->{sic}',
431 language = '$form->{language}',
432 account_number = '$form->{account_number}',
433 bank_code = '$form->{bank_code}',
434 bank = '$form->{bank}',
435 obsolete = '$form->{obsolete}',
436 ustid = '$form->{ustid}',
437 username = '$form->{username}',
438 user_password = '$form->{user_password}',
439 v_customer_id = '$form->{v_customer_id}'
440 WHERE id = $form->{id}|;
441 $dbh->do($query) || $form->dberror($query);
443 if ($form->{cp_id}) {
444 $query = qq|UPDATE contacts SET
445 cp_greeting = '$form->{cp_greeting}',
446 cp_title = '$form->{cp_title}',
447 cp_givenname = '$form->{cp_givenname}',
448 cp_name = '$form->{cp_name}',
449 cp_email = '$form->{cp_email}',
450 cp_phone1 = '$form->{cp_phone1}',
451 cp_phone2 = '$form->{cp_phone2}'
452 WHERE cp_id = $form->{cp_id}|;
453 } elsif ($form->{cp_name} || $form->{cp_givenname}) {
454 $query = qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2)
455 VALUES ($form->{id}, '$form->{cp_greeting}','$form->{cp_title}','$form->{cp_givenname}','$form->{cp_name}','$form->{cp_email}','$form->{cp_phone1}','$form->{cp_phone2}')|;
457 $dbh->do($query) || $form->dberror($query);
460 foreach $item (split / /, $form->{taxaccounts}) {
461 if ($form->{"tax_$item"}) {
462 $query = qq|INSERT INTO vendortax (vendor_id, chart_id)
463 VALUES ($form->{id}, (SELECT c.id
465 WHERE c.accno = '$item'))|;
466 $dbh->do($query) || $form->dberror($query);
471 $form->add_shipto($dbh, $form->{id});
475 $main::lxdebug->leave_sub();
481 $main::lxdebug->enter_sub();
483 my ($self, $myconfig, $form) = @_;
485 # connect to database
486 my $dbh = $form->dbconnect($myconfig);
489 my $query = qq|DELETE FROM $form->{db}
490 WHERE id = $form->{id}|;
491 $dbh->do($query) || $form->dberror($query);
495 $main::lxdebug->leave_sub();
500 $main::lxdebug->enter_sub();
502 my ($self, $myconfig, $form) = @_;
504 # connect to database
505 my $dbh = $form->dbconnect($myconfig);
508 $form->{sort} = "name" unless ($form->{sort});
510 if ($form->{"$form->{db}number"}) {
511 my $companynumber = $form->like(lc $form->{"$form->{db}number"});
512 $where .= " AND lower(ct.$form->{db}number) LIKE '$companynumber'";
515 my $name = $form->like(lc $form->{name});
516 $where .= " AND lower(ct.name) LIKE '$name'";
518 if ($form->{contact}) {
519 my $contact = $form->like(lc $form->{contact});
520 $where .= " AND lower(ct.contact) LIKE '$contact'";
522 if ($form->{email}) {
523 my $email = $form->like(lc $form->{email});
524 $where .= " AND lower(ct.email) LIKE '$email'";
527 if ($form->{status} eq 'orphaned') {
528 $where .= qq| AND ct.id NOT IN (SELECT o.$form->{db}_id
529 FROM oe o, $form->{db} cv
530 WHERE cv.id = o.$form->{db}_id)|;
531 if ($form->{db} eq 'customer') {
532 $where .= qq| AND ct.id NOT IN (SELECT a.customer_id
533 FROM ar a, customer cv
534 WHERE cv.id = a.customer_id)|;
536 if ($form->{db} eq 'vendor') {
537 $where .= qq| AND ct.id NOT IN (SELECT a.vendor_id
539 WHERE cv.id = a.vendor_id)|;
541 $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
544 my $query = qq|SELECT ct.*, b.description AS business
546 LEFT JOIN business b ON (ct.business_id = b.id)
549 # redo for invoices, orders and quotations
550 if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
552 my ($ar, $union, $module);
555 if ($form->{l_invnumber}) {
556 $ar = ($form->{db} eq 'customer') ? 'ar' : 'ap';
557 $module = ($ar eq 'ar') ? 'is' : 'ir';
559 $query = qq|SELECT ct.*, b.description AS business,
560 a.invnumber, a.ordnumber, a.quonumber, a.id AS invid,
561 '$module' AS module, 'invoice' AS formtype,
562 (a.amount = a.paid) AS closed
564 JOIN $ar a ON (a.$form->{db}_id = ct.id)
565 LEFT JOIN business b ON (ct.business_id = b.id)
567 AND a.invoice = '1'|;
574 if ($form->{l_ordnumber}) {
576 SELECT ct.*, b.description AS business,
577 ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
578 'oe' AS module, 'order' AS formtype,
581 JOIN oe o ON (o.$form->{db}_id = ct.id)
582 LEFT JOIN business b ON (ct.business_id = b.id)
584 AND o.quotation = '0'|;
590 if ($form->{l_quonumber}) {
592 SELECT ct.*, b.description AS business,
593 ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
594 'oe' AS module, 'quotation' AS formtype,
597 JOIN oe o ON (o.$form->{db}_id = ct.id)
598 LEFT JOIN business b ON (ct.business_id = b.id)
600 AND o.quotation = '1'|;
606 ORDER BY $form->{sort}|;
608 my $sth = $dbh->prepare($query);
609 $sth->execute || $form->dberror($query);
611 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
612 $ref->{address} = "";
613 map { $ref->{address} .= "$ref->{$_} "; } qw(street zipcode city country);
614 push @{ $form->{CT} }, $ref;
620 $main::lxdebug->leave_sub();