Alle Dateien durch Perltidy laufen lassen. Die verwendeten Optionen sind am Ende...
[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
40 sub get_tuple {
41   $main::lxdebug->enter_sub();
42
43   my ($self, $myconfig, $form) = @_;
44
45   my $dbh   = $form->dbconnect($myconfig);
46   my $query = qq|SELECT ct.*, b.id AS business, s.*, cp.*
47                  FROM $form->{db} ct
48                  LEFT JOIN business b on ct.business_id = b.id
49                  LEFT JOIN shipto s on ct.id = s.trans_id
50                  LEFT JOIN contacts cp on ct.id = cp.cp_cv_id
51                  WHERE ct.id = $form->{id}  order by cp.cp_id limit 1|;
52   my $sth = $dbh->prepare($query);
53   $sth->execute || $form->dberror($query);
54
55   my $ref = $sth->fetchrow_hashref(NAME_lc);
56
57   map { $form->{$_} = $ref->{$_} } keys %$ref;
58
59   $sth->finish;
60   if ($form->{salesman_id}) {
61     my $query = qq|SELECT ct.name AS salesman
62                   FROM $form->{db} ct
63                   WHERE ct.id = $form->{salesman_id}|;
64     my $sth = $dbh->prepare($query);
65     $sth->execute || $form->dberror($query);
66
67     my ($ref) = $sth->fetchrow_array();
68
69     $form->{salesman} = $ref;
70
71     $sth->finish;
72   }
73
74   # check if it is orphaned
75   my $arap = ($form->{db} eq 'customer') ? "ar" : "ap";
76   $query = qq|SELECT a.id
77               FROM $arap a
78               JOIN $form->{db} ct ON (a.$form->{db}_id = ct.id)
79               WHERE ct.id = $form->{id}
80             UNION
81               SELECT a.id
82               FROM oe a
83               JOIN $form->{db} ct ON (a.$form->{db}_id = ct.id)
84               WHERE ct.id = $form->{id}|;
85   $sth = $dbh->prepare($query);
86   $sth->execute || $form->dberror($query);
87
88   unless ($sth->fetchrow_array) {
89     $form->{status} = "orphaned";
90   }
91   $sth->finish;
92
93   # get tax labels
94   $query = qq|SELECT c.accno, c.description
95               FROM chart c
96               JOIN tax t ON (t.chart_id = c.id)
97               WHERE c.link LIKE '%CT_tax%'
98               ORDER BY c.accno|;
99   $sth = $dbh->prepare($query);
100   $sth->execute || $form->dberror($query);
101
102   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
103     $form->{taxaccounts} .= "$ref->{accno} ";
104     $form->{tax}{ $ref->{accno} }{description} = $ref->{description};
105   }
106   $sth->finish;
107   chop $form->{taxaccounts};
108
109   # get taxes for customer/vendor
110   $query = qq|SELECT c.accno
111               FROM chart c
112               JOIN $form->{db}tax t ON (t.chart_id = c.id)
113               WHERE t.$form->{db}_id = $form->{id}|;
114   $sth = $dbh->prepare($query);
115   $sth->execute || $form->dberror($query);
116
117   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
118     $form->{tax}{ $ref->{accno} }{taxable} = 1;
119   }
120   $sth->finish;
121
122   # get business types
123   $query = qq|SELECT id, description
124               FROM business
125               ORDER BY 1|;
126   $sth = $dbh->prepare($query);
127   $sth->execute || $form->dberror($query);
128
129   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
130     push @{ $form->{all_business} }, $ref;
131   }
132   $sth->finish;
133
134   $dbh->disconnect;
135
136   $main::lxdebug->leave_sub();
137 }
138
139 ## LINET
140 sub query_titles_and_greetings {
141   $main::lxdebug->enter_sub();
142
143   my ($self, $myconfig, $form) = @_;
144   my (%tmp,  $ref);
145
146   my $dbh = $form->dbconnect($myconfig);
147
148   $query =
149     "SELECT DISTINCT(c.cp_greeting) FROM contacts c WHERE c.cp_greeting LIKE '%'";
150   $sth = $dbh->prepare($query);
151   $sth->execute() || $form->dberror($query);
152   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
153     next unless ($ref->{cp_greeting} =~ /[a-zA-Z]/);
154     $tmp{ $ref->{cp_greeting} } = 1;
155   }
156   $sth->finish();
157
158   @{ $form->{GREETINGS} } = sort(keys(%tmp));
159
160   %tmp = ();
161
162   $query =
163     "SELECT DISTINCT(c.cp_title) FROM contacts c WHERE c.cp_title LIKE '%'";
164   $sth = $dbh->prepare($query);
165   $sth->execute() || $form->dberror($query);
166   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
167     next unless ($ref->{cp_title} =~ /[a-zA-Z]/);
168     $tmp{ $ref->{cp_title} } = 1;
169   }
170   $sth->finish();
171
172   @{ $form->{TITLES} } = sort(keys(%tmp));
173
174   $dbh->disconnect();
175   $main::lxdebug->leave_sub();
176 }
177 ## /LINET
178
179 sub taxaccounts {
180   $main::lxdebug->enter_sub();
181
182   my ($self, $myconfig, $form) = @_;
183
184   my $dbh = $form->dbconnect($myconfig);
185
186   # get tax labels
187   my $query = qq|SELECT accno, description
188                  FROM chart c, tax t
189                  WHERE c.link LIKE '%CT_tax%'
190                  AND c.id = t.chart_id
191                  ORDER BY accno|;
192   $sth = $dbh->prepare($query);
193   $sth->execute || $form->dberror($query);
194
195   my $ref = ();
196   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
197     $form->{taxaccounts} .= "$ref->{accno} ";
198     $form->{tax}{ $ref->{accno} }{description} = $ref->{description};
199   }
200   $sth->finish;
201   chop $form->{taxaccounts};
202
203   # this is just for the selection for type of business
204   $query = qq|SELECT id, description
205               FROM business|;
206   $sth = $dbh->prepare($query);
207   $sth->execute || $form->dberror($query);
208
209   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
210     push @{ $form->{all_business} }, $ref;
211   }
212   $sth->finish;
213
214   $dbh->disconnect;
215
216   $main::lxdebug->leave_sub();
217 }
218
219 sub save_customer {
220   $main::lxdebug->enter_sub();
221
222   my ($self, $myconfig, $form) = @_;
223
224   # connect to database
225   my $dbh = $form->dbconnect($myconfig);
226 ##LINET
227   map({ $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
228           if ($form->{"selected_cp_${_}"});
229   } qw(title greeting));
230
231   # escape '
232   map { $form->{$_} =~ s/\'/\'\'/g }
233     qw(customernumber name street zipcode city country homepage contact notes cp_title cp_greeting language);
234 ##/LINET
235   # assign value discount, terms, creditlimit
236   $form->{discount} = $form->parse_amount($myconfig, $form->{discount});
237   $form->{discount} /= 100;
238   $form->{terms}       *= 1;
239   $form->{taxincluded} *= 1;
240   $form->{obsolete}    *= 1;
241   $form->{business}    *= 1;
242   $form->{salesman_id} *= 1;
243   $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
244
245   my ($query, $sth);
246
247   if ($form->{id}) {
248     $query = qq|DELETE FROM customertax
249                 WHERE customer_id = $form->{id}|;
250     $dbh->do($query) || $form->dberror($query);
251
252     $query = qq|DELETE FROM shipto
253                 WHERE trans_id = $form->{id}|;
254     $dbh->do($query) || $form->dberror($query);
255   } else {
256     my $uid = rand() . time;
257
258     $uid .= $form->{login};
259
260     $uid = substr($uid, 2, 75);
261
262     $query = qq|INSERT INTO customer (name)
263                 VALUES ('$uid')|;
264     $dbh->do($query) || $form->dberror($query);
265
266     $query = qq|SELECT c.id FROM customer c
267                 WHERE c.name = '$uid'|;
268     $sth = $dbh->prepare($query);
269     $sth->execute || $form->dberror($query);
270
271     ($form->{id}) = $sth->fetchrow_array;
272     $sth->finish;
273     if (!$form->{customernumber} && $form->{business}) {
274       $form->{customernumber} =
275         $form->update_business($myconfig, $form->{business});
276     }
277     if (!$form->{customernumber}) {
278       $form->{customernumber} =
279         $form->update_defaults($myconfig, "customernumber");
280     }
281
282   }
283
284   $query = qq|UPDATE customer SET
285               customernumber = '$form->{customernumber}',
286               name = '$form->{name}',
287               department_1 = '$form->{department_1}',
288               department_2 = '$form->{department_2}',
289               street = '$form->{street}',
290               zipcode = '$form->{zipcode}',
291               city = '$form->{city}',
292               country = '$form->{country}',
293               homepage = '$form->{homepage}',
294               contact = '$form->{contact}',
295               phone = '$form->{phone}',
296               fax = '$form->{fax}',
297               email = '$form->{email}',
298               cc = '$form->{cc}',
299               bcc = '$form->{bcc}',
300               notes = '$form->{notes}',
301               discount = $form->{discount},
302               creditlimit = $form->{creditlimit},
303               terms = $form->{terms},
304               taxincluded = '$form->{taxincluded}',
305               business_id = $form->{business},
306               taxnumber = '$form->{taxnumber}',
307               sic_code = '$form->{sic}',
308               language = '$form->{language}',
309               account_number = '$form->{account_number}',
310               bank_code = '$form->{bank_code}',
311               bank = '$form->{bank}',
312               obsolete = '$form->{obsolete}',
313               ustid = '$form->{ustid}',
314               username = '$form->{username}',
315               salesman_id = '$form->{salesman_id}',
316               user_password = '$form->{user_password}',
317               c_vendor_id = '$form->{c_vendor_id}'
318               WHERE id = $form->{id}|;
319   $dbh->do($query) || $form->dberror($query);
320
321   if ($form->{cp_id}) {
322     $query = qq|UPDATE contacts SET
323                 cp_greeting = '$form->{cp_greeting}',
324                 cp_title = '$form->{cp_title}',
325                 cp_givenname = '$form->{cp_givenname}',
326                 cp_name = '$form->{cp_name}',
327                 cp_email = '$form->{cp_email}',
328                 cp_phone1 = '$form->{cp_phone1}',
329                 cp_phone2 = '$form->{cp_phone2}'
330                 WHERE cp_id = $form->{cp_id}|;
331   } elsif ($form->{cp_name} || $form->{cp_givenname}) {
332     $query =
333       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2)
334                   VALUES ($form->{id}, '$form->{cp_greeting}','$form->{cp_title}','$form->{cp_givenname}','$form->{cp_name}','$form->{cp_email}','$form->{cp_phone1}','$form->{cp_phone2}')|;
335   }
336   $dbh->do($query) || $form->dberror($query);
337
338   # save taxes
339   foreach $item (split / /, $form->{taxaccounts}) {
340     if ($form->{"tax_$item"}) {
341       $query = qq|INSERT INTO customertax (customer_id, chart_id)
342                   VALUES ($form->{id}, (SELECT c.id
343                                         FROM chart c
344                                         WHERE c.accno = '$item'))|;
345       $dbh->do($query) || $form->dberror($query);
346     }
347   }
348
349   # add shipto
350   $form->add_shipto($dbh, $form->{id});
351
352   $dbh->disconnect;
353
354   $main::lxdebug->leave_sub();
355 }
356
357 sub save_vendor {
358   $main::lxdebug->enter_sub();
359
360   my ($self, $myconfig, $form) = @_;
361
362   # connect to database
363   my $dbh = $form->dbconnect($myconfig);
364 ##LINET
365   map({ $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
366           if ($form->{"selected_cp_${_}"});
367   } qw(title greeting));
368
369   # escape '
370   map { $form->{$_} =~ s/\'/\'\'/g }
371     qw(vendornumber name street zipcode city country homepage contact notes cp_title cp_greeting language);
372 ##/LINET
373   $form->{discount} = $form->parse_amount($myconfig, $form->{discount});
374   $form->{discount} /= 100;
375   $form->{terms}       *= 1;
376   $form->{taxincluded} *= 1;
377   $form->{obsolete}    *= 1;
378   $form->{business}    *= 1;
379   $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
380
381   my $query;
382
383   if ($form->{id}) {
384     $query = qq|DELETE FROM vendortax
385                 WHERE vendor_id = $form->{id}|;
386     $dbh->do($query) || $form->dberror($query);
387
388     $query = qq|DELETE FROM shipto
389                 WHERE trans_id = $form->{id}|;
390     $dbh->do($query) || $form->dberror($query);
391   } else {
392     my $uid = time;
393     $uid .= $form->{login};
394
395     $query = qq|INSERT INTO vendor (name)
396                 VALUES ('$uid')|;
397     $dbh->do($query) || $form->dberror($query);
398
399     $query = qq|SELECT v.id FROM vendor v
400                 WHERE v.name = '$uid'|;
401     $sth = $dbh->prepare($query);
402     $sth->execute || $form->dberror($query);
403
404     ($form->{id}) = $sth->fetchrow_array;
405     $sth->finish;
406     if (!$form->{vendornumber}) {
407       $form->{vendornumber} =
408         $form->update_defaults($myconfig, "vendornumber");
409     }
410
411   }
412
413 ##LINET
414   $query = qq|UPDATE vendor SET
415               vendornumber = '$form->{vendornumber}',
416               name = '$form->{name}',
417               department_1 = '$form->{department_1}',
418               department_2 = '$form->{department_2}',
419               street = '$form->{street}',
420               zipcode = '$form->{zipcode}',
421               city = '$form->{city}',
422               country = '$form->{country}',
423               homepage = '$form->{homepage}',
424               contact = '$form->{contact}',
425               phone = '$form->{phone}',
426               fax = '$form->{fax}',
427               email = '$form->{email}',
428               cc = '$form->{cc}',
429               bcc = '$form->{bcc}',
430               notes = '$form->{notes}',
431               terms = $form->{terms},
432               discount = $form->{discount},
433               creditlimit = $form->{creditlimit},
434               taxincluded = '$form->{taxincluded}',
435               gifi_accno = '$form->{gifi_accno}',
436               business_id = $form->{business},
437               taxnumber = '$form->{taxnumber}',
438               sic_code = '$form->{sic}',
439               language = '$form->{language}',
440               account_number = '$form->{account_number}',
441               bank_code = '$form->{bank_code}',
442               bank = '$form->{bank}',
443               obsolete = '$form->{obsolete}',
444               ustid = '$form->{ustid}',
445               username = '$form->{username}',
446               user_password = '$form->{user_password}',
447               v_customer_id = '$form->{v_customer_id}'
448               WHERE id = $form->{id}|;
449   $dbh->do($query) || $form->dberror($query);
450
451   if ($form->{cp_id}) {
452     $query = qq|UPDATE contacts SET
453                 cp_greeting = '$form->{cp_greeting}',
454                 cp_title = '$form->{cp_title}',
455                 cp_givenname = '$form->{cp_givenname}',
456                 cp_name = '$form->{cp_name}',
457                 cp_email = '$form->{cp_email}',
458                 cp_phone1 = '$form->{cp_phone1}',
459                 cp_phone2 = '$form->{cp_phone2}'
460                 WHERE cp_id = $form->{cp_id}|;
461   } elsif ($form->{cp_name} || $form->{cp_givenname}) {
462     $query =
463       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2)
464                   VALUES ($form->{id}, '$form->{cp_greeting}','$form->{cp_title}','$form->{cp_givenname}','$form->{cp_name}','$form->{cp_email}','$form->{cp_phone1}','$form->{cp_phone2}')|;
465   }
466   $dbh->do($query) || $form->dberror($query);
467
468   # save taxes
469   foreach $item (split / /, $form->{taxaccounts}) {
470     if ($form->{"tax_$item"}) {
471       $query = qq|INSERT INTO vendortax (vendor_id, chart_id)
472                   VALUES ($form->{id}, (SELECT c.id
473                                         FROM chart c
474                                         WHERE c.accno = '$item'))|;
475       $dbh->do($query) || $form->dberror($query);
476     }
477   }
478
479   # add shipto
480   $form->add_shipto($dbh, $form->{id});
481
482   $dbh->disconnect;
483
484   $main::lxdebug->leave_sub();
485 }
486
487 sub delete {
488   $main::lxdebug->enter_sub();
489
490   my ($self, $myconfig, $form) = @_;
491
492   # connect to database
493   my $dbh = $form->dbconnect($myconfig);
494
495   # delete vendor
496   my $query = qq|DELETE FROM $form->{db}
497                  WHERE id = $form->{id}|;
498   $dbh->do($query) || $form->dberror($query);
499
500   $dbh->disconnect;
501
502   $main::lxdebug->leave_sub();
503 }
504
505 sub search {
506   $main::lxdebug->enter_sub();
507
508   my ($self, $myconfig, $form) = @_;
509
510   # connect to database
511   my $dbh = $form->dbconnect($myconfig);
512
513   my $where = "1 = 1";
514   $form->{sort} = "name" unless ($form->{sort});
515
516   if ($form->{"$form->{db}number"}) {
517     my $companynumber = $form->like(lc $form->{"$form->{db}number"});
518     $where .= " AND lower(ct.$form->{db}number) LIKE '$companynumber'";
519   }
520   if ($form->{name}) {
521     my $name = $form->like(lc $form->{name});
522     $where .= " AND lower(ct.name) LIKE '$name'";
523   }
524   if ($form->{contact}) {
525     my $contact = $form->like(lc $form->{contact});
526     $where .= " AND lower(ct.contact) LIKE '$contact'";
527   }
528   if ($form->{email}) {
529     my $email = $form->like(lc $form->{email});
530     $where .= " AND lower(ct.email) LIKE '$email'";
531   }
532
533   if ($form->{status} eq 'orphaned') {
534     $where .= qq| AND ct.id NOT IN (SELECT o.$form->{db}_id
535                                     FROM oe o, $form->{db} cv
536                                     WHERE cv.id = o.$form->{db}_id)|;
537     if ($form->{db} eq 'customer') {
538       $where .= qq| AND ct.id NOT IN (SELECT a.customer_id
539                                       FROM ar a, customer cv
540                                       WHERE cv.id = a.customer_id)|;
541     }
542     if ($form->{db} eq 'vendor') {
543       $where .= qq| AND ct.id NOT IN (SELECT a.vendor_id
544                                       FROM ap a, vendor cv
545                                       WHERE cv.id = a.vendor_id)|;
546     }
547     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
548   }
549
550   my $query = qq|SELECT ct.*, b.description AS business
551                  FROM $form->{db} ct
552               LEFT JOIN business b ON (ct.business_id = b.id)
553                  WHERE $where|;
554
555   # redo for invoices, orders and quotations
556   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
557
558     my ($ar, $union, $module);
559     $query = "";
560
561     if ($form->{l_invnumber}) {
562       $ar     = ($form->{db} eq 'customer') ? 'ar' : 'ap';
563       $module = ($ar         eq 'ar')       ? 'is' : 'ir';
564
565       $query = qq|SELECT ct.*, b.description AS business,
566                   a.invnumber, a.ordnumber, a.quonumber, a.id AS invid,
567                   '$module' AS module, 'invoice' AS formtype,
568                   (a.amount = a.paid) AS closed
569                   FROM $form->{db} ct
570                 JOIN $ar a ON (a.$form->{db}_id = ct.id)
571                 LEFT JOIN business b ON (ct.business_id = b.id)
572                   WHERE $where
573                   AND a.invoice = '1'|;
574
575       $union = qq|
576               UNION|;
577
578     }
579
580     if ($form->{l_ordnumber}) {
581       $query .= qq|$union
582                   SELECT ct.*, b.description AS business,
583                   ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
584                   'oe' AS module, 'order' AS formtype,
585                   o.closed
586                   FROM $form->{db} ct
587                 JOIN oe o ON (o.$form->{db}_id = ct.id)
588                 LEFT JOIN business b ON (ct.business_id = b.id)
589                   WHERE $where
590                   AND o.quotation = '0'|;
591
592       $union = qq|
593               UNION|;
594     }
595
596     if ($form->{l_quonumber}) {
597       $query .= qq|$union
598                   SELECT ct.*, b.description AS business,
599                   ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
600                   'oe' AS module, 'quotation' AS formtype,
601                   o.closed
602                   FROM $form->{db} ct
603                 JOIN oe o ON (o.$form->{db}_id = ct.id)
604                 LEFT JOIN business b ON (ct.business_id = b.id)
605                   WHERE $where
606                   AND o.quotation = '1'|;
607
608     }
609   }
610
611   $query .= qq|
612                  ORDER BY $form->{sort}|;
613
614   my $sth = $dbh->prepare($query);
615   $sth->execute || $form->dberror($query);
616 ##LINET
617   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
618     $ref->{address} = "";
619     map { $ref->{address} .= "$ref->{$_} "; } qw(street zipcode city country);
620     push @{ $form->{CT} }, $ref;
621   }
622 ##/LINET
623   $sth->finish;
624   $dbh->disconnect;
625
626   $main::lxdebug->leave_sub();
627 }
628
629 1;
630