Implementation des Features "Benutzerdefinierte Variablen für Kunden- und Lieferanten...
[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::CVar;
43 use SL::DBUtils;
44
45 sub get_tuple {
46   $main::lxdebug->enter_sub();
47
48   my ( $self, $myconfig, $form ) = @_;
49
50   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
51
52   my $dbh   = $form->dbconnect($myconfig);
53   my $query =
54     qq|SELECT ct.*, b.id AS business, cp.* | .
55     qq|FROM $cv ct | .
56     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
57     qq|LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id) | .
58     qq|WHERE (ct.id = ?) | .
59     qq|ORDER BY cp.cp_id LIMIT 1|;
60   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
61
62   my $ref = $sth->fetchrow_hashref(NAME_lc);
63
64   map { $form->{$_} = $ref->{$_} } keys %$ref;
65
66   $sth->finish;
67   if ( $form->{salesman_id} ) {
68     my $query =
69       qq|SELECT ct.name AS salesman | .
70       qq|FROM $cv ct | .
71       qq|WHERE ct.id = ?|;
72     ($form->{salesman}) =
73       selectrow_query($form, $dbh, $query, $form->{salesman_id});
74   }
75
76   # check if it is orphaned
77   my $arap = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
78   $query =
79     qq|SELECT a.id | .
80     qq|FROM $arap a | .
81     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
82     qq|WHERE ct.id = ? | .
83     qq|UNION | .
84     qq|SELECT a.id | .
85     qq|FROM oe a | .
86     qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
87     qq|WHERE ct.id = ?|;
88   my ($dummy) = selectrow_query($form, $dbh, $query, $form->{id}, $form->{id});
89   $form->{status} = "orphaned" unless ($dummy);
90
91   $dbh->disconnect;
92
93   $main::lxdebug->leave_sub();
94 }
95
96 sub populate_drop_down_boxes {
97   $main::lxdebug->enter_sub();
98
99   my ($self, $myconfig, $form, $provided_dbh) = @_;
100
101   my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
102
103   # get business types
104   $query = qq|SELECT id, description FROM business ORDER BY id|;
105   $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
106
107   # get shipto address
108   $query =
109     qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
110     qq|FROM shipto WHERE (trans_id = ?) AND (module = 'CT')|;
111   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
112
113   # get contacts
114   $query  = qq|SELECT cp_id, cp_name FROM contacts WHERE cp_cv_id = ?|;
115   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
116
117   # get languages
118   $query = qq|SELECT id, description FROM language ORDER BY id|;
119   $form->{languages} = selectall_hashref_query($form, $dbh, $query);
120
121   # get payment terms
122   $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
123   $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
124
125   $dbh->disconnect() unless ($provided_dbh);
126
127   $main::lxdebug->leave_sub();
128 }
129
130 sub query_titles_and_greetings {
131   $main::lxdebug->enter_sub();
132
133   my ( $self, $myconfig, $form ) = @_;
134   my ( %tmp,  $ref );
135
136   my $dbh = $form->dbconnect($myconfig);
137
138   $query =
139     qq|SELECT DISTINCT(cp_greeting) | .
140     qq|FROM contacts | .
141     qq|WHERE cp_greeting ~ '[a-zA-Z]' | .
142     qq|ORDER BY cp_greeting|;
143   $form->{GREETINGS} = [ selectall_array_query($form, $dbh, $query) ];
144
145   $query =
146     qq|SELECT DISTINCT(greeting) | .
147     qq|FROM customer | .
148     qq|WHERE greeting ~ '[a-zA-Z]' | .
149     qq|UNION | .
150     qq|SELECT DISTINCT(greeting) | .
151     qq|FROM vendor | .
152     qq|WHERE greeting ~ '[a-zA-Z]' | .
153     qq|ORDER BY greeting|;
154   my %tmp;
155   map({ $tmp{$_} = 1; } selectall_array_query($form, $dbh, $query));
156   $form->{COMPANY_GREETINGS} = [ sort(keys(%tmp)) ];
157
158   $query =
159     qq|SELECT DISTINCT(cp_title) | .
160     qq|FROM contacts | .
161     qq|WHERE cp_title ~ '[a-zA-Z]'|;
162   $form->{TITLES} = [ selectall_array_query($form, $dbh, $query) ];
163
164   $query =
165     qq|SELECT DISTINCT(cp_abteilung) | .
166     qq|FROM contacts | .
167     qq|WHERE cp_abteilung ~ '[a-zA-Z]'|;
168   $form->{DEPARTMENT} = [ selectall_array_query($form, $dbh, $query) ];
169
170   $dbh->disconnect();
171   $main::lxdebug->leave_sub();
172 }
173
174 sub save_customer {
175   $main::lxdebug->enter_sub();
176
177   my ( $self, $myconfig, $form ) = @_;
178
179   # set pricegroup to default
180   $form->{klass} = 0 unless ($form->{klass});
181
182   # connect to database
183   my $dbh = $form->dbconnect_noauto($myconfig);
184
185   map( {
186     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
187     if ( $form->{"selected_cp_${_}"} );
188        } qw(title greeting abteilung) );
189   $form->{"greeting"} = $form->{"selected_company_greeting"}
190   if ( $form->{"selected_company_greeting"} );
191
192   # assign value discount, terms, creditlimit
193   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
194   $form->{discount} /= 100;
195   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
196
197   my ( $query, $sth, $f_id );
198
199   if ( $form->{id} ) {
200     $query = qq|SELECT id FROM customer WHERE customernumber = ?|;
201     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
202
203     if (($f_id ne $form->{id}) && ($f_id ne "")) {
204       $main::lxdebug->leave_sub();
205       return 3;
206     }
207
208   } else {
209     if (!$form->{customernumber} && $form->{business}) {
210       $form->{customernumber} =
211         $form->update_business($myconfig, $form->{business}, $dbh);
212     }
213     if (!$form->{customernumber}) {
214       $form->{customernumber} =
215         $form->update_defaults($myconfig, "customernumber", $dbh);
216     }
217
218     $query  = qq|SELECT c.id FROM customer c WHERE c.customernumber = ?|;
219     ($f_id) = selectrow_query($form, $dbh, $query, $form->{customernumber});
220     if ($f_id ne "") {
221       $main::lxdebug->leave_sub();
222       return 3;
223     }
224
225     $query = qq|SELECT nextval('id')|;
226     ($form->{id}) = selectrow_query($form, $dbh, $query);
227
228     $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|;
229     do_query($form, $dbh, $query, $form->{id});
230   }
231
232   $query = qq|UPDATE customer SET | .
233     qq|customernumber = ?, | .
234     qq|name = ?, | .
235     qq|greeting = ?, | .
236     qq|department_1 = ?, | .
237     qq|department_2 = ?, | .
238     qq|street = ?, | .
239     qq|zipcode = ?, | .
240     qq|city = ?, | .
241     qq|country = ?, | .
242     qq|homepage = ?, | .
243     qq|contact = ?, | .
244     qq|phone = ?, | .
245     qq|fax = ?, | .
246     qq|email = ?, | .
247     qq|cc = ?, | .
248     qq|bcc = ?, | .
249     qq|notes = ?, | .
250     qq|discount = ?, | .
251     qq|creditlimit = ?, | .
252     qq|terms = ?, | .
253     qq|business_id = ?, | .
254     qq|taxnumber = ?, | .
255     qq|sic_code = ?, | .
256     qq|language = ?, | .
257     qq|account_number = ?, | .
258     qq|bank_code = ?, | .
259     qq|bank = ?, | .
260     qq|obsolete = ?, | .
261     qq|ustid = ?, | .
262     qq|username = ?, | .
263     qq|salesman_id = ?, | .
264     qq|language_id = ?, | .
265     qq|payment_id = ?, | .
266     qq|taxzone_id = ?, | .
267     qq|user_password = ?, | .
268     qq|c_vendor_id = ?, | .
269     qq|klass = ? | .
270     qq|WHERE id = ?|;
271   my @values = (
272     $form->{customernumber},
273     $form->{name},
274     $form->{greeting},
275     $form->{department_1},
276     $form->{department_2},
277     $form->{street},
278     $form->{zipcode},
279     $form->{city},
280     $form->{country},
281     $form->{homepage},
282     $form->{contact},
283     $form->{phone},
284     $form->{fax},
285     $form->{email},
286     $form->{cc},
287     $form->{bcc},
288     $form->{notes},
289     $form->{discount},
290     $form->{creditlimit},
291     conv_i($form->{terms}),
292     conv_i($form->{business}),
293     $form->{taxnumber},
294     $form->{sic},
295     $form->{language},
296     $form->{account_number},
297     $form->{bank_code},
298     $form->{bank},
299     $form->{obsolete} ? 't' : 'f',
300     $form->{ustid},
301     $form->{username},
302     conv_i($form->{salesman_id}),
303     conv_i($form->{language_id}),
304     conv_i($form->{payment_id}),
305     conv_i($form->{taxzone_id}, 0),
306     $form->{user_password},
307     $form->{c_vendor_id},
308     conv_i($form->{klass}),
309     $form->{id}
310     );
311   do_query( $form, $dbh, $query, @values );
312
313   $query = undef;
314   if ( $form->{cp_id} ) {
315     $query = qq|UPDATE contacts SET | .
316       qq|cp_greeting = ?, | .
317       qq|cp_title = ?,  | .
318       qq|cp_givenname = ?, | .
319       qq|cp_name = ?, | .
320       qq|cp_email = ?, | .
321       qq|cp_phone1 = ?, | .
322       qq|cp_phone2 = ?, | .
323       qq|cp_abteilung = ?, | .
324       qq|cp_fax = ?, | .
325       qq|cp_mobile1 = ?, | .
326       qq|cp_mobile2 = ?, | .
327       qq|cp_satphone = ?, | .
328       qq|cp_satfax = ?, | .
329       qq|cp_project = ?, | .
330       qq|cp_privatphone = ?, | .
331       qq|cp_privatemail = ?, | .
332       qq|cp_birthday = ? | .
333       qq|WHERE cp_id = ?|;
334     @values = (
335       $form->{cp_greeting},
336       $form->{cp_title},
337       $form->{cp_givenname},
338       $form->{cp_name},
339       $form->{cp_email},
340       $form->{cp_phone1},
341       $form->{cp_phone2},
342       $form->{cp_abteilung},
343       $form->{cp_fax},
344       $form->{cp_mobile1},
345       $form->{cp_mobile2},
346       $form->{cp_satphone},
347       $form->{cp_satfax},
348       $form->{cp_project},
349       $form->{cp_privatphone},
350       $form->{cp_privatemail},
351       $form->{cp_birthday},
352       $form->{cp_id}
353       );
354   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
355     $query =
356       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname,  | .
357       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
358       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
359       qq|  cp_birthday) | .
360       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
361     @values = (
362       $form->{id},
363       $form->{cp_greeting},
364       $form->{cp_title},
365       $form->{cp_givenname},
366       $form->{cp_name},
367       $form->{cp_email},
368       $form->{cp_phone1},
369       $form->{cp_phone2},
370       $form->{cp_abteilung},
371       $form->{cp_fax},
372       $form->{cp_mobile1},
373       $form->{cp_mobile2},
374       $form->{cp_satphone},
375       $form->{cp_satfax},
376       $form->{cp_project},
377       $form->{cp_privatphone},
378       $form->{cp_privatemail},
379       $form->{cp_birthday}
380       );
381   }
382   do_query( $form, $dbh, $query, @values ) if ($query);
383
384   # add shipto
385   $form->add_shipto( $dbh, $form->{id}, "CT" );
386
387   CVar->save_custom_variables('dbh'       => $dbh,
388                               'module'    => 'CT',
389                               'trans_id'  => $form->{id},
390                               'variables' => $form);
391
392   $rc = $dbh->commit();
393   $dbh->disconnect();
394
395   $main::lxdebug->leave_sub();
396   return $rc;
397 }
398
399 sub save_vendor {
400   $main::lxdebug->enter_sub();
401
402   my ( $self, $myconfig, $form ) = @_;
403
404   $form->{taxzone_id} *= 1;
405   # connect to database
406   my $dbh = $form->dbconnect_noauto($myconfig);
407
408   map( {
409     $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
410     if ( $form->{"selected_cp_${_}"} );
411        } qw(title greeting abteilung) );
412   $form->{"greeting"} = $form->{"selected_company_greeting"}
413   if ( $form->{"selected_company_greeting"} );
414
415   $form->{discount} = $form->parse_amount( $myconfig, $form->{discount} );
416   $form->{discount} /= 100;
417   $form->{creditlimit} = $form->parse_amount( $myconfig, $form->{creditlimit} );
418
419   my $query;
420
421   if ( $form->{id} ) {
422     $query = qq|DELETE FROM shipto WHERE (trans_id = ?) AND (module = 'CT')|;
423     do_query($form, $dbh, $query, $form->{id});
424
425   } else {
426     $query = qq|SELECT nextval('id')|;
427     ($form->{id}) = selectrow_query($form, $dbh, $query);
428
429     $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|;
430     do_query($form, $dbh, $query, $form->{id});
431
432     if ( !$form->{vendornumber} ) {
433       $form->{vendornumber} = $form->update_defaults( $myconfig, "vendornumber", $dbh );
434     }
435   }
436
437   $query =
438     qq|UPDATE vendor SET | .
439     qq|  vendornumber = ?, | .
440     qq|  name = ?, | .
441     qq|  greeting = ?, | .
442     qq|  department_1 = ?, | .
443     qq|  department_2 = ?, | .
444     qq|  street = ?, | .
445     qq|  zipcode = ?, | .
446     qq|  city = ?, | .
447     qq|  country = ?, | .
448     qq|  homepage = ?, | .
449     qq|  contact = ?, | .
450     qq|  phone = ?, | .
451     qq|  fax = ?, | .
452     qq|  email = ?, | .
453     qq|  cc = ?, | .
454     qq|  bcc = ?, | .
455     qq|  notes = ?, | .
456     qq|  terms = ?, | .
457     qq|  discount = ?, | .
458     qq|  creditlimit = ?, | .
459     qq|  business_id = ?, | .
460     qq|  taxnumber = ?, | .
461     qq|  sic_code = ?, | .
462     qq|  language = ?, | .
463     qq|  account_number = ?, | .
464     qq|  bank_code = ?, | .
465     qq|  bank = ?, | .
466     qq|  obsolete = ?, | .
467     qq|  ustid = ?, | .
468     qq|  payment_id = ?, | .
469     qq|  taxzone_id = ?, | .
470     qq|  language_id = ?, | .
471     qq|  username = ?, | .
472     qq|  user_password = ?, | .
473     qq|  v_customer_id = ? | .
474     qq|WHERE id = ?|;
475   @values = (
476     $form->{vendornumber},
477     $form->{name},
478     $form->{greeting},
479     $form->{department_1},
480     $form->{department_2},
481     $form->{street},
482     $form->{zipcode},
483     $form->{city},
484     $form->{country},
485     $form->{homepage},
486     $form->{contact},
487     $form->{phone},
488     $form->{fax},
489     $form->{email},
490     $form->{cc},
491     $form->{bcc},
492     $form->{notes},
493     conv_i($form->{terms}),
494     $form->{discount},
495     $form->{creditlimit},
496     conv_i($form->{business}),
497     $form->{taxnumber},
498     $form->{sic},
499     $form->{language},
500     $form->{account_number},
501     $form->{bank_code},
502     $form->{bank},
503     $form->{obsolete} ? 't' : 'f',
504     $form->{ustid},
505     conv_i($form->{payment_id}),
506     conv_i($form->{taxzone_id}, 0),
507     conv_i( $form->{language_id}),
508     $form->{username},
509     $form->{user_password},
510     $form->{v_customer_id},
511     $form->{id}
512     );
513   do_query($form, $dbh, $query, @values);
514
515   $query = undef;
516   if ( $form->{cp_id} ) {
517     $query = qq|UPDATE contacts SET | .
518       qq|cp_greeting = ?, | .
519       qq|cp_title = ?,  | .
520       qq|cp_givenname = ?, | .
521       qq|cp_name = ?, | .
522       qq|cp_email = ?, | .
523       qq|cp_phone1 = ?, | .
524       qq|cp_phone2 = ?, | .
525       qq|cp_abteilung = ?, | .
526       qq|cp_fax = ?, | .
527       qq|cp_mobile1 = ?, | .
528       qq|cp_mobile2 = ?, | .
529       qq|cp_satphone = ?, | .
530       qq|cp_satfax = ?, | .
531       qq|cp_project = ?, | .
532       qq|cp_privatphone = ?, | .
533       qq|cp_privatemail = ?, | .
534       qq|cp_birthday = ? | .
535       qq|WHERE cp_id = ?|;
536     @values = (
537       $form->{cp_greeting},
538       $form->{cp_title},
539       $form->{cp_givenname},
540       $form->{cp_name},
541       $form->{cp_email},
542       $form->{cp_phone1},
543       $form->{cp_phone2},
544       $form->{cp_abteilung},
545       $form->{cp_fax},
546       $form->{cp_mobile1},
547       $form->{cp_mobile2},
548       $form->{cp_satphone},
549       $form->{cp_satfax},
550       $form->{cp_project},
551       $form->{cp_privatphone},
552       $form->{cp_privatemail},
553       $form->{cp_birthday},
554       $form->{cp_id}
555       );
556   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
557     $query =
558       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname,  | .
559       qq|  cp_name, cp_email, cp_phone1, cp_phone2, cp_abteilung, cp_fax, cp_mobile1, | .
560       qq|  cp_mobile2, cp_satphone, cp_satfax, cp_project, cp_privatphone, cp_privatemail, | .
561       qq|  cp_birthday) | .
562       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
563     @values = (
564       $form->{id},
565       $form->{cp_greeting},
566       $form->{cp_title},
567       $form->{cp_givenname},
568       $form->{cp_name},
569       $form->{cp_email},
570       $form->{cp_phone1},
571       $form->{cp_phone2},
572       $form->{cp_abteilung},
573       $form->{cp_fax},
574       $form->{cp_mobile1},
575       $form->{cp_mobile2},
576       $form->{cp_satphone},
577       $form->{cp_satfax},
578       $form->{cp_project},
579       $form->{cp_privatphone},
580       $form->{cp_privatemail},
581       $form->{cp_birthday}
582       );
583   }
584   do_query($form, $dbh, $query, @values) if ($query);
585
586   # add shipto
587   $form->add_shipto( $dbh, $form->{id}, "CT" );
588
589   CVar->save_custom_variables('dbh'       => $dbh,
590                               'module'    => 'CT',
591                               'trans_id'  => $form->{id},
592                               'variables' => $form);
593
594   $rc = $dbh->commit();
595   $dbh->disconnect();
596
597   $main::lxdebug->leave_sub();
598   return $rc;
599 }
600
601 sub delete {
602   $main::lxdebug->enter_sub();
603
604   my ( $self, $myconfig, $form ) = @_;
605   # connect to database
606   my $dbh = $form->dbconnect($myconfig);
607
608   # delete vendor
609   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
610   my $query = qq|DELETE FROM $cv WHERE id = ?|;
611   do_query($form, $dbh, $query, $form->{id});
612
613   $dbh->disconnect;
614
615   $main::lxdebug->leave_sub();
616 }
617
618 sub search {
619   $main::lxdebug->enter_sub();
620
621   my ( $self, $myconfig, $form ) = @_;
622
623   # connect to database
624   my $dbh = $form->dbconnect($myconfig);
625
626   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
627
628   my $where = "1 = 1";
629   my @values;
630
631   my %allowed_sort_columns =
632     map({ $_, 1 } qw(id customernumber name address contact phone fax email
633                      taxnumber sic_code business invnumber ordnumber quonumber));
634   $sortorder =
635     $form->{sort} && $allowed_sort_columns{$form->{sort}} ?
636     $form->{sort} : "name";
637   $form->{sort} = $sortorder;
638   $sortorder = "country,city,street" if ($sortorder eq "address");
639
640   if ($form->{"${cv}number"}) {
641     $where .= " AND ct.${cv}number ILIKE ?";
642     push(@values, '%' . $form->{"${cv}number"} . '%');
643   }
644
645   foreach my $key (qw(name contact email)) {
646     if ($form->{$key}) {
647       $where .= " AND ct.$key ILIKE ?";
648       push(@values, '%' . $form->{$key} . '%');
649     }
650   }
651
652   if ( $form->{status} eq 'orphaned' ) {
653     $where .=
654       qq| AND ct.id NOT IN | .
655       qq|   (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
656     if ($cv eq 'customer') {
657       $where .=
658         qq| AND ct.id NOT IN | .
659         qq| (SELECT a.customer_id FROM ar a, customer cv | .
660         qq|  WHERE cv.id = a.customer_id)|;
661     }
662     if ($cv eq 'vendor') {
663       $where .=
664         qq| AND ct.id NOT IN | .
665         qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
666         qq|  WHERE cv.id = a.vendor_id)|;
667     }
668     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
669   }
670
671   if ($form->{obsolete} eq "Y") {
672     $where .= qq| AND obsolete|;
673   } elsif ($form->{obsolete} eq "N") {
674     $where .= qq| AND NOT obsolete|;
675   }
676
677   if ($form->{business_id}) {
678     $where .= qq| AND (business_id = ?)|;
679     push(@values, conv_i($form->{business_id}));
680   }
681
682   my ($cvar_where, @cvar_values) = CVar->build_filter_query('module'         => 'CT',
683                                                             'trans_id_field' => 'ct.id',
684                                                             'filter'         => $form);
685
686   if ($cvar_where) {
687     $where .= qq| AND ($cvar_where)|;
688     push @values, @cvar_values;
689   }
690
691   my $query =
692     qq|SELECT ct.*, b.description AS business | .
693     qq|FROM $cv ct | .
694     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
695     qq|WHERE $where|;
696
697   my @saved_values = @values;
698   # redo for invoices, orders and quotations
699   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
700     my ($ar, $union, $module);
701     $query = "";
702
703     if ($form->{l_invnumber}) {
704       my $ar = $cv eq 'customer' ? 'ar' : 'ap';
705       my $module = $ar eq 'ar' ? 'is' : 'ir';
706
707       $query =
708         qq|SELECT ct.*, b.description AS business, | .
709         qq|  a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
710         qq|  '$module' AS module, 'invoice' AS formtype, | .
711         qq|  (a.amount = a.paid) AS closed | .
712         qq|FROM $cv ct | .
713         qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
714         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
715         qq|WHERE $where AND (a.invoice = '1')|;
716
717       $union = qq|UNION|;
718     }
719
720     if ( $form->{l_ordnumber} ) {
721       if ($union eq "UNION") {
722         push(@values, @saved_values);
723       }
724       $query .=
725         qq| $union | .
726         qq|SELECT ct.*, b.description AS business,| .
727         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
728         qq|  'oe' AS module, 'order' AS formtype, o.closed | .
729         qq|FROM $cv ct | .
730         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
731         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
732         qq|WHERE $where AND (o.quotation = '0')|;
733
734       $union = qq|UNION|;
735     }
736
737     if ( $form->{l_quonumber} ) {
738       if ($union eq "UNION") {
739         push(@values, @saved_values);
740       }
741       $query .=
742         qq| $union | .
743         qq|SELECT ct.*, b.description AS business, | .
744         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
745         qq|  'oe' AS module, 'quotation' AS formtype, o.closed | .
746         qq|FROM $cv ct | .
747         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
748         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
749         qq|WHERE $where AND (o.quotation = '1')|;
750     }
751   }
752
753   $query .= qq| ORDER BY $sortorder|;
754
755   $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
756   map({ my $ref = $_; $ref->{address} = join(" ", map({ $ref->{$_} } qw(street zipcode city country))); }
757       @{ $form->{CT} });
758
759   $main::lxdebug->leave_sub();
760 }
761
762 sub get_contact {
763   $main::lxdebug->enter_sub();
764
765   my ( $self, $myconfig, $form ) = @_;
766   my $dbh   = $form->dbconnect($myconfig);
767   my $query =
768     qq|SELECT * FROM contacts c | .
769     qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
770   my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
771   my $ref = $sth->fetchrow_hashref(NAME_lc);
772
773   map { $form->{$_} = $ref->{$_} } keys %$ref;
774
775   $sth->finish;
776   $dbh->disconnect;
777
778   $main::lxdebug->leave_sub();
779 }
780
781 sub get_shipto {
782   $main::lxdebug->enter_sub();
783
784   my ( $self, $myconfig, $form ) = @_;
785   my $dbh   = $form->dbconnect($myconfig);
786   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
787   my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
788
789   my $ref = $sth->fetchrow_hashref(NAME_lc);
790
791   map { $form->{$_} = $ref->{$_} } keys %$ref;
792
793   $sth->finish;
794   $dbh->disconnect;
795
796   $main::lxdebug->leave_sub();
797 }
798
799 sub get_delivery {
800   $main::lxdebug->enter_sub();
801
802   my ( $self, $myconfig, $form ) = @_;
803   my $dbh = $form->dbconnect($myconfig);
804
805   my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
806   my $db = $form->{db} eq "customer" ? "customer" : "vendor";
807
808   my $where = " WHERE 1=1 ";
809   my @values;
810
811   if ($form->{shipto_id} && ($arap eq "ar")) {
812     $where .= "AND ${arap}.shipto_id = ?";
813     push(@values, $form->{shipto_id});
814   } else {
815     $where .= "AND ${arap}.${db}_id = ?";
816     push(@values, $form->{id});
817   }
818
819   if ($form->{from}) {
820     $where .= "AND ${arap}.transdate >= ?";
821     push(@values, conv_date($form->{from}));
822   }
823   if ($form->{to}) {
824     $where .= "AND ${arap}.transdate <= ?";
825     push(@values, conv_date($form->{to}));
826   }
827   my $query =
828     qq|SELECT s.shiptoname, i.qty, | .
829     qq|  ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
830     qq|  i.description, i.unit, i.sellprice | .
831     qq|FROM $arap | .
832     qq|LEFT JOIN shipto s ON | .
833     ($arap eq "ar"
834      ? qq|(ar.shipto_id = s.shipto_id) |
835      : qq|(ap.id = s.trans_id) |) .
836     qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
837     qq|LEFT join parts p ON (p.id = i.parts_id) | .
838     $where .
839     qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
840
841   $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
842
843   $dbh->disconnect;
844
845   $main::lxdebug->leave_sub();
846 }
847
848 1;