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 #======================================================================
51 $main::lxdebug->enter_sub();
53 my ( $self, $myconfig, $form ) = @_;
55 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
57 my $dbh = $form->dbconnect($myconfig);
59 qq|SELECT ct.*, b.id AS business, cp.* | .
61 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
62 qq|LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id) | .
63 qq|WHERE (ct.id = ?) | .
64 qq|ORDER BY cp.cp_id LIMIT 1|;
65 my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
67 my $ref = $sth->fetchrow_hashref("NAME_lc");
69 map { $form->{$_} = $ref->{$_} } keys %$ref;
72 if ( $form->{salesman_id} ) {
74 qq|SELECT ct.name AS salesman | .
78 selectrow_query($form, $dbh, $query, $form->{salesman_id});
81 my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
83 qq|SELECT n.*, n.itime::DATE AS created_on,
84 e.name AS created_by_name, e.login AS created_by_login
86 LEFT JOIN employee e ON (n.created_by = e.id)
87 WHERE (n.trans_id = ?) AND (n.trans_module = 'ct')|;
88 $form->{NOTES} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
91 qq|SELECT fu.follow_up_date, fu.done AS follow_up_done, e.name AS created_for_name, e.name AS created_for_login
93 LEFT JOIN employee e ON (fu.created_for_user = e.id)
94 WHERE (fu.note_id = ?)
95 AND NOT COALESCE(fu.done, FALSE)
96 AND ( (fu.created_by = ?)
97 OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?)))|;
98 $sth = prepare_query($form, $dbh, $query);
100 foreach my $note (@{ $form->{NOTES} }) {
101 do_statement($form, $sth, $query, conv_i($note->{id}), conv_i($note->{created_by}), conv_i($employee_id));
102 $ref = $sth->fetchrow_hashref();
104 map { $note->{$_} = $ref->{$_} } keys %{ $ref } if ($ref);
109 if ($form->{edit_note_id}) {
111 qq|SELECT n.id AS NOTE_id, n.subject AS NOTE_subject, n.body AS NOTE_body,
112 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
114 LEFT JOIN follow_ups fu ON ((n.id = fu.note_id) AND NOT COALESCE(fu.done, FALSE))
116 $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{edit_note_id}));
119 foreach my $key (keys %{ $ref }) {
121 $new_key =~ s/^([^_]+)/\U$1\E/;
122 $form->{$new_key} = $ref->{$key};
127 # check if it is orphaned
128 my $arap = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
131 if ($form->{db} eq 'vendor') {
132 $makemodel = qq| UNION SELECT 1 FROM makemodel mm WHERE mm.make = ?|;
139 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
140 qq|WHERE ct.id = ? | .
144 qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
147 my ($dummy) = selectrow_query($form, $dbh, $query, (conv_i($form->{id})) x $num_args);
149 $form->{status} = "orphaned" unless ($dummy);
153 $main::lxdebug->leave_sub();
156 sub populate_drop_down_boxes {
157 $main::lxdebug->enter_sub();
159 my ($self, $myconfig, $form, $provided_dbh) = @_;
162 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
165 $query = qq|SELECT id, description FROM business ORDER BY id|;
166 $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
170 qq|SELECT shipto_id, shiptoname, shiptodepartment_1, shiptostreet, shiptocity
172 WHERE (trans_id = ?) AND (module = 'CT')|;
173 $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
176 $query = qq|SELECT cp_id, cp_name, cp_givenname FROM contacts WHERE cp_cv_id = ? ORDER BY cp_name|;
177 $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
180 $query = qq|SELECT id, description FROM language ORDER BY id|;
181 $form->{languages} = selectall_hashref_query($form, $dbh, $query);
184 $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
185 $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
187 $dbh->disconnect() unless ($provided_dbh);
189 $main::lxdebug->leave_sub();
192 sub query_titles_and_greetings {
193 $main::lxdebug->enter_sub();
195 my ( $self, $myconfig, $form ) = @_;
196 my ( %tmp, $ref, $query );
198 my $dbh = $form->dbconnect($myconfig);
201 qq|SELECT DISTINCT(greeting) | .
203 qq|WHERE greeting ~ '[a-zA-Z]' | .
205 qq|SELECT DISTINCT(greeting) | .
207 qq|WHERE greeting ~ '[a-zA-Z]' | .
208 qq|ORDER BY greeting|;
210 map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
211 $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
214 qq|SELECT DISTINCT(cp_title) | .
216 qq|WHERE cp_title ~ '[a-zA-Z]'|;
217 $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
220 qq|SELECT DISTINCT(cp_abteilung) | .
222 qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
223 $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
226 $main::lxdebug->leave_sub();
230 $main::lxdebug->enter_sub();
232 my ( $self, $myconfig, $form ) = @_;
234 # set pricegroup to default
235 $form->{klass} = 0 unless ($form->{klass});
237 # connect to database
238 my $dbh = $form->dbconnect_noauto($myconfig);
241 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
242 if ( $form->{"selected_cp_${_}"} );
243 } qw(title greeting abteilung) );
244 $form->{"greeting"} = $form->{"selected_company_greeting"}
245 if ( $form->{"selected_company_greeting"} );
247 # assign value discount, terms, creditlimit
248 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
249 $form->{discount} /= 100;
250 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
252 my ( $query, $sth, $f_id );
255 $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
256 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
258 if (($f_id ne $form->{id}) && ($f_id ne "")) {
259 $main::lxdebug->leave_sub();
264 if (!$form->{customernumber} && $form->{business}) {
265 $form->{customernumber} =
266 $form->update_business($myconfig, $form->{business}, $dbh);
268 if (!$form->{customernumber}) {
269 $form->{customernumber} =
270 $form->update_defaults($myconfig, "customernumber", $dbh);
273 $query = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
274 ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
276 $main::lxdebug->leave_sub();
280 $query = qq|SELECT nextval('id')|;
281 ($form->{id}) = selectrow_query($form, $dbh, $query);
283 $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
284 do_query($form, $dbh, $query, $form->{id});
287 $query = qq|UPDATE customer SET | .
288 qq|customernumber = ?, | .
291 qq|department_1 = ?, | .
292 qq|department_2 = ?, | .
306 qq|creditlimit = ?, | .
308 qq|business_id = ?, | .
309 qq|taxnumber = ?, | .
311 qq|account_number = ?, | .
312 qq|bank_code = ?, | .
317 qq|direct_debit = ?, | .
320 qq|salesman_id = ?, | .
321 qq|language_id = ?, | .
322 qq|payment_id = ?, | .
323 qq|taxzone_id = ?, | .
324 qq|user_password = ?, | .
325 qq|c_vendor_id = ?, | .
329 $form->{customernumber},
332 $form->{department_1},
333 $form->{department_2},
347 $form->{creditlimit},
348 conv_i($form->{terms}),
349 conv_i($form->{business}),
352 $form->{account_number},
357 $form->{obsolete} ? 't' : 'f',
358 $form->{direct_debit} ? 't' : 'f',
361 conv_i($form->{salesman_id}),
362 conv_i($form->{language_id}),
363 conv_i($form->{payment_id}),
364 conv_i($form->{taxzone_id}, 0),
365 $form->{user_password},
366 $form->{c_vendor_id},
367 conv_i($form->{klass}),
370 do_query( $form, $dbh, $query, @values );
373 if ( $form->{cp_id} ) {
374 $query = qq|UPDATE contacts SET | .
376 qq|cp_givenname = ?, | .
379 qq|cp_phone1 = ?, | .
380 qq|cp_phone2 = ?, | .
381 qq|cp_abteilung = ?, | .
383 qq|cp_mobile1 = ?, | .
384 qq|cp_mobile2 = ?, | .
385 qq|cp_satphone = ?, | .
386 qq|cp_satfax = ?, | .
387 qq|cp_project = ?, | .
388 qq|cp_privatphone = ?, | .
389 qq|cp_privatemail = ?, | .
390 qq|cp_birthday = ?, | .
395 $form->{cp_givenname},
400 $form->{cp_abteilung},
404 $form->{cp_satphone},
407 $form->{cp_privatphone},
408 $form->{cp_privatemail},
409 $form->{cp_birthday},
410 $form->{cp_gender} eq 'f' ? 'f' : 'm',
413 } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
415 qq|INSERT INTO contacts ( cp_cv_id, cp_title, cp_givenname, | .
416 qq| cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
417 qq| cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
418 qq| cp_birthday, cp_gender) | .
419 qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
423 $form->{cp_givenname},
428 $form->{cp_abteilung},
432 $form->{cp_satphone},
435 $form->{cp_privatphone},
436 $form->{cp_privatemail},
437 $form->{cp_birthday},
438 $form->{cp_gender} eq 'f' ? 'f' : 'm',
441 do_query( $form, $dbh, $query, @values ) if ($query);
444 $form->add_shipto( $dbh, $form->{id}, "CT" );
446 $self->_save_note('dbh' => $dbh);
447 $self->_delete_selected_notes('dbh' => $dbh);
449 CVar->save_custom_variables('dbh' => $dbh,
451 'trans_id' => $form->{id},
452 'variables' => $form,
453 'always_valid' => 1);
455 my $rc = $dbh->commit();
458 $main::lxdebug->leave_sub();
463 $main::lxdebug->enter_sub();
465 my ( $self, $myconfig, $form ) = @_;
467 $form->{taxzone_id} *= 1;
468 # connect to database
469 my $dbh = $form->dbconnect_noauto($myconfig);
472 $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
473 if ( $form->{"selected_cp_${_}"} );
474 } qw(title greeting abteilung) );
475 $form->{"greeting"} = $form->{"selected_company_greeting"}
476 if ( $form->{"selected_company_greeting"} );
478 $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
479 $form->{discount} /= 100;
480 $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
485 $query = qq|SELECT nextval('id')|;
486 ($form->{id}) = selectrow_query($form, $dbh, $query);
488 $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
489 do_query($form, $dbh, $query, $form->{id});
491 if ( !$form->{vendornumber} ) {
492 $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
497 qq|UPDATE vendor SET | .
498 qq| vendornumber = ?, | .
500 qq| greeting = ?, | .
501 qq| department_1 = ?, | .
502 qq| department_2 = ?, | .
507 qq| homepage = ?, | .
516 qq| discount = ?, | .
517 qq| creditlimit = ?, | .
518 qq| business_id = ?, | .
519 qq| taxnumber = ?, | .
520 qq| language = ?, | .
521 qq| account_number = ?, | .
522 qq| bank_code = ?, | .
526 qq| obsolete = ?, | .
527 qq| direct_debit = ?, | .
529 qq| payment_id = ?, | .
530 qq| taxzone_id = ?, | .
531 qq| language_id = ?, | .
532 qq| username = ?, | .
533 qq| user_password = ?, | .
534 qq| v_customer_id = ? | .
537 $form->{vendornumber},
540 $form->{department_1},
541 $form->{department_2},
554 conv_i($form->{terms}),
556 $form->{creditlimit},
557 conv_i($form->{business}),
560 $form->{account_number},
565 $form->{obsolete} ? 't' : 'f',
566 $form->{direct_debit} ? 't' : 'f',
568 conv_i($form->{payment_id}),
569 conv_i($form->{taxzone_id}, 0),
570 conv_i( $form->{language_id}),
572 $form->{user_password},
573 $form->{v_customer_id},
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();
664 $main::lxdebug->leave_sub();
669 $main::lxdebug->enter_sub();
671 my ( $self, $myconfig, $form ) = @_;
672 # connect to database
673 my $dbh = $form->dbconnect($myconfig);
676 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
677 my $query = qq|DELETE FROM $cv WHERE id = ?|;
678 do_query($form, $dbh, $query, $form->{id});
682 $main::lxdebug->leave_sub();
686 $main::lxdebug->enter_sub();
688 my ( $self, $myconfig, $form ) = @_;
690 # connect to database
691 my $dbh = $form->dbconnect($myconfig);
693 my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
698 my %allowed_sort_columns =
700 id customernumber vendornumber name contact phone fax email street
701 taxnumber business invnumber ordnumber quonumber
703 my $sortorder = $form->{sort} && $allowed_sort_columns{$form->{sort}} ? $form->{sort} : "name";
704 $form->{sort} = $sortorder;
705 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
707 if ($sortorder ne 'id' && 1 >= scalar grep { $form->{$_} } qw(l_ordnumber l_quonumber l_invnumber)) {
708 $sortorder = "lower($sortorder) ${sortdir}";
710 $sortorder .= " ${sortdir}";
713 if ($form->{"${cv}number"}) {
714 $where .= " AND ct.${cv}number ILIKE ?";
715 push(@values, '%' . $form->{"${cv}number"} . '%');
718 foreach my $key (qw(name contact email)) {
720 $where .= " AND ct.$key ILIKE ?";
721 push(@values, '%' . $form->{$key} . '%');
725 if ($form->{cp_name}) {
726 $where .= " AND ct.id IN (SELECT cp_cv_id FROM contacts WHERE lower(cp_name) LIKE lower(?))";
727 push @values, '%' . $form->{cp_name} . '%';
730 if ($form->{addr_city}) {
731 $where .= " AND ((lower(ct.city) LIKE lower(?))
736 WHERE (module = 'CT')
737 AND (lower(shiptocity) LIKE lower(?))
740 push @values, ('%' . $form->{addr_city} . '%') x 2;
743 if ( $form->{status} eq 'orphaned' ) {
745 qq| AND ct.id NOT IN | .
746 qq| (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
747 if ($cv eq 'customer') {
749 qq| AND ct.id NOT IN | .
750 qq| (SELECT a.customer_id FROM ar a, customer cv | .
751 qq| WHERE cv.id = a.customer_id)|;
753 if ($cv eq 'vendor') {
755 qq| AND ct.id NOT IN | .
756 qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
757 qq| WHERE cv.id = a.vendor_id)|;
759 $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
762 if ($form->{obsolete} eq "Y") {
763 $where .= qq| AND obsolete|;
764 } elsif ($form->{obsolete} eq "N") {
765 $where .= qq| AND NOT obsolete|;
768 if ($form->{business_id}) {
769 $where .= qq| AND (business_id = ?)|;
770 push(@values, conv_i($form->{business_id}));
773 my ($cvar_where, @cvar_values) = CVar->build_filter_query('module' => 'CT',
774 'trans_id_field' => 'ct.id',
778 $where .= qq| AND ($cvar_where)|;
779 push @values, @cvar_values;
782 if ($form->{addr_street}) {
783 $where .= qq| AND (street ILIKE ?)|;
784 push @values, '%' . $form->{addr_street} . '%';
787 if ($form->{addr_zipcode}) {
788 $where .= qq| AND (zipcode ILIKE ?)|;
789 push @values, $form->{addr_zipcode} . '%';
793 qq|SELECT ct.*, b.description AS business | .
795 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
798 my @saved_values = @values;
799 # redo for invoices, orders and quotations
800 if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
801 my ($ar, $union, $module);
804 if ($form->{l_invnumber}) {
805 my $ar = $cv eq 'customer' ? 'ar' : 'ap';
806 my $module = $ar eq 'ar' ? 'is' : 'ir';
809 qq|SELECT ct.*, b.description AS business, | .
810 qq| a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
811 qq| '$module' AS module, 'invoice' AS formtype, | .
812 qq| (a.amount = a.paid) AS closed | .
814 qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
815 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
816 qq|WHERE $where AND (a.invoice = '1')|;
821 if ( $form->{l_ordnumber} ) {
822 if ($union eq "UNION") {
823 push(@values, @saved_values);
827 qq|SELECT ct.*, b.description AS business,| .
828 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
829 qq| 'oe' AS module, 'order' AS formtype, o.closed | .
831 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
832 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
833 qq|WHERE $where AND (o.quotation = '0')|;
838 if ( $form->{l_quonumber} ) {
839 if ($union eq "UNION") {
840 push(@values, @saved_values);
844 qq|SELECT ct.*, b.description AS business, | .
845 qq| ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
846 qq| 'oe' AS module, 'quotation' AS formtype, o.closed | .
848 qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
849 qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
850 qq|WHERE $where AND (o.quotation = '1')|;
854 $query .= qq| ORDER BY $sortorder|;
856 $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
858 $main::lxdebug->leave_sub();
862 $main::lxdebug->enter_sub();
864 my ( $self, $myconfig, $form ) = @_;
865 my $dbh = $form->dbconnect($myconfig);
867 qq|SELECT * FROM contacts c | .
868 qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
869 my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
870 my $ref = $sth->fetchrow_hashref("NAME_lc");
872 map { $form->{$_} = $ref->{$_} } keys %$ref;
874 $query = qq|SELECT COUNT(cp_id) AS used FROM (
875 SELECT cp_id FROM oe UNION
876 SELECT cp_id FROM ar UNION
877 SELECT cp_id FROM ap UNION
878 SELECT cp_id FROM delivery_orders
879 ) AS cpid WHERE cp_id = ? OR ? = 0|;
880 ($form->{cp_used}) = selectfirst_array_query($form, $dbh, $query, ($form->{cp_id})x2);
885 $main::lxdebug->leave_sub();
889 $main::lxdebug->enter_sub();
891 my ( $self, $myconfig, $form ) = @_;
892 my $dbh = $form->dbconnect($myconfig);
893 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
894 my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
896 my $ref = $sth->fetchrow_hashref("NAME_lc");
898 map { $form->{$_} = $ref->{$_} } keys %$ref;
900 $query = qq|SELECT COUNT(shipto_id) AS used FROM (
901 SELECT shipto_id FROM oe UNION
902 SELECT shipto_id FROM ar UNION
903 SELECT shipto_id FROM delivery_orders
904 ) AS stid WHERE shipto_id = ? OR ? = 0|;
905 ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
910 $main::lxdebug->leave_sub();
914 $main::lxdebug->enter_sub();
916 my ( $self, $myconfig, $form ) = @_;
917 my $dbh = $form->dbconnect($myconfig);
919 my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
920 my $db = $form->{db} eq "customer" ? "customer" : "vendor";
921 my $qty_sign = $form->{db} eq 'vendor' ? ' * -1 AS qty' : '';
923 my $where = " WHERE 1=1 ";
926 if ($form->{shipto_id} && ($arap eq "ar")) {
927 $where .= "AND ${arap}.shipto_id = ?";
928 push(@values, $form->{shipto_id});
930 $where .= "AND ${arap}.${db}_id = ?";
931 push(@values, $form->{id});
935 $where .= "AND ${arap}.transdate >= ?";
936 push(@values, conv_date($form->{from}));
939 $where .= "AND ${arap}.transdate <= ?";
940 push(@values, conv_date($form->{to}));
943 qq|SELECT s.shiptoname, i.qty $qty_sign, | .
944 qq| ${arap}.id, ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
945 qq| i.description, i.unit, i.sellprice, | .
946 qq| oe.id AS oe_id, invoice | .
948 qq|LEFT JOIN shipto s ON | .
950 ? qq|(ar.shipto_id = s.shipto_id) |
951 : qq|(ap.id = s.trans_id) |) .
952 qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
953 qq|LEFT join parts p ON (p.id = i.parts_id) | .
954 qq|LEFT JOIN oe ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '') | .
956 qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
958 $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
962 $main::lxdebug->leave_sub();
966 $main::lxdebug->enter_sub();
971 my $form = $main::form;
973 Common::check_params(\%params, 'dbh');
975 if (!$form->{NOTE_subject}) {
976 $main::lxdebug->leave_sub();
980 my $dbh = $params{dbh};
984 'id' => $form->{NOTE_id},
985 'subject' => $form->{NOTE_subject},
986 'body' => $form->{NOTE_body},
987 'trans_id' => $form->{id},
988 'trans_module' => 'ct',
991 $note{id} = Notes->save(%note);
993 if ($form->{FU_date}) {
995 'id' => $form->{FU_id},
996 'note_id' => $note{id},
997 'follow_up_date' => $form->{FU_date},
998 'created_for_user' => $form->{FU_created_for_user},
999 'done' => $form->{FU_done} ? 1 : 0,
1000 'subject' => $form->{NOTE_subject},
1001 'body' => $form->{NOTE_body},
1004 'trans_id' => $form->{id},
1005 'trans_type' => $form->{db} eq 'customer' ? 'customer' : 'vendor',
1006 'trans_info' => $form->{name},
1011 $follow_up{id} = FU->save(%follow_up);
1013 } elsif ($form->{FU_id}) {
1014 do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
1015 do_query($form, $dbh, qq|DELETE FROM follow_ups WHERE id = ?|, conv_i($form->{FU_id}));
1018 delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
1020 $main::lxdebug->leave_sub();
1023 sub _delete_selected_notes {
1024 $main::lxdebug->enter_sub();
1029 Common::check_params(\%params, 'dbh');
1031 my $form = $main::form;
1032 my $dbh = $params{dbh};
1034 foreach my $i (1 .. $form->{NOTES_rowcount}) {
1035 next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
1037 Notes->delete('dbh' => $params{dbh},
1038 'id' => $form->{"NOTE_id_$i"});
1041 $main::lxdebug->leave_sub();
1045 $main::lxdebug->enter_sub();
1048 my $shipto_id = shift;
1050 my $form = $main::form;
1051 my %myconfig = %main::myconfig;
1052 my $dbh = $form->get_standard_dbh(\%myconfig);
1054 do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $shipto_id);
1058 $main::lxdebug->leave_sub();
1062 $main::lxdebug->enter_sub();
1067 Common::check_params(\%params, qw(vc id));
1069 my $myconfig = \%main::myconfig;
1070 my $form = $main::form;
1072 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1074 my $table = $params{vc} eq 'customer' ? 'customer' : 'vendor';
1075 my @ids = ref $params{id} eq 'ARRAY' ? @{ $params{id} } : ($params{id});
1076 my $placeholders = join ", ", ('?') x scalar @ids;
1077 my $query = qq|SELECT id, name, account_number, bank, bank_code, iban, bic
1079 WHERE id IN (${placeholders})|;
1081 my $result = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
1083 if (ref $params{id} eq 'ARRAY') {
1084 $result = { map { $_->{id} => $_ } @{ $result } };
1086 $result = $result->[0] || { 'id' => $params{id} };
1089 $main::lxdebug->leave_sub();
1094 sub parse_excel_file {
1095 $main::lxdebug->enter_sub();
1097 my ($self, $myconfig, $form) = @_;
1098 my $locale = $main::locale;
1100 $form->{formname} = 'sales_quotation';
1101 $form->{type} = 'sales_quotation';
1102 $form->{format} = 'excel';
1103 $form->{media} = 'screen';
1104 $form->{quonumber} = 1;
1107 # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
1108 $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
1112 $form->{"${inv}date"} = $form->{transdate};
1113 $form->{label} = $locale->text('Quotation');
1114 my $numberfld = "sqnumber";
1118 $form->{what_done} = $form->{formname};
1120 map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
1122 my $output_dateformat = $myconfig->{"dateformat"};
1123 my $output_numberformat = $myconfig->{"numberformat"};
1124 my $output_longdates = 1;
1126 # map login user variables
1127 map { $form->{"login_$_"} = $myconfig->{$_} } ("name", "email", "fax", "tel", "company");
1130 for my $field (qw(transdate_oe deliverydate_oe)) {
1132 $form->{$field}[$_] = $locale->date($myconfig, $form->{$field}[$_], 1);
1133 } 0 .. $#{ $form->{$field} };
1136 if ($form->{shipto_id}) {
1137 $form->get_shipto($myconfig);
1140 $form->{notes} =~ s/^\s+//g;
1142 $form->{templates} = $myconfig->{templates};
1144 delete $form->{printer_command};
1146 $form->get_employee_info($myconfig);
1148 my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
1150 if (scalar @{ $cvar_date_fields }) {
1151 format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
1154 while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
1155 reformat_numbers($output_numberformat, $precision, @{ $field_list });
1159 my $extension = 'xls';
1161 $form->{IN} = "$form->{formname}.${extension}";
1163 delete $form->{OUT};
1165 $form->parse_template($myconfig, $main::userspath);
1167 $main::lxdebug->leave_sub();