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 | .
159 qq|FROM shipto WHERE (trans_id = ?) AND (module = 'CT')|;
160 $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
163 $query = qq|SELECT cp_id, cp_name, cp_givenname FROM contacts WHERE cp_cv_id = ? ORDER BY cp_name|;
164 $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
167 $query = qq|SELECT id, description FROM language ORDER BY id|;
168 $form->{languages} = selectall_hashref_query($form, $dbh, $query);
171 $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
172 $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
174 $dbh->disconnect() unless ($provided_dbh);
176 $main::lxdebug->leave_sub();
179 sub query_titles_and_greetings {
180 $main::lxdebug->enter_sub();
182 my ( $self, $myconfig, $form ) = @_;
185 my $dbh = $form->dbconnect($myconfig);
188 qq|SELECT DISTINCT(cp_greeting) | .
190 qq|WHERE cp_greeting ~ '[a-zA-Z]' | .
191 qq|ORDER BY cp_greeting|;
192 $form->{GREETINGS} = [ selectall_array_query($form, $dbh, $query) ];
195 qq|SELECT DISTINCT(greeting) | .
197 qq|WHERE greeting ~ '[a-zA-Z]' | .
199 qq|SELECT DISTINCT(greeting) | .
201 qq|WHERE greeting ~ '[a-zA-Z]' | .
202 qq|ORDER BY greeting|;
204 map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
205 $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
208 qq|SELECT DISTINCT(cp_title) | .
210 qq|WHERE cp_title ~ '[a-zA-Z]'|;
211 $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
214 qq|SELECT DISTINCT(cp_abteilung) | .
216 qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
217 $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
220 $main::lxdebug->leave_sub();
224 $main::lxdebug->enter_sub();
226 my ( $self, $myconfig, $form ) = @_;
228 # set pricegroup to default
229 $form->{klass} = 0 unless ($form->{klass});
231 # connect to database
232 my $dbh = $form->dbconnect_noauto($myconfig);
235 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
236 if ( $form->{"selected_cp_${_}"} );
237 } qw(title greeting abteilung) );
238 $form->{"greeting"} = $form->{"selected_company_greeting"}
239 if ( $form->{"selected_company_greeting"} );
241 # assign value discount, terms, creditlimit
242 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
243 $form->{discount} /= 100;
244 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
246 my ( $query, $sth, $f_id );
249 $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
250 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
252 if (($f_id ne $form->{id}) && ($f_id ne "")) {
253 $main::lxdebug->leave_sub();
258 if (!$form->{customernumber} && $form->{business}) {
259 $form->{customernumber} =
260 $form->update_business($myconfig, $form->{business}, $dbh);
262 if (!$form->{customernumber}) {
263 $form->{customernumber} =
264 $form->update_defaults($myconfig, "customernumber", $dbh);
267 $query = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
268 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
270 $main::lxdebug->leave_sub();
274 $query = qq|SELECT nextval('id')|;
275 ($form->{id}) = selectrow_query($form, $dbh, $query);
277 $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
278 do_query($form, $dbh, $query, $form->{id});
281 $query = qq|UPDATE customer SET | .
282 qq|customernumber = ?, | .
285 qq|department_1 = ?, | .
286 qq|department_2 = ?, | .
300 qq|creditlimit = ?, | .
302 qq|business_id = ?, | .
303 qq|taxnumber = ?, | .
306 qq|account_number = ?, | .
307 qq|bank_code = ?, | .
312 qq|salesman_id = ?, | .
313 qq|language_id = ?, | .
314 qq|payment_id = ?, | .
315 qq|taxzone_id = ?, | .
316 qq|user_password = ?, | .
317 qq|c_vendor_id = ?, | .
321 $form->{customernumber},
324 $form->{department_1},
325 $form->{department_2},
339 $form->{creditlimit},
340 conv_i($form->{terms}),
341 conv_i($form->{business}),
345 $form->{account_number},
348 $form->{obsolete} ? 't' : 'f',
351 conv_i($form->{salesman_id}),
352 conv_i($form->{language_id}),
353 conv_i($form->{payment_id}),
354 conv_i($form->{taxzone_id}, 0),
355 $form->{user_password},
356 $form->{c_vendor_id},
357 conv_i($form->{klass}),
360 do_query( $form, $dbh, $query, @values );
363 if ( $form->{cp_id} ) {
364 $query = qq|UPDATE contacts SET | .
365 qq|cp_greeting = ?, | .
367 qq|cp_givenname = ?, | .
370 qq|cp_phone1 = ?, | .
371 qq|cp_phone2 = ?, | .
372 qq|cp_abteilung = ?, | .
374 qq|cp_mobile1 = ?, | .
375 qq|cp_mobile2 = ?, | .
376 qq|cp_satphone = ?, | .
377 qq|cp_satfax = ?, | .
378 qq|cp_project = ?, | .
379 qq|cp_privatphone = ?, | .
380 qq|cp_privatemail = ?, | .
381 qq|cp_birthday = ? | .
384 $form->{cp_greeting},
386 $form->{cp_givenname},
391 $form->{cp_abteilung},
395 $form->{cp_satphone},
398 $form->{cp_privatphone},
399 $form->{cp_privatemail},
400 $form->{cp_birthday},
403 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
405 qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, | .
406 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
407 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
409 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
412 $form->{cp_greeting},
414 $form->{cp_givenname},
419 $form->{cp_abteilung},
423 $form->{cp_satphone},
426 $form->{cp_privatphone},
427 $form->{cp_privatemail},
431 do_query( $form, $dbh, $query, @values ) if ($query);
434 $form->add_shipto( $dbh, $form->{id}, "CT" );
436 $self->_save_note('dbh' => $dbh);
437 $self->_delete_selected_notes('dbh' => $dbh);
439 CVar->save_custom_variables('dbh' => $dbh,
441 'trans_id' => $form->{id},
442 'variables' => $form);
444 $rc = $dbh->commit();
447 $main::lxdebug->leave_sub();
452 $main::lxdebug->enter_sub();
454 my ( $self, $myconfig, $form ) = @_;
456 $form->{taxzone_id} *= 1;
457 # connect to database
458 my $dbh = $form->dbconnect_noauto($myconfig);
461 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
462 if ( $form->{"selected_cp_${_}"} );
463 } qw(title greeting abteilung) );
464 $form->{"greeting"} = $form->{"selected_company_greeting"}
465 if ( $form->{"selected_company_greeting"} );
467 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
468 $form->{discount} /= 100;
469 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
474 $query = qq|SELECT nextval('id')|;
475 ($form->{id}) = selectrow_query($form, $dbh, $query);
477 $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
478 do_query($form, $dbh, $query, $form->{id});
480 if ( !$form->{vendornumber} ) {
481 $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
486 qq|UPDATE vendor SET | .
487 qq| vendornumber = ?, | .
489 qq| greeting = ?, | .
490 qq| department_1 = ?, | .
491 qq| department_2 = ?, | .
496 qq| homepage = ?, | .
505 qq| discount = ?, | .
506 qq| creditlimit = ?, | .
507 qq| business_id = ?, | .
508 qq| taxnumber = ?, | .
509 qq| sic_code = ?, | .
510 qq| language = ?, | .
511 qq| account_number = ?, | .
512 qq| bank_code = ?, | .
514 qq| obsolete = ?, | .
516 qq| payment_id = ?, | .
517 qq| taxzone_id = ?, | .
518 qq| language_id = ?, | .
519 qq| username = ?, | .
520 qq| user_password = ?, | .
521 qq| v_customer_id = ? | .
524 $form->{vendornumber},
527 $form->{department_1},
528 $form->{department_2},
541 conv_i($form->{terms}),
543 $form->{creditlimit},
544 conv_i($form->{business}),
548 $form->{account_number},
551 $form->{obsolete} ? 't' : 'f',
553 conv_i($form->{payment_id}),
554 conv_i($form->{taxzone_id}, 0),
555 conv_i( $form->{language_id}),
557 $form->{user_password},
558 $form->{v_customer_id},
561 do_query($form, $dbh, $query, @values);
564 if ( $form->{cp_id} ) {
565 $query = qq|UPDATE contacts SET | .
566 qq|cp_greeting = ?, | .
568 qq|cp_givenname = ?, | .
571 qq|cp_phone1 = ?, | .
572 qq|cp_phone2 = ?, | .
573 qq|cp_abteilung = ?, | .
575 qq|cp_mobile1 = ?, | .
576 qq|cp_mobile2 = ?, | .
577 qq|cp_satphone = ?, | .
578 qq|cp_satfax = ?, | .
579 qq|cp_project = ?, | .
580 qq|cp_privatphone = ?, | .
581 qq|cp_privatemail = ?, | .
582 qq|cp_birthday = ? | .
585 $form->{cp_greeting},
587 $form->{cp_givenname},
592 $form->{cp_abteilung},
596 $form->{cp_satphone},
599 $form->{cp_privatphone},
600 $form->{cp_privatemail},
601 $form->{cp_birthday},
604 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
606 qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, | .
607 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
608 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
610 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
613 $form->{cp_greeting},
615 $form->{cp_givenname},
620 $form->{cp_abteilung},
624 $form->{cp_satphone},
627 $form->{cp_privatphone},
628 $form->{cp_privatemail},
632 do_query($form, $dbh, $query, @values) if ($query);
635 $form->add_shipto( $dbh, $form->{id}, "CT" );
637 $self->_save_note('dbh' => $dbh);
638 $self->_delete_selected_notes('dbh' => $dbh);
640 CVar->save_custom_variables('dbh' => $dbh,
642 'trans_id' => $form->{id},
643 'variables' => $form);
645 $rc = $dbh->commit();
648 $main::lxdebug->leave_sub();
653 $main::lxdebug->enter_sub();
655 my ( $self, $myconfig, $form ) = @_;
656 # connect to database
657 my $dbh = $form->dbconnect($myconfig);
660 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
661 my $query = qq|DELETE FROM $cv WHERE id = ?|;
662 do_query($form, $dbh, $query, $form->{id});
666 $main::lxdebug->leave_sub();
670 $main::lxdebug->enter_sub();
672 my ( $self, $myconfig, $form ) = @_;
674 # connect to database
675 my $dbh = $form->dbconnect($myconfig);
677 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
682 my %allowed_sort_columns =
683 map({ $_, 1 } qw(id customernumber name address contact phone fax email
684 taxnumber sic_code business invnumber ordnumber quonumber));
686 $form->{sort} && $allowed_sort_columns{$form->{sort}} ?
687 $form->{sort} : "name";
688 $form->{sort} = $sortorder;
689 $sortorder = "country,city,street" if ($sortorder eq "address");
691 if ($form->{"${cv}number"}) {
692 $where .= " AND ct.${cv}number ILIKE ?";
693 push(@values, '%' . $form->{"${cv}number"} . '%');
696 foreach my $key (qw(name contact email)) {
698 $where .= " AND ct.$key ILIKE ?";
699 push(@values, '%' . $form->{$key} . '%');
703 if ( $form->{status} eq 'orphaned' ) {
705 qq| AND ct.id NOT IN | .
706 qq| (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
707 if ($cv eq 'customer') {
709 qq| AND ct.id NOT IN | .
710 qq| (SELECT a.customer_id FROM ar a, customer cv | .
711 qq| WHERE cv.id = a.customer_id)|;
713 if ($cv eq 'vendor') {
715 qq| AND ct.id NOT IN | .
716 qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
717 qq| WHERE cv.id = a.vendor_id)|;
719 $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
722 if ($form->{obsolete} eq "Y") {
723 $where .= qq| AND obsolete|;
724 } elsif ($form->{obsolete} eq "N") {
725 $where .= qq| AND NOT obsolete|;
728 if ($form->{business_id}) {
729 $where .= qq| AND (business_id = ?)|;
730 push(@values, conv_i($form->{business_id}));
733 my ($cvar_where, @cvar_values) = CVar->build_filter_query('module' => 'CT',
734 'trans_id_field' => 'ct.id',
738 $where .= qq| AND ($cvar_where)|;
739 push @values, @cvar_values;
743 qq|SELECT ct.*, b.description AS business | .
745 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
748 my @saved_values = @values;
749 # redo for invoices, orders and quotations
750 if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
751 my ($ar, $union, $module);
754 if ($form->{l_invnumber}) {
755 my $ar = $cv eq 'customer' ? 'ar' : 'ap';
756 my $module = $ar eq 'ar' ? 'is' : 'ir';
759 qq|SELECT ct.*, b.description AS business, | .
760 qq| a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
761 qq| '$module' AS module, 'invoice' AS formtype, | .
762 qq| (a.amount = a.paid) AS closed | .
764 qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
765 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
766 qq|WHERE $where AND (a.invoice = '1')|;
771 if ( $form->{l_ordnumber} ) {
772 if ($union eq "UNION") {
773 push(@values, @saved_values);
777 qq|SELECT ct.*, b.description AS business,| .
778 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
779 qq| 'oe' AS module, 'order' AS formtype, o.closed | .
781 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
782 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
783 qq|WHERE $where AND (o.quotation = '0')|;
788 if ( $form->{l_quonumber} ) {
789 if ($union eq "UNION") {
790 push(@values, @saved_values);
794 qq|SELECT ct.*, b.description AS business, | .
795 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
796 qq| 'oe' AS module, 'quotation' AS formtype, o.closed | .
798 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
799 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
800 qq|WHERE $where AND (o.quotation = '1')|;
804 $query .= qq| ORDER BY $sortorder|;
806 $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
807 map({ my $ref = $_; $ref->{address} = join(" ", map({ $ref->{$_} } qw(street zipcode city country))); }
810 $main::lxdebug->leave_sub();
814 $main::lxdebug->enter_sub();
816 my ( $self, $myconfig, $form ) = @_;
817 my $dbh = $form->dbconnect($myconfig);
819 qq|SELECT * FROM contacts c | .
820 qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
821 my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
822 my $ref = $sth->fetchrow_hashref(NAME_lc);
824 map { $form->{$_} = $ref->{$_} } keys %$ref;
826 $query = qq|SELECT COUNT(cp_id) AS used FROM (
827 SELECT cp_id FROM oe UNION
828 SELECT cp_id FROM ar UNION
829 SELECT cp_id FROM ap UNION
830 SELECT cp_id FROM delivery_orders
831 ) AS cpid WHERE cp_id = ? OR ? = 0|;
832 ($form->{cp_used}) = selectfirst_array_query($form, $dbh, $query, ($form->{cp_id})x2);
837 $main::lxdebug->leave_sub();
841 $main::lxdebug->enter_sub();
843 my ( $self, $myconfig, $form ) = @_;
844 my $dbh = $form->dbconnect($myconfig);
845 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
846 my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
848 my $ref = $sth->fetchrow_hashref(NAME_lc);
850 map { $form->{$_} = $ref->{$_} } keys %$ref;
852 $query = qq|SELECT COUNT(shipto_id) AS used FROM (
853 SELECT shipto_id FROM oe UNION
854 SELECT shipto_id FROM ar UNION
855 SELECT shipto_id FROM delivery_orders
856 ) AS stid WHERE shipto_id = ? OR ? = 0|;
857 ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
862 $main::lxdebug->leave_sub();
866 $main::lxdebug->enter_sub();
868 my ( $self, $myconfig, $form ) = @_;
869 my $dbh = $form->dbconnect($myconfig);
871 my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
872 my $db = $form->{db} eq "customer" ? "customer" : "vendor";
874 my $where = " WHERE 1=1 ";
877 if ($form->{shipto_id} && ($arap eq "ar")) {
878 $where .= "AND ${arap}.shipto_id = ?";
879 push(@values, $form->{shipto_id});
881 $where .= "AND ${arap}.${db}_id = ?";
882 push(@values, $form->{id});
886 $where .= "AND ${arap}.transdate >= ?";
887 push(@values, conv_date($form->{from}));
890 $where .= "AND ${arap}.transdate <= ?";
891 push(@values, conv_date($form->{to}));
894 qq|SELECT s.shiptoname, i.qty, | .
895 qq| ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
896 qq| i.description, i.unit, i.sellprice | .
898 qq|LEFT JOIN shipto s ON | .
900 ? qq|(ar.shipto_id = s.shipto_id) |
901 : qq|(ap.id = s.trans_id) |) .
902 qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
903 qq|LEFT join parts p ON (p.id = i.parts_id) | .
905 qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
907 $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
911 $main::lxdebug->leave_sub();
915 $main::lxdebug->enter_sub();
920 my $form = $main::form;
922 Common::check_params(\%params, 'dbh');
924 if (!$form->{NOTE_subject}) {
925 $main::lxdebug->leave_sub();
929 my $dbh = $params{dbh};
933 'id' => $form->{NOTE_id},
934 'subject' => $form->{NOTE_subject},
935 'body' => $form->{NOTE_body},
936 'trans_id' => $form->{id},
937 'trans_module' => 'ct',
940 $note{id} = Notes->save(%note);
942 if ($form->{FU_date}) {
944 'id' => $form->{FU_id},
945 'note_id' => $note{id},
946 'follow_up_date' => $form->{FU_date},
947 'created_for_user' => $form->{FU_created_for_user},
948 'done' => $form->{FU_done} ? 1 : 0,
949 'subject' => $form->{NOTE_subject},
950 'body' => $form->{NOTE_body},
953 'trans_id' => $form->{id},
954 'trans_type' => $form->{db} eq 'customer' ? 'customer' : 'vendor',
955 'trans_info' => $form->{name},
960 $follow_up{id} = FU->save(%follow_up);
962 } elsif ($form->{FU_id}) {
963 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
964 do_query($form, $dbh, qq|DELETE FROM follow_ups WHERE id = ?|, conv_i($form->{FU_id}));
967 delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
969 $main::lxdebug->leave_sub();
972 sub _delete_selected_notes {
973 $main::lxdebug->enter_sub();
978 Common::check_params(\%params, 'dbh');
980 my $form = $main::form;
981 my $dbh = $params{dbh};
983 foreach my $i (1 .. $form->{NOTES_rowcount}) {
984 next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
986 Notes->delete('dbh' => $params{dbh},
987 'id' => $form->{"NOTE_id_$i"});
990 $main::lxdebug->leave_sub();
994 $main::lxdebug->enter_sub();
997 my $shipto_id = shift;
999 my $form = $main::form;
1000 my %myconfig = %main::myconfig;
1001 my $dbh = $form->get_standard_dbh(\%myconfig);
1003 do_query($form, $dbh, qq|UPDATE shipto SET trans_id = NULL WHERE shipto_id = ?|, $shipto_id);
1007 $main::lxdebug->leave_sub();
1011 $main::lxdebug->enter_sub();
1014 my $shipto_id = shift;
1016 my $form = $main::form;
1017 my %myconfig = %main::myconfig;
1018 my $dbh = $form->get_standard_dbh(\%myconfig);
1020 do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $shipto_id);
1024 $main::lxdebug->leave_sub();