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