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 #======================================================================
52 $main::lxdebug->enter_sub();
54 my ( $self, $myconfig, $form ) = @_;
56 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
58 my $dbh = $form->dbconnect($myconfig);
60 qq|SELECT ct.*, b.id AS business, cp.* | .
62 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
63 qq|LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id) | .
64 qq|WHERE (ct.id = ?) | .
65 qq|ORDER BY cp.cp_id LIMIT 1|;
66 my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
68 my $ref = $sth->fetchrow_hashref("NAME_lc");
70 map { $form->{$_} = $ref->{$_} } keys %$ref;
73 if ( $form->{salesman_id} ) {
75 qq|SELECT ct.name AS salesman | .
79 selectrow_query($form, $dbh, $query, $form->{salesman_id});
82 my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
84 qq|SELECT n.*, n.itime::DATE AS created_on,
85 e.name AS created_by_name, e.login AS created_by_login
87 LEFT JOIN employee e ON (n.created_by = e.id)
88 WHERE (n.trans_id = ?) AND (n.trans_module = 'ct')|;
89 $form->{NOTES} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
92 qq|SELECT fu.follow_up_date, fu.done AS follow_up_done, e.name AS created_for_name, e.name AS created_for_login
94 LEFT JOIN employee e ON (fu.created_for_user = e.id)
95 WHERE (fu.note_id = ?)
96 AND NOT COALESCE(fu.done, FALSE)
97 AND ( (fu.created_by = ?)
98 OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?)))|;
99 $sth = prepare_query($form, $dbh, $query);
101 foreach my $note (@{ $form->{NOTES} }) {
102 do_statement($form, $sth, $query, conv_i($note->{id}), conv_i($note->{created_by}), conv_i($employee_id));
103 $ref = $sth->fetchrow_hashref();
105 map { $note->{$_} = $ref->{$_} } keys %{ $ref } if ($ref);
110 if ($form->{edit_note_id}) {
112 qq|SELECT n.id AS NOTE_id, n.subject AS NOTE_subject, n.body AS NOTE_body,
113 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
115 LEFT JOIN follow_ups fu ON ((n.id = fu.note_id) AND NOT COALESCE(fu.done, FALSE))
117 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{edit_note_id}));
120 foreach my $key (keys %{ $ref }) {
122 $new_key =~ s/^([^_]+)/\U$1\E/;
123 $form->{$new_key} = $ref->{$key};
128 # check if it is orphaned
129 my $arap = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
132 if ($form->{db} eq 'vendor') {
133 $makemodel = qq| UNION SELECT 1 FROM makemodel mm WHERE mm.make = ?|;
140 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
141 qq|WHERE ct.id = ? | .
145 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
148 my ($dummy) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x $num_args);
150 $form->{status} = "orphaned" unless ($dummy);
154 $main::lxdebug->leave_sub();
157 sub populate_drop_down_boxes {
158 $main::lxdebug->enter_sub();
160 my ($self, $myconfig, $form, $provided_dbh) = @_;
163 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
166 $query = qq|SELECT id, description FROM business ORDER BY id|;
167 $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
171 qq|SELECT shipto_id, shiptoname, shiptodepartment_1, shiptostreet, shiptocity
173 WHERE (trans_id = ?) AND (module = 'CT')|;
174 $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
177 $query = qq|SELECT cp_id, cp_name, cp_givenname FROM contacts WHERE cp_cv_id = ? ORDER BY cp_name|;
178 $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
181 $query = qq|SELECT id, description FROM language ORDER BY id|;
182 $form->{languages} = selectall_hashref_query($form, $dbh, $query);
185 $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
186 $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
188 $dbh->disconnect() unless ($provided_dbh);
190 $main::lxdebug->leave_sub();
193 sub query_titles_and_greetings {
194 $main::lxdebug->enter_sub();
196 my ( $self, $myconfig, $form ) = @_;
197 my ( %tmp, $ref, $query );
199 my $dbh = $form->dbconnect($myconfig);
202 qq|SELECT DISTINCT(greeting) | .
204 qq|WHERE greeting ~ '[a-zA-Z]' | .
206 qq|SELECT DISTINCT(greeting) | .
208 qq|WHERE greeting ~ '[a-zA-Z]' | .
209 qq|ORDER BY greeting|;
211 map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
212 $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
215 qq|SELECT DISTINCT(cp_title) | .
217 qq|WHERE cp_title ~ '[a-zA-Z]'|;
218 $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
221 qq|SELECT DISTINCT(cp_abteilung) | .
223 qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
224 $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
227 $main::lxdebug->leave_sub();
231 $main::lxdebug->enter_sub();
233 my ( $self, $myconfig, $form ) = @_;
235 # set pricegroup to default
236 $form->{klass} = 0 unless ($form->{klass});
238 # connect to database
239 my $dbh = $form->get_standard_dbh;
242 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
243 if ( $form->{"selected_cp_${_}"} );
244 } qw(title greeting abteilung) );
245 $form->{"greeting"} = $form->{"selected_company_greeting"}
246 if ( $form->{"selected_company_greeting"} );
248 # assign value discount, terms, creditlimit
249 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
250 $form->{discount} /= 100;
251 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
253 my ( $query, $sth, $f_id );
256 $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
257 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
259 if (($f_id ne $form->{id}) && ($f_id ne "")) {
260 $main::lxdebug->leave_sub();
265 my $customernumber = SL::TransNumber->new(type => 'customer',
267 number => $form->{customernumber},
268 business_id => $form->{business},
270 $form->{customernumber} = $customernumber->create_unique unless $customernumber->is_unique;
272 $query = qq|SELECT nextval('id')|;
273 ($form->{id}) = selectrow_query($form, $dbh, $query);
275 $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
276 do_query($form, $dbh, $query, $form->{id});
279 $query = qq|UPDATE customer SET | .
280 qq|customernumber = ?, | .
283 qq|department_1 = ?, | .
284 qq|department_2 = ?, | .
298 qq|creditlimit = ?, | .
300 qq|business_id = ?, | .
301 qq|taxnumber = ?, | .
303 qq|account_number = ?, | .
304 qq|bank_code = ?, | .
309 qq|direct_debit = ?, | .
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}),
344 $form->{account_number},
349 $form->{obsolete} ? 't' : 'f',
350 $form->{direct_debit} ? 't' : 'f',
353 conv_i($form->{salesman_id}),
354 conv_i($form->{language_id}),
355 conv_i($form->{payment_id}),
356 conv_i($form->{taxzone_id}, 0),
357 $form->{user_password},
358 $form->{c_vendor_id},
359 conv_i($form->{klass}),
362 do_query( $form, $dbh, $query, @values );
365 if ( $form->{cp_id} ) {
366 $query = qq|UPDATE contacts SET | .
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 = ?, | .
387 $form->{cp_givenname},
392 $form->{cp_abteilung},
396 $form->{cp_satphone},
399 $form->{cp_privatphone},
400 $form->{cp_privatemail},
401 $form->{cp_birthday},
402 $form->{cp_gender} eq 'f' ? 'f' : 'm',
405 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
407 qq|INSERT INTO contacts ( cp_cv_id, cp_title, cp_givenname, | .
408 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
409 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
410 qq| cp_birthday, cp_gender) | .
411 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
415 $form->{cp_givenname},
420 $form->{cp_abteilung},
424 $form->{cp_satphone},
427 $form->{cp_privatphone},
428 $form->{cp_privatemail},
429 $form->{cp_birthday},
430 $form->{cp_gender} eq 'f' ? 'f' : 'm',
433 do_query( $form, $dbh, $query, @values ) if ($query);
436 $form->add_shipto( $dbh, $form->{id}, "CT" );
438 $self->_save_note('dbh' => $dbh);
439 $self->_delete_selected_notes('dbh' => $dbh);
441 CVar->save_custom_variables('dbh' => $dbh,
443 'trans_id' => $form->{id},
444 'variables' => $form,
445 'always_valid' => 1);
447 my $rc = $dbh->commit();
449 $main::lxdebug->leave_sub();
454 $main::lxdebug->enter_sub();
456 my ( $self, $myconfig, $form ) = @_;
458 $form->{taxzone_id} *= 1;
459 # connect to database
460 my $dbh = $form->dbconnect_noauto($myconfig);
463 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
464 if ( $form->{"selected_cp_${_}"} );
465 } qw(title greeting abteilung) );
466 $form->{"greeting"} = $form->{"selected_company_greeting"}
467 if ( $form->{"selected_company_greeting"} );
469 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
470 $form->{discount} /= 100;
471 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
476 $query = qq|SELECT nextval('id')|;
477 ($form->{id}) = selectrow_query($form, $dbh, $query);
479 $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
480 do_query($form, $dbh, $query, $form->{id});
482 my $vendornumber = SL::TransNumber->new(type => 'vendor',
484 number => $form->{vendornumber},
486 $form->{vendornumber} = $vendornumber->create_unique unless $vendornumber->is_unique;
490 qq|UPDATE vendor SET | .
491 qq| vendornumber = ?, | .
493 qq| greeting = ?, | .
494 qq| department_1 = ?, | .
495 qq| department_2 = ?, | .
500 qq| homepage = ?, | .
509 qq| discount = ?, | .
510 qq| creditlimit = ?, | .
511 qq| business_id = ?, | .
512 qq| taxnumber = ?, | .
513 qq| language = ?, | .
514 qq| account_number = ?, | .
515 qq| bank_code = ?, | .
519 qq| obsolete = ?, | .
520 qq| direct_debit = ?, | .
522 qq| payment_id = ?, | .
523 qq| taxzone_id = ?, | .
524 qq| language_id = ?, | .
525 qq| username = ?, | .
526 qq| user_password = ?, | .
527 qq| v_customer_id = ? | .
530 $form->{vendornumber},
533 $form->{department_1},
534 $form->{department_2},
547 conv_i($form->{terms}),
549 $form->{creditlimit},
550 conv_i($form->{business}),
553 $form->{account_number},
558 $form->{obsolete} ? 't' : 'f',
559 $form->{direct_debit} ? 't' : 'f',
561 conv_i($form->{payment_id}),
562 conv_i($form->{taxzone_id}, 0),
563 conv_i( $form->{language_id}),
565 $form->{user_password},
566 $form->{v_customer_id},
569 do_query($form, $dbh, $query, @values);
572 if ( $form->{cp_id} ) {
573 $query = qq|UPDATE contacts SET | .
575 qq|cp_givenname = ?, | .
578 qq|cp_phone1 = ?, | .
579 qq|cp_phone2 = ?, | .
580 qq|cp_abteilung = ?, | .
582 qq|cp_mobile1 = ?, | .
583 qq|cp_mobile2 = ?, | .
584 qq|cp_satphone = ?, | .
585 qq|cp_satfax = ?, | .
586 qq|cp_project = ?, | .
587 qq|cp_privatphone = ?, | .
588 qq|cp_privatemail = ?, | .
589 qq|cp_birthday = ?, | .
594 $form->{cp_givenname},
599 $form->{cp_abteilung},
603 $form->{cp_satphone},
606 $form->{cp_privatphone},
607 $form->{cp_privatemail},
608 $form->{cp_birthday},
609 $form->{cp_gender} eq 'f' ? 'f' : 'm',
612 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
614 qq|INSERT INTO contacts ( cp_cv_id, cp_title, cp_givenname, | .
615 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
616 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
617 qq| cp_birthday, cp_gender) | .
618 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
622 $form->{cp_givenname},
627 $form->{cp_abteilung},
631 $form->{cp_satphone},
634 $form->{cp_privatphone},
635 $form->{cp_privatemail},
636 $form->{cp_birthday},
640 do_query($form, $dbh, $query, @values) if ($query);
643 $form->add_shipto( $dbh, $form->{id}, "CT" );
645 $self->_save_note('dbh' => $dbh);
646 $self->_delete_selected_notes('dbh' => $dbh);
648 CVar->save_custom_variables('dbh' => $dbh,
650 'trans_id' => $form->{id},
651 'variables' => $form,
652 'always_valid' => 1);
654 my $rc = $dbh->commit();
657 $main::lxdebug->leave_sub();
662 $main::lxdebug->enter_sub();
664 my ( $self, $myconfig, $form ) = @_;
665 # connect to database
666 my $dbh = $form->dbconnect($myconfig);
669 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
670 my $query = qq|DELETE FROM $cv WHERE id = ?|;
671 do_query($form, $dbh, $query, $form->{id});
675 $main::lxdebug->leave_sub();
679 $main::lxdebug->enter_sub();
681 my ( $self, $myconfig, $form ) = @_;
683 # connect to database
684 my $dbh = $form->dbconnect($myconfig);
686 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
691 my %allowed_sort_columns =
693 id customernumber vendornumber name contact phone fax email street
694 taxnumber business invnumber ordnumber quonumber zipcode city
696 my $sortorder = $form->{sort} && $allowed_sort_columns{$form->{sort}} ? $form->{sort} : "name";
697 $form->{sort} = $sortorder;
698 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
700 if ($sortorder ne 'id' && 1 >= scalar grep { $form->{$_} } qw(l_ordnumber l_quonumber l_invnumber)) {
701 $sortorder = "lower($sortorder) ${sortdir}";
703 $sortorder .= " ${sortdir}";
706 if ($form->{"${cv}number"}) {
707 $where .= " AND ct.${cv}number ILIKE ?";
708 push(@values, '%' . $form->{"${cv}number"} . '%');
711 foreach my $key (qw(name contact email)) {
713 $where .= " AND ct.$key ILIKE ?";
714 push(@values, '%' . $form->{$key} . '%');
718 if ($form->{cp_name}) {
719 $where .= " AND ct.id IN (SELECT cp_cv_id FROM contacts WHERE lower(cp_name) LIKE lower(?))";
720 push @values, '%' . $form->{cp_name} . '%';
723 if ($form->{addr_city}) {
724 $where .= " AND ((lower(ct.city) LIKE lower(?))
729 WHERE (module = 'CT')
730 AND (lower(shiptocity) LIKE lower(?))
733 push @values, ('%' . $form->{addr_city} . '%') x 2;
736 if ( $form->{status} eq 'orphaned' ) {
738 qq| AND ct.id NOT IN | .
739 qq| (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
740 if ($cv eq 'customer') {
742 qq| AND ct.id NOT IN | .
743 qq| (SELECT a.customer_id FROM ar a, customer cv | .
744 qq| WHERE cv.id = a.customer_id)|;
746 if ($cv eq 'vendor') {
748 qq| AND ct.id NOT IN | .
749 qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
750 qq| WHERE cv.id = a.vendor_id)|;
752 $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
755 if ($form->{obsolete} eq "Y") {
756 $where .= qq| AND obsolete|;
757 } elsif ($form->{obsolete} eq "N") {
758 $where .= qq| AND NOT obsolete|;
761 if ($form->{business_id}) {
762 $where .= qq| AND (business_id = ?)|;
763 push(@values, conv_i($form->{business_id}));
766 my ($cvar_where, @cvar_values) = CVar->build_filter_query('module' => 'CT',
767 'trans_id_field' => 'ct.id',
771 $where .= qq| AND ($cvar_where)|;
772 push @values, @cvar_values;
775 if ($form->{addr_street}) {
776 $where .= qq| AND (street ILIKE ?)|;
777 push @values, '%' . $form->{addr_street} . '%';
780 if ($form->{addr_zipcode}) {
781 $where .= qq| AND (zipcode ILIKE ?)|;
782 push @values, $form->{addr_zipcode} . '%';
786 qq|SELECT ct.*, b.description AS business | .
788 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
791 my @saved_values = @values;
792 # redo for invoices, orders and quotations
793 if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
794 my ($ar, $union, $module);
797 if ($form->{l_invnumber}) {
798 my $ar = $cv eq 'customer' ? 'ar' : 'ap';
799 my $module = $ar eq 'ar' ? 'is' : 'ir';
802 qq|SELECT ct.*, b.description AS business, | .
803 qq| a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
804 qq| '$module' AS module, 'invoice' AS formtype, | .
805 qq| (a.amount = a.paid) AS closed | .
807 qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
808 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
809 qq|WHERE $where AND (a.invoice = '1')|;
814 if ( $form->{l_ordnumber} ) {
815 if ($union eq "UNION") {
816 push(@values, @saved_values);
820 qq|SELECT ct.*, b.description AS business,| .
821 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
822 qq| 'oe' AS module, 'order' AS formtype, o.closed | .
824 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
825 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
826 qq|WHERE $where AND (o.quotation = '0')|;
831 if ( $form->{l_quonumber} ) {
832 if ($union eq "UNION") {
833 push(@values, @saved_values);
837 qq|SELECT ct.*, b.description AS business, | .
838 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
839 qq| 'oe' AS module, 'quotation' AS formtype, o.closed | .
841 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
842 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
843 qq|WHERE $where AND (o.quotation = '1')|;
847 $query .= qq| ORDER BY $sortorder|;
849 $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
851 $main::lxdebug->leave_sub();
855 $main::lxdebug->enter_sub();
857 my ( $self, $myconfig, $form ) = @_;
858 my $dbh = $form->dbconnect($myconfig);
860 qq|SELECT * FROM contacts c | .
861 qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
862 my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
863 my $ref = $sth->fetchrow_hashref("NAME_lc");
865 map { $form->{$_} = $ref->{$_} } keys %$ref;
867 $query = qq|SELECT COUNT(cp_id) AS used FROM (
868 SELECT cp_id FROM oe UNION
869 SELECT cp_id FROM ar UNION
870 SELECT cp_id FROM ap UNION
871 SELECT cp_id FROM delivery_orders
872 ) AS cpid WHERE cp_id = ? OR ? = 0|;
873 ($form->{cp_used}) = selectfirst_array_query($form, $dbh, $query, ($form->{cp_id})x2);
878 $main::lxdebug->leave_sub();
882 $main::lxdebug->enter_sub();
884 my ( $self, $myconfig, $form ) = @_;
885 my $dbh = $form->dbconnect($myconfig);
886 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
887 my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
889 my $ref = $sth->fetchrow_hashref("NAME_lc");
891 map { $form->{$_} = $ref->{$_} } keys %$ref;
893 $query = qq|SELECT COUNT(shipto_id) AS used FROM (
894 SELECT shipto_id FROM oe UNION
895 SELECT shipto_id FROM ar UNION
896 SELECT shipto_id FROM delivery_orders
897 ) AS stid WHERE shipto_id = ? OR ? = 0|;
898 ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
903 $main::lxdebug->leave_sub();
907 $main::lxdebug->enter_sub();
909 my ( $self, $myconfig, $form ) = @_;
910 my $dbh = $form->dbconnect($myconfig);
912 my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
913 my $db = $form->{db} eq "customer" ? "customer" : "vendor";
914 my $qty_sign = $form->{db} eq 'vendor' ? ' * -1 AS qty' : '';
916 my $where = " WHERE 1=1 ";
919 if ($form->{shipto_id} && ($arap eq "ar")) {
920 $where .= "AND ${arap}.shipto_id = ?";
921 push(@values, $form->{shipto_id});
923 $where .= "AND ${arap}.${db}_id = ?";
924 push(@values, $form->{id});
928 $where .= "AND ${arap}.transdate >= ?";
929 push(@values, conv_date($form->{from}));
932 $where .= "AND ${arap}.transdate <= ?";
933 push(@values, conv_date($form->{to}));
936 qq|SELECT s.shiptoname, i.qty $qty_sign, | .
937 qq| ${arap}.id, ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
938 qq| i.description, i.unit, i.sellprice, | .
939 qq| oe.id AS oe_id, invoice | .
941 qq|LEFT JOIN shipto s ON | .
943 ? qq|(ar.shipto_id = s.shipto_id) |
944 : qq|(ap.id = s.trans_id) |) .
945 qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
946 qq|LEFT join parts p ON (p.id = i.parts_id) | .
947 qq|LEFT JOIN oe ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '') | .
949 qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
951 $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
955 $main::lxdebug->leave_sub();
959 $main::lxdebug->enter_sub();
964 my $form = $main::form;
966 Common::check_params(\%params, 'dbh');
968 if (!$form->{NOTE_subject}) {
969 $main::lxdebug->leave_sub();
973 my $dbh = $params{dbh};
977 'id' => $form->{NOTE_id},
978 'subject' => $form->{NOTE_subject},
979 'body' => $form->{NOTE_body},
980 'trans_id' => $form->{id},
981 'trans_module' => 'ct',
984 $note{id} = Notes->save(%note);
986 if ($form->{FU_date}) {
988 'id' => $form->{FU_id},
989 'note_id' => $note{id},
990 'follow_up_date' => $form->{FU_date},
991 'created_for_user' => $form->{FU_created_for_user},
992 'done' => $form->{FU_done} ? 1 : 0,
993 'subject' => $form->{NOTE_subject},
994 'body' => $form->{NOTE_body},
997 'trans_id' => $form->{id},
998 'trans_type' => $form->{db} eq 'customer' ? 'customer' : 'vendor',
999 'trans_info' => $form->{name},
1004 $follow_up{id} = FU->save(%follow_up);
1006 } elsif ($form->{FU_id}) {
1007 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
1008 do_query($form, $dbh, qq|DELETE FROM follow_ups WHERE id = ?|, conv_i($form->{FU_id}));
1011 delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
1013 $main::lxdebug->leave_sub();
1016 sub _delete_selected_notes {
1017 $main::lxdebug->enter_sub();
1022 Common::check_params(\%params, 'dbh');
1024 my $form = $main::form;
1025 my $dbh = $params{dbh};
1027 foreach my $i (1 .. $form->{NOTES_rowcount}) {
1028 next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
1030 Notes->delete('dbh' => $params{dbh},
1031 'id' => $form->{"NOTE_id_$i"});
1034 $main::lxdebug->leave_sub();
1038 $main::lxdebug->enter_sub();
1041 my $shipto_id = shift;
1043 my $form = $main::form;
1044 my %myconfig = %main::myconfig;
1045 my $dbh = $form->get_standard_dbh(\%myconfig);
1047 do_query($form, $dbh, qq|UPDATE shipto SET trans_id = NULL WHERE shipto_id = ?|, $shipto_id);
1051 $main::lxdebug->leave_sub();
1054 sub delete_contact {
1055 $main::lxdebug->enter_sub();
1060 my $form = $main::form;
1061 my %myconfig = %main::myconfig;
1062 my $dbh = $form->get_standard_dbh(\%myconfig);
1064 do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $cp_id);
1068 $main::lxdebug->leave_sub();
1072 $main::lxdebug->enter_sub();
1077 Common::check_params(\%params, qw(vc id));
1079 my $myconfig = \%main::myconfig;
1080 my $form = $main::form;
1082 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1084 my $table = $params{vc} eq 'customer' ? 'customer' : 'vendor';
1085 my @ids = ref $params{id} eq 'ARRAY' ? @{ $params{id} } : ($params{id});
1086 my $placeholders = join ", ", ('?') x scalar @ids;
1087 my $query = qq|SELECT id, name, account_number, bank, bank_code, iban, bic
1089 WHERE id IN (${placeholders})|;
1091 my $result = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
1093 if (ref $params{id} eq 'ARRAY') {
1094 $result = { map { $_->{id} => $_ } @{ $result } };
1096 $result = $result->[0] || { 'id' => $params{id} };
1099 $main::lxdebug->leave_sub();
1104 sub parse_excel_file {
1105 $main::lxdebug->enter_sub();
1107 my ($self, $myconfig, $form) = @_;
1108 my $locale = $main::locale;
1110 $form->{formname} = 'sales_quotation';
1111 $form->{type} = 'sales_quotation';
1112 $form->{format} = 'excel';
1113 $form->{media} = 'screen';
1114 $form->{quonumber} = 1;
1117 # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1118 $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1122 $form->{"${inv}date"} = $form->{transdate};
1123 $form->{label} = $locale->text('Quotation');
1124 my $numberfld = "sqnumber";
1128 $form->{what_done} = $form->{formname};
1130 map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
1132 my $output_dateformat = $myconfig->{"dateformat"};
1133 my $output_numberformat = $myconfig->{"numberformat"};
1134 my $output_longdates = 1;
1136 # map login user variables
1137 map { $form->{"login_$_"} = $myconfig->{$_} } ("name", "email", "fax", "tel", "company");
1140 for my $field (qw(transdate_oe deliverydate_oe)) {
1142 $form->{$field}[$_] = $locale->date($myconfig, $form->{$field}[$_], 1);
1143 } 0 .. $#{ $form->{$field} };
1146 if ($form->{shipto_id}) {
1147 $form->get_shipto($myconfig);
1150 $form->{notes} =~ s/^\s+//g;
1152 $form->{templates} = $myconfig->{templates};
1154 delete $form->{printer_command};
1156 $form->get_employee_info($myconfig);
1158 my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
1160 if (scalar @{ $cvar_date_fields }) {
1161 format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
1164 while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
1165 reformat_numbers($output_numberformat, $precision, @{ $field_list });
1169 my $extension = 'xls';
1171 $form->{IN} = "$form->{formname}.${extension}";
1173 delete $form->{OUT};
1175 $form->parse_template($myconfig);
1177 $main::lxdebug->leave_sub();