]> wagnertech.de Git - kivitendo-erp.git/blob - SL/CT.pm
Stammdaten -> Berichte -> Kunden -> Suchfeld | Das zusammengefasst Adressfeld (Straße...
[kivitendo-erp.git] / SL / CT.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
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.
21 #
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 #======================================================================
30 #
31 # backend code for customers and vendors
32 #
33 # CHANGE LOG:
34 #   DS. 2000-07-04  Created
35 #
36 #======================================================================
37
38 package CT;
39
40 use Data::Dumper;
41
42 use SL::Common;
43 use SL::CVar;
44 use SL::DBUtils;
45 use SL::FU;
46 use SL::Notes;
47
48 sub get_tuple {
49   $main::lxdebug->enter_sub();
50
51   my ( $self, $myconfig, $form ) = @_;
52
53   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
54
55   my $dbh   = $form->dbconnect($myconfig);
56   my $query =
57     qq|SELECT ct.*, b.id AS business, cp.* | .
58     qq|FROM $cv ct | .
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});
64
65   my $ref = $sth->fetchrow_hashref(NAME_lc);
66
67   map { $form->{$_} = $ref->{$_} } keys %$ref;
68
69   $sth->finish;
70   if ( $form->{salesman_id} ) {
71     my $query =
72       qq|SELECT ct.name AS salesman | .
73       qq|FROM $cv ct | .
74       qq|WHERE ct.id = ?|;
75     ($form->{salesman}) =
76       selectrow_query($form, $dbh, $query, $form->{salesman_id});
77   }
78
79   my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
80   $query =
81     qq|SELECT n.*, n.itime::DATE AS created_on,
82          e.name AS created_by_name, e.login AS created_by_login
83        FROM notes n
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}));
87
88   $query =
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
90        FROM follow_ups fu
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);
97
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();
101
102     map { $note->{$_} = $ref->{$_} } keys %{ $ref } if ($ref);
103   }
104
105   $sth->finish();
106
107   if ($form->{edit_note_id}) {
108     $query =
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
111          FROM notes n
112          LEFT JOIN follow_ups fu ON ((n.id = fu.note_id) AND NOT COALESCE(fu.done, FALSE))
113          WHERE n.id = ?|;
114     $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{edit_note_id}));
115
116     if ($ref) {
117       foreach my $key (keys %{ $ref }) {
118         my $new_key       =  $key;
119         $new_key          =~ s/^([^_]+)/\U\1\E/;
120         $form->{$new_key} =  $ref->{$key};
121       }
122     }
123   }
124
125   # check if it is orphaned
126   my $arap = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
127   $query =
128     qq|SELECT a.id | .
129     qq|FROM $arap a | .
130     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
131     qq|WHERE ct.id = ? | .
132     qq|UNION | .
133     qq|SELECT a.id | .
134     qq|FROM oe a | .
135     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
136     qq|WHERE ct.id = ?|;
137   my ($dummy) = selectrow_query($form, $dbh, $query, $form->{id}, $form->{id});
138   $form->{status} = "orphaned" unless ($dummy);
139
140   $dbh->disconnect;
141
142   $main::lxdebug->leave_sub();
143 }
144
145 sub populate_drop_down_boxes {
146   $main::lxdebug->enter_sub();
147
148   my ($self, $myconfig, $form, $provided_dbh) = @_;
149
150   my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
151
152   # get business types
153   $query = qq|SELECT id, description FROM business ORDER BY id|;
154   $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
155
156   # get shipto address
157   $query =
158     qq|SELECT shipto_id, shiptoname, shiptodepartment_1, shiptostreet, shiptocity
159        FROM shipto
160        WHERE (trans_id = ?) AND (module = 'CT')|;
161   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
162
163   # get contacts
164   $query  = qq|SELECT cp_id, cp_name, cp_givenname FROM contacts WHERE cp_cv_id = ? ORDER BY cp_name|;
165   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
166
167   # get languages
168   $query = qq|SELECT id, description FROM language ORDER BY id|;
169   $form->{languages} = selectall_hashref_query($form, $dbh, $query);
170
171   # get payment terms
172   $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
173   $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
174
175   $dbh->disconnect() unless ($provided_dbh);
176
177   $main::lxdebug->leave_sub();
178 }
179
180 sub query_titles_and_greetings {
181   $main::lxdebug->enter_sub();
182
183   my ( $self, $myconfig, $form ) = @_;
184   my ( %tmp,  $ref );
185
186   my $dbh = $form->dbconnect($myconfig);
187
188   $query =
189     qq|SELECT DISTINCT(cp_greeting) | .
190     qq|FROM contacts | .
191     qq|WHERE cp_greeting ~ '[a-zA-Z]' | .
192     qq|ORDER BY cp_greeting|;
193   $form->{GREETINGS} = [ selectall_array_query($form, $dbh, $query) ];
194
195   $query =
196     qq|SELECT DISTINCT(greeting) | .
197     qq|FROM customer | .
198     qq|WHERE greeting ~ '[a-zA-Z]' | .
199     qq|UNION | .
200     qq|SELECT DISTINCT(greeting) | .
201     qq|FROM vendor | .
202     qq|WHERE greeting ~ '[a-zA-Z]' | .
203     qq|ORDER BY greeting|;
204   my %tmp;
205   map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
206   $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
207
208   $query =
209     qq|SELECT DISTINCT(cp_title) | .
210     qq|FROM contacts | .
211     qq|WHERE cp_title ~ '[a-zA-Z]'|;
212   $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
213
214   $query =
215     qq|SELECT DISTINCT(cp_abteilung) | .
216     qq|FROM contacts | .
217     qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
218   $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
219
220   $dbh->disconnect();
221   $main::lxdebug->leave_sub();
222 }
223
224 sub save_customer {
225   $main::lxdebug->enter_sub();
226
227   my ( $self, $myconfig, $form ) = @_;
228
229   # set pricegroup to default
230   $form->{klass} = 0 unless ($form->{klass});
231
232   # connect to database
233   my $dbh = $form->dbconnect_noauto($myconfig);
234
235   map( {
236     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
237     if ( $form->{"selected_cp_${_}"} );
238        } qw(title greeting abteilung) );
239   $form->{"greeting"} = $form->{"selected_company_greeting"}
240   if ( $form->{"selected_company_greeting"} );
241
242   # assign value discount, terms, creditlimit
243   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
244   $form->{discount} /= 100;
245   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
246
247   my ( $query, $sth, $f_id );
248
249   if ( $form->{id} ) {
250     $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
251     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
252
253     if (($f_id ne $form->{id}) && ($f_id ne "")) {
254       $main::lxdebug->leave_sub();
255       return 3;
256     }
257
258   } else {
259     if (!$form->{customernumber} && $form->{business}) {
260       $form->{customernumber} =
261         $form->update_business($myconfig, $form->{business}, $dbh);
262     }
263     if (!$form->{customernumber}) {
264       $form->{customernumber} =
265         $form->update_defaults($myconfig, "customernumber", $dbh);
266     }
267
268     $query  = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
269     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
270     if ($f_id ne "") {
271       $main::lxdebug->leave_sub();
272       return 3;
273     }
274
275     $query = qq|SELECT nextval('id')|;
276     ($form->{id}) = selectrow_query($form, $dbh, $query);
277
278     $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
279     do_query($form, $dbh, $query, $form->{id});
280   }
281
282   $query = qq|UPDATE customer SET | .
283     qq|customernumber = ?, | .
284     qq|name = ?, | .
285     qq|greeting = ?, | .
286     qq|department_1 = ?, | .
287     qq|department_2 = ?, | .
288     qq|street = ?, | .
289     qq|zipcode = ?, | .
290     qq|city = ?, | .
291     qq|country = ?, | .
292     qq|homepage = ?, | .
293     qq|contact = ?, | .
294     qq|phone = ?, | .
295     qq|fax = ?, | .
296     qq|email = ?, | .
297     qq|cc = ?, | .
298     qq|bcc = ?, | .
299     qq|notes = ?, | .
300     qq|discount = ?, | .
301     qq|creditlimit = ?, | .
302     qq|terms = ?, | .
303     qq|business_id = ?, | .
304     qq|taxnumber = ?, | .
305     qq|language = ?, | .
306     qq|account_number = ?, | .
307     qq|bank_code = ?, | .
308     qq|bank = ?, | .
309     qq|obsolete = ?, | .
310     qq|direct_debit = ?, | .
311     qq|ustid = ?, | .
312     qq|username = ?, | .
313     qq|salesman_id = ?, | .
314     qq|language_id = ?, | .
315     qq|payment_id = ?, | .
316     qq|taxzone_id = ?, | .
317     qq|user_password = ?, | .
318     qq|c_vendor_id = ?, | .
319     qq|klass = ?, | .
320     qq|v_customer_id = ? | .
321     qq|WHERE id = ?|;
322   my @values = (
323     $form->{customernumber},
324     $form->{name},
325     $form->{greeting},
326     $form->{department_1},
327     $form->{department_2},
328     $form->{street},
329     $form->{zipcode},
330     $form->{city},
331     $form->{country},
332     $form->{homepage},
333     $form->{contact},
334     $form->{phone},
335     $form->{fax},
336     $form->{email},
337     $form->{cc},
338     $form->{bcc},
339     $form->{notes},
340     $form->{discount},
341     $form->{creditlimit},
342     conv_i($form->{terms}),
343     conv_i($form->{business}),
344     $form->{taxnumber},
345     $form->{language},
346     $form->{account_number},
347     $form->{bank_code},
348     $form->{bank},
349     $form->{obsolete} ? 't' : 'f',
350     $form->{direct_debit} ? 't' : 'f',
351     $form->{ustid},
352     $form->{username},
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}),
360     $form->{v_customer_id},
361     $form->{id}
362     );
363   do_query( $form, $dbh, $query, @values );
364
365   $query = undef;
366   if ( $form->{cp_id} ) {
367     $query = qq|UPDATE contacts SET | .
368       qq|cp_greeting = ?, | .
369       qq|cp_title = ?,  | .
370       qq|cp_givenname = ?, | .
371       qq|cp_name = ?, | .
372       qq|cp_email = ?, | .
373       qq|cp_phone1 = ?, | .
374       qq|cp_phone2 = ?, | .
375       qq|cp_abteilung = ?, | .
376       qq|cp_fax = ?, | .
377       qq|cp_mobile1 = ?, | .
378       qq|cp_mobile2 = ?, | .
379       qq|cp_satphone = ?, | .
380       qq|cp_satfax = ?, | .
381       qq|cp_project = ?, | .
382       qq|cp_privatphone = ?, | .
383       qq|cp_privatemail = ?, | .
384       qq|cp_birthday = ? | .
385       qq|WHERE cp_id = ?|;
386     @values = (
387       $form->{cp_greeting},
388       $form->{cp_title},
389       $form->{cp_givenname},
390       $form->{cp_name},
391       $form->{cp_email},
392       $form->{cp_phone1},
393       $form->{cp_phone2},
394       $form->{cp_abteilung},
395       $form->{cp_fax},
396       $form->{cp_mobile1},
397       $form->{cp_mobile2},
398       $form->{cp_satphone},
399       $form->{cp_satfax},
400       $form->{cp_project},
401       $form->{cp_privatphone},
402       $form->{cp_privatemail},
403       $form->{cp_birthday},
404       $form->{cp_id}
405       );
406   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
407     $query =
408       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname,  | .
409       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
410       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
411       qq|  cp_birthday) | .
412       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
413     @values = (
414       $form->{id},
415       $form->{cp_greeting},
416       $form->{cp_title},
417       $form->{cp_givenname},
418       $form->{cp_name},
419       $form->{cp_email},
420       $form->{cp_phone1},
421       $form->{cp_phone2},
422       $form->{cp_abteilung},
423       $form->{cp_fax},
424       $form->{cp_mobile1},
425       $form->{cp_mobile2},
426       $form->{cp_satphone},
427       $form->{cp_satfax},
428       $form->{cp_project},
429       $form->{cp_privatphone},
430       $form->{cp_privatemail},
431       $form->{cp_birthday}
432       );
433   }
434   do_query( $form, $dbh, $query, @values ) if ($query);
435
436   # add shipto
437   $form->add_shipto( $dbh, $form->{id}, "CT" );
438
439   $self->_save_note('dbh' => $dbh);
440   $self->_delete_selected_notes('dbh' => $dbh);
441
442   CVar->save_custom_variables('dbh'       => $dbh,
443                               'module'    => 'CT',
444                               'trans_id'  => $form->{id},
445                               'variables' => $form);
446
447   $rc = $dbh->commit();
448   $dbh->disconnect();
449
450   $main::lxdebug->leave_sub();
451   return $rc;
452 }
453
454 sub save_vendor {
455   $main::lxdebug->enter_sub();
456
457   my ( $self, $myconfig, $form ) = @_;
458
459   $form->{taxzone_id} *= 1;
460   # connect to database
461   my $dbh = $form->dbconnect_noauto($myconfig);
462
463   map( {
464     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
465     if ( $form->{"selected_cp_${_}"} );
466        } qw(title greeting abteilung) );
467   $form->{"greeting"} = $form->{"selected_company_greeting"}
468   if ( $form->{"selected_company_greeting"} );
469
470   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
471   $form->{discount} /= 100;
472   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
473
474   my $query;
475
476   if (!$form->{id}) {
477     $query = qq|SELECT nextval('id')|;
478     ($form->{id}) = selectrow_query($form, $dbh, $query);
479
480     $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
481     do_query($form, $dbh, $query, $form->{id});
482
483     if ( !$form->{vendornumber} ) {
484       $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
485     }
486   }
487
488   $query =
489     qq|UPDATE vendor SET | .
490     qq|  vendornumber = ?, | .
491     qq|  name = ?, | .
492     qq|  greeting = ?, | .
493     qq|  department_1 = ?, | .
494     qq|  department_2 = ?, | .
495     qq|  street = ?, | .
496     qq|  zipcode = ?, | .
497     qq|  city = ?, | .
498     qq|  country = ?, | .
499     qq|  homepage = ?, | .
500     qq|  contact = ?, | .
501     qq|  phone = ?, | .
502     qq|  fax = ?, | .
503     qq|  email = ?, | .
504     qq|  cc = ?, | .
505     qq|  bcc = ?, | .
506     qq|  notes = ?, | .
507     qq|  terms = ?, | .
508     qq|  discount = ?, | .
509     qq|  creditlimit = ?, | .
510     qq|  business_id = ?, | .
511     qq|  taxnumber = ?, | .
512     qq|  language = ?, | .
513     qq|  account_number = ?, | .
514     qq|  bank_code = ?, | .
515     qq|  bank = ?, | .
516     qq|  obsolete = ?, | .
517     qq|  direct_debit = ?, | .
518     qq|  ustid = ?, | .
519     qq|  payment_id = ?, | .
520     qq|  taxzone_id = ?, | .
521     qq|  language_id = ?, | .
522     qq|  username = ?, | .
523     qq|  user_password = ?, | .
524     qq|  v_customer_id = ? | .
525     qq|WHERE id = ?|;
526   @values = (
527     $form->{vendornumber},
528     $form->{name},
529     $form->{greeting},
530     $form->{department_1},
531     $form->{department_2},
532     $form->{street},
533     $form->{zipcode},
534     $form->{city},
535     $form->{country},
536     $form->{homepage},
537     $form->{contact},
538     $form->{phone},
539     $form->{fax},
540     $form->{email},
541     $form->{cc},
542     $form->{bcc},
543     $form->{notes},
544     conv_i($form->{terms}),
545     $form->{discount},
546     $form->{creditlimit},
547     conv_i($form->{business}),
548     $form->{taxnumber},
549     $form->{language},
550     $form->{account_number},
551     $form->{bank_code},
552     $form->{bank},
553     $form->{obsolete} ? 't' : 'f',
554     $form->{direct_debit} ? 't' : 'f',
555     $form->{ustid},
556     conv_i($form->{payment_id}),
557     conv_i($form->{taxzone_id}, 0),
558     conv_i( $form->{language_id}),
559     $form->{username},
560     $form->{user_password},
561     $form->{v_customer_id},
562     $form->{id}
563     );
564   do_query($form, $dbh, $query, @values);
565
566   $query = undef;
567   if ( $form->{cp_id} ) {
568     $query = qq|UPDATE contacts SET | .
569       qq|cp_greeting = ?, | .
570       qq|cp_title = ?,  | .
571       qq|cp_givenname = ?, | .
572       qq|cp_name = ?, | .
573       qq|cp_email = ?, | .
574       qq|cp_phone1 = ?, | .
575       qq|cp_phone2 = ?, | .
576       qq|cp_abteilung = ?, | .
577       qq|cp_fax = ?, | .
578       qq|cp_mobile1 = ?, | .
579       qq|cp_mobile2 = ?, | .
580       qq|cp_satphone = ?, | .
581       qq|cp_satfax = ?, | .
582       qq|cp_project = ?, | .
583       qq|cp_privatphone = ?, | .
584       qq|cp_privatemail = ?, | .
585       qq|cp_birthday = ? | .
586       qq|WHERE cp_id = ?|;
587     @values = (
588       $form->{cp_greeting},
589       $form->{cp_title},
590       $form->{cp_givenname},
591       $form->{cp_name},
592       $form->{cp_email},
593       $form->{cp_phone1},
594       $form->{cp_phone2},
595       $form->{cp_abteilung},
596       $form->{cp_fax},
597       $form->{cp_mobile1},
598       $form->{cp_mobile2},
599       $form->{cp_satphone},
600       $form->{cp_satfax},
601       $form->{cp_project},
602       $form->{cp_privatphone},
603       $form->{cp_privatemail},
604       $form->{cp_birthday},
605       $form->{cp_id}
606       );
607   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
608     $query =
609       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname,  | .
610       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
611       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
612       qq|  cp_birthday) | .
613       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
614     @values = (
615       $form->{id},
616       $form->{cp_greeting},
617       $form->{cp_title},
618       $form->{cp_givenname},
619       $form->{cp_name},
620       $form->{cp_email},
621       $form->{cp_phone1},
622       $form->{cp_phone2},
623       $form->{cp_abteilung},
624       $form->{cp_fax},
625       $form->{cp_mobile1},
626       $form->{cp_mobile2},
627       $form->{cp_satphone},
628       $form->{cp_satfax},
629       $form->{cp_project},
630       $form->{cp_privatphone},
631       $form->{cp_privatemail},
632       $form->{cp_birthday}
633       );
634   }
635   do_query($form, $dbh, $query, @values) if ($query);
636
637   # add shipto
638   $form->add_shipto( $dbh, $form->{id}, "CT" );
639
640   $self->_save_note('dbh' => $dbh);
641   $self->_delete_selected_notes('dbh' => $dbh);
642
643   CVar->save_custom_variables('dbh'       => $dbh,
644                               'module'    => 'CT',
645                               'trans_id'  => $form->{id},
646                               'variables' => $form);
647
648   $rc = $dbh->commit();
649   $dbh->disconnect();
650
651   $main::lxdebug->leave_sub();
652   return $rc;
653 }
654
655 sub delete {
656   $main::lxdebug->enter_sub();
657
658   my ( $self, $myconfig, $form ) = @_;
659   # connect to database
660   my $dbh = $form->dbconnect($myconfig);
661
662   # delete vendor
663   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
664   my $query = qq|DELETE FROM $cv WHERE id = ?|;
665   do_query($form, $dbh, $query, $form->{id});
666
667   $dbh->disconnect;
668
669   $main::lxdebug->leave_sub();
670 }
671
672 sub search {
673   $main::lxdebug->enter_sub();
674
675   my ( $self, $myconfig, $form ) = @_;
676
677   # connect to database
678   my $dbh = $form->dbconnect($myconfig);
679
680   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
681
682   my $where = "1 = 1";
683   my @values;
684
685   my %allowed_sort_columns =
686     map({ $_, 1 } qw(id customernumber vendornumber name contact phone fax email
687                      taxnumber business invnumber ordnumber quonumber));
688   $sortorder    = $form->{sort} && $allowed_sort_columns{$form->{sort}} ? $form->{sort} : "name";
689   $form->{sort} = $sortorder;
690   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
691
692 if ($sortorder ne 'id') {
693     $sortorder  = "lower($sortorder) ${sortdir}";
694   } else {
695     $sortorder .= " ${sortdir}";
696   }
697
698   if ($form->{"${cv}number"}) {
699     $where .= " AND ct.${cv}number ILIKE ?";
700     push(@values, '%' . $form->{"${cv}number"} . '%');
701   }
702
703   foreach my $key (qw(name contact email)) {
704     if ($form->{$key}) {
705       $where .= " AND ct.$key ILIKE ?";
706       push(@values, '%' . $form->{$key} . '%');
707     }
708   }
709
710   if ($form->{cp_name}) {
711     $where .= " AND ct.id IN (SELECT cp_cv_id FROM contacts WHERE lower(cp_name) LIKE lower(?))";
712     push @values, '%' . $form->{cp_name} . '%';
713   }
714
715   if ($form->{addr_city}) {
716     $where .= " AND ((lower(ct.city) LIKE lower(?))
717                      OR
718                      (ct.id IN (
719                         SELECT trans_id
720                         FROM shipto
721                         WHERE (module = 'CT')
722                           AND (lower(shiptocity) LIKE lower(?))
723                       ))
724                      )";
725     push @values, ('%' . $form->{addr_city} . '%') x 2;
726   }
727
728   if ( $form->{status} eq 'orphaned' ) {
729     $where .=
730       qq| AND ct.id NOT IN | .
731       qq|   (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
732     if ($cv eq 'customer') {
733       $where .=
734         qq| AND ct.id NOT IN | .
735         qq| (SELECT a.customer_id FROM ar a, customer cv | .
736         qq|  WHERE cv.id = a.customer_id)|;
737     }
738     if ($cv eq 'vendor') {
739       $where .=
740         qq| AND ct.id NOT IN | .
741         qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
742         qq|  WHERE cv.id = a.vendor_id)|;
743     }
744     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
745   }
746
747   if ($form->{obsolete} eq "Y") {
748     $where .= qq| AND obsolete|;
749   } elsif ($form->{obsolete} eq "N") {
750     $where .= qq| AND NOT obsolete|;
751   }
752
753   if ($form->{business_id}) {
754     $where .= qq| AND (business_id = ?)|;
755     push(@values, conv_i($form->{business_id}));
756   }
757
758   my ($cvar_where, @cvar_values) = CVar->build_filter_query('module'         => 'CT',
759                                                             'trans_id_field' => 'ct.id',
760                                                             'filter'         => $form);
761
762   if ($cvar_where) {
763     $where .= qq| AND ($cvar_where)|;
764     push @values, @cvar_values;
765   }
766   # Um nach Straße  in der Berichtsmaske zu suchen ... jb 13.11.2008               
767     if ($form->{addr_street}) {                                                
768       $where .= qq| AND (street ILIKE ?)|;                               
769       push @values, ('%' . $form->{addr_street} . '%');                     
770     }                                                        
771                                                                 
772   # Um nach PLZ  in der Berichtsmaske zu suchen ... jb 13.11.2008
773     if ($form->{addr_zipcode}) {                                    
774       $where .= qq| AND (zipcode ILIKE ?)|;                            
775       push @values, ($form->{addr_zipcode} . '%');                      
776     }   
777   my $query =
778     qq|SELECT ct.*, b.description AS business | .
779     qq|FROM $cv ct | .
780     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
781     qq|WHERE $where|;
782
783   my @saved_values = @values;
784   # redo for invoices, orders and quotations
785   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
786     my ($ar, $union, $module);
787     $query = "";
788
789     if ($form->{l_invnumber}) {
790       my $ar = $cv eq 'customer' ? 'ar' : 'ap';
791       my $module = $ar eq 'ar' ? 'is' : 'ir';
792
793       $query =
794         qq|SELECT ct.*, b.description AS business, | .
795         qq|  a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
796         qq|  '$module' AS module, 'invoice' AS formtype, | .
797         qq|  (a.amount = a.paid) AS closed | .
798         qq|FROM $cv ct | .
799         qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
800         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
801         qq|WHERE $where AND (a.invoice = '1')|;
802
803       $union = qq|UNION|;
804     }
805
806     if ( $form->{l_ordnumber} ) {
807       if ($union eq "UNION") {
808         push(@values, @saved_values);
809       }
810       $query .=
811         qq| $union | .
812         qq|SELECT ct.*, b.description AS business,| .
813         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
814         qq|  'oe' AS module, 'order' AS formtype, o.closed | .
815         qq|FROM $cv ct | .
816         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
817         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
818         qq|WHERE $where AND (o.quotation = '0')|;
819
820       $union = qq|UNION|;
821     }
822
823     if ( $form->{l_quonumber} ) {
824       if ($union eq "UNION") {
825         push(@values, @saved_values);
826       }
827       $query .=
828         qq| $union | .
829         qq|SELECT ct.*, b.description AS business, | .
830         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
831         qq|  'oe' AS module, 'quotation' AS formtype, o.closed | .
832         qq|FROM $cv ct | .
833         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
834         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
835         qq|WHERE $where AND (o.quotation = '1')|;
836     }
837   }
838
839   $query .= qq| ORDER BY $sortorder|;
840
841   $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
842
843   $main::lxdebug->leave_sub();
844 }
845
846 sub get_contact {
847   $main::lxdebug->enter_sub();
848
849   my ( $self, $myconfig, $form ) = @_;
850   my $dbh   = $form->dbconnect($myconfig);
851   my $query =
852     qq|SELECT * FROM contacts c | .
853     qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
854   my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
855   my $ref = $sth->fetchrow_hashref(NAME_lc);
856
857   map { $form->{$_} = $ref->{$_} } keys %$ref;
858
859   $query = qq|SELECT COUNT(cp_id) AS used FROM (
860     SELECT cp_id FROM oe UNION
861     SELECT cp_id FROM ar UNION
862     SELECT cp_id FROM ap UNION
863     SELECT cp_id FROM delivery_orders
864   ) AS cpid WHERE cp_id = ? OR ? = 0|;
865   ($form->{cp_used}) = selectfirst_array_query($form, $dbh, $query, ($form->{cp_id})x2);
866
867   $sth->finish;
868   $dbh->disconnect;
869
870   $main::lxdebug->leave_sub();
871 }
872
873 sub get_shipto {
874   $main::lxdebug->enter_sub();
875
876   my ( $self, $myconfig, $form ) = @_;
877   my $dbh   = $form->dbconnect($myconfig);
878   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
879   my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
880
881   my $ref = $sth->fetchrow_hashref(NAME_lc);
882
883   map { $form->{$_} = $ref->{$_} } keys %$ref;
884
885   $query = qq|SELECT COUNT(shipto_id) AS used FROM (
886     SELECT shipto_id FROM oe UNION
887     SELECT shipto_id FROM ar UNION
888     SELECT shipto_id FROM delivery_orders
889   ) AS stid WHERE shipto_id = ? OR ? = 0|;
890   ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
891
892   $sth->finish;
893   $dbh->disconnect;
894
895   $main::lxdebug->leave_sub();
896 }
897
898 sub get_delivery {
899   $main::lxdebug->enter_sub();
900
901   my ( $self, $myconfig, $form ) = @_;
902   my $dbh = $form->dbconnect($myconfig);
903
904   my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
905   my $db = $form->{db} eq "customer" ? "customer" : "vendor";
906
907   my $where = " WHERE 1=1 ";
908   my @values;
909
910   if ($form->{shipto_id} && ($arap eq "ar")) {
911     $where .= "AND ${arap}.shipto_id = ?";
912     push(@values, $form->{shipto_id});
913   } else {
914     $where .= "AND ${arap}.${db}_id = ?";
915     push(@values, $form->{id});
916   }
917
918   if ($form->{from}) {
919     $where .= "AND ${arap}.transdate >= ?";
920     push(@values, conv_date($form->{from}));
921   }
922   if ($form->{to}) {
923     $where .= "AND ${arap}.transdate <= ?";
924     push(@values, conv_date($form->{to}));
925   }
926   my $query =
927     qq|SELECT s.shiptoname, i.qty, | .
928     qq|  ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
929     qq|  i.description, i.unit, i.sellprice | .
930     qq|FROM $arap | .
931     qq|LEFT JOIN shipto s ON | .
932     ($arap eq "ar"
933      ? qq|(ar.shipto_id = s.shipto_id) |
934      : qq|(ap.id = s.trans_id) |) .
935     qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
936     qq|LEFT join parts p ON (p.id = i.parts_id) | .
937     $where .
938     qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
939
940   $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
941
942   $dbh->disconnect;
943
944   $main::lxdebug->leave_sub();
945 }
946
947 sub _save_note {
948   $main::lxdebug->enter_sub();
949
950   my $self   = shift;
951   my %params = @_;
952
953   my $form   = $main::form;
954
955   Common::check_params(\%params, 'dbh');
956
957   if (!$form->{NOTE_subject}) {
958     $main::lxdebug->leave_sub();
959     return;
960   }
961
962   my $dbh = $params{dbh};
963
964   my %follow_up;
965   my %note = (
966     'id'           => $form->{NOTE_id},
967     'subject'      => $form->{NOTE_subject},
968     'body'         => $form->{NOTE_body},
969     'trans_id'     => $form->{id},
970     'trans_module' => 'ct',
971   );
972
973   $note{id} = Notes->save(%note);
974
975   if ($form->{FU_date}) {
976     %follow_up = (
977       'id'               => $form->{FU_id},
978       'note_id'          => $note{id},
979       'follow_up_date'   => $form->{FU_date},
980       'created_for_user' => $form->{FU_created_for_user},
981       'done'             => $form->{FU_done} ? 1 : 0,
982       'subject'          => $form->{NOTE_subject},
983       'body'             => $form->{NOTE_body},
984       'LINKS'            => [
985         {
986           'trans_id'     => $form->{id},
987           'trans_type'   => $form->{db} eq 'customer' ? 'customer' : 'vendor',
988           'trans_info'   => $form->{name},
989         },
990       ],
991     );
992
993     $follow_up{id} = FU->save(%follow_up);
994
995   } elsif ($form->{FU_id}) {
996     do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
997     do_query($form, $dbh, qq|DELETE FROM follow_ups      WHERE id = ?|,           conv_i($form->{FU_id}));
998   }
999
1000   delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
1001
1002   $main::lxdebug->leave_sub();
1003 }
1004
1005 sub _delete_selected_notes {
1006   $main::lxdebug->enter_sub();
1007
1008   my $self   = shift;
1009   my %params = @_;
1010
1011   Common::check_params(\%params, 'dbh');
1012
1013   my $form = $main::form;
1014   my $dbh  = $params{dbh};
1015
1016   foreach my $i (1 .. $form->{NOTES_rowcount}) {
1017     next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
1018
1019     Notes->delete('dbh' => $params{dbh},
1020                   'id'  => $form->{"NOTE_id_$i"});
1021   }
1022
1023   $main::lxdebug->leave_sub();
1024 }
1025
1026 sub delete_shipto {
1027   $main::lxdebug->enter_sub();
1028
1029   my $self      = shift;
1030   my $shipto_id = shift;
1031
1032   my $form      = $main::form;
1033   my %myconfig  = %main::myconfig;
1034   my $dbh       = $form->get_standard_dbh(\%myconfig);
1035
1036   do_query($form, $dbh, qq|UPDATE shipto SET trans_id = NULL WHERE shipto_id = ?|, $shipto_id);
1037
1038   $dbh->commit();
1039
1040   $main::lxdebug->leave_sub();
1041 }
1042
1043 sub delete_shipto {
1044   $main::lxdebug->enter_sub();
1045
1046   my $self      = shift;
1047   my $shipto_id = shift;
1048
1049   my $form      = $main::form;
1050   my %myconfig  = %main::myconfig;
1051   my $dbh       = $form->get_standard_dbh(\%myconfig);
1052
1053   do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $shipto_id);
1054
1055   $dbh->commit();
1056
1057   $main::lxdebug->leave_sub();
1058 }
1059
1060 1;