1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #====================================================================
11 use Time::HiRes qw(gettimeofday);
16 use vars qw(@db_encodings %db_encoding_to_charset);
19 { "label" => "ASCII", "dbencoding" => "SQL_ASCII", "charset" => "ASCII" },
20 { "label" => "UTF-8 Unicode", "dbencoding" => "UNICODE", "charset" => "UTF-8" },
21 { "label" => "ISO 8859-1", "dbencoding" => "LATIN1", "charset" => "ISO-8859-1" },
22 { "label" => "ISO 8859-2", "dbencoding" => "LATIN2", "charset" => "ISO-8859-2" },
23 { "label" => "ISO 8859-3", "dbencoding" => "LATIN3", "charset" => "ISO-8859-3" },
24 { "label" => "ISO 8859-4", "dbencoding" => "LATIN4", "charset" => "ISO-8859-4" },
25 { "label" => "ISO 8859-5", "dbencoding" => "LATIN5", "charset" => "ISO-8859-5" },
26 { "label" => "ISO 8859-15", "dbencoding" => "LATIN9", "charset" => "ISO-8859-15" },
27 { "label" => "KOI8-R", "dbencoding" => "KOI8", "charset" => "KOI8-R" },
28 { "label" => "Windows CP1251", "dbencoding" => "WIN", "charset" => "CP1251" },
29 { "label" => "Windows CP866", "dbencoding" => "ALT", "charset" => "CP866" },
32 %db_encoding_to_charset = map { $_->{dbencoding}, $_->{charset} } @db_encodings;
33 %charset_to_db_encoding = map { $_->{charset}, $_->{dbencoding} } @db_encodings;
35 use constant DEFAULT_CHARSET => 'ISO-8859-15';
38 my ($a, $b) = gettimeofday();
39 return "${a}-${b}-${$}";
43 return "/tmp/lx-office-tmp-" . unique_id();
47 $main::lxdebug->enter_sub();
49 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
51 my $dbh = $form->dbconnect($myconfig);
53 my (@filter_values, $filter);
55 foreach (qw(partnumber description ean)) {
56 next unless $form->{$_};
58 $filter .= qq| AND ($_ ILIKE ?)|;
59 push @filter_values, '%' . $form->{$_} . '%';
62 if ($form->{no_assemblies}) {
63 $filter .= qq| AND (NOT COALESCE(assembly, FALSE))|;
65 if ($form->{assemblies}) {
66 $filter .= qq| AND assembly=TRUE|;
69 if ($form->{no_services}) {
70 $filter .= qq| AND (inventory_accno_id is not NULL or assembly=TRUE)|; # @mb hier nochmal optimieren ... nach kurzer ruecksprache alles i.o.
73 substr($filter, 1, 3) = "WHERE" if ($filter);
75 $order_by =~ s/[^a-zA-Z_]//g;
76 $order_dir = $order_dir ? "ASC" : "DESC";
79 qq|SELECT id, partnumber, description, ean | .
80 qq|FROM parts $filter | .
81 qq|ORDER BY $order_by $order_dir|;
82 my $sth = $dbh->prepare($query);
83 $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
85 while (my $ref = $sth->fetchrow_hashref()) {
86 push(@{$parts}, $ref);
91 $main::lxdebug->leave_sub();
96 sub retrieve_projects {
97 $main::lxdebug->enter_sub();
99 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
101 my $dbh = $form->dbconnect($myconfig);
103 my (@filter_values, $filter);
104 if ($form->{"projectnumber"}) {
105 $filter .= qq| AND (projectnumber ILIKE ?)|;
106 push(@filter_values, '%' . $form->{"projectnumber"} . '%');
108 if ($form->{"description"}) {
109 $filter .= qq| AND (description ILIKE ?)|;
110 push(@filter_values, '%' . $form->{"description"} . '%');
112 substr($filter, 1, 3) = "WHERE" if ($filter);
114 $order_by =~ s/[^a-zA-Z_]//g;
115 $order_dir = $order_dir ? "ASC" : "DESC";
118 qq|SELECT id, projectnumber, description | .
119 qq|FROM project $filter | .
120 qq|ORDER BY $order_by $order_dir|;
121 my $sth = $dbh->prepare($query);
122 $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
124 while (my $ref = $sth->fetchrow_hashref()) {
125 push(@{$projects}, $ref);
130 $main::lxdebug->leave_sub();
135 sub retrieve_employees {
136 $main::lxdebug->enter_sub();
138 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
140 my $dbh = $form->dbconnect($myconfig);
142 my (@filter_values, $filter);
143 if ($form->{"name"}) {
144 $filter .= qq| AND (name ILIKE ?)|;
145 push(@filter_values, '%' . $form->{"name"} . '%');
147 substr($filter, 1, 3) = "WHERE" if ($filter);
149 $order_by =~ s/[^a-zA-Z_]//g;
150 $order_dir = $order_dir ? "ASC" : "DESC";
153 qq|SELECT id, name | .
154 qq|FROM employee $filter | .
155 qq|ORDER BY $order_by $order_dir|;
156 my $sth = $dbh->prepare($query);
157 $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
159 while (my $ref = $sth->fetchrow_hashref()) {
160 push(@{$employees}, $ref);
165 $main::lxdebug->leave_sub();
170 sub retrieve_customers_or_vendors {
171 $main::lxdebug->enter_sub();
173 my ($self, $myconfig, $form, $order_by, $order_dir, $is_vendor, $allow_both) = @_;
175 my $dbh = $form->dbconnect($myconfig);
177 my (@filter_values, $filter);
178 if ($form->{"name"}) {
179 $filter .= " AND (TABLE.name ILIKE ?)";
180 push(@filter_values, '%' . $form->{"name"} . '%');
182 if (!$form->{"obsolete"}) {
183 $filter .= " AND NOT TABLE.obsolete";
185 substr($filter, 1, 3) = "WHERE" if ($filter);
187 $order_by =~ s/[^a-zA-Z_]//g;
188 $order_dir = $order_dir ? "ASC" : "DESC";
190 my (@queries, @query_parameters);
192 if ($allow_both || !$is_vendor) {
193 my $c_filter = $filter;
194 $c_filter =~ s/TABLE/c/g;
195 push(@queries, qq|SELECT
196 c.id, c.name, 0 AS customer_is_vendor,
197 c.street, c.zipcode, c.city,
198 ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
200 LEFT JOIN contacts ct ON (c.id = ct.cp_cv_id)
202 push(@query_parameters, @filter_values);
205 if ($allow_both || $is_vendor) {
206 my $v_filter = $filter;
207 $v_filter =~ s/TABLE/v/g;
208 push(@queries, qq|SELECT
209 v.id, v.name, 1 AS customer_is_vendor,
210 v.street, v.zipcode, v.city,
211 ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
213 LEFT JOIN contacts ct ON (v.id = ct.cp_cv_id)
215 push(@query_parameters, @filter_values);
218 my $query = join(" UNION ", @queries) . " ORDER BY $order_by $order_dir";
219 my $sth = $dbh->prepare($query);
220 $sth->execute(@query_parameters) || $form->dberror($query . " (" . join(", ", @query_parameters) . ")");
222 while (my $ref = $sth->fetchrow_hashref()) {
223 push(@{$customers}, $ref);
228 $main::lxdebug->leave_sub();
233 sub retrieve_delivery_customer {
234 $main::lxdebug->enter_sub();
236 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
238 my $dbh = $form->dbconnect($myconfig);
240 my (@filter_values, $filter);
241 if ($form->{"name"}) {
242 $filter .= qq| (name ILIKE ?) AND|;
243 push(@filter_values, '%' . $form->{"name"} . '%');
246 $order_by =~ s/[^a-zA-Z_]//g;
247 $order_dir = $order_dir ? "ASC" : "DESC";
250 qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address ! .
252 qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Endkunde') ! .
253 qq!ORDER BY $order_by $order_dir!;
254 my $sth = $dbh->prepare($query);
255 $sth->execute(@filter_values) ||
256 $form->dberror($query . " (" . join(", ", @filter_values) . ")");
257 my $delivery_customers = [];
258 while (my $ref = $sth->fetchrow_hashref()) {
259 push(@{$delivery_customers}, $ref);
264 $main::lxdebug->leave_sub();
266 return $delivery_customers;
269 sub retrieve_vendor {
270 $main::lxdebug->enter_sub();
272 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
274 my $dbh = $form->dbconnect($myconfig);
276 my (@filter_values, $filter);
277 if ($form->{"name"}) {
278 $filter .= qq| (name ILIKE ?) AND|;
279 push(@filter_values, '%' . $form->{"name"} . '%');
282 $order_by =~ s/[^a-zA-Z_]//g;
283 $order_dir = $order_dir ? "ASC" : "DESC";
286 qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address FROM customer ! .
287 qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Händler') ! .
288 qq!ORDER BY $order_by $order_dir!;
289 my $sth = $dbh->prepare($query);
290 $sth->execute(@filter_values) ||
291 $form->dberror($query . " (" . join(", ", @filter_values) . ")");
293 while (my $ref = $sth->fetchrow_hashref()) {
294 push(@{$vendors}, $ref);
299 $main::lxdebug->leave_sub();
304 sub mkdir_with_parents {
305 $main::lxdebug->enter_sub();
307 my ($full_path) = @_;
311 $full_path =~ s|/+|/|;
313 foreach my $part (split(m|/|, $full_path)) {
314 $path .= "/" if ($path);
317 die("Could not create directory '$path' because a file exists with " .
318 "the same name.\n") if (-f $path);
321 mkdir($path, 0770) || die("Could not create the directory '$path'. " .
326 $main::lxdebug->leave_sub();
330 $main::lxdebug->enter_sub();
334 return $main::lxdebug->leave_sub()
335 unless ($main::webdav && $form->{id});
339 $form->{WEBDAV} = [];
341 if ($form->{type} eq "sales_quotation") {
342 ($path, $number) = ("angebote", $form->{quonumber});
343 } elsif ($form->{type} eq "sales_order") {
344 ($path, $number) = ("bestellungen", $form->{ordnumber});
345 } elsif ($form->{type} eq "request_quotation") {
346 ($path, $number) = ("anfragen", $form->{quonumber});
347 } elsif ($form->{type} eq "purchase_order") {
348 ($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
349 } elsif ($form->{type} eq "credit_note") {
350 ($path, $number) = ("gutschriften", $form->{invnumber});
351 } elsif ($form->{vc} eq "customer") {
352 ($path, $number) = ("rechnungen", $form->{invnumber});
354 ($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
357 return $main::lxdebug->leave_sub() unless ($path && $number);
359 $number =~ s|[/\\]|_|g;
361 $path = "webdav/${path}/${number}";
364 mkdir_with_parents($path);
367 my $base_path = substr($ENV{'SCRIPT_NAME'}, 1);
368 $base_path =~ s|[^/]+$||;
369 $base_path =~ s|/$||;
370 # wo kommt der wert für dir her? es wird doch gar nichts übergeben? fix für strict my $dir jb 21.2.
371 if (opendir my $dir, $path) {
372 foreach my $file (sort { lc $a cmp lc $b } readdir $dir) {
373 next if (($file eq '.') || ($file eq '..'));
378 my $is_directory = -d "$path/$file";
380 $file = join('/', map { $form->escape($_) } grep { $_ } split m|/+|, "$path/$file");
381 $file .= '/' if ($is_directory);
383 push @{ $form->{WEBDAV} }, {
385 'link' => ($ENV{"HTTPS"} ? "https://" : "http://") . $ENV{'SERVER_NAME'} . "/$base_path/$file",
386 'type' => $is_directory ? $main::locale->text('Directory') : $main::locale->text('File'),
394 $main::lxdebug->leave_sub();
398 $main::lxdebug->enter_sub();
400 my ($self, $myconfig, $form, $vc, $vc_id) = @_;
402 $vc = $vc eq "customer" ? "customer" : "vendor";
404 my $dbh = $form->dbconnect($myconfig);
411 pt.description AS payment_terms,
412 b.description AS business,
413 l.description AS language
415 LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id)
416 LEFT JOIN business b ON (vc.business_id = b.id)
417 LEFT JOIN language l ON (vc.language_id = l.id)
419 my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id);
423 $main::lxdebug->leave_sub();
427 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
429 map { $form->{$_} = $form->format_amount($myconfig, $form->{$_} * 1) } qw(discount creditlimit);
431 $query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
432 $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
434 $query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
435 $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
439 $main::lxdebug->leave_sub();
444 sub get_shipto_by_id {
445 $main::lxdebug->enter_sub();
447 my ($self, $myconfig, $form, $shipto_id, $prefix) = @_;
451 my $dbh = $form->dbconnect($myconfig);
453 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
454 my $ref = selectfirst_hashref_query($form, $dbh, $query, $shipto_id);
456 map { $form->{"${prefix}${_}"} = $ref->{$_} } keys %{ $ref } if $ref;
460 $main::lxdebug->leave_sub();
463 sub save_email_status {
464 $main::lxdebug->enter_sub();
466 my ($self, $myconfig, $form) = @_;
468 my ($table, $query, $dbh);
470 if ($form->{script} eq 'oe.pl') {
473 } elsif ($form->{script} eq 'is.pl') {
476 } elsif ($form->{script} eq 'ir.pl') {
481 return $main::lxdebug->leave_sub() if (!$form->{id} || !$table || !$form->{formname});
483 $dbh = $form->get_standard_dbh($myconfig);
485 my ($intnotes) = selectrow_query($form, $dbh, qq|SELECT intnotes FROM $table WHERE id = ?|, $form->{id});
487 $intnotes =~ s|\r||g;
488 $intnotes =~ s|\n$||;
490 $intnotes .= "\n\n" if ($intnotes);
492 my $cc = $main::locale->text('Cc') . ": $form->{cc}\n" if $form->{cc};
493 my $bcc = $main::locale->text('Bcc') . ": $form->{bcc}\n" if $form->{bcc};
494 my $now = scalar localtime;
496 $intnotes .= $main::locale->text('[email]') . "\n"
497 . $main::locale->text('Date') . ": $now\n"
498 . $main::locale->text('To (email)') . ": $form->{email}\n"
500 . $main::locale->text('Subject') . ": $form->{subject}\n\n"
501 . $main::locale->text('Message') . ": $form->{message}";
503 $intnotes =~ s|\r||g;
505 do_query($form, $dbh, qq|UPDATE $table SET intnotes = ? WHERE id = ?|, $intnotes, $form->{id});
507 $form->save_status($dbh);
511 $main::lxdebug->leave_sub();
517 foreach my $key (@_) {
518 if ((ref $key eq '') && !defined $params->{$key}) {
519 my $subroutine = (caller(1))[3];
520 $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
521 $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
522 $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
524 } elsif (ref $key eq 'ARRAY') {
526 foreach my $subkey (@{ $key }) {
527 if (defined $params->{$subkey}) {
534 my $subroutine = (caller(1))[3];
535 $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
536 $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
537 $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
546 foreach my $key (@_) {
547 if ((ref $key eq '') && !exists $params->{$key}) {
548 my $subroutine = (caller(1))[3];
549 $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
551 } elsif (ref $key eq 'ARRAY') {
553 foreach my $subkey (@{ $key }) {
554 if (exists $params->{$subkey}) {
561 my $subroutine = (caller(1))[3];
562 $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));