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 #======================================================================
47 use List::MoreUtils qw(true);
50 $main::lxdebug->enter_sub();
52 my ( $self, $myconfig, $form ) = @_;
54 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
56 my $dbh = $form->dbconnect($myconfig);
58 qq|SELECT ct.*, b.id AS business, cp.* | .
60 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
61 qq|LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id) | .
62 qq|WHERE (ct.id = ?) | .
63 qq|ORDER BY cp.cp_id LIMIT 1|;
64 my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
66 my $ref = $sth->fetchrow_hashref(NAME_lc);
68 map { $form->{$_} = $ref->{$_} } keys %$ref;
71 if ( $form->{salesman_id} ) {
73 qq|SELECT ct.name AS salesman | .
77 selectrow_query($form, $dbh, $query, $form->{salesman_id});
80 my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
82 qq|SELECT n.*, n.itime::DATE AS created_on,
83 e.name AS created_by_name, e.login AS created_by_login
85 LEFT JOIN employee e ON (n.created_by = e.id)
86 WHERE (n.trans_id = ?) AND (n.trans_module = 'ct')|;
87 $form->{NOTES} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
90 qq|SELECT fu.follow_up_date, fu.done AS follow_up_done, e.name AS created_for_name, e.name AS created_for_login
92 LEFT JOIN employee e ON (fu.created_for_user = e.id)
93 WHERE (fu.note_id = ?)
94 AND NOT COALESCE(fu.done, FALSE)
95 AND ( (fu.created_by = ?)
96 OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?)))|;
97 $sth = prepare_query($form, $dbh, $query);
99 foreach my $note (@{ $form->{NOTES} }) {
100 do_statement($form, $sth, $query, conv_i($note->{id}), conv_i($note->{created_by}), conv_i($employee_id));
101 $ref = $sth->fetchrow_hashref();
103 map { $note->{$_} = $ref->{$_} } keys %{ $ref } if ($ref);
108 if ($form->{edit_note_id}) {
110 qq|SELECT n.id AS NOTE_id, n.subject AS NOTE_subject, n.body AS NOTE_body,
111 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
113 LEFT JOIN follow_ups fu ON ((n.id = fu.note_id) AND NOT COALESCE(fu.done, FALSE))
115 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{edit_note_id}));
118 foreach my $key (keys %{ $ref }) {
120 $new_key =~ s/^([^_]+)/\U\1\E/;
121 $form->{$new_key} = $ref->{$key};
126 # check if it is orphaned
127 my $arap = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
130 if ($form->{db} eq 'vendor') {
131 $makemodel = qq| UNION SELECT 1 FROM makemodel mm WHERE mm.make = ?|;
138 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
139 qq|WHERE ct.id = ? | .
143 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
146 my ($dummy) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x $num_args);
148 $form->{status} = "orphaned" unless ($dummy);
152 $main::lxdebug->leave_sub();
155 sub populate_drop_down_boxes {
156 $main::lxdebug->enter_sub();
158 my ($self, $myconfig, $form, $provided_dbh) = @_;
160 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
163 $query = qq|SELECT id, description FROM business ORDER BY id|;
164 $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
168 qq|SELECT shipto_id, shiptoname, shiptodepartment_1, shiptostreet, shiptocity
170 WHERE (trans_id = ?) AND (module = 'CT')|;
171 $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
174 $query = qq|SELECT cp_id, cp_name, cp_givenname FROM contacts WHERE cp_cv_id = ? ORDER BY cp_name|;
175 $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
178 $query = qq|SELECT id, description FROM language ORDER BY id|;
179 $form->{languages} = selectall_hashref_query($form, $dbh, $query);
182 $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
183 $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
185 $dbh->disconnect() unless ($provided_dbh);
187 $main::lxdebug->leave_sub();
190 sub query_titles_and_greetings {
191 $main::lxdebug->enter_sub();
193 my ( $self, $myconfig, $form ) = @_;
196 my $dbh = $form->dbconnect($myconfig);
199 qq|SELECT DISTINCT(cp_greeting) | .
201 qq|WHERE cp_greeting ~ '[a-zA-Z]' | .
202 qq|ORDER BY cp_greeting|;
203 $form->{GREETINGS} = [ selectall_array_query($form, $dbh, $query) ];
206 qq|SELECT DISTINCT(greeting) | .
208 qq|WHERE greeting ~ '[a-zA-Z]' | .
210 qq|SELECT DISTINCT(greeting) | .
212 qq|WHERE greeting ~ '[a-zA-Z]' | .
213 qq|ORDER BY greeting|;
215 map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
216 $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
219 qq|SELECT DISTINCT(cp_title) | .
221 qq|WHERE cp_title ~ '[a-zA-Z]'|;
222 $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
225 qq|SELECT DISTINCT(cp_abteilung) | .
227 qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
228 $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
231 $main::lxdebug->leave_sub();
235 $main::lxdebug->enter_sub();
237 my ( $self, $myconfig, $form ) = @_;
239 # set pricegroup to default
240 $form->{klass} = 0 unless ($form->{klass});
242 # connect to database
243 my $dbh = $form->dbconnect_noauto($myconfig);
246 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
247 if ( $form->{"selected_cp_${_}"} );
248 } qw(title greeting abteilung) );
249 $form->{"greeting"} = $form->{"selected_company_greeting"}
250 if ( $form->{"selected_company_greeting"} );
252 # assign value discount, terms, creditlimit
253 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
254 $form->{discount} /= 100;
255 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
257 my ( $query, $sth, $f_id );
260 $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
261 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
263 if (($f_id ne $form->{id}) && ($f_id ne "")) {
264 $main::lxdebug->leave_sub();
269 if (!$form->{customernumber} && $form->{business}) {
270 $form->{customernumber} =
271 $form->update_business($myconfig, $form->{business}, $dbh);
273 if (!$form->{customernumber}) {
274 $form->{customernumber} =
275 $form->update_defaults($myconfig, "customernumber", $dbh);
278 $query = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
279 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
281 $main::lxdebug->leave_sub();
285 $query = qq|SELECT nextval('id')|;
286 ($form->{id}) = selectrow_query($form, $dbh, $query);
288 $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
289 do_query($form, $dbh, $query, $form->{id});
292 $query = qq|UPDATE customer SET | .
293 qq|customernumber = ?, | .
296 qq|department_1 = ?, | .
297 qq|department_2 = ?, | .
311 qq|creditlimit = ?, | .
313 qq|business_id = ?, | .
314 qq|taxnumber = ?, | .
316 qq|account_number = ?, | .
317 qq|bank_code = ?, | .
322 qq|direct_debit = ?, | .
325 qq|salesman_id = ?, | .
326 qq|language_id = ?, | .
327 qq|payment_id = ?, | .
328 qq|taxzone_id = ?, | .
329 qq|user_password = ?, | .
330 qq|c_vendor_id = ?, | .
334 $form->{customernumber},
337 $form->{department_1},
338 $form->{department_2},
352 $form->{creditlimit},
353 conv_i($form->{terms}),
354 conv_i($form->{business}),
357 $form->{account_number},
362 $form->{obsolete} ? 't' : 'f',
363 $form->{direct_debit} ? 't' : 'f',
366 conv_i($form->{salesman_id}),
367 conv_i($form->{language_id}),
368 conv_i($form->{payment_id}),
369 conv_i($form->{taxzone_id}, 0),
370 $form->{user_password},
371 $form->{c_vendor_id},
372 conv_i($form->{klass}),
375 do_query( $form, $dbh, $query, @values );
378 if ( $form->{cp_id} ) {
379 $query = qq|UPDATE contacts SET | .
380 qq|cp_greeting = ?, | .
382 qq|cp_givenname = ?, | .
385 qq|cp_phone1 = ?, | .
386 qq|cp_phone2 = ?, | .
387 qq|cp_abteilung = ?, | .
389 qq|cp_mobile1 = ?, | .
390 qq|cp_mobile2 = ?, | .
391 qq|cp_satphone = ?, | .
392 qq|cp_satfax = ?, | .
393 qq|cp_project = ?, | .
394 qq|cp_privatphone = ?, | .
395 qq|cp_privatemail = ?, | .
396 qq|cp_birthday = ? | .
399 $form->{cp_greeting},
401 $form->{cp_givenname},
406 $form->{cp_abteilung},
410 $form->{cp_satphone},
413 $form->{cp_privatphone},
414 $form->{cp_privatemail},
415 $form->{cp_birthday},
418 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
420 qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, | .
421 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
422 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
424 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
427 $form->{cp_greeting},
429 $form->{cp_givenname},
434 $form->{cp_abteilung},
438 $form->{cp_satphone},
441 $form->{cp_privatphone},
442 $form->{cp_privatemail},
446 do_query( $form, $dbh, $query, @values ) if ($query);
449 $form->add_shipto( $dbh, $form->{id}, "CT" );
451 $self->_save_note('dbh' => $dbh);
452 $self->_delete_selected_notes('dbh' => $dbh);
454 CVar->save_custom_variables('dbh' => $dbh,
456 'trans_id' => $form->{id},
457 'variables' => $form);
459 $rc = $dbh->commit();
462 $main::lxdebug->leave_sub();
467 $main::lxdebug->enter_sub();
469 my ( $self, $myconfig, $form ) = @_;
471 $form->{taxzone_id} *= 1;
472 # connect to database
473 my $dbh = $form->dbconnect_noauto($myconfig);
476 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
477 if ( $form->{"selected_cp_${_}"} );
478 } qw(title greeting abteilung) );
479 $form->{"greeting"} = $form->{"selected_company_greeting"}
480 if ( $form->{"selected_company_greeting"} );
482 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
483 $form->{discount} /= 100;
484 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
489 $query = qq|SELECT nextval('id')|;
490 ($form->{id}) = selectrow_query($form, $dbh, $query);
492 $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
493 do_query($form, $dbh, $query, $form->{id});
495 if ( !$form->{vendornumber} ) {
496 $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
501 qq|UPDATE vendor SET | .
502 qq| vendornumber = ?, | .
504 qq| greeting = ?, | .
505 qq| department_1 = ?, | .
506 qq| department_2 = ?, | .
511 qq| homepage = ?, | .
520 qq| discount = ?, | .
521 qq| creditlimit = ?, | .
522 qq| business_id = ?, | .
523 qq| taxnumber = ?, | .
524 qq| language = ?, | .
525 qq| account_number = ?, | .
526 qq| bank_code = ?, | .
530 qq| obsolete = ?, | .
531 qq| direct_debit = ?, | .
533 qq| payment_id = ?, | .
534 qq| taxzone_id = ?, | .
535 qq| language_id = ?, | .
536 qq| username = ?, | .
537 qq| user_password = ?, | .
538 qq| v_customer_id = ? | .
541 $form->{vendornumber},
544 $form->{department_1},
545 $form->{department_2},
558 conv_i($form->{terms}),
560 $form->{creditlimit},
561 conv_i($form->{business}),
564 $form->{account_number},
569 $form->{obsolete} ? 't' : 'f',
570 $form->{direct_debit} ? 't' : 'f',
572 conv_i($form->{payment_id}),
573 conv_i($form->{taxzone_id}, 0),
574 conv_i( $form->{language_id}),
576 $form->{user_password},
577 $form->{v_customer_id},
580 do_query($form, $dbh, $query, @values);
583 if ( $form->{cp_id} ) {
584 $query = qq|UPDATE contacts SET | .
585 qq|cp_greeting = ?, | .
587 qq|cp_givenname = ?, | .
590 qq|cp_phone1 = ?, | .
591 qq|cp_phone2 = ?, | .
592 qq|cp_abteilung = ?, | .
594 qq|cp_mobile1 = ?, | .
595 qq|cp_mobile2 = ?, | .
596 qq|cp_satphone = ?, | .
597 qq|cp_satfax = ?, | .
598 qq|cp_project = ?, | .
599 qq|cp_privatphone = ?, | .
600 qq|cp_privatemail = ?, | .
601 qq|cp_birthday = ? | .
604 $form->{cp_greeting},
606 $form->{cp_givenname},
611 $form->{cp_abteilung},
615 $form->{cp_satphone},
618 $form->{cp_privatphone},
619 $form->{cp_privatemail},
620 $form->{cp_birthday},
623 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
625 qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, | .
626 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
627 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
629 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
632 $form->{cp_greeting},
634 $form->{cp_givenname},
639 $form->{cp_abteilung},
643 $form->{cp_satphone},
646 $form->{cp_privatphone},
647 $form->{cp_privatemail},
651 do_query($form, $dbh, $query, @values) if ($query);
654 $form->add_shipto( $dbh, $form->{id}, "CT" );
656 $self->_save_note('dbh' => $dbh);
657 $self->_delete_selected_notes('dbh' => $dbh);
659 CVar->save_custom_variables('dbh' => $dbh,
661 'trans_id' => $form->{id},
662 'variables' => $form);
664 $rc = $dbh->commit();
667 $main::lxdebug->leave_sub();
672 $main::lxdebug->enter_sub();
674 my ( $self, $myconfig, $form ) = @_;
675 # connect to database
676 my $dbh = $form->dbconnect($myconfig);
679 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
680 my $query = qq|DELETE FROM $cv WHERE id = ?|;
681 do_query($form, $dbh, $query, $form->{id});
685 $main::lxdebug->leave_sub();
689 $main::lxdebug->enter_sub();
691 my ( $self, $myconfig, $form ) = @_;
693 # connect to database
694 my $dbh = $form->dbconnect($myconfig);
696 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
701 my %allowed_sort_columns =
702 map({ $_, 1 } qw(id customernumber vendornumber name contact phone fax email
703 taxnumber business invnumber ordnumber quonumber));
704 $sortorder = $form->{sort} && $allowed_sort_columns{$form->{sort}} ? $form->{sort} : "name";
705 $form->{sort} = $sortorder;
706 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
708 if ($sortorder ne 'id' && 1 >= true { $form->{$_} } qw(l_ordnumber l_quonumber l_invnumber)) {
709 $sortorder = "lower($sortorder) ${sortdir}";
711 $sortorder .= " ${sortdir}";
714 if ($form->{"${cv}number"}) {
715 $where .= " AND ct.${cv}number ILIKE ?";
716 push(@values, '%' . $form->{"${cv}number"} . '%');
719 foreach my $key (qw(name contact email)) {
721 $where .= " AND ct.$key ILIKE ?";
722 push(@values, '%' . $form->{$key} . '%');
726 if ($form->{cp_name}) {
727 $where .= " AND ct.id IN (SELECT cp_cv_id FROM contacts WHERE lower(cp_name) LIKE lower(?))";
728 push @values, '%' . $form->{cp_name} . '%';
731 if ($form->{addr_city}) {
732 $where .= " AND ((lower(ct.city) LIKE lower(?))
737 WHERE (module = 'CT')
738 AND (lower(shiptocity) LIKE lower(?))
741 push @values, ('%' . $form->{addr_city} . '%') x 2;
744 if ( $form->{status} eq 'orphaned' ) {
746 qq| AND ct.id NOT IN | .
747 qq| (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
748 if ($cv eq 'customer') {
750 qq| AND ct.id NOT IN | .
751 qq| (SELECT a.customer_id FROM ar a, customer cv | .
752 qq| WHERE cv.id = a.customer_id)|;
754 if ($cv eq 'vendor') {
756 qq| AND ct.id NOT IN | .
757 qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
758 qq| WHERE cv.id = a.vendor_id)|;
760 $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
763 if ($form->{obsolete} eq "Y") {
764 $where .= qq| AND obsolete|;
765 } elsif ($form->{obsolete} eq "N") {
766 $where .= qq| AND NOT obsolete|;
769 if ($form->{business_id}) {
770 $where .= qq| AND (business_id = ?)|;
771 push(@values, conv_i($form->{business_id}));
774 my ($cvar_where, @cvar_values) = CVar->build_filter_query('module' => 'CT',
775 'trans_id_field' => 'ct.id',
779 $where .= qq| AND ($cvar_where)|;
780 push @values, @cvar_values;
783 if ($form->{addr_street}) {
784 $where .= qq| AND (street ILIKE ?)|;
785 push @values, '%' . $form->{addr_street} . '%';
788 if ($form->{addr_zipcode}) {
789 $where .= qq| AND (zipcode ILIKE ?)|;
790 push @values, $form->{addr_zipcode} . '%';
794 qq|SELECT ct.*, b.description AS business | .
796 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
799 my @saved_values = @values;
800 # redo for invoices, orders and quotations
801 if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
802 my ($ar, $union, $module);
805 if ($form->{l_invnumber}) {
806 my $ar = $cv eq 'customer' ? 'ar' : 'ap';
807 my $module = $ar eq 'ar' ? 'is' : 'ir';
810 qq|SELECT ct.*, b.description AS business, | .
811 qq| a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
812 qq| '$module' AS module, 'invoice' AS formtype, | .
813 qq| (a.amount = a.paid) AS closed | .
815 qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
816 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
817 qq|WHERE $where AND (a.invoice = '1')|;
822 if ( $form->{l_ordnumber} ) {
823 if ($union eq "UNION") {
824 push(@values, @saved_values);
828 qq|SELECT ct.*, b.description AS business,| .
829 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
830 qq| 'oe' AS module, 'order' AS formtype, o.closed | .
832 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
833 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
834 qq|WHERE $where AND (o.quotation = '0')|;
839 if ( $form->{l_quonumber} ) {
840 if ($union eq "UNION") {
841 push(@values, @saved_values);
845 qq|SELECT ct.*, b.description AS business, | .
846 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
847 qq| 'oe' AS module, 'quotation' AS formtype, o.closed | .
849 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
850 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
851 qq|WHERE $where AND (o.quotation = '1')|;
855 $query .= qq| ORDER BY $sortorder|;
857 $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
859 $main::lxdebug->leave_sub();
863 $main::lxdebug->enter_sub();
865 my ( $self, $myconfig, $form ) = @_;
866 my $dbh = $form->dbconnect($myconfig);
868 qq|SELECT * FROM contacts c | .
869 qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
870 my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
871 my $ref = $sth->fetchrow_hashref(NAME_lc);
873 map { $form->{$_} = $ref->{$_} } keys %$ref;
875 $query = qq|SELECT COUNT(cp_id) AS used FROM (
876 SELECT cp_id FROM oe UNION
877 SELECT cp_id FROM ar UNION
878 SELECT cp_id FROM ap UNION
879 SELECT cp_id FROM delivery_orders
880 ) AS cpid WHERE cp_id = ? OR ? = 0|;
881 ($form->{cp_used}) = selectfirst_array_query($form, $dbh, $query, ($form->{cp_id})x2);
886 $main::lxdebug->leave_sub();
890 $main::lxdebug->enter_sub();
892 my ( $self, $myconfig, $form ) = @_;
893 my $dbh = $form->dbconnect($myconfig);
894 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
895 my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
897 my $ref = $sth->fetchrow_hashref(NAME_lc);
899 map { $form->{$_} = $ref->{$_} } keys %$ref;
901 $query = qq|SELECT COUNT(shipto_id) AS used FROM (
902 SELECT shipto_id FROM oe UNION
903 SELECT shipto_id FROM ar UNION
904 SELECT shipto_id FROM delivery_orders
905 ) AS stid WHERE shipto_id = ? OR ? = 0|;
906 ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
911 $main::lxdebug->leave_sub();
915 $main::lxdebug->enter_sub();
917 my ( $self, $myconfig, $form ) = @_;
918 my $dbh = $form->dbconnect($myconfig);
920 my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
921 my $db = $form->{db} eq "customer" ? "customer" : "vendor";
923 my $where = " WHERE 1=1 ";
926 if ($form->{shipto_id} && ($arap eq "ar")) {
927 $where .= "AND ${arap}.shipto_id = ?";
928 push(@values, $form->{shipto_id});
930 $where .= "AND ${arap}.${db}_id = ?";
931 push(@values, $form->{id});
935 $where .= "AND ${arap}.transdate >= ?";
936 push(@values, conv_date($form->{from}));
939 $where .= "AND ${arap}.transdate <= ?";
940 push(@values, conv_date($form->{to}));
943 qq|SELECT s.shiptoname, i.qty, | .
944 qq| ${arap}.id, ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
945 qq| i.description, i.unit, i.sellprice, | .
946 qq| oe.id AS oe_id | .
948 qq|LEFT JOIN shipto s ON | .
950 ? qq|(ar.shipto_id = s.shipto_id) |
951 : qq|(ap.id = s.trans_id) |) .
952 qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
953 qq|LEFT join parts p ON (p.id = i.parts_id) | .
954 qq|LEFT JOIN oe ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '') | .
956 qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
958 $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
962 $main::lxdebug->leave_sub();
966 $main::lxdebug->enter_sub();
971 my $form = $main::form;
973 Common::check_params(\%params, 'dbh');
975 if (!$form->{NOTE_subject}) {
976 $main::lxdebug->leave_sub();
980 my $dbh = $params{dbh};
984 'id' => $form->{NOTE_id},
985 'subject' => $form->{NOTE_subject},
986 'body' => $form->{NOTE_body},
987 'trans_id' => $form->{id},
988 'trans_module' => 'ct',
991 $note{id} = Notes->save(%note);
993 if ($form->{FU_date}) {
995 'id' => $form->{FU_id},
996 'note_id' => $note{id},
997 'follow_up_date' => $form->{FU_date},
998 'created_for_user' => $form->{FU_created_for_user},
999 'done' => $form->{FU_done} ? 1 : 0,
1000 'subject' => $form->{NOTE_subject},
1001 'body' => $form->{NOTE_body},
1004 'trans_id' => $form->{id},
1005 'trans_type' => $form->{db} eq 'customer' ? 'customer' : 'vendor',
1006 'trans_info' => $form->{name},
1011 $follow_up{id} = FU->save(%follow_up);
1013 } elsif ($form->{FU_id}) {
1014 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
1015 do_query($form, $dbh, qq|DELETE FROM follow_ups WHERE id = ?|, conv_i($form->{FU_id}));
1018 delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
1020 $main::lxdebug->leave_sub();
1023 sub _delete_selected_notes {
1024 $main::lxdebug->enter_sub();
1029 Common::check_params(\%params, 'dbh');
1031 my $form = $main::form;
1032 my $dbh = $params{dbh};
1034 foreach my $i (1 .. $form->{NOTES_rowcount}) {
1035 next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
1037 Notes->delete('dbh' => $params{dbh},
1038 'id' => $form->{"NOTE_id_$i"});
1041 $main::lxdebug->leave_sub();
1045 $main::lxdebug->enter_sub();
1048 my $shipto_id = shift;
1050 my $form = $main::form;
1051 my %myconfig = %main::myconfig;
1052 my $dbh = $form->get_standard_dbh(\%myconfig);
1054 do_query($form, $dbh, qq|UPDATE shipto SET trans_id = NULL WHERE shipto_id = ?|, $shipto_id);
1058 $main::lxdebug->leave_sub();
1062 $main::lxdebug->enter_sub();
1065 my $shipto_id = shift;
1067 my $form = $main::form;
1068 my %myconfig = %main::myconfig;
1069 my $dbh = $form->get_standard_dbh(\%myconfig);
1071 do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $shipto_id);
1075 $main::lxdebug->leave_sub();
1079 $main::lxdebug->enter_sub();
1084 Common::check_params(\%params, qw(vc id));
1086 my $myconfig = \%main::myconfig;
1087 my $form = $main::form;
1089 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1091 my $table = $params{vc} eq 'customer' ? 'customer' : 'vendor';
1092 my @ids = ref $params{id} eq 'ARRAY' ? @{ $params{id} } : ($params{id});
1093 my $placeholders = ('?') x scalar @ids;
1094 my $query = qq|SELECT id, name, account_number, bank, bank_code, iban, bic
1096 WHERE id IN (${placeholders})|;
1098 my $result = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
1100 if (ref $params{id} eq 'ARRAY') {
1101 $result = { map { $_->{id} => $_ } @{ $result } };
1103 $result = $result->[0] || { 'id' => $params{id} };
1106 $main::lxdebug->leave_sub();