Auswahl der Rechnungen für Mahnungen: Wenn das nächste Mahnlevel als Filter ausgewähl...
[kivitendo-erp.git] / SL / Common.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
9 package Common;
10
11 use Time::HiRes qw(gettimeofday);
12
13 use vars qw(@db_encodings %db_encoding_to_charset);
14
15 @db_encodings = (
16   { "label" => "ASCII",          "dbencoding" => "SQL_ASCII", "charset" => "ASCII" },
17   { "label" => "UTF-8 Unicode",  "dbencoding" => "UNICODE",   "charset" => "UTF-8" },
18   { "label" => "ISO 8859-1",     "dbencoding" => "LATIN1",    "charset" => "ISO-8859-1" },
19   { "label" => "ISO 8859-2",     "dbencoding" => "LATIN2",    "charset" => "ISO-8859-2" },
20   { "label" => "ISO 8859-3",     "dbencoding" => "LATIN3",    "charset" => "ISO-8859-3" },
21   { "label" => "ISO 8859-4",     "dbencoding" => "LATIN4",    "charset" => "ISO-8859-4" },
22   { "label" => "ISO 8859-5",     "dbencoding" => "LATIN5",    "charset" => "ISO-8859-5" },
23   { "label" => "ISO 8859-15",    "dbencoding" => "LATIN9",    "charset" => "ISO-8859-15" },
24   { "label" => "KOI8-R",         "dbencoding" => "KOI8",      "charset" => "KOI8-R" },
25   { "label" => "Windows CP1251", "dbencoding" => "WIN",       "charset" => "CP1251" },
26   { "label" => "Windows CP866",  "dbencoding" => "ALT",       "charset" => "CP866" },
27 );
28
29 %db_encoding_to_charset = map { $_->{dbencoding}, $_->{charset} } @db_encodings;
30
31 use constant DEFAULT_CHARSET => 'ISO-8859-15';
32
33 sub unique_id {
34   my ($a, $b) = gettimeofday();
35   return "${a}-${b}-${$}";
36 }
37
38 sub tmpname {
39   return "/tmp/lx-office-tmp-" . unique_id();
40 }
41
42 sub retrieve_parts {
43   $main::lxdebug->enter_sub();
44
45   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
46
47   my $dbh = $form->dbconnect($myconfig);
48
49   my (@filter_values, $filter);
50   if ($form->{"partnumber"}) {
51     $filter .= qq| AND (partnumber ILIKE ?)|;
52     push(@filter_values, '%' . $form->{"partnumber"} . '%');
53   }
54   if ($form->{"description"}) {
55     $filter .= qq| AND (description ILIKE ?)|;
56     push(@filter_values, '%' . $form->{"description"} . '%');
57   }
58   substr($filter, 1, 3) = "WHERE" if ($filter);
59
60   $order_by =~ s/[^a-zA-Z_]//g;
61   $order_dir = $order_dir ? "ASC" : "DESC";
62
63   my $query =
64     qq|SELECT id, partnumber, description | .
65     qq|FROM parts $filter | .
66     qq|ORDER BY $order_by $order_dir|;
67   my $sth = $dbh->prepare($query);
68   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
69   my $parts = [];
70   while (my $ref = $sth->fetchrow_hashref()) {
71     push(@{$parts}, $ref);
72   }
73   $sth->finish();
74   $dbh->disconnect();
75
76   $main::lxdebug->leave_sub();
77
78   return $parts;
79 }
80
81 sub retrieve_projects {
82   $main::lxdebug->enter_sub();
83
84   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
85
86   my $dbh = $form->dbconnect($myconfig);
87
88   my (@filter_values, $filter);
89   if ($form->{"projectnumber"}) {
90     $filter .= qq| AND (projectnumber ILIKE ?)|;
91     push(@filter_values, '%' . $form->{"projectnumber"} . '%');
92   }
93   if ($form->{"description"}) {
94     $filter .= qq| AND (description ILIKE ?)|;
95     push(@filter_values, '%' . $form->{"description"} . '%');
96   }
97   substr($filter, 1, 3) = "WHERE" if ($filter);
98
99   $order_by =~ s/[^a-zA-Z_]//g;
100   $order_dir = $order_dir ? "ASC" : "DESC";
101
102   my $query =
103     qq|SELECT id, projectnumber, description | .
104     qq|FROM project $filter | .
105     qq|ORDER BY $order_by $order_dir|;
106   my $sth = $dbh->prepare($query);
107   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
108   my $projects = [];
109   while (my $ref = $sth->fetchrow_hashref()) {
110     push(@{$projects}, $ref);
111   }
112   $sth->finish();
113   $dbh->disconnect();
114
115   $main::lxdebug->leave_sub();
116
117   return $projects;
118 }
119
120 sub retrieve_employees {
121   $main::lxdebug->enter_sub();
122
123   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
124
125   my $dbh = $form->dbconnect($myconfig);
126
127   my (@filter_values, $filter);
128   if ($form->{"name"}) {
129     $filter .= qq| AND (name ILIKE ?)|;
130     push(@filter_values, '%' . $form->{"name"} . '%');
131   }
132   substr($filter, 1, 3) = "WHERE" if ($filter);
133
134   $order_by =~ s/[^a-zA-Z_]//g;
135   $order_dir = $order_dir ? "ASC" : "DESC";
136
137   my $query =
138     qq|SELECT id, name | .
139     qq|FROM employee $filter | .
140     qq|ORDER BY $order_by $order_dir|;
141   my $sth = $dbh->prepare($query);
142   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
143   my $employees = [];
144   while (my $ref = $sth->fetchrow_hashref()) {
145     push(@{$employees}, $ref);
146   }
147   $sth->finish();
148   $dbh->disconnect();
149
150   $main::lxdebug->leave_sub();
151
152   return $employees;
153 }
154
155 sub retrieve_delivery_customer {
156   $main::lxdebug->enter_sub();
157
158   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
159
160   my $dbh = $form->dbconnect($myconfig);
161
162   my (@filter_values, $filter);
163   if ($form->{"name"}) {
164     $filter .= qq| (name ILIKE ?) AND|;
165     push(@filter_values, '%' . $form->{"name"} . '%');
166   }
167
168   $order_by =~ s/[^a-zA-Z_]//g;
169   $order_dir = $order_dir ? "ASC" : "DESC";
170
171   my $query =
172     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address ! .
173     qq!FROM customer ! .
174     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Endkunde') ! .
175     qq!ORDER BY $order_by $order_dir!;
176   my $sth = $dbh->prepare($query);
177   $sth->execute(@filter_values) ||
178     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
179   my $delivery_customers = [];
180   while (my $ref = $sth->fetchrow_hashref()) {
181     push(@{$delivery_customers}, $ref);
182   }
183   $sth->finish();
184   $dbh->disconnect();
185
186   $main::lxdebug->leave_sub();
187
188   return $delivery_customers;
189 }
190
191 sub retrieve_vendor {
192   $main::lxdebug->enter_sub();
193
194   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
195
196   my $dbh = $form->dbconnect($myconfig);
197
198   my (@filter_values, $filter);
199   if ($form->{"name"}) {
200     $filter .= qq| (name ILIKE ?) AND|;
201     push(@filter_values, '%' . $form->{"name"} . '%');
202   }
203
204   $order_by =~ s/[^a-zA-Z_]//g;
205   $order_dir = $order_dir ? "ASC" : "DESC";
206
207   my $query =
208     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address FROM customer ! .
209     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Händler') ! .
210     qq!ORDER BY $order_by $order_dir!;
211   my $sth = $dbh->prepare($query);
212   $sth->execute(@filter_values) ||
213     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
214   my $vendors = [];
215   while (my $ref = $sth->fetchrow_hashref()) {
216     push(@{$vendors}, $ref);
217   }
218   $sth->finish();
219   $dbh->disconnect();
220
221   $main::lxdebug->leave_sub();
222
223   return $vendors;
224 }
225
226 sub mkdir_with_parents {
227   $main::lxdebug->enter_sub();
228
229   my ($full_path) = @_;
230
231   my $path = "";
232
233   $full_path =~ s|/+|/|;
234
235   foreach my $part (split(m|/|, $full_path)) {
236     $path .= "/" if ($path);
237     $path .= $part;
238
239     die("Could not create directory '$path' because a file exists with " .
240         "the same name.\n") if (-f $path);
241
242     if (! -d $path) {
243       mkdir($path, 0770) || die("Could not create the directory '$path'. " .
244                                 "OS error: $!\n");
245     }
246   }
247
248   $main::lxdebug->leave_sub();
249 }
250
251 sub webdav_folder {
252   $main::lxdebug->enter_sub();
253
254   my ($form) = @_;
255
256   return $main::lxdebug->leave_sub()
257     unless ($main::webdav && $form->{id});
258
259   my ($path, $number);
260
261   $form->{WEBDAV} = {};
262
263   if ($form->{type} eq "sales_quotation") {
264     ($path, $number) = ("angebote", $form->{quonumber});
265   } elsif ($form->{type} eq "sales_order") {
266     ($path, $number) = ("bestellungen", $form->{ordnumber});
267   } elsif ($form->{type} eq "request_quotation") {
268     ($path, $number) = ("anfragen", $form->{quonumber});
269   } elsif ($form->{type} eq "purchase_order") {
270     ($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
271   } elsif ($form->{type} eq "credit_note") {
272     ($path, $number) = ("gutschriften", $form->{invnumber});
273   } elsif ($form->{vc} eq "customer") {
274     ($path, $number) = ("rechnungen", $form->{invnumber});
275   } else {
276     ($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
277   }
278
279   return $main::lxdebug->leave_sub() unless ($path && $number);
280
281   $path = "webdav/${path}/${number}";
282
283   if (!-d $path) {
284     mkdir_with_parents($path);
285
286   } else {
287     my $base_path = substr($ENV{'SCRIPT_NAME'}, 1);
288     $base_path =~ s|[^/]+$||;
289
290     foreach my $file (<$path/*>) {
291       my $fname = $file;
292       $fname =~ s|.*/||;
293       $form->{WEBDAV}{$fname} =
294         ($ENV{"HTTPS"} ? "https://" : "http://") .
295         $ENV{'SERVER_NAME'} . "/" . $base_path . $file;
296     }
297   }
298
299   $main::lxdebug->leave_sub();
300 }
301
302 1;