51c4507a495bb07df4e7b9db8b2f11f2f3e46539
[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     conv_i($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 =
510       qq|UPDATE contacts SET | .
511       qq|  cp_greeting = ?, | .
512       qq|  cp_title = ?, | .
513       qq|  cp_givenname = ?, | .
514       qq|  cp_name = ?, | .
515       qq|  cp_email = ?, | .
516       qq|  cp_phone1 = ?, | .
517       qq|  cp_phone2 = ? | .
518       qq|WHERE cp_id = ?|;
519     @values = (
520       $form->{cp_greeting},
521       $form->{cp_title},
522       $form->{cp_givenname},
523       $form->{cp_name},
524       $form->{cp_email},
525       $form->{cp_phone1},
526       $form->{cp_phone2},
527       $form->{cp_id});
528   } elsif ( $form->{cp_name} || $form->{cp_givenname} ) {
529     $query =
530       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2) | .
531       qq|VALUES (?, ?, ?, ?, ?, ?, ?, ?)|;
532     @values = (
533       conv_i($form->{id}),
534       $form->{cp_greeting},
535       $form->{cp_title},
536       $form->{cp_givenname},
537       $form->{cp_name},
538       $form->{cp_email},
539       $form->{cp_phone1},
540       $form->{cp_phone2});
541   }
542   do_query($form, $dbh, $query, @values) if ($query);
543
544   # add shipto
545   $form->add_shipto( $dbh, $form->{id}, "CT" );
546
547   $rc = $dbh->commit();
548   $dbh->disconnect();
549
550   $main::lxdebug->leave_sub();
551   return $rc;
552 }
553
554 sub delete {
555   $main::lxdebug->enter_sub();
556
557   my ( $self, $myconfig, $form ) = @_;
558   # connect to database
559   my $dbh = $form->dbconnect($myconfig);
560
561   # delete vendor
562   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
563   my $query = qq|DELETE FROM $cv WHERE id = ?|;
564   do_query($form, $dbh, $query, $form->{id});
565
566   $dbh->disconnect;
567
568   $main::lxdebug->leave_sub();
569 }
570
571 sub search {
572   $main::lxdebug->enter_sub();
573
574   my ( $self, $myconfig, $form ) = @_;
575
576   # connect to database
577   my $dbh = $form->dbconnect($myconfig);
578
579   my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
580
581   my $where = "1 = 1";
582   my @values;
583
584   my %allowed_sort_columns =
585     map({ $_, 1 } qw(id customernumber name address contact phone fax email
586                      taxnumber sic_code business invnumber ordnumber quonumber));
587   $sortorder =
588     $form->{sort} && $allowed_sort_columns{$form->{sort}} ?
589     $form->{sort} : "name";
590   $form->{sort} = $sortorder;
591   $sortorder = "country,city,street" if ($sortorder eq "address");
592
593   if ($form->{"${cv}number"}) {
594     $where .= " AND ct.${cv}number ILIKE ?";
595     push(@values, '%' . $form->{"${cv}number"} . '%');
596   }
597
598   foreach my $key (qw(name contact email)) {
599     if ($form->{$key}) {
600       $where .= " AND ct.$key ILIKE ?";
601       push(@values, '%' . $form->{$key} . '%');
602     }
603   }
604
605   if ( $form->{status} eq 'orphaned' ) {
606     $where .=
607       qq| AND ct.id NOT IN | .
608       qq|   (SELECT o.${cv}_id FROM oe o, $cv cv WHERE cv.id = o.${cv}_id)|;
609     if ($cv eq 'customer') {
610       $where .=
611         qq| AND ct.id NOT IN | .
612         qq| (SELECT a.customer_id FROM ar a, customer cv | .
613         qq|  WHERE cv.id = a.customer_id)|;
614     }
615     if ($cv eq 'vendor') {
616       $where .=
617         qq| AND ct.id NOT IN | .
618         qq| (SELECT a.vendor_id FROM ap a, vendor cv | .
619         qq|  WHERE cv.id = a.vendor_id)|;
620     }
621     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
622   }
623
624   if ($form->{obsolete} eq "Y") {
625     $where .= qq| AND obsolete|;
626   } elsif ($form->{obsolete} eq "N") {
627     $where .= qq| AND NOT obsolete|;
628   }
629
630   if ($form->{business_id}) {
631     $where .= qq| AND (business_id = ?)|;
632     push(@values, conv_i($form->{business_id}));
633   }
634
635   my $query =
636     qq|SELECT ct.*, b.description AS business | .
637     qq|FROM $cv ct | .
638     qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
639     qq|WHERE $where|;
640
641   # redo for invoices, orders and quotations
642   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
643     my ($ar, $union, $module);
644     $query = "";
645
646     if ($form->{l_invnumber}) {
647       my $ar = $cv eq 'customer' ? 'ar' : 'ap';
648       my $module = $ar eq 'ar' ? 'is' : 'ir';
649
650       $query =
651         qq|SELECT ct.*, b.description AS business, | .
652         qq|  a.invnumber, a.ordnumber, a.quonumber, a.id AS invid, | .
653         qq|  '$module' AS module, 'invoice' AS formtype, | .
654         qq|  (a.amount = a.paid) AS closed | .
655         qq|FROM $cv ct | .
656         qq|JOIN $ar a ON (a.${cv}_id = ct.id) | .
657         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
658         qq|WHERE $where AND (a.invoice = '1')|;
659
660       $union = qq|UNION|;
661     }
662
663     if ( $form->{l_ordnumber} ) {
664       $query .=
665         qq| $union | .
666         qq|SELECT ct.*, b.description AS business,| .
667         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
668         qq|  'oe' AS module, 'order' AS formtype, o.closed | .
669         qq|FROM $cv ct | .
670         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
671         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
672         qq|WHERE $where AND (o.quotation = '0')|;
673
674       $union = qq|UNION|;
675     }
676
677     if ( $form->{l_quonumber} ) {
678       $query .=
679         qq| $union | .
680         qq|SELECT ct.*, b.description AS business, | .
681         qq|  ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid, | .
682         qq|  'oe' AS module, 'quotation' AS formtype, o.closed | .
683         qq|FROM $cv ct | .
684         qq|JOIN oe o ON (o.${cv}_id = ct.id) | .
685         qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
686         qq|WHERE $where AND (o.quotation = '1')|;
687     }
688   }
689
690   $query .= qq| ORDER BY $sortorder|;
691
692   $form->{CT} = selectall_hashref_query($form, $dbh, $query, @values);
693   map({ my $ref = $_; $ref->{address} = join(" ", map({ $ref->{$_} } qw(street zipcode city country))); }
694       @{ $form->{CT} });
695
696   $main::lxdebug->leave_sub();
697 }
698
699 sub get_contact {
700   $main::lxdebug->enter_sub();
701
702   my ( $self, $myconfig, $form ) = @_;
703   my $dbh   = $form->dbconnect($myconfig);
704   my $query =
705     qq|SELECT * FROM contacts c | .
706     qq|WHERE cp_id = ? ORDER BY cp_id limit 1|;
707   my $sth = prepare_execute_query($form, $dbh, $query, $form->{cp_id});
708   my $ref = $sth->fetchrow_hashref(NAME_lc);
709
710   map { $form->{$_} = $ref->{$_} } keys %$ref;
711
712   $sth->finish;
713   $dbh->disconnect;
714
715   $main::lxdebug->leave_sub();
716 }
717
718 sub get_shipto {
719   $main::lxdebug->enter_sub();
720
721   my ( $self, $myconfig, $form ) = @_;
722   my $dbh   = $form->dbconnect($myconfig);
723   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
724   my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
725
726   my $ref = $sth->fetchrow_hashref(NAME_lc);
727
728   map { $form->{$_} = $ref->{$_} } keys %$ref;
729
730   $sth->finish;
731   $dbh->disconnect;
732
733   $main::lxdebug->leave_sub();
734 }
735
736 sub get_delivery {
737   $main::lxdebug->enter_sub();
738
739   my ( $self, $myconfig, $form ) = @_;
740   my $dbh = $form->dbconnect($myconfig);
741
742   my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
743   my $db = $form->{db} eq "customer" ? "customer" : "vendor";
744
745   my $where = " WHERE 1=1 ";
746   my @values;
747
748   if ($form->{shipto_id} && ($arap eq "ar")) {
749     $where .= "AND ${arap}.shipto_id = ?";
750     push(@values, $form->{shipto_id});
751   } else {
752     $where .= "AND ${arap}.${db}_id = ?";
753     push(@values, $form->{id});
754   }
755
756   if ($form->{from}) {
757     $where .= "AND ${arap}.transdate >= ?";
758     push(@values, conv_date($form->{from}));
759   }
760   if ($form->{to}) {
761     $where .= "AND ${arap}.transdate <= ?";
762     push(@values, conv_date($form->{to}));
763   }
764   my $query =
765     qq|SELECT s.shiptoname, i.qty, | .
766     qq|  ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
767     qq|  invoice.description, invoice.unit | .
768     qq|FROM $arap | .
769     qq|LEFT JOIN shipto s ON | .
770     ($arap eq "ar"
771      ? qq|(ar.shipto_id = s.shipto_id) |
772      : qq|(ap.id = s.trans_id) |) .
773     qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
774     qq|LEFT join parts p ON (p.id = i.parts_id) | .
775     $where .
776     qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
777
778   $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
779
780   $dbh->disconnect;
781
782   $main::lxdebug->leave_sub();
783 }
784
785 1;