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 = ?, | .
310 qq|direct_debit = ?, | .
313 qq|salesman_id = ?, | .
314 qq|language_id = ?, | .
315 qq|payment_id = ?, | .
316 qq|taxzone_id = ?, | .
317 qq|user_password = ?, | .
318 qq|c_vendor_id = ?, | .
322 $form->{customernumber},
325 $form->{department_1},
326 $form->{department_2},
340 $form->{creditlimit},
341 conv_i($form->{terms}),
342 conv_i($form->{business}),
345 $form->{account_number},
348 $form->{obsolete} ? 't' : 'f',
349 $form->{direct_debit} ? 't' : 'f',
352 conv_i($form->{salesman_id}),
353 conv_i($form->{language_id}),
354 conv_i($form->{payment_id}),
355 conv_i($form->{taxzone_id}, 0),
356 $form->{user_password},
357 $form->{c_vendor_id},
358 conv_i($form->{klass}),
361 do_query( $form, $dbh, $query, @values );
364 if ( $form->{cp_id} ) {
365 $query = qq|UPDATE contacts SET | .
366 qq|cp_greeting = ?, | .
368 qq|cp_givenname = ?, | .
371 qq|cp_phone1 = ?, | .
372 qq|cp_phone2 = ?, | .
373 qq|cp_abteilung = ?, | .
375 qq|cp_mobile1 = ?, | .
376 qq|cp_mobile2 = ?, | .
377 qq|cp_satphone = ?, | .
378 qq|cp_satfax = ?, | .
379 qq|cp_project = ?, | .
380 qq|cp_privatphone = ?, | .
381 qq|cp_privatemail = ?, | .
382 qq|cp_birthday = ? | .
385 $form->{cp_greeting},
387 $form->{cp_givenname},
392 $form->{cp_abteilung},
396 $form->{cp_satphone},
399 $form->{cp_privatphone},
400 $form->{cp_privatemail},
401 $form->{cp_birthday},
404 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
406 qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, | .
407 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
408 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
410 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
413 $form->{cp_greeting},
415 $form->{cp_givenname},
420 $form->{cp_abteilung},
424 $form->{cp_satphone},
427 $form->{cp_privatphone},
428 $form->{cp_privatemail},
432 do_query( $form, $dbh, $query, @values ) if ($query);
435 $form->add_shipto( $dbh, $form->{id}, "CT" );
437 $self->_save_note('dbh' => $dbh);
438 $self->_delete_selected_notes('dbh' => $dbh);
440 CVar->save_custom_variables('dbh' => $dbh,
442 'trans_id' => $form->{id},
443 'variables' => $form);
445 $rc = $dbh->commit();
448 $main::lxdebug->leave_sub();
453 $main::lxdebug->enter_sub();
455 my ( $self, $myconfig, $form ) = @_;
457 $form->{taxzone_id} *= 1;
458 # connect to database
459 my $dbh = $form->dbconnect_noauto($myconfig);
462 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
463 if ( $form->{"selected_cp_${_}"} );
464 } qw(title greeting abteilung) );
465 $form->{"greeting"} = $form->{"selected_company_greeting"}
466 if ( $form->{"selected_company_greeting"} );
468 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
469 $form->{discount} /= 100;
470 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
475 $query = qq|SELECT nextval('id')|;
476 ($form->{id}) = selectrow_query($form, $dbh, $query);
478 $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
479 do_query($form, $dbh, $query, $form->{id});
481 if ( !$form->{vendornumber} ) {
482 $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
487 qq|UPDATE vendor SET | .
488 qq| vendornumber = ?, | .
490 qq| greeting = ?, | .
491 qq| department_1 = ?, | .
492 qq| department_2 = ?, | .
497 qq| homepage = ?, | .
506 qq| discount = ?, | .
507 qq| creditlimit = ?, | .
508 qq| business_id = ?, | .
509 qq| taxnumber = ?, | .
510 qq| language = ?, | .
511 qq| account_number = ?, | .
512 qq| bank_code = ?, | .
514 qq| obsolete = ?, | .
515 qq| direct_debit = ?, | .
517 qq| payment_id = ?, | .
518 qq| taxzone_id = ?, | .
519 qq| language_id = ?, | .
520 qq| username = ?, | .
521 qq| user_password = ?, | .
522 qq| v_customer_id = ? | .
525 $form->{vendornumber},
528 $form->{department_1},
529 $form->{department_2},
542 conv_i($form->{terms}),
544 $form->{creditlimit},
545 conv_i($form->{business}),
548 $form->{account_number},
551 $form->{obsolete} ? 't' : 'f',
552 $form->{direct_debit} ? 't' : 'f',
554 conv_i($form->{payment_id}),
555 conv_i($form->{taxzone_id}, 0),
556 conv_i( $form->{language_id}),
558 $form->{user_password},
559 $form->{v_customer_id},
562 do_query($form, $dbh, $query, @values);
565 if ( $form->{cp_id} ) {
566 $query = qq|UPDATE contacts SET | .
567 qq|cp_greeting = ?, | .
569 qq|cp_givenname = ?, | .
572 qq|cp_phone1 = ?, | .
573 qq|cp_phone2 = ?, | .
574 qq|cp_abteilung = ?, | .
576 qq|cp_mobile1 = ?, | .
577 qq|cp_mobile2 = ?, | .
578 qq|cp_satphone = ?, | .
579 qq|cp_satfax = ?, | .
580 qq|cp_project = ?, | .
581 qq|cp_privatphone = ?, | .
582 qq|cp_privatemail = ?, | .
583 qq|cp_birthday = ? | .
586 $form->{cp_greeting},
588 $form->{cp_givenname},
593 $form->{cp_abteilung},
597 $form->{cp_satphone},
600 $form->{cp_privatphone},
601 $form->{cp_privatemail},
602 $form->{cp_birthday},
605 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
607 qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, | .
608 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
609 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
611 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
614 $form->{cp_greeting},
616 $form->{cp_givenname},
621 $form->{cp_abteilung},
625 $form->{cp_satphone},
628 $form->{cp_privatphone},
629 $form->{cp_privatemail},
633 do_query($form, $dbh, $query, @values) if ($query);
636 $form->add_shipto( $dbh, $form->{id}, "CT" );
638 $self->_save_note('dbh' => $dbh);
639 $self->_delete_selected_notes('dbh' => $dbh);
641 CVar->save_custom_variables('dbh' => $dbh,
643 'trans_id' => $form->{id},
644 'variables' => $form);
646 $rc = $dbh->commit();
649 $main::lxdebug->leave_sub();
654 $main::lxdebug->enter_sub();
656 my ( $self, $myconfig, $form ) = @_;
657 # connect to database
658 my $dbh = $form->dbconnect($myconfig);
661 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
662 my $query = qq|DELETE FROM $cv WHERE id = ?|;
663 do_query($form, $dbh, $query, $form->{id});
667 $main::lxdebug->leave_sub();
671 $main::lxdebug->enter_sub();
673 my ( $self, $myconfig, $form ) = @_;
675 # connect to database
676 my $dbh = $form->dbconnect($myconfig);
678 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
683 my %allowed_sort_columns =
684 map({ $_, 1 } qw(id customernumber vendornumber name contact phone fax email
685 taxnumber business invnumber ordnumber quonumber));
686 $sortorder = $form->{sort} && $allowed_sort_columns{$form->{sort}} ? $form->{sort} : "name";
687 $form->{sort} = $sortorder;
688 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
690 if ($sortorder ne 'id') {
691 $sortorder = "lower($sortorder) ${sortdir}";
693 $sortorder .= " ${sortdir}";
696 if ($form->{"${cv}number"}) {
697 $where .= " AND ct.${cv}number ILIKE ?";
698 push(@values, '%' . $form->{"${cv}number"} . '%');
701 foreach my $key (qw(name contact email)) {
703 $where .= " AND ct.$key ILIKE ?";
704 push(@values, '%' . $form->{$key} . '%');
708 if ($form->{cp_name}) {
709 $where .= " AND ct.id IN (SELECT cp_cv_id FROM contacts WHERE lower(cp_name) LIKE lower(?))";
710 push @values, '%' . $form->{cp_name} . '%';
713 if ($form->{addr_city}) {
714 $where .= " AND ((lower(ct.city) LIKE lower(?))
719 WHERE (module = 'CT')
720 AND (lower(shiptocity) LIKE lower(?))
723 push @values, ('%' . $form->{addr_city} . '%') x 2;
726 if ( $form->{status} eq 'orphaned' ) {
728 qq| AND ct.id NOT IN | .
729 qq| (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
730 if ($cv eq 'customer') {
732 qq| AND ct.id NOT IN | .
733 qq| (SELECT a.customer_id FROM ar a, customer cv | .
734 qq| WHERE cv.id = a.customer_id)|;
736 if ($cv eq 'vendor') {
738 qq| AND ct.id NOT IN | .
739 qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
740 qq| WHERE cv.id = a.vendor_id)|;
742 $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
745 if ($form->{obsolete} eq "Y") {
746 $where .= qq| AND obsolete|;
747 } elsif ($form->{obsolete} eq "N") {
748 $where .= qq| AND NOT obsolete|;
751 if ($form->{business_id}) {
752 $where .= qq| AND (business_id = ?)|;
753 push(@values, conv_i($form->{business_id}));
756 my ($cvar_where, @cvar_values) = CVar->build_filter_query('module' => 'CT',
757 'trans_id_field' => 'ct.id',
761 $where .= qq| AND ($cvar_where)|;
762 push @values, @cvar_values;
764 # Um nach Straße in der Berichtsmaske zu suchen ... jb 13.11.2008
765 if ($form->{addr_street}) {
766 $where .= qq| AND (street ILIKE ?)|;
767 push @values, ('%' . $form->{addr_street} . '%');
770 # Um nach PLZ in der Berichtsmaske zu suchen ... jb 13.11.2008
771 if ($form->{addr_zipcode}) {
772 $where .= qq| AND (zipcode ILIKE ?)|;
773 push @values, ($form->{addr_zipcode} . '%');
776 qq|SELECT ct.*, b.description AS business | .
778 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
781 my @saved_values = @values;
782 # redo for invoices, orders and quotations
783 if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
784 my ($ar, $union, $module);
787 if ($form->{l_invnumber}) {
788 my $ar = $cv eq 'customer' ? 'ar' : 'ap';
789 my $module = $ar eq 'ar' ? 'is' : 'ir';
792 qq|SELECT ct.*, b.description AS business, | .
793 qq| a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
794 qq| '$module' AS module, 'invoice' AS formtype, | .
795 qq| (a.amount = a.paid) AS closed | .
797 qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
798 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
799 qq|WHERE $where AND (a.invoice = '1')|;
804 if ( $form->{l_ordnumber} ) {
805 if ($union eq "UNION") {
806 push(@values, @saved_values);
810 qq|SELECT ct.*, b.description AS business,| .
811 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
812 qq| 'oe' AS module, 'order' AS formtype, o.closed | .
814 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
815 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
816 qq|WHERE $where AND (o.quotation = '0')|;
821 if ( $form->{l_quonumber} ) {
822 if ($union eq "UNION") {
823 push(@values, @saved_values);
827 qq|SELECT ct.*, b.description AS business, | .
828 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
829 qq| 'oe' AS module, 'quotation' AS formtype, o.closed | .
831 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
832 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
833 qq|WHERE $where AND (o.quotation = '1')|;
837 $query .= qq| ORDER BY $sortorder|;
839 $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
841 $main::lxdebug->leave_sub();
845 $main::lxdebug->enter_sub();
847 my ( $self, $myconfig, $form ) = @_;
848 my $dbh = $form->dbconnect($myconfig);
850 qq|SELECT * FROM contacts c | .
851 qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
852 my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
853 my $ref = $sth->fetchrow_hashref(NAME_lc);
855 map { $form->{$_} = $ref->{$_} } keys %$ref;
857 $query = qq|SELECT COUNT(cp_id) AS used FROM (
858 SELECT cp_id FROM oe UNION
859 SELECT cp_id FROM ar UNION
860 SELECT cp_id FROM ap UNION
861 SELECT cp_id FROM delivery_orders
862 ) AS cpid WHERE cp_id = ? OR ? = 0|;
863 ($form->{cp_used}) = selectfirst_array_query($form, $dbh, $query, ($form->{cp_id})x2);
868 $main::lxdebug->leave_sub();
872 $main::lxdebug->enter_sub();
874 my ( $self, $myconfig, $form ) = @_;
875 my $dbh = $form->dbconnect($myconfig);
876 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
877 my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
879 my $ref = $sth->fetchrow_hashref(NAME_lc);
881 map { $form->{$_} = $ref->{$_} } keys %$ref;
883 $query = qq|SELECT COUNT(shipto_id) AS used FROM (
884 SELECT shipto_id FROM oe UNION
885 SELECT shipto_id FROM ar UNION
886 SELECT shipto_id FROM delivery_orders
887 ) AS stid WHERE shipto_id = ? OR ? = 0|;
888 ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
893 $main::lxdebug->leave_sub();
897 $main::lxdebug->enter_sub();
899 my ( $self, $myconfig, $form ) = @_;
900 my $dbh = $form->dbconnect($myconfig);
902 my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
903 my $db = $form->{db} eq "customer" ? "customer" : "vendor";
905 my $where = " WHERE 1=1 ";
908 if ($form->{shipto_id} && ($arap eq "ar")) {
909 $where .= "AND ${arap}.shipto_id = ?";
910 push(@values, $form->{shipto_id});
912 $where .= "AND ${arap}.${db}_id = ?";
913 push(@values, $form->{id});
917 $where .= "AND ${arap}.transdate >= ?";
918 push(@values, conv_date($form->{from}));
921 $where .= "AND ${arap}.transdate <= ?";
922 push(@values, conv_date($form->{to}));
925 qq|SELECT s.shiptoname, i.qty, | .
926 qq| ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
927 qq| i.description, i.unit, i.sellprice | .
929 qq|LEFT JOIN shipto s ON | .
931 ? qq|(ar.shipto_id = s.shipto_id) |
932 : qq|(ap.id = s.trans_id) |) .
933 qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
934 qq|LEFT join parts p ON (p.id = i.parts_id) | .
936 qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
938 $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
942 $main::lxdebug->leave_sub();
946 $main::lxdebug->enter_sub();
951 my $form = $main::form;
953 Common::check_params(\%params, 'dbh');
955 if (!$form->{NOTE_subject}) {
956 $main::lxdebug->leave_sub();
960 my $dbh = $params{dbh};
964 'id' => $form->{NOTE_id},
965 'subject' => $form->{NOTE_subject},
966 'body' => $form->{NOTE_body},
967 'trans_id' => $form->{id},
968 'trans_module' => 'ct',
971 $note{id} = Notes->save(%note);
973 if ($form->{FU_date}) {
975 'id' => $form->{FU_id},
976 'note_id' => $note{id},
977 'follow_up_date' => $form->{FU_date},
978 'created_for_user' => $form->{FU_created_for_user},
979 'done' => $form->{FU_done} ? 1 : 0,
980 'subject' => $form->{NOTE_subject},
981 'body' => $form->{NOTE_body},
984 'trans_id' => $form->{id},
985 'trans_type' => $form->{db} eq 'customer' ? 'customer' : 'vendor',
986 'trans_info' => $form->{name},
991 $follow_up{id} = FU->save(%follow_up);
993 } elsif ($form->{FU_id}) {
994 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
995 do_query($form, $dbh, qq|DELETE FROM follow_ups WHERE id = ?|, conv_i($form->{FU_id}));
998 delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
1000 $main::lxdebug->leave_sub();
1003 sub _delete_selected_notes {
1004 $main::lxdebug->enter_sub();
1009 Common::check_params(\%params, 'dbh');
1011 my $form = $main::form;
1012 my $dbh = $params{dbh};
1014 foreach my $i (1 .. $form->{NOTES_rowcount}) {
1015 next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
1017 Notes->delete('dbh' => $params{dbh},
1018 'id' => $form->{"NOTE_id_$i"});
1021 $main::lxdebug->leave_sub();
1025 $main::lxdebug->enter_sub();
1028 my $shipto_id = shift;
1030 my $form = $main::form;
1031 my %myconfig = %main::myconfig;
1032 my $dbh = $form->get_standard_dbh(\%myconfig);
1034 do_query($form, $dbh, qq|UPDATE shipto SET trans_id = NULL WHERE shipto_id = ?|, $shipto_id);
1038 $main::lxdebug->leave_sub();
1042 $main::lxdebug->enter_sub();
1045 my $shipto_id = shift;
1047 my $form = $main::form;
1048 my %myconfig = %main::myconfig;
1049 my $dbh = $form->get_standard_dbh(\%myconfig);
1051 do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $shipto_id);
1055 $main::lxdebug->leave_sub();