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;
72 # remove any trailing whitespace
73 $form->{curr} =~ s/\s*$//;
76 if ( $form->{salesman_id} ) {
78 qq|SELECT ct.name AS salesman | .
82 selectrow_query($form, $dbh, $query, $form->{salesman_id});
85 my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
87 qq|SELECT n.*, n.itime::DATE AS created_on,
88 e.name AS created_by_name, e.login AS created_by_login
90 LEFT JOIN employee e ON (n.created_by = e.id)
91 WHERE (n.trans_id = ?) AND (n.trans_module = 'ct')|;
92 $form->{NOTES} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
95 qq|SELECT fu.follow_up_date, fu.done AS follow_up_done, e.name AS created_for_name, e.name AS created_for_login
97 LEFT JOIN employee e ON (fu.created_for_user = e.id)
98 WHERE (fu.note_id = ?)
99 AND NOT COALESCE(fu.done, FALSE)
100 AND ( (fu.created_by = ?)
101 OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?)))|;
102 $sth = prepare_query($form, $dbh, $query);
104 foreach my $note (@{ $form->{NOTES} }) {
105 do_statement($form, $sth, $query, conv_i($note->{id}), conv_i($note->{created_by}), conv_i($employee_id));
106 $ref = $sth->fetchrow_hashref();
108 map { $note->{$_} = $ref->{$_} } keys %{ $ref } if ($ref);
113 if ($form->{edit_note_id}) {
115 qq|SELECT n.id AS NOTE_id, n.subject AS NOTE_subject, n.body AS NOTE_body,
116 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
118 LEFT JOIN follow_ups fu ON ((n.id = fu.note_id) AND NOT COALESCE(fu.done, FALSE))
120 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{edit_note_id}));
123 foreach my $key (keys %{ $ref }) {
125 $new_key =~ s/^([^_]+)/\U$1\E/;
126 $form->{$new_key} = $ref->{$key};
131 # check if it is orphaned
132 my $arap = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
135 if ($form->{db} eq 'vendor') {
136 $makemodel = qq| UNION SELECT 1 FROM makemodel mm WHERE mm.make = ?|;
143 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
144 qq|WHERE ct.id = ? | .
148 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
151 my ($dummy) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x $num_args);
153 $form->{status} = "orphaned" unless ($dummy);
157 $main::lxdebug->leave_sub();
160 sub populate_drop_down_boxes {
161 $main::lxdebug->enter_sub();
163 my ($self, $myconfig, $form, $provided_dbh) = @_;
166 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
169 $query = qq|SELECT id, description FROM business ORDER BY id|;
170 $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
174 qq|SELECT shipto_id, shiptoname, shiptodepartment_1, shiptostreet, shiptocity
176 WHERE (trans_id = ?) AND (module = 'CT')|;
177 $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
180 $query = qq|SELECT cp_id, cp_name, cp_givenname FROM contacts WHERE cp_cv_id = ? ORDER BY cp_name|;
181 $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
184 $query = qq|SELECT id, description FROM language ORDER BY id|;
185 $form->{languages} = selectall_hashref_query($form, $dbh, $query);
188 $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
189 $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
191 $dbh->disconnect() unless ($provided_dbh);
193 $main::lxdebug->leave_sub();
196 sub query_titles_and_greetings {
197 $main::lxdebug->enter_sub();
199 my ( $self, $myconfig, $form ) = @_;
200 my ( %tmp, $ref, $query );
202 my $dbh = $form->dbconnect($myconfig);
205 qq|SELECT DISTINCT(greeting) | .
207 qq|WHERE greeting ~ '[a-zA-Z]' | .
209 qq|SELECT DISTINCT(greeting) | .
211 qq|WHERE greeting ~ '[a-zA-Z]' | .
212 qq|ORDER BY greeting|;
214 map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
215 $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
218 qq|SELECT DISTINCT(cp_title) | .
220 qq|WHERE cp_title ~ '[a-zA-Z]'|;
221 $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
224 qq|SELECT DISTINCT(cp_abteilung) | .
226 qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
227 $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
230 $main::lxdebug->leave_sub();
234 $main::lxdebug->enter_sub();
236 my ( $self, $myconfig, $form ) = @_;
238 # set pricegroup to default
239 $form->{klass} = 0 unless ($form->{klass});
241 # connect to database
242 my $dbh = $form->get_standard_dbh;
245 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
246 if ( $form->{"selected_cp_${_}"} );
247 } qw(title greeting abteilung) );
248 $form->{"greeting"} = $form->{"selected_company_greeting"}
249 if ( $form->{"selected_company_greeting"} );
251 # assign value discount, terms, creditlimit
252 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
253 $form->{discount} /= 100;
254 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
256 my ( $query, $sth, $f_id );
259 $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
260 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
262 if (($f_id ne $form->{id}) && ($f_id ne "")) {
263 $main::lxdebug->leave_sub();
268 my $customernumber = SL::TransNumber->new(type => 'customer',
270 number => $form->{customernumber},
271 business_id => $form->{business},
273 $form->{customernumber} = $customernumber->create_unique unless $customernumber->is_unique;
275 $query = qq|SELECT nextval('id')|;
276 ($form->{id}) = selectrow_query($form, $dbh, $query);
278 $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
279 do_query($form, $dbh, $query, $form->{id});
282 $query = qq|UPDATE customer SET | .
283 qq|customernumber = ?, | .
286 qq|department_1 = ?, | .
287 qq|department_2 = ?, | .
301 qq|creditlimit = ?, | .
303 qq|business_id = ?, | .
304 qq|taxnumber = ?, | .
306 qq|account_number = ?, | .
307 qq|bank_code = ?, | .
312 qq|direct_debit = ?, | .
315 qq|salesman_id = ?, | .
316 qq|language_id = ?, | .
317 qq|payment_id = ?, | .
318 qq|taxzone_id = ?, | .
319 qq|user_password = ?, | .
320 qq|c_vendor_id = ?, | .
325 $form->{customernumber},
328 $form->{department_1},
329 $form->{department_2},
343 $form->{creditlimit},
344 conv_i($form->{terms}),
345 conv_i($form->{business}),
348 $form->{account_number},
353 $form->{obsolete} ? 't' : 'f',
354 $form->{direct_debit} ? 't' : 'f',
357 conv_i($form->{salesman_id}),
358 conv_i($form->{language_id}),
359 conv_i($form->{payment_id}),
360 conv_i($form->{taxzone_id}, 0),
361 $form->{user_password},
362 $form->{c_vendor_id},
363 conv_i($form->{klass}),
364 substr($form->{currency}, 0, 3),
367 do_query( $form, $dbh, $query, @values );
370 if ( $form->{cp_id} ) {
371 $query = qq|UPDATE contacts SET | .
373 qq|cp_givenname = ?, | .
376 qq|cp_phone1 = ?, | .
377 qq|cp_phone2 = ?, | .
378 qq|cp_abteilung = ?, | .
380 qq|cp_mobile1 = ?, | .
381 qq|cp_mobile2 = ?, | .
382 qq|cp_satphone = ?, | .
383 qq|cp_satfax = ?, | .
384 qq|cp_project = ?, | .
385 qq|cp_privatphone = ?, | .
386 qq|cp_privatemail = ?, | .
387 qq|cp_birthday = ?, | .
392 $form->{cp_givenname},
397 $form->{cp_abteilung},
401 $form->{cp_satphone},
404 $form->{cp_privatphone},
405 $form->{cp_privatemail},
406 $form->{cp_birthday},
407 $form->{cp_gender} eq 'f' ? 'f' : 'm',
410 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
412 qq|INSERT INTO contacts ( cp_cv_id, cp_title, cp_givenname, | .
413 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
414 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
415 qq| cp_birthday, cp_gender) | .
416 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
420 $form->{cp_givenname},
425 $form->{cp_abteilung},
429 $form->{cp_satphone},
432 $form->{cp_privatphone},
433 $form->{cp_privatemail},
434 $form->{cp_birthday},
435 $form->{cp_gender} eq 'f' ? 'f' : 'm',
438 do_query( $form, $dbh, $query, @values ) if ($query);
441 $form->add_shipto( $dbh, $form->{id}, "CT" );
443 $self->_save_note('dbh' => $dbh);
444 $self->_delete_selected_notes('dbh' => $dbh);
446 CVar->save_custom_variables('dbh' => $dbh,
448 'trans_id' => $form->{id},
449 'variables' => $form,
450 'always_valid' => 1);
452 my $rc = $dbh->commit();
454 $main::lxdebug->leave_sub();
459 $main::lxdebug->enter_sub();
461 my ( $self, $myconfig, $form ) = @_;
463 $form->{taxzone_id} *= 1;
464 # connect to database
465 my $dbh = $form->get_standard_dbh;
468 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
469 if ( $form->{"selected_cp_${_}"} );
470 } qw(title greeting abteilung) );
471 $form->{"greeting"} = $form->{"selected_company_greeting"}
472 if ( $form->{"selected_company_greeting"} );
474 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
475 $form->{discount} /= 100;
476 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
481 $query = qq|SELECT nextval('id')|;
482 ($form->{id}) = selectrow_query($form, $dbh, $query);
484 $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
485 do_query($form, $dbh, $query, $form->{id});
487 my $vendornumber = SL::TransNumber->new(type => 'vendor',
489 number => $form->{vendornumber},
491 $form->{vendornumber} = $vendornumber->create_unique unless $vendornumber->is_unique;
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 = ?, | .
536 $form->{vendornumber},
539 $form->{department_1},
540 $form->{department_2},
553 conv_i($form->{terms}),
555 $form->{creditlimit},
556 conv_i($form->{business}),
559 $form->{account_number},
564 $form->{obsolete} ? 't' : 'f',
565 $form->{direct_debit} ? 't' : 'f',
567 conv_i($form->{payment_id}),
568 conv_i($form->{taxzone_id}, 0),
569 conv_i( $form->{language_id}),
571 $form->{user_password},
572 $form->{v_customer_id},
573 substr($form->{currency}, 0, 3),
576 do_query($form, $dbh, $query, @values);
579 if ( $form->{cp_id} ) {
580 $query = qq|UPDATE contacts SET | .
582 qq|cp_givenname = ?, | .
585 qq|cp_phone1 = ?, | .
586 qq|cp_phone2 = ?, | .
587 qq|cp_abteilung = ?, | .
589 qq|cp_mobile1 = ?, | .
590 qq|cp_mobile2 = ?, | .
591 qq|cp_satphone = ?, | .
592 qq|cp_satfax = ?, | .
593 qq|cp_project = ?, | .
594 qq|cp_privatphone = ?, | .
595 qq|cp_privatemail = ?, | .
596 qq|cp_birthday = ?, | .
601 $form->{cp_givenname},
606 $form->{cp_abteilung},
610 $form->{cp_satphone},
613 $form->{cp_privatphone},
614 $form->{cp_privatemail},
615 $form->{cp_birthday},
616 $form->{cp_gender} eq 'f' ? 'f' : 'm',
619 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
621 qq|INSERT INTO contacts ( cp_cv_id, cp_title, cp_givenname, | .
622 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
623 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
624 qq| cp_birthday, cp_gender) | .
625 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
629 $form->{cp_givenname},
634 $form->{cp_abteilung},
638 $form->{cp_satphone},
641 $form->{cp_privatphone},
642 $form->{cp_privatemail},
643 $form->{cp_birthday},
647 do_query($form, $dbh, $query, @values) if ($query);
650 $form->add_shipto( $dbh, $form->{id}, "CT" );
652 $self->_save_note('dbh' => $dbh);
653 $self->_delete_selected_notes('dbh' => $dbh);
655 CVar->save_custom_variables('dbh' => $dbh,
657 'trans_id' => $form->{id},
658 'variables' => $form,
659 'always_valid' => 1);
661 my $rc = $dbh->commit();
663 $main::lxdebug->leave_sub();
668 $main::lxdebug->enter_sub();
670 my ( $self, $myconfig, $form ) = @_;
671 # connect to database
672 my $dbh = $form->dbconnect($myconfig);
675 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
676 my $query = qq|DELETE FROM $cv WHERE id = ?|;
677 do_query($form, $dbh, $query, $form->{id});
681 $main::lxdebug->leave_sub();
685 $main::lxdebug->enter_sub();
687 my ( $self, $myconfig, $form ) = @_;
689 # connect to database
690 my $dbh = $form->dbconnect($myconfig);
692 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
697 my %allowed_sort_columns =
699 id customernumber vendornumber name contact phone fax email street
700 taxnumber business invnumber ordnumber quonumber zipcode city
702 my $sortorder = $form->{sort} && $allowed_sort_columns{$form->{sort}} ? $form->{sort} : "name";
703 $form->{sort} = $sortorder;
704 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
706 if ($sortorder !~ /(business|id)/ && 1 >= scalar grep { $form->{$_} } qw(l_ordnumber l_quonumber l_invnumber )) {
707 $sortorder = "lower($sortorder) ${sortdir}";
709 $sortorder .= " ${sortdir}";
712 if ($form->{"${cv}number"}) {
713 $where .= " AND ct.${cv}number ILIKE ?";
714 push(@values, '%' . $form->{"${cv}number"} . '%');
717 foreach my $key (qw(name contact email)) {
719 $where .= " AND ct.$key ILIKE ?";
720 push(@values, '%' . $form->{$key} . '%');
724 if ($form->{cp_name}) {
725 $where .= " AND ct.id IN (SELECT cp_cv_id FROM contacts WHERE lower(cp_name) LIKE lower(?))";
726 push @values, '%' . $form->{cp_name} . '%';
729 if ($form->{addr_city}) {
730 $where .= " AND ((lower(ct.city) LIKE lower(?))
735 WHERE (module = 'CT')
736 AND (lower(shiptocity) LIKE lower(?))
739 push @values, ('%' . $form->{addr_city} . '%') x 2;
742 if ( $form->{status} eq 'orphaned' ) {
744 qq| AND ct.id NOT IN | .
745 qq| (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
746 if ($cv eq 'customer') {
748 qq| AND ct.id NOT IN | .
749 qq| (SELECT a.customer_id FROM ar a, customer cv | .
750 qq| WHERE cv.id = a.customer_id)|;
752 if ($cv eq 'vendor') {
754 qq| AND ct.id NOT IN | .
755 qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
756 qq| WHERE cv.id = a.vendor_id)|;
758 $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
761 if ($form->{obsolete} eq "Y") {
762 $where .= qq| AND obsolete|;
763 } elsif ($form->{obsolete} eq "N") {
764 $where .= qq| AND NOT obsolete|;
767 if ($form->{business_id}) {
768 $where .= qq| AND (business_id = ?)|;
769 push(@values, conv_i($form->{business_id}));
772 # Nur Kunden finden, bei denen ich selber der Verkäufer bin
773 # Gilt nicht fĂĽr Lieferanten
774 if ($cv eq 'customer' && !$main::auth->assert('customer_vendor_all_edit', 1)) {
775 $where .= qq| AND ct.salesman_id = (select id from employee where login= ?)|;
776 push(@values, $form->{login});
779 my ($cvar_where, @cvar_values) = CVar->build_filter_query('module' => 'CT',
780 'trans_id_field' => 'ct.id',
784 $where .= qq| AND ($cvar_where)|;
785 push @values, @cvar_values;
788 if ($form->{addr_street}) {
789 $where .= qq| AND (street ILIKE ?)|;
790 push @values, '%' . $form->{addr_street} . '%';
793 if ($form->{addr_zipcode}) {
794 $where .= qq| AND (zipcode ILIKE ?)|;
795 push @values, $form->{addr_zipcode} . '%';
799 qq|SELECT ct.*, b.description AS business | .
801 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
804 my @saved_values = @values;
805 # redo for invoices, orders and quotations
806 if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
807 my ($ar, $union, $module);
810 if ($form->{l_invnumber}) {
811 my $ar = $cv eq 'customer' ? 'ar' : 'ap';
812 my $module = $ar eq 'ar' ? 'is' : 'ir';
815 qq|SELECT ct.*, b.description AS business, | .
816 qq| a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
817 qq| '$module' AS module, 'invoice' AS formtype, | .
818 qq| (a.amount = a.paid) AS closed | .
820 qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
821 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
822 qq|WHERE $where AND (a.invoice = '1')|;
827 if ( $form->{l_ordnumber} ) {
828 if ($union eq "UNION") {
829 push(@values, @saved_values);
833 qq|SELECT ct.*, b.description AS business,| .
834 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
835 qq| 'oe' AS module, 'order' AS formtype, o.closed | .
837 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
838 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
839 qq|WHERE $where AND (o.quotation = '0')|;
844 if ( $form->{l_quonumber} ) {
845 if ($union eq "UNION") {
846 push(@values, @saved_values);
850 qq|SELECT ct.*, b.description AS business, | .
851 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
852 qq| 'oe' AS module, 'quotation' AS formtype, o.closed | .
854 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
855 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
856 qq|WHERE $where AND (o.quotation = '1')|;
860 $query .= qq| ORDER BY $sortorder|;
862 $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
864 $main::lxdebug->leave_sub();
868 $main::lxdebug->enter_sub();
870 my ( $self, $myconfig, $form ) = @_;
871 my $dbh = $form->dbconnect($myconfig);
873 qq|SELECT * FROM contacts c | .
874 qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
875 my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
876 my $ref = $sth->fetchrow_hashref("NAME_lc");
878 map { $form->{$_} = $ref->{$_} } keys %$ref;
880 $query = qq|SELECT COUNT(cp_id) AS used FROM (
881 SELECT cp_id FROM oe UNION
882 SELECT cp_id FROM ar UNION
883 SELECT cp_id FROM ap UNION
884 SELECT cp_id FROM delivery_orders
885 ) AS cpid WHERE cp_id = ? OR ? = 0|;
886 ($form->{cp_used}) = selectfirst_array_query($form, $dbh, $query, ($form->{cp_id})x2);
891 $main::lxdebug->leave_sub();
895 $main::lxdebug->enter_sub();
897 my ( $self, $myconfig, $form ) = @_;
898 my $dbh = $form->dbconnect($myconfig);
899 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
900 my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
902 my $ref = $sth->fetchrow_hashref("NAME_lc");
904 map { $form->{$_} = $ref->{$_} } keys %$ref;
906 $query = qq|SELECT COUNT(shipto_id) AS used FROM (
907 SELECT shipto_id FROM oe UNION
908 SELECT shipto_id FROM ar UNION
909 SELECT shipto_id FROM delivery_orders
910 ) AS stid WHERE shipto_id = ? OR ? = 0|;
911 ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
916 $main::lxdebug->leave_sub();
920 $main::lxdebug->enter_sub();
922 my ( $self, $myconfig, $form ) = @_;
923 my $dbh = $form->dbconnect($myconfig);
925 my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
926 my $db = $form->{db} eq "customer" ? "customer" : "vendor";
927 my $qty_sign = $form->{db} eq 'vendor' ? ' * -1 AS qty' : '';
929 my $where = " WHERE 1=1 ";
932 if ($form->{shipto_id} && ($arap eq "ar")) {
933 $where .= "AND ${arap}.shipto_id = ?";
934 push(@values, $form->{shipto_id});
936 $where .= "AND ${arap}.${db}_id = ?";
937 push(@values, $form->{id});
941 $where .= "AND ${arap}.transdate >= ?";
942 push(@values, conv_date($form->{from}));
945 $where .= "AND ${arap}.transdate <= ?";
946 push(@values, conv_date($form->{to}));
949 qq|SELECT s.shiptoname, i.qty $qty_sign, | .
950 qq| ${arap}.id, ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
951 qq| i.description, i.unit, i.sellprice, | .
952 qq| oe.id AS oe_id, invoice | .
954 qq|LEFT JOIN shipto s ON | .
956 ? qq|(ar.shipto_id = s.shipto_id) |
957 : qq|(ap.id = s.trans_id) |) .
958 qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
959 qq|LEFT join parts p ON (p.id = i.parts_id) | .
960 qq|LEFT JOIN oe ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '') | .
962 qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
964 $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
968 $main::lxdebug->leave_sub();
972 $main::lxdebug->enter_sub();
977 my $form = $main::form;
979 Common::check_params(\%params, 'dbh');
981 if (!$form->{NOTE_subject}) {
982 $main::lxdebug->leave_sub();
986 my $dbh = $params{dbh};
990 'id' => $form->{NOTE_id},
991 'subject' => $form->{NOTE_subject},
992 'body' => $form->{NOTE_body},
993 'trans_id' => $form->{id},
994 'trans_module' => 'ct',
997 $note{id} = Notes->save(%note);
999 if ($form->{FU_date}) {
1001 'id' => $form->{FU_id},
1002 'note_id' => $note{id},
1003 'follow_up_date' => $form->{FU_date},
1004 'created_for_user' => $form->{FU_created_for_user},
1005 'done' => $form->{FU_done} ? 1 : 0,
1006 'subject' => $form->{NOTE_subject},
1007 'body' => $form->{NOTE_body},
1010 'trans_id' => $form->{id},
1011 'trans_type' => $form->{db} eq 'customer' ? 'customer' : 'vendor',
1012 'trans_info' => $form->{name},
1017 $follow_up{id} = FU->save(%follow_up);
1019 } elsif ($form->{FU_id}) {
1020 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
1021 do_query($form, $dbh, qq|DELETE FROM follow_ups WHERE id = ?|, conv_i($form->{FU_id}));
1024 delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
1026 $main::lxdebug->leave_sub();
1029 sub _delete_selected_notes {
1030 $main::lxdebug->enter_sub();
1035 Common::check_params(\%params, 'dbh');
1037 my $form = $main::form;
1038 my $dbh = $params{dbh};
1040 foreach my $i (1 .. $form->{NOTES_rowcount}) {
1041 next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
1043 Notes->delete('dbh' => $params{dbh},
1044 'id' => $form->{"NOTE_id_$i"});
1047 $main::lxdebug->leave_sub();
1051 $main::lxdebug->enter_sub();
1054 my $shipto_id = shift;
1056 my $form = $main::form;
1057 my %myconfig = %main::myconfig;
1058 my $dbh = $form->get_standard_dbh(\%myconfig);
1060 do_query($form, $dbh, qq|UPDATE shipto SET trans_id = NULL WHERE shipto_id = ?|, $shipto_id);
1064 $main::lxdebug->leave_sub();
1067 sub delete_contact {
1068 $main::lxdebug->enter_sub();
1073 my $form = $main::form;
1074 my %myconfig = %main::myconfig;
1075 my $dbh = $form->get_standard_dbh(\%myconfig);
1077 do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $cp_id);
1081 $main::lxdebug->leave_sub();
1085 $main::lxdebug->enter_sub();
1090 Common::check_params(\%params, qw(vc id));
1092 my $myconfig = \%main::myconfig;
1093 my $form = $main::form;
1095 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1097 my $table = $params{vc} eq 'customer' ? 'customer' : 'vendor';
1098 my @ids = ref $params{id} eq 'ARRAY' ? @{ $params{id} } : ($params{id});
1099 my $placeholders = join ", ", ('?') x scalar @ids;
1100 my $query = qq|SELECT id, name, account_number, bank, bank_code, iban, bic
1102 WHERE id IN (${placeholders})|;
1104 my $result = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
1106 if (ref $params{id} eq 'ARRAY') {
1107 $result = { map { $_->{id} => $_ } @{ $result } };
1109 $result = $result->[0] || { 'id' => $params{id} };
1112 $main::lxdebug->leave_sub();
1117 sub parse_excel_file {
1118 $main::lxdebug->enter_sub();
1120 my ($self, $myconfig, $form) = @_;
1121 my $locale = $main::locale;
1123 $form->{formname} = 'sales_quotation';
1124 $form->{type} = 'sales_quotation';
1125 $form->{format} = 'excel';
1126 $form->{media} = 'screen';
1127 $form->{quonumber} = 1;
1130 # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1131 $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1135 $form->{"${inv}date"} = $form->{transdate};
1136 $form->{label} = $locale->text('Quotation');
1137 my $numberfld = "sqnumber";
1141 $form->{what_done} = $form->{formname};
1143 map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
1145 my $output_dateformat = $myconfig->{"dateformat"};
1146 my $output_numberformat = $myconfig->{"numberformat"};
1147 my $output_longdates = 1;
1149 # map login user variables
1150 map { $form->{"login_$_"} = $myconfig->{$_} } ("name", "email", "fax", "tel", "company");
1153 for my $field (qw(transdate_oe deliverydate_oe)) {
1155 $form->{$field}[$_] = $locale->date($myconfig, $form->{$field}[$_], 1);
1156 } 0 .. $#{ $form->{$field} };
1159 if ($form->{shipto_id}) {
1160 $form->get_shipto($myconfig);
1163 $form->{notes} =~ s/^\s+//g;
1165 $form->{templates} = $myconfig->{templates};
1167 delete $form->{printer_command};
1169 $form->get_employee_info($myconfig);
1171 my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
1173 if (scalar @{ $cvar_date_fields }) {
1174 format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
1177 while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
1178 reformat_numbers($output_numberformat, $precision, @{ $field_list });
1182 my $extension = 'xls';
1184 $form->{IN} = "$form->{formname}.${extension}";
1186 delete $form->{OUT};
1188 $form->parse_template($myconfig);
1190 $main::lxdebug->leave_sub();