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