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";
129 if ($form->{db} eq 'vendor') {
130 $makemodel = qq| UNION SELECT 1 FROM makemodel mm WHERE mm.make = ?|;
137 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
138 qq|WHERE ct.id = ? | .
142 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
145 my ($dummy) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x $num_args);
147 $form->{status} = "orphaned" unless ($dummy);
151 $main::lxdebug->leave_sub();
154 sub populate_drop_down_boxes {
155 $main::lxdebug->enter_sub();
157 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 ) = @_;
194 my ( %tmp, $ref, $query );
196 my $dbh = $form->dbconnect($myconfig);
199 qq|SELECT DISTINCT(greeting) | .
201 qq|WHERE greeting ~ '[a-zA-Z]' | .
203 qq|SELECT DISTINCT(greeting) | .
205 qq|WHERE greeting ~ '[a-zA-Z]' | .
206 qq|ORDER BY greeting|;
208 map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
209 $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
212 qq|SELECT DISTINCT(cp_title) | .
214 qq|WHERE cp_title ~ '[a-zA-Z]'|;
215 $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
218 qq|SELECT DISTINCT(cp_abteilung) | .
220 qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
221 $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
224 $main::lxdebug->leave_sub();
228 $main::lxdebug->enter_sub();
230 my ( $self, $myconfig, $form ) = @_;
232 # set pricegroup to default
233 $form->{klass} = 0 unless ($form->{klass});
235 # connect to database
236 my $dbh = $form->dbconnect_noauto($myconfig);
239 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
240 if ( $form->{"selected_cp_${_}"} );
241 } qw(title greeting abteilung) );
242 $form->{"greeting"} = $form->{"selected_company_greeting"}
243 if ( $form->{"selected_company_greeting"} );
245 # assign value discount, terms, creditlimit
246 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
247 $form->{discount} /= 100;
248 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
250 my ( $query, $sth, $f_id );
253 $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
254 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
256 if (($f_id ne $form->{id}) && ($f_id ne "")) {
257 $main::lxdebug->leave_sub();
262 if (!$form->{customernumber} && $form->{business}) {
263 $form->{customernumber} =
264 $form->update_business($myconfig, $form->{business}, $dbh);
266 if (!$form->{customernumber}) {
267 $form->{customernumber} =
268 $form->update_defaults($myconfig, "customernumber", $dbh);
271 $query = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
272 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
274 $main::lxdebug->leave_sub();
278 $query = qq|SELECT nextval('id')|;
279 ($form->{id}) = selectrow_query($form, $dbh, $query);
281 $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
282 do_query($form, $dbh, $query, $form->{id});
285 $query = qq|UPDATE customer SET | .
286 qq|customernumber = ?, | .
289 qq|department_1 = ?, | .
290 qq|department_2 = ?, | .
304 qq|creditlimit = ?, | .
306 qq|business_id = ?, | .
307 qq|taxnumber = ?, | .
309 qq|account_number = ?, | .
310 qq|bank_code = ?, | .
315 qq|direct_debit = ?, | .
318 qq|salesman_id = ?, | .
319 qq|language_id = ?, | .
320 qq|payment_id = ?, | .
321 qq|taxzone_id = ?, | .
322 qq|user_password = ?, | .
323 qq|c_vendor_id = ?, | .
327 $form->{customernumber},
330 $form->{department_1},
331 $form->{department_2},
345 $form->{creditlimit},
346 conv_i($form->{terms}),
347 conv_i($form->{business}),
350 $form->{account_number},
355 $form->{obsolete} ? 't' : 'f',
356 $form->{direct_debit} ? 't' : 'f',
359 conv_i($form->{salesman_id}),
360 conv_i($form->{language_id}),
361 conv_i($form->{payment_id}),
362 conv_i($form->{taxzone_id}, 0),
363 $form->{user_password},
364 $form->{c_vendor_id},
365 conv_i($form->{klass}),
368 do_query( $form, $dbh, $query, @values );
371 if ( $form->{cp_id} ) {
372 $query = qq|UPDATE contacts SET | .
374 qq|cp_givenname = ?, | .
377 qq|cp_phone1 = ?, | .
378 qq|cp_phone2 = ?, | .
379 qq|cp_abteilung = ?, | .
381 qq|cp_mobile1 = ?, | .
382 qq|cp_mobile2 = ?, | .
383 qq|cp_satphone = ?, | .
384 qq|cp_satfax = ?, | .
385 qq|cp_project = ?, | .
386 qq|cp_privatphone = ?, | .
387 qq|cp_privatemail = ?, | .
388 qq|cp_birthday = ?, | .
393 $form->{cp_givenname},
398 $form->{cp_abteilung},
402 $form->{cp_satphone},
405 $form->{cp_privatphone},
406 $form->{cp_privatemail},
407 $form->{cp_birthday},
408 $form->{cp_gender} eq 'f' ? 'f' : 'm',
411 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
413 qq|INSERT INTO contacts ( cp_cv_id, cp_title, cp_givenname, | .
414 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
415 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
416 qq| cp_birthday, cp_gender) | .
417 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
421 $form->{cp_givenname},
426 $form->{cp_abteilung},
430 $form->{cp_satphone},
433 $form->{cp_privatphone},
434 $form->{cp_privatemail},
435 $form->{cp_birthday},
436 $form->{cp_gender} eq 'f' ? 'f' : 'm',
439 do_query( $form, $dbh, $query, @values ) if ($query);
442 $form->add_shipto( $dbh, $form->{id}, "CT" );
444 $self->_save_note('dbh' => $dbh);
445 $self->_delete_selected_notes('dbh' => $dbh);
447 CVar->save_custom_variables('dbh' => $dbh,
449 'trans_id' => $form->{id},
450 'variables' => $form,
451 'always_valid' => 1);
453 my $rc = $dbh->commit();
456 $main::lxdebug->leave_sub();
461 $main::lxdebug->enter_sub();
463 my ( $self, $myconfig, $form ) = @_;
465 $form->{taxzone_id} *= 1;
466 # connect to database
467 my $dbh = $form->dbconnect_noauto($myconfig);
470 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
471 if ( $form->{"selected_cp_${_}"} );
472 } qw(title greeting abteilung) );
473 $form->{"greeting"} = $form->{"selected_company_greeting"}
474 if ( $form->{"selected_company_greeting"} );
476 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
477 $form->{discount} /= 100;
478 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
483 $query = qq|SELECT nextval('id')|;
484 ($form->{id}) = selectrow_query($form, $dbh, $query);
486 $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
487 do_query($form, $dbh, $query, $form->{id});
489 if ( !$form->{vendornumber} ) {
490 $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
495 qq|UPDATE vendor SET | .
496 qq| vendornumber = ?, | .
498 qq| greeting = ?, | .
499 qq| department_1 = ?, | .
500 qq| department_2 = ?, | .
505 qq| homepage = ?, | .
514 qq| discount = ?, | .
515 qq| creditlimit = ?, | .
516 qq| business_id = ?, | .
517 qq| taxnumber = ?, | .
518 qq| language = ?, | .
519 qq| account_number = ?, | .
520 qq| bank_code = ?, | .
524 qq| obsolete = ?, | .
525 qq| direct_debit = ?, | .
527 qq| payment_id = ?, | .
528 qq| taxzone_id = ?, | .
529 qq| language_id = ?, | .
530 qq| username = ?, | .
531 qq| user_password = ?, | .
532 qq| v_customer_id = ? | .
535 $form->{vendornumber},
538 $form->{department_1},
539 $form->{department_2},
552 conv_i($form->{terms}),
554 $form->{creditlimit},
555 conv_i($form->{business}),
558 $form->{account_number},
563 $form->{obsolete} ? 't' : 'f',
564 $form->{direct_debit} ? 't' : 'f',
566 conv_i($form->{payment_id}),
567 conv_i($form->{taxzone_id}, 0),
568 conv_i( $form->{language_id}),
570 $form->{user_password},
571 $form->{v_customer_id},
574 do_query($form, $dbh, $query, @values);
577 if ( $form->{cp_id} ) {
578 $query = qq|UPDATE contacts SET | .
580 qq|cp_givenname = ?, | .
583 qq|cp_phone1 = ?, | .
584 qq|cp_phone2 = ?, | .
585 qq|cp_abteilung = ?, | .
587 qq|cp_mobile1 = ?, | .
588 qq|cp_mobile2 = ?, | .
589 qq|cp_satphone = ?, | .
590 qq|cp_satfax = ?, | .
591 qq|cp_project = ?, | .
592 qq|cp_privatphone = ?, | .
593 qq|cp_privatemail = ?, | .
594 qq|cp_birthday = ?, | .
599 $form->{cp_givenname},
604 $form->{cp_abteilung},
608 $form->{cp_satphone},
611 $form->{cp_privatphone},
612 $form->{cp_privatemail},
613 $form->{cp_birthday},
614 $form->{cp_gender} eq 'f' ? 'f' : 'm',
617 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
619 qq|INSERT INTO contacts ( cp_cv_id, cp_title, cp_givenname, | .
620 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
621 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
622 qq| cp_birthday, cp_gender) | .
623 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
627 $form->{cp_givenname},
632 $form->{cp_abteilung},
636 $form->{cp_satphone},
639 $form->{cp_privatphone},
640 $form->{cp_privatemail},
641 $form->{cp_birthday},
645 do_query($form, $dbh, $query, @values) if ($query);
648 $form->add_shipto( $dbh, $form->{id}, "CT" );
650 $self->_save_note('dbh' => $dbh);
651 $self->_delete_selected_notes('dbh' => $dbh);
653 CVar->save_custom_variables('dbh' => $dbh,
655 'trans_id' => $form->{id},
656 'variables' => $form,
657 'always_valid' => 1);
659 my $rc = $dbh->commit();
662 $main::lxdebug->leave_sub();
667 $main::lxdebug->enter_sub();
669 my ( $self, $myconfig, $form ) = @_;
670 # connect to database
671 my $dbh = $form->dbconnect($myconfig);
674 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
675 my $query = qq|DELETE FROM $cv WHERE id = ?|;
676 do_query($form, $dbh, $query, $form->{id});
680 $main::lxdebug->leave_sub();
684 $main::lxdebug->enter_sub();
686 my ( $self, $myconfig, $form ) = @_;
688 # connect to database
689 my $dbh = $form->dbconnect($myconfig);
691 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
696 my %allowed_sort_columns =
697 map({ $_, 1 } qw(id customernumber vendornumber name contact phone fax email
698 taxnumber business invnumber ordnumber quonumber));
699 my $sortorder = $form->{sort} && $allowed_sort_columns{$form->{sort}} ? $form->{sort} : "name";
700 $form->{sort} = $sortorder;
701 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
703 if ($sortorder ne 'id' && 1 >= scalar grep { $form->{$_} } qw(l_ordnumber l_quonumber l_invnumber)) {
704 $sortorder = "lower($sortorder) ${sortdir}";
706 $sortorder .= " ${sortdir}";
709 if ($form->{"${cv}number"}) {
710 $where .= " AND ct.${cv}number ILIKE ?";
711 push(@values, '%' . $form->{"${cv}number"} . '%');
714 foreach my $key (qw(name contact email)) {
716 $where .= " AND ct.$key ILIKE ?";
717 push(@values, '%' . $form->{$key} . '%');
721 if ($form->{cp_name}) {
722 $where .= " AND ct.id IN (SELECT cp_cv_id FROM contacts WHERE lower(cp_name) LIKE lower(?))";
723 push @values, '%' . $form->{cp_name} . '%';
726 if ($form->{addr_city}) {
727 $where .= " AND ((lower(ct.city) LIKE lower(?))
732 WHERE (module = 'CT')
733 AND (lower(shiptocity) LIKE lower(?))
736 push @values, ('%' . $form->{addr_city} . '%') x 2;
739 if ( $form->{status} eq 'orphaned' ) {
741 qq| AND ct.id NOT IN | .
742 qq| (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
743 if ($cv eq 'customer') {
745 qq| AND ct.id NOT IN | .
746 qq| (SELECT a.customer_id FROM ar a, customer cv | .
747 qq| WHERE cv.id = a.customer_id)|;
749 if ($cv eq 'vendor') {
751 qq| AND ct.id NOT IN | .
752 qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
753 qq| WHERE cv.id = a.vendor_id)|;
755 $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
758 if ($form->{obsolete} eq "Y") {
759 $where .= qq| AND obsolete|;
760 } elsif ($form->{obsolete} eq "N") {
761 $where .= qq| AND NOT obsolete|;
764 if ($form->{business_id}) {
765 $where .= qq| AND (business_id = ?)|;
766 push(@values, conv_i($form->{business_id}));
769 my ($cvar_where, @cvar_values) = CVar->build_filter_query('module' => 'CT',
770 'trans_id_field' => 'ct.id',
774 $where .= qq| AND ($cvar_where)|;
775 push @values, @cvar_values;
778 if ($form->{addr_street}) {
779 $where .= qq| AND (street ILIKE ?)|;
780 push @values, '%' . $form->{addr_street} . '%';
783 if ($form->{addr_zipcode}) {
784 $where .= qq| AND (zipcode ILIKE ?)|;
785 push @values, $form->{addr_zipcode} . '%';
789 qq|SELECT ct.*, b.description AS business | .
791 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
794 my @saved_values = @values;
795 # redo for invoices, orders and quotations
796 if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
797 my ($ar, $union, $module);
800 if ($form->{l_invnumber}) {
801 my $ar = $cv eq 'customer' ? 'ar' : 'ap';
802 my $module = $ar eq 'ar' ? 'is' : 'ir';
805 qq|SELECT ct.*, b.description AS business, | .
806 qq| a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
807 qq| '$module' AS module, 'invoice' AS formtype, | .
808 qq| (a.amount = a.paid) AS closed | .
810 qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
811 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
812 qq|WHERE $where AND (a.invoice = '1')|;
817 if ( $form->{l_ordnumber} ) {
818 if ($union eq "UNION") {
819 push(@values, @saved_values);
823 qq|SELECT ct.*, b.description AS business,| .
824 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
825 qq| 'oe' AS module, 'order' AS formtype, o.closed | .
827 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
828 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
829 qq|WHERE $where AND (o.quotation = '0')|;
834 if ( $form->{l_quonumber} ) {
835 if ($union eq "UNION") {
836 push(@values, @saved_values);
840 qq|SELECT ct.*, b.description AS business, | .
841 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
842 qq| 'oe' AS module, 'quotation' AS formtype, o.closed | .
844 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
845 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
846 qq|WHERE $where AND (o.quotation = '1')|;
850 $query .= qq| ORDER BY $sortorder|;
852 $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
854 $main::lxdebug->leave_sub();
858 $main::lxdebug->enter_sub();
860 my ( $self, $myconfig, $form ) = @_;
861 my $dbh = $form->dbconnect($myconfig);
863 qq|SELECT * FROM contacts c | .
864 qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
865 my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
866 my $ref = $sth->fetchrow_hashref("NAME_lc");
868 map { $form->{$_} = $ref->{$_} } keys %$ref;
870 $query = qq|SELECT COUNT(cp_id) AS used FROM (
871 SELECT cp_id FROM oe UNION
872 SELECT cp_id FROM ar UNION
873 SELECT cp_id FROM ap UNION
874 SELECT cp_id FROM delivery_orders
875 ) AS cpid WHERE cp_id = ? OR ? = 0|;
876 ($form->{cp_used}) = selectfirst_array_query($form, $dbh, $query, ($form->{cp_id})x2);
881 $main::lxdebug->leave_sub();
885 $main::lxdebug->enter_sub();
887 my ( $self, $myconfig, $form ) = @_;
888 my $dbh = $form->dbconnect($myconfig);
889 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
890 my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
892 my $ref = $sth->fetchrow_hashref("NAME_lc");
894 map { $form->{$_} = $ref->{$_} } keys %$ref;
896 $query = qq|SELECT COUNT(shipto_id) AS used FROM (
897 SELECT shipto_id FROM oe UNION
898 SELECT shipto_id FROM ar UNION
899 SELECT shipto_id FROM delivery_orders
900 ) AS stid WHERE shipto_id = ? OR ? = 0|;
901 ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
906 $main::lxdebug->leave_sub();
910 $main::lxdebug->enter_sub();
912 my ( $self, $myconfig, $form ) = @_;
913 my $dbh = $form->dbconnect($myconfig);
915 my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
916 my $db = $form->{db} eq "customer" ? "customer" : "vendor";
917 my $qty_sign = $form->{db} eq 'vendor' ? ' * -1 AS qty' : '';
919 my $where = " WHERE 1=1 ";
922 if ($form->{shipto_id} && ($arap eq "ar")) {
923 $where .= "AND ${arap}.shipto_id = ?";
924 push(@values, $form->{shipto_id});
926 $where .= "AND ${arap}.${db}_id = ?";
927 push(@values, $form->{id});
931 $where .= "AND ${arap}.transdate >= ?";
932 push(@values, conv_date($form->{from}));
935 $where .= "AND ${arap}.transdate <= ?";
936 push(@values, conv_date($form->{to}));
939 qq|SELECT s.shiptoname, i.qty $qty_sign, | .
940 qq| ${arap}.id, ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
941 qq| i.description, i.unit, i.sellprice, | .
942 qq| oe.id AS oe_id | .
944 qq|LEFT JOIN shipto s ON | .
946 ? qq|(ar.shipto_id = s.shipto_id) |
947 : qq|(ap.id = s.trans_id) |) .
948 qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
949 qq|LEFT join parts p ON (p.id = i.parts_id) | .
950 qq|LEFT JOIN oe ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '') | .
952 qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
954 $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
958 $main::lxdebug->leave_sub();
962 $main::lxdebug->enter_sub();
967 my $form = $main::form;
969 Common::check_params(\%params, 'dbh');
971 if (!$form->{NOTE_subject}) {
972 $main::lxdebug->leave_sub();
976 my $dbh = $params{dbh};
980 'id' => $form->{NOTE_id},
981 'subject' => $form->{NOTE_subject},
982 'body' => $form->{NOTE_body},
983 'trans_id' => $form->{id},
984 'trans_module' => 'ct',
987 $note{id} = Notes->save(%note);
989 if ($form->{FU_date}) {
991 'id' => $form->{FU_id},
992 'note_id' => $note{id},
993 'follow_up_date' => $form->{FU_date},
994 'created_for_user' => $form->{FU_created_for_user},
995 'done' => $form->{FU_done} ? 1 : 0,
996 'subject' => $form->{NOTE_subject},
997 'body' => $form->{NOTE_body},
1000 'trans_id' => $form->{id},
1001 'trans_type' => $form->{db} eq 'customer' ? 'customer' : 'vendor',
1002 'trans_info' => $form->{name},
1007 $follow_up{id} = FU->save(%follow_up);
1009 } elsif ($form->{FU_id}) {
1010 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
1011 do_query($form, $dbh, qq|DELETE FROM follow_ups WHERE id = ?|, conv_i($form->{FU_id}));
1014 delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
1016 $main::lxdebug->leave_sub();
1019 sub _delete_selected_notes {
1020 $main::lxdebug->enter_sub();
1025 Common::check_params(\%params, 'dbh');
1027 my $form = $main::form;
1028 my $dbh = $params{dbh};
1030 foreach my $i (1 .. $form->{NOTES_rowcount}) {
1031 next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
1033 Notes->delete('dbh' => $params{dbh},
1034 'id' => $form->{"NOTE_id_$i"});
1037 $main::lxdebug->leave_sub();
1041 $main::lxdebug->enter_sub();
1044 my $shipto_id = shift;
1046 my $form = $main::form;
1047 my %myconfig = %main::myconfig;
1048 my $dbh = $form->get_standard_dbh(\%myconfig);
1050 do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $shipto_id);
1054 $main::lxdebug->leave_sub();
1058 $main::lxdebug->enter_sub();
1063 Common::check_params(\%params, qw(vc id));
1065 my $myconfig = \%main::myconfig;
1066 my $form = $main::form;
1068 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1070 my $table = $params{vc} eq 'customer' ? 'customer' : 'vendor';
1071 my @ids = ref $params{id} eq 'ARRAY' ? @{ $params{id} } : ($params{id});
1072 my $placeholders = ('?') x scalar @ids;
1073 my $query = qq|SELECT id, name, account_number, bank, bank_code, iban, bic
1075 WHERE id IN (${placeholders})|;
1077 my $result = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
1079 if (ref $params{id} eq 'ARRAY') {
1080 $result = { map { $_->{id} => $_ } @{ $result } };
1082 $result = $result->[0] || { 'id' => $params{id} };
1085 $main::lxdebug->leave_sub();