Eine Funktion zur Überprüfung der Existens von Funktionsparametern hinzugefügt, wenn...
[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 SL::DBUtils;
14
15 use vars qw(@db_encodings %db_encoding_to_charset);
16
17 @db_encodings = (
18   { "label" => "ASCII",          "dbencoding" => "SQL_ASCII", "charset" => "ASCII" },
19   { "label" => "UTF-8 Unicode",  "dbencoding" => "UNICODE",   "charset" => "UTF-8" },
20   { "label" => "ISO 8859-1",     "dbencoding" => "LATIN1",    "charset" => "ISO-8859-1" },
21   { "label" => "ISO 8859-2",     "dbencoding" => "LATIN2",    "charset" => "ISO-8859-2" },
22   { "label" => "ISO 8859-3",     "dbencoding" => "LATIN3",    "charset" => "ISO-8859-3" },
23   { "label" => "ISO 8859-4",     "dbencoding" => "LATIN4",    "charset" => "ISO-8859-4" },
24   { "label" => "ISO 8859-5",     "dbencoding" => "LATIN5",    "charset" => "ISO-8859-5" },
25   { "label" => "ISO 8859-15",    "dbencoding" => "LATIN9",    "charset" => "ISO-8859-15" },
26   { "label" => "KOI8-R",         "dbencoding" => "KOI8",      "charset" => "KOI8-R" },
27   { "label" => "Windows CP1251", "dbencoding" => "WIN",       "charset" => "CP1251" },
28   { "label" => "Windows CP866",  "dbencoding" => "ALT",       "charset" => "CP866" },
29 );
30
31 %db_encoding_to_charset = map { $_->{dbencoding}, $_->{charset} } @db_encodings;
32
33 use constant DEFAULT_CHARSET => 'ISO-8859-15';
34
35 sub unique_id {
36   my ($a, $b) = gettimeofday();
37   return "${a}-${b}-${$}";
38 }
39
40 sub tmpname {
41   return "/tmp/lx-office-tmp-" . unique_id();
42 }
43
44 sub retrieve_parts {
45   $main::lxdebug->enter_sub();
46
47   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
48
49   my $dbh = $form->dbconnect($myconfig);
50
51   my (@filter_values, $filter);
52   if ($form->{"partnumber"}) {
53     $filter .= qq| AND (partnumber ILIKE ?)|;
54     push(@filter_values, '%' . $form->{"partnumber"} . '%');
55   }
56   if ($form->{"description"}) {
57     $filter .= qq| AND (description ILIKE ?)|;
58     push(@filter_values, '%' . $form->{"description"} . '%');
59   }
60   substr($filter, 1, 3) = "WHERE" if ($filter);
61
62   $order_by =~ s/[^a-zA-Z_]//g;
63   $order_dir = $order_dir ? "ASC" : "DESC";
64
65   my $query =
66     qq|SELECT id, partnumber, description | .
67     qq|FROM parts $filter | .
68     qq|ORDER BY $order_by $order_dir|;
69   my $sth = $dbh->prepare($query);
70   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
71   my $parts = [];
72   while (my $ref = $sth->fetchrow_hashref()) {
73     push(@{$parts}, $ref);
74   }
75   $sth->finish();
76   $dbh->disconnect();
77
78   $main::lxdebug->leave_sub();
79
80   return $parts;
81 }
82
83 sub retrieve_projects {
84   $main::lxdebug->enter_sub();
85
86   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
87
88   my $dbh = $form->dbconnect($myconfig);
89
90   my (@filter_values, $filter);
91   if ($form->{"projectnumber"}) {
92     $filter .= qq| AND (projectnumber ILIKE ?)|;
93     push(@filter_values, '%' . $form->{"projectnumber"} . '%');
94   }
95   if ($form->{"description"}) {
96     $filter .= qq| AND (description ILIKE ?)|;
97     push(@filter_values, '%' . $form->{"description"} . '%');
98   }
99   substr($filter, 1, 3) = "WHERE" if ($filter);
100
101   $order_by =~ s/[^a-zA-Z_]//g;
102   $order_dir = $order_dir ? "ASC" : "DESC";
103
104   my $query =
105     qq|SELECT id, projectnumber, description | .
106     qq|FROM project $filter | .
107     qq|ORDER BY $order_by $order_dir|;
108   my $sth = $dbh->prepare($query);
109   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
110   my $projects = [];
111   while (my $ref = $sth->fetchrow_hashref()) {
112     push(@{$projects}, $ref);
113   }
114   $sth->finish();
115   $dbh->disconnect();
116
117   $main::lxdebug->leave_sub();
118
119   return $projects;
120 }
121
122 sub retrieve_employees {
123   $main::lxdebug->enter_sub();
124
125   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
126
127   my $dbh = $form->dbconnect($myconfig);
128
129   my (@filter_values, $filter);
130   if ($form->{"name"}) {
131     $filter .= qq| AND (name ILIKE ?)|;
132     push(@filter_values, '%' . $form->{"name"} . '%');
133   }
134   substr($filter, 1, 3) = "WHERE" if ($filter);
135
136   $order_by =~ s/[^a-zA-Z_]//g;
137   $order_dir = $order_dir ? "ASC" : "DESC";
138
139   my $query =
140     qq|SELECT id, name | .
141     qq|FROM employee $filter | .
142     qq|ORDER BY $order_by $order_dir|;
143   my $sth = $dbh->prepare($query);
144   $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
145   my $employees = [];
146   while (my $ref = $sth->fetchrow_hashref()) {
147     push(@{$employees}, $ref);
148   }
149   $sth->finish();
150   $dbh->disconnect();
151
152   $main::lxdebug->leave_sub();
153
154   return $employees;
155 }
156
157 sub retrieve_delivery_customer {
158   $main::lxdebug->enter_sub();
159
160   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
161
162   my $dbh = $form->dbconnect($myconfig);
163
164   my (@filter_values, $filter);
165   if ($form->{"name"}) {
166     $filter .= qq| (name ILIKE ?) AND|;
167     push(@filter_values, '%' . $form->{"name"} . '%');
168   }
169
170   $order_by =~ s/[^a-zA-Z_]//g;
171   $order_dir = $order_dir ? "ASC" : "DESC";
172
173   my $query =
174     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address ! .
175     qq!FROM customer ! .
176     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Endkunde') ! .
177     qq!ORDER BY $order_by $order_dir!;
178   my $sth = $dbh->prepare($query);
179   $sth->execute(@filter_values) ||
180     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
181   my $delivery_customers = [];
182   while (my $ref = $sth->fetchrow_hashref()) {
183     push(@{$delivery_customers}, $ref);
184   }
185   $sth->finish();
186   $dbh->disconnect();
187
188   $main::lxdebug->leave_sub();
189
190   return $delivery_customers;
191 }
192
193 sub retrieve_vendor {
194   $main::lxdebug->enter_sub();
195
196   my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
197
198   my $dbh = $form->dbconnect($myconfig);
199
200   my (@filter_values, $filter);
201   if ($form->{"name"}) {
202     $filter .= qq| (name ILIKE ?) AND|;
203     push(@filter_values, '%' . $form->{"name"} . '%');
204   }
205
206   $order_by =~ s/[^a-zA-Z_]//g;
207   $order_dir = $order_dir ? "ASC" : "DESC";
208
209   my $query =
210     qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address FROM customer ! .
211     qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Händler') ! .
212     qq!ORDER BY $order_by $order_dir!;
213   my $sth = $dbh->prepare($query);
214   $sth->execute(@filter_values) ||
215     $form->dberror($query . " (" . join(", ", @filter_values) . ")");
216   my $vendors = [];
217   while (my $ref = $sth->fetchrow_hashref()) {
218     push(@{$vendors}, $ref);
219   }
220   $sth->finish();
221   $dbh->disconnect();
222
223   $main::lxdebug->leave_sub();
224
225   return $vendors;
226 }
227
228 sub mkdir_with_parents {
229   $main::lxdebug->enter_sub();
230
231   my ($full_path) = @_;
232
233   my $path = "";
234
235   $full_path =~ s|/+|/|;
236
237   foreach my $part (split(m|/|, $full_path)) {
238     $path .= "/" if ($path);
239     $path .= $part;
240
241     die("Could not create directory '$path' because a file exists with " .
242         "the same name.\n") if (-f $path);
243
244     if (! -d $path) {
245       mkdir($path, 0770) || die("Could not create the directory '$path'. " .
246                                 "OS error: $!\n");
247     }
248   }
249
250   $main::lxdebug->leave_sub();
251 }
252
253 sub webdav_folder {
254   $main::lxdebug->enter_sub();
255
256   my ($form) = @_;
257
258   return $main::lxdebug->leave_sub()
259     unless ($main::webdav && $form->{id});
260
261   my ($path, $number);
262
263   $form->{WEBDAV} = [];
264
265   if ($form->{type} eq "sales_quotation") {
266     ($path, $number) = ("angebote", $form->{quonumber});
267   } elsif ($form->{type} eq "sales_order") {
268     ($path, $number) = ("bestellungen", $form->{ordnumber});
269   } elsif ($form->{type} eq "request_quotation") {
270     ($path, $number) = ("anfragen", $form->{quonumber});
271   } elsif ($form->{type} eq "purchase_order") {
272     ($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
273   } elsif ($form->{type} eq "credit_note") {
274     ($path, $number) = ("gutschriften", $form->{invnumber});
275   } elsif ($form->{vc} eq "customer") {
276     ($path, $number) = ("rechnungen", $form->{invnumber});
277   } else {
278     ($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
279   }
280
281   return $main::lxdebug->leave_sub() unless ($path && $number);
282
283   $number =~ s|[/\\]|_|g;
284
285   $path = "webdav/${path}/${number}";
286
287   if (!-d $path) {
288     mkdir_with_parents($path);
289
290   } else {
291     my $base_path = substr($ENV{'SCRIPT_NAME'}, 1);
292     $base_path =~ s|[^/]+$||;
293     $base_path =~ s|/$||;
294
295     if (opendir $dir, $path) {
296       foreach my $file (sort { lc $a cmp lc $b } readdir $dir) {
297         next if (($file eq '.') || ($file eq '..'));
298
299         my $fname = $file;
300         $fname  =~ s|.*/||;
301
302         my $is_directory = -d "$path/$file";
303
304         $file  = join('/', map { $form->escape($_) } grep { $_ } split m|/+|, "$path/$file");
305         $file .=  '/' if ($is_directory);
306
307         push @{ $form->{WEBDAV} }, {
308           'name' => $fname,
309           'link' => ($ENV{"HTTPS"} ? "https://" : "http://") . $ENV{'SERVER_NAME'} . "/$base_path/$file",
310           'type' => $is_directory ? $main::locale->text('Directory') : $main::locale->text('File'),
311         };
312       }
313
314       closedir $dir;
315     }
316   }
317
318   $main::lxdebug->leave_sub();
319 }
320
321 sub get_vc_details {
322   $main::lxdebug->enter_sub();
323
324   my ($self, $myconfig, $form, $vc, $vc_id) = @_;
325
326   $vc = $vc eq "customer" ? "customer" : "vendor";
327
328   my $dbh = $form->dbconnect($myconfig);
329
330   my $query;
331
332   $query =
333     qq|SELECT
334          vc.*,
335          pt.description AS payment_terms,
336          b.description AS business,
337          l.description AS language
338        FROM ${vc} vc
339        LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id)
340        LEFT JOIN business b ON (vc.business_id = b.id)
341        LEFT JOIN language l ON (vc.language_id = l.id)
342        WHERE vc.id = ?|;
343   my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id);
344
345   if (!$ref) {
346     $dbh->disconnect();
347     $main::lxdebug->leave_sub();
348     return 0;
349   }
350
351   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
352
353   map { $form->{$_} = $form->format_amount($myconfig, $form->{$_} * 1) } qw(discount creditlimit);
354
355   $query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
356   $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
357
358   $query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
359   $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
360
361   $dbh->disconnect();
362
363   $main::lxdebug->leave_sub();
364
365   return 1;
366 }
367
368 sub get_shipto_by_id {
369   $main::lxdebug->enter_sub();
370
371   my ($self, $myconfig, $form, $shipto_id, $prefix) = @_;
372
373   $prefix ||= "";
374
375   my $dbh = $form->dbconnect($myconfig);
376
377   my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
378   my $ref   = selectfirst_hashref_query($form, $dbh, $query, $shipto_id);
379
380   map { $form->{"${prefix}${_}"} = $ref->{$_} } keys %{ $ref } if $ref;
381
382   $dbh->disconnect();
383
384   $main::lxdebug->leave_sub();
385 }
386
387 sub save_email_status {
388   $main::lxdebug->enter_sub();
389
390   my ($self, $myconfig, $form) = @_;
391
392   my ($table, $query, $dbh);
393
394   if ($form->{script} eq 'oe.pl') {
395     $table = 'oe';
396
397   } elsif ($form->{script} eq 'is.pl') {
398     $table = 'ar';
399
400   } elsif ($form->{script} eq 'ir.pl') {
401     $table = 'ap';
402
403   }
404
405   return $main::lxdebug->leave_sub() if (!$form->{id} || !$table || !$form->{formname});
406
407   $dbh = $form->get_standard_dbh($myconfig);
408
409   my ($intnotes) = selectrow_query($form, $dbh, qq|SELECT intnotes FROM $table WHERE id = ?|, $form->{id});
410
411   $intnotes =~ s|\r||g;
412   $intnotes =~ s|\n$||;
413
414   $intnotes .= "\n\n" if ($intnotes);
415
416   my $cc  = $main::locale->text('Cc') . ": $form->{cc}\n"   if $form->{cc};
417   my $bcc = $main::locale->text('Bcc') . ": $form->{bcc}\n" if $form->{bcc};
418   my $now = scalar localtime;
419
420   $intnotes .= $main::locale->text('[email]') . "\n"
421     . $main::locale->text('Date') . ": $now\n"
422     . $main::locale->text('To (email)') . ": $form->{email}\n"
423     . "${cc}${bcc}"
424     . $main::locale->text('Subject') . ": $form->{subject}\n\n"
425     . $main::locale->text('Message') . ": $form->{message}";
426
427   $intnotes =~ s|\r||g;
428
429   do_query($form, $dbh, qq|UPDATE $table SET intnotes = ? WHERE id = ?|, $intnotes, $form->{id});
430
431   $form->save_status($dbh);
432
433   $dbh->commit();
434
435   $main::lxdebug->leave_sub();
436 }
437
438 sub check_params {
439   my $params = shift;
440
441   foreach my $key (@_) {
442     if (!defined $params->{$key}) {
443       my $subroutine = (caller(1))[3];
444       $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
445     }
446   }
447 }
448
449 1;