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