Uebernahme der kompletten Version, so wie sie Philip als "Demo-Version" gezeigt hat...
[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
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 id, shiptoname
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->{language_id} *= 1;
377   $form->{payment_id} *= 1;
378   $form->{taxzone_id} *= 1;
379   $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
380
381   my ($query, $sth);
382
383   if ($form->{id}) {
384     $query = qq|DELETE FROM customertax
385                 WHERE customer_id = $form->{id}|;
386     $dbh->do($query) || $form->dberror($query);
387
388 #     $query = qq|DELETE FROM shipto
389 #                 WHERE trans_id = $form->{id} AND module = 'CT'|;
390 #     $dbh->do($query) || $form->dberror($query);
391   } else {
392     my $uid = rand() . time;
393
394     $uid .= $form->{login};
395
396     $uid = substr($uid, 2, 75);
397
398     $query = qq|INSERT INTO customer (name)
399                 VALUES ('$uid')|;
400     $dbh->do($query) || $form->dberror($query);
401
402     $query = qq|SELECT c.id FROM customer c
403                 WHERE c.name = '$uid'|;
404     $sth = $dbh->prepare($query);
405     $sth->execute || $form->dberror($query);
406
407     ($form->{id}) = $sth->fetchrow_array;
408     $sth->finish;
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   }
419
420   $query = qq|UPDATE customer SET
421               customernumber = '$form->{customernumber}',
422               name = '$form->{name}',
423               greeting = '$form->{greeting}',
424               department_1 = '$form->{department_1}',
425               department_2 = '$form->{department_2}',
426               street = '$form->{street}',
427               zipcode = '$form->{zipcode}',
428               city = '$form->{city}',
429               country = '$form->{country}',
430               homepage = '$form->{homepage}',
431               contact = '$form->{contact}',
432               phone = '$form->{phone}',
433               fax = '$form->{fax}',
434               email = '$form->{email}',
435               cc = '$form->{cc}',
436               bcc = '$form->{bcc}',
437               notes = '$form->{notes}',
438               discount = $form->{discount},
439               creditlimit = $form->{creditlimit},
440               terms = $form->{terms},
441               taxincluded = '$form->{taxincluded}',
442               business_id = $form->{business},
443               taxnumber = '$form->{taxnumber}',
444               sic_code = '$form->{sic}',
445               language = '$form->{language}',
446               account_number = '$form->{account_number}',
447               bank_code = '$form->{bank_code}',
448               bank = '$form->{bank}',
449               obsolete = '$form->{obsolete}',
450               ustid = '$form->{ustid}',
451               username = '$form->{username}',
452               salesman_id = '$form->{salesman_id}',
453               language_id = '$form->{language_id}',
454               payment_id = '$form->{payment_id}',
455               taxzone_id = '$form->{taxzone_id}',
456               user_password = | . $dbh->quote($form->{user_password}) . qq|,
457               c_vendor_id = '$form->{c_vendor_id}',
458               klass = '$form->{klass}'
459               WHERE id = $form->{id}|;
460   $dbh->do($query) || $form->dberror($query);
461
462   if ($form->{cp_id}) {
463     $query = qq|UPDATE contacts SET
464                 cp_greeting = '$form->{cp_greeting}',
465                 cp_title = '$form->{cp_title}',
466                 cp_givenname = '$form->{cp_givenname}',
467                 cp_name = '$form->{cp_name}',
468                 cp_email = '$form->{cp_email}',
469                 cp_phone1 = '$form->{cp_phone1}',
470                 cp_phone2 = '$form->{cp_phone2}',
471                 cp_abteilung = | . $dbh->quote($form->{cp_abteilung}) . qq|,
472                 cp_fax = | . $dbh->quote($form->{cp_fax}) . qq|,
473                 cp_mobile1 = | . $dbh->quote($form->{cp_mobile1}) . qq|,
474                 cp_mobile2 = | . $dbh->quote($form->{cp_mobile2}) . qq|,
475                 cp_satphone = | . $dbh->quote($form->{cp_satphone}) . qq|,
476                 cp_satfax = | . $dbh->quote($form->{cp_satfax}) . qq|,
477                 cp_project = | . $dbh->quote($form->{cp_project}) . qq|,
478                 cp_privatphone = | . $dbh->quote($form->{cp_privatphone}) . qq|,
479                 cp_privatemail = | . $dbh->quote($form->{cp_privatemail}) . qq|,
480                 cp_birthday = | . $dbh->quote($form->{cp_birthday}) . qq|
481                 WHERE cp_id = $form->{cp_id}|;
482   } elsif ($form->{cp_name} || $form->{cp_givenname}) {
483     $query =
484       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)
485                   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|)|;
486   }
487   $dbh->do($query) || $form->dberror($query);
488
489   # save taxes
490   foreach $item (split / /, $form->{taxaccounts}) {
491     if ($form->{"tax_$item"}) {
492       $query = qq|INSERT INTO customertax (customer_id, chart_id)
493                   VALUES ($form->{id}, (SELECT c.id
494                                         FROM chart c
495                                         WHERE c.accno = '$item'))|;
496       $dbh->do($query) || $form->dberror($query);
497     }
498   }
499   print(STDERR "SHIPTO_ID $form->{shipto_id}\n");
500   # add shipto
501   $form->add_shipto($dbh, $form->{id}, "CT");
502
503   $rc = $dbh->disconnect;
504
505   $main::lxdebug->leave_sub();
506   return $rc;
507 }
508
509 sub save_vendor {
510   $main::lxdebug->enter_sub();
511
512   my ($self, $myconfig, $form) = @_;
513
514   # connect to database
515   my $dbh = $form->dbconnect($myconfig);
516 ##LINET
517   map({
518       $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
519         if ($form->{"selected_cp_${_}"});
520   } qw(title greeting abteilung));
521   $form->{"greeting"} = $form->{"selected_company_greeting"}
522         if ($form->{"selected_company_greeting"});
523   # escape '
524   map { $form->{$_} =~ s/\'/\'\'/g }
525     qw(vendornumber name street zipcode city country homepage contact notes cp_title cp_greeting language);
526 ##/LINET
527   $form->{discount} = $form->parse_amount($myconfig, $form->{discount});
528   $form->{discount} /= 100;
529   $form->{terms}       *= 1;
530   $form->{taxincluded} *= 1;
531   $form->{obsolete}    *= 1;
532   $form->{business}    *= 1;
533   $form->{payment_id}    *= 1;
534   $form->{language_id}    *= 1;
535   $form->{taxzone_id}    *= 1;
536   $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
537
538   my $query;
539
540   if ($form->{id}) {
541     $query = qq|DELETE FROM vendortax
542                 WHERE vendor_id = $form->{id}|;
543     $dbh->do($query) || $form->dberror($query);
544
545     $query = qq|DELETE FROM shipto
546                 WHERE trans_id = $form->{id} AND module = 'CT'|;
547     $dbh->do($query) || $form->dberror($query);
548   } else {
549     my $uid = time;
550     $uid .= $form->{login};
551     my $uid = rand() . time;
552     $uid .= $form->{login};
553     $uid = substr($uid, 2, 75);
554     $query = qq|INSERT INTO vendor (name)
555                 VALUES ('$uid')|;
556     $dbh->do($query) || $form->dberror($query);
557
558     $query = qq|SELECT v.id FROM vendor v
559                 WHERE v.name = '$uid'|;
560     $sth = $dbh->prepare($query);
561     $sth->execute || $form->dberror($query);
562
563     ($form->{id}) = $sth->fetchrow_array;
564     $sth->finish;
565     if (!$form->{vendornumber}) {
566       $form->{vendornumber} =
567         $form->update_defaults($myconfig, "vendornumber");
568     }
569
570   }
571
572 ##LINET
573   $query = qq|UPDATE vendor SET
574               vendornumber = '$form->{vendornumber}',
575               name = '$form->{name}',
576               greeting = '$form->{greeting}',
577               department_1 = '$form->{department_1}',
578               department_2 = '$form->{department_2}',
579               street = '$form->{street}',
580               zipcode = '$form->{zipcode}',
581               city = '$form->{city}',
582               country = '$form->{country}',
583               homepage = '$form->{homepage}',
584               contact = '$form->{contact}',
585               phone = '$form->{phone}',
586               fax = '$form->{fax}',
587               email = '$form->{email}',
588               cc = '$form->{cc}',
589               bcc = '$form->{bcc}',
590               notes = '$form->{notes}',
591               terms = $form->{terms},
592               discount = $form->{discount},
593               creditlimit = $form->{creditlimit},
594               taxincluded = '$form->{taxincluded}',
595               gifi_accno = '$form->{gifi_accno}',
596               business_id = $form->{business},
597               taxnumber = '$form->{taxnumber}',
598               sic_code = '$form->{sic}',
599               language = '$form->{language}',
600               account_number = '$form->{account_number}',
601               bank_code = '$form->{bank_code}',
602               bank = '$form->{bank}',
603               obsolete = '$form->{obsolete}',
604               ustid = '$form->{ustid}',
605               payment_id = '$form->{payment_id}',
606               taxzone_id = '$form->{taxzone_id}',
607               language_id = '$form->{language_id}',
608               username = '$form->{username}',
609               user_password = '$form->{user_password}',
610               v_customer_id = '$form->{v_customer_id}'
611               WHERE id = $form->{id}|;
612   $dbh->do($query) || $form->dberror($query);
613
614   if ($form->{cp_id}) {
615     $query = qq|UPDATE contacts SET
616                 cp_greeting = '$form->{cp_greeting}',
617                 cp_title = '$form->{cp_title}',
618                 cp_givenname = '$form->{cp_givenname}',
619                 cp_name = '$form->{cp_name}',
620                 cp_email = '$form->{cp_email}',
621                 cp_phone1 = '$form->{cp_phone1}',
622                 cp_phone2 = '$form->{cp_phone2}'
623                 WHERE cp_id = $form->{cp_id}|;
624   } elsif ($form->{cp_name} || $form->{cp_givenname}) {
625     $query =
626       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2)
627                   VALUES ($form->{id}, '$form->{cp_greeting}','$form->{cp_title}','$form->{cp_givenname}','$form->{cp_name}','$form->{cp_email}','$form->{cp_phone1}','$form->{cp_phone2}')|;
628   }
629   $dbh->do($query) || $form->dberror($query);
630
631   # save taxes
632   foreach $item (split / /, $form->{taxaccounts}) {
633     if ($form->{"tax_$item"}) {
634       $query = qq|INSERT INTO vendortax (vendor_id, chart_id)
635                   VALUES ($form->{id}, (SELECT c.id
636                                         FROM chart c
637                                         WHERE c.accno = '$item'))|;
638       $dbh->do($query) || $form->dberror($query);
639     }
640   }
641
642   # add shipto
643   $form->add_shipto($dbh, $form->{id});
644
645   $rc = $dbh->disconnect;
646
647   $main::lxdebug->leave_sub();
648   return $rc;
649 }
650
651 sub delete {
652   $main::lxdebug->enter_sub();
653
654   my ($self, $myconfig, $form) = @_;
655
656   # connect to database
657   my $dbh = $form->dbconnect($myconfig);
658
659   # delete vendor
660   my $query = qq|DELETE FROM $form->{db}
661                  WHERE id = $form->{id}|;
662   $dbh->do($query) || $form->dberror($query);
663
664   $dbh->disconnect;
665
666   $main::lxdebug->leave_sub();
667 }
668
669 sub search {
670   $main::lxdebug->enter_sub();
671
672   my ($self, $myconfig, $form) = @_;
673
674   # connect to database
675   my $dbh = $form->dbconnect($myconfig);
676
677   my $where = "1 = 1";
678   $form->{sort} = "name" unless ($form->{sort});
679
680   if ($form->{"$form->{db}number"}) {
681     my $companynumber = $form->like(lc $form->{"$form->{db}number"});
682     $where .= " AND lower(ct.$form->{db}number) LIKE '$companynumber'";
683   }
684   if ($form->{name}) {
685     my $name = $form->like(lc $form->{name});
686     $where .= " AND lower(ct.name) LIKE '$name'";
687   }
688   if ($form->{contact}) {
689     my $contact = $form->like(lc $form->{contact});
690     $where .= " AND lower(ct.contact) LIKE '$contact'";
691   }
692   if ($form->{email}) {
693     my $email = $form->like(lc $form->{email});
694     $where .= " AND lower(ct.email) LIKE '$email'";
695   }
696
697   if ($form->{status} eq 'orphaned') {
698     $where .= qq| AND ct.id NOT IN (SELECT o.$form->{db}_id
699                                     FROM oe o, $form->{db} cv
700                                     WHERE cv.id = o.$form->{db}_id)|;
701     if ($form->{db} eq 'customer') {
702       $where .= qq| AND ct.id NOT IN (SELECT a.customer_id
703                                       FROM ar a, customer cv
704                                       WHERE cv.id = a.customer_id)|;
705     }
706     if ($form->{db} eq 'vendor') {
707       $where .= qq| AND ct.id NOT IN (SELECT a.vendor_id
708                                       FROM ap a, vendor cv
709                                       WHERE cv.id = a.vendor_id)|;
710     }
711     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
712   }
713
714   my $query = qq|SELECT ct.*, b.description AS business
715                  FROM $form->{db} ct
716               LEFT JOIN business b ON (ct.business_id = b.id)
717                  WHERE $where|;
718
719   # redo for invoices, orders and quotations
720   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
721
722     my ($ar, $union, $module);
723     $query = "";
724
725     if ($form->{l_invnumber}) {
726       $ar     = ($form->{db} eq 'customer') ? 'ar' : 'ap';
727       $module = ($ar         eq 'ar')       ? 'is' : 'ir';
728
729       $query = qq|SELECT ct.*, b.description AS business,
730                   a.invnumber, a.ordnumber, a.quonumber, a.id AS invid,
731                   '$module' AS module, 'invoice' AS formtype,
732                   (a.amount = a.paid) AS closed
733                   FROM $form->{db} ct
734                 JOIN $ar a ON (a.$form->{db}_id = ct.id)
735                 LEFT JOIN business b ON (ct.business_id = b.id)
736                   WHERE $where
737                   AND a.invoice = '1'|;
738
739       $union = qq|
740               UNION|;
741
742     }
743
744     if ($form->{l_ordnumber}) {
745       $query .= qq|$union
746                   SELECT ct.*, b.description AS business,
747                   ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
748                   'oe' AS module, 'order' AS formtype,
749                   o.closed
750                   FROM $form->{db} ct
751                 JOIN oe o ON (o.$form->{db}_id = ct.id)
752                 LEFT JOIN business b ON (ct.business_id = b.id)
753                   WHERE $where
754                   AND o.quotation = '0'|;
755
756       $union = qq|
757               UNION|;
758     }
759
760     if ($form->{l_quonumber}) {
761       $query .= qq|$union
762                   SELECT ct.*, b.description AS business,
763                   ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
764                   'oe' AS module, 'quotation' AS formtype,
765                   o.closed
766                   FROM $form->{db} ct
767                 JOIN oe o ON (o.$form->{db}_id = ct.id)
768                 LEFT JOIN business b ON (ct.business_id = b.id)
769                   WHERE $where
770                   AND o.quotation = '1'|;
771
772     }
773   }
774
775   $query .= qq|
776                  ORDER BY $form->{sort}|;
777
778   my $sth = $dbh->prepare($query);
779   $sth->execute || $form->dberror($query);
780 ##LINET
781   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
782     $ref->{address} = "";
783     map { $ref->{address} .= "$ref->{$_} "; } qw(street zipcode city country);
784     push @{ $form->{CT} }, $ref;
785   }
786 ##/LINET
787   $sth->finish;
788   $dbh->disconnect;
789
790   $main::lxdebug->leave_sub();
791 }
792
793 sub get_contact {
794   $main::lxdebug->enter_sub();
795
796   my ($self, $myconfig, $form) = @_;
797   my $dbh   = $form->dbconnect($myconfig);
798   my $query = qq|SELECT c.*
799                  FROM contacts c
800                  WHERE c.cp_id = $form->{cp_id}  order by c.cp_id limit 1|;
801   my $sth = $dbh->prepare($query);
802   $sth->execute || $form->dberror($query);
803
804   my $ref = $sth->fetchrow_hashref(NAME_lc);
805
806   map { $form->{$_} = $ref->{$_} } keys %$ref;
807
808   $sth->finish;
809   $dbh->disconnect;
810
811   $main::lxdebug->leave_sub();
812 }
813
814
815 sub get_shipto {
816   $main::lxdebug->enter_sub();
817
818   my ($self, $myconfig, $form) = @_;
819   my $dbh   = $form->dbconnect($myconfig);
820   my $query = qq|SELECT s.*
821                  FROM shipto s
822                  WHERE s.id = $form->{shipto_id}  order by s.id limit 1|;
823   my $sth = $dbh->prepare($query);
824   $sth->execute || $form->dberror($query);
825
826   my $ref = $sth->fetchrow_hashref(NAME_lc);
827
828   map { $form->{$_} = $ref->{$_} } keys %$ref;
829
830   $sth->finish;
831   $dbh->disconnect;
832
833   $main::lxdebug->leave_sub();
834 }
835
836 sub get_delivery {
837   $main::lxdebug->enter_sub();
838
839   my ($self, $myconfig, $form) = @_;
840   my $dbh   = $form->dbconnect($myconfig);
841   $where = " WHERE 1=1 ";
842   if ($form->{shipto_id}) {
843     $where .= "AND ar.shipto_id=$form->{shipto_id} ";
844   }
845   if ($form->{from}) {
846     $where .= "AND ar.transdate >= '$form->{from}' ";
847   }
848   if ($form->{to}) {
849     $where .= "AND ar.transdate <= '$form->{to}' ";
850   }
851
852   my $query = qq|select shiptoname, adr_code, ar.transdate, ar.invnumber, ar.ordnumber, invoice.description, qty, invoice.unit FROM ar LEFT join shipto ON (ar.shipto_id=shipto.id) LEFT join invoice on (ar.id=invoice.trans_id) LEFT join parts ON (parts.id=invoice.parts_id) LEFT join adr ON (parts.adr_id=adr.id) $where ORDER BY ar.transdate DESC LIMIT 15|;
853   my $sth = $dbh->prepare($query);
854   $sth->execute || $form->dberror($query);
855
856
857   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
858     push @{ $form->{DELIVERY} }, $ref;
859   }
860   $sth->finish;
861   $dbh->disconnect;
862
863   $main::lxdebug->leave_sub();
864 }
865
866 sub adr {
867   $main::lxdebug->enter_sub();
868
869   my ($self, $myconfig, $form) = @_;
870   my $dbh   = $form->dbconnect($myconfig);
871   $where = " WHERE 1=1 ";
872   if ($form->{from}) {
873     $where .= "AND ar.transdate >= '$form->{from}' ";
874   }
875   if ($form->{to}) {
876     $where .= "AND ar.transdate <= '$form->{to}' ";
877   }
878   if ($form->{year}) {
879     $where = " WHERE ar.transdate >= '$form->{year}-01-01' AND ar.transdate <= '$form->{year}-12-31' ";
880   }
881
882   my $query = qq|select adr_code, adr_description, sum(base_qty), parts.unit from ar LEFT join invoice on (ar.id=invoice.trans_id) LEFT join parts ON (invoice.parts_id=parts.id) LEFT join adr ON (adr.id=parts.adr_id) $where GROUP BY adr_code,adr_description,parts.unit|;
883   my $sth = $dbh->prepare($query);
884   $sth->execute || $form->dberror($query);
885
886
887   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
888     push @{ $form->{ADR} }, $ref;
889   }
890   $sth->finish;
891   $dbh->disconnect;
892
893   $main::lxdebug->leave_sub();
894 }
895
896 1;
897