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 #======================================================================
46 $main::lxdebug->enter_sub();
48 my ( $self, $myconfig, $form ) = @_;
50 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
52 my $dbh = $form->dbconnect($myconfig);
54 qq|SELECT ct.*, b.id AS business, cp.* | .
56 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
57 qq|LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id) | .
58 qq|WHERE (ct.id = ?) | .
59 qq|ORDER BY cp.cp_id LIMIT 1|;
60 my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
62 my $ref = $sth->fetchrow_hashref(NAME_lc);
64 map { $form->{$_} = $ref->{$_} } keys %$ref;
67 if ( $form->{salesman_id} ) {
69 qq|SELECT ct.name AS salesman | .
73 selectrow_query($form, $dbh, $query, $form->{salesman_id});
76 # check if it is orphaned
77 my $arap = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
81 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
82 qq|WHERE ct.id = ? | .
86 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
88 my ($dummy) = selectrow_query($form, $dbh, $query, $form->{id}, $form->{id});
89 $form->{status} = "orphaned" unless ($dummy);
93 $main::lxdebug->leave_sub();
96 sub populate_drop_down_boxes {
97 $main::lxdebug->enter_sub();
99 my ($self, $myconfig, $form, $provided_dbh) = @_;
101 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
104 $query = qq|SELECT id, description FROM business ORDER BY id|;
105 $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
109 qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
110 qq|FROM shipto WHERE (trans_id = ?) AND (module = 'CT')|;
111 $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
114 $query = qq|SELECT cp_id, cp_name FROM contacts WHERE cp_cv_id = ?|;
115 $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
118 $query = qq|SELECT id, description FROM language ORDER BY id|;
119 $form->{languages} = selectall_hashref_query($form, $dbh, $query);
122 $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
123 $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
125 $dbh->disconnect() unless ($provided_dbh);
127 $main::lxdebug->leave_sub();
130 sub query_titles_and_greetings {
131 $main::lxdebug->enter_sub();
133 my ( $self, $myconfig, $form ) = @_;
136 my $dbh = $form->dbconnect($myconfig);
139 qq|SELECT DISTINCT(cp_greeting) | .
141 qq|WHERE cp_greeting ~ '[a-zA-Z]' | .
142 qq|ORDER BY cp_greeting|;
143 $form->{GREETINGS} = [ selectall_array_query($form, $dbh, $query) ];
146 qq|SELECT DISTINCT(greeting) | .
148 qq|WHERE greeting ~ '[a-zA-Z]' | .
150 qq|SELECT DISTINCT(greeting) | .
152 qq|WHERE greeting ~ '[a-zA-Z]' | .
153 qq|ORDER BY greeting|;
155 map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
156 $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
159 qq|SELECT DISTINCT(cp_title) | .
161 qq|WHERE cp_title ~ '[a-zA-Z]'|;
162 $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
165 qq|SELECT DISTINCT(cp_abteilung) | .
167 qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
168 $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
171 $main::lxdebug->leave_sub();
175 $main::lxdebug->enter_sub();
177 my ( $self, $myconfig, $form ) = @_;
179 # set pricegroup to default
180 $form->{klass} = 0 unless ($form->{klass});
182 # connect to database
183 my $dbh = $form->dbconnect_noauto($myconfig);
186 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
187 if ( $form->{"selected_cp_${_}"} );
188 } qw(title greeting abteilung) );
189 $form->{"greeting"} = $form->{"selected_company_greeting"}
190 if ( $form->{"selected_company_greeting"} );
192 # assign value discount, terms, creditlimit
193 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
194 $form->{discount} /= 100;
195 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
197 my ( $query, $sth, $f_id );
200 $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
201 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
203 if (($f_id ne $form->{id}) && ($f_id ne "")) {
204 $main::lxdebug->leave_sub();
209 if (!$form->{customernumber} && $form->{business}) {
210 $form->{customernumber} =
211 $form->update_business($myconfig, $form->{business}, $dbh);
213 if (!$form->{customernumber}) {
214 $form->{customernumber} =
215 $form->update_defaults($myconfig, "customernumber", $dbh);
218 $query = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
219 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
221 $main::lxdebug->leave_sub();
225 $query = qq|SELECT nextval('id')|;
226 ($form->{id}) = selectrow_query($form, $dbh, $query);
228 $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
229 do_query($form, $dbh, $query, $form->{id});
232 $query = qq|UPDATE customer SET | .
233 qq|customernumber = ?, | .
236 qq|department_1 = ?, | .
237 qq|department_2 = ?, | .
251 qq|creditlimit = ?, | .
253 qq|business_id = ?, | .
254 qq|taxnumber = ?, | .
257 qq|account_number = ?, | .
258 qq|bank_code = ?, | .
263 qq|salesman_id = ?, | .
264 qq|language_id = ?, | .
265 qq|payment_id = ?, | .
266 qq|taxzone_id = ?, | .
267 qq|user_password = ?, | .
268 qq|c_vendor_id = ?, | .
272 $form->{customernumber},
275 $form->{department_1},
276 $form->{department_2},
290 $form->{creditlimit},
291 conv_i($form->{terms}),
292 conv_i($form->{business}),
296 $form->{account_number},
299 $form->{obsolete} ? 't' : 'f',
302 conv_i($form->{salesman_id}),
303 conv_i($form->{language_id}),
304 conv_i($form->{payment_id}),
305 conv_i($form->{taxzone_id}, 0),
306 $form->{user_password},
307 $form->{c_vendor_id},
308 conv_i($form->{klass}),
311 do_query( $form, $dbh, $query, @values );
314 if ( $form->{cp_id} ) {
315 $query = qq|UPDATE contacts SET | .
316 qq|cp_greeting = ?, | .
318 qq|cp_givenname = ?, | .
321 qq|cp_phone1 = ?, | .
322 qq|cp_phone2 = ?, | .
323 qq|cp_abteilung = ?, | .
325 qq|cp_mobile1 = ?, | .
326 qq|cp_mobile2 = ?, | .
327 qq|cp_satphone = ?, | .
328 qq|cp_satfax = ?, | .
329 qq|cp_project = ?, | .
330 qq|cp_privatphone = ?, | .
331 qq|cp_privatemail = ?, | .
332 qq|cp_birthday = ? | .
335 $form->{cp_greeting},
337 $form->{cp_givenname},
342 $form->{cp_abteilung},
346 $form->{cp_satphone},
349 $form->{cp_privatphone},
350 $form->{cp_privatemail},
351 $form->{cp_birthday},
354 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
356 qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, | .
357 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
358 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
360 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
363 $form->{cp_greeting},
365 $form->{cp_givenname},
370 $form->{cp_abteilung},
374 $form->{cp_satphone},
377 $form->{cp_privatphone},
378 $form->{cp_privatemail},
382 do_query( $form, $dbh, $query, @values ) if ($query);
385 $form->add_shipto( $dbh, $form->{id}, "CT" );
387 CVar->save_custom_variables('dbh' => $dbh,
389 'trans_id' => $form->{id},
390 'variables' => $form);
392 $rc = $dbh->commit();
395 $main::lxdebug->leave_sub();
400 $main::lxdebug->enter_sub();
402 my ( $self, $myconfig, $form ) = @_;
404 $form->{taxzone_id} *= 1;
405 # connect to database
406 my $dbh = $form->dbconnect_noauto($myconfig);
409 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
410 if ( $form->{"selected_cp_${_}"} );
411 } qw(title greeting abteilung) );
412 $form->{"greeting"} = $form->{"selected_company_greeting"}
413 if ( $form->{"selected_company_greeting"} );
415 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
416 $form->{discount} /= 100;
417 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
422 $query = qq|DELETE FROM shipto WHERE (trans_id = ?) AND (module = 'CT')|;
423 do_query($form, $dbh, $query, $form->{id});
426 $query = qq|SELECT nextval('id')|;
427 ($form->{id}) = selectrow_query($form, $dbh, $query);
429 $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
430 do_query($form, $dbh, $query, $form->{id});
432 if ( !$form->{vendornumber} ) {
433 $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
438 qq|UPDATE vendor SET | .
439 qq| vendornumber = ?, | .
441 qq| greeting = ?, | .
442 qq| department_1 = ?, | .
443 qq| department_2 = ?, | .
448 qq| homepage = ?, | .
457 qq| discount = ?, | .
458 qq| creditlimit = ?, | .
459 qq| business_id = ?, | .
460 qq| taxnumber = ?, | .
461 qq| sic_code = ?, | .
462 qq| language = ?, | .
463 qq| account_number = ?, | .
464 qq| bank_code = ?, | .
466 qq| obsolete = ?, | .
468 qq| payment_id = ?, | .
469 qq| taxzone_id = ?, | .
470 qq| language_id = ?, | .
471 qq| username = ?, | .
472 qq| user_password = ?, | .
473 qq| v_customer_id = ? | .
476 $form->{vendornumber},
479 $form->{department_1},
480 $form->{department_2},
493 conv_i($form->{terms}),
495 $form->{creditlimit},
496 conv_i($form->{business}),
500 $form->{account_number},
503 $form->{obsolete} ? 't' : 'f',
505 conv_i($form->{payment_id}),
506 conv_i($form->{taxzone_id}, 0),
507 conv_i( $form->{language_id}),
509 $form->{user_password},
510 $form->{v_customer_id},
513 do_query($form, $dbh, $query, @values);
516 if ( $form->{cp_id} ) {
517 $query = qq|UPDATE contacts SET | .
518 qq|cp_greeting = ?, | .
520 qq|cp_givenname = ?, | .
523 qq|cp_phone1 = ?, | .
524 qq|cp_phone2 = ?, | .
525 qq|cp_abteilung = ?, | .
527 qq|cp_mobile1 = ?, | .
528 qq|cp_mobile2 = ?, | .
529 qq|cp_satphone = ?, | .
530 qq|cp_satfax = ?, | .
531 qq|cp_project = ?, | .
532 qq|cp_privatphone = ?, | .
533 qq|cp_privatemail = ?, | .
534 qq|cp_birthday = ? | .
537 $form->{cp_greeting},
539 $form->{cp_givenname},
544 $form->{cp_abteilung},
548 $form->{cp_satphone},
551 $form->{cp_privatphone},
552 $form->{cp_privatemail},
553 $form->{cp_birthday},
556 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
558 qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, | .
559 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
560 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
562 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
565 $form->{cp_greeting},
567 $form->{cp_givenname},
572 $form->{cp_abteilung},
576 $form->{cp_satphone},
579 $form->{cp_privatphone},
580 $form->{cp_privatemail},
584 do_query($form, $dbh, $query, @values) if ($query);
587 $form->add_shipto( $dbh, $form->{id}, "CT" );
589 CVar->save_custom_variables('dbh' => $dbh,
591 'trans_id' => $form->{id},
592 'variables' => $form);
594 $rc = $dbh->commit();
597 $main::lxdebug->leave_sub();
602 $main::lxdebug->enter_sub();
604 my ( $self, $myconfig, $form ) = @_;
605 # connect to database
606 my $dbh = $form->dbconnect($myconfig);
609 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
610 my $query = qq|DELETE FROM $cv WHERE id = ?|;
611 do_query($form, $dbh, $query, $form->{id});
615 $main::lxdebug->leave_sub();
619 $main::lxdebug->enter_sub();
621 my ( $self, $myconfig, $form ) = @_;
623 # connect to database
624 my $dbh = $form->dbconnect($myconfig);
626 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
631 my %allowed_sort_columns =
632 map({ $_, 1 } qw(id customernumber name address contact phone fax email
633 taxnumber sic_code business invnumber ordnumber quonumber));
635 $form->{sort} && $allowed_sort_columns{$form->{sort}} ?
636 $form->{sort} : "name";
637 $form->{sort} = $sortorder;
638 $sortorder = "country,city,street" if ($sortorder eq "address");
640 if ($form->{"${cv}number"}) {
641 $where .= " AND ct.${cv}number ILIKE ?";
642 push(@values, '%' . $form->{"${cv}number"} . '%');
645 foreach my $key (qw(name contact email)) {
647 $where .= " AND ct.$key ILIKE ?";
648 push(@values, '%' . $form->{$key} . '%');
652 if ( $form->{status} eq 'orphaned' ) {
654 qq| AND ct.id NOT IN | .
655 qq| (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
656 if ($cv eq 'customer') {
658 qq| AND ct.id NOT IN | .
659 qq| (SELECT a.customer_id FROM ar a, customer cv | .
660 qq| WHERE cv.id = a.customer_id)|;
662 if ($cv eq 'vendor') {
664 qq| AND ct.id NOT IN | .
665 qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
666 qq| WHERE cv.id = a.vendor_id)|;
668 $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
671 if ($form->{obsolete} eq "Y") {
672 $where .= qq| AND obsolete|;
673 } elsif ($form->{obsolete} eq "N") {
674 $where .= qq| AND NOT obsolete|;
677 if ($form->{business_id}) {
678 $where .= qq| AND (business_id = ?)|;
679 push(@values, conv_i($form->{business_id}));
682 my ($cvar_where, @cvar_values) = CVar->build_filter_query('module' => 'CT',
683 'trans_id_field' => 'ct.id',
687 $where .= qq| AND ($cvar_where)|;
688 push @values, @cvar_values;
692 qq|SELECT ct.*, b.description AS business | .
694 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
697 my @saved_values = @values;
698 # redo for invoices, orders and quotations
699 if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
700 my ($ar, $union, $module);
703 if ($form->{l_invnumber}) {
704 my $ar = $cv eq 'customer' ? 'ar' : 'ap';
705 my $module = $ar eq 'ar' ? 'is' : 'ir';
708 qq|SELECT ct.*, b.description AS business, | .
709 qq| a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
710 qq| '$module' AS module, 'invoice' AS formtype, | .
711 qq| (a.amount = a.paid) AS closed | .
713 qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
714 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
715 qq|WHERE $where AND (a.invoice = '1')|;
720 if ( $form->{l_ordnumber} ) {
721 if ($union eq "UNION") {
722 push(@values, @saved_values);
726 qq|SELECT ct.*, b.description AS business,| .
727 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
728 qq| 'oe' AS module, 'order' AS formtype, o.closed | .
730 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
731 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
732 qq|WHERE $where AND (o.quotation = '0')|;
737 if ( $form->{l_quonumber} ) {
738 if ($union eq "UNION") {
739 push(@values, @saved_values);
743 qq|SELECT ct.*, b.description AS business, | .
744 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
745 qq| 'oe' AS module, 'quotation' AS formtype, o.closed | .
747 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
748 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
749 qq|WHERE $where AND (o.quotation = '1')|;
753 $query .= qq| ORDER BY $sortorder|;
755 $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
756 map({ my $ref = $_; $ref->{address} = join(" ", map({ $ref->{$_} } qw(street zipcode city country))); }
759 $main::lxdebug->leave_sub();
763 $main::lxdebug->enter_sub();
765 my ( $self, $myconfig, $form ) = @_;
766 my $dbh = $form->dbconnect($myconfig);
768 qq|SELECT * FROM contacts c | .
769 qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
770 my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
771 my $ref = $sth->fetchrow_hashref(NAME_lc);
773 map { $form->{$_} = $ref->{$_} } keys %$ref;
778 $main::lxdebug->leave_sub();
782 $main::lxdebug->enter_sub();
784 my ( $self, $myconfig, $form ) = @_;
785 my $dbh = $form->dbconnect($myconfig);
786 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
787 my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
789 my $ref = $sth->fetchrow_hashref(NAME_lc);
791 map { $form->{$_} = $ref->{$_} } keys %$ref;
796 $main::lxdebug->leave_sub();
800 $main::lxdebug->enter_sub();
802 my ( $self, $myconfig, $form ) = @_;
803 my $dbh = $form->dbconnect($myconfig);
805 my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
806 my $db = $form->{db} eq "customer" ? "customer" : "vendor";
808 my $where = " WHERE 1=1 ";
811 if ($form->{shipto_id} && ($arap eq "ar")) {
812 $where .= "AND ${arap}.shipto_id = ?";
813 push(@values, $form->{shipto_id});
815 $where .= "AND ${arap}.${db}_id = ?";
816 push(@values, $form->{id});
820 $where .= "AND ${arap}.transdate >= ?";
821 push(@values, conv_date($form->{from}));
824 $where .= "AND ${arap}.transdate <= ?";
825 push(@values, conv_date($form->{to}));
828 qq|SELECT s.shiptoname, i.qty, | .
829 qq| ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
830 qq| i.description, i.unit, i.sellprice | .
832 qq|LEFT JOIN shipto s ON | .
834 ? qq|(ar.shipto_id = s.shipto_id) |
835 : qq|(ap.id = s.trans_id) |) .
836 qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
837 qq|LEFT join parts p ON (p.id = i.parts_id) | .
839 qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
841 $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
845 $main::lxdebug->leave_sub();