1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #====================================================================
11 use Time::HiRes qw(gettimeofday);
15 use vars qw(@db_encodings %db_encoding_to_charset);
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" },
31 %db_encoding_to_charset = map { $_->{dbencoding}, $_->{charset} } @db_encodings;
32 %charset_to_db_encoding = map { $_->{charset}, $_->{dbencoding} } @db_encodings;
34 use constant DEFAULT_CHARSET => 'ISO-8859-15';
37 my ($a, $b) = gettimeofday();
38 return "${a}-${b}-${$}";
42 return "/tmp/lx-office-tmp-" . unique_id();
46 $main::lxdebug->enter_sub();
48 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
50 my $dbh = $form->dbconnect($myconfig);
52 my (@filter_values, $filter);
54 foreach (qw(partnumber description ean)) {
55 next unless $form->{$_};
57 $filter .= qq| AND ($_ ILIKE ?)|;
58 push @filter_values, '%' . $form->{$_} . '%';
61 if ($form->{no_assemblies}) {
62 $filter .= qq| AND (NOT COALESCE(assembly, FALSE))|;
64 if ($form->{assemblies}) {
65 $filter .= qq| AND assembly=TRUE|;
68 if ($form->{no_services}) {
69 $filter .= qq| AND (inventory_accno_id is not NULL or assembly=TRUE)|; # @mb hier nochmal optimieren ... nach kurzer ruecksprache alles i.o.
72 substr($filter, 1, 3) = "WHERE" if ($filter);
74 $order_by =~ s/[^a-zA-Z_]//g;
75 $order_dir = $order_dir ? "ASC" : "DESC";
78 qq|SELECT id, partnumber, description, ean | .
79 qq|FROM parts $filter | .
80 qq|ORDER BY $order_by $order_dir|;
81 my $sth = $dbh->prepare($query);
82 $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
84 while (my $ref = $sth->fetchrow_hashref()) {
85 push(@{$parts}, $ref);
90 $main::lxdebug->leave_sub();
95 sub retrieve_projects {
96 $main::lxdebug->enter_sub();
98 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
100 my $dbh = $form->dbconnect($myconfig);
102 my (@filter_values, $filter);
103 if ($form->{"projectnumber"}) {
104 $filter .= qq| AND (projectnumber ILIKE ?)|;
105 push(@filter_values, '%' . $form->{"projectnumber"} . '%');
107 if ($form->{"description"}) {
108 $filter .= qq| AND (description ILIKE ?)|;
109 push(@filter_values, '%' . $form->{"description"} . '%');
111 substr($filter, 1, 3) = "WHERE" if ($filter);
113 $order_by =~ s/[^a-zA-Z_]//g;
114 $order_dir = $order_dir ? "ASC" : "DESC";
117 qq|SELECT id, projectnumber, description | .
118 qq|FROM project $filter | .
119 qq|ORDER BY $order_by $order_dir|;
120 my $sth = $dbh->prepare($query);
121 $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
123 while (my $ref = $sth->fetchrow_hashref()) {
124 push(@{$projects}, $ref);
129 $main::lxdebug->leave_sub();
134 sub retrieve_employees {
135 $main::lxdebug->enter_sub();
137 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
139 my $dbh = $form->dbconnect($myconfig);
141 my (@filter_values, $filter);
142 if ($form->{"name"}) {
143 $filter .= qq| AND (name ILIKE ?)|;
144 push(@filter_values, '%' . $form->{"name"} . '%');
146 substr($filter, 1, 3) = "WHERE" if ($filter);
148 $order_by =~ s/[^a-zA-Z_]//g;
149 $order_dir = $order_dir ? "ASC" : "DESC";
152 qq|SELECT id, name | .
153 qq|FROM employee $filter | .
154 qq|ORDER BY $order_by $order_dir|;
155 my $sth = $dbh->prepare($query);
156 $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
158 while (my $ref = $sth->fetchrow_hashref()) {
159 push(@{$employees}, $ref);
164 $main::lxdebug->leave_sub();
169 sub retrieve_customers_or_vendors {
170 $main::lxdebug->enter_sub();
172 my ($self, $myconfig, $form, $order_by, $order_dir, $is_vendor, $allow_both) = @_;
174 my $dbh = $form->dbconnect($myconfig);
176 my (@filter_values, $filter);
177 if ($form->{"name"}) {
178 $filter .= " AND (TABLE.name ILIKE ?)";
179 push(@filter_values, '%' . $form->{"name"} . '%');
181 if (!$form->{"obsolete"}) {
182 $filter .= " AND NOT TABLE.obsolete";
184 substr($filter, 1, 3) = "WHERE" if ($filter);
186 $order_by =~ s/[^a-zA-Z_]//g;
187 $order_dir = $order_dir ? "ASC" : "DESC";
189 my (@queries, @query_parameters);
191 if ($allow_both || !$is_vendor) {
192 my $c_filter = $filter;
193 $c_filter =~ s/TABLE/c/g;
194 push(@queries, qq|SELECT
195 c.id, c.name, 0 AS customer_is_vendor,
196 c.street, c.zipcode, c.city,
197 ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
199 LEFT JOIN contacts ct ON (c.id = ct.cp_cv_id)
201 push(@query_parameters, @filter_values);
204 if ($allow_both || $is_vendor) {
205 my $v_filter = $filter;
206 $v_filter =~ s/TABLE/v/g;
207 push(@queries, qq|SELECT
208 v.id, v.name, 1 AS customer_is_vendor,
209 v.street, v.zipcode, v.city,
210 ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
212 LEFT JOIN contacts ct ON (v.id = ct.cp_cv_id)
214 push(@query_parameters, @filter_values);
217 my $query = join(" UNION ", @queries) . " ORDER BY $order_by $order_dir";
218 my $sth = $dbh->prepare($query);
219 $sth->execute(@query_parameters) || $form->dberror($query . " (" . join(", ", @query_parameters) . ")");
221 while (my $ref = $sth->fetchrow_hashref()) {
222 push(@{$customers}, $ref);
227 $main::lxdebug->leave_sub();
232 sub retrieve_delivery_customer {
233 $main::lxdebug->enter_sub();
235 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
237 my $dbh = $form->dbconnect($myconfig);
239 my (@filter_values, $filter);
240 if ($form->{"name"}) {
241 $filter .= qq| (name ILIKE ?) AND|;
242 push(@filter_values, '%' . $form->{"name"} . '%');
245 $order_by =~ s/[^a-zA-Z_]//g;
246 $order_dir = $order_dir ? "ASC" : "DESC";
249 qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address ! .
251 qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Endkunde') ! .
252 qq!ORDER BY $order_by $order_dir!;
253 my $sth = $dbh->prepare($query);
254 $sth->execute(@filter_values) ||
255 $form->dberror($query . " (" . join(", ", @filter_values) . ")");
256 my $delivery_customers = [];
257 while (my $ref = $sth->fetchrow_hashref()) {
258 push(@{$delivery_customers}, $ref);
263 $main::lxdebug->leave_sub();
265 return $delivery_customers;
268 sub retrieve_vendor {
269 $main::lxdebug->enter_sub();
271 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
273 my $dbh = $form->dbconnect($myconfig);
275 my (@filter_values, $filter);
276 if ($form->{"name"}) {
277 $filter .= qq| (name ILIKE ?) AND|;
278 push(@filter_values, '%' . $form->{"name"} . '%');
281 $order_by =~ s/[^a-zA-Z_]//g;
282 $order_dir = $order_dir ? "ASC" : "DESC";
285 qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address FROM customer ! .
286 qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Händler') ! .
287 qq!ORDER BY $order_by $order_dir!;
288 my $sth = $dbh->prepare($query);
289 $sth->execute(@filter_values) ||
290 $form->dberror($query . " (" . join(", ", @filter_values) . ")");
292 while (my $ref = $sth->fetchrow_hashref()) {
293 push(@{$vendors}, $ref);
298 $main::lxdebug->leave_sub();
303 sub mkdir_with_parents {
304 $main::lxdebug->enter_sub();
306 my ($full_path) = @_;
310 $full_path =~ s|/+|/|;
312 foreach my $part (split(m|/|, $full_path)) {
313 $path .= "/" if ($path);
316 die("Could not create directory '$path' because a file exists with " .
317 "the same name.\n") if (-f $path);
320 mkdir($path, 0770) || die("Could not create the directory '$path'. " .
325 $main::lxdebug->leave_sub();
329 $main::lxdebug->enter_sub();
333 return $main::lxdebug->leave_sub()
334 unless ($main::webdav && $form->{id});
338 $form->{WEBDAV} = [];
340 if ($form->{type} eq "sales_quotation") {
341 ($path, $number) = ("angebote", $form->{quonumber});
342 } elsif ($form->{type} eq "sales_order") {
343 ($path, $number) = ("bestellungen", $form->{ordnumber});
344 } elsif ($form->{type} eq "request_quotation") {
345 ($path, $number) = ("anfragen", $form->{quonumber});
346 } elsif ($form->{type} eq "purchase_order") {
347 ($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
348 } elsif ($form->{type} eq "credit_note") {
349 ($path, $number) = ("gutschriften", $form->{invnumber});
350 } elsif ($form->{vc} eq "customer") {
351 ($path, $number) = ("rechnungen", $form->{invnumber});
353 ($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
356 return $main::lxdebug->leave_sub() unless ($path && $number);
358 $number =~ s|[/\\]|_|g;
360 $path = "webdav/${path}/${number}";
363 mkdir_with_parents($path);
366 my $base_path = substr($ENV{'SCRIPT_NAME'}, 1);
367 $base_path =~ s|[^/]+$||;
368 $base_path =~ s|/$||;
369 # wo kommt der wert für dir her? es wird doch gar nichts übergeben? fix für strict my $dir jb 21.2.
370 if (opendir my $dir, $path) {
371 foreach my $file (sort { lc $a cmp lc $b } readdir $dir) {
372 next if (($file eq '.') || ($file eq '..'));
377 my $is_directory = -d "$path/$file";
379 $file = join('/', map { $form->escape($_) } grep { $_ } split m|/+|, "$path/$file");
380 $file .= '/' if ($is_directory);
382 push @{ $form->{WEBDAV} }, {
384 'link' => ($ENV{"HTTPS"} ? "https://" : "http://") . $ENV{'SERVER_NAME'} . "/$base_path/$file",
385 'type' => $is_directory ? $main::locale->text('Directory') : $main::locale->text('File'),
393 $main::lxdebug->leave_sub();
397 $main::lxdebug->enter_sub();
399 my ($self, $myconfig, $form, $vc, $vc_id) = @_;
401 $vc = $vc eq "customer" ? "customer" : "vendor";
403 my $dbh = $form->dbconnect($myconfig);
410 pt.description AS payment_terms,
411 b.description AS business,
412 l.description AS language
414 LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id)
415 LEFT JOIN business b ON (vc.business_id = b.id)
416 LEFT JOIN language l ON (vc.language_id = l.id)
418 my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id);
422 $main::lxdebug->leave_sub();
426 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
428 map { $form->{$_} = $form->format_amount($myconfig, $form->{$_} * 1) } qw(discount creditlimit);
430 $query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
431 $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
433 $query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
434 $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
438 $main::lxdebug->leave_sub();
443 sub get_shipto_by_id {
444 $main::lxdebug->enter_sub();
446 my ($self, $myconfig, $form, $shipto_id, $prefix) = @_;
450 my $dbh = $form->dbconnect($myconfig);
452 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
453 my $ref = selectfirst_hashref_query($form, $dbh, $query, $shipto_id);
455 map { $form->{"${prefix}${_}"} = $ref->{$_} } keys %{ $ref } if $ref;
459 $main::lxdebug->leave_sub();
462 sub save_email_status {
463 $main::lxdebug->enter_sub();
465 my ($self, $myconfig, $form) = @_;
467 my ($table, $query, $dbh);
469 if ($form->{script} eq 'oe.pl') {
472 } elsif ($form->{script} eq 'is.pl') {
475 } elsif ($form->{script} eq 'ir.pl') {
480 return $main::lxdebug->leave_sub() if (!$form->{id} || !$table || !$form->{formname});
482 $dbh = $form->get_standard_dbh($myconfig);
484 my ($intnotes) = selectrow_query($form, $dbh, qq|SELECT intnotes FROM $table WHERE id = ?|, $form->{id});
486 $intnotes =~ s|\r||g;
487 $intnotes =~ s|\n$||;
489 $intnotes .= "\n\n" if ($intnotes);
491 my $cc = $main::locale->text('Cc') . ": $form->{cc}\n" if $form->{cc};
492 my $bcc = $main::locale->text('Bcc') . ": $form->{bcc}\n" if $form->{bcc};
493 my $now = scalar localtime;
495 $intnotes .= $main::locale->text('[email]') . "\n"
496 . $main::locale->text('Date') . ": $now\n"
497 . $main::locale->text('To (email)') . ": $form->{email}\n"
499 . $main::locale->text('Subject') . ": $form->{subject}\n\n"
500 . $main::locale->text('Message') . ": $form->{message}";
502 $intnotes =~ s|\r||g;
504 do_query($form, $dbh, qq|UPDATE $table SET intnotes = ? WHERE id = ?|, $intnotes, $form->{id});
506 $form->save_status($dbh);
510 $main::lxdebug->leave_sub();
516 foreach my $key (@_) {
517 if ((ref $key eq '') && !defined $params->{$key}) {
518 my $subroutine = (caller(1))[3];
519 $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
521 } elsif (ref $key eq 'ARRAY') {
523 foreach my $subkey (@{ $key }) {
524 if (defined $params->{$subkey}) {
531 my $subroutine = (caller(1))[3];
532 $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
541 foreach my $key (@_) {
542 if ((ref $key eq '') && !exists $params->{$key}) {
543 my $subroutine = (caller(1))[3];
544 $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
546 } elsif (ref $key eq 'ARRAY') {
548 foreach my $subkey (@{ $key }) {
549 if (exists $params->{$subkey}) {
556 my $subroutine = (caller(1))[3];
557 $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));