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