Kosmetik: Perltidy-Lauf nach den Einstellungen in doc/programmierrichtlinien.txt...
[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   # set pricegroup to default
225   if ($form->{klass}) { }
226   else { $form->{klass} = 0; }
227
228   # connect to database
229   my $dbh = $form->dbconnect($myconfig);
230 ##LINET
231   map({
232       $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
233         if ($form->{"selected_cp_${_}"});
234   } qw(title greeting));
235
236   #
237   # escape '
238   map { $form->{$_} =~ s/\'/\'\'/g }
239     qw(customernumber name street zipcode city country homepage contact notes cp_title cp_greeting language pricegroup);
240 ##/LINET
241   # assign value discount, terms, creditlimit
242   $form->{discount} = $form->parse_amount($myconfig, $form->{discount});
243   $form->{discount} /= 100;
244   $form->{terms}       *= 1;
245   $form->{taxincluded} *= 1;
246   $form->{obsolete}    *= 1;
247   $form->{business}    *= 1;
248   $form->{salesman_id} *= 1;
249   $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
250
251   my ($query, $sth);
252
253   if ($form->{id}) {
254     $query = qq|DELETE FROM customertax
255                 WHERE customer_id = $form->{id}|;
256     $dbh->do($query) || $form->dberror($query);
257
258     $query = qq|DELETE FROM shipto
259                 WHERE trans_id = $form->{id}|;
260     $dbh->do($query) || $form->dberror($query);
261   } else {
262     my $uid = rand() . time;
263
264     $uid .= $form->{login};
265
266     $uid = substr($uid, 2, 75);
267
268     $query = qq|INSERT INTO customer (name)
269                 VALUES ('$uid')|;
270     $dbh->do($query) || $form->dberror($query);
271
272     $query = qq|SELECT c.id FROM customer c
273                 WHERE c.name = '$uid'|;
274     $sth = $dbh->prepare($query);
275     $sth->execute || $form->dberror($query);
276
277     ($form->{id}) = $sth->fetchrow_array;
278     $sth->finish;
279     if (!$form->{customernumber} && $form->{business}) {
280       $form->{customernumber} =
281         $form->update_business($myconfig, $form->{business});
282     }
283     if (!$form->{customernumber}) {
284       $form->{customernumber} =
285         $form->update_defaults($myconfig, "customernumber");
286     }
287
288   }
289
290   $query = qq|UPDATE customer SET
291               customernumber = '$form->{customernumber}',
292               name = '$form->{name}',
293               department_1 = '$form->{department_1}',
294               department_2 = '$form->{department_2}',
295               street = '$form->{street}',
296               zipcode = '$form->{zipcode}',
297               city = '$form->{city}',
298               country = '$form->{country}',
299               homepage = '$form->{homepage}',
300               contact = '$form->{contact}',
301               phone = '$form->{phone}',
302               fax = '$form->{fax}',
303               email = '$form->{email}',
304               cc = '$form->{cc}',
305               bcc = '$form->{bcc}',
306               notes = '$form->{notes}',
307               discount = $form->{discount},
308               creditlimit = $form->{creditlimit},
309               terms = $form->{terms},
310               taxincluded = '$form->{taxincluded}',
311               business_id = $form->{business},
312               taxnumber = '$form->{taxnumber}',
313               sic_code = '$form->{sic}',
314               language = '$form->{language}',
315               account_number = '$form->{account_number}',
316               bank_code = '$form->{bank_code}',
317               bank = '$form->{bank}',
318               obsolete = '$form->{obsolete}',
319               ustid = '$form->{ustid}',
320               username = '$form->{username}',
321               salesman_id = '$form->{salesman_id}',
322               user_password = | . $dbh->quote($form->{user_password}) . qq|,
323               c_vendor_id = '$form->{c_vendor_id}',
324               klass = '$form->{klass}'
325               WHERE id = $form->{id}|;
326   $dbh->do($query) || $form->dberror($query);
327
328   if ($form->{cp_id}) {
329     $query = qq|UPDATE contacts SET
330                 cp_greeting = '$form->{cp_greeting}',
331                 cp_title = '$form->{cp_title}',
332                 cp_givenname = '$form->{cp_givenname}',
333                 cp_name = '$form->{cp_name}',
334                 cp_email = '$form->{cp_email}',
335                 cp_phone1 = '$form->{cp_phone1}',
336                 cp_phone2 = '$form->{cp_phone2}'
337                 WHERE cp_id = $form->{cp_id}|;
338   } elsif ($form->{cp_name} || $form->{cp_givenname}) {
339     $query =
340       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2)
341                   VALUES ($form->{id}, '$form->{cp_greeting}','$form->{cp_title}','$form->{cp_givenname}','$form->{cp_name}','$form->{cp_email}','$form->{cp_phone1}','$form->{cp_phone2}')|;
342   }
343   $dbh->do($query) || $form->dberror($query);
344
345   # save taxes
346   foreach $item (split / /, $form->{taxaccounts}) {
347     if ($form->{"tax_$item"}) {
348       $query = qq|INSERT INTO customertax (customer_id, chart_id)
349                   VALUES ($form->{id}, (SELECT c.id
350                                         FROM chart c
351                                         WHERE c.accno = '$item'))|;
352       $dbh->do($query) || $form->dberror($query);
353     }
354   }
355
356   # add shipto
357   $form->add_shipto($dbh, $form->{id});
358
359   $rc = $dbh->disconnect;
360
361   $main::lxdebug->leave_sub();
362   return $rc;
363 }
364
365 sub save_vendor {
366   $main::lxdebug->enter_sub();
367
368   my ($self, $myconfig, $form) = @_;
369
370   # connect to database
371   my $dbh = $form->dbconnect($myconfig);
372 ##LINET
373   map({
374       $form->{"cp_${_}"} = $form->{"selected_cp_${_}"}
375         if ($form->{"selected_cp_${_}"});
376   } qw(title greeting));
377
378   # escape '
379   map { $form->{$_} =~ s/\'/\'\'/g }
380     qw(vendornumber name street zipcode city country homepage contact notes cp_title cp_greeting language);
381 ##/LINET
382   $form->{discount} = $form->parse_amount($myconfig, $form->{discount});
383   $form->{discount} /= 100;
384   $form->{terms}       *= 1;
385   $form->{taxincluded} *= 1;
386   $form->{obsolete}    *= 1;
387   $form->{business}    *= 1;
388   $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
389
390   my $query;
391
392   if ($form->{id}) {
393     $query = qq|DELETE FROM vendortax
394                 WHERE vendor_id = $form->{id}|;
395     $dbh->do($query) || $form->dberror($query);
396
397     $query = qq|DELETE FROM shipto
398                 WHERE trans_id = $form->{id}|;
399     $dbh->do($query) || $form->dberror($query);
400   } else {
401     my $uid = time;
402     $uid .= $form->{login};
403     my $uid = rand() . time;
404     $uid .= $form->{login};
405     $uid = substr($uid, 2, 75);
406     $query = qq|INSERT INTO vendor (name)
407                 VALUES ('$uid')|;
408     $dbh->do($query) || $form->dberror($query);
409
410     $query = qq|SELECT v.id FROM vendor v
411                 WHERE v.name = '$uid'|;
412     $sth = $dbh->prepare($query);
413     $sth->execute || $form->dberror($query);
414
415     ($form->{id}) = $sth->fetchrow_array;
416     $sth->finish;
417     if (!$form->{vendornumber}) {
418       $form->{vendornumber} =
419         $form->update_defaults($myconfig, "vendornumber");
420     }
421
422   }
423
424 ##LINET
425   $query = qq|UPDATE vendor SET
426               vendornumber = '$form->{vendornumber}',
427               name = '$form->{name}',
428               department_1 = '$form->{department_1}',
429               department_2 = '$form->{department_2}',
430               street = '$form->{street}',
431               zipcode = '$form->{zipcode}',
432               city = '$form->{city}',
433               country = '$form->{country}',
434               homepage = '$form->{homepage}',
435               contact = '$form->{contact}',
436               phone = '$form->{phone}',
437               fax = '$form->{fax}',
438               email = '$form->{email}',
439               cc = '$form->{cc}',
440               bcc = '$form->{bcc}',
441               notes = '$form->{notes}',
442               terms = $form->{terms},
443               discount = $form->{discount},
444               creditlimit = $form->{creditlimit},
445               taxincluded = '$form->{taxincluded}',
446               gifi_accno = '$form->{gifi_accno}',
447               business_id = $form->{business},
448               taxnumber = '$form->{taxnumber}',
449               sic_code = '$form->{sic}',
450               language = '$form->{language}',
451               account_number = '$form->{account_number}',
452               bank_code = '$form->{bank_code}',
453               bank = '$form->{bank}',
454               obsolete = '$form->{obsolete}',
455               ustid = '$form->{ustid}',
456               username = '$form->{username}',
457               user_password = '$form->{user_password}',
458               v_customer_id = '$form->{v_customer_id}'
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                 WHERE cp_id = $form->{cp_id}|;
472   } elsif ($form->{cp_name} || $form->{cp_givenname}) {
473     $query =
474       qq|INSERT INTO contacts ( cp_cv_id, cp_greeting, cp_title, cp_givenname, cp_name, cp_email, cp_phone1, cp_phone2)
475                   VALUES ($form->{id}, '$form->{cp_greeting}','$form->{cp_title}','$form->{cp_givenname}','$form->{cp_name}','$form->{cp_email}','$form->{cp_phone1}','$form->{cp_phone2}')|;
476   }
477   $dbh->do($query) || $form->dberror($query);
478
479   # save taxes
480   foreach $item (split / /, $form->{taxaccounts}) {
481     if ($form->{"tax_$item"}) {
482       $query = qq|INSERT INTO vendortax (vendor_id, chart_id)
483                   VALUES ($form->{id}, (SELECT c.id
484                                         FROM chart c
485                                         WHERE c.accno = '$item'))|;
486       $dbh->do($query) || $form->dberror($query);
487     }
488   }
489
490   # add shipto
491   $form->add_shipto($dbh, $form->{id});
492
493   $rc = $dbh->disconnect;
494
495   $main::lxdebug->leave_sub();
496   return $rc;
497 }
498
499 sub delete {
500   $main::lxdebug->enter_sub();
501
502   my ($self, $myconfig, $form) = @_;
503
504   # connect to database
505   my $dbh = $form->dbconnect($myconfig);
506
507   # delete vendor
508   my $query = qq|DELETE FROM $form->{db}
509                  WHERE id = $form->{id}|;
510   $dbh->do($query) || $form->dberror($query);
511
512   $dbh->disconnect;
513
514   $main::lxdebug->leave_sub();
515 }
516
517 sub search {
518   $main::lxdebug->enter_sub();
519
520   my ($self, $myconfig, $form) = @_;
521
522   # connect to database
523   my $dbh = $form->dbconnect($myconfig);
524
525   my $where = "1 = 1";
526   $form->{sort} = "name" unless ($form->{sort});
527
528   if ($form->{"$form->{db}number"}) {
529     my $companynumber = $form->like(lc $form->{"$form->{db}number"});
530     $where .= " AND lower(ct.$form->{db}number) LIKE '$companynumber'";
531   }
532   if ($form->{name}) {
533     my $name = $form->like(lc $form->{name});
534     $where .= " AND lower(ct.name) LIKE '$name'";
535   }
536   if ($form->{contact}) {
537     my $contact = $form->like(lc $form->{contact});
538     $where .= " AND lower(ct.contact) LIKE '$contact'";
539   }
540   if ($form->{email}) {
541     my $email = $form->like(lc $form->{email});
542     $where .= " AND lower(ct.email) LIKE '$email'";
543   }
544
545   if ($form->{status} eq 'orphaned') {
546     $where .= qq| AND ct.id NOT IN (SELECT o.$form->{db}_id
547                                     FROM oe o, $form->{db} cv
548                                     WHERE cv.id = o.$form->{db}_id)|;
549     if ($form->{db} eq 'customer') {
550       $where .= qq| AND ct.id NOT IN (SELECT a.customer_id
551                                       FROM ar a, customer cv
552                                       WHERE cv.id = a.customer_id)|;
553     }
554     if ($form->{db} eq 'vendor') {
555       $where .= qq| AND ct.id NOT IN (SELECT a.vendor_id
556                                       FROM ap a, vendor cv
557                                       WHERE cv.id = a.vendor_id)|;
558     }
559     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
560   }
561
562   my $query = qq|SELECT ct.*, b.description AS business
563                  FROM $form->{db} ct
564               LEFT JOIN business b ON (ct.business_id = b.id)
565                  WHERE $where|;
566
567   # redo for invoices, orders and quotations
568   if ($form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
569
570     my ($ar, $union, $module);
571     $query = "";
572
573     if ($form->{l_invnumber}) {
574       $ar     = ($form->{db} eq 'customer') ? 'ar' : 'ap';
575       $module = ($ar         eq 'ar')       ? 'is' : 'ir';
576
577       $query = qq|SELECT ct.*, b.description AS business,
578                   a.invnumber, a.ordnumber, a.quonumber, a.id AS invid,
579                   '$module' AS module, 'invoice' AS formtype,
580                   (a.amount = a.paid) AS closed
581                   FROM $form->{db} ct
582                 JOIN $ar a ON (a.$form->{db}_id = ct.id)
583                 LEFT JOIN business b ON (ct.business_id = b.id)
584                   WHERE $where
585                   AND a.invoice = '1'|;
586
587       $union = qq|
588               UNION|;
589
590     }
591
592     if ($form->{l_ordnumber}) {
593       $query .= qq|$union
594                   SELECT ct.*, b.description AS business,
595                   ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
596                   'oe' AS module, 'order' AS formtype,
597                   o.closed
598                   FROM $form->{db} ct
599                 JOIN oe o ON (o.$form->{db}_id = ct.id)
600                 LEFT JOIN business b ON (ct.business_id = b.id)
601                   WHERE $where
602                   AND o.quotation = '0'|;
603
604       $union = qq|
605               UNION|;
606     }
607
608     if ($form->{l_quonumber}) {
609       $query .= qq|$union
610                   SELECT ct.*, b.description AS business,
611                   ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
612                   'oe' AS module, 'quotation' AS formtype,
613                   o.closed
614                   FROM $form->{db} ct
615                 JOIN oe o ON (o.$form->{db}_id = ct.id)
616                 LEFT JOIN business b ON (ct.business_id = b.id)
617                   WHERE $where
618                   AND o.quotation = '1'|;
619
620     }
621   }
622
623   $query .= qq|
624                  ORDER BY $form->{sort}|;
625
626   my $sth = $dbh->prepare($query);
627   $sth->execute || $form->dberror($query);
628 ##LINET
629   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
630     $ref->{address} = "";
631     map { $ref->{address} .= "$ref->{$_} "; } qw(street zipcode city country);
632     push @{ $form->{CT} }, $ref;
633   }
634 ##/LINET
635   $sth->finish;
636   $dbh->disconnect;
637
638   $main::lxdebug->leave_sub();
639 }
640
641 1;
642