Revision 88bf1c88
Von Tamino Steinert vor 2 Monaten hinzugefügt
bin/mozilla/ar.pl | ||
---|---|---|
35 | 35 |
use POSIX qw(strftime); |
36 | 36 |
use List::Util qw(sum first max); |
37 | 37 |
use List::UtilsBy qw(sort_by); |
38 |
use Archive::Zip; |
|
39 |
use Params::Validate qw(:all); |
|
40 | 38 |
|
41 | 39 |
use SL::AR; |
42 | 40 |
use SL::Controller::Base; |
... | ... | |
49 | 47 |
use SL::DB::Currency; |
50 | 48 |
use SL::DB::Default; |
51 | 49 |
use SL::DB::Employee; |
52 |
use SL::DB::Invoice; |
|
53 | 50 |
use SL::DB::Manager::Invoice; |
54 | 51 |
use SL::DB::InvoiceItem; |
55 | 52 |
use SL::DB::RecordTemplate; |
... | ... | |
62 | 59 |
use SL::Presenter::Chart; |
63 | 60 |
use SL::Presenter::ItemsList; |
64 | 61 |
use SL::ReportGenerator; |
65 |
use SL::File::Object; |
|
66 |
use SL::DB::Manager::File; |
|
67 |
use SL::File::Backend::Filesystem; |
|
68 |
use SL::Webdav; |
|
69 |
use SL::File::Backend::Webdav; |
|
70 | 62 |
|
71 | 63 |
require "bin/mozilla/common.pl"; |
72 | 64 |
require "bin/mozilla/reportgenerator.pl"; |
... | ... | |
1032 | 1024 |
action => [ $::locale->text('PDF-Export') ], |
1033 | 1025 |
action => [ |
1034 | 1026 |
t8('WebDAV'), |
1035 |
submit => [ '#report_form', { action => 'webdav_pdf_export' } ], |
|
1027 |
submit => [ '#report_form', { action => 'Invoice/webdav_pdf_export' } ],
|
|
1036 | 1028 |
checks => [ ['kivi.check_if_entries_selected', '[name="id[]"]'] ], |
1037 | 1029 |
disabled => !$webdav_enabled ? t8('WebDAV is not enabled.') |
1038 | 1030 |
: undef, |
1039 | 1031 |
], |
1040 | 1032 |
action => [ |
1041 | 1033 |
t8('Documents'), |
1042 |
submit => [ '#report_form', { action => 'files_pdf_export' } ], |
|
1034 |
submit => [ '#report_form', { action => 'Invoice/files_pdf_export' } ],
|
|
1043 | 1035 |
checks => [ ['kivi.check_if_entries_selected', '[name="id[]"]'] ], |
1044 | 1036 |
disabled => !$files_enabled ? t8('No File Management enabled.') |
1045 | 1037 |
: undef, |
... | ... | |
1656 | 1648 |
$::request->layout->add_javascripts('kivi.Validator.js'); |
1657 | 1649 |
} |
1658 | 1650 |
|
1659 |
sub _check_access_right_for_ids { |
|
1660 |
my ($ids) = @_; |
|
1661 |
$main::lxdebug->enter_sub(); |
|
1662 |
|
|
1663 |
my $form = Form->new; |
|
1664 |
AR->ar_transactions(\%::myconfig, \%$form); |
|
1665 |
my %allowed_ids = (); |
|
1666 |
|
|
1667 |
my @allowed_ar_ids = map {$_->{id}} @{$form->{AR}}; |
|
1668 |
foreach my $ar_id (@allowed_ar_ids) { |
|
1669 |
$allowed_ids{$ar_id} = 1 ; |
|
1670 |
} |
|
1671 |
foreach my $id (@$ids) { |
|
1672 |
unless ($allowed_ids{$id}) { |
|
1673 |
$::auth->deny_access(); |
|
1674 |
} |
|
1675 |
} |
|
1676 |
|
|
1677 |
$main::lxdebug->leave_sub(); |
|
1678 |
} |
|
1679 |
|
|
1680 |
sub webdav_pdf_export { |
|
1681 |
$main::lxdebug->enter_sub(); |
|
1682 |
|
|
1683 |
my $form = $main::form; |
|
1684 |
my $ids = $form->{id}; |
|
1685 |
|
|
1686 |
_check_access_right_for_ids($ids); |
|
1687 |
|
|
1688 |
my $invoices = SL::DB::Manager::Invoice->get_all(where => [ id => $ids ]); |
|
1689 |
|
|
1690 |
my @file_names_and_file_paths; |
|
1691 |
my @errors; |
|
1692 |
foreach my $invoice (@{$invoices}) { |
|
1693 |
my $record_type = $invoice->record_type; |
|
1694 |
$record_type = 'general_ledger' if $record_type eq 'ar_transaction'; |
|
1695 |
$record_type = 'invoice' if $record_type eq 'invoice_storno'; |
|
1696 |
my $webdav = SL::Webdav->new( |
|
1697 |
type => $record_type, |
|
1698 |
number => $invoice->record_number, |
|
1699 |
); |
|
1700 |
my @latest_object = $webdav->get_all_latest(); |
|
1701 |
unless (scalar @latest_object) { |
|
1702 |
push @errors, t8( |
|
1703 |
"No Dokument found for record '#1'. Please deselect it or create a document it.", |
|
1704 |
$invoice->displayable_name() |
|
1705 |
); |
|
1706 |
next; |
|
1707 |
} |
|
1708 |
push @file_names_and_file_paths, { |
|
1709 |
file_name => $latest_object[0]->basename . "." . $latest_object[0]->extension, |
|
1710 |
file_path => $latest_object[0]->full_filedescriptor(), |
|
1711 |
} |
|
1712 |
} |
|
1713 |
|
|
1714 |
if (scalar @errors) { |
|
1715 |
die join("\n", @errors); |
|
1716 |
} |
|
1717 |
_create_and_send_zip(\@file_names_and_file_paths); |
|
1718 |
|
|
1719 |
$main::lxdebug->leave_sub(); |
|
1720 |
} |
|
1721 |
|
|
1722 |
sub files_pdf_export { |
|
1723 |
$main::lxdebug->enter_sub(); |
|
1724 |
|
|
1725 |
my $form = $main::form; |
|
1726 |
my $ids = $form->{id}; |
|
1727 |
|
|
1728 |
_check_access_right_for_ids($ids); |
|
1729 |
|
|
1730 |
my $invoices = SL::DB::Manager::Invoice->get_all(where => [ id => $ids ]); |
|
1731 |
|
|
1732 |
my @file_names_and_file_paths; |
|
1733 |
my @errors; |
|
1734 |
foreach my $invoice (@{$invoices}) { |
|
1735 |
my $record_type = $invoice->record_type; |
|
1736 |
$record_type = 'invoice' if $record_type eq 'ar_transaction'; |
|
1737 |
$record_type = 'invoice' if $record_type eq 'invoice_storno'; |
|
1738 |
my @file_entries = @{SL::DB::Manager::File->get_all( |
|
1739 |
where => [ |
|
1740 |
object_type => $record_type, |
|
1741 |
object_id => $invoice->id, |
|
1742 |
file_type => 'document', |
|
1743 |
source => 'created', |
|
1744 |
], |
|
1745 |
)}; |
|
1746 |
|
|
1747 |
unless (scalar @file_entries) { |
|
1748 |
push @errors, t8( |
|
1749 |
"No Dokument found for record '#1'. Please deselect it or create a document it.", |
|
1750 |
$invoice->displayable_name() |
|
1751 |
); |
|
1752 |
next; |
|
1753 |
} |
|
1754 |
foreach my $file_entry (@file_entries) { |
|
1755 |
my $file = SL::File::Object->new( |
|
1756 |
db_file => $file_entry, |
|
1757 |
id => $file_entry->id, |
|
1758 |
loaded => 1, |
|
1759 |
); |
|
1760 |
eval { |
|
1761 |
push @file_names_and_file_paths, { |
|
1762 |
file_name => $file->file_name, |
|
1763 |
file_path => $file->get_file(), |
|
1764 |
}; |
|
1765 |
} or do { |
|
1766 |
push @errors, $@, |
|
1767 |
}; |
|
1768 |
} |
|
1769 |
} |
|
1770 |
|
|
1771 |
if (scalar @errors) { |
|
1772 |
die join("\n", @errors); |
|
1773 |
} |
|
1774 |
_create_and_send_zip(\@file_names_and_file_paths); |
|
1775 |
|
|
1776 |
$main::lxdebug->leave_sub(); |
|
1777 |
} |
|
1778 |
|
|
1779 |
sub _create_and_send_zip { |
|
1780 |
$main::lxdebug->enter_sub(); |
|
1781 |
my ($file_names_and_file_paths) = validate_pos(@_, { |
|
1782 |
type => ARRAYREF, |
|
1783 |
callbacks => { |
|
1784 |
"has 'file_name' and 'file_path'" => sub { |
|
1785 |
foreach my $file_entry (@{$_[0]}) { |
|
1786 |
return 0 unless defined $file_entry->{file_name} |
|
1787 |
&& defined $file_entry->{file_path}; |
|
1788 |
} |
|
1789 |
return 1; |
|
1790 |
} |
|
1791 |
} |
|
1792 |
}); |
|
1793 |
|
|
1794 |
my ($fh, $zipfile) = File::Temp::tempfile(); |
|
1795 |
my $zip = Archive::Zip->new(); |
|
1796 |
foreach my $file (@{$file_names_and_file_paths}) { |
|
1797 |
$zip->addFile($file->{file_path}, $file->{file_name}); |
|
1798 |
} |
|
1799 |
$zip->writeToFileHandle($fh) == Archive::Zip::AZ_OK() or die 'error writing zip file'; |
|
1800 |
close($fh); |
|
1801 |
|
|
1802 |
my $controller = SL::Controller::Base->new; |
|
1803 |
|
|
1804 |
$controller->send_file( |
|
1805 |
$zipfile, |
|
1806 |
name => t8('pdf_records.zip'), unlink => 1, |
|
1807 |
type => 'application/zip', |
|
1808 |
); |
|
1809 |
|
|
1810 |
$main::lxdebug->leave_sub(); |
|
1811 |
} |
|
1812 |
|
|
1813 |
|
|
1814 | 1651 |
1; |
Auch abrufbar als: Unified diff
FIX: Verschiebe PDF-Export für VK-Rechnungen in neuen Controller ...