1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #====================================================================
15 use Time::HiRes qw(gettimeofday);
21 my ($a, $b) = gettimeofday();
22 return "${a}-${b}-${$}";
26 return "/tmp/kivitendo-tmp-" . unique_id();
30 my ($text, %params) = @_;
33 $params{at} = 3 if 3 > $params{at};
35 $params{strip} //= '';
37 $text =~ s/[\r\n]+$//g if $params{strip} =~ m/^(?: 1 | newlines? | full )$/x;
38 $text =~ s/[\r\n]+/ /g if $params{strip} =~ m/^(?: newlines? | full )$/x;
40 return $text if length($text) <= $params{at};
41 return substr($text, 0, $params{at} - 3) . '...';
45 $main::lxdebug->enter_sub();
47 my ($self, $myconfig, $form, $order_by, $order_dir) = @_;
49 my $dbh = $form->dbconnect($myconfig);
51 my (@filter_values, $filter);
53 foreach (qw(partnumber description ean)) {
54 next unless $form->{$_};
56 $filter .= qq| AND ($_ ILIKE ?)|;
57 push @filter_values, '%' . $form->{$_} . '%';
60 if ($form->{no_assemblies}) {
61 $filter .= qq| AND (NOT COALESCE(assembly, FALSE))|;
63 if ($form->{assemblies}) {
64 $filter .= qq| AND assembly=TRUE|;
67 if ($form->{no_services}) {
68 $filter .= qq| AND (inventory_accno_id is not NULL or assembly=TRUE)|;
71 substr($filter, 1, 3) = "WHERE" if ($filter);
73 $order_by =~ s/[^a-zA-Z_]//g;
74 $order_dir = $order_dir ? "ASC" : "DESC";
77 qq|SELECT id, partnumber, description, ean, | .
78 qq| warehouse_id, bin_id | .
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 = ?') ! .
287 qq!ORDER BY $order_by $order_dir!;
288 push @filter_values, $::locale->{iconv_utf8}->convert('Händler');
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 ($::lx_office_conf{features}->{webdav} && $form->{id});
337 croak "No client set in \$::auth" unless $::auth->client;
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/" . $::auth->client->{id} . "/${path}/${number}";
370 mkdir_with_parents($path);
373 my $base_path = $ENV{'SCRIPT_NAME'};
374 $base_path =~ s|[^/]+$||;
375 if (opendir my $dir, $path) {
376 foreach my $file (sort { lc $a cmp lc $b } readdir $dir) {
377 next if (($file eq '.') || ($file eq '..'));
382 my $is_directory = -d "$path/$file";
384 $file = join('/', map { $form->escape($_) } grep { $_ } split m|/+|, "$path/$file");
385 $file .= '/' if ($is_directory);
387 push @{ $form->{WEBDAV} }, {
389 'link' => $base_path . $file,
390 'type' => $is_directory ? $main::locale->text('Directory') : $main::locale->text('File'),
398 $main::lxdebug->leave_sub();
402 $main::lxdebug->enter_sub();
404 my ($self, $myconfig, $form, $vc, $vc_id) = @_;
406 $vc = $vc eq "customer" ? "customer" : "vendor";
408 my $dbh = $form->dbconnect($myconfig);
415 pt.description AS payment_terms,
416 b.description AS business,
417 l.description AS language
419 LEFT JOIN payment_terms pt ON (vc.payment_id = pt.id)
420 LEFT JOIN business b ON (vc.business_id = b.id)
421 LEFT JOIN language l ON (vc.language_id = l.id)
423 my $ref = selectfirst_hashref_query($form, $dbh, $query, $vc_id);
427 $main::lxdebug->leave_sub();
431 map { $form->{$_} = $ref->{$_} } keys %{ $ref };
433 map { $form->{$_} = $form->format_amount($myconfig, $form->{$_} * 1) } qw(discount creditlimit);
435 $query = qq|SELECT * FROM shipto WHERE (trans_id = ?)|;
436 $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $vc_id);
438 $query = qq|SELECT * FROM contacts WHERE (cp_cv_id = ?)|;
439 $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $vc_id);
441 # Only show default pricegroup for customer, not vendor, which is why this is outside the main query
442 ($form->{pricegroup}) = selectrow_query($form, $dbh, qq|SELECT pricegroup FROM pricegroup WHERE id = ?|, $form->{klass});
446 $main::lxdebug->leave_sub();
451 sub get_shipto_by_id {
452 $main::lxdebug->enter_sub();
454 my ($self, $myconfig, $form, $shipto_id, $prefix) = @_;
458 my $dbh = $form->dbconnect($myconfig);
460 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
461 my $ref = selectfirst_hashref_query($form, $dbh, $query, $shipto_id);
463 map { $form->{"${prefix}${_}"} = $ref->{$_} } keys %{ $ref } if $ref;
467 $main::lxdebug->leave_sub();
470 sub save_email_status {
471 $main::lxdebug->enter_sub();
473 my ($self, $myconfig, $form) = @_;
475 my ($table, $query, $dbh);
477 if ($form->{script} eq 'oe.pl') {
480 } elsif ($form->{script} eq 'is.pl') {
483 } elsif ($form->{script} eq 'ir.pl') {
486 } elsif ($form->{script} eq 'do.pl') {
487 $table = 'delivery_orders';
490 return $main::lxdebug->leave_sub() if (!$form->{id} || !$table || !$form->{formname});
492 $dbh = $form->get_standard_dbh($myconfig);
494 my ($intnotes) = selectrow_query($form, $dbh, qq|SELECT intnotes FROM $table WHERE id = ?|, $form->{id});
496 $intnotes =~ s|\r||g;
497 $intnotes =~ s|\n$||;
499 $intnotes .= "\n\n" if ($intnotes);
501 my $cc = $form->{cc} ? $main::locale->text('Cc') . ": $form->{cc}\n" : '';
502 my $bcc = $form->{bcc} ? $main::locale->text('Bcc') . ": $form->{bcc}\n" : '';
503 my $now = scalar localtime;
505 $intnotes .= $main::locale->text('[email]') . "\n"
506 . $main::locale->text('Date') . ": $now\n"
507 . $main::locale->text('To (email)') . ": $form->{email}\n"
509 . $main::locale->text('Subject') . ": $form->{subject}\n\n"
510 . $main::locale->text('Message') . ": $form->{message}";
512 $intnotes =~ s|\r||g;
514 do_query($form, $dbh, qq|UPDATE $table SET intnotes = ? WHERE id = ?|, $intnotes, $form->{id});
516 $form->save_status($dbh);
520 $main::lxdebug->leave_sub();
526 foreach my $key (@_) {
527 if ((ref $key eq '') && !defined $params->{$key}) {
528 my $subroutine = (caller(1))[3];
529 $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
530 $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
531 $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
533 } elsif (ref $key eq 'ARRAY') {
535 foreach my $subkey (@{ $key }) {
536 if (defined $params->{$subkey}) {
543 my $subroutine = (caller(1))[3];
544 $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, "[Common::check_params] failed, params object dumped below");
545 $main::lxdebug->message(LXDebug->BACKTRACE_ON_ERROR, Dumper($params));
546 $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
555 foreach my $key (@_) {
556 if ((ref $key eq '') && !exists $params->{$key}) {
557 my $subroutine = (caller(1))[3];
558 $main::form->error($main::locale->text("Missing parameter #1 in call to sub #2.", $key, $subroutine));
560 } elsif (ref $key eq 'ARRAY') {
562 foreach my $subkey (@{ $key }) {
563 if (exists $params->{$subkey}) {
570 my $subroutine = (caller(1))[3];
571 $main::form->error($main::locale->text("Missing parameter (at least one of #1) in call to sub #2.", join(', ', @{ $key }), $subroutine));
586 Common - Common routines used in a lot of places.
590 my $short_text = Common::truncate($long_text, at => 10);
596 =item C<truncate $text, %params>
598 Truncates C<$text> at a position and insert an ellipsis if the text is
599 longer. The maximum number of characters to return is given with the
600 paramter C<at> which defaults to 50.
602 The optional parameter C<strip> can be used to remove unwanted line
603 feed/carriage return characters from the text before truncation. It
604 can be set to C<1> (only strip those at the end of C<$text>) or
605 C<full> (replace consecutive line feed/carriage return characters in
606 the middle by a single space and remove tailing line feed/carriage
617 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>,
618 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>