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