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 #======================================================================
49 $main::lxdebug->enter_sub();
51 my ( $self, $myconfig, $form ) = @_;
53 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
55 my $dbh = $form->dbconnect($myconfig);
57 qq|SELECT ct.*, b.id AS business, cp.* | .
59 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
60 qq|LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id) | .
61 qq|WHERE (ct.id = ?) | .
62 qq|ORDER BY cp.cp_id LIMIT 1|;
63 my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
65 my $ref = $sth->fetchrow_hashref(NAME_lc);
67 map { $form->{$_} = $ref->{$_} } keys %$ref;
70 if ( $form->{salesman_id} ) {
72 qq|SELECT ct.name AS salesman | .
76 selectrow_query($form, $dbh, $query, $form->{salesman_id});
79 my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
81 qq|SELECT n.*, n.itime::DATE AS created_on,
82 e.name AS created_by_name, e.login AS created_by_login
84 LEFT JOIN employee e ON (n.created_by = e.id)
85 WHERE (n.trans_id = ?) AND (n.trans_module = 'ct')|;
86 $form->{NOTES} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
89 qq|SELECT fu.follow_up_date, fu.done AS follow_up_done, e.name AS created_for_name, e.name AS created_for_login
91 LEFT JOIN employee e ON (fu.created_for_user = e.id)
92 WHERE (fu.note_id = ?)
93 AND NOT COALESCE(fu.done, FALSE)
94 AND ( (fu.created_by = ?)
95 OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?)))|;
96 $sth = prepare_query($form, $dbh, $query);
98 foreach my $note (@{ $form->{NOTES} }) {
99 do_statement($form, $sth, $query, conv_i($note->{id}), conv_i($note->{created_by}), conv_i($employee_id));
100 $ref = $sth->fetchrow_hashref();
102 map { $note->{$_} = $ref->{$_} } keys %{ $ref } if ($ref);
107 if ($form->{edit_note_id}) {
109 qq|SELECT n.id AS NOTE_id, n.subject AS NOTE_subject, n.body AS NOTE_body,
110 fu.id AS FU_id, fu.follow_up_date AS FU_date, fu.done AS FU_done, fu.created_for_user AS FU_created_for_user
112 LEFT JOIN follow_ups fu ON ((n.id = fu.note_id) AND NOT COALESCE(fu.done, FALSE))
114 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{edit_note_id}));
117 foreach my $key (keys %{ $ref }) {
119 $new_key =~ s/^([^_]+)/\U\1\E/;
120 $form->{$new_key} = $ref->{$key};
125 # check if it is orphaned
126 my $arap = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
130 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
131 qq|WHERE ct.id = ? | .
135 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
137 my ($dummy) = selectrow_query($form, $dbh, $query, $form->{id}, $form->{id});
138 $form->{status} = "orphaned" unless ($dummy);
142 $main::lxdebug->leave_sub();
145 sub populate_drop_down_boxes {
146 $main::lxdebug->enter_sub();
148 my ($self, $myconfig, $form, $provided_dbh) = @_;
150 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
153 $query = qq|SELECT id, description FROM business ORDER BY id|;
154 $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
158 qq|SELECT shipto_id, shiptoname, shiptodepartment_1, shiptostreet, shiptocity
160 WHERE (trans_id = ?) AND (module = 'CT')|;
161 $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
164 $query = qq|SELECT cp_id, cp_name, cp_givenname FROM contacts WHERE cp_cv_id = ? ORDER BY cp_name|;
165 $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
168 $query = qq|SELECT id, description FROM language ORDER BY id|;
169 $form->{languages} = selectall_hashref_query($form, $dbh, $query);
172 $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
173 $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
175 $dbh->disconnect() unless ($provided_dbh);
177 $main::lxdebug->leave_sub();
180 sub query_titles_and_greetings {
181 $main::lxdebug->enter_sub();
183 my ( $self, $myconfig, $form ) = @_;
186 my $dbh = $form->dbconnect($myconfig);
189 qq|SELECT DISTINCT(cp_greeting) | .
191 qq|WHERE cp_greeting ~ '[a-zA-Z]' | .
192 qq|ORDER BY cp_greeting|;
193 $form->{GREETINGS} = [ selectall_array_query($form, $dbh, $query) ];
196 qq|SELECT DISTINCT(greeting) | .
198 qq|WHERE greeting ~ '[a-zA-Z]' | .
200 qq|SELECT DISTINCT(greeting) | .
202 qq|WHERE greeting ~ '[a-zA-Z]' | .
203 qq|ORDER BY greeting|;
205 map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
206 $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
209 qq|SELECT DISTINCT(cp_title) | .
211 qq|WHERE cp_title ~ '[a-zA-Z]'|;
212 $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
215 qq|SELECT DISTINCT(cp_abteilung) | .
217 qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
218 $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
221 $main::lxdebug->leave_sub();
225 $main::lxdebug->enter_sub();
227 my ( $self, $myconfig, $form ) = @_;
229 # set pricegroup to default
230 $form->{klass} = 0 unless ($form->{klass});
232 # connect to database
233 my $dbh = $form->dbconnect_noauto($myconfig);
236 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
237 if ( $form->{"selected_cp_${_}"} );
238 } qw(title greeting abteilung) );
239 $form->{"greeting"} = $form->{"selected_company_greeting"}
240 if ( $form->{"selected_company_greeting"} );
242 # assign value discount, terms, creditlimit
243 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
244 $form->{discount} /= 100;
245 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
247 my ( $query, $sth, $f_id );
250 $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
251 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
253 if (($f_id ne $form->{id}) && ($f_id ne "")) {
254 $main::lxdebug->leave_sub();
259 if (!$form->{customernumber} && $form->{business}) {
260 $form->{customernumber} =
261 $form->update_business($myconfig, $form->{business}, $dbh);
263 if (!$form->{customernumber}) {
264 $form->{customernumber} =
265 $form->update_defaults($myconfig, "customernumber", $dbh);
268 $query = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
269 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
271 $main::lxdebug->leave_sub();
275 $query = qq|SELECT nextval('id')|;
276 ($form->{id}) = selectrow_query($form, $dbh, $query);
278 $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
279 do_query($form, $dbh, $query, $form->{id});
282 $query = qq|UPDATE customer SET | .
283 qq|customernumber = ?, | .
286 qq|department_1 = ?, | .
287 qq|department_2 = ?, | .
301 qq|creditlimit = ?, | .
303 qq|business_id = ?, | .
304 qq|taxnumber = ?, | .
306 qq|account_number = ?, | .
307 qq|bank_code = ?, | .
312 qq|direct_debit = ?, | .
315 qq|salesman_id = ?, | .
316 qq|language_id = ?, | .
317 qq|payment_id = ?, | .
318 qq|taxzone_id = ?, | .
319 qq|user_password = ?, | .
320 qq|c_vendor_id = ?, | .
324 $form->{customernumber},
327 $form->{department_1},
328 $form->{department_2},
342 $form->{creditlimit},
343 conv_i($form->{terms}),
344 conv_i($form->{business}),
347 $form->{account_number},
352 $form->{obsolete} ? 't' : 'f',
353 $form->{direct_debit} ? 't' : 'f',
356 conv_i($form->{salesman_id}),
357 conv_i($form->{language_id}),
358 conv_i($form->{payment_id}),
359 conv_i($form->{taxzone_id}, 0),
360 $form->{user_password},
361 $form->{c_vendor_id},
362 conv_i($form->{klass}),
365 do_query( $form, $dbh, $query, @values );
368 if ( $form->{cp_id} ) {
369 $query = qq|UPDATE contacts SET | .
370 qq|cp_greeting = ?, | .
372 qq|cp_givenname = ?, | .
375 qq|cp_phone1 = ?, | .
376 qq|cp_phone2 = ?, | .
377 qq|cp_abteilung = ?, | .
379 qq|cp_mobile1 = ?, | .
380 qq|cp_mobile2 = ?, | .
381 qq|cp_satphone = ?, | .
382 qq|cp_satfax = ?, | .
383 qq|cp_project = ?, | .
384 qq|cp_privatphone = ?, | .
385 qq|cp_privatemail = ?, | .
386 qq|cp_birthday = ? | .
389 $form->{cp_greeting},
391 $form->{cp_givenname},
396 $form->{cp_abteilung},
400 $form->{cp_satphone},
403 $form->{cp_privatphone},
404 $form->{cp_privatemail},
405 $form->{cp_birthday},
408 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
410 qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, | .
411 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
412 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
414 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
417 $form->{cp_greeting},
419 $form->{cp_givenname},
424 $form->{cp_abteilung},
428 $form->{cp_satphone},
431 $form->{cp_privatphone},
432 $form->{cp_privatemail},
436 do_query( $form, $dbh, $query, @values ) if ($query);
439 $form->add_shipto( $dbh, $form->{id}, "CT" );
441 $self->_save_note('dbh' => $dbh);
442 $self->_delete_selected_notes('dbh' => $dbh);
444 CVar->save_custom_variables('dbh' => $dbh,
446 'trans_id' => $form->{id},
447 'variables' => $form);
449 $rc = $dbh->commit();
452 $main::lxdebug->leave_sub();
457 $main::lxdebug->enter_sub();
459 my ( $self, $myconfig, $form ) = @_;
461 $form->{taxzone_id} *= 1;
462 # connect to database
463 my $dbh = $form->dbconnect_noauto($myconfig);
466 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
467 if ( $form->{"selected_cp_${_}"} );
468 } qw(title greeting abteilung) );
469 $form->{"greeting"} = $form->{"selected_company_greeting"}
470 if ( $form->{"selected_company_greeting"} );
472 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
473 $form->{discount} /= 100;
474 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
479 $query = qq|SELECT nextval('id')|;
480 ($form->{id}) = selectrow_query($form, $dbh, $query);
482 $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
483 do_query($form, $dbh, $query, $form->{id});
485 if ( !$form->{vendornumber} ) {
486 $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
491 qq|UPDATE vendor SET | .
492 qq| vendornumber = ?, | .
494 qq| greeting = ?, | .
495 qq| department_1 = ?, | .
496 qq| department_2 = ?, | .
501 qq| homepage = ?, | .
510 qq| discount = ?, | .
511 qq| creditlimit = ?, | .
512 qq| business_id = ?, | .
513 qq| taxnumber = ?, | .
514 qq| language = ?, | .
515 qq| account_number = ?, | .
516 qq| bank_code = ?, | .
520 qq| obsolete = ?, | .
521 qq| direct_debit = ?, | .
523 qq| payment_id = ?, | .
524 qq| taxzone_id = ?, | .
525 qq| language_id = ?, | .
526 qq| username = ?, | .
527 qq| user_password = ?, | .
528 qq| v_customer_id = ? | .
531 $form->{vendornumber},
534 $form->{department_1},
535 $form->{department_2},
548 conv_i($form->{terms}),
550 $form->{creditlimit},
551 conv_i($form->{business}),
554 $form->{account_number},
559 $form->{obsolete} ? 't' : 'f',
560 $form->{direct_debit} ? 't' : 'f',
562 conv_i($form->{payment_id}),
563 conv_i($form->{taxzone_id}, 0),
564 conv_i( $form->{language_id}),
566 $form->{user_password},
567 $form->{v_customer_id},
570 do_query($form, $dbh, $query, @values);
573 if ( $form->{cp_id} ) {
574 $query = qq|UPDATE contacts SET | .
575 qq|cp_greeting = ?, | .
577 qq|cp_givenname = ?, | .
580 qq|cp_phone1 = ?, | .
581 qq|cp_phone2 = ?, | .
582 qq|cp_abteilung = ?, | .
584 qq|cp_mobile1 = ?, | .
585 qq|cp_mobile2 = ?, | .
586 qq|cp_satphone = ?, | .
587 qq|cp_satfax = ?, | .
588 qq|cp_project = ?, | .
589 qq|cp_privatphone = ?, | .
590 qq|cp_privatemail = ?, | .
591 qq|cp_birthday = ? | .
594 $form->{cp_greeting},
596 $form->{cp_givenname},
601 $form->{cp_abteilung},
605 $form->{cp_satphone},
608 $form->{cp_privatphone},
609 $form->{cp_privatemail},
610 $form->{cp_birthday},
613 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
615 qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, | .
616 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
617 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
619 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
622 $form->{cp_greeting},
624 $form->{cp_givenname},
629 $form->{cp_abteilung},
633 $form->{cp_satphone},
636 $form->{cp_privatphone},
637 $form->{cp_privatemail},
641 do_query($form, $dbh, $query, @values) if ($query);
644 $form->add_shipto( $dbh, $form->{id}, "CT" );
646 $self->_save_note('dbh' => $dbh);
647 $self->_delete_selected_notes('dbh' => $dbh);
649 CVar->save_custom_variables('dbh' => $dbh,
651 'trans_id' => $form->{id},
652 'variables' => $form);
654 $rc = $dbh->commit();
657 $main::lxdebug->leave_sub();
662 $main::lxdebug->enter_sub();
664 my ( $self, $myconfig, $form ) = @_;
665 # connect to database
666 my $dbh = $form->dbconnect($myconfig);
669 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
670 my $query = qq|DELETE FROM $cv WHERE id = ?|;
671 do_query($form, $dbh, $query, $form->{id});
675 $main::lxdebug->leave_sub();
679 $main::lxdebug->enter_sub();
681 my ( $self, $myconfig, $form ) = @_;
683 # connect to database
684 my $dbh = $form->dbconnect($myconfig);
686 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
691 my %allowed_sort_columns =
692 map({ $_, 1 } qw(id customernumber vendornumber name contact phone fax email
693 taxnumber business invnumber ordnumber quonumber));
694 $sortorder = $form->{sort} && $allowed_sort_columns{$form->{sort}} ? $form->{sort} : "name";
695 $form->{sort} = $sortorder;
696 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
698 if ($sortorder ne 'id') {
699 $sortorder = "lower($sortorder) ${sortdir}";
701 $sortorder .= " ${sortdir}";
704 if ($form->{"${cv}number"}) {
705 $where .= " AND ct.${cv}number ILIKE ?";
706 push(@values, '%' . $form->{"${cv}number"} . '%');
709 foreach my $key (qw(name contact email)) {
711 $where .= " AND ct.$key ILIKE ?";
712 push(@values, '%' . $form->{$key} . '%');
716 if ($form->{cp_name}) {
717 $where .= " AND ct.id IN (SELECT cp_cv_id FROM contacts WHERE lower(cp_name) LIKE lower(?))";
718 push @values, '%' . $form->{cp_name} . '%';
721 if ($form->{addr_city}) {
722 $where .= " AND ((lower(ct.city) LIKE lower(?))
727 WHERE (module = 'CT')
728 AND (lower(shiptocity) LIKE lower(?))
731 push @values, ('%' . $form->{addr_city} . '%') x 2;
734 if ( $form->{status} eq 'orphaned' ) {
736 qq| AND ct.id NOT IN | .
737 qq| (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
738 if ($cv eq 'customer') {
740 qq| AND ct.id NOT IN | .
741 qq| (SELECT a.customer_id FROM ar a, customer cv | .
742 qq| WHERE cv.id = a.customer_id)|;
744 if ($cv eq 'vendor') {
746 qq| AND ct.id NOT IN | .
747 qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
748 qq| WHERE cv.id = a.vendor_id)|;
750 $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
753 if ($form->{obsolete} eq "Y") {
754 $where .= qq| AND obsolete|;
755 } elsif ($form->{obsolete} eq "N") {
756 $where .= qq| AND NOT obsolete|;
759 if ($form->{business_id}) {
760 $where .= qq| AND (business_id = ?)|;
761 push(@values, conv_i($form->{business_id}));
764 my ($cvar_where, @cvar_values) = CVar->build_filter_query('module' => 'CT',
765 'trans_id_field' => 'ct.id',
769 $where .= qq| AND ($cvar_where)|;
770 push @values, @cvar_values;
773 if ($form->{addr_street}) {
774 $where .= qq| AND (street ILIKE ?)|;
775 push @values, '%' . $form->{addr_street} . '%';
778 if ($form->{addr_zipcode}) {
779 $where .= qq| AND (zipcode ILIKE ?)|;
780 push @values, $form->{addr_zipcode} . '%';
784 qq|SELECT ct.*, b.description AS business | .
786 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
789 my @saved_values = @values;
790 # redo for invoices, orders and quotations
791 if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
792 my ($ar, $union, $module);
795 if ($form->{l_invnumber}) {
796 my $ar = $cv eq 'customer' ? 'ar' : 'ap';
797 my $module = $ar eq 'ar' ? 'is' : 'ir';
800 qq|SELECT ct.*, b.description AS business, | .
801 qq| a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
802 qq| '$module' AS module, 'invoice' AS formtype, | .
803 qq| (a.amount = a.paid) AS closed | .
805 qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
806 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
807 qq|WHERE $where AND (a.invoice = '1')|;
812 if ( $form->{l_ordnumber} ) {
813 if ($union eq "UNION") {
814 push(@values, @saved_values);
818 qq|SELECT ct.*, b.description AS business,| .
819 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
820 qq| 'oe' AS module, 'order' AS formtype, o.closed | .
822 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
823 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
824 qq|WHERE $where AND (o.quotation = '0')|;
829 if ( $form->{l_quonumber} ) {
830 if ($union eq "UNION") {
831 push(@values, @saved_values);
835 qq|SELECT ct.*, b.description AS business, | .
836 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
837 qq| 'oe' AS module, 'quotation' AS formtype, o.closed | .
839 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
840 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
841 qq|WHERE $where AND (o.quotation = '1')|;
845 $query .= qq| ORDER BY $sortorder|;
847 $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
849 $main::lxdebug->leave_sub();
853 $main::lxdebug->enter_sub();
855 my ( $self, $myconfig, $form ) = @_;
856 my $dbh = $form->dbconnect($myconfig);
858 qq|SELECT * FROM contacts c | .
859 qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
860 my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
861 my $ref = $sth->fetchrow_hashref(NAME_lc);
863 map { $form->{$_} = $ref->{$_} } keys %$ref;
865 $query = qq|SELECT COUNT(cp_id) AS used FROM (
866 SELECT cp_id FROM oe UNION
867 SELECT cp_id FROM ar UNION
868 SELECT cp_id FROM ap UNION
869 SELECT cp_id FROM delivery_orders
870 ) AS cpid WHERE cp_id = ? OR ? = 0|;
871 ($form->{cp_used}) = selectfirst_array_query($form, $dbh, $query, ($form->{cp_id})x2);
876 $main::lxdebug->leave_sub();
880 $main::lxdebug->enter_sub();
882 my ( $self, $myconfig, $form ) = @_;
883 my $dbh = $form->dbconnect($myconfig);
884 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
885 my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
887 my $ref = $sth->fetchrow_hashref(NAME_lc);
889 map { $form->{$_} = $ref->{$_} } keys %$ref;
891 $query = qq|SELECT COUNT(shipto_id) AS used FROM (
892 SELECT shipto_id FROM oe UNION
893 SELECT shipto_id FROM ar UNION
894 SELECT shipto_id FROM delivery_orders
895 ) AS stid WHERE shipto_id = ? OR ? = 0|;
896 ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
901 $main::lxdebug->leave_sub();
905 $main::lxdebug->enter_sub();
907 my ( $self, $myconfig, $form ) = @_;
908 my $dbh = $form->dbconnect($myconfig);
910 my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
911 my $db = $form->{db} eq "customer" ? "customer" : "vendor";
913 my $where = " WHERE 1=1 ";
916 if ($form->{shipto_id} && ($arap eq "ar")) {
917 $where .= "AND ${arap}.shipto_id = ?";
918 push(@values, $form->{shipto_id});
920 $where .= "AND ${arap}.${db}_id = ?";
921 push(@values, $form->{id});
925 $where .= "AND ${arap}.transdate >= ?";
926 push(@values, conv_date($form->{from}));
929 $where .= "AND ${arap}.transdate <= ?";
930 push(@values, conv_date($form->{to}));
933 qq|SELECT s.shiptoname, i.qty, | .
934 qq| ${arap}.id, ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
935 qq| i.description, i.unit, i.sellprice, | .
936 qq| oe.id AS oe_id | .
938 qq|LEFT JOIN shipto s ON | .
940 ? qq|(ar.shipto_id = s.shipto_id) |
941 : qq|(ap.id = s.trans_id) |) .
942 qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
943 qq|LEFT join parts p ON (p.id = i.parts_id) | .
944 qq|LEFT JOIN oe ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '') | .
946 qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
948 $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
952 $main::lxdebug->leave_sub();
956 $main::lxdebug->enter_sub();
961 my $form = $main::form;
963 Common::check_params(\%params, 'dbh');
965 if (!$form->{NOTE_subject}) {
966 $main::lxdebug->leave_sub();
970 my $dbh = $params{dbh};
974 'id' => $form->{NOTE_id},
975 'subject' => $form->{NOTE_subject},
976 'body' => $form->{NOTE_body},
977 'trans_id' => $form->{id},
978 'trans_module' => 'ct',
981 $note{id} = Notes->save(%note);
983 if ($form->{FU_date}) {
985 'id' => $form->{FU_id},
986 'note_id' => $note{id},
987 'follow_up_date' => $form->{FU_date},
988 'created_for_user' => $form->{FU_created_for_user},
989 'done' => $form->{FU_done} ? 1 : 0,
990 'subject' => $form->{NOTE_subject},
991 'body' => $form->{NOTE_body},
994 'trans_id' => $form->{id},
995 'trans_type' => $form->{db} eq 'customer' ? 'customer' : 'vendor',
996 'trans_info' => $form->{name},
1001 $follow_up{id} = FU->save(%follow_up);
1003 } elsif ($form->{FU_id}) {
1004 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
1005 do_query($form, $dbh, qq|DELETE FROM follow_ups WHERE id = ?|, conv_i($form->{FU_id}));
1008 delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
1010 $main::lxdebug->leave_sub();
1013 sub _delete_selected_notes {
1014 $main::lxdebug->enter_sub();
1019 Common::check_params(\%params, 'dbh');
1021 my $form = $main::form;
1022 my $dbh = $params{dbh};
1024 foreach my $i (1 .. $form->{NOTES_rowcount}) {
1025 next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
1027 Notes->delete('dbh' => $params{dbh},
1028 'id' => $form->{"NOTE_id_$i"});
1031 $main::lxdebug->leave_sub();
1035 $main::lxdebug->enter_sub();
1038 my $shipto_id = shift;
1040 my $form = $main::form;
1041 my %myconfig = %main::myconfig;
1042 my $dbh = $form->get_standard_dbh(\%myconfig);
1044 do_query($form, $dbh, qq|UPDATE shipto SET trans_id = NULL WHERE shipto_id = ?|, $shipto_id);
1048 $main::lxdebug->leave_sub();
1052 $main::lxdebug->enter_sub();
1055 my $shipto_id = shift;
1057 my $form = $main::form;
1058 my %myconfig = %main::myconfig;
1059 my $dbh = $form->get_standard_dbh(\%myconfig);
1061 do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $shipto_id);
1065 $main::lxdebug->leave_sub();
1069 $main::lxdebug->enter_sub();
1074 Common::check_params(\%params, qw(vc id));
1076 my $myconfig = \%main::myconfig;
1077 my $form = $main::form;
1079 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1081 my $table = $params{vc} eq 'customer' ? 'customer' : 'vendor';
1082 my @ids = ref $params{id} eq 'ARRAY' ? @{ $params{id} } : ($params{id});
1083 my $placeholders = ('?') x scalar @ids;
1084 my $query = qq|SELECT id, name, account_number, bank, bank_code, iban, bic
1086 WHERE id IN (${placeholders})|;
1088 my $result = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
1090 if (ref $params{id} eq 'ARRAY') {
1091 $result = { map { $_->{id} => $_ } @{ $result } };
1093 $result = $result->[0] || { 'id' => $params{id} };
1096 $main::lxdebug->leave_sub();