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