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 %charset_to_db_encoding);
21 { "label" => "ASCII", "dbencoding" => "SQL_ASCII", "charset" => "ASCII" },
22 { "label" => "UTF-8 Unicode", "dbencoding" => "UNICODE", "charset" => "UTF-8" },
23 { "label" => "ISO 8859-1", "dbencoding" => "LATIN1", "charset" => "ISO-8859-1" },
24 { "label" => "ISO 8859-2", "dbencoding" => "LATIN2", "charset" => "ISO-8859-2" },
25 { "label" => "ISO 8859-3", "dbencoding" => "LATIN3", "charset" => "ISO-8859-3" },
26 { "label" => "ISO 8859-4", "dbencoding" => "LATIN4", "charset" => "ISO-8859-4" },
27 { "label" => "ISO 8859-5", "dbencoding" => "LATIN5", "charset" => "ISO-8859-5" },
28 { "label" => "ISO 8859-15", "dbencoding" => "LATIN9", "charset" => "ISO-8859-15" },
29 { "label" => "KOI8-R", "dbencoding" => "KOI8", "charset" => "KOI8-R" },
30 { "label" => "Windows CP1251", "dbencoding" => "WIN", "charset" => "CP1251" },
31 { "label" => "Windows CP866", "dbencoding" => "ALT", "charset" => "CP866" },
34 %db_encoding_to_charset = map { $_->{dbencoding}, $_->{charset} } @db_encodings;
35 %charset_to_db_encoding = map { $_->{charset}, $_->{dbencoding} } @db_encodings;
37 use constant DEFAULT_CHARSET => 'ISO-8859-15';
40 my ($a, $b) = gettimeofday();
41 return "${a}-${b}-${$}";
45 return "/tmp/lx-office-tmp-" . unique_id();
49 $main::lxdebug->enter_sub();
51 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
53 my $dbh = $form->dbconnect($myconfig);
55 my (@filter_values, $filter);
57 foreach (qw(partnumber description ean)) {
58 next unless $form->{$_};
60 $filter .= qq| AND ($_ ILIKE ?)|;
61 push @filter_values, '%' . $form->{$_} . '%';
64 if ($form->{no_assemblies}) {
65 $filter .= qq| AND (NOT COALESCE(assembly, FALSE))|;
67 if ($form->{assemblies}) {
68 $filter .= qq| AND assembly=TRUE|;
71 if ($form->{no_services}) {
72 $filter .= qq| AND (inventory_accno_id is not NULL or assembly=TRUE)|; # @mb hier nochmal optimieren ... nach kurzer ruecksprache alles i.o.
75 substr($filter, 1, 3) = "WHERE" if ($filter);
77 $order_by =~ s/[^a-zA-Z_]//g;
78 $order_dir = $order_dir ? "ASC" : "DESC";
81 qq|SELECT id, partnumber, description, ean | .
82 qq|FROM parts $filter | .
83 qq|ORDER BY $order_by $order_dir|;
84 my $sth = $dbh->prepare($query);
85 $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
87 while (my $ref = $sth->fetchrow_hashref()) {
88 push(@{$parts}, $ref);
93 $main::lxdebug->leave_sub();
98 sub retrieve_projects {
99 $main::lxdebug->enter_sub();
101 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
103 my $dbh = $form->dbconnect($myconfig);
105 my (@filter_values, $filter);
106 if ($form->{"projectnumber"}) {
107 $filter .= qq| AND (projectnumber ILIKE ?)|;
108 push(@filter_values, '%' . $form->{"projectnumber"} . '%');
110 if ($form->{"description"}) {
111 $filter .= qq| AND (description ILIKE ?)|;
112 push(@filter_values, '%' . $form->{"description"} . '%');
114 substr($filter, 1, 3) = "WHERE" if ($filter);
116 $order_by =~ s/[^a-zA-Z_]//g;
117 $order_dir = $order_dir ? "ASC" : "DESC";
120 qq|SELECT id, projectnumber, description | .
121 qq|FROM project $filter | .
122 qq|ORDER BY $order_by $order_dir|;
123 my $sth = $dbh->prepare($query);
124 $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
126 while (my $ref = $sth->fetchrow_hashref()) {
127 push(@{$projects}, $ref);
132 $main::lxdebug->leave_sub();
137 sub retrieve_employees {
138 $main::lxdebug->enter_sub();
140 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
142 my $dbh = $form->dbconnect($myconfig);
144 my (@filter_values, $filter);
145 if ($form->{"name"}) {
146 $filter .= qq| AND (name ILIKE ?)|;
147 push(@filter_values, '%' . $form->{"name"} . '%');
149 substr($filter, 1, 3) = "WHERE" if ($filter);
151 $order_by =~ s/[^a-zA-Z_]//g;
152 $order_dir = $order_dir ? "ASC" : "DESC";
155 qq|SELECT id, name | .
156 qq|FROM employee $filter | .
157 qq|ORDER BY $order_by $order_dir|;
158 my $sth = $dbh->prepare($query);
159 $sth->execute(@filter_values) || $form->dberror($query . " (" . join(", ", @filter_values) . ")");
161 while (my $ref = $sth->fetchrow_hashref()) {
162 push(@{$employees}, $ref);
167 $main::lxdebug->leave_sub();
172 sub retrieve_customers_or_vendors {
173 $main::lxdebug->enter_sub();
175 my ($self, $myconfig, $form, $order_by, $order_dir, $is_vendor, $allow_both) = @_;
177 my $dbh = $form->dbconnect($myconfig);
179 my (@filter_values, $filter);
180 if ($form->{"name"}) {
181 $filter .= " AND (TABLE.name ILIKE ?)";
182 push(@filter_values, '%' . $form->{"name"} . '%');
184 if (!$form->{"obsolete"}) {
185 $filter .= " AND NOT TABLE.obsolete";
187 substr($filter, 1, 3) = "WHERE" if ($filter);
189 $order_by =~ s/[^a-zA-Z_]//g;
190 $order_dir = $order_dir ? "ASC" : "DESC";
192 my (@queries, @query_parameters);
194 if ($allow_both || !$is_vendor) {
195 my $c_filter = $filter;
196 $c_filter =~ s/TABLE/c/g;
197 push(@queries, qq|SELECT
198 c.id, c.name, 0 AS customer_is_vendor,
199 c.street, c.zipcode, c.city,
200 ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
202 LEFT JOIN contacts ct ON (c.id = ct.cp_cv_id)
204 push(@query_parameters, @filter_values);
207 if ($allow_both || $is_vendor) {
208 my $v_filter = $filter;
209 $v_filter =~ s/TABLE/v/g;
210 push(@queries, qq|SELECT
211 v.id, v.name, 1 AS customer_is_vendor,
212 v.street, v.zipcode, v.city,
213 ct.cp_gender, ct.cp_title, ct.cp_givenname, ct.cp_name
215 LEFT JOIN contacts ct ON (v.id = ct.cp_cv_id)
217 push(@query_parameters, @filter_values);
220 my $query = join(" UNION ", @queries) . " ORDER BY $order_by $order_dir";
221 my $sth = $dbh->prepare($query);
222 $sth->execute(@query_parameters) || $form->dberror($query . " (" . join(", ", @query_parameters) . ")");
224 while (my $ref = $sth->fetchrow_hashref()) {
225 push(@{$customers}, $ref);
230 $main::lxdebug->leave_sub();
235 sub retrieve_delivery_customer {
236 $main::lxdebug->enter_sub();
238 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
240 my $dbh = $form->dbconnect($myconfig);
242 my (@filter_values, $filter);
243 if ($form->{"name"}) {
244 $filter .= qq| (name ILIKE ?) AND|;
245 push(@filter_values, '%' . $form->{"name"} . '%');
248 $order_by =~ s/[^a-zA-Z_]//g;
249 $order_dir = $order_dir ? "ASC" : "DESC";
252 qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address ! .
254 qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Endkunde') ! .
255 qq!ORDER BY $order_by $order_dir!;
256 my $sth = $dbh->prepare($query);
257 $sth->execute(@filter_values) ||
258 $form->dberror($query . " (" . join(", ", @filter_values) . ")");
259 my $delivery_customers = [];
260 while (my $ref = $sth->fetchrow_hashref()) {
261 push(@{$delivery_customers}, $ref);
266 $main::lxdebug->leave_sub();
268 return $delivery_customers;
271 sub retrieve_vendor {
272 $main::lxdebug->enter_sub();
274 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
276 my $dbh = $form->dbconnect($myconfig);
278 my (@filter_values, $filter);
279 if ($form->{"name"}) {
280 $filter .= qq| (name ILIKE ?) AND|;
281 push(@filter_values, '%' . $form->{"name"} . '%');
284 $order_by =~ s/[^a-zA-Z_]//g;
285 $order_dir = $order_dir ? "ASC" : "DESC";
288 qq!SELECT id, name, customernumber, (street || ', ' || zipcode || city) AS address FROM customer ! .
289 qq!WHERE $filter business_id = (SELECT id FROM business WHERE description = 'Händler') ! .
290 qq!ORDER BY $order_by $order_dir!;
291 my $sth = $dbh->prepare($query);
292 $sth->execute(@filter_values) ||
293 $form->dberror($query . " (" . join(", ", @filter_values) . ")");
295 while (my $ref = $sth->fetchrow_hashref()) {
296 push(@{$vendors}, $ref);
301 $main::lxdebug->leave_sub();
306 sub mkdir_with_parents {
307 $main::lxdebug->enter_sub();
309 my ($full_path) = @_;
313 $full_path =~ s|/+|/|;
315 foreach my $part (split(m|/|, $full_path)) {
316 $path .= "/" if ($path);
319 die("Could not create directory '$path' because a file exists with " .
320 "the same name.\n") if (-f $path);
323 mkdir($path, 0770) || die("Could not create the directory '$path'. " .
328 $main::lxdebug->leave_sub();
332 $main::lxdebug->enter_sub();
336 return $main::lxdebug->leave_sub()
337 unless ($main::webdav && $form->{id});
341 $form->{WEBDAV} = [];
343 if ($form->{type} eq "sales_quotation") {
344 ($path, $number) = ("angebote", $form->{quonumber});
345 } elsif ($form->{type} eq "sales_order") {
346 ($path, $number) = ("bestellungen", $form->{ordnumber});
347 } elsif ($form->{type} eq "request_quotation") {
348 ($path, $number) = ("anfragen", $form->{quonumber});
349 } elsif ($form->{type} eq "purchase_order") {
350 ($path, $number) = ("lieferantenbestellungen", $form->{ordnumber});
351 } elsif ($form->{type} eq "sales_delivery_order") {
352 ($path, $number) = ("verkaufslieferscheine", $form->{donumber});
353 } elsif ($form->{type} eq "purchase_delivery_order") {
354 ($path, $number) = ("einkaufslieferscheine", $form->{donumber});
355 } elsif ($form->{type} eq "credit_note") {
356 ($path, $number) = ("gutschriften", $form->{invnumber});
357 } elsif ($form->{vc} eq "customer") {
358 ($path, $number) = ("rechnungen", $form->{invnumber});
360 ($path, $number) = ("einkaufsrechnungen", $form->{invnumber});
363 return $main::lxdebug->leave_sub() unless ($path && $number);
365 $number =~ s|[/\\]|_|g;
367 $path = "webdav/${path}/${number}";
370 mkdir_with_parents($path);
373 my $base_path = substr($ENV{'SCRIPT_NAME'}, 1);
374 $base_path =~ s|[^/]+$||;
375 $base_path =~ s|/$||;
376 # wo kommt der wert für dir her? es wird doch gar nichts übergeben? fix für strict my $dir jb 21.2.
377 if (opendir my $dir, $path) {
378 foreach my $file (sort { lc $a cmp lc $b } readdir $dir) {
379 next if (($file eq '.') || ($file eq '..'));
384 my $is_directory = -d "$path/$file";
386 $file = join('/', map { $form->escape($_) } grep { $_ } split m|/+|, "$path/$file");
387 $file .= '/' if ($is_directory);
389 push @{ $form->{WEBDAV} }, {
391 'link' => "$base_path/$file",
392 'type' => $is_directory ? $main::locale->text('Directory') : $main::locale->text('File'),
400 $main::lxdebug->leave_sub();
404 $main::lxdebug->enter_sub();
406 my ($self, $myconfig, $form, $vc, $vc_id) = @_;
408 $vc = $vc eq "customer" ? "customer" : "vendor";
410 my $dbh = $form->dbconnect($myconfig);
417 pt.description AS payment_terms,
418 b.description AS business,
419 l.description AS language
421 LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id)
422 LEFT JOIN business b ON (vc.business_id = b.id)
423 LEFT JOIN language l ON (vc.language_id = l.id)
425 my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id);
429 $main::lxdebug->leave_sub();
433 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
435 map { $form->{$_} = $form->format_amount($myconfig, $form->{$_} * 1) } qw(discount creditlimit);
437 $query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
438 $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
440 $query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
441 $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
445 $main::lxdebug->leave_sub();
450 sub get_shipto_by_id {
451 $main::lxdebug->enter_sub();
453 my ($self, $myconfig, $form, $shipto_id, $prefix) = @_;
457 my $dbh = $form->dbconnect($myconfig);
459 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
460 my $ref = selectfirst_hashref_query($form, $dbh, $query, $shipto_id);
462 map { $form->{"${prefix}${_}"} = $ref->{$_} } keys %{ $ref } if $ref;
466 $main::lxdebug->leave_sub();
469 sub save_email_status {
470 $main::lxdebug->enter_sub();
472 my ($self, $myconfig, $form) = @_;
474 my ($table, $query, $dbh);
476 if ($form->{script} eq 'oe.pl') {
479 } elsif ($form->{script} eq 'is.pl') {
482 } elsif ($form->{script} eq 'ir.pl') {
487 return $main::lxdebug->leave_sub() if (!$form->{id} || !$table || !$form->{formname});
489 $dbh = $form->get_standard_dbh($myconfig);
491 my ($intnotes) = selectrow_query($form, $dbh, qq|SELECT intnotes FROM $table WHERE id = ?|, $form->{id});
493 $intnotes =~ s|\r||g;
494 $intnotes =~ s|\n$||;
496 $intnotes .= "\n\n" if ($intnotes);
498 my $cc = $main::locale->text('Cc') . ": $form->{cc}\n" if $form->{cc};
499 my $bcc = $main::locale->text('Bcc') . ": $form->{bcc}\n" if $form->{bcc};
500 my $now = scalar localtime;
502 $intnotes .= $main::locale->text('[email]') . "\n"
503 . $main::locale->text('Date') . ": $now\n"
504 . $main::locale->text('To (email)') . ": $form->{email}\n"
506 . $main::locale->text('Subject') . ": $form->{subject}\n\n"
507 . $main::locale->text('Message') . ": $form->{message}";
509 $intnotes =~ s|\r||g;
511 do_query($form, $dbh, qq|UPDATE $table SET intnotes = ? WHERE id = ?|, $intnotes, $form->{id});
513 $form->save_status($dbh);
517 $main::lxdebug->leave_sub();
523 foreach my $key (@_) {
524 if ((ref $key eq '') && !defined $params->{$key}) {
525 my $subroutine = (caller(1))[3];
526 $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
527 $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
528 $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
530 } elsif (ref $key eq 'ARRAY') {
532 foreach my $subkey (@{ $key }) {
533 if (defined $params->{$subkey}) {
540 my $subroutine = (caller(1))[3];
541 $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
542 $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
543 $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
552 foreach my $key (@_) {
553 if ((ref $key eq '') && !exists $params->{$key}) {
554 my $subroutine = (caller(1))[3];
555 $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
557 } elsif (ref $key eq 'ARRAY') {
559 foreach my $subkey (@{ $key }) {
560 if (exists $params->{$subkey}) {
567 my $subroutine = (caller(1))[3];
568 $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));